## 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
##
##----- 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.
##
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 ---
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
--- /dev/null
+/* -*-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 <errno.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#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 -------------------------------------------------*/
--- /dev/null
+/* -*-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