chiark / gitweb /
@@@ fltfmt fettling
[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 <time.h>
34
35 #include <sys/types.h>
36 #include <unistd.h>
37 #include <fcntl.h>
38
39 #include "lock.h"
40
41 /*----- Tunable constants -------------------------------------------------*/
42
43 #define LOCK_TIMEOUT 10                 /* Maximum time in seconds to wait */
44
45 /*----- Static variables --------------------------------------------------*/
46
47 static jmp_buf jmp;                     /* Jump here to interrup @fcntl@ */
48
49 /*----- Main code ---------------------------------------------------------*/
50
51 /* --- @sigalrm@ --- *
52  *
53  * Arguments:   @int sig@ = signal number
54  *
55  * Returns:     ---
56  *
57  * Use:         Makes sure that a @SIGALRM@ signal interrupts system calls.
58  */
59
60 static void sigalrm(int sig) { longjmp(jmp, 1); }
61
62 /* --- @lock_file@ --- *
63  *
64  * Arguments:   @int fd@ = file descriptor to lock
65  *              @unsigned how@ = type of lock required
66  *
67  * Returns:     0 if OK, -1 if it failed.
68  *
69  * Use:         Acquires a lock on the given file.  The value @how@
70  *              specifies the type of lock to acquire: @LOCK_EXCL@ gets
71  *              an exclusive (write) lock; @LOCK_NONEXCL@ gets a non-
72  *              exclusive (read) lock and @LOCK_UNLOCK@ releases any locks.
73  *              Acquiring a lock gets timed out after a while with an
74  *              error.
75  */
76
77 int lock_file(int fd, unsigned how)
78 {
79   struct flock fk;
80   struct sigaction sa, osa;
81   sigset_t ss, oss;
82   int e;
83   int al, d, left;
84
85   /* --- Fill in the easy bits --- */
86
87   fk.l_whence = SEEK_SET;
88   fk.l_start = 0;
89   fk.l_len = 0;
90
91   /* --- Unlocking is really easy --- */
92
93   if (how == LOCK_UNLOCK) {
94     fk.l_type = F_UNLCK;
95     return (fcntl(fd, F_SETLK, &fk));
96   }
97
98   /* --- Decide how to do the locking --- */
99
100   if (how == LOCK_EXCL)
101     fk.l_type = F_WRLCK;
102   else if (how == LOCK_NONEXCL)
103     fk.l_type = F_RDLCK;
104   else {
105     errno = EINVAL;
106     return (-1);
107   }
108
109   /* --- Block @SIGALRM@ for a while --- *
110    *
111    * I don't want stray alarms going off while I'm busy here.
112    */
113
114   sigemptyset(&ss);
115   sigaddset(&ss, SIGALRM);
116   if (sigprocmask(SIG_BLOCK, &ss, &oss))
117     return (-1);
118
119   /* --- Set up the signal handler --- */
120
121   sa.sa_handler = sigalrm;
122   sa.sa_flags = 0;
123 #ifdef SA_INTERRUPT
124   sa.sa_flags |= SA_INTERRUPT;
125 #endif
126   sigemptyset(&sa.sa_mask);
127   if (sigaction(SIGALRM, &sa, &osa))
128     return (-1);
129
130   /* --- Set up the alarm, remembering when it's meant to go off --- */
131
132   al = alarm(0);
133   if (al && al < LOCK_TIMEOUT)
134     d = al;
135   else
136     d = LOCK_TIMEOUT;
137   alarm(d);
138
139   /* --- Set up the return context for the signal handler --- */
140
141   if (setjmp(jmp)) {
142     sigprocmask(SIG_SETMASK, &oss, 0);
143     errno = EINTR;
144     e = -1;
145     goto done;
146   }
147
148   /* --- Unblock the signal and we're ready --- */
149
150   if (sigprocmask(SIG_SETMASK, &oss, 0)) {
151     alarm(al);
152     e = -1;
153     goto done;
154   }
155
156   /* --- Do it --- */
157
158   e = fcntl(fd, F_SETLKW, &fk);
159
160   /* --- Tidy up the mess I left --- */
161
162   left = alarm(0);
163   if (al)
164     alarm(al - d + left);
165 done:
166   sigaction(SIGALRM, &osa, 0);
167   return (e);
168 }
169
170 /*----- That's all, fokls -------------------------------------------------*/