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 ------------------------------------------------------*/
35 #include <sys/types.h>
41 /*----- Tunable constants -------------------------------------------------*/
43 #define LOCK_TIMEOUT 10 /* Maximum time in seconds to wait */
45 /*----- Static variables --------------------------------------------------*/
47 static jmp_buf jmp; /* Jump here to interrup @fcntl@ */
49 /*----- Main code ---------------------------------------------------------*/
51 /* --- @sigalrm@ --- *
53 * Arguments: @int sig@ = signal number
57 * Use: Makes sure that a @SIGALRM@ signal interrupts system calls.
60 static void sigalrm(int sig) { longjmp(jmp, 1); }
62 /* --- @lock_file@ --- *
64 * Arguments: @int fd@ = file descriptor to lock
65 * @unsigned how@ = type of lock required
67 * Returns: 0 if OK, -1 if it failed.
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
77 int lock_file(int fd, unsigned how)
80 struct sigaction sa, osa;
85 /* --- Fill in the easy bits --- */
87 fk.l_whence = SEEK_SET;
91 /* --- Unlocking is really easy --- */
93 if (how == LOCK_UNLOCK) {
95 return (fcntl(fd, F_SETLK, &fk));
98 /* --- Decide how to do the locking --- */
100 if (how == LOCK_EXCL)
102 else if (how == LOCK_NONEXCL)
109 /* --- Block @SIGALRM@ for a while --- *
111 * I don't want stray alarms going off while I'm busy here.
115 sigaddset(&ss, SIGALRM);
116 if (sigprocmask(SIG_BLOCK, &ss, &oss))
119 /* --- Set up the signal handler --- */
121 sa.sa_handler = sigalrm;
124 sa.sa_flags |= SA_INTERRUPT;
126 sigemptyset(&sa.sa_mask);
127 if (sigaction(SIGALRM, &sa, &osa))
130 /* --- Set up the alarm, remembering when it's meant to go off --- */
133 if (al && al < LOCK_TIMEOUT)
139 /* --- Set up the return context for the signal handler --- */
142 sigprocmask(SIG_SETMASK, &oss, 0);
148 /* --- Unblock the signal and we're ready --- */
150 if (sigprocmask(SIG_SETMASK, &oss, 0)) {
158 e = fcntl(fd, F_SETLKW, &fk);
160 /* --- Tidy up the mess I left --- */
164 alarm(al - d + left);
166 sigaction(SIGALRM, &osa, 0);
170 /*----- That's all, fokls -------------------------------------------------*/