OSDN Git Service

Last portion of libc_hidden_proto removal.
[uclinux-h8/uClibc.git] / libc / pwd_grp / lckpwdf.c
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.
6
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.
11
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.
16
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
20    02111-1307 USA.  */
21
22 #include <features.h>
23 #include <fcntl.h>
24 #include <signal.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <sys/file.h>
28 #include <paths.h>
29 #include <shadow.h>
30
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) */
41
42 /* How long to wait for getting the lock before returning with an
43    error.  */
44 #define TIMEOUT 15 /* sec */
45
46
47 /* File descriptor for lock file.  */
48 static int lock_fd = -1;
49
50 /* Prevent problems in multithreaded program by using mutex.  */
51 #include <bits/uClibc_mutex.h>
52 __UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
53
54
55 /* Prototypes for local functions.  */
56 static void noop_handler (int __sig);
57
58
59 int
60 lckpwdf (void)
61 {
62   int flags;
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.  */
68   int result;
69   int rv = -1;
70
71   if (lock_fd != -1)
72     /* Still locked by own process.  */
73     return -1;
74
75   /* Prevent problems caused by multiple threads.  */
76   __UCLIBC_MUTEX_LOCK(mylock);
77
78   lock_fd = open (_PATH_PASSWD, O_WRONLY);
79   if (lock_fd == -1) {
80     /* Cannot create lock file.  */
81         goto DONE;
82   }
83
84   /* Make sure file gets correctly closed when process finished.  */
85   flags = fcntl (lock_fd, F_GETFD, 0);
86   if (flags == -1) {
87     /* Cannot get file flags.  */
88     close(lock_fd);
89     lock_fd = -1;
90         goto DONE;
91   }
92   flags |= FD_CLOEXEC;          /* Close on exit.  */
93   if (fcntl (lock_fd, F_SETFD, flags) < 0) {
94     /* Cannot set new flags.  */
95     close(lock_fd);
96     lock_fd = -1;
97         goto DONE;
98   }
99
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
104      resolved by waiting.
105
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;
112
113   /* Install new action handler for alarm and save old.  */
114   if (sigaction (SIGALRM, &new_act, &saved_act) < 0) {
115     /* Cannot install signal handler.  */
116     close(lock_fd);
117     lock_fd = -1;
118         goto DONE;
119   }
120
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);
126     close(lock_fd);
127     lock_fd = -1;
128         goto DONE;
129   }
130
131   /* Start timer.  If we cannot get the lock in the specified time we
132      get a signal.  */
133   alarm (TIMEOUT);
134
135   /* Try to get the lock.  */
136   memset (&fl, '\0', sizeof (struct flock));
137   fl.l_type = F_WRLCK;
138   fl.l_whence = SEEK_SET;
139   result = fcntl (lock_fd, F_SETLKW, &fl);
140
141   /* Clear alarm.  */
142   alarm (0);
143
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);
147
148   /* Restore old action handler for alarm.  We don't need to know
149      about the current one.  */
150   sigaction (SIGALRM, &saved_act, NULL);
151
152   if (result < 0) {
153     close(lock_fd);
154     lock_fd = -1;
155         goto DONE;
156   }
157   rv = 0;
158
159 DONE:
160   __UCLIBC_MUTEX_UNLOCK(mylock);
161   return 0;
162 }
163
164
165 int
166 ulckpwdf (void)
167 {
168   int result;
169
170   if (lock_fd == -1)
171     /* There is no lock set.  */
172     result = -1;
173   else
174     {
175       /* Prevent problems caused by multiple threads.  */
176           __UCLIBC_MUTEX_LOCK(mylock);
177
178       result = close (lock_fd);
179
180       /* Mark descriptor as unused.  */
181       lock_fd = -1;
182
183       /* Clear mutex.  */
184           __UCLIBC_MUTEX_UNLOCK(mylock);
185     }
186
187   return result;
188 }
189
190
191 static void
192 noop_handler (int sig)
193 {
194   /* We simply return which makes the `fcntl' call return with an error.  */
195 }