lckpwdf.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* vi: set sw=4 ts=4: */
  2. /* Handle locking of password file.
  3. Copyright (C) 1996,98,2000,02 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 Lesser General Public
  8. License as published by the Free Software Foundation; either
  9. 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.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with the GNU C Library; if not, write to the Free
  16. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  17. 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. /* Experimentally off - 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. /* How long to wait for getting the lock before returning with an error. */
  37. #define TIMEOUT 15 /* sec */
  38. /* File descriptor for lock file. */
  39. static int lock_fd = -1;
  40. /* Prevent problems in multithreaded program by using mutex. */
  41. #include <bits/uClibc_mutex.h>
  42. __UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
  43. /* Prototypes for local functions. */
  44. static void noop_handler (int __sig);
  45. int
  46. lckpwdf (void)
  47. {
  48. int flags;
  49. sigset_t saved_set; /* Saved set of caught signals. */
  50. struct sigaction saved_act; /* Saved signal action. */
  51. sigset_t new_set; /* New set of caught signals. */
  52. struct sigaction new_act; /* New signal action. */
  53. struct flock fl; /* Information struct for locking. */
  54. int result;
  55. int rv = -1;
  56. if (lock_fd != -1)
  57. /* Still locked by own process. */
  58. return -1;
  59. /* Prevent problems caused by multiple threads. */
  60. __UCLIBC_MUTEX_LOCK(mylock);
  61. lock_fd = open (_PATH_PASSWD, O_WRONLY);
  62. if (lock_fd == -1) {
  63. /* Cannot create lock file. */
  64. goto DONE;
  65. }
  66. /* Make sure file gets correctly closed when process finished. */
  67. flags = fcntl (lock_fd, F_GETFD);
  68. #if 0 /* never fails */
  69. if (flags == -1) {
  70. /* Cannot get file flags. */
  71. close(lock_fd);
  72. lock_fd = -1;
  73. goto DONE;
  74. }
  75. #endif
  76. flags |= FD_CLOEXEC;
  77. #if 1
  78. fcntl (lock_fd, F_SETFD, flags);
  79. #else /* never fails */
  80. if (fcntl (lock_fd, F_SETFD, flags) < 0) {
  81. /* Cannot set new flags. */
  82. close(lock_fd);
  83. lock_fd = -1;
  84. goto DONE;
  85. }
  86. #endif
  87. /* Now we have to get exclusive write access. Since multiple
  88. process could try this we won't stop when it first fails.
  89. Instead we set a timeout for the system call. Once the timer
  90. expires it is likely that there are some problems which cannot be
  91. resolved by waiting. (sa_flags have no SA_RESTART. Thus SIGALRM
  92. will EINTR fcntl(F_SETLKW)
  93. It is important that we don't change the signal state. We must
  94. restore the old signal behaviour. */
  95. memset (&new_act, '\0', sizeof (struct sigaction));
  96. new_act.sa_handler = noop_handler;
  97. __sigfillset (&new_act.sa_mask);
  98. /* new_act.sa_flags = 0; */
  99. /* Install new action handler for alarm and save old. */
  100. if (sigaction (SIGALRM, &new_act, &saved_act) < 0) {
  101. /* Cannot install signal handler. */
  102. close(lock_fd);
  103. lock_fd = -1;
  104. goto DONE;
  105. }
  106. /* Now make sure the alarm signal is not blocked. */
  107. __sigemptyset (&new_set);
  108. __sigaddset (&new_set, SIGALRM);
  109. #if 1
  110. sigprocmask (SIG_UNBLOCK, &new_set, &saved_set);
  111. #else /* never fails */
  112. if (sigprocmask (SIG_UNBLOCK, &new_set, &saved_set) < 0) {
  113. sigaction (SIGALRM, &saved_act, NULL);
  114. close(lock_fd);
  115. lock_fd = -1;
  116. goto DONE;
  117. }
  118. #endif
  119. /* Start timer. If we cannot get the lock in the specified time we
  120. get a signal. */
  121. alarm (TIMEOUT);
  122. /* Try to get the lock. */
  123. memset (&fl, '\0', sizeof (fl));
  124. if (F_WRLCK)
  125. fl.l_type = F_WRLCK;
  126. if (SEEK_SET)
  127. fl.l_whence = SEEK_SET;
  128. result = fcntl (lock_fd, F_SETLKW, &fl);
  129. /* Clear alarm. */
  130. alarm (0);
  131. sigprocmask (SIG_SETMASK, &saved_set, NULL);
  132. sigaction (SIGALRM, &saved_act, NULL);
  133. if (result < 0) {
  134. close(lock_fd);
  135. lock_fd = -1;
  136. goto DONE;
  137. }
  138. rv = 0;
  139. DONE:
  140. __UCLIBC_MUTEX_UNLOCK(mylock);
  141. return 0;
  142. }
  143. int
  144. ulckpwdf (void)
  145. {
  146. int result;
  147. if (lock_fd == -1)
  148. /* There is no lock set. */
  149. result = -1;
  150. else
  151. {
  152. /* Prevent problems caused by multiple threads. */
  153. __UCLIBC_MUTEX_LOCK(mylock);
  154. result = close (lock_fd);
  155. /* Mark descriptor as unused. */
  156. lock_fd = -1;
  157. /* Clear mutex. */
  158. __UCLIBC_MUTEX_UNLOCK(mylock);
  159. }
  160. return result;
  161. }
  162. static void
  163. noop_handler (int sig)
  164. {
  165. /* We simply return which makes the `fcntl' call return with an error. */
  166. }