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>
24 #ifdef TIME_WITH_SYS_TIME
25 # include <sys/time.h>
28 # ifdef TM_IN_SYS_TIME
29 # include <sys/time.h>
40 # include <mmdfonly.h>
41 # include <lockonly.h>
47 # include <sys/file.h>
50 #if defined(LOCKF_LOCKING) || defined(FLOCK_LOCKING)
51 # include <sys/file.h>
56 #if defined(HAVE_LIBLOCKFILE)
61 char *lockdir = LOCKDIR;
64 /* Are we using any kernel locking? */
65 #if defined (FLOCK_LOCKING) || defined(LOCKF_LOCKING) || defined(FCNTL_LOCKING)
66 # define KERNEL_LOCKING
71 /* struct for getting name of lock file to create */
74 #if !defined(HAVE_LIBLOCKFILE)
80 * Amount of time to wait before
81 * updating ctime of lock file.
85 #if !defined(HAVE_LIBLOCKFILE)
87 * How old does a lock file need to be
88 * before we remove it.
91 #endif /* HAVE_LIBLOCKFILE */
93 /* struct for recording and updating locks */
100 /* top of list containing all open locks */
101 static struct lock *l_top = NULL;
102 #endif /* DOT_LOCKING */
107 #ifdef KERNEL_LOCKING
108 static int lkopen_kernel (char *, int, mode_t);
112 static int lkopen_dot (char *, int, mode_t);
113 static int lockit (struct lockinfo *);
114 static void lockname (char *, struct lockinfo *, int);
115 static void timerON (char *, int);
116 static void timerOFF (int);
117 static RETSIGTYPE alrmser (int);
122 * Base routine to open and lock a file,
123 * and return a file descriptor.
127 lkopen (char *file, int access, mode_t mode)
129 #ifdef KERNEL_LOCKING
130 return lkopen_kernel(file, access, mode);
134 return lkopen_dot(file, access, mode);
140 * Base routine to close and unlock a file,
141 * given a file descriptor.
145 lkclose (int fd, char *file)
152 struct lockinfo lkinfo;
159 buf.l_type = F_UNLCK;
160 buf.l_whence = SEEK_SET;
163 fcntl(fd, F_SETLK, &buf);
171 /* make sure we unlock the whole thing */
172 lseek (fd, (off_t) 0, SEEK_SET);
173 lockf (fd, F_ULOCK, 0L);
177 lockname (file, &lkinfo, 0); /* get name of lock file */
178 #if !defined(HAVE_LIBLOCKFILE)
179 unlink (lkinfo.curlock); /* remove lock file */
181 lockfile_remove(lkinfo.curlock);
182 #endif /* HAVE_LIBLOCKFILE */
183 timerOFF (fd); /* turn off lock timer */
184 #endif /* DOT_LOCKING */
191 * Base routine to open and lock a file,
192 * and return a FILE pointer
196 lkfopen (char *file, char *mode)
201 if (strcmp (mode, "r") == 0)
203 else if (strcmp (mode, "r+") == 0)
205 else if (strcmp (mode, "w") == 0)
206 access = O_WRONLY | O_CREAT | O_TRUNC;
207 else if (strcmp (mode, "w+") == 0)
208 access = O_RDWR | O_CREAT | O_TRUNC;
209 else if (strcmp (mode, "a") == 0)
210 access = O_WRONLY | O_CREAT | O_APPEND;
211 else if (strcmp (mode, "a+") == 0)
212 access = O_RDWR | O_CREAT | O_APPEND;
218 if ((fd = lkopen (file, access, 0666)) == -1)
221 if ((fp = fdopen (fd, mode)) == NULL) {
231 * Base routine to close and unlock a file,
232 * given a FILE pointer
236 lkfclose (FILE *fp, char *file)
243 struct lockinfo lkinfo;
250 buf.l_type = F_UNLCK;
251 buf.l_whence = SEEK_SET;
254 fcntl(fileno(fp), F_SETLK, &buf);
258 flock (fileno(fp), LOCK_UN);
262 /* make sure we unlock the whole thing */
263 fseek (fp, 0L, SEEK_SET);
264 lockf (fileno(fp), F_ULOCK, 0L);
268 lockname (file, &lkinfo, 0); /* get name of lock file */
269 #if !defined(HAVE_LIBLOCKFILE)
270 unlink (lkinfo.curlock); /* remove lock file */
272 lockfile_remove(lkinfo.curlock);
273 #endif /* HAVE_LIBLOCKFILE */
274 timerOFF (fileno(fp)); /* turn off lock timer */
275 #endif /* DOT_LOCKING */
277 return (fclose (fp));
281 #ifdef KERNEL_LOCKING
284 * open and lock a file, using kernel locking
288 lkopen_kernel (char *file, int access, mode_t mode)
292 # ifdef FCNTL_LOCKING
294 # endif /* FCNTL_LOCKING */
296 for (i = 0; i < 5; i++) {
298 # if defined(LOCKF_LOCKING) || defined(FCNTL_LOCKING)
299 /* remember the original mode */
302 /* make sure we open at the beginning */
306 * We MUST have write permission or
307 * lockf/fcntl() won't work
309 if ((access & 03) == O_RDONLY) {
313 # endif /* LOCKF_LOCKING || FCNTL_LOCKING */
315 if ((fd = open (file, access | O_NDELAY, mode)) == -1)
318 # ifdef FCNTL_LOCKING
319 buf.l_type = F_WRLCK;
320 buf.l_whence = SEEK_SET;
323 if (fcntl (fd, F_SETLK, &buf) != -1)
327 # ifdef FLOCK_LOCKING
328 if (flock (fd, (((access & 03) == O_RDONLY) ? LOCK_SH : LOCK_EX)
333 # ifdef LOCKF_LOCKING
334 if (lockf (fd, F_TLOCK, 0L) != -1) {
335 /* see if we should be at the end */
337 lseek (fd, (off_t) 0, SEEK_END);
352 #endif /* KERNEL_LOCKING */
358 * open and lock a file, using dot locking
362 lkopen_dot (char *file, int access, mode_t mode)
366 struct lockinfo lkinfo;
370 if ((fd = open (file, access, mode)) == -1)
374 * Get the name of the eventual lock file, as well
375 * as a name for a temporary lock file.
377 lockname (file, &lkinfo, 1);
379 #if !defined(HAVE_LIBLOCKFILE)
381 /* attempt to create lock file */
382 if (lockit (&lkinfo) == 0) {
383 /* if successful, turn on timer and return */
384 timerON (lkinfo.curlock, fd);
388 * Abort locking, if we fail to lock after 5 attempts
389 * and are never able to stat the lock file.
391 if (stat (lkinfo.curlock, &st) == -1) {
399 /* check for stale lockfile, else sleep */
400 if (curtime > st.st_ctime + RSECS)
401 unlink (lkinfo.curlock);
405 lockname (file, &lkinfo, 1);
409 if (lockfile_create(lkinfo.curlock, 5, 0) == L_SUCCESS) {
410 timerON(lkinfo.curlock, fd);
417 #endif /* HAVE_LIBLOCKFILE */
420 #if !defined(HAVE_LIBLOCKFILE)
422 * Routine that actually tries to create
427 lockit (struct lockinfo *li)
430 char *curlock, *tmplock;
436 curlock = li->curlock;
437 tmplock = li->tmplock;
440 if ((fd = mkstemp(tmplock)) == -1)
443 if (mktemp(tmplock) == NULL)
445 if (unlink(tmplock) == -1 && errno != ENOENT)
447 /* create the temporary lock file */
448 if ((fd = creat(tmplock, 0600)) == -1)
453 /* write our process id into lock file */
454 snprintf (buffer, sizeof(buffer), "nmh lock: pid %d\n", (int) getpid());
455 write(fd, buffer, strlen(buffer) + 1);
461 * Now try to create the real lock file
462 * by linking to the temporary file.
464 fd = link(tmplock, curlock);
467 return (fd == -1 ? -1 : 0);
469 #endif /* HAVE_LIBLOCKFILE */
472 * Get name of lock file, and temporary lock file
476 lockname (char *file, struct lockinfo *li, int isnewlock)
485 if ((cp = strrchr (file, '/')) == NULL || *++cp == 0)
491 snprintf (bp, sizeof(li->curlock), "%s/", lockdir);
492 tmplen = strlen (bp);
497 snprintf (bp, sizeof(li->curlock), "%.*s", cp - file, file);
498 tmplen = strlen (bp);
506 * mmdf style dot locking. Currently not supported.
507 * If we start supporting mmdf style dot locking,
508 * we will need to change the return value of lockname
510 if (stat (file, &st) == -1)
513 snprintf (bp, sizeof(li->curlock) - bplen, "LCK%05d.%05d",
514 st.st_dev, st.st_ino);
517 snprintf (bp, sizeof(li->curlock) - bplen, "%s.lock", cp);
519 #if !defined(HAVE_LIBLOCKFILE)
521 * If this is for a new lock, create a name for
522 * the temporary lock file for lockit()
525 if ((cp = strrchr (li->curlock, '/')) == NULL || *++cp == 0)
526 strncpy (li->tmplock, ",LCK.XXXXXX", sizeof(li->tmplock));
528 snprintf (li->tmplock, sizeof(li->tmplock), "%.*s,LCK.XXXXXX",
529 cp - li->curlock, li->curlock);
536 * Add new lockfile to the list of open lockfiles
537 * and start the lock file timer.
541 timerON (char *curlock, int fd)
546 lp = (struct lock *) mh_xmalloc (sizeof(*lp));
548 len = strlen(curlock) + 1;
550 lp->l_lock = mh_xmalloc (len);
551 memcpy (lp->l_lock, curlock, len);
555 /* perhaps SIGT{STP,TIN,TOU} ? */
556 SIGNAL (SIGALRM, alrmser);
565 * Search through the list of lockfiles for the
566 * current lockfile, and remove it from the list.
572 struct lock *pp, *lp;
577 for (pp = lp = l_top; lp; pp = lp, lp = lp->l_next) {
585 pp->l_next = lp->l_next;
592 /* if there are locks left, restart timer */
599 * If timer goes off, we update the ctime of all open
600 * lockfiles, so another command doesn't remove them.
610 #ifndef RELIABLE_SIGNALS
611 SIGNAL (SIGALRM, alrmser);
614 /* update the ctime of all the lock files */
615 for (lp = l_top; lp; lp = lp->l_next) {
616 lockfile = lp->l_lock;
617 #if !defined(HAVE_LIBLOCKFILE)
618 if (*lockfile && (j = creat (lockfile, 0600)) != -1)
621 lockfile_touch(lockfile);
625 /* restart the alarm */
629 #endif /* DOT_LOCKING */