Update.
[glibc.git] / nis / lckcache.c
blobf8c0a97296d1fa46f622dc57e2ed95cdf34e2fb3
1 /* Handle locking of NIS+ cache file.
2 Copyright (C) 1996, 1997 Free Software Foundation, Inc.
3 This file is part of the GNU C Library and based on shadow/lckfile.c.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <fcntl.h>
21 #include <bits/libc-lock.h>
22 #include <shadow.h>
23 #include <signal.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/file.h>
27 #include <rpcsvc/nis.h>
28 #include <rpcsvc/nislib.h>
29 #include <rpcsvc/nis_cache.h>
31 /* How long to wait for getting the lock before returning with an
32 error. */
33 #define TIMEOUT 5 /* sec */
36 /* File descriptor for lock file. */
37 static int lock_fd = -1;
39 /* Prevent problems in multithreaded program by using mutex. */
40 __libc_lock_define_initialized (static, lock)
43 /* Prototypes for local functions. */
44 static void noop_handler __P ((int __sig));
47 /* We cannot simply return in error cases. We have to close the file
48 and perhaps restore the signal handler. */
49 #define RETURN_CLOSE_FD(code) \
50 do { \
51 if ((code) < 0 && lock_fd >= 0) \
52 { \
53 close (lock_fd); \
54 lock_fd = -1; \
55 } \
56 __libc_lock_unlock (lock); \
57 return (code); \
58 } while (0)
60 #define RETURN_RESTORE_HANDLER(code) \
61 do { \
62 /* Restore old action handler for alarm. We don't need to know \
63 about the current one. */ \
64 sigaction (SIGALRM, &saved_act, NULL); \
65 RETURN_CLOSE_FD (code); \
66 } while (0)
68 #define RETURN_CLEAR_ALARM(code) \
69 do { \
70 /* Clear alarm. */ \
71 alarm (0); \
72 /* Restore old set of handled signals. We don't need to know \
73 about the current one.*/ \
74 sigprocmask (SIG_SETMASK, &saved_set, NULL); \
75 RETURN_RESTORE_HANDLER (code); \
76 } while (0)
79 int
80 __nis_lock_cache (void)
82 int flags;
83 sigset_t saved_set; /* Saved set of caught signals. */
84 struct sigaction saved_act; /* Saved signal action. */
85 sigset_t new_set; /* New set of caught signals. */
86 struct sigaction new_act; /* New signal action. */
87 struct flock fl; /* Information struct for locking. */
88 int result;
90 if (lock_fd != -1)
91 /* Still locked by own process. */
92 return -1;
94 /* Prevent problems caused by multiple threads. */
95 __libc_lock_lock (lock);
97 lock_fd = open (CACHELOCK, O_RDONLY|O_CREAT, 0666);
98 if (lock_fd == -1)
99 /* Cannot create lock file. */
100 RETURN_CLOSE_FD (-1);
102 /* Make sure file gets correctly closed when process finished. */
103 flags = fcntl (lock_fd, F_GETFD, 0);
104 if (flags == -1)
105 /* Cannot get file flags. */
106 RETURN_CLOSE_FD (-1);
107 flags |= FD_CLOEXEC; /* Close on exit. */
108 if (fcntl (lock_fd, F_SETFD, flags) < 0)
109 /* Cannot set new flags. */
110 RETURN_CLOSE_FD (-1);
112 /* Now we have to get exclusive write access. Since multiple
113 process could try this we won't stop when it first fails.
114 Instead we set a timeout for the system call. Once the timer
115 expires it is likely that there are some problems which cannot be
116 resolved by waiting.
118 It is important that we don't change the signal state. We must
119 restore the old signal behaviour. */
120 memset (&new_act, '\0', sizeof (struct sigaction));
121 new_act.sa_handler = noop_handler;
122 sigfillset (&new_act.sa_mask);
123 new_act.sa_flags = 0ul;
125 /* Install new action handler for alarm and save old. */
126 if (sigaction (SIGALRM, &new_act, &saved_act) < 0)
127 /* Cannot install signal handler. */
128 RETURN_CLOSE_FD (-1);
130 /* Now make sure the alarm signal is not blocked. */
131 sigemptyset (&new_set);
132 sigaddset (&new_set, SIGALRM);
133 if (sigprocmask (SIG_UNBLOCK, &new_set, &saved_set) < 0)
134 RETURN_RESTORE_HANDLER (-1);
136 /* Start timer. If we cannot get the lock in the specified time we
137 get a signal. */
138 alarm (TIMEOUT);
140 /* Try to get the lock. */
141 memset (&fl, '\0', sizeof (struct flock));
142 fl.l_type = F_RDLCK;
143 fl.l_whence = SEEK_SET;
144 result = fcntl (lock_fd, F_SETLKW, &fl);
146 RETURN_CLEAR_ALARM (result);
151 __nis_unlock_cache ()
153 int result;
155 if (lock_fd == -1)
156 /* There is no lock set. */
157 result = -1;
158 else
160 /* Prevent problems caused by multiple threads. */
161 __libc_lock_lock (lock);
163 result = close (lock_fd);
165 /* Mark descriptor as unused. */
166 lock_fd = -1;
168 /* Clear mutex. */
169 __libc_lock_unlock (lock);
172 return result;
176 static void
177 noop_handler (sig)
178 int sig;
180 /* We simply return which makes the `fcntl' call return with an error. */