fchmodat.c 874 B

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