/* -*-c-*-
*
- * $Id: lock.c,v 1.2 1999/05/26 20:53:40 mdw Exp $
+ * $Id: lock.c,v 1.3 1999/06/06 01:23:00 mdw Exp $
*
* Simplified POSIX locking interface
*
/*----- Revision history --------------------------------------------------*
*
* $Log: lock.c,v $
+ * Revision 1.3 1999/06/06 01:23:00 mdw
+ * Fix signal handling.
+ *
* Revision 1.2 1999/05/26 20:53:40 mdw
* Fixes for stupid bugs.
*
int lock_file(int fd, unsigned how)
{
struct flock fk;
- void (*alrm)(int);
+ struct sigaction sa, oldsa;
int e;
/* --- Fill in the easy bits --- */
return (fcntl(fd, F_SETLK, &fk));
}
- /* --- Set an alarm handler --- */
+ /* --- Decide how to do the locking --- */
if (how == LOCK_EXCL)
fk.l_type = F_WRLCK;
return (-1);
}
- alrm = signal(SIGALRM, lock_alarm);
+ /* --- Set up the alarm --- */
+
+ sa.sa_handler = lock_alarm;
+ sa.sa_flags = 0;
+ sigemptyset(&sa.sa_mask);
+ if (sigaction(SIGALRM, &sa, &oldsa))
+ return (-1);
+
+ /* --- Do it --- */
+
alarm(LOCK_TIMEOUT);
if ((e = fcntl(fd, F_SETLKW, &fk)) != 0) {
if (errno == EINTR)
errno = EAGAIN;
}
+
+ /* --- Remove the alarm handling --- */
+
alarm(0);
- signal(SIGALRM, alrm);
+ sigaction(SIGALRM, &oldsa, 0);
return (e);
}