edb89af3 |
1 | /* -*-c-*- |
2 | * |
8656dc50 |
3 | * $Id: lock.c,v 1.6 2004/04/08 01:36:13 mdw Exp $ |
edb89af3 |
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 | |
edb89af3 |
30 | /*----- Header files ------------------------------------------------------*/ |
31 | |
32 | #include <errno.h> |
988b0fdb |
33 | #include <setjmp.h> |
edb89af3 |
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 | |
988b0fdb |
50 | /*----- Static variables --------------------------------------------------*/ |
51 | |
8e94a44e |
52 | static jmp_buf jmp; /* Jump here to interrup @fcntl@ */ |
988b0fdb |
53 | |
edb89af3 |
54 | /*----- Main code ---------------------------------------------------------*/ |
55 | |
988b0fdb |
56 | /* --- @sigalrm@ --- * |
edb89af3 |
57 | * |
58 | * Arguments: @int sig@ = signal number |
59 | * |
60 | * Returns: --- |
61 | * |
62 | * Use: Makes sure that a @SIGALRM@ signal interrupts system calls. |
63 | */ |
64 | |
988b0fdb |
65 | static void sigalrm(int sig) { longjmp(jmp, 1); } |
edb89af3 |
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; |
988b0fdb |
85 | struct sigaction sa, osa; |
86 | sigset_t ss, oss; |
edb89af3 |
87 | int e; |
988b0fdb |
88 | int al, d, left; |
edb89af3 |
89 | |
90 | /* --- Fill in the easy bits --- */ |
91 | |
92 | fk.l_whence = SEEK_SET; |
93 | fk.l_start = 0; |
898dd094 |
94 | fk.l_len = 0; |
edb89af3 |
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 | |
55ed379c |
103 | /* --- Decide how to do the locking --- */ |
edb89af3 |
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 | |
988b0fdb |
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 --- */ |
55ed379c |
125 | |
988b0fdb |
126 | sa.sa_handler = sigalrm; |
55ed379c |
127 | sa.sa_flags = 0; |
988b0fdb |
128 | #ifdef SA_INTERRUPT |
129 | sa.sa_flags |= SA_INTERRUPT; |
130 | #endif |
55ed379c |
131 | sigemptyset(&sa.sa_mask); |
988b0fdb |
132 | if (sigaction(SIGALRM, &sa, &osa)) |
55ed379c |
133 | return (-1); |
134 | |
988b0fdb |
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); |
55ed379c |
143 | |
988b0fdb |
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; |
edb89af3 |
159 | } |
55ed379c |
160 | |
988b0fdb |
161 | /* --- Do it --- */ |
162 | |
163 | e = fcntl(fd, F_SETLKW, &fk); |
164 | |
165 | /* --- Tidy up the mess I left --- */ |
55ed379c |
166 | |
988b0fdb |
167 | left = alarm(0); |
168 | if (al) |
169 | alarm(al - d + left); |
170 | done: |
171 | sigaction(SIGALRM, &osa, 0); |
edb89af3 |
172 | return (e); |
173 | } |
174 | |
175 | /*----- That's all, fokls -------------------------------------------------*/ |