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