3 * $Id: lock.c,v 1.6 2004/04/08 01:36:13 mdw Exp $
5 * Simplified POSIX locking interface
7 * (c) 1997 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
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.
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.
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,
30 /*----- Header files ------------------------------------------------------*/
40 #include <sys/types.h>
46 /*----- Tunable constants -------------------------------------------------*/
48 #define LOCK_TIMEOUT 10 /* Maximum time in seconds to wait */
50 /*----- Static variables --------------------------------------------------*/
52 static jmp_buf jmp; /* Jump here to interrup @fcntl@ */
54 /*----- Main code ---------------------------------------------------------*/
56 /* --- @sigalrm@ --- *
58 * Arguments: @int sig@ = signal number
62 * Use: Makes sure that a @SIGALRM@ signal interrupts system calls.
65 static void sigalrm(int sig) { longjmp(jmp, 1); }
67 /* --- @lock_file@ --- *
69 * Arguments: @int fd@ = file descriptor to lock
70 * @unsigned how@ = type of lock required
72 * Returns: 0 if OK, -1 if it failed.
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
82 int lock_file(int fd, unsigned how)
85 struct sigaction sa, osa;
90 /* --- Fill in the easy bits --- */
92 fk.l_whence = SEEK_SET;
96 /* --- Unlocking is really easy --- */
98 if (how == LOCK_UNLOCK) {
100 return (fcntl(fd, F_SETLK, &fk));
103 /* --- Decide how to do the locking --- */
105 if (how == LOCK_EXCL)
107 else if (how == LOCK_NONEXCL)
114 /* --- Block @SIGALRM@ for a while --- *
116 * I don't want stray alarms going off while I'm busy here.
120 sigaddset(&ss, SIGALRM);
121 if (sigprocmask(SIG_BLOCK, &ss, &oss))
124 /* --- Set up the signal handler --- */
126 sa.sa_handler = sigalrm;
129 sa.sa_flags |= SA_INTERRUPT;
131 sigemptyset(&sa.sa_mask);
132 if (sigaction(SIGALRM, &sa, &osa))
135 /* --- Set up the alarm, remembering when it's meant to go off --- */
138 if (al && al < LOCK_TIMEOUT)
144 /* --- Set up the return context for the signal handler --- */
147 sigprocmask(SIG_SETMASK, &oss, 0);
153 /* --- Unblock the signal and we're ready --- */
155 if (sigprocmask(SIG_SETMASK, &oss, 0)) {
163 e = fcntl(fd, F_SETLKW, &fk);
165 /* --- Tidy up the mess I left --- */
169 alarm(al - d + left);
171 sigaction(SIGALRM, &osa, 0);
175 /*----- That's all, fokls -------------------------------------------------*/