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)
204 else if (strcmp (mode, "r+") == 0)
206 else if (strcmp (mode, "w") == 0)
207 access = O_WRONLY | O_CREAT | O_TRUNC;
208 else if (strcmp (mode, "w+") == 0)
209 access = O_RDWR | O_CREAT | O_TRUNC;
210 else if (strcmp (mode, "a") == 0)
211 access = O_WRONLY | O_CREAT | O_APPEND;
212 else if (strcmp (mode, "a+") == 0)
213 access = O_RDWR | O_CREAT | O_APPEND;
219 if ((fd = lkopen (file, access, 0666)) == -1)
222 if ((fp = fdopen (fd, mode)) == NULL) {
232 * Base routine to close and unlock a file,
233 * given a FILE pointer
237 lkfclose (FILE *fp, char *file)
244 struct lockinfo lkinfo;
251 buf.l_type = F_UNLCK;
252 buf.l_whence = SEEK_SET;
255 fcntl(fileno(fp), F_SETLK, &buf);
259 flock (fileno(fp), LOCK_UN);
263 /* make sure we unlock the whole thing */
264 fseek (fp, 0L, SEEK_SET);
265 lockf (fileno(fp), F_ULOCK, 0L);
269 lockname (file, &lkinfo, 0); /* get name of lock file */
270 #if !defined(HAVE_LIBLOCKFILE)
271 unlink (lkinfo.curlock); /* remove lock file */
273 lockfile_remove(lkinfo.curlock);
274 #endif /* HAVE_LIBLOCKFILE */
275 timerOFF (fileno(fp)); /* turn off lock timer */
276 #endif /* DOT_LOCKING */
278 return (fclose (fp));
282 #ifdef KERNEL_LOCKING
285 * open and lock a file, using kernel locking
289 lkopen_kernel (char *file, int access, mode_t mode)
293 # ifdef FCNTL_LOCKING
295 # endif /* FCNTL_LOCKING */
297 for (i = 0; i < 5; i++) {
299 # if defined(LOCKF_LOCKING) || defined(FCNTL_LOCKING)
300 /* remember the original mode */
303 /* make sure we open at the beginning */
307 * We MUST have write permission or
308 * lockf/fcntl() won't work
310 if ((access & 03) == O_RDONLY) {
314 # endif /* LOCKF_LOCKING || FCNTL_LOCKING */
316 if ((fd = open (file, access | O_NDELAY, mode)) == -1)
319 # ifdef FCNTL_LOCKING
320 buf.l_type = F_WRLCK;
321 buf.l_whence = SEEK_SET;
324 if (fcntl (fd, F_SETLK, &buf) != -1)
328 # ifdef FLOCK_LOCKING
329 if (flock (fd, (((access & 03) == O_RDONLY) ? LOCK_SH : LOCK_EX)
334 # ifdef LOCKF_LOCKING
335 if (lockf (fd, F_TLOCK, 0L) != -1) {
336 /* see if we should be at the end */
338 lseek (fd, (off_t) 0, SEEK_END);
353 #endif /* KERNEL_LOCKING */
359 * open and lock a file, using dot locking
363 lkopen_dot (char *file, int access, mode_t mode)
367 struct lockinfo lkinfo;
371 if ((fd = open (file, access, mode)) == -1)
375 * Get the name of the eventual lock file, as well
376 * as a name for a temporary lock file.
378 lockname (file, &lkinfo, 1);
380 #if !defined(HAVE_LIBLOCKFILE)
382 /* attempt to create lock file */
383 if (lockit (&lkinfo) == 0) {
384 /* if successful, turn on timer and return */
385 timerON (lkinfo.curlock, fd);
389 * Abort locking, if we fail to lock after 5 attempts
390 * and are never able to stat the lock file.
392 if (stat (lkinfo.curlock, &st) == -1) {
400 /* check for stale lockfile, else sleep */
401 if (curtime > st.st_ctime + RSECS)
402 unlink (lkinfo.curlock);
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 if (!(lp = (struct lock *) malloc (sizeof(*lp))))
549 len = strlen(curlock) + 1;
551 if (!(lp->l_lock = malloc (len))) {
555 memcpy (lp->l_lock, curlock, len);
559 /* perhaps SIGT{STP,TIN,TOU} ? */
560 SIGNAL (SIGALRM, alrmser);
569 * Search through the list of lockfiles for the
570 * current lockfile, and remove it from the list.
576 struct lock *pp, *lp;
581 for (pp = lp = l_top; lp; pp = lp, lp = lp->l_next) {
589 pp->l_next = lp->l_next;
596 /* if there are locks left, restart timer */
603 * If timer goes off, we update the ctime of all open
604 * lockfiles, so another command doesn't remove them.
614 #ifndef RELIABLE_SIGNALS
615 SIGNAL (SIGALRM, alrmser);
618 /* update the ctime of all the lock files */
619 for (lp = l_top; lp; lp = lp->l_next) {
620 lockfile = lp->l_lock;
621 #if !defined(HAVE_LIBLOCKFILE)
622 if (*lockfile && (j = creat (lockfile, 0600)) != -1)
625 lockfile_touch(lockfile);
629 /* restart the alarm */
633 #endif /* DOT_LOCKING */