chiark / gitweb /
Change spelling of `multiplexor'. ;-)
[mLib] / lock.c
CommitLineData
edb89af3 1/* -*-c-*-
2 *
3 * $Id: lock.c,v 1.1 1999/05/15 10:33:53 mdw Exp $
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
30/*----- Revision history --------------------------------------------------*
31 *
32 * $Log: lock.c,v $
33 * Revision 1.1 1999/05/15 10:33:53 mdw
34 * Add simplified locking code.
35 *
36 */
37
38/*----- Header files ------------------------------------------------------*/
39
40#include <errno.h>
41#include <signal.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
45#include <time.h>
46
47#include <sys/types.h>
48#include <unistd.h>
49#include <fcntl.h>
50
51#include "lock.h"
52
53/*----- Tunable constants -------------------------------------------------*/
54
55#define LOCK_TIMEOUT 10 /* Maximum time in seconds to wait */
56
57/*----- Main code ---------------------------------------------------------*/
58
59/* --- @lock_alarm@ --- *
60 *
61 * Arguments: @int sig@ = signal number
62 *
63 * Returns: ---
64 *
65 * Use: Makes sure that a @SIGALRM@ signal interrupts system calls.
66 */
67
68static void lock_alarm(int sig) { ; }
69
70/* --- @lock_file@ --- *
71 *
72 * Arguments: @int fd@ = file descriptor to lock
73 * @unsigned how@ = type of lock required
74 *
75 * Returns: 0 if OK, -1 if it failed.
76 *
77 * Use: Acquires a lock on the given file. The value @how@
78 * specifies the type of lock to acquire: @LOCK_EXCL@ gets
79 * an exclusive (write) lock; @LOCK_NONEXCL@ gets a non-
80 * exclusive (read) lock and @LOCK_UNLOCK@ releases any locks.
81 * Acquiring a lock gets timed out after a while with an
82 * error.
83 */
84
85int lock_file(int fd, unsigned how)
86{
87 struct flock fk;
88 void (*alrm)(int);
89 int e;
90
91 /* --- Fill in the easy bits --- */
92
93 fk.l_whence = SEEK_SET;
94 fk.l_start = 0;
95 fk.l_whence = 0;
96
97 /* --- Unlocking is really easy --- */
98
99 if (how == LOCK_UNLOCK) {
100 fk.l_type = F_UNLCK;
101 return (fcntl(fd, F_SETLK, &fk));
102 }
103
104 /* --- Set an alarm handler --- */
105
106 if (how == LOCK_EXCL)
107 fk.l_type = F_WRLCK;
108 else if (how == LOCK_NONEXCL)
109 fk.l_type = F_RDLCK;
110 else {
111 errno = EINVAL;
112 return (-1);
113 }
114
115 alrm = signal(SIGALRM, lock_alarm);
116 alarm(LOCK_TIMEOUT);
117 if ((e = fcntl(fd, F_SETLKW, &fk)) != 0) {
118 if (errno == EINTR)
119 errno = EAGAIN;
120 }
121 signal(SIGALRM, alrm);
122 return (e);
123}
124
125/*----- That's all, fokls -------------------------------------------------*/