lckpwdf.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* vi: set sw=4 ts=4: */
  2. /* Handle locking of password file.
  3. Copyright (C) 1996, 1998 Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
  6. The GNU C Library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Library General Public License as
  8. published by the Free Software Foundation; either version 2 of the
  9. 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. Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB. If not,
  16. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. Boston, MA 02111-1307, USA. */
  18. #define sigfillset __sigfillset_internal
  19. #define sigaction __sigaction_internal
  20. #include <features.h>
  21. #include <fcntl.h>
  22. #include <signal.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. #include <sys/file.h>
  26. #include <paths.h>
  27. #ifdef __UCLIBC_HAS_THREADS__
  28. #include <pthread.h>
  29. static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
  30. # define LOCK __pthread_mutex_lock(&mylock)
  31. # define UNLOCK __pthread_mutex_unlock(&mylock);
  32. #else
  33. # define LOCK
  34. # define UNLOCK
  35. #endif
  36. /* How long to wait for getting the lock before returning with an
  37. error. */
  38. #define TIMEOUT 15 /* sec */
  39. /* File descriptor for lock file. */
  40. static int lock_fd = -1;
  41. /* Prototypes for local functions. */
  42. static void noop_handler __P ((int __sig));
  43. int lckpwdf (void)
  44. {
  45. int flags;
  46. sigset_t saved_set; /* Saved set of caught signals. */
  47. struct sigaction saved_act; /* Saved signal action. */
  48. sigset_t new_set; /* New set of caught signals. */
  49. struct sigaction new_act; /* New signal action. */
  50. struct flock fl; /* Information struct for locking. */
  51. int result;
  52. if (lock_fd != -1)
  53. /* Still locked by own process. */
  54. return -1;
  55. LOCK;
  56. lock_fd = __open (_PATH_PASSWD, O_WRONLY);
  57. if (lock_fd == -1) {
  58. /* Cannot create lock file. */
  59. UNLOCK;
  60. return -1;
  61. }
  62. /* Make sure file gets correctly closed when process finished. */
  63. flags = fcntl (lock_fd, F_GETFD, 0);
  64. if (flags == -1) {
  65. /* Cannot get file flags. */
  66. __close(lock_fd);
  67. lock_fd = -1;
  68. UNLOCK;
  69. return -1;
  70. }
  71. flags |= FD_CLOEXEC; /* Close on exit. */
  72. if (fcntl (lock_fd, F_SETFD, flags) < 0) {
  73. /* Cannot set new flags. */
  74. __close(lock_fd);
  75. lock_fd = -1;
  76. UNLOCK;
  77. return -1;
  78. }
  79. /* Now we have to get exclusive write access. Since multiple
  80. process could try this we won't stop when it first fails.
  81. Instead we set a timeout for the system call. Once the timer
  82. expires it is likely that there are some problems which cannot be
  83. resolved by waiting.
  84. It is important that we don't change the signal state. We must
  85. restore the old signal behaviour. */
  86. __memset (&new_act, '\0', sizeof (struct sigaction));
  87. new_act.sa_handler = noop_handler;
  88. sigfillset (&new_act.sa_mask);
  89. new_act.sa_flags = 0ul;
  90. /* Install new action handler for alarm and save old. */
  91. if (sigaction (SIGALRM, &new_act, &saved_act) < 0) {
  92. /* Cannot install signal handler. */
  93. __close(lock_fd);
  94. lock_fd = -1;
  95. UNLOCK;
  96. return -1;
  97. }
  98. /* Now make sure the alarm signal is not blocked. */
  99. sigemptyset (&new_set);
  100. sigaddset (&new_set, SIGALRM);
  101. if (__sigprocmask (SIG_UNBLOCK, &new_set, &saved_set) < 0) {
  102. sigaction (SIGALRM, &saved_act, NULL);
  103. __close(lock_fd);
  104. lock_fd = -1;
  105. UNLOCK;
  106. return -1;
  107. }
  108. /* Start timer. If we cannot get the lock in the specified time we
  109. get a signal. */
  110. alarm (TIMEOUT);
  111. /* Try to get the lock. */
  112. __memset (&fl, '\0', sizeof (struct flock));
  113. fl.l_type = F_WRLCK;
  114. fl.l_whence = SEEK_SET;
  115. result = fcntl (lock_fd, F_SETLKW, &fl);
  116. /* Clear alarm. */
  117. alarm (0);
  118. /* Restore old set of handled signals. We don't need to know
  119. about the current one.*/
  120. __sigprocmask (SIG_SETMASK, &saved_set, NULL);
  121. /* Restore old action handler for alarm. We don't need to know
  122. about the current one. */
  123. sigaction (SIGALRM, &saved_act, NULL);
  124. if (result < 0) {
  125. __close(lock_fd);
  126. lock_fd = -1;
  127. UNLOCK;
  128. return -1;
  129. }
  130. UNLOCK;
  131. return 0;
  132. }
  133. int ulckpwdf (void)
  134. {
  135. int result;
  136. if (lock_fd == -1) {
  137. /* There is no lock set. */
  138. result = -1;
  139. }
  140. else {
  141. LOCK;
  142. result = __close (lock_fd);
  143. /* Mark descriptor as unused. */
  144. lock_fd = -1;
  145. UNLOCK;
  146. }
  147. return result;
  148. }
  149. static void noop_handler (int sig)
  150. {
  151. /* We simply return which makes the `fcntl' call return with an error. */
  152. }