typo.
[glibc.git] / shadow / lckpwdf.c
blob67d9937ac2c6703b6c72411b74bd1136ff6086a5
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.
6 The GNU C Library is free software; you can redistribute it and/or
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.
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 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <fcntl.h>
22 #include <bits/libc-lock.h>
23 #include <shadow.h>
24 #include <signal.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <sys/file.h>
30 /* Name of the lock file. */
31 #define PWD_LOCKFILE "/etc/.pwd.lock"
33 /* How long to wait for getting the lock before returning with an
34 error. */
35 #define TIMEOUT 15 /* sec */
38 /* File descriptor for lock file. */
39 static int lock_fd = -1;
41 /* Prevent problems in multithreaded program by using mutex. */
42 __libc_lock_define_initialized (static, lock)
45 /* Prototypes for local functions. */
46 static void noop_handler (int __sig);
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)
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)
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)
81 int
82 __lckpwdf (void)
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. */
89 struct flock fl; /* Information struct for locking. */
90 int result;
92 if (lock_fd != -1)
93 /* Still locked by own process. */
94 return -1;
96 /* Prevent problems caused by multiple threads. */
97 __libc_lock_lock (lock);
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);
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);
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.
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;
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);
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);
138 /* Start timer. If we cannot get the lock in the specified time we
139 get a signal. */
140 alarm (TIMEOUT);
142 /* Try to get the lock. */
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);
148 RETURN_CLEAR_ALARM (result);
150 weak_alias (__lckpwdf, lckpwdf)
154 __ulckpwdf (void)
156 int result;
158 if (lock_fd == -1)
159 /* There is no lock set. */
160 result = -1;
161 else
163 /* Prevent problems caused by multiple threads. */
164 __libc_lock_lock (lock);
166 result = __close (lock_fd);
168 /* Mark descriptor as unused. */
169 lock_fd = -1;
171 /* Clear mutex. */
172 __libc_lock_unlock (lock);
175 return result;
177 weak_alias (__ulckpwdf, ulckpwdf)
180 static void
181 noop_handler (int sig)
183 /* We simply return which makes the `fcntl' call return with an error. */