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