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