]>
Commit | Line | Data |
---|---|---|
6259ec0d UD |
1 | /* Handle locking of password file. |
2 | Copyright (C) 1996 Free Software Foundation, Inc. | |
3 | This file is part of the GNU C Library. | |
4 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996. | |
dcf0671d | 5 | |
6259ec0d UD |
6 | The GNU C Library is free software; you can redistribute it and/or |
7 | modify it under the terms of the GNU Library General Public License as | |
8 | published by the Free Software Foundation; either version 2 of the | |
9 | 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 | |
14 | Library General Public License for more details. | |
dcf0671d | 15 | |
6259ec0d UD |
16 | You should have received a copy of the GNU Library General Public |
17 | License along with the GNU C Library; see the file COPYING.LIB. If not, | |
18 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
19 | Boston, MA 02111-1307, USA. */ | |
dcf0671d UD |
20 | |
21 | #include <fcntl.h> | |
22 | #include <libc-lock.h> | |
23 | #include <shadow.h> | |
24 | #include <signal.h> | |
25 | #include <string.h> | |
26 | #include <unistd.h> | |
27 | #include <sys/file.h> | |
28 | ||
29 | ||
30 | /* Name of the lock file. */ | |
6259ec0d | 31 | #define PWD_LOCKFILE "/etc/.pwd.lock" |
dcf0671d UD |
32 | |
33 | /* How long to wait for getting the lock before returning with an | |
34 | error. */ | |
35 | #define TIMEOUT 15 /* sec */ | |
36 | ||
37 | ||
38 | /* File descriptor for lock file. */ | |
39 | static int lock_fd = -1; | |
40 | ||
41 | /* Prevent problems in multithreaded program by using mutex. */ | |
42 | __libc_lock_define_initialized (static, lock) | |
43 | ||
44 | ||
45 | /* Prototypes for local functions. */ | |
46 | static void noop_handler __P ((int __sig)); | |
47 | ||
48 | ||
49 | /* We cannot simply return in error cases. We have to close the file | |
50 | and perhaps restore the signal handler. */ | |
51 | #define RETURN_CLOSE_FD(code) \ | |
52 | do { \ | |
53 | if ((code) < 0 && lock_fd >= 0) \ | |
54 | { \ | |
55 | close (lock_fd); \ | |
56 | lock_fd = -1; \ | |
57 | } \ | |
58 | __libc_lock_unlock (lock); \ | |
59 | return (code); \ | |
60 | } while (0) | |
61 | ||
62 | #define RETURN_RESTORE_HANDLER(code) \ | |
63 | do { \ | |
64 | /* Restore old action handler for alarm. We don't need to know \ | |
65 | about the current one. */ \ | |
66 | sigaction (SIGALRM, &saved_act, NULL); \ | |
67 | RETURN_CLOSE_FD (code); \ | |
68 | } while (0) | |
69 | ||
70 | #define RETURN_CLEAR_ALARM(code) \ | |
71 | do { \ | |
72 | /* Clear alarm. */ \ | |
73 | alarm (0); \ | |
74 | /* Restore old set of handled signals. We don't need to know \ | |
75 | about the current one.*/ \ | |
76 | sigprocmask (SIG_SETMASK, &saved_set, NULL); \ | |
77 | RETURN_RESTORE_HANDLER (code); \ | |
78 | } while (0) | |
79 | ||
80 | ||
81 | int | |
82 | __lckpwdf () | |
83 | { | |
84 | int flags; | |
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 | ||
99 | lock_fd = open (PWD_LOCKFILE, O_WRONLY | O_CREAT, 0600); | |
100 | if (lock_fd == -1) | |
101 | /* Cannot create lock file. */ | |
102 | RETURN_CLOSE_FD (-1); | |
103 | ||
104 | /* Make sure file gets correctly closed when process finished. */ | |
105 | flags = fcntl (lock_fd, F_GETFD, 0); | |
106 | if (flags == -1) | |
107 | /* Cannot get file flags. */ | |
108 | RETURN_CLOSE_FD (-1); | |
109 | flags |= FD_CLOEXEC; /* Close on exit. */ | |
110 | if (fcntl (lock_fd, F_SETFD, flags) < 0) | |
111 | /* Cannot set new flags. */ | |
112 | RETURN_CLOSE_FD (-1); | |
113 | ||
114 | /* Now we have to get exclusive write access. Since multiple | |
115 | process could try this we won't stop when it first fails. | |
116 | Instead we set a timeout for the system call. Once the timer | |
117 | expires it is likely that there are some problems which cannot be | |
118 | resolved by waiting. | |
119 | ||
120 | It is important that we don't change the signal state. We must | |
121 | restore the old signal behaviour. */ | |
122 | memset (&new_act, '\0', sizeof (struct sigaction)); | |
123 | new_act.sa_handler = noop_handler; | |
124 | sigfillset (&new_act.sa_mask); | |
125 | new_act.sa_flags = 0ul; | |
126 | ||
127 | /* Install new action handler for alarm and save old. */ | |
128 | if (sigaction (SIGALRM, &new_act, &saved_act) < 0) | |
129 | /* Cannot install signal handler. */ | |
130 | RETURN_CLOSE_FD (-1); | |
131 | ||
132 | /* Now make sure the alarm signal is not blocked. */ | |
133 | sigemptyset (&new_set); | |
134 | sigaddset (&new_set, SIGALRM); | |
135 | if (sigprocmask (SIG_UNBLOCK, &new_set, &saved_set) < 0) | |
136 | RETURN_RESTORE_HANDLER (-1); | |
137 | ||
138 | /* Start timer. If we cannot get the lock in the specified time we | |
139 | get a signal. */ | |
140 | alarm (TIMEOUT); | |
141 | ||
142 | /* Try to get the lock. */ | |
f8b87ef0 UD |
143 | memset (&fl, '\0', sizeof (struct flock)); |
144 | fl.l_type = F_WRLCK; | |
145 | fl.l_whence = SEEK_SET; | |
146 | result = fcntl (lock_fd, F_SETLKW, &fl); | |
dcf0671d UD |
147 | |
148 | RETURN_CLEAR_ALARM (result); | |
149 | } | |
150 | weak_alias (__lckpwdf, lckpwdf) | |
151 | ||
152 | ||
153 | int | |
154 | __ulckpwdf () | |
155 | { | |
156 | int result; | |
157 | ||
158 | if (lock_fd == -1) | |
159 | /* There is no lock set. */ | |
160 | result = -1; | |
161 | else | |
162 | { | |
163 | /* Prevent problems caused by multiple threads. */ | |
db813f25 | 164 | __libc_lock_lock (lock); |
dcf0671d UD |
165 | |
166 | result = close (lock_fd); | |
167 | ||
168 | /* Mark descriptor as unused. */ | |
169 | lock_fd = -1; | |
170 | ||
171 | /* Clear mutex. */ | |
172 | __libc_lock_unlock (lock); | |
173 | } | |
174 | ||
175 | return result; | |
176 | } | |
177 | weak_alias (__ulckpwdf, ulckpwdf) | |
178 | ||
179 | ||
180 | static void | |
181 | noop_handler (sig) | |
182 | int sig; | |
183 | { | |
f8b87ef0 | 184 | /* We simply return which makes the `fcntl' call return with an error. */ |
dcf0671d | 185 | } |