1 /* Copyright (C) 2002-2014 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
25 #include <semaphore.h>
33 #include <sys/statfs.h>
34 #include <linux_fsinfo.h>
35 #include "semaphoreP.h"
39 /* Information about the mount point. */
40 struct mountpoint_info mountpoint attribute_hidden
;
42 /* This is the default mount point. */
43 static const char defaultmount
[] = "/dev/shm";
44 /* This is the default directory. */
45 static const char defaultdir
[] = "/dev/shm/sem.";
47 /* Protect the `mountpoint' variable above. */
48 pthread_once_t __namedsem_once attribute_hidden
= PTHREAD_ONCE_INIT
;
51 /* Determine where the shmfs is mounted (if at all). */
54 __where_is_shmfs (void)
62 /* The canonical place is /dev/shm. This is at least what the
63 documentation tells everybody to do. */
64 if (__statfs (defaultmount
, &f
) == 0 && f
.f_type
== SHMFS_SUPER_MAGIC
)
66 /* It is in the normal place. */
67 mountpoint
.dir
= (char *) defaultdir
;
68 mountpoint
.dirlen
= sizeof (defaultdir
) - 1;
73 /* OK, do it the hard way. Look through the /proc/mounts file and if
74 this does not exist through /etc/fstab to find the mount point. */
75 fp
= __setmntent ("/proc/mounts", "r");
76 if (__builtin_expect (fp
== NULL
, 0))
78 fp
= __setmntent (_PATH_MNTTAB
, "r");
79 if (__builtin_expect (fp
== NULL
, 0))
80 /* There is nothing we can do. Blind guesses are not helpful. */
84 /* Now read the entries. */
85 while ((mp
= __getmntent_r (fp
, &resmem
, buf
, sizeof buf
)) != NULL
)
86 /* The original name is "shm" but this got changed in early Linux
88 if (strcmp (mp
->mnt_type
, "tmpfs") == 0
89 || strcmp (mp
->mnt_type
, "shm") == 0)
91 /* Found it. There might be more than one place where the
92 filesystem is mounted but one is enough for us. */
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
)
101 namelen
= strlen (mp
->mnt_dir
);
104 /* Hum, maybe some crippled entry. Keep on searching. */
107 mountpoint
.dir
= (char *) malloc (namelen
+ 4 + 2);
108 if (mountpoint
.dir
!= NULL
)
110 char *cp
= __mempcpy (mountpoint
.dir
, mp
->mnt_dir
, namelen
);
113 cp
= stpcpy (cp
, "sem.");
114 mountpoint
.dirlen
= cp
- mountpoint
.dir
;
120 /* Close the stream. */
125 /* Comparison function for search of existing mapping. */
128 __sem_search (const void *a
, const void *b
)
130 const struct inuse_sem
*as
= (const struct inuse_sem
*) a
;
131 const struct inuse_sem
*bs
= (const struct inuse_sem
*) b
;
133 if (as
->ino
!= bs
->ino
)
134 /* Cannot return the difference the type is larger than int. */
135 return as
->ino
< bs
->ino
? -1 : (as
->ino
== bs
->ino
? 0 : 1);
137 if (as
->dev
!= bs
->dev
)
138 /* Cannot return the difference the type is larger than int. */
139 return as
->dev
< bs
->dev
? -1 : (as
->dev
== bs
->dev
? 0 : 1);
141 return strcmp (as
->name
, bs
->name
);
145 /* The search tree for existing mappings. */
146 void *__sem_mappings attribute_hidden
;
148 /* Lock to protect the search tree. */
149 int __sem_mappings_lock attribute_hidden
= LLL_LOCK_INITIALIZER
;
152 /* Search for existing mapping and if possible add the one provided. */
154 check_add_mapping (const char *name
, size_t namelen
, int fd
, sem_t
*existing
)
156 sem_t
*result
= SEM_FAILED
;
158 /* Get the information about the file. */
160 if (__fxstat64 (_STAT_VER
, fd
, &st
) == 0)
163 lll_lock (__sem_mappings_lock
, LLL_PRIVATE
);
165 /* Search for an existing mapping given the information we have. */
166 struct inuse_sem
*fake
;
167 fake
= (struct inuse_sem
*) alloca (sizeof (*fake
) + namelen
);
168 memcpy (fake
->name
, name
, namelen
);
169 fake
->dev
= st
.st_dev
;
170 fake
->ino
= st
.st_ino
;
172 struct inuse_sem
**foundp
= tfind (fake
, &__sem_mappings
, __sem_search
);
175 /* There is already a mapping. Use it. */
176 result
= (*foundp
)->sem
;
181 /* We haven't found a mapping. Install ione. */
182 struct inuse_sem
*newp
;
184 newp
= (struct inuse_sem
*) malloc (sizeof (*newp
) + namelen
);
187 /* If the caller hasn't provided any map it now. */
188 if (existing
== SEM_FAILED
)
189 existing
= (sem_t
*) mmap (NULL
, sizeof (sem_t
),
190 PROT_READ
| PROT_WRITE
, MAP_SHARED
,
193 newp
->dev
= st
.st_dev
;
194 newp
->ino
= st
.st_ino
;
196 newp
->sem
= existing
;
197 memcpy (newp
->name
, name
, namelen
);
199 /* Insert the new value. */
200 if (existing
!= MAP_FAILED
201 && tsearch (newp
, &__sem_mappings
, __sem_search
) != NULL
)
205 /* Something went wrong while inserting the new
206 value. We fail completely. */
211 /* Release the lock. */
212 lll_unlock (__sem_mappings_lock
, LLL_PRIVATE
);
215 if (result
!= existing
&& existing
!= SEM_FAILED
&& existing
!= MAP_FAILED
)
217 /* Do not disturb errno. */
218 INTERNAL_SYSCALL_DECL (err
);
219 INTERNAL_SYSCALL (munmap
, err
, 2, existing
, sizeof (sem_t
));
227 sem_open (const char *name
, int oflag
, ...)
230 sem_t
*result
= SEM_FAILED
;
233 /* Determine where the shmfs is mounted. */
234 __pthread_once (&__namedsem_once
, __where_is_shmfs
);
236 /* If we don't know the mount points there is nothing we can do. Ever. */
237 if (mountpoint
.dir
== NULL
)
239 __set_errno (ENOSYS
);
243 /* Construct the filename. */
244 while (name
[0] == '/')
249 /* The name "/" is not supported. */
250 __set_errno (EINVAL
);
253 size_t namelen
= strlen (name
) + 1;
255 /* Create the name of the final file. */
256 finalname
= (char *) alloca (mountpoint
.dirlen
+ namelen
);
257 __mempcpy (__mempcpy (finalname
, mountpoint
.dir
, mountpoint
.dirlen
),
260 /* If the semaphore object has to exist simply open it. */
261 if ((oflag
& O_CREAT
) == 0 || (oflag
& O_EXCL
) == 0)
264 fd
= __libc_open (finalname
,
265 (oflag
& ~(O_CREAT
|O_ACCMODE
)) | O_NOFOLLOW
| O_RDWR
);
269 /* If we are supposed to create the file try this next. */
270 if ((oflag
& O_CREAT
) != 0 && errno
== ENOENT
)
273 /* Return. errno is already set. */
276 /* Check whether we already have this semaphore mapped and
277 create one if necessary. */
278 result
= check_add_mapping (name
, namelen
, fd
, SEM_FAILED
);
282 /* We have to open a temporary file first since it must have the
283 correct form before we can start using it. */
290 va_start (ap
, oflag
);
292 mode
= va_arg (ap
, mode_t
);
293 value
= va_arg (ap
, unsigned int);
297 if (value
> SEM_VALUE_MAX
)
299 __set_errno (EINVAL
);
303 /* Create the initial file content. */
307 struct new_sem newsem
;
310 sem
.newsem
.value
= value
;
311 sem
.newsem
.private = 0;
312 sem
.newsem
.nwaiters
= 0;
314 /* Initialize the remaining bytes as well. */
315 memset ((char *) &sem
.initsem
+ sizeof (struct new_sem
), '\0',
316 sizeof (sem_t
) - sizeof (struct new_sem
));
318 tmpfname
= (char *) alloca (mountpoint
.dirlen
+ 6 + 1);
319 char *xxxxxx
= __mempcpy (tmpfname
, mountpoint
.dir
, mountpoint
.dirlen
);
325 /* Add the suffix for mktemp. */
326 strcpy (xxxxxx
, "XXXXXX");
328 /* We really want to use mktemp here. We cannot use mkstemp
329 since the file must be opened with a specific mode. The
330 mode cannot later be set since then we cannot apply the
332 if (__mktemp (tmpfname
) == NULL
)
335 /* Open the file. Make sure we do not overwrite anything. */
336 fd
= __libc_open (tmpfname
, O_RDWR
| O_CREAT
| O_EXCL
, mode
);
341 if (++retries
< NRETRIES
)
344 __set_errno (EAGAIN
);
354 if (TEMP_FAILURE_RETRY (__libc_write (fd
, &sem
.initsem
, sizeof (sem_t
)))
356 /* Map the sem_t structure from the file. */
357 && (result
= (sem_t
*) mmap (NULL
, sizeof (sem_t
),
358 PROT_READ
| PROT_WRITE
, MAP_SHARED
,
359 fd
, 0)) != MAP_FAILED
)
361 /* Create the file. Don't overwrite an existing file. */
362 if (link (tmpfname
, finalname
) != 0)
364 /* Undo the mapping. */
365 (void) munmap (result
, sizeof (sem_t
));
367 /* Reinitialize 'result'. */
370 /* This failed. If O_EXCL is not set and the problem was
371 that the file exists, try again. */
372 if ((oflag
& O_EXCL
) == 0 && errno
== EEXIST
)
374 /* Remove the file. */
375 (void) unlink (tmpfname
);
377 /* Close the file. */
378 (void) __libc_close (fd
);
384 /* Insert the mapping into the search tree. This also
385 determines whether another thread sneaked by and already
386 added such a mapping despite the fact that we created it. */
387 result
= check_add_mapping (name
, namelen
, fd
, result
);
390 /* Now remove the temporary name. This should never fail. If
391 it fails we leak a file name. Better fix the kernel. */
392 (void) unlink (tmpfname
);
395 /* Map the mmap error to the error we need. */
396 if (MAP_FAILED
!= (void *) SEM_FAILED
&& result
== MAP_FAILED
)
399 /* We don't need the file descriptor anymore. */
402 /* Do not disturb errno. */
403 INTERNAL_SYSCALL_DECL (err
);
404 INTERNAL_SYSCALL (close
, err
, 1, fd
);