3 * $Id: lock.c,v 1.4 1999/06/19 20:33:16 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 /*----- Revision history --------------------------------------------------*
33 * Revision 1.4 1999/06/19 20:33:16 mdw
34 * More sophisticated and excessive signal and alarm handling.
36 * Revision 1.3 1999/06/06 01:23:00 mdw
37 * Fix signal handling.
39 * Revision 1.2 1999/05/26 20:53:40 mdw
40 * Fixes for stupid bugs.
42 * Revision 1.1 1999/05/15 10:33:53 mdw
43 * Add simplified locking code.
47 /*----- Header files ------------------------------------------------------*/
57 #include <sys/types.h>
63 /*----- Tunable constants -------------------------------------------------*/
65 #define LOCK_TIMEOUT 10 /* Maximum time in seconds to wait */
67 /*----- Static variables --------------------------------------------------*/
69 static sigjmp_buf jmp; /* Jump here to interrup @fcntl@ */
71 /*----- Main code ---------------------------------------------------------*/
73 /* --- @sigalrm@ --- *
75 * Arguments: @int sig@ = signal number
79 * Use: Makes sure that a @SIGALRM@ signal interrupts system calls.
82 static void sigalrm(int sig) { longjmp(jmp, 1); }
84 /* --- @lock_file@ --- *
86 * Arguments: @int fd@ = file descriptor to lock
87 * @unsigned how@ = type of lock required
89 * Returns: 0 if OK, -1 if it failed.
91 * Use: Acquires a lock on the given file. The value @how@
92 * specifies the type of lock to acquire: @LOCK_EXCL@ gets
93 * an exclusive (write) lock; @LOCK_NONEXCL@ gets a non-
94 * exclusive (read) lock and @LOCK_UNLOCK@ releases any locks.
95 * Acquiring a lock gets timed out after a while with an
99 int lock_file(int fd, unsigned how)
102 struct sigaction sa, osa;
107 /* --- Fill in the easy bits --- */
109 fk.l_whence = SEEK_SET;
113 /* --- Unlocking is really easy --- */
115 if (how == LOCK_UNLOCK) {
117 return (fcntl(fd, F_SETLK, &fk));
120 /* --- Decide how to do the locking --- */
122 if (how == LOCK_EXCL)
124 else if (how == LOCK_NONEXCL)
131 /* --- Block @SIGALRM@ for a while --- *
133 * I don't want stray alarms going off while I'm busy here.
137 sigaddset(&ss, SIGALRM);
138 if (sigprocmask(SIG_BLOCK, &ss, &oss))
141 /* --- Set up the signal handler --- */
143 sa.sa_handler = sigalrm;
146 sa.sa_flags |= SA_INTERRUPT;
148 sigemptyset(&sa.sa_mask);
149 if (sigaction(SIGALRM, &sa, &osa))
152 /* --- Set up the alarm, remembering when it's meant to go off --- */
155 if (al && al < LOCK_TIMEOUT)
161 /* --- Set up the return context for the signal handler --- */
164 sigprocmask(SIG_SETMASK, &oss, 0);
170 /* --- Unblock the signal and we're ready --- */
172 if (sigprocmask(SIG_SETMASK, &oss, 0)) {
180 e = fcntl(fd, F_SETLKW, &fk);
182 /* --- Tidy up the mess I left --- */
186 alarm(al - d + left);
188 sigaction(SIGALRM, &osa, 0);
192 /*----- That's all, fokls -------------------------------------------------*/