struct/dstr-putf.c: Remove apparently redundant inclusion of <math.h>.
[mLib] / sys / lock.c
1 /* -*-c-*-
2 *
3 * Simplified POSIX locking interface
4 *
5 * (c) 1997 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of the mLib utilities library.
11 *
12 * mLib is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
16 *
17 * mLib is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
21 *
22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * MA 02111-1307, USA.
26 */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include <errno.h>
31 #include <setjmp.h>
32 #include <signal.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <time.h>
37
38 #include <sys/types.h>
39 #include <unistd.h>
40 #include <fcntl.h>
41
42 #include "lock.h"
43
44 /*----- Tunable constants -------------------------------------------------*/
45
46 #define LOCK_TIMEOUT 10 /* Maximum time in seconds to wait */
47
48 /*----- Static variables --------------------------------------------------*/
49
50 static jmp_buf jmp; /* Jump here to interrup @fcntl@ */
51
52 /*----- Main code ---------------------------------------------------------*/
53
54 /* --- @sigalrm@ --- *
55 *
56 * Arguments: @int sig@ = signal number
57 *
58 * Returns: ---
59 *
60 * Use: Makes sure that a @SIGALRM@ signal interrupts system calls.
61 */
62
63 static void sigalrm(int sig) { longjmp(jmp, 1); }
64
65 /* --- @lock_file@ --- *
66 *
67 * Arguments: @int fd@ = file descriptor to lock
68 * @unsigned how@ = type of lock required
69 *
70 * Returns: 0 if OK, -1 if it failed.
71 *
72 * Use: Acquires a lock on the given file. The value @how@
73 * specifies the type of lock to acquire: @LOCK_EXCL@ gets
74 * an exclusive (write) lock; @LOCK_NONEXCL@ gets a non-
75 * exclusive (read) lock and @LOCK_UNLOCK@ releases any locks.
76 * Acquiring a lock gets timed out after a while with an
77 * error.
78 */
79
80 int lock_file(int fd, unsigned how)
81 {
82 struct flock fk;
83 struct sigaction sa, osa;
84 sigset_t ss, oss;
85 int e;
86 int al, d, left;
87
88 /* --- Fill in the easy bits --- */
89
90 fk.l_whence = SEEK_SET;
91 fk.l_start = 0;
92 fk.l_len = 0;
93
94 /* --- Unlocking is really easy --- */
95
96 if (how == LOCK_UNLOCK) {
97 fk.l_type = F_UNLCK;
98 return (fcntl(fd, F_SETLK, &fk));
99 }
100
101 /* --- Decide how to do the locking --- */
102
103 if (how == LOCK_EXCL)
104 fk.l_type = F_WRLCK;
105 else if (how == LOCK_NONEXCL)
106 fk.l_type = F_RDLCK;
107 else {
108 errno = EINVAL;
109 return (-1);
110 }
111
112 /* --- Block @SIGALRM@ for a while --- *
113 *
114 * I don't want stray alarms going off while I'm busy here.
115 */
116
117 sigemptyset(&ss);
118 sigaddset(&ss, SIGALRM);
119 if (sigprocmask(SIG_BLOCK, &ss, &oss))
120 return (-1);
121
122 /* --- Set up the signal handler --- */
123
124 sa.sa_handler = sigalrm;
125 sa.sa_flags = 0;
126 #ifdef SA_INTERRUPT
127 sa.sa_flags |= SA_INTERRUPT;
128 #endif
129 sigemptyset(&sa.sa_mask);
130 if (sigaction(SIGALRM, &sa, &osa))
131 return (-1);
132
133 /* --- Set up the alarm, remembering when it's meant to go off --- */
134
135 al = alarm(0);
136 if (al && al < LOCK_TIMEOUT)
137 d = al;
138 else
139 d = LOCK_TIMEOUT;
140 alarm(d);
141
142 /* --- Set up the return context for the signal handler --- */
143
144 if (setjmp(jmp)) {
145 sigprocmask(SIG_SETMASK, &oss, 0);
146 errno = EINTR;
147 e = -1;
148 goto done;
149 }
150
151 /* --- Unblock the signal and we're ready --- */
152
153 if (sigprocmask(SIG_SETMASK, &oss, 0)) {
154 alarm(al);
155 e = -1;
156 goto done;
157 }
158
159 /* --- Do it --- */
160
161 e = fcntl(fd, F_SETLKW, &fk);
162
163 /* --- Tidy up the mess I left --- */
164
165 left = alarm(0);
166 if (al)
167 alarm(al - d + left);
168 done:
169 sigaction(SIGALRM, &osa, 0);
170 return (e);
171 }
172
173 /*----- That's all, fokls -------------------------------------------------*/