lckpwdf.c 4.7 KB

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