1 /* vi: set sw=4 ts=4: */
2 /* Handle locking of password file.
3 Copyright (C) 1996,98,2000,02 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
31 /* Experimentally off - libc_hidden_proto(memset) */
32 /* libc_hidden_proto(open) */
33 /* libc_hidden_proto(fcntl) */
34 /* libc_hidden_proto(close) */
35 /* libc_hidden_proto(sigfillset) */
36 /* libc_hidden_proto(sigaction) */
37 /* libc_hidden_proto(sigprocmask) */
38 /* libc_hidden_proto(sigaddset) */
39 /* libc_hidden_proto(sigemptyset) */
40 /* libc_hidden_proto(alarm) */
42 /* How long to wait for getting the lock before returning with an
44 #define TIMEOUT 15 /* sec */
47 /* File descriptor for lock file. */
48 static int lock_fd = -1;
50 /* Prevent problems in multithreaded program by using mutex. */
51 #include <bits/uClibc_mutex.h>
52 __UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
55 /* Prototypes for local functions. */
56 static void noop_handler (int __sig);
63 sigset_t saved_set; /* Saved set of caught signals. */
64 struct sigaction saved_act; /* Saved signal action. */
65 sigset_t new_set; /* New set of caught signals. */
66 struct sigaction new_act; /* New signal action. */
67 struct flock fl; /* Information struct for locking. */
72 /* Still locked by own process. */
75 /* Prevent problems caused by multiple threads. */
76 __UCLIBC_MUTEX_LOCK(mylock);
78 lock_fd = open (_PATH_PASSWD, O_WRONLY);
80 /* Cannot create lock file. */
84 /* Make sure file gets correctly closed when process finished. */
85 flags = fcntl (lock_fd, F_GETFD, 0);
87 /* Cannot get file flags. */
92 flags |= FD_CLOEXEC; /* Close on exit. */
93 if (fcntl (lock_fd, F_SETFD, flags) < 0) {
94 /* Cannot set new flags. */
100 /* Now we have to get exclusive write access. Since multiple
101 process could try this we won't stop when it first fails.
102 Instead we set a timeout for the system call. Once the timer
103 expires it is likely that there are some problems which cannot be
106 It is important that we don't change the signal state. We must
107 restore the old signal behaviour. */
108 memset (&new_act, '\0', sizeof (struct sigaction));
109 new_act.sa_handler = noop_handler;
110 sigfillset (&new_act.sa_mask);
111 new_act.sa_flags = 0ul;
113 /* Install new action handler for alarm and save old. */
114 if (sigaction (SIGALRM, &new_act, &saved_act) < 0) {
115 /* Cannot install signal handler. */
121 /* Now make sure the alarm signal is not blocked. */
122 sigemptyset (&new_set);
123 sigaddset (&new_set, SIGALRM);
124 if (sigprocmask (SIG_UNBLOCK, &new_set, &saved_set) < 0) {
125 sigaction (SIGALRM, &saved_act, NULL);
131 /* Start timer. If we cannot get the lock in the specified time we
135 /* Try to get the lock. */
136 memset (&fl, '\0', sizeof (struct flock));
138 fl.l_whence = SEEK_SET;
139 result = fcntl (lock_fd, F_SETLKW, &fl);
144 /* Restore old set of handled signals. We don't need to know
145 about the current one.*/
146 sigprocmask (SIG_SETMASK, &saved_set, NULL);
148 /* Restore old action handler for alarm. We don't need to know
149 about the current one. */
150 sigaction (SIGALRM, &saved_act, NULL);
160 __UCLIBC_MUTEX_UNLOCK(mylock);
171 /* There is no lock set. */
175 /* Prevent problems caused by multiple threads. */
176 __UCLIBC_MUTEX_LOCK(mylock);
178 result = close (lock_fd);
180 /* Mark descriptor as unused. */
184 __UCLIBC_MUTEX_UNLOCK(mylock);
192 noop_handler (int sig)
194 /* We simply return which makes the `fcntl' call return with an error. */