sem_open.c 11 KB

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