*** empty log message ***
[glibc/history.git] / shadow / lckpwdf.c
blob60cf2eecf75e5434f10b9c6a2443beb5931d1270
1 /* lckpwdf - 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.
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
18 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
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>
30 /* Name of the lock file. */
31 #define PWD_LOCKFILE "/var/lock/lock.pwd"
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 int result;
91 if (lock_fd != -1)
92 /* Still locked by own process. */
93 return -1;
95 /* Prevent problems caused by multiple threads. */
96 __libc_lock_lock (lock);
98 lock_fd = open (PWD_LOCKFILE, O_WRONLY | O_CREAT, 0600);
99 if (lock_fd == -1)
100 /* Cannot create lock file. */
101 RETURN_CLOSE_FD (-1);
103 /* Make sure file gets correctly closed when process finished. */
104 flags = fcntl (lock_fd, F_GETFD, 0);
105 if (flags == -1)
106 /* Cannot get file flags. */
107 RETURN_CLOSE_FD (-1);
108 flags |= FD_CLOEXEC; /* Close on exit. */
109 if (fcntl (lock_fd, F_SETFD, flags) < 0)
110 /* Cannot set new flags. */
111 RETURN_CLOSE_FD (-1);
113 /* Now we have to get exclusive write access. Since multiple
114 process could try this we won't stop when it first fails.
115 Instead we set a timeout for the system call. Once the timer
116 expires it is likely that there are some problems which cannot be
117 resolved by waiting.
119 It is important that we don't change the signal state. We must
120 restore the old signal behaviour. */
121 memset (&new_act, '\0', sizeof (struct sigaction));
122 new_act.sa_handler = noop_handler;
123 sigfillset (&new_act.sa_mask);
124 new_act.sa_flags = 0ul;
126 /* Install new action handler for alarm and save old. */
127 if (sigaction (SIGALRM, &new_act, &saved_act) < 0)
128 /* Cannot install signal handler. */
129 RETURN_CLOSE_FD (-1);
131 /* Now make sure the alarm signal is not blocked. */
132 sigemptyset (&new_set);
133 sigaddset (&new_set, SIGALRM);
134 if (sigprocmask (SIG_UNBLOCK, &new_set, &saved_set) < 0)
135 RETURN_RESTORE_HANDLER (-1);
137 /* Start timer. If we cannot get the lock in the specified time we
138 get a signal. */
139 alarm (TIMEOUT);
141 /* Try to get the lock. */
142 result = flock (lock_fd, LOCK_EX);
144 RETURN_CLEAR_ALARM (result);
146 weak_alias (__lckpwdf, lckpwdf)
150 __ulckpwdf ()
152 int result;
154 if (lock_fd == -1)
155 /* There is no lock set. */
156 result = -1;
157 else
159 /* Prevent problems caused by multiple threads. */
160 __libc_lock_lock (lock);
162 result = close (lock_fd);
164 /* Mark descriptor as unused. */
165 lock_fd = -1;
167 /* Clear mutex. */
168 __libc_lock_unlock (lock);
171 return result;
173 weak_alias (__ulckpwdf, ulckpwdf)
176 static void
177 noop_handler (sig)
178 int sig;
180 /* We simply return which makes the `flock' call return with an error. */