]>
Commit | Line | Data |
---|---|---|
6259ec0d | 1 | /* Handle locking of password file. |
b168057a | 2 | Copyright (C) 1996-2015 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> | |
27 | ||
7cf89e95 UD |
28 | #include <kernel-features.h> |
29 | ||
dcf0671d UD |
30 | |
31 | /* Name of the lock file. */ | |
6259ec0d | 32 | #define PWD_LOCKFILE "/etc/.pwd.lock" |
dcf0671d UD |
33 | |
34 | /* How long to wait for getting the lock before returning with an | |
35 | error. */ | |
36 | #define TIMEOUT 15 /* sec */ | |
37 | ||
38 | ||
39 | /* File descriptor for lock file. */ | |
40 | static int lock_fd = -1; | |
41 | ||
42 | /* Prevent problems in multithreaded program by using mutex. */ | |
43 | __libc_lock_define_initialized (static, lock) | |
44 | ||
45 | ||
46 | /* Prototypes for local functions. */ | |
bcaad6ee | 47 | static void noop_handler (int __sig); |
dcf0671d UD |
48 | |
49 | ||
50 | /* We cannot simply return in error cases. We have to close the file | |
51 | and perhaps restore the signal handler. */ | |
52 | #define RETURN_CLOSE_FD(code) \ | |
53 | do { \ | |
54 | if ((code) < 0 && lock_fd >= 0) \ | |
55 | { \ | |
50304ef0 | 56 | __close (lock_fd); \ |
dcf0671d UD |
57 | lock_fd = -1; \ |
58 | } \ | |
59 | __libc_lock_unlock (lock); \ | |
60 | return (code); \ | |
61 | } while (0) | |
62 | ||
63 | #define RETURN_RESTORE_HANDLER(code) \ | |
64 | do { \ | |
65 | /* Restore old action handler for alarm. We don't need to know \ | |
66 | about the current one. */ \ | |
50304ef0 | 67 | __sigaction (SIGALRM, &saved_act, NULL); \ |
dcf0671d UD |
68 | RETURN_CLOSE_FD (code); \ |
69 | } while (0) | |
70 | ||
71 | #define RETURN_CLEAR_ALARM(code) \ | |
72 | do { \ | |
73 | /* Clear alarm. */ \ | |
74 | alarm (0); \ | |
75 | /* Restore old set of handled signals. We don't need to know \ | |
76 | about the current one.*/ \ | |
50304ef0 | 77 | __sigprocmask (SIG_SETMASK, &saved_set, NULL); \ |
dcf0671d UD |
78 | RETURN_RESTORE_HANDLER (code); \ |
79 | } while (0) | |
80 | ||
81 | ||
82 | int | |
bcaad6ee | 83 | __lckpwdf (void) |
dcf0671d | 84 | { |
dcf0671d UD |
85 | sigset_t saved_set; /* Saved set of caught signals. */ |
86 | struct sigaction saved_act; /* Saved signal action. */ | |
87 | sigset_t new_set; /* New set of caught signals. */ | |
88 | struct sigaction new_act; /* New signal action. */ | |
f8b87ef0 | 89 | struct flock fl; /* Information struct for locking. */ |
dcf0671d UD |
90 | int result; |
91 | ||
92 | if (lock_fd != -1) | |
93 | /* Still locked by own process. */ | |
94 | return -1; | |
95 | ||
96 | /* Prevent problems caused by multiple threads. */ | |
97 | __libc_lock_lock (lock); | |
98 | ||
7cf89e95 UD |
99 | int oflags = O_WRONLY | O_CREAT; |
100 | #ifdef O_CLOEXEC | |
101 | oflags |= O_CLOEXEC; | |
102 | #endif | |
103 | lock_fd = __open (PWD_LOCKFILE, oflags, 0600); | |
dcf0671d UD |
104 | if (lock_fd == -1) |
105 | /* Cannot create lock file. */ | |
106 | RETURN_CLOSE_FD (-1); | |
107 | ||
7cf89e95 UD |
108 | #ifndef __ASSUME_O_CLOEXEC |
109 | # ifdef O_CLOEXEC | |
110 | if (__have_o_cloexec <= 0) | |
111 | # endif | |
112 | { | |
113 | /* Make sure file gets correctly closed when process finished. */ | |
ed8d4fa9 | 114 | int flags = __fcntl (lock_fd, F_GETFD, 0); |
7cf89e95 UD |
115 | if (flags == -1) |
116 | /* Cannot get file flags. */ | |
117 | RETURN_CLOSE_FD (-1); | |
118 | # ifdef O_CLOEXEC | |
119 | if (__have_o_cloexec == 0) | |
120 | __have_o_cloexec = (flags & FD_CLOEXEC) == 0 ? -1 : 1; | |
121 | if (__have_o_cloexec < 0) | |
122 | # endif | |
123 | { | |
124 | flags |= FD_CLOEXEC; /* Close on exit. */ | |
125 | if (__fcntl (lock_fd, F_SETFD, flags) < 0) | |
126 | /* Cannot set new flags. */ | |
127 | RETURN_CLOSE_FD (-1); | |
128 | } | |
129 | } | |
130 | #endif | |
dcf0671d UD |
131 | |
132 | /* Now we have to get exclusive write access. Since multiple | |
133 | process could try this we won't stop when it first fails. | |
134 | Instead we set a timeout for the system call. Once the timer | |
135 | expires it is likely that there are some problems which cannot be | |
136 | resolved by waiting. | |
137 | ||
138 | It is important that we don't change the signal state. We must | |
139 | restore the old signal behaviour. */ | |
140 | memset (&new_act, '\0', sizeof (struct sigaction)); | |
141 | new_act.sa_handler = noop_handler; | |
284128f6 | 142 | __sigfillset (&new_act.sa_mask); |
dcf0671d UD |
143 | new_act.sa_flags = 0ul; |
144 | ||
145 | /* Install new action handler for alarm and save old. */ | |
50304ef0 | 146 | if (__sigaction (SIGALRM, &new_act, &saved_act) < 0) |
dcf0671d UD |
147 | /* Cannot install signal handler. */ |
148 | RETURN_CLOSE_FD (-1); | |
149 | ||
150 | /* Now make sure the alarm signal is not blocked. */ | |
284128f6 | 151 | __sigemptyset (&new_set); |
4aebaa6b | 152 | __sigaddset (&new_set, SIGALRM); |
50304ef0 | 153 | if (__sigprocmask (SIG_UNBLOCK, &new_set, &saved_set) < 0) |
dcf0671d UD |
154 | RETURN_RESTORE_HANDLER (-1); |
155 | ||
156 | /* Start timer. If we cannot get the lock in the specified time we | |
157 | get a signal. */ | |
158 | alarm (TIMEOUT); | |
159 | ||
160 | /* Try to get the lock. */ | |
f8b87ef0 UD |
161 | memset (&fl, '\0', sizeof (struct flock)); |
162 | fl.l_type = F_WRLCK; | |
163 | fl.l_whence = SEEK_SET; | |
50304ef0 | 164 | result = __fcntl (lock_fd, F_SETLKW, &fl); |
dcf0671d UD |
165 | |
166 | RETURN_CLEAR_ALARM (result); | |
167 | } | |
168 | weak_alias (__lckpwdf, lckpwdf) | |
169 | ||
170 | ||
171 | int | |
bcaad6ee | 172 | __ulckpwdf (void) |
dcf0671d UD |
173 | { |
174 | int result; | |
175 | ||
176 | if (lock_fd == -1) | |
177 | /* There is no lock set. */ | |
178 | result = -1; | |
179 | else | |
180 | { | |
181 | /* Prevent problems caused by multiple threads. */ | |
db813f25 | 182 | __libc_lock_lock (lock); |
dcf0671d | 183 | |
50304ef0 | 184 | result = __close (lock_fd); |
dcf0671d UD |
185 | |
186 | /* Mark descriptor as unused. */ | |
187 | lock_fd = -1; | |
188 | ||
189 | /* Clear mutex. */ | |
190 | __libc_lock_unlock (lock); | |
191 | } | |
192 | ||
193 | return result; | |
194 | } | |
195 | weak_alias (__ulckpwdf, ulckpwdf) | |
196 | ||
197 | ||
198 | static void | |
bcaad6ee | 199 | noop_handler (int sig) |
dcf0671d | 200 | { |
f8b87ef0 | 201 | /* We simply return which makes the `fcntl' call return with an error. */ |
dcf0671d | 202 | } |