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. */
22 #include <bits/libc-lock.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
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) \
53 if ((code) < 0 && lock_fd >= 0) \
58 __libc_lock_unlock (lock); \
62 #define RETURN_RESTORE_HANDLER(code) \
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); \
70 #define RETURN_CLEAR_ALARM(code) \
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); \
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. */
93 /* Still locked by own process. */
96 /* Prevent problems caused by multiple threads. */
97 __libc_lock_lock (lock
);
99 lock_fd
= __open (PWD_LOCKFILE
, O_WRONLY
| O_CREAT
, 0600);
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);
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
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
142 /* Try to get the lock. */
143 memset (&fl
, '\0', sizeof (struct flock
));
145 fl
.l_whence
= SEEK_SET
;
146 result
= __fcntl (lock_fd
, F_SETLKW
, &fl
);
148 RETURN_CLEAR_ALARM (result
);
150 weak_alias (__lckpwdf
, lckpwdf
)
159 /* There is no lock set. */
163 /* Prevent problems caused by multiple threads. */
164 __libc_lock_lock (lock
);
166 result
= __close (lock_fd
);
168 /* Mark descriptor as unused. */
172 __libc_lock_unlock (lock
);
177 weak_alias (__ulckpwdf
, ulckpwdf
)
184 /* We simply return which makes the `fcntl' call return with an error. */