Update.
[glibc.git] / shadow / lckpwdf.c
blob861e0e0cd2b3295bd554ca74aa722776c97057a2
1 /* Handle locking of password file.
2 Copyright (C) 1996, 1998 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 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.
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.
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. */
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 __P ((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 ()
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 ()
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 (sig)
182 int sig;
184 /* We simply return which makes the `fcntl' call return with an error. */