shm-directory.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* Determine directory for shm/sem files. Linux version.
  2. Copyright (C) 2000-2019 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. <http://www.gnu.org/licenses/>. */
  15. #include "shm-directory.h"
  16. #include <errno.h>
  17. #include <mntent.h>
  18. #include <paths.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <sys/statfs.h>
  22. #include <libc-lock.h>
  23. #include "linux_fsinfo.h"
  24. /* Mount point of the shared memory filesystem. */
  25. static struct
  26. {
  27. char *dir;
  28. size_t dirlen;
  29. } mountpoint;
  30. /* This is the default directory. */
  31. static const char defaultdir[] = "/dev/shm/";
  32. /* Protect the `mountpoint' variable above. */
  33. __libc_once_define (static, once);
  34. /* Determine where the shmfs is mounted (if at all). */
  35. static void
  36. where_is_shmfs (void)
  37. {
  38. char buf[512];
  39. struct statfs f;
  40. struct mntent resmem;
  41. struct mntent *mp;
  42. FILE *fp;
  43. /* The canonical place is /dev/shm. This is at least what the
  44. documentation tells everybody to do. */
  45. if (__statfs (defaultdir, &f) == 0 && (f.f_type == SHMFS_SUPER_MAGIC
  46. || f.f_type == RAMFS_MAGIC))
  47. {
  48. /* It is in the normal place. */
  49. mountpoint.dir = (char *) defaultdir;
  50. mountpoint.dirlen = sizeof (defaultdir) - 1;
  51. return;
  52. }
  53. /* OK, do it the hard way. Look through the /proc/mounts file and if
  54. this does not exist through /etc/fstab to find the mount point. */
  55. fp = __setmntent ("/proc/mounts", "r");
  56. if (__glibc_unlikely (fp == NULL))
  57. {
  58. fp = __setmntent (_PATH_MNTTAB, "r");
  59. if (__glibc_unlikely (fp == NULL))
  60. /* There is nothing we can do. Blind guesses are not helpful. */
  61. return;
  62. }
  63. /* Now read the entries. */
  64. while ((mp = __getmntent_r (fp, &resmem, buf, sizeof buf)) != NULL)
  65. /* The original name is "shm" but this got changed in early Linux
  66. 2.4.x to "tmpfs". */
  67. if (strcmp (mp->mnt_type, "tmpfs") == 0
  68. || strcmp (mp->mnt_type, "shm") == 0)
  69. {
  70. /* Found it. There might be more than one place where the
  71. filesystem is mounted but one is enough for us. */
  72. size_t namelen;
  73. /* First make sure this really is the correct entry. At least
  74. some versions of the kernel give wrong information because
  75. of the implicit mount of the shmfs for SysV IPC. */
  76. if (__statfs (mp->mnt_dir, &f) != 0 || (f.f_type != SHMFS_SUPER_MAGIC
  77. && f.f_type != RAMFS_MAGIC))
  78. continue;
  79. namelen = strlen (mp->mnt_dir);
  80. if (namelen == 0)
  81. /* Hum, maybe some crippled entry. Keep on searching. */
  82. continue;
  83. mountpoint.dir = (char *) malloc (namelen + 2);
  84. if (mountpoint.dir != NULL)
  85. {
  86. char *cp = __mempcpy (mountpoint.dir, mp->mnt_dir, namelen);
  87. if (cp[-1] != '/')
  88. *cp++ = '/';
  89. *cp = '\0';
  90. mountpoint.dirlen = cp - mountpoint.dir;
  91. }
  92. break;
  93. }
  94. /* Close the stream. */
  95. __endmntent (fp);
  96. }
  97. const char *
  98. __shm_directory (size_t *len)
  99. {
  100. /* Determine where the shmfs is mounted. */
  101. __libc_once (once, where_is_shmfs);
  102. /* If we don't know the mount points there is nothing we can do. Ever. */
  103. if (__glibc_unlikely (mountpoint.dir == NULL))
  104. {
  105. __set_errno (ENOSYS);
  106. return NULL;
  107. }
  108. *len = mountpoint.dirlen;
  109. return mountpoint.dir;
  110. }
  111. #if IS_IN (libpthread)
  112. hidden_def (__shm_directory)
  113. /* Make sure the table is freed if we want to free everything before
  114. exiting. */
  115. void
  116. __shm_directory_freeres (void)
  117. {
  118. if (mountpoint.dir != defaultdir)
  119. free (mountpoint.dir);
  120. }
  121. #endif