lckpwdf.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. #include <features.h>
  19. #include <fcntl.h>
  20. #include <signal.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. #include <sys/file.h>
  24. #include <paths.h>
  25. #include <shadow.h>
  26. libc_hidden_proto(memset)
  27. libc_hidden_proto(open)
  28. libc_hidden_proto(fcntl)
  29. libc_hidden_proto(close)
  30. libc_hidden_proto(sigfillset)
  31. libc_hidden_proto(sigaction)
  32. libc_hidden_proto(sigprocmask)
  33. libc_hidden_proto(sigaddset)
  34. libc_hidden_proto(sigemptyset)
  35. libc_hidden_proto(alarm)
  36. #ifdef __UCLIBC_HAS_THREADS__
  37. # include <pthread.h>
  38. static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
  39. #endif
  40. #define LOCK __pthread_mutex_lock(&mylock)
  41. #define UNLOCK __pthread_mutex_unlock(&mylock)
  42. /* How long to wait for getting the lock before returning with an
  43. error. */
  44. #define TIMEOUT 15 /* sec */
  45. /* File descriptor for lock file. */
  46. static int lock_fd = -1;
  47. /* Prototypes for local functions. */
  48. static void noop_handler (int __sig);
  49. int lckpwdf (void)
  50. {
  51. int flags;
  52. sigset_t saved_set; /* Saved set of caught signals. */
  53. struct sigaction saved_act; /* Saved signal action. */
  54. sigset_t new_set; /* New set of caught signals. */
  55. struct sigaction new_act; /* New signal action. */
  56. struct flock fl; /* Information struct for locking. */
  57. int result;
  58. if (lock_fd != -1)
  59. /* Still locked by own process. */
  60. return -1;
  61. LOCK;
  62. lock_fd = open (_PATH_PASSWD, O_WRONLY);
  63. if (lock_fd == -1) {
  64. /* Cannot create lock file. */
  65. UNLOCK;
  66. return -1;
  67. }
  68. /* Make sure file gets correctly closed when process finished. */
  69. flags = fcntl (lock_fd, F_GETFD, 0);
  70. if (flags == -1) {
  71. /* Cannot get file flags. */
  72. close(lock_fd);
  73. lock_fd = -1;
  74. UNLOCK;
  75. return -1;
  76. }
  77. flags |= FD_CLOEXEC; /* Close on exit. */
  78. if (fcntl (lock_fd, F_SETFD, flags) < 0) {
  79. /* Cannot set new flags. */
  80. close(lock_fd);
  81. lock_fd = -1;
  82. UNLOCK;
  83. return -1;
  84. }
  85. /* Now we have to get exclusive write access. Since multiple
  86. process could try this we won't stop when it first fails.
  87. Instead we set a timeout for the system call. Once the timer
  88. expires it is likely that there are some problems which cannot be
  89. resolved by waiting.
  90. It is important that we don't change the signal state. We must
  91. restore the old signal behaviour. */
  92. memset (&new_act, '\0', sizeof (struct sigaction));
  93. new_act.sa_handler = noop_handler;
  94. sigfillset (&new_act.sa_mask);
  95. new_act.sa_flags = 0ul;
  96. /* Install new action handler for alarm and save old. */
  97. if (sigaction (SIGALRM, &new_act, &saved_act) < 0) {
  98. /* Cannot install signal handler. */
  99. close(lock_fd);
  100. lock_fd = -1;
  101. UNLOCK;
  102. return -1;
  103. }
  104. /* Now make sure the alarm signal is not blocked. */
  105. sigemptyset (&new_set);
  106. sigaddset (&new_set, SIGALRM);
  107. if (sigprocmask (SIG_UNBLOCK, &new_set, &saved_set) < 0) {
  108. sigaction (SIGALRM, &saved_act, NULL);
  109. close(lock_fd);
  110. lock_fd = -1;
  111. UNLOCK;
  112. return -1;
  113. }
  114. /* Start timer. If we cannot get the lock in the specified time we
  115. get a signal. */
  116. alarm (TIMEOUT);
  117. /* Try to get the lock. */
  118. memset (&fl, '\0', sizeof (struct flock));
  119. fl.l_type = F_WRLCK;
  120. fl.l_whence = SEEK_SET;
  121. result = fcntl (lock_fd, F_SETLKW, &fl);
  122. /* Clear alarm. */
  123. alarm (0);
  124. /* Restore old set of handled signals. We don't need to know
  125. about the current one.*/
  126. sigprocmask (SIG_SETMASK, &saved_set, NULL);
  127. /* Restore old action handler for alarm. We don't need to know
  128. about the current one. */
  129. sigaction (SIGALRM, &saved_act, NULL);
  130. if (result < 0) {
  131. close(lock_fd);
  132. lock_fd = -1;
  133. UNLOCK;
  134. return -1;
  135. }
  136. UNLOCK;
  137. return 0;
  138. }
  139. int ulckpwdf (void)
  140. {
  141. int result;
  142. if (lock_fd == -1) {
  143. /* There is no lock set. */
  144. result = -1;
  145. }
  146. else {
  147. LOCK;
  148. result = close (lock_fd);
  149. /* Mark descriptor as unused. */
  150. lock_fd = -1;
  151. UNLOCK;
  152. }
  153. return result;
  154. }
  155. static void noop_handler (attribute_unused int sig)
  156. {
  157. /* We simply return which makes the `fcntl' call return with an error. */
  158. }