9p-util.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * 9p utilities
  3. *
  4. * Copyright IBM, Corp. 2017
  5. *
  6. * Authors:
  7. * Greg Kurz <groug@kaod.org>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  10. * See the COPYING file in the top-level directory.
  11. */
  12. #ifndef QEMU_9P_UTIL_H
  13. #define QEMU_9P_UTIL_H
  14. #ifdef O_PATH
  15. #define O_PATH_9P_UTIL O_PATH
  16. #else
  17. #define O_PATH_9P_UTIL 0
  18. #endif
  19. static inline void close_preserve_errno(int fd)
  20. {
  21. int serrno = errno;
  22. close(fd);
  23. errno = serrno;
  24. }
  25. static inline int openat_dir(int dirfd, const char *name)
  26. {
  27. return openat(dirfd, name,
  28. O_DIRECTORY | O_RDONLY | O_NOFOLLOW | O_PATH_9P_UTIL);
  29. }
  30. static inline int openat_file(int dirfd, const char *name, int flags,
  31. mode_t mode)
  32. {
  33. int fd, serrno, ret;
  34. fd = openat(dirfd, name, flags | O_NOFOLLOW | O_NOCTTY | O_NONBLOCK,
  35. mode);
  36. if (fd == -1) {
  37. return -1;
  38. }
  39. serrno = errno;
  40. /* O_NONBLOCK was only needed to open the file. Let's drop it. We don't
  41. * do that with O_PATH since fcntl(F_SETFL) isn't supported, and openat()
  42. * ignored it anyway.
  43. */
  44. if (!(flags & O_PATH_9P_UTIL)) {
  45. ret = fcntl(fd, F_SETFL, flags);
  46. assert(!ret);
  47. }
  48. errno = serrno;
  49. return fd;
  50. }
  51. ssize_t fgetxattrat_nofollow(int dirfd, const char *path, const char *name,
  52. void *value, size_t size);
  53. int fsetxattrat_nofollow(int dirfd, const char *path, const char *name,
  54. void *value, size_t size, int flags);
  55. ssize_t flistxattrat_nofollow(int dirfd, const char *filename,
  56. char *list, size_t size);
  57. ssize_t fremovexattrat_nofollow(int dirfd, const char *filename,
  58. const char *name);
  59. #endif