OSDN Git Service

beed02ee58bcee9bee5180517764f8c752347343
[uclinux-h8/uClibc.git] / libpthread / nptl / sem_unlink.c
1 /* Copyright (C) 2002, 2004 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
4
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.
9
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.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
20 #include <errno.h>
21 #include <semaphore.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include "semaphoreP.h"
26
27
28 int
29 sem_unlink (
30      const char *name)
31 {
32   char *fname;
33   size_t namelen;
34
35   /* Determine where the shmfs is mounted.  */
36   __pthread_once (&__namedsem_once, __where_is_shmfs);
37
38   /* If we don't know the mount points there is nothing we can do.  Ever.  */
39   if (mountpoint.dir == NULL)
40     {
41       __set_errno (ENOSYS);
42       return -1;
43     }
44
45   /* Construct the filename.  */
46   while (name[0] == '/')
47     ++name;
48
49   if (name[0] == '\0')
50     {
51       /* The name "/" is not supported.  */
52       __set_errno (ENOENT);
53       return -1;
54     }
55   namelen = strlen (name);
56
57   /* Create the name of the file.  */
58   fname = (char *) alloca (mountpoint.dirlen + namelen + 1);
59   mempcpy (mempcpy (fname, mountpoint.dir, mountpoint.dirlen),
60              name, namelen + 1);
61
62   /* Now try removing it.  */
63   int ret = unlink (fname);
64   if (ret < 0 && errno == EPERM)
65     __set_errno (EACCES);
66   return ret;
67 }