debloat code that depends on /proc/self/fd/%d with shared function
[musl] / src / stat / fchmodat.c
1 #include <sys/stat.h>
2 #include <fcntl.h>
3 #include <errno.h>
4 #include <stdio.h>
5 #include "syscall.h"
6
7 void __procfdname(char *, unsigned);
8
9 int fchmodat(int fd, const char *path, mode_t mode, int flag)
10 {
11         if (!flag) return syscall(SYS_fchmodat, fd, path, mode, flag);
12
13         if (flag != AT_SYMLINK_NOFOLLOW)
14                 return __syscall_ret(-EINVAL);
15
16         struct stat st;
17         int ret, fd2;
18         char proc[15+3*sizeof(int)];
19
20         if ((ret = __syscall(SYS_fstatat, fd, path, &st, flag)))
21                 return __syscall_ret(ret);
22         if (S_ISLNK(st.st_mode))
23                 return __syscall_ret(-EOPNOTSUPP);
24
25         if ((fd2 = __syscall(SYS_openat, fd, path, O_RDONLY|O_PATH|O_NOFOLLOW|O_NOCTTY)) < 0) {
26                 if (fd2 == -ELOOP)
27                         return __syscall_ret(-EOPNOTSUPP);
28                 return __syscall_ret(fd2);
29         }
30
31         __procfdname(proc, fd2);
32         if (!(ret = __syscall(SYS_stat, proc, &st)) && !S_ISLNK(st.st_mode))
33                 ret = __syscall(SYS_chmod, proc, mode);
34
35         __syscall(SYS_close, fd2);
36         return __syscall_ret(ret);
37 }