3 * lock.c -- routines to lock/unlock files
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
10 /* Modified by Ruud de Rooij to support Miquel van Smoorenburg's liblockfile
12 * Since liblockfile locking shares most of its code with dot locking, it
13 * is enabled by defining both DOT_LOCKING and HAVE_LIBLOCKFILE.
15 * Ruud de Rooij <ruud@debian.org> Sun, 28 Mar 1999 15:34:03 +0200
19 #include <h/signals.h>
22 #ifdef HAVE_SYS_TIME_H
23 # include <sys/time.h>
31 # include <sys/file.h>
34 #if defined(LOCKF_LOCKING) || defined(FLOCK_LOCKING)
35 # include <sys/file.h>
40 #if defined(HAVE_LIBLOCKFILE)
45 char *lockdir = LOCKDIR;
48 /* Are we using any kernel locking? */
49 #if defined (FLOCK_LOCKING) || defined(LOCKF_LOCKING) || defined(FCNTL_LOCKING)
50 # define KERNEL_LOCKING
55 /* struct for getting name of lock file to create */
58 #if !defined(HAVE_LIBLOCKFILE)
64 * Amount of time to wait before
65 * updating ctime of lock file.
69 #if !defined(HAVE_LIBLOCKFILE)
71 * How old does a lock file need to be
72 * before we remove it.
75 #endif /* HAVE_LIBLOCKFILE */
77 /* struct for recording and updating locks */
84 /* top of list containing all open locks */
85 static struct lock *l_top = NULL;
86 #endif /* DOT_LOCKING */
92 static int lkopen_kernel (char *, int, mode_t);
96 static int lkopen_dot (char *, int, mode_t);
97 static void lockname (char *, struct lockinfo *, int);
98 static void timerON (char *, int);
99 static void timerOFF (int);
100 static void alrmser (int);
102 #if !defined(HAVE_LIBLOCKFILE)
103 static int lockit (struct lockinfo *);
108 * Base routine to open and lock a file,
109 * and return a file descriptor.
113 lkopen (char *file, int access, mode_t mode)
115 #ifdef KERNEL_LOCKING
116 return lkopen_kernel(file, access, mode);
120 return lkopen_dot(file, access, mode);
126 * Base routine to close and unlock a file,
127 * given a file descriptor.
131 lkclose (int fd, char *file)
138 struct lockinfo lkinfo;
145 buf.l_type = F_UNLCK;
146 buf.l_whence = SEEK_SET;
149 fcntl(fd, F_SETLK, &buf);
157 /* make sure we unlock the whole thing */
158 lseek (fd, (off_t) 0, SEEK_SET);
159 lockf (fd, F_ULOCK, 0L);
163 lockname (file, &lkinfo, 0); /* get name of lock file */
164 #if !defined(HAVE_LIBLOCKFILE)
165 unlink (lkinfo.curlock); /* remove lock file */
167 lockfile_remove(lkinfo.curlock);
168 #endif /* HAVE_LIBLOCKFILE */
169 timerOFF (fd); /* turn off lock timer */
170 #endif /* DOT_LOCKING */
177 * Base routine to open and lock a file,
178 * and return a FILE pointer
182 lkfopen (char *file, char *mode)
187 if (strcmp (mode, "r") == 0)
189 else if (strcmp (mode, "r+") == 0)
191 else if (strcmp (mode, "w") == 0)
192 access = O_WRONLY | O_CREAT | O_TRUNC;
193 else if (strcmp (mode, "w+") == 0)
194 access = O_RDWR | O_CREAT | O_TRUNC;
195 else if (strcmp (mode, "a") == 0)
196 access = O_WRONLY | O_CREAT | O_APPEND;
197 else if (strcmp (mode, "a+") == 0)
198 access = O_RDWR | O_CREAT | O_APPEND;
204 if ((fd = lkopen (file, access, 0666)) == -1)
207 if ((fp = fdopen (fd, mode)) == NULL) {
217 * Base routine to close and unlock a file,
218 * given a FILE pointer
222 lkfclose (FILE *fp, char *file)
229 struct lockinfo lkinfo;
236 buf.l_type = F_UNLCK;
237 buf.l_whence = SEEK_SET;
240 fcntl(fileno(fp), F_SETLK, &buf);
244 flock (fileno(fp), LOCK_UN);
248 /* make sure we unlock the whole thing */
249 fseek (fp, 0L, SEEK_SET);
250 lockf (fileno(fp), F_ULOCK, 0L);
254 lockname (file, &lkinfo, 0); /* get name of lock file */
255 #if !defined(HAVE_LIBLOCKFILE)
256 unlink (lkinfo.curlock); /* remove lock file */
258 lockfile_remove(lkinfo.curlock);
259 #endif /* HAVE_LIBLOCKFILE */
260 timerOFF (fileno(fp)); /* turn off lock timer */
261 #endif /* DOT_LOCKING */
263 return (fclose (fp));
267 #ifdef KERNEL_LOCKING
270 * open and lock a file, using kernel locking
274 lkopen_kernel (char *file, int access, mode_t mode)
278 # ifdef FCNTL_LOCKING
280 # endif /* FCNTL_LOCKING */
282 for (i = 0; i < 5; i++) {
284 # if defined(LOCKF_LOCKING) || defined(FCNTL_LOCKING)
285 /* remember the original mode */
288 /* make sure we open at the beginning */
292 * We MUST have write permission or
293 * lockf/fcntl() won't work
295 if ((access & 03) == O_RDONLY) {
299 # endif /* LOCKF_LOCKING || FCNTL_LOCKING */
301 if ((fd = open (file, access | O_NDELAY, mode)) == -1)
304 # ifdef FCNTL_LOCKING
305 buf.l_type = F_WRLCK;
306 buf.l_whence = SEEK_SET;
309 if (fcntl (fd, F_SETLK, &buf) != -1)
313 # ifdef FLOCK_LOCKING
314 if (flock (fd, (((access & 03) == O_RDONLY) ? LOCK_SH : LOCK_EX)
319 # ifdef LOCKF_LOCKING
320 if (lockf (fd, F_TLOCK, 0L) != -1) {
321 /* see if we should be at the end */
323 lseek (fd, (off_t) 0, SEEK_END);
338 #endif /* KERNEL_LOCKING */
344 * open and lock a file, using dot locking
348 lkopen_dot (char *file, int access, mode_t mode)
351 struct lockinfo lkinfo;
354 if ((fd = open (file, access, mode)) == -1)
358 * Get the name of the eventual lock file, as well
359 * as a name for a temporary lock file.
361 lockname (file, &lkinfo, 1);
363 #if !defined(HAVE_LIBLOCKFILE)
367 /* attempt to create lock file */
368 if (lockit (&lkinfo) == 0) {
369 /* if successful, turn on timer and return */
370 timerON (lkinfo.curlock, fd);
374 * Abort locking, if we fail to lock after 5 attempts
375 * and are never able to stat the lock file.
378 if (stat (lkinfo.curlock, &st) == -1) {
387 /* check for stale lockfile, else sleep */
388 if (curtime > st.st_ctime + RSECS)
389 unlink (lkinfo.curlock);
393 lockname (file, &lkinfo, 1);
398 if (lockfile_create(lkinfo.curlock, 5, 0) == L_SUCCESS) {
399 timerON(lkinfo.curlock, fd);
406 #endif /* HAVE_LIBLOCKFILE */
409 #if !defined(HAVE_LIBLOCKFILE)
411 * Routine that actually tries to create
416 lockit (struct lockinfo *li)
419 char *curlock, *tmplock;
425 curlock = li->curlock;
426 tmplock = li->tmplock;
428 if ((fd = mkstemp(tmplock)) == -1)
432 /* write our process id into lock file */
433 snprintf (buffer, sizeof(buffer), "nmh lock: pid %d\n", (int) getpid());
434 write(fd, buffer, strlen(buffer) + 1);
440 * Now try to create the real lock file
441 * by linking to the temporary file.
443 fd = link(tmplock, curlock);
446 return (fd == -1 ? -1 : 0);
448 #endif /* HAVE_LIBLOCKFILE */
451 * Get name of lock file, and temporary lock file
455 lockname (char *file, struct lockinfo *li, int isnewlock)
464 if ((cp = strrchr (file, '/')) == NULL || *++cp == 0)
470 snprintf (bp, sizeof(li->curlock), "%s/", lockdir);
471 tmplen = strlen (bp);
476 snprintf (bp, sizeof(li->curlock), "%.*s", (int)(cp - file), file);
477 tmplen = strlen (bp);
485 * mmdf style dot locking. Currently not supported.
486 * If we start supporting mmdf style dot locking,
487 * we will need to change the return value of lockname
489 if (stat (file, &st) == -1)
492 snprintf (bp, sizeof(li->curlock) - bplen, "LCK%05d.%05d",
493 st.st_dev, st.st_ino);
496 snprintf (bp, sizeof(li->curlock) - bplen, "%s.lock", cp);
498 #if !defined(HAVE_LIBLOCKFILE)
500 * If this is for a new lock, create a name for
501 * the temporary lock file for lockit()
504 if ((cp = strrchr (li->curlock, '/')) == NULL || *++cp == 0)
505 strncpy (li->tmplock, ",LCK.XXXXXX", sizeof(li->tmplock));
507 snprintf (li->tmplock, sizeof(li->tmplock), "%.*s,LCK.XXXXXX",
508 (int)(cp - li->curlock), li->curlock);
515 * Add new lockfile to the list of open lockfiles
516 * and start the lock file timer.
520 timerON (char *curlock, int fd)
525 lp = (struct lock *) mh_xmalloc (sizeof(*lp));
527 len = strlen(curlock) + 1;
529 lp->l_lock = mh_xmalloc (len);
530 memcpy (lp->l_lock, curlock, len);
534 /* perhaps SIGT{STP,TIN,TOU} ? */
535 SIGNAL (SIGALRM, alrmser);
544 * Search through the list of lockfiles for the
545 * current lockfile, and remove it from the list.
551 struct lock *pp, *lp;
556 for (pp = lp = l_top; lp; pp = lp, lp = lp->l_next) {
564 pp->l_next = lp->l_next;
571 /* if there are locks left, restart timer */
578 * If timer goes off, we update the ctime of all open
579 * lockfiles, so another command doesn't remove them.
588 /* update the ctime of all the lock files */
589 for (lp = l_top; lp; lp = lp->l_next) {
590 lockfile = lp->l_lock;
591 #if !defined(HAVE_LIBLOCKFILE)
594 if (*lockfile && (j = creat (lockfile, 0600)) != -1)
598 lockfile_touch(lockfile);
602 /* restart the alarm */
606 #endif /* DOT_LOCKING */