musl/src/stat/fchmodat.c
Rich Felker 7edbcbeb76 drop direct use of stat syscalls in fchmodat
instead, use the fstatat/stat functions, so that the logic for which
syscalls are present and usable is all in fstatat.

this results in a slight increase in cost for old kernels on 32-bit
archs: now statx will be attempted first rather than just using the
legacy time32 syscalls, despite us not caring about timestamps.
however, it's not even clear that the legacy syscalls *should* succeed
if the timestamps are out of range; arguably they should fail with
EOVERFLOW. as such, paying a small cost here on old kernels seems
well-motivated.

with this change, fchmodat itself is no longer blocking ports to new
archs that lack the legacy syscalls.
2022-05-01 23:25:21 -04:00

37 lines
874 B
C

#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include "syscall.h"
int fchmodat(int fd, const char *path, mode_t mode, int flag)
{
if (!flag) return syscall(SYS_fchmodat, fd, path, mode, flag);
if (flag != AT_SYMLINK_NOFOLLOW)
return __syscall_ret(-EINVAL);
struct stat st;
int ret, fd2;
char proc[15+3*sizeof(int)];
if (fstatat(fd, path, &st, flag))
return -1;
if (S_ISLNK(st.st_mode))
return __syscall_ret(-EOPNOTSUPP);
if ((fd2 = __syscall(SYS_openat, fd, path, O_RDONLY|O_PATH|O_NOFOLLOW|O_NOCTTY|O_CLOEXEC)) < 0) {
if (fd2 == -ELOOP)
return __syscall_ret(-EOPNOTSUPP);
return __syscall_ret(fd2);
}
__procfdname(proc, fd2);
ret = stat(proc, &st);
if (!ret) {
if (S_ISLNK(st.st_mode)) ret = __syscall_ret(-EOPNOTSUPP);
else ret = syscall(SYS_fchmodat, AT_FDCWD, proc, mode);
}
__syscall(SYS_close, fd2);
return ret;
}