master
 1#include <sys/stat.h>
 2#include <fcntl.h>
 3#include <errno.h>
 4#include "syscall.h"
 5
 6int fchmodat(int fd, const char *path, mode_t mode, int flag)
 7{
 8	if (!flag) return syscall(SYS_fchmodat, fd, path, mode);
 9
10	int ret = __syscall(SYS_fchmodat2, fd, path, mode, flag);
11	if (ret != -ENOSYS) return __syscall_ret(ret);
12
13	if (flag != AT_SYMLINK_NOFOLLOW)
14		return __syscall_ret(-EINVAL);
15
16	struct stat st;
17	int fd2;
18	char proc[15+3*sizeof(int)];
19
20	if (fstatat(fd, path, &st, flag))
21		return -1;
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|O_CLOEXEC)) < 0) {
26		if (fd2 == -ELOOP)
27			return __syscall_ret(-EOPNOTSUPP);
28		return __syscall_ret(fd2);
29	}
30
31	__procfdname(proc, fd2);
32	ret = stat(proc, &st);
33	if (!ret) {
34		if (S_ISLNK(st.st_mode)) ret = __syscall_ret(-EOPNOTSUPP);
35		else ret = syscall(SYS_fchmodat, AT_FDCWD, proc, mode);
36	}
37
38	__syscall(SYS_close, fd2);
39	return ret;
40}