lckpwdf.c 4.1 KB

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