sem_open.c 11 KB

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