3 * lock.c -- routines to lock/unlock files
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
12 /* Modified by Ruud de Rooij to support Miquel van Smoorenburg's liblockfile
14 * Since liblockfile locking shares most of its code with dot locking, it
15 * is enabled by defining both DOT_LOCKING and HAVE_LIBLOCKFILE.
17 * Ruud de Rooij <ruud@debian.org> Sun, 28 Mar 1999 15:34:03 +0200
21 #include <h/signals.h>
23 #ifdef TIME_WITH_SYS_TIME
24 # include <sys/time.h>
27 # ifdef TM_IN_SYS_TIME
28 # include <sys/time.h>
39 # include <mmdfonly.h>
40 # include <lockonly.h>
46 # include <sys/file.h>
49 #if defined(LOCKF_LOCKING) || defined(FLOCK_LOCKING)
50 # include <sys/file.h>
55 #if defined(HAVE_LIBLOCKFILE)
62 char *lockdir = LOCKDIR;
65 /* Are we using any kernel locking? */
66 #if defined (FLOCK_LOCKING) || defined(LOCKF_LOCKING) || defined(FCNTL_LOCKING)
67 # define KERNEL_LOCKING
72 /* struct for getting name of lock file to create */
75 #if !defined(HAVE_LIBLOCKFILE)
81 * Amount of time to wait before
82 * updating ctime of lock file.
86 #if !defined(HAVE_LIBLOCKFILE)
88 * How old does a lock file need to be
89 * before we remove it.
92 #endif /* HAVE_LIBLOCKFILE */
94 /* struct for recording and updating locks */
101 /* top of list containing all open locks */
102 static struct lock *l_top = NULL;
103 #endif /* DOT_LOCKING */
108 #ifdef KERNEL_LOCKING
109 static int lkopen_kernel (char *, int, mode_t);
113 static int lkopen_dot (char *, int, mode_t);
114 static int lockit (struct lockinfo *);
115 static void lockname (char *, struct lockinfo *, int);
116 static void timerON (char *, int);
117 static void timerOFF (int);
118 static RETSIGTYPE alrmser (int);
123 * Base routine to open and lock a file,
124 * and return a file descriptor.
128 lkopen (char *file, int access, mode_t mode)
130 #ifdef KERNEL_LOCKING
131 return lkopen_kernel(file, access, mode);
135 return lkopen_dot(file, access, mode);
141 * Base routine to close and unlock a file,
142 * given a file descriptor.
146 lkclose (int fd, char *file)
153 struct lockinfo lkinfo;
160 buf.l_type = F_UNLCK;
161 buf.l_whence = SEEK_SET;
164 fcntl(fd, F_SETLK, &buf);
172 /* make sure we unlock the whole thing */
173 lseek (fd, (off_t) 0, SEEK_SET);
174 lockf (fd, F_ULOCK, 0L);
178 lockname (file, &lkinfo, 0); /* get name of lock file */
179 #if !defined(HAVE_LIBLOCKFILE)
180 unlink (lkinfo.curlock); /* remove lock file */
182 lockfile_remove(lkinfo.curlock);
183 #endif /* HAVE_LIBLOCKFILE */
184 timerOFF (fd); /* turn off lock timer */
185 #endif /* DOT_LOCKING */
192 * Base routine to open and lock a file,
193 * and return a FILE pointer
197 lkfopen (char *file, char *mode)
202 if (strcmp (mode, "r") == 0)
207 if ((fd = lkopen (file, access, 0)) == -1)
210 if ((fp = fdopen (fd, mode)) == NULL) {
220 * Base routine to close and unlock a file,
221 * given a FILE pointer
225 lkfclose (FILE *fp, char *file)
232 struct lockinfo lkinfo;
239 buf.l_type = F_UNLCK;
240 buf.l_whence = SEEK_SET;
243 fcntl(fileno(fp), F_SETLK, &buf);
247 flock (fileno(fp), LOCK_UN);
251 /* make sure we unlock the whole thing */
252 fseek (fp, 0L, SEEK_SET);
253 lockf (fileno(fp), F_ULOCK, 0L);
257 lockname (file, &lkinfo, 0); /* get name of lock file */
258 #if !defined(HAVE_LIBLOCKFILE)
259 unlink (lkinfo.curlock); /* remove lock file */
261 lockfile_remove(lkinfo.curlock);
262 #endif /* HAVE_LIBLOCKFILE */
263 timerOFF (fileno(fp)); /* turn off lock timer */
264 #endif /* DOT_LOCKING */
266 return (fclose (fp));
270 #ifdef KERNEL_LOCKING
273 * open and lock a file, using kernel locking
277 lkopen_kernel (char *file, int access, mode_t mode)
281 # ifdef FCNTL_LOCKING
283 # endif /* FCNTL_LOCKING */
285 for (i = 0; i < 5; i++) {
287 # if defined(LOCKF_LOCKING) || defined(FCNTL_LOCKING)
288 /* remember the original mode */
291 /* make sure we open at the beginning */
295 * We MUST have write permission or
296 * lockf/fcntl() won't work
298 if ((access & 03) == O_RDONLY) {
302 # endif /* LOCKF_LOCKING || FCNTL_LOCKING */
304 if ((fd = open (file, access | O_NDELAY, mode)) == -1)
307 # ifdef FCNTL_LOCKING
308 buf.l_type = F_WRLCK;
309 buf.l_whence = SEEK_SET;
312 if (fcntl (fd, F_SETLK, &buf) != -1)
316 # ifdef FLOCK_LOCKING
317 if (flock (fd, LOCK_EX | LOCK_NB) != -1)
321 # ifdef LOCKF_LOCKING
322 if (lockf (fd, F_TLOCK, 0L) != -1) {
323 /* see if we should be at the end */
325 lseek (fd, (off_t) 0, SEEK_END);
340 #endif /* KERNEL_LOCKING */
346 * open and lock a file, using dot locking
350 lkopen_dot (char *file, int access, mode_t mode)
354 struct lockinfo lkinfo;
358 if ((fd = open (file, access, mode)) == -1)
362 * Get the name of the eventual lock file, as well
363 * as a name for a temporary lock file.
365 lockname (file, &lkinfo, 1);
367 #if !defined(HAVE_LIBLOCKFILE)
369 /* attempt to create lock file */
370 if (lockit (&lkinfo) == 0) {
371 /* if successful, turn on timer and return */
372 timerON (lkinfo.curlock, fd);
376 * Abort locking, if we fail to lock after 5 attempts
377 * and are never able to stat the lock file.
379 if (stat (lkinfo.curlock, &st) == -1) {
387 /* check for stale lockfile, else sleep */
388 if (curtime > st.st_ctime + RSECS)
389 unlink (lkinfo.curlock);
396 if (lockfile_create(lkinfo.curlock, 5, 0) == L_SUCCESS) {
397 timerON(lkinfo.curlock, fd);
404 #endif /* HAVE_LIBLOCKFILE */
407 #if !defined(HAVE_LIBLOCKFILE)
409 * Routine that actually tries to create
414 lockit (struct lockinfo *li)
417 char *curlock, *tmplock;
423 curlock = li->curlock;
424 tmplock = li->tmplock;
427 if ((fd = mkstemp(tmplock)) == -1)
430 if (mktemp(tmplock) == NULL)
432 if (unlink(tmplock) == -1 && errno != ENOENT)
434 /* create the temporary lock file */
435 if ((fd = creat(tmplock, 0600)) == -1)
440 /* write our process id into lock file */
441 snprintf (buffer, sizeof(buffer), "nmh lock: pid %d\n", (int) getpid());
442 write(fd, buffer, strlen(buffer) + 1);
448 * Now try to create the real lock file
449 * by linking to the temporary file.
451 fd = link(tmplock, curlock);
454 return (fd == -1 ? -1 : 0);
456 #endif /* HAVE_LIBLOCKFILE */
459 * Get name of lock file, and temporary lock file
463 lockname (char *file, struct lockinfo *li, int isnewlock)
472 if ((cp = strrchr (file, '/')) == NULL || *++cp == 0)
478 snprintf (bp, sizeof(li->curlock), "%s/", lockdir);
479 tmplen = strlen (bp);
484 snprintf (bp, sizeof(li->curlock), "%.*s", cp - file, file);
485 tmplen = strlen (bp);
493 * mmdf style dot locking. Currently not supported.
494 * If we start supporting mmdf style dot locking,
495 * we will need to change the return value of lockname
497 if (stat (file, &st) == -1)
500 snprintf (bp, sizeof(li->curlock) - bplen, "LCK%05d.%05d",
501 st.st_dev, st.st_ino);
504 snprintf (bp, sizeof(li->curlock) - bplen, "%s.lock", cp);
506 #if !defined(HAVE_LIBLOCKFILE)
508 * If this is for a new lock, create a name for
509 * the temporary lock file for lockit()
512 if ((cp = strrchr (li->curlock, '/')) == NULL || *++cp == 0)
513 strncpy (li->tmplock, ",LCK.XXXXXX", sizeof(li->tmplock));
515 snprintf (li->tmplock, sizeof(li->tmplock), "%.*s,LCK.XXXXXX",
516 cp - li->curlock, li->curlock);
523 * Add new lockfile to the list of open lockfiles
524 * and start the lock file timer.
528 timerON (char *curlock, int fd)
533 if (!(lp = (struct lock *) malloc (sizeof(*lp))))
536 len = strlen(curlock) + 1;
538 if (!(lp->l_lock = malloc (len))) {
542 memcpy (lp->l_lock, curlock, len);
546 /* perhaps SIGT{STP,TIN,TOU} ? */
547 SIGNAL (SIGALRM, alrmser);
556 * Search through the list of lockfiles for the
557 * current lockfile, and remove it from the list.
563 struct lock *pp, *lp;
568 for (pp = lp = l_top; lp; pp = lp, lp = lp->l_next) {
576 pp->l_next = lp->l_next;
583 /* if there are locks left, restart timer */
590 * If timer goes off, we update the ctime of all open
591 * lockfiles, so another command doesn't remove them.
601 #ifndef RELIABLE_SIGNALS
602 SIGNAL (SIGALRM, alrmser);
605 /* update the ctime of all the lock files */
606 for (lp = l_top; lp; lp = lp->l_next) {
607 lockfile = lp->l_lock;
608 #if !defined(HAVE_LIBLOCKFILE)
609 if (*lockfile && (j = creat (lockfile, 0600)) != -1)
612 lockfile_touch(lockfile);
616 /* restart the alarm */
620 #endif /* DOT_LOCKING */