lckpwdf.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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, see
  16. <http://www.gnu.org/licenses/>. */
  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. #include <shadow.h>
  25. /* How long to wait for getting the lock before returning with an error. */
  26. #define TIMEOUT 15 /* sec */
  27. /* File descriptor for lock file. */
  28. static int lock_fd = -1;
  29. /* Prevent problems in multithreaded program by using mutex. */
  30. #include <bits/uClibc_mutex.h>
  31. __UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
  32. /* Prototypes for local functions. */
  33. static void noop_handler (int __sig);
  34. int
  35. lckpwdf (void)
  36. {
  37. sigset_t saved_set; /* Saved set of caught signals. */
  38. struct sigaction saved_act; /* Saved signal action. */
  39. sigset_t new_set; /* New set of caught signals. */
  40. struct sigaction new_act; /* New signal action. */
  41. struct flock fl; /* Information struct for locking. */
  42. int result;
  43. int rv = -1;
  44. if (lock_fd != -1)
  45. /* Still locked by own process. */
  46. return -1;
  47. /* Prevent problems caused by multiple threads. */
  48. __UCLIBC_MUTEX_LOCK(mylock);
  49. lock_fd = open (_PATH_PASSWD, O_WRONLY | O_CLOEXEC);
  50. if (lock_fd == -1) {
  51. goto DONE;
  52. }
  53. #ifndef __ASSUME_O_CLOEXEC
  54. /* Make sure file gets correctly closed when process finished. */
  55. fcntl (lock_fd, F_SETFD, FD_CLOEXEC);
  56. #endif
  57. /* Now we have to get exclusive write access. Since multiple
  58. process could try this we won't stop when it first fails.
  59. Instead we set a timeout for the system call. Once the timer
  60. expires it is likely that there are some problems which cannot be
  61. resolved by waiting. (sa_flags have no SA_RESTART. Thus SIGALRM
  62. will EINTR fcntl(F_SETLKW)
  63. It is important that we don't change the signal state. We must
  64. restore the old signal behaviour. */
  65. memset (&new_act, '\0', sizeof (new_act));
  66. new_act.sa_handler = noop_handler;
  67. __sigfillset (&new_act.sa_mask);
  68. /* Install new action handler for alarm and save old.
  69. * This never fails in Linux. */
  70. sigaction (SIGALRM, &new_act, &saved_act);
  71. /* Now make sure the alarm signal is not blocked. */
  72. __sigemptyset (&new_set);
  73. __sigaddset (&new_set, SIGALRM);
  74. sigprocmask (SIG_UNBLOCK, &new_set, &saved_set);
  75. /* Start timer. If we cannot get the lock in the specified time we
  76. get a signal. */
  77. alarm (TIMEOUT);
  78. /* Try to get the lock. */
  79. memset (&fl, '\0', sizeof (fl));
  80. if (F_WRLCK)
  81. fl.l_type = F_WRLCK;
  82. if (SEEK_SET)
  83. fl.l_whence = SEEK_SET;
  84. result = fcntl (lock_fd, F_SETLKW, &fl);
  85. /* Clear alarm. */
  86. alarm (0);
  87. sigprocmask (SIG_SETMASK, &saved_set, NULL);
  88. sigaction (SIGALRM, &saved_act, NULL);
  89. if (result < 0) {
  90. close(lock_fd);
  91. lock_fd = -1;
  92. goto DONE;
  93. }
  94. rv = 0;
  95. DONE:
  96. __UCLIBC_MUTEX_UNLOCK(mylock);
  97. return rv;
  98. }
  99. int
  100. ulckpwdf (void)
  101. {
  102. int result;
  103. if (lock_fd == -1)
  104. /* There is no lock set. */
  105. result = -1;
  106. else
  107. {
  108. /* Prevent problems caused by multiple threads. */
  109. __UCLIBC_MUTEX_LOCK(mylock);
  110. result = close (lock_fd);
  111. /* Mark descriptor as unused. */
  112. lock_fd = -1;
  113. /* Clear mutex. */
  114. __UCLIBC_MUTEX_UNLOCK(mylock);
  115. }
  116. return result;
  117. }
  118. static void
  119. noop_handler (int sig attribute_unused) {
  120. /* We simply return which makes the `fcntl' call return with an error. */
  121. }