3 * Simplified POSIX locking interface
5 * (c) 1997 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
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.
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.
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,
28 /*----- Header files ------------------------------------------------------*/
38 #include <sys/types.h>
44 /*----- Tunable constants -------------------------------------------------*/
46 #define LOCK_TIMEOUT 10 /* Maximum time in seconds to wait */
48 /*----- Static variables --------------------------------------------------*/
50 static jmp_buf jmp
; /* Jump here to interrup @fcntl@ */
52 /*----- Main code ---------------------------------------------------------*/
54 /* --- @sigalrm@ --- *
56 * Arguments: @int sig@ = signal number
60 * Use: Makes sure that a @SIGALRM@ signal interrupts system calls.
63 static void sigalrm(int sig
) { longjmp(jmp
, 1); }
65 /* --- @lock_file@ --- *
67 * Arguments: @int fd@ = file descriptor to lock
68 * @unsigned how@ = type of lock required
70 * Returns: 0 if OK, -1 if it failed.
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
80 int lock_file(int fd
, unsigned how
)
83 struct sigaction sa
, osa
;
88 /* --- Fill in the easy bits --- */
90 fk
.l_whence
= SEEK_SET
;
94 /* --- Unlocking is really easy --- */
96 if (how
== LOCK_UNLOCK
) {
98 return (fcntl(fd
, F_SETLK
, &fk
));
101 /* --- Decide how to do the locking --- */
103 if (how
== LOCK_EXCL
)
105 else if (how
== LOCK_NONEXCL
)
112 /* --- Block @SIGALRM@ for a while --- *
114 * I don't want stray alarms going off while I'm busy here.
118 sigaddset(&ss
, SIGALRM
);
119 if (sigprocmask(SIG_BLOCK
, &ss
, &oss
))
122 /* --- Set up the signal handler --- */
124 sa
.sa_handler
= sigalrm
;
127 sa
.sa_flags
|= SA_INTERRUPT
;
129 sigemptyset(&sa
.sa_mask
);
130 if (sigaction(SIGALRM
, &sa
, &osa
))
133 /* --- Set up the alarm, remembering when it's meant to go off --- */
136 if (al
&& al
< LOCK_TIMEOUT
)
142 /* --- Set up the return context for the signal handler --- */
145 sigprocmask(SIG_SETMASK
, &oss
, 0);
151 /* --- Unblock the signal and we're ready --- */
153 if (sigprocmask(SIG_SETMASK
, &oss
, 0)) {
161 e
= fcntl(fd
, F_SETLKW
, &fk
);
163 /* --- Tidy up the mess I left --- */
167 alarm(al
- d
+ left
);
169 sigaction(SIGALRM
, &osa
, 0);
173 /*----- That's all, fokls -------------------------------------------------*/