chiark / gitweb /
Infrastructure: Switch testing over to Autotest.
[mLib] / sys / lock.c
CommitLineData
edb89af3 1/* -*-c-*-
2 *
edb89af3 3 * Simplified POSIX locking interface
4 *
5 * (c) 1997 Straylight/Edgeware
6 */
7
d4efbcd9 8/*----- Licensing notice --------------------------------------------------*
edb89af3 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.
d4efbcd9 16 *
edb89af3 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.
d4efbcd9 21 *
edb89af3 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
edb89af3 28/*----- Header files ------------------------------------------------------*/
29
30#include <errno.h>
988b0fdb 31#include <setjmp.h>
edb89af3 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
988b0fdb 48/*----- Static variables --------------------------------------------------*/
49
8e94a44e 50static jmp_buf jmp; /* Jump here to interrup @fcntl@ */
988b0fdb 51
edb89af3 52/*----- Main code ---------------------------------------------------------*/
53
988b0fdb 54/* --- @sigalrm@ --- *
edb89af3 55 *
56 * Arguments: @int sig@ = signal number
57 *
58 * Returns: ---
59 *
60 * Use: Makes sure that a @SIGALRM@ signal interrupts system calls.
61 */
62
988b0fdb 63static void sigalrm(int sig) { longjmp(jmp, 1); }
edb89af3 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
80int lock_file(int fd, unsigned how)
81{
82 struct flock fk;
988b0fdb 83 struct sigaction sa, osa;
d4efbcd9 84 sigset_t ss, oss;
edb89af3 85 int e;
988b0fdb 86 int al, d, left;
edb89af3 87
88 /* --- Fill in the easy bits --- */
89
90 fk.l_whence = SEEK_SET;
91 fk.l_start = 0;
898dd094 92 fk.l_len = 0;
edb89af3 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
55ed379c 101 /* --- Decide how to do the locking --- */
edb89af3 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
988b0fdb 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 --- */
55ed379c 123
988b0fdb 124 sa.sa_handler = sigalrm;
55ed379c 125 sa.sa_flags = 0;
988b0fdb 126#ifdef SA_INTERRUPT
127 sa.sa_flags |= SA_INTERRUPT;
128#endif
55ed379c 129 sigemptyset(&sa.sa_mask);
988b0fdb 130 if (sigaction(SIGALRM, &sa, &osa))
55ed379c 131 return (-1);
132
988b0fdb 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);
55ed379c 141
988b0fdb 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;
edb89af3 157 }
55ed379c 158
988b0fdb 159 /* --- Do it --- */
160
161 e = fcntl(fd, F_SETLKW, &fk);
162
163 /* --- Tidy up the mess I left --- */
55ed379c 164
988b0fdb 165 left = alarm(0);
166 if (al)
167 alarm(al - d + left);
168done:
169 sigaction(SIGALRM, &osa, 0);
edb89af3 170 return (e);
171}
172
173/*----- That's all, fokls -------------------------------------------------*/