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