Skip to content

Commit 758b9b8

Browse files
authored
Simplify *chown* syscalls (#16393)
This avoids musl calling the chown/lchown syscalls, and using fchown/fchownat only. This leaves less work on the syscalls layer, so it will help WasmFS. Also simplify the logic in fchown. For some reason musl tries chown on a path of /proc/fd/NNN if fchown NNN fails. It seems like both should fail at equal times? At least in our implementation of the filesystem they do, unless I'm missing something. Also add support for the nofollow flag to fchownat, which is now necessary.
1 parent b2fa2bd commit 758b9b8

File tree

5 files changed

+7
-21
lines changed

5 files changed

+7
-21
lines changed

src/library_syscall.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -744,20 +744,10 @@ var SyscallsLibrary = {
744744
var stream = SYSCALLS.getStreamFromFD(fd);
745745
return SYSCALLS.doStat(FS.stat, stream.path, buf);
746746
},
747-
__syscall_lchown32: function(path, owner, group) {
748-
path = SYSCALLS.getStr(path);
749-
FS.chown(path, owner, group); // XXX we ignore the 'l' aspect, and do the same as chown
750-
return 0;
751-
},
752747
__syscall_fchown32: function(fd, owner, group) {
753748
FS.fchown(fd, owner, group);
754749
return 0;
755750
},
756-
__syscall_chown32: function(path, owner, group) {
757-
path = SYSCALLS.getStr(path);
758-
FS.chown(path, owner, group);
759-
return 0;
760-
},
761751
__syscall_getdents64: function(fd, dirp, count) {
762752
var stream = SYSCALLS.getStreamFromFD(fd)
763753
if (!stream.getdents) {
@@ -923,11 +913,13 @@ var SyscallsLibrary = {
923913
err('warning: untested syscall');
924914
#endif
925915
path = SYSCALLS.getStr(path);
916+
var nofollow = flags & {{{ cDefine('AT_SYMLINK_NOFOLLOW') }}};
917+
flags = flags & (~{{{ cDefine('AT_SYMLINK_NOFOLLOW') }}});
926918
#if ASSERTIONS
927919
assert(flags === 0);
928920
#endif
929921
path = SYSCALLS.calculateAt(dirfd, path);
930-
FS.chown(path, owner, group);
922+
(nofollow ? FS.lchown : FS.chown)(path, owner, group);
931923
return 0;
932924
},
933925
__syscall_newfstatat: function(dirfd, path, buf, flags) {

system/lib/libc/musl/arch/emscripten/bits/syscall.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
#define SYS_stat64 __syscall_stat64
5050
#define SYS_lstat64 __syscall_lstat64
5151
#define SYS_fstat64 __syscall_fstat64
52-
#define SYS_lchown32 __syscall_lchown32
5352
#define SYS_getuid32 __syscall_getuid32
5453
#define SYS_getgid32 __syscall_getgid32
5554
#define SYS_geteuid32 __syscall_geteuid32
@@ -58,7 +57,6 @@
5857
#define SYS_fchown32 __syscall_fchown32
5958
#define SYS_getresuid32 __syscall_getresuid32
6059
#define SYS_getresgid32 __syscall_getresgid32
61-
#define SYS_chown32 __syscall_chown32
6260
#define SYS_mincore __syscall_mincore
6361
#define SYS_madvise __syscall_madvise
6462
#define SYS_getdents64 __syscall_getdents64

system/lib/libc/musl/arch/emscripten/syscall_arch.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ long __syscall_ftruncate64(long fd, long low, long high);
6565
long __syscall_stat64(long path, long buf);
6666
long __syscall_lstat64(long path, long buf);
6767
long __syscall_fstat64(long fd, long buf);
68-
long __syscall_lchown32(long path, long owner, long group);
6968
long __syscall_getuid32(void);
7069
long __syscall_getgid32(void);
7170
long __syscall_geteuid32(void);
@@ -78,7 +77,6 @@ long __syscall_setresuid32(long ruid, long euid, long suid);
7877
long __syscall_getresuid32(long ruid, long euid, long suid);
7978
long __syscall_setresgid32(long rgid, long egid, long sgid);
8079
long __syscall_getresgid32(long rgid, long egid, long sgid);
81-
long __syscall_chown32(long path, long owner, long group);
8280
long __syscall_setuid32(long uid);
8381
long __syscall_setgid32(long uid);
8482
long __syscall_mincore(long addr, long length, long vec);

system/lib/libc/musl/src/internal/syscall.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ static inline long __alt_socketcall(int sys, int sock, int cp, long a, long b, l
128128
#undef SYS_setgid
129129
#undef SYS_setfsuid
130130
#undef SYS_setfsgid
131-
#define SYS_lchown SYS_lchown32
132131
#define SYS_getuid SYS_getuid32
133132
#define SYS_getgid SYS_getgid32
134133
#define SYS_geteuid SYS_geteuid32
@@ -142,7 +141,6 @@ static inline long __alt_socketcall(int sys, int sock, int cp, long a, long b, l
142141
#define SYS_getresuid SYS_getresuid32
143142
#define SYS_setresgid SYS_setresgid32
144143
#define SYS_getresgid SYS_getresgid32
145-
#define SYS_chown SYS_chown32
146144
#define SYS_setuid SYS_setuid32
147145
#define SYS_setgid SYS_setgid32
148146
#define SYS_setfsuid SYS_setfsuid32

system/lib/libc/musl/src/unistd/fchown.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ int fchown(int fd, uid_t uid, gid_t gid)
1010
{
1111
int ret = __syscall(SYS_fchown, fd, uid, gid);
1212
#if __EMSCRIPTEN__
13-
if (ret != -EBADF || !__wasi_fd_is_valid(fd))
14-
return __syscall_ret(ret);
13+
// We can't continue onwards to try the /proc/fd/NNN approach that musl does,
14+
// as we don't support that much of POSIX.
15+
return __syscall_ret(ret);
1516
#else
1617
if (ret != -EBADF || __syscall(SYS_fcntl, fd, F_GETFD) < 0)
1718
return __syscall_ret(ret);
18-
#endif
1919

2020
char buf[15+3*sizeof(int)];
2121
__procfdname(buf, fd);
@@ -24,5 +24,5 @@ int fchown(int fd, uid_t uid, gid_t gid)
2424
#else
2525
return syscall(SYS_fchownat, AT_FDCWD, buf, uid, gid, 0);
2626
#endif
27-
27+
#endif // EMSCRIPTEN
2828
}

0 commit comments

Comments
 (0)