sem_open.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /* Copyright (C) 2002, 2003, 2006, 2007, 2009 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
  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 <errno.h>
  16. #include <fcntl.h>
  17. #include <mntent.h>
  18. #include <paths.h>
  19. #include <pthread.h>
  20. #include <search.h>
  21. #include <semaphore.h>
  22. #include <stdarg.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <unistd.h>
  27. #include <sys/mman.h>
  28. #include <sys/stat.h>
  29. #include <sys/statfs.h>
  30. #include <linux_fsinfo.h>
  31. #include "semaphoreP.h"
  32. #include "../../libc/misc/internals/tempname.h"
  33. /* Compatibility defines. */
  34. #define __endmntent endmntent
  35. #define __getmntent_r getmntent_r
  36. #define __setmntent setmntent
  37. #define __statfs statfs
  38. #define __libc_close close
  39. #define __libc_open open64
  40. #define __fxstat64(vers, fd, buf) fstat64(fd, buf)
  41. #define __libc_write write
  42. /* Information about the mount point. */
  43. struct mountpoint_info mountpoint attribute_hidden;
  44. /* This is the default mount point. */
  45. static const char defaultmount[] = "/dev/shm";
  46. /* This is the default directory. */
  47. static const char defaultdir[] = "/dev/shm/sem.";
  48. /* Protect the `mountpoint' variable above. */
  49. pthread_once_t __namedsem_once attribute_hidden = PTHREAD_ONCE_INIT;
  50. /* Determine where the shmfs is mounted (if at all). */
  51. void
  52. attribute_hidden
  53. __where_is_shmfs (void)
  54. {
  55. char buf[512];
  56. struct statfs f;
  57. struct mntent resmem;
  58. struct mntent *mp;
  59. FILE *fp;
  60. /* The canonical place is /dev/shm. This is at least what the
  61. documentation tells everybody to do. */
  62. if (__statfs (defaultmount, &f) == 0
  63. && (f.f_type == SHMFS_SUPER_MAGIC_WITH_MMU
  64. || f.f_type == SHMFS_SUPER_MAGIC_WITHOUT_MMU))
  65. {
  66. /* It is in the normal place. */
  67. mountpoint.dir = (char *) defaultdir;
  68. mountpoint.dirlen = sizeof (defaultdir) - 1;
  69. return;
  70. }
  71. /* OK, do it the hard way. Look through the /proc/mounts file and if
  72. this does not exist through /etc/fstab to find the mount point. */
  73. fp = __setmntent ("/proc/mounts", "r");
  74. if (unlikely (fp == NULL))
  75. {
  76. fp = __setmntent (_PATH_MNTTAB, "r");
  77. if (unlikely (fp == NULL))
  78. /* There is nothing we can do. Blind guesses are not helpful. */
  79. return;
  80. }
  81. /* Now read the entries. */
  82. while ((mp = __getmntent_r (fp, &resmem, buf, sizeof buf)) != NULL)
  83. /* The original name is "shm" but this got changed in early Linux
  84. 2.4.x to "tmpfs". */
  85. if (strcmp (mp->mnt_type, "tmpfs") == 0
  86. || strcmp (mp->mnt_type, "shm") == 0)
  87. {
  88. /* Found it. There might be more than one place where the
  89. filesystem is mounted but one is enough for us. */
  90. size_t namelen;
  91. /* First make sure this really is the correct entry. At least
  92. some versions of the kernel give wrong information because
  93. of the implicit mount of the shmfs for SysV IPC. */
  94. if (__statfs (mp->mnt_dir, &f) != 0
  95. || (f.f_type != SHMFS_SUPER_MAGIC_WITH_MMU
  96. && f.f_type != SHMFS_SUPER_MAGIC_WITHOUT_MMU))
  97. continue;
  98. namelen = strlen (mp->mnt_dir);
  99. if (namelen == 0)
  100. /* Hum, maybe some crippled entry. Keep on searching. */
  101. continue;
  102. mountpoint.dir = (char *) malloc (namelen + 4 + 2);
  103. if (mountpoint.dir != NULL)
  104. {
  105. char *cp = mempcpy (mountpoint.dir, mp->mnt_dir, namelen);
  106. if (cp[-1] != '/')
  107. *cp++ = '/';
  108. cp = stpcpy (cp, "sem.");
  109. mountpoint.dirlen = cp - mountpoint.dir;
  110. }
  111. break;
  112. }
  113. /* Close the stream. */
  114. __endmntent (fp);
  115. }
  116. /* Comparison function for search of existing mapping. */
  117. int
  118. attribute_hidden
  119. __sem_search (const void *a, const void *b)
  120. {
  121. const struct inuse_sem *as = (const struct inuse_sem *) a;
  122. const struct inuse_sem *bs = (const struct inuse_sem *) b;
  123. if (as->ino != bs->ino)
  124. /* Cannot return the difference the type is larger than int. */
  125. return as->ino < bs->ino ? -1 : 1;
  126. if (as->dev != bs->dev)
  127. /* Cannot return the difference the type is larger than int. */
  128. return as->dev < bs->dev ? -1 : 1;
  129. return strcmp (as->name, bs->name);
  130. }
  131. /* The search tree for existing mappings. */
  132. void *__sem_mappings attribute_hidden;
  133. /* Lock to protect the search tree. */
  134. int __sem_mappings_lock attribute_hidden = LLL_LOCK_INITIALIZER;
  135. /* Search for existing mapping and if possible add the one provided. */
  136. static sem_t *
  137. check_add_mapping (const char *name, size_t namelen, int fd, sem_t *existing)
  138. {
  139. sem_t *result = SEM_FAILED;
  140. /* Get the information about the file. */
  141. struct stat64 st;
  142. if (__fxstat64 (_STAT_VER, fd, &st) == 0)
  143. {
  144. /* Get the lock. */
  145. lll_lock (__sem_mappings_lock, LLL_PRIVATE);
  146. /* Search for an existing mapping given the information we have. */
  147. struct inuse_sem *fake;
  148. fake = (struct inuse_sem *) alloca (sizeof (*fake) + namelen);
  149. memcpy (fake->name, name, namelen);
  150. fake->dev = st.st_dev;
  151. fake->ino = st.st_ino;
  152. struct inuse_sem **foundp = tfind (fake, &__sem_mappings, __sem_search);
  153. if (foundp != NULL)
  154. {
  155. /* There is already a mapping. Use it. */
  156. result = (*foundp)->sem;
  157. ++(*foundp)->refcnt;
  158. }
  159. else
  160. {
  161. /* We haven't found a mapping. Install ione. */
  162. struct inuse_sem *newp;
  163. newp = (struct inuse_sem *) malloc (sizeof (*newp) + namelen);
  164. if (newp != NULL)
  165. {
  166. /* If the caller hasn't provided any map it now. */
  167. if (existing == SEM_FAILED)
  168. existing = (sem_t *) mmap (NULL, sizeof (sem_t),
  169. PROT_READ | PROT_WRITE, MAP_SHARED,
  170. fd, 0);
  171. newp->dev = st.st_dev;
  172. newp->ino = st.st_ino;
  173. newp->refcnt = 1;
  174. newp->sem = existing;
  175. memcpy (newp->name, name, namelen);
  176. /* Insert the new value. */
  177. if (existing != MAP_FAILED
  178. && tsearch (newp, &__sem_mappings, __sem_search) != NULL)
  179. /* Successful. */
  180. result = existing;
  181. else
  182. /* Something went wrong while inserting the new
  183. value. We fail completely. */
  184. free (newp);
  185. }
  186. }
  187. /* Release the lock. */
  188. lll_unlock (__sem_mappings_lock, LLL_PRIVATE);
  189. }
  190. if (result != existing && existing != SEM_FAILED && existing != MAP_FAILED)
  191. {
  192. /* Do not disturb errno. */
  193. INTERNAL_SYSCALL_DECL (err);
  194. INTERNAL_SYSCALL (munmap, err, 2, existing, sizeof (sem_t));
  195. }
  196. return result;
  197. }
  198. sem_t *
  199. sem_open (const char *name, int oflag, ...)
  200. {
  201. char *finalname;
  202. sem_t *result = SEM_FAILED;
  203. int fd;
  204. /* Determine where the shmfs is mounted. */
  205. INTUSE(__pthread_once) (&__namedsem_once, __where_is_shmfs);
  206. /* If we don't know the mount points there is nothing we can do. Ever. */
  207. if (mountpoint.dir == NULL)
  208. {
  209. __set_errno (ENOSYS);
  210. return SEM_FAILED;
  211. }
  212. /* Construct the filename. */
  213. while (name[0] == '/')
  214. ++name;
  215. if (name[0] == '\0')
  216. {
  217. /* The name "/" is not supported. */
  218. __set_errno (EINVAL);
  219. return SEM_FAILED;
  220. }
  221. size_t namelen = strlen (name) + 1;
  222. /* Create the name of the final file. */
  223. finalname = (char *) alloca (mountpoint.dirlen + namelen);
  224. mempcpy (mempcpy (finalname, mountpoint.dir, mountpoint.dirlen),
  225. name, namelen);
  226. /* If the semaphore object has to exist simply open it. */
  227. if ((oflag & O_CREAT) == 0 || (oflag & O_EXCL) == 0)
  228. {
  229. try_again:
  230. fd = __libc_open (finalname,
  231. (oflag & ~(O_CREAT|O_ACCMODE)) | O_NOFOLLOW | O_RDWR);
  232. if (fd == -1)
  233. {
  234. /* If we are supposed to create the file try this next. */
  235. if ((oflag & O_CREAT) != 0 && errno == ENOENT)
  236. goto try_create;
  237. /* Return. errno is already set. */
  238. }
  239. else
  240. /* Check whether we already have this semaphore mapped and
  241. create one if necessary. */
  242. result = check_add_mapping (name, namelen, fd, SEM_FAILED);
  243. }
  244. else
  245. {
  246. /* We have to open a temporary file first since it must have the
  247. correct form before we can start using it. */
  248. char *tmpfname;
  249. mode_t mode;
  250. unsigned int value;
  251. va_list ap;
  252. try_create:
  253. va_start (ap, oflag);
  254. mode = va_arg (ap, mode_t);
  255. value = va_arg (ap, unsigned int);
  256. va_end (ap);
  257. if (value > SEM_VALUE_MAX)
  258. {
  259. __set_errno (EINVAL);
  260. return SEM_FAILED;
  261. }
  262. /* Create the initial file content. */
  263. union
  264. {
  265. sem_t initsem;
  266. struct new_sem newsem;
  267. } sem;
  268. sem.newsem.value = value;
  269. sem.newsem.private = 0;
  270. sem.newsem.nwaiters = 0;
  271. /* Initialize the remaining bytes as well. */
  272. memset ((char *) &sem.initsem + sizeof (struct new_sem), '\0',
  273. sizeof (sem_t) - sizeof (struct new_sem));
  274. tmpfname = (char *) alloca (mountpoint.dirlen + 6 + 1);
  275. mempcpy (mempcpy (tmpfname, mountpoint.dir, mountpoint.dirlen),
  276. "XXXXXX", 7);
  277. fd = __gen_tempname (tmpfname, __GT_FILE, 0, 0, mode);
  278. if (fd == -1)
  279. return SEM_FAILED;
  280. if (TEMP_FAILURE_RETRY (__libc_write (fd, &sem.initsem, sizeof (sem_t)))
  281. == sizeof (sem_t)
  282. /* Map the sem_t structure from the file. */
  283. && (result = (sem_t *) mmap (NULL, sizeof (sem_t),
  284. PROT_READ | PROT_WRITE, MAP_SHARED,
  285. fd, 0)) != MAP_FAILED)
  286. {
  287. /* Create the file. Don't overwrite an existing file. */
  288. if (link (tmpfname, finalname) != 0)
  289. {
  290. /* Undo the mapping. */
  291. (void) munmap (result, sizeof (sem_t));
  292. /* Reinitialize 'result'. */
  293. result = SEM_FAILED;
  294. /* This failed. If O_EXCL is not set and the problem was
  295. that the file exists, try again. */
  296. if ((oflag & O_EXCL) == 0 && errno == EEXIST)
  297. {
  298. /* Remove the file. */
  299. (void) unlink (tmpfname);
  300. /* Close the file. */
  301. (void) __libc_close (fd);
  302. goto try_again;
  303. }
  304. }
  305. else
  306. /* Insert the mapping into the search tree. This also
  307. determines whether another thread sneaked by and already
  308. added such a mapping despite the fact that we created it. */
  309. result = check_add_mapping (name, namelen, fd, result);
  310. }
  311. /* Now remove the temporary name. This should never fail. If
  312. it fails we leak a file name. Better fix the kernel. */
  313. (void) unlink (tmpfname);
  314. }
  315. /* Map the mmap error to the error we need. */
  316. if (MAP_FAILED != (void *) SEM_FAILED && result == MAP_FAILED)
  317. result = SEM_FAILED;
  318. /* We don't need the file descriptor anymore. */
  319. if (fd != -1)
  320. {
  321. /* Do not disturb errno. */
  322. INTERNAL_SYSCALL_DECL (err);
  323. INTERNAL_SYSCALL (close, err, 1, fd);
  324. }
  325. return result;
  326. }