sys/stat.h: fix typo in statx member name stx_dio_offset_align
[musl.git] / src / unistd / faccessat.c
blob43052dd70eeeb4a2b5e8a009b395e9b14eebbc50
1 #include <unistd.h>
2 #include <fcntl.h>
3 #include <sys/wait.h>
4 #include "syscall.h"
5 #include "pthread_impl.h"
7 struct ctx {
8 int fd;
9 const char *filename;
10 int amode;
11 int p;
14 static int checker(void *p)
16 struct ctx *c = p;
17 int ret;
18 if (__syscall(SYS_setregid, __syscall(SYS_getegid), -1)
19 || __syscall(SYS_setreuid, __syscall(SYS_geteuid), -1))
20 __syscall(SYS_exit, 1);
21 ret = __syscall(SYS_faccessat, c->fd, c->filename, c->amode, 0);
22 __syscall(SYS_write, c->p, &ret, sizeof ret);
23 return 0;
26 int faccessat(int fd, const char *filename, int amode, int flag)
28 if (flag) {
29 int ret = __syscall(SYS_faccessat2, fd, filename, amode, flag);
30 if (ret != -ENOSYS) return __syscall_ret(ret);
33 if (flag & ~AT_EACCESS)
34 return __syscall_ret(-EINVAL);
36 if (!flag || (getuid()==geteuid() && getgid()==getegid()))
37 return syscall(SYS_faccessat, fd, filename, amode);
39 char stack[1024];
40 sigset_t set;
41 pid_t pid;
42 int status;
43 int ret, p[2];
45 if (pipe2(p, O_CLOEXEC)) return __syscall_ret(-EBUSY);
46 struct ctx c = { .fd = fd, .filename = filename, .amode = amode, .p = p[1] };
48 __block_all_sigs(&set);
50 pid = __clone(checker, stack+sizeof stack, 0, &c);
51 __syscall(SYS_close, p[1]);
53 if (pid<0 || __syscall(SYS_read, p[0], &ret, sizeof ret) != sizeof(ret))
54 ret = -EBUSY;
55 __syscall(SYS_close, p[0]);
56 __sys_wait4(pid, &status, __WCLONE, 0);
58 __restore_sigs(&set);
60 return __syscall_ret(ret);