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)
60 char *lockdir = LOCKDIR;
63 /* Are we using any kernel locking? */
64 #if defined (FLOCK_LOCKING) || defined(LOCKF_LOCKING) || defined(FCNTL_LOCKING)
65 # define KERNEL_LOCKING
70 /* struct for getting name of lock file to create */
73 #if !defined(HAVE_LIBLOCKFILE)
79 * Amount of time to wait before
80 * updating ctime of lock file.
84 #if !defined(HAVE_LIBLOCKFILE)
86 * How old does a lock file need to be
87 * before we remove it.
90 #endif /* HAVE_LIBLOCKFILE */
92 /* struct for recording and updating locks */
99 /* top of list containing all open locks */
100 static struct lock *l_top = NULL;
101 #endif /* DOT_LOCKING */
106 #ifdef KERNEL_LOCKING
107 static int lkopen_kernel (char *, int, mode_t);
111 static int lkopen_dot (char *, int, mode_t);
112 static int lockit (struct lockinfo *);
113 static void lockname (char *, struct lockinfo *, int);
114 static void timerON (char *, int);
115 static void timerOFF (int);
116 static RETSIGTYPE alrmser (int);
121 * Base routine to open and lock a file,
122 * and return a file descriptor.
126 lkopen (char *file, int access, mode_t mode)
128 #ifdef KERNEL_LOCKING
129 return lkopen_kernel(file, access, mode);
133 return lkopen_dot(file, access, mode);
139 * Base routine to close and unlock a file,
140 * given a file descriptor.
144 lkclose (int fd, char *file)
151 struct lockinfo lkinfo;
158 buf.l_type = F_UNLCK;
159 buf.l_whence = SEEK_SET;
162 fcntl(fd, F_SETLK, &buf);
170 /* make sure we unlock the whole thing */
171 lseek (fd, (off_t) 0, SEEK_SET);
172 lockf (fd, F_ULOCK, 0L);
176 lockname (file, &lkinfo, 0); /* get name of lock file */
177 #if !defined(HAVE_LIBLOCKFILE)
178 unlink (lkinfo.curlock); /* remove lock file */
180 lockfile_remove(lkinfo.curlock);
181 #endif /* HAVE_LIBLOCKFILE */
182 timerOFF (fd); /* turn off lock timer */
183 #endif /* DOT_LOCKING */
190 * Base routine to open and lock a file,
191 * and return a FILE pointer
195 lkfopen (char *file, char *mode)
200 if (strcmp (mode, "r") == 0)
202 else if (strcmp (mode, "r+") == 0)
204 else if (strcmp (mode, "w") == 0)
205 access = O_WRONLY | O_CREAT | O_TRUNC;
206 else if (strcmp (mode, "w+") == 0)
207 access = O_RDWR | O_CREAT | O_TRUNC;
208 else if (strcmp (mode, "a") == 0)
209 access = O_WRONLY | O_CREAT | O_APPEND;
210 else if (strcmp (mode, "a+") == 0)
211 access = O_RDWR | O_CREAT | O_APPEND;
217 if ((fd = lkopen (file, access, 0666)) == -1)
220 if ((fp = fdopen (fd, mode)) == NULL) {
230 * Base routine to close and unlock a file,
231 * given a FILE pointer
235 lkfclose (FILE *fp, char *file)
242 struct lockinfo lkinfo;
249 buf.l_type = F_UNLCK;
250 buf.l_whence = SEEK_SET;
253 fcntl(fileno(fp), F_SETLK, &buf);
257 flock (fileno(fp), LOCK_UN);
261 /* make sure we unlock the whole thing */
262 fseek (fp, 0L, SEEK_SET);
263 lockf (fileno(fp), F_ULOCK, 0L);
267 lockname (file, &lkinfo, 0); /* get name of lock file */
268 #if !defined(HAVE_LIBLOCKFILE)
269 unlink (lkinfo.curlock); /* remove lock file */
271 lockfile_remove(lkinfo.curlock);
272 #endif /* HAVE_LIBLOCKFILE */
273 timerOFF (fileno(fp)); /* turn off lock timer */
274 #endif /* DOT_LOCKING */
276 return (fclose (fp));
280 #ifdef KERNEL_LOCKING
283 * open and lock a file, using kernel locking
287 lkopen_kernel (char *file, int access, mode_t mode)
291 # ifdef FCNTL_LOCKING
293 # endif /* FCNTL_LOCKING */
295 for (i = 0; i < 5; i++) {
297 # if defined(LOCKF_LOCKING) || defined(FCNTL_LOCKING)
298 /* remember the original mode */
301 /* make sure we open at the beginning */
305 * We MUST have write permission or
306 * lockf/fcntl() won't work
308 if ((access & 03) == O_RDONLY) {
312 # endif /* LOCKF_LOCKING || FCNTL_LOCKING */
314 if ((fd = open (file, access | O_NDELAY, mode)) == -1)
317 # ifdef FCNTL_LOCKING
318 buf.l_type = F_WRLCK;
319 buf.l_whence = SEEK_SET;
322 if (fcntl (fd, F_SETLK, &buf) != -1)
326 # ifdef FLOCK_LOCKING
327 if (flock (fd, (((access & 03) == O_RDONLY) ? LOCK_SH : LOCK_EX)
332 # ifdef LOCKF_LOCKING
333 if (lockf (fd, F_TLOCK, 0L) != -1) {
334 /* see if we should be at the end */
336 lseek (fd, (off_t) 0, SEEK_END);
351 #endif /* KERNEL_LOCKING */
357 * open and lock a file, using dot locking
361 lkopen_dot (char *file, int access, mode_t mode)
365 struct lockinfo lkinfo;
369 if ((fd = open (file, access, mode)) == -1)
373 * Get the name of the eventual lock file, as well
374 * as a name for a temporary lock file.
376 lockname (file, &lkinfo, 1);
378 #if !defined(HAVE_LIBLOCKFILE)
380 /* attempt to create lock file */
381 if (lockit (&lkinfo) == 0) {
382 /* if successful, turn on timer and return */
383 timerON (lkinfo.curlock, fd);
387 * Abort locking, if we fail to lock after 5 attempts
388 * and are never able to stat the lock file.
390 if (stat (lkinfo.curlock, &st) == -1) {
398 /* check for stale lockfile, else sleep */
399 if (curtime > st.st_ctime + RSECS)
400 unlink (lkinfo.curlock);
404 lockname (file, &lkinfo, 1);
408 if (lockfile_create(lkinfo.curlock, 5, 0) == L_SUCCESS) {
409 timerON(lkinfo.curlock, fd);
416 #endif /* HAVE_LIBLOCKFILE */
419 #if !defined(HAVE_LIBLOCKFILE)
421 * Routine that actually tries to create
426 lockit (struct lockinfo *li)
429 char *curlock, *tmplock;
435 curlock = li->curlock;
436 tmplock = li->tmplock;
439 if ((fd = mkstemp(tmplock)) == -1)
442 if (mktemp(tmplock) == NULL)
444 if (unlink(tmplock) == -1 && errno != ENOENT)
446 /* create the temporary lock file */
447 if ((fd = creat(tmplock, 0600)) == -1)
452 /* write our process id into lock file */
453 snprintf (buffer, sizeof(buffer), "nmh lock: pid %d\n", (int) getpid());
454 write(fd, buffer, strlen(buffer) + 1);
460 * Now try to create the real lock file
461 * by linking to the temporary file.
463 fd = link(tmplock, curlock);
466 return (fd == -1 ? -1 : 0);
468 #endif /* HAVE_LIBLOCKFILE */
471 * Get name of lock file, and temporary lock file
475 lockname (char *file, struct lockinfo *li, int isnewlock)
484 if ((cp = strrchr (file, '/')) == NULL || *++cp == 0)
490 snprintf (bp, sizeof(li->curlock), "%s/", lockdir);
491 tmplen = strlen (bp);
496 snprintf (bp, sizeof(li->curlock), "%.*s", cp - file, file);
497 tmplen = strlen (bp);
505 * mmdf style dot locking. Currently not supported.
506 * If we start supporting mmdf style dot locking,
507 * we will need to change the return value of lockname
509 if (stat (file, &st) == -1)
512 snprintf (bp, sizeof(li->curlock) - bplen, "LCK%05d.%05d",
513 st.st_dev, st.st_ino);
516 snprintf (bp, sizeof(li->curlock) - bplen, "%s.lock", cp);
518 #if !defined(HAVE_LIBLOCKFILE)
520 * If this is for a new lock, create a name for
521 * the temporary lock file for lockit()
524 if ((cp = strrchr (li->curlock, '/')) == NULL || *++cp == 0)
525 strncpy (li->tmplock, ",LCK.XXXXXX", sizeof(li->tmplock));
527 snprintf (li->tmplock, sizeof(li->tmplock), "%.*s,LCK.XXXXXX",
528 cp - li->curlock, li->curlock);
535 * Add new lockfile to the list of open lockfiles
536 * and start the lock file timer.
540 timerON (char *curlock, int fd)
545 if (!(lp = (struct lock *) malloc (sizeof(*lp))))
548 len = strlen(curlock) + 1;
550 if (!(lp->l_lock = malloc (len))) {
554 memcpy (lp->l_lock, curlock, len);
558 /* perhaps SIGT{STP,TIN,TOU} ? */
559 SIGNAL (SIGALRM, alrmser);
568 * Search through the list of lockfiles for the
569 * current lockfile, and remove it from the list.
575 struct lock *pp, *lp;
580 for (pp = lp = l_top; lp; pp = lp, lp = lp->l_next) {
588 pp->l_next = lp->l_next;
595 /* if there are locks left, restart timer */
602 * If timer goes off, we update the ctime of all open
603 * lockfiles, so another command doesn't remove them.
613 #ifndef RELIABLE_SIGNALS
614 SIGNAL (SIGALRM, alrmser);
617 /* update the ctime of all the lock files */
618 for (lp = l_top; lp; lp = lp->l_next) {
619 lockfile = lp->l_lock;
620 #if !defined(HAVE_LIBLOCKFILE)
621 if (*lockfile && (j = creat (lockfile, 0600)) != -1)
624 lockfile_touch(lockfile);
628 /* restart the alarm */
632 #endif /* DOT_LOCKING */