fstat: add missing return value statement for the statx wrapping case
[uclibc-ng.git] / libc / pwd_grp / lckpwdf.c
blob296d8d98e2bae65ee841bdae90979d39b750e55d
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, see
18 <http://www.gnu.org/licenses/>. */
20 #include <features.h>
21 #include <fcntl.h>
22 #include <signal.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <sys/file.h>
26 #include <paths.h>
27 #include <shadow.h>
29 /* How long to wait for getting the lock before returning with an error. */
30 #define TIMEOUT 15 /* sec */
32 /* File descriptor for lock file. */
33 static int lock_fd = -1;
35 /* Prevent problems in multithreaded program by using mutex. */
36 #include <bits/uClibc_mutex.h>
37 __UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
39 /* Prototypes for local functions. */
40 static void noop_handler (int __sig);
43 int
44 lckpwdf (void)
46 sigset_t saved_set; /* Saved set of caught signals. */
47 struct sigaction saved_act; /* Saved signal action. */
48 sigset_t new_set; /* New set of caught signals. */
49 struct sigaction new_act; /* New signal action. */
50 struct flock fl; /* Information struct for locking. */
51 int result;
52 int rv = -1;
54 if (lock_fd != -1)
55 /* Still locked by own process. */
56 return -1;
58 /* Prevent problems caused by multiple threads. */
59 __UCLIBC_MUTEX_LOCK(mylock);
61 lock_fd = open (_PATH_PASSWD, O_WRONLY | O_CLOEXEC);
62 if (lock_fd == -1) {
63 goto DONE;
65 #ifndef __ASSUME_O_CLOEXEC
66 /* Make sure file gets correctly closed when process finished. */
67 fcntl (lock_fd, F_SETFD, FD_CLOEXEC);
68 #endif
70 /* Now we have to get exclusive write access. Since multiple
71 process could try this we won't stop when it first fails.
72 Instead we set a timeout for the system call. Once the timer
73 expires it is likely that there are some problems which cannot be
74 resolved by waiting. (sa_flags have no SA_RESTART. Thus SIGALRM
75 will EINTR fcntl(F_SETLKW)
77 It is important that we don't change the signal state. We must
78 restore the old signal behaviour. */
79 memset (&new_act, '\0', sizeof (new_act));
80 new_act.sa_handler = noop_handler;
81 __sigfillset (&new_act.sa_mask);
83 /* Install new action handler for alarm and save old.
84 * This never fails in Linux. */
85 sigaction (SIGALRM, &new_act, &saved_act);
87 /* Now make sure the alarm signal is not blocked. */
88 __sigemptyset (&new_set);
89 __sigaddset (&new_set, SIGALRM);
90 sigprocmask (SIG_UNBLOCK, &new_set, &saved_set);
92 /* Start timer. If we cannot get the lock in the specified time we
93 get a signal. */
94 alarm (TIMEOUT);
96 /* Try to get the lock. */
97 memset (&fl, '\0', sizeof (fl));
98 if (F_WRLCK)
99 fl.l_type = F_WRLCK;
100 if (SEEK_SET)
101 fl.l_whence = SEEK_SET;
102 result = fcntl (lock_fd, F_SETLKW, &fl);
104 /* Clear alarm. */
105 alarm (0);
107 sigprocmask (SIG_SETMASK, &saved_set, NULL);
108 sigaction (SIGALRM, &saved_act, NULL);
110 if (result < 0) {
111 close(lock_fd);
112 lock_fd = -1;
113 goto DONE;
115 rv = 0;
117 DONE:
118 __UCLIBC_MUTEX_UNLOCK(mylock);
119 return rv;
124 ulckpwdf (void)
126 int result;
128 if (lock_fd == -1)
129 /* There is no lock set. */
130 result = -1;
131 else
133 /* Prevent problems caused by multiple threads. */
134 __UCLIBC_MUTEX_LOCK(mylock);
136 result = close (lock_fd);
138 /* Mark descriptor as unused. */
139 lock_fd = -1;
141 /* Clear mutex. */
142 __UCLIBC_MUTEX_UNLOCK(mylock);
145 return result;
149 static void
150 noop_handler (int sig attribute_unused) {
152 /* We simply return which makes the `fcntl' call return with an error. */