]> git.ipfire.org Git - thirdparty/glibc.git/blame - shadow/lckpwdf.c
powerpc: Refactor powerpc64 lround/lroundf/llround/llroundf
[thirdparty/glibc.git] / shadow / lckpwdf.c
CommitLineData
6259ec0d 1/* Handle locking of password file.
04277e02 2 Copyright (C) 1996-2019 Free Software Foundation, Inc.
6259ec0d
UD
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
dcf0671d 5
6259ec0d 6 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
dcf0671d 10
6259ec0d
UD
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41bdb6e2 14 Lesser General Public License for more details.
dcf0671d 15
41bdb6e2 16 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
dcf0671d
UD
19
20#include <fcntl.h>
ec999b8e 21#include <libc-lock.h>
dcf0671d
UD
22#include <shadow.h>
23#include <signal.h>
24#include <string.h>
25#include <unistd.h>
26#include <sys/file.h>
a992f506 27#include <sigsetops.h>
dcf0671d 28
7cf89e95
UD
29#include <kernel-features.h>
30
dcf0671d
UD
31
32/* Name of the lock file. */
6259ec0d 33#define PWD_LOCKFILE "/etc/.pwd.lock"
dcf0671d
UD
34
35/* How long to wait for getting the lock before returning with an
36 error. */
37#define TIMEOUT 15 /* sec */
38
39
40/* File descriptor for lock file. */
41static int lock_fd = -1;
42
43/* Prevent problems in multithreaded program by using mutex. */
44__libc_lock_define_initialized (static, lock)
45
46
47/* Prototypes for local functions. */
bcaad6ee 48static void noop_handler (int __sig);
dcf0671d
UD
49
50
51/* We cannot simply return in error cases. We have to close the file
52 and perhaps restore the signal handler. */
53#define RETURN_CLOSE_FD(code) \
54 do { \
55 if ((code) < 0 && lock_fd >= 0) \
56 { \
50304ef0 57 __close (lock_fd); \
dcf0671d
UD
58 lock_fd = -1; \
59 } \
60 __libc_lock_unlock (lock); \
61 return (code); \
62 } while (0)
63
64#define RETURN_RESTORE_HANDLER(code) \
65 do { \
66 /* Restore old action handler for alarm. We don't need to know \
67 about the current one. */ \
50304ef0 68 __sigaction (SIGALRM, &saved_act, NULL); \
dcf0671d
UD
69 RETURN_CLOSE_FD (code); \
70 } while (0)
71
72#define RETURN_CLEAR_ALARM(code) \
73 do { \
74 /* Clear alarm. */ \
75 alarm (0); \
76 /* Restore old set of handled signals. We don't need to know \
77 about the current one.*/ \
50304ef0 78 __sigprocmask (SIG_SETMASK, &saved_set, NULL); \
dcf0671d
UD
79 RETURN_RESTORE_HANDLER (code); \
80 } while (0)
81
82
83int
bcaad6ee 84__lckpwdf (void)
dcf0671d 85{
dcf0671d
UD
86 sigset_t saved_set; /* Saved set of caught signals. */
87 struct sigaction saved_act; /* Saved signal action. */
88 sigset_t new_set; /* New set of caught signals. */
89 struct sigaction new_act; /* New signal action. */
f8b87ef0 90 struct flock fl; /* Information struct for locking. */
dcf0671d
UD
91 int result;
92
93 if (lock_fd != -1)
94 /* Still locked by own process. */
95 return -1;
96
97 /* Prevent problems caused by multiple threads. */
98 __libc_lock_lock (lock);
99
cef9b653 100 int oflags = O_WRONLY | O_CREAT | O_CLOEXEC;
7cf89e95 101 lock_fd = __open (PWD_LOCKFILE, oflags, 0600);
dcf0671d
UD
102 if (lock_fd == -1)
103 /* Cannot create lock file. */
104 RETURN_CLOSE_FD (-1);
105
dcf0671d
UD
106 /* Now we have to get exclusive write access. Since multiple
107 process could try this we won't stop when it first fails.
108 Instead we set a timeout for the system call. Once the timer
109 expires it is likely that there are some problems which cannot be
110 resolved by waiting.
111
112 It is important that we don't change the signal state. We must
113 restore the old signal behaviour. */
114 memset (&new_act, '\0', sizeof (struct sigaction));
115 new_act.sa_handler = noop_handler;
284128f6 116 __sigfillset (&new_act.sa_mask);
dcf0671d
UD
117 new_act.sa_flags = 0ul;
118
119 /* Install new action handler for alarm and save old. */
50304ef0 120 if (__sigaction (SIGALRM, &new_act, &saved_act) < 0)
dcf0671d
UD
121 /* Cannot install signal handler. */
122 RETURN_CLOSE_FD (-1);
123
124 /* Now make sure the alarm signal is not blocked. */
284128f6 125 __sigemptyset (&new_set);
4aebaa6b 126 __sigaddset (&new_set, SIGALRM);
50304ef0 127 if (__sigprocmask (SIG_UNBLOCK, &new_set, &saved_set) < 0)
dcf0671d
UD
128 RETURN_RESTORE_HANDLER (-1);
129
130 /* Start timer. If we cannot get the lock in the specified time we
131 get a signal. */
132 alarm (TIMEOUT);
133
134 /* Try to get the lock. */
f8b87ef0
UD
135 memset (&fl, '\0', sizeof (struct flock));
136 fl.l_type = F_WRLCK;
137 fl.l_whence = SEEK_SET;
50304ef0 138 result = __fcntl (lock_fd, F_SETLKW, &fl);
dcf0671d
UD
139
140 RETURN_CLEAR_ALARM (result);
141}
142weak_alias (__lckpwdf, lckpwdf)
143
144
145int
bcaad6ee 146__ulckpwdf (void)
dcf0671d
UD
147{
148 int result;
149
150 if (lock_fd == -1)
151 /* There is no lock set. */
152 result = -1;
153 else
154 {
155 /* Prevent problems caused by multiple threads. */
db813f25 156 __libc_lock_lock (lock);
dcf0671d 157
50304ef0 158 result = __close (lock_fd);
dcf0671d
UD
159
160 /* Mark descriptor as unused. */
161 lock_fd = -1;
162
163 /* Clear mutex. */
164 __libc_lock_unlock (lock);
165 }
166
167 return result;
168}
169weak_alias (__ulckpwdf, ulckpwdf)
170
171
172static void
bcaad6ee 173noop_handler (int sig)
dcf0671d 174{
f8b87ef0 175 /* We simply return which makes the `fcntl' call return with an error. */
dcf0671d 176}