memfd.h 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * Copyright (C) 2018 Kovid Goyal <kovid at kovidgoyal.net>
  3. *
  4. * Distributed under terms of the GPL3 license.
  5. */
  6. #pragma once
  7. #ifdef HAS_MEMFD_CREATE
  8. #include <unistd.h>
  9. #include <sys/syscall.h>
  10. static inline int glfw_memfd_create(const char *name, unsigned int flags) {
  11. return (int)syscall(__NR_memfd_create, name, flags);
  12. }
  13. #ifndef F_LINUX_SPECIFIC_BASE
  14. #define F_LINUX_SPECIFIC_BASE 1024
  15. #endif
  16. #ifndef F_ADD_SEALS
  17. #define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
  18. #define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10)
  19. #define F_SEAL_SEAL 0x0001 /* prevent further seals from being set */
  20. #define F_SEAL_SHRINK 0x0002 /* prevent file from shrinking */
  21. #define F_SEAL_GROW 0x0004 /* prevent file from growing */
  22. #define F_SEAL_WRITE 0x0008 /* prevent writes */
  23. #endif
  24. #ifndef MFD_CLOEXEC
  25. #define MFD_CLOEXEC 0x0001U
  26. #define MFD_ALLOW_SEALING 0x0002U
  27. #endif
  28. #else
  29. #include <stdlib.h>
  30. #include <unistd.h>
  31. #include <fcntl.h>
  32. static inline int createTmpfileCloexec(char* tmpname)
  33. {
  34. int fd;
  35. fd = mkostemp(tmpname, O_CLOEXEC);
  36. if (fd >= 0)
  37. unlink(tmpname);
  38. return fd;
  39. }
  40. #endif