2 * lock.c -- routines to lock/unlock files
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
9 /* Modified by Ruud de Rooij to support Miquel van Smoorenburg's liblockfile
11 * Since liblockfile locking shares most of its code with dot locking, it
12 * is enabled by defining both DOT_LOCKING and HAVE_LIBLOCKFILE.
14 * Ruud de Rooij <ruud@debian.org> Sun, 28 Mar 1999 15:34:03 +0200
18 #include <h/signals.h>
21 #ifdef TIME_WITH_SYS_TIME
22 # include <sys/time.h>
25 # ifdef TM_IN_SYS_TIME
26 # include <sys/time.h>
39 # include <sys/file.h>
42 #if defined(LOCKF_LOCKING) || defined(FLOCK_LOCKING)
43 # include <sys/file.h>
48 #if defined(HAVE_LIBLOCKFILE)
53 char *lockdir = LOCKDIR;
56 /* Are we using any kernel locking? */
57 #if defined (FLOCK_LOCKING) || defined(LOCKF_LOCKING) || defined(FCNTL_LOCKING)
58 # define KERNEL_LOCKING
63 /* struct for getting name of lock file to create */
66 #if !defined(HAVE_LIBLOCKFILE)
72 * Amount of time to wait before
73 * updating ctime of lock file.
77 #if !defined(HAVE_LIBLOCKFILE)
79 * How old does a lock file need to be
80 * before we remove it.
83 #endif /* HAVE_LIBLOCKFILE */
85 /* struct for recording and updating locks */
92 /* top of list containing all open locks */
93 static struct lock *l_top = NULL;
94 #endif /* DOT_LOCKING */
100 static int lkopen_kernel (char *, int, mode_t);
104 static int lkopen_dot (char *, int, mode_t);
105 static void lockname (char *, struct lockinfo *, int);
106 static void timerON (char *, int);
107 static void timerOFF (int);
108 static RETSIGTYPE alrmser (int);
111 #if !defined(HAVE_LIBLOCKFILE)
112 static int lockit (struct lockinfo *);
116 * Base routine to open and lock a file,
117 * and return a file descriptor.
121 lkopen (char *file, int access, mode_t mode)
123 #ifdef KERNEL_LOCKING
124 return lkopen_kernel(file, access, mode);
128 return lkopen_dot(file, access, mode);
134 * Base routine to close and unlock a file,
135 * given a file descriptor.
139 lkclose (int fd, char *file)
146 struct lockinfo lkinfo;
153 buf.l_type = F_UNLCK;
154 buf.l_whence = SEEK_SET;
157 fcntl(fd, F_SETLK, &buf);
165 /* make sure we unlock the whole thing */
166 lseek (fd, (off_t) 0, SEEK_SET);
167 lockf (fd, F_ULOCK, 0L);
171 lockname (file, &lkinfo, 0); /* get name of lock file */
172 #if !defined(HAVE_LIBLOCKFILE)
173 unlink (lkinfo.curlock); /* remove lock file */
175 lockfile_remove(lkinfo.curlock);
176 #endif /* HAVE_LIBLOCKFILE */
177 timerOFF (fd); /* turn off lock timer */
178 #endif /* DOT_LOCKING */
185 * Base routine to open and lock a file,
186 * and return a FILE pointer
190 lkfopen (char *file, char *mode)
195 if (strcmp (mode, "r") == 0)
197 else if (strcmp (mode, "r+") == 0)
199 else if (strcmp (mode, "w") == 0)
200 access = O_WRONLY | O_CREAT | O_TRUNC;
201 else if (strcmp (mode, "w+") == 0)
202 access = O_RDWR | O_CREAT | O_TRUNC;
203 else if (strcmp (mode, "a") == 0)
204 access = O_WRONLY | O_CREAT | O_APPEND;
205 else if (strcmp (mode, "a+") == 0)
206 access = O_RDWR | O_CREAT | O_APPEND;
212 if ((fd = lkopen (file, access, 0666)) == -1)
215 if ((fp = fdopen (fd, mode)) == NULL) {
225 * Base routine to close and unlock a file,
226 * given a FILE pointer
230 lkfclose (FILE *fp, char *file)
237 struct lockinfo lkinfo;
244 buf.l_type = F_UNLCK;
245 buf.l_whence = SEEK_SET;
248 fcntl(fileno(fp), F_SETLK, &buf);
252 flock (fileno(fp), LOCK_UN);
256 /* make sure we unlock the whole thing */
257 fseek (fp, 0L, SEEK_SET);
258 lockf (fileno(fp), F_ULOCK, 0L);
262 lockname (file, &lkinfo, 0); /* get name of lock file */
263 #if !defined(HAVE_LIBLOCKFILE)
264 unlink (lkinfo.curlock); /* remove lock file */
266 lockfile_remove(lkinfo.curlock);
267 #endif /* HAVE_LIBLOCKFILE */
268 timerOFF (fileno(fp)); /* turn off lock timer */
269 #endif /* DOT_LOCKING */
271 return (fclose (fp));
275 #ifdef KERNEL_LOCKING
278 * open and lock a file, using kernel locking
282 lkopen_kernel (char *file, int access, mode_t mode)
286 # ifdef FCNTL_LOCKING
288 # endif /* FCNTL_LOCKING */
290 for (i = 0; i < 5; i++) {
292 # if defined(LOCKF_LOCKING) || defined(FCNTL_LOCKING)
293 /* remember the original mode */
296 /* make sure we open at the beginning */
300 * We MUST have write permission or
301 * lockf/fcntl() won't work
303 if ((access & 03) == O_RDONLY) {
307 # endif /* LOCKF_LOCKING || FCNTL_LOCKING */
309 if ((fd = open (file, access | O_NDELAY, mode)) == -1)
312 # ifdef FCNTL_LOCKING
313 buf.l_type = F_WRLCK;
314 buf.l_whence = SEEK_SET;
317 if (fcntl (fd, F_SETLK, &buf) != -1)
321 # ifdef FLOCK_LOCKING
322 if (flock (fd, (((access & 03) == O_RDONLY) ? LOCK_SH :
323 LOCK_EX) | LOCK_NB) != -1)
327 # ifdef LOCKF_LOCKING
328 if (lockf (fd, F_TLOCK, 0L) != -1) {
329 /* see if we should be at the end */
331 lseek (fd, (off_t) 0, SEEK_END);
346 #endif /* KERNEL_LOCKING */
352 * open and lock a file, using dot locking
356 lkopen_dot (char *file, int access, mode_t mode)
359 struct lockinfo lkinfo;
362 if ((fd = open (file, access, mode)) == -1)
366 * Get the name of the eventual lock file, as well
367 * as a name for a temporary lock file.
369 lockname (file, &lkinfo, 1);
371 #if !defined(HAVE_LIBLOCKFILE)
375 /* attempt to create lock file */
376 if (lockit (&lkinfo) == 0) {
377 /* if successful, turn on timer and return */
378 timerON (lkinfo.curlock, fd);
382 * Abort locking, if we fail to lock after 5 attempts
383 * and are never able to stat the lock file.
386 if (stat (lkinfo.curlock, &st) == -1) {
395 /* check for stale lockfile, else sleep */
396 if (curtime > st.st_ctime + RSECS)
397 unlink (lkinfo.curlock);
401 lockname (file, &lkinfo, 1);
406 if (lockfile_create(lkinfo.curlock, 5, 0) == L_SUCCESS) {
407 timerON(lkinfo.curlock, fd);
413 #endif /* HAVE_LIBLOCKFILE */
416 #if !defined(HAVE_LIBLOCKFILE)
418 * Routine that actually tries to create
423 lockit (struct lockinfo *li)
426 char *curlock, *tmplock;
432 curlock = li->curlock;
433 tmplock = li->tmplock;
436 if ((fd = mkstemp(tmplock)) == -1)
439 if (mktemp(tmplock) == NULL)
441 if (unlink(tmplock) == -1 && errno != ENOENT)
443 /* create the temporary lock file */
444 if ((fd = creat(tmplock, 0600)) == -1)
449 /* write our process id into lock file */
450 snprintf (buffer, sizeof(buffer), "nmh lock: pid %d\n", (int) getpid());
451 write(fd, buffer, strlen(buffer) + 1);
457 * Now try to create the real lock file
458 * by linking to the temporary file.
460 fd = link(tmplock, curlock);
463 return (fd == -1 ? -1 : 0);
465 #endif /* HAVE_LIBLOCKFILE */
468 * Get name of lock file, and temporary lock file
472 lockname (char *file, struct lockinfo *li, int isnewlock)
481 if ((cp = strrchr (file, '/')) == NULL || *++cp == 0)
487 snprintf (bp, sizeof(li->curlock), "%s/", lockdir);
488 tmplen = strlen (bp);
493 snprintf (bp, sizeof(li->curlock), "%.*s", (int)(cp - file), file);
494 tmplen = strlen (bp);
502 * mmdf style dot locking. Currently not supported.
503 * If we start supporting mmdf style dot locking,
504 * we will need to change the return value of lockname
506 if (stat (file, &st) == -1)
509 snprintf (bp, sizeof(li->curlock) - bplen, "LCK%05d.%05d",
510 st.st_dev, st.st_ino);
513 snprintf (bp, sizeof(li->curlock) - bplen, "%s.lock", cp);
515 #if !defined(HAVE_LIBLOCKFILE)
517 * If this is for a new lock, create a name for
518 * the temporary lock file for lockit()
521 if ((cp = strrchr (li->curlock, '/')) == NULL || *++cp == 0)
522 strncpy (li->tmplock, ",LCK.XXXXXX", sizeof(li->tmplock));
524 snprintf (li->tmplock, sizeof(li->tmplock), "%.*s,LCK.XXXXXX",
525 (int)(cp - li->curlock), li->curlock);
532 * Add new lockfile to the list of open lockfiles
533 * and start the lock file timer.
537 timerON (char *curlock, int fd)
542 lp = (struct lock *) mh_xmalloc (sizeof(*lp));
544 len = strlen(curlock) + 1;
546 lp->l_lock = mh_xmalloc (len);
547 memcpy (lp->l_lock, curlock, len);
551 /* perhaps SIGT{STP,TIN,TOU} ? */
552 SIGNAL (SIGALRM, alrmser);
561 * Search through the list of lockfiles for the
562 * current lockfile, and remove it from the list.
568 struct lock *pp, *lp;
573 for (pp = lp = l_top; lp; pp = lp, lp = lp->l_next) {
581 pp->l_next = lp->l_next;
588 /* if there are locks left, restart timer */
595 * If timer goes off, we update the ctime of all open
596 * lockfiles, so another command doesn't remove them.
605 #ifndef RELIABLE_SIGNALS
606 SIGNAL (SIGALRM, alrmser);
609 /* update the ctime of all the lock files */
610 for (lp = l_top; lp; lp = lp->l_next) {
611 lockfile = lp->l_lock;
612 #if !defined(HAVE_LIBLOCKFILE)
615 if (*lockfile && (j = creat (lockfile, 0600)) != -1)
619 lockfile_touch(lockfile);
623 /* restart the alarm */
627 #endif /* DOT_LOCKING */