sem_unlink.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. 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 <semaphore.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. #include "semaphoreP.h"
  21. int
  22. sem_unlink (
  23. const char *name)
  24. {
  25. char *fname;
  26. size_t namelen;
  27. /* Determine where the shmfs is mounted. */
  28. INTUSE(__pthread_once) (&__namedsem_once, __where_is_shmfs);
  29. /* If we don't know the mount points there is nothing we can do. Ever. */
  30. if (mountpoint.dir == NULL)
  31. {
  32. __set_errno (ENOSYS);
  33. return -1;
  34. }
  35. /* Construct the filename. */
  36. while (name[0] == '/')
  37. ++name;
  38. if (name[0] == '\0')
  39. {
  40. /* The name "/" is not supported. */
  41. __set_errno (ENOENT);
  42. return -1;
  43. }
  44. namelen = strlen (name);
  45. /* Create the name of the file. */
  46. fname = (char *) alloca (mountpoint.dirlen + namelen + 1);
  47. mempcpy (mempcpy (fname, mountpoint.dir, mountpoint.dirlen),
  48. name, namelen + 1);
  49. /* Now try removing it. */
  50. int ret = unlink (fname);
  51. if (ret < 0 && errno == EPERM)
  52. __set_errno (EACCES);
  53. return ret;
  54. }