From: mdw Date: Sat, 15 May 1999 10:33:53 +0000 (+0000) Subject: Add simplified locking code. X-Git-Tag: 2.0.4~323 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/mLib/commitdiff_plain/edb89af3766326a4d3ff1c9834f626502692d95e Add simplified locking code. --- diff --git a/Makefile.am b/Makefile.am index d143566..a1e1e21 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,7 +1,7 @@ ## Process this file with Automake to generate `Makefile.in' ## -*-Makefile-*- ## -## $Id: Makefile.am,v 1.5 1999/05/14 21:01:28 mdw Exp $ +## $Id: Makefile.am,v 1.6 1999/05/15 10:33:53 mdw Exp $ ## ## Building the distribution ## @@ -30,6 +30,9 @@ ##----- Revision history ---------------------------------------------------- ## ## $Log: Makefile.am,v $ +## Revision 1.6 1999/05/15 10:33:53 mdw +## Add simplified locking code. +## ## Revision 1.5 1999/05/14 21:01:28 mdw ## Integrated `select' handling bits from the background resolver project. ## @@ -60,6 +63,7 @@ lib_LIBRARIES = libmLib.a pkginclude_HEADERS = \ alloc.h crc32.h dstr.h dynarray.h exc.h mdwopt.h \ quis.h report.h sub.h sym.h testrig.h trace.h track.h \ + lock.h \ conn.h lbuf.h sel.h selbuf.h tv.h ## --- Things to put in the library --- @@ -69,4 +73,5 @@ pkginclude_HEADERS = \ libmLib_a_SOURCES = \ alloc.c crc32.c dstr.c exc.c mdwopt.c quis.c \ report.c sub.c sym.c testrig.c trace.c track.c \ + lock.c \ conn.c lbuf.c sel.c selbuf.c tv.c diff --git a/lock.c b/lock.c new file mode 100644 index 0000000..48cc813 --- /dev/null +++ b/lock.c @@ -0,0 +1,125 @@ +/* -*-c-*- + * + * $Id: lock.c,v 1.1 1999/05/15 10:33:53 mdw Exp $ + * + * Simplified POSIX locking interface + * + * (c) 1997 Straylight/Edgeware + */ + +/*----- Licensing notice --------------------------------------------------* + * + * This file is part of the mLib utilities library. + * + * mLib is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * mLib is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with mLib; if not, write to the Free + * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, + * MA 02111-1307, USA. + */ + +/*----- Revision history --------------------------------------------------* + * + * $Log: lock.c,v $ + * Revision 1.1 1999/05/15 10:33:53 mdw + * Add simplified locking code. + * + */ + +/*----- Header files ------------------------------------------------------*/ + +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "lock.h" + +/*----- Tunable constants -------------------------------------------------*/ + +#define LOCK_TIMEOUT 10 /* Maximum time in seconds to wait */ + +/*----- Main code ---------------------------------------------------------*/ + +/* --- @lock_alarm@ --- * + * + * Arguments: @int sig@ = signal number + * + * Returns: --- + * + * Use: Makes sure that a @SIGALRM@ signal interrupts system calls. + */ + +static void lock_alarm(int sig) { ; } + +/* --- @lock_file@ --- * + * + * Arguments: @int fd@ = file descriptor to lock + * @unsigned how@ = type of lock required + * + * Returns: 0 if OK, -1 if it failed. + * + * Use: Acquires a lock on the given file. The value @how@ + * specifies the type of lock to acquire: @LOCK_EXCL@ gets + * an exclusive (write) lock; @LOCK_NONEXCL@ gets a non- + * exclusive (read) lock and @LOCK_UNLOCK@ releases any locks. + * Acquiring a lock gets timed out after a while with an + * error. + */ + +int lock_file(int fd, unsigned how) +{ + struct flock fk; + void (*alrm)(int); + int e; + + /* --- Fill in the easy bits --- */ + + fk.l_whence = SEEK_SET; + fk.l_start = 0; + fk.l_whence = 0; + + /* --- Unlocking is really easy --- */ + + if (how == LOCK_UNLOCK) { + fk.l_type = F_UNLCK; + return (fcntl(fd, F_SETLK, &fk)); + } + + /* --- Set an alarm handler --- */ + + if (how == LOCK_EXCL) + fk.l_type = F_WRLCK; + else if (how == LOCK_NONEXCL) + fk.l_type = F_RDLCK; + else { + errno = EINVAL; + return (-1); + } + + alrm = signal(SIGALRM, lock_alarm); + alarm(LOCK_TIMEOUT); + if ((e = fcntl(fd, F_SETLKW, &fk)) != 0) { + if (errno == EINTR) + errno = EAGAIN; + } + signal(SIGALRM, alrm); + return (e); +} + +/*----- That's all, fokls -------------------------------------------------*/ diff --git a/lock.h b/lock.h new file mode 100644 index 0000000..3c8bed8 --- /dev/null +++ b/lock.h @@ -0,0 +1,78 @@ +/* -*-c-*- + * + * $Id: lock.h,v 1.1 1999/05/15 10:33:53 mdw Exp $ + * + * Simplified POSIX locking interface + * + * (c) 1999 Straylight/Edgeware + */ + +/*----- Licensing notice --------------------------------------------------* + * + * This file is part of the mLib utilities library. + * + * mLib is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * mLib is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with mLib; if not, write to the Free + * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, + * MA 02111-1307, USA. + */ + +/*----- Revision history --------------------------------------------------* + * + * $Log: lock.h,v $ + * Revision 1.1 1999/05/15 10:33:53 mdw + * Add simplified locking code. + * + */ + +#ifndef LOCK_H +#define LOCK_H + +#ifdef __cplusplus + extern "C" { +#endif + +/*----- Magic constants ---------------------------------------------------*/ + +enum { + LOCK_UNLOCK, /* Release a lock I obtained */ + LOCK_EXCL, /* Obtain an exclusive lock */ + LOCK_NONEXCL /* Obtain a nonexclusive lock */ +}; + +/*----- Functions provided ------------------------------------------------*/ + +/* --- @lock_file@ --- * + * + * Arguments: @int fd@ = file descriptor to lock + * @unsigned how@ = type of lock required + * + * Returns: 0 if OK, -1 if it failed. + * + * Use: Acquires a lock on the given file. The value @how@ + * specifies the type of lock to acquire: @LOCK_EXCL@ gets + * an exclusive (write) lock; @LOCK_NONEXCL@ gets a non- + * exclusive (read) lock and @LOCK_UNLOCK@ releases any locks. + * Acquiring a lock gets timed out after a while with an + * error. + */ + +extern int lock_file(int /*fd*/, unsigned /*how*/); + +/*----- That's all, folks -------------------------------------------------*/ + +#ifdef __cplusplus + } +#endif + +#endif