lckpwdf.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* Handle locking of password file.
  2. Copyright (C) 1996-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <fcntl.h>
  16. #include <libc-lock.h>
  17. #include <shadow.h>
  18. #include <signal.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. #include <sys/file.h>
  22. #include <sigsetops.h>
  23. #include <kernel-features.h>
  24. /* Name of the lock file. */
  25. #define PWD_LOCKFILE "/etc/.pwd.lock"
  26. /* How long to wait for getting the lock before returning with an
  27. error. */
  28. #define TIMEOUT 15 /* sec */
  29. /* File descriptor for lock file. */
  30. static int lock_fd = -1;
  31. /* Prevent problems in multithreaded program by using mutex. */
  32. __libc_lock_define_initialized (static, lock)
  33. /* Prototypes for local functions. */
  34. static void noop_handler (int __sig);
  35. /* We cannot simply return in error cases. We have to close the file
  36. and perhaps restore the signal handler. */
  37. #define RETURN_CLOSE_FD(code) \
  38. do { \
  39. if ((code) < 0 && lock_fd >= 0) \
  40. { \
  41. __close (lock_fd); \
  42. lock_fd = -1; \
  43. } \
  44. __libc_lock_unlock (lock); \
  45. return (code); \
  46. } while (0)
  47. #define RETURN_RESTORE_HANDLER(code) \
  48. do { \
  49. /* Restore old action handler for alarm. We don't need to know \
  50. about the current one. */ \
  51. __sigaction (SIGALRM, &saved_act, NULL); \
  52. RETURN_CLOSE_FD (code); \
  53. } while (0)
  54. #define RETURN_CLEAR_ALARM(code) \
  55. do { \
  56. /* Clear alarm. */ \
  57. alarm (0); \
  58. /* Restore old set of handled signals. We don't need to know \
  59. about the current one.*/ \
  60. __sigprocmask (SIG_SETMASK, &saved_set, NULL); \
  61. RETURN_RESTORE_HANDLER (code); \
  62. } while (0)
  63. int
  64. __lckpwdf (void)
  65. {
  66. sigset_t saved_set; /* Saved set of caught signals. */
  67. struct sigaction saved_act; /* Saved signal action. */
  68. sigset_t new_set; /* New set of caught signals. */
  69. struct sigaction new_act; /* New signal action. */
  70. struct flock fl; /* Information struct for locking. */
  71. int result;
  72. if (lock_fd != -1)
  73. /* Still locked by own process. */
  74. return -1;
  75. /* Prevent problems caused by multiple threads. */
  76. __libc_lock_lock (lock);
  77. int oflags = O_WRONLY | O_CREAT | O_CLOEXEC;
  78. lock_fd = __open (PWD_LOCKFILE, oflags, 0600);
  79. if (lock_fd == -1)
  80. /* Cannot create lock file. */
  81. RETURN_CLOSE_FD (-1);
  82. /* Now we have to get exclusive write access. Since multiple
  83. process could try this we won't stop when it first fails.
  84. Instead we set a timeout for the system call. Once the timer
  85. expires it is likely that there are some problems which cannot be
  86. resolved by waiting.
  87. It is important that we don't change the signal state. We must
  88. restore the old signal behaviour. */
  89. memset (&new_act, '\0', sizeof (struct sigaction));
  90. new_act.sa_handler = noop_handler;
  91. __sigfillset (&new_act.sa_mask);
  92. new_act.sa_flags = 0ul;
  93. /* Install new action handler for alarm and save old. */
  94. if (__sigaction (SIGALRM, &new_act, &saved_act) < 0)
  95. /* Cannot install signal handler. */
  96. RETURN_CLOSE_FD (-1);
  97. /* Now make sure the alarm signal is not blocked. */
  98. __sigemptyset (&new_set);
  99. __sigaddset (&new_set, SIGALRM);
  100. if (__sigprocmask (SIG_UNBLOCK, &new_set, &saved_set) < 0)
  101. RETURN_RESTORE_HANDLER (-1);
  102. /* Start timer. If we cannot get the lock in the specified time we
  103. get a signal. */
  104. alarm (TIMEOUT);
  105. /* Try to get the lock. */
  106. memset (&fl, '\0', sizeof (struct flock));
  107. fl.l_type = F_WRLCK;
  108. fl.l_whence = SEEK_SET;
  109. result = __fcntl (lock_fd, F_SETLKW, &fl);
  110. RETURN_CLEAR_ALARM (result);
  111. }
  112. weak_alias (__lckpwdf, lckpwdf)
  113. int
  114. __ulckpwdf (void)
  115. {
  116. int result;
  117. if (lock_fd == -1)
  118. /* There is no lock set. */
  119. result = -1;
  120. else
  121. {
  122. /* Prevent problems caused by multiple threads. */
  123. __libc_lock_lock (lock);
  124. result = __close (lock_fd);
  125. /* Mark descriptor as unused. */
  126. lock_fd = -1;
  127. /* Clear mutex. */
  128. __libc_lock_unlock (lock);
  129. }
  130. return result;
  131. }
  132. weak_alias (__ulckpwdf, ulckpwdf)
  133. static void
  134. noop_handler (int sig)
  135. {
  136. /* We simply return which makes the `fcntl' call return with an error. */
  137. }