sem_open.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. #ifdef __UCLIBC_HAS_LFS__
  40. # define __libc_open open64
  41. # define __fxstat64(vers, fd, buf) fstat64(fd, buf)
  42. #else
  43. # define __libc_open open
  44. # define __fxstat64(vers, fd, buf) fstat(fd, buf)
  45. # define stat64 stat
  46. #endif
  47. #define __libc_write write
  48. /* Information about the mount point. */
  49. struct mountpoint_info mountpoint attribute_hidden;
  50. /* This is the default mount point. */
  51. static const char defaultmount[] = "/dev/shm";
  52. /* This is the default directory. */
  53. static const char defaultdir[] = "/dev/shm/sem.";
  54. /* Protect the `mountpoint' variable above. */
  55. pthread_once_t __namedsem_once attribute_hidden = PTHREAD_ONCE_INIT;
  56. /* Determine where the shmfs is mounted (if at all). */
  57. void
  58. attribute_hidden
  59. __where_is_shmfs (void)
  60. {
  61. char buf[512];
  62. struct statfs f;
  63. struct mntent resmem;
  64. struct mntent *mp;
  65. FILE *fp;
  66. /* The canonical place is /dev/shm. This is at least what the
  67. documentation tells everybody to do. */
  68. if (__statfs (defaultmount, &f) == 0 && f.f_type == SHMFS_SUPER_MAGIC)
  69. {
  70. /* It is in the normal place. */
  71. mountpoint.dir = (char *) defaultdir;
  72. mountpoint.dirlen = sizeof (defaultdir) - 1;
  73. return;
  74. }
  75. /* OK, do it the hard way. Look through the /proc/mounts file and if
  76. this does not exist through /etc/fstab to find the mount point. */
  77. fp = __setmntent ("/proc/mounts", "r");
  78. if (unlikely (fp == NULL))
  79. {
  80. fp = __setmntent (_PATH_MNTTAB, "r");
  81. if (unlikely (fp == NULL))
  82. /* There is nothing we can do. Blind guesses are not helpful. */
  83. return;
  84. }
  85. /* Now read the entries. */
  86. while ((mp = __getmntent_r (fp, &resmem, buf, sizeof buf)) != NULL)
  87. /* The original name is "shm" but this got changed in early Linux
  88. 2.4.x to "tmpfs". */
  89. if (strcmp (mp->mnt_type, "tmpfs") == 0
  90. || strcmp (mp->mnt_type, "shm") == 0)
  91. {
  92. /* Found it. There might be more than one place where the
  93. filesystem is mounted but one is enough for us. */
  94. size_t namelen;
  95. /* First make sure this really is the correct entry. At least
  96. some versions of the kernel give wrong information because
  97. of the implicit mount of the shmfs for SysV IPC. */
  98. if (__statfs (mp->mnt_dir, &f) != 0 || f.f_type != SHMFS_SUPER_MAGIC)
  99. continue;
  100. namelen = strlen (mp->mnt_dir);
  101. if (namelen == 0)
  102. /* Hum, maybe some crippled entry. Keep on searching. */
  103. continue;
  104. mountpoint.dir = (char *) malloc (namelen + 4 + 2);
  105. if (mountpoint.dir != NULL)
  106. {
  107. char *cp = mempcpy (mountpoint.dir, mp->mnt_dir, namelen);
  108. if (cp[-1] != '/')
  109. *cp++ = '/';
  110. cp = stpcpy (cp, "sem.");
  111. mountpoint.dirlen = cp - mountpoint.dir;
  112. }
  113. break;
  114. }
  115. /* Close the stream. */
  116. __endmntent (fp);
  117. }
  118. /* Comparison function for search of existing mapping. */
  119. int
  120. attribute_hidden
  121. __sem_search (const void *a, const void *b)
  122. {
  123. const struct inuse_sem *as = (const struct inuse_sem *) a;
  124. const struct inuse_sem *bs = (const struct inuse_sem *) b;
  125. if (as->ino != bs->ino)
  126. /* Cannot return the difference the type is larger than int. */
  127. return as->ino < bs->ino ? -1 : (as->ino == bs->ino ? 0 : 1);
  128. if (as->dev != bs->dev)
  129. /* Cannot return the difference the type is larger than int. */
  130. return as->dev < bs->dev ? -1 : (as->dev == bs->dev ? 0 : 1);
  131. return strcmp (as->name, bs->name);
  132. }
  133. /* The search tree for existing mappings. */
  134. void *__sem_mappings attribute_hidden;
  135. /* Lock to protect the search tree. */
  136. int __sem_mappings_lock attribute_hidden = LLL_LOCK_INITIALIZER;
  137. /* Search for existing mapping and if possible add the one provided. */
  138. static sem_t *
  139. check_add_mapping (const char *name, size_t namelen, int fd, sem_t *existing)
  140. {
  141. sem_t *result = SEM_FAILED;
  142. /* Get the information about the file. */
  143. struct stat64 st;
  144. if (__fxstat64 (_STAT_VER, fd, &st) == 0)
  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. INTUSE(__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 (mempcpy (tmpfname, mountpoint.dir, mountpoint.dirlen),
  278. "XXXXXX", 7);
  279. fd = __gen_tempname (tmpfname, __GT_FILE, 0, mode);
  280. if (fd == -1)
  281. return SEM_FAILED;
  282. if (TEMP_FAILURE_RETRY (__libc_write (fd, &sem.initsem, sizeof (sem_t)))
  283. == sizeof (sem_t)
  284. /* Map the sem_t structure from the file. */
  285. && (result = (sem_t *) mmap (NULL, sizeof (sem_t),
  286. PROT_READ | PROT_WRITE, MAP_SHARED,
  287. fd, 0)) != MAP_FAILED)
  288. {
  289. /* Create the file. Don't overwrite an existing file. */
  290. if (link (tmpfname, finalname) != 0)
  291. {
  292. /* Undo the mapping. */
  293. (void) munmap (result, sizeof (sem_t));
  294. /* Reinitialize 'result'. */
  295. result = SEM_FAILED;
  296. /* This failed. If O_EXCL is not set and the problem was
  297. that the file exists, try again. */
  298. if ((oflag & O_EXCL) == 0 && errno == EEXIST)
  299. {
  300. /* Remove the file. */
  301. (void) unlink (tmpfname);
  302. /* Close the file. */
  303. (void) __libc_close (fd);
  304. goto try_again;
  305. }
  306. }
  307. else
  308. /* Insert the mapping into the search tree. This also
  309. determines whether another thread sneaked by and already
  310. added such a mapping despite the fact that we created it. */
  311. result = check_add_mapping (name, namelen, fd, result);
  312. }
  313. /* Now remove the temporary name. This should never fail. If
  314. it fails we leak a file name. Better fix the kernel. */
  315. (void) unlink (tmpfname);
  316. }
  317. /* Map the mmap error to the error we need. */
  318. if (MAP_FAILED != (void *) SEM_FAILED && result == MAP_FAILED)
  319. result = SEM_FAILED;
  320. /* We don't need the file descriptor anymore. */
  321. if (fd != -1)
  322. {
  323. /* Do not disturb errno. */
  324. INTERNAL_SYSCALL_DECL (err);
  325. INTERNAL_SYSCALL (close, err, 1, fd);
  326. }
  327. return result;
  328. }