shm_open.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /* shm_open -- open a POSIX shared memory object. Generic POSIX file version.
  2. Copyright (C) 2001-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <fcntl.h>
  17. #include <not-cancel.h>
  18. #include <pthread.h>
  19. #include <shlib-compat.h>
  20. #include <shm-directory.h>
  21. #include <unistd.h>
  22. #include <sys/mman.h>
  23. /* Open shared memory object. */
  24. int
  25. __shm_open (const char *name, int oflag, mode_t mode)
  26. {
  27. struct shmdir_name dirname;
  28. int ret =__shm_get_name (&dirname, name, false);
  29. if (ret != 0)
  30. {
  31. __set_errno (ret);
  32. return -1;
  33. }
  34. oflag |= O_NOFOLLOW | O_CLOEXEC;
  35. #if defined (SHM_ANON) && defined (O_TMPFILE)
  36. if (name == SHM_ANON)
  37. oflag |= O_TMPFILE;
  38. #endif
  39. int fd = __open64_nocancel (dirname.name, oflag, mode);
  40. if (fd == -1 && __glibc_unlikely (errno == EISDIR))
  41. /* It might be better to fold this error with EINVAL since
  42. directory names are just another example for unsuitable shared
  43. object names and the standard does not mention EISDIR. */
  44. __set_errno (EINVAL);
  45. return fd;
  46. }
  47. versioned_symbol (libc, __shm_open, shm_open, GLIBC_2_34);
  48. #if OTHER_SHLIB_COMPAT (librt, GLIBC_2_2, GLIBC_2_34)
  49. compat_symbol (libc, __shm_open, shm_open, GLIBC_2_2);
  50. #endif