xref: /third_party/musl/src/stat/fchmodat.c (revision 570af302)
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	int ret;
10#ifdef SYS_fchmodat2
11	ret = __syscall(SYS_fchmodat2, fd, path, mode, flag);
12	if (ret != -ENOSYS) return __syscall_ret(ret);
13#endif
14
15	if (flag != AT_SYMLINK_NOFOLLOW)
16		return __syscall_ret(-EINVAL);
17
18	struct stat st;
19	int fd2;
20	char proc[15+3*sizeof(int)];
21
22	if (fstatat(fd, path, &st, flag))
23		return -1;
24	if (S_ISLNK(st.st_mode))
25		return __syscall_ret(-EOPNOTSUPP);
26
27	if ((fd2 = __syscall(SYS_openat, fd, path, O_RDONLY|O_PATH|O_NOFOLLOW|O_NOCTTY|O_CLOEXEC)) < 0) {
28		if (fd2 == -ELOOP)
29			return __syscall_ret(-EOPNOTSUPP);
30		return __syscall_ret(fd2);
31	}
32
33	__procfdname(proc, fd2);
34	ret = stat(proc, &st);
35	if (!ret) {
36		if (S_ISLNK(st.st_mode)) ret = __syscall_ret(-EOPNOTSUPP);
37		else ret = syscall(SYS_fchmodat, AT_FDCWD, proc, mode);
38	}
39
40	__syscall(SYS_close, fd2);
41	return ret;
42}
43