3 * lock.c -- routines to lock/unlock files
8 /* Modified by Ruud de Rooij to support Miquel van Smoorenburg's liblockfile
10 * Since liblockfile locking shares most of its code with dot locking, it
11 * is enabled by defining both DOT_LOCKING and HAVE_LIBLOCKFILE.
13 * Ruud de Rooij <ruud@debian.org> Sun, 28 Mar 1999 15:34:03 +0200
17 #include <h/signals.h>
19 #ifdef TIME_WITH_SYS_TIME
20 # include <sys/time.h>
23 # ifdef TM_IN_SYS_TIME
24 # include <sys/time.h>
35 # include <mmdfonly.h>
36 # include <lockonly.h>
42 # include <sys/file.h>
45 #if defined(LOCKF_LOCKING) || defined(FLOCK_LOCKING)
46 # include <sys/file.h>
51 #if defined(HAVE_LIBLOCKFILE)
58 char *lockdir = LOCKDIR;
61 /* Are we using any kernel locking? */
62 #if defined (FLOCK_LOCKING) || defined(LOCKF_LOCKING) || defined(FCNTL_LOCKING)
63 # define KERNEL_LOCKING
68 /* struct for getting name of lock file to create */
71 #if !defined(HAVE_LIBLOCKFILE)
77 * Amount of time to wait before
78 * updating ctime of lock file.
82 #if !defined(HAVE_LIBLOCKFILE)
84 * How old does a lock file need to be
85 * before we remove it.
88 #endif /* HAVE_LIBLOCKFILE */
90 /* struct for recording and updating locks */
97 /* top of list containing all open locks */
98 static struct lock *l_top = NULL;
99 #endif /* DOT_LOCKING */
104 #ifdef KERNEL_LOCKING
105 static int lkopen_kernel (char *, int, mode_t);
109 static int lkopen_dot (char *, int, mode_t);
110 static int lockit (struct lockinfo *);
111 static void lockname (char *, struct lockinfo *, int);
112 static void timerON (char *, int);
113 static void timerOFF (int);
114 static RETSIGTYPE alrmser (int);
119 * Base routine to open and lock a file,
120 * and return a file descriptor.
124 lkopen (char *file, int access, mode_t mode)
126 #ifdef KERNEL_LOCKING
127 return lkopen_kernel(file, access, mode);
131 return lkopen_dot(file, access, mode);
137 * Base routine to close and unlock a file,
138 * given a file descriptor.
142 lkclose (int fd, char *file)
149 struct lockinfo lkinfo;
156 buf.l_type = F_UNLCK;
157 buf.l_whence = SEEK_SET;
160 fcntl(fd, F_SETLK, &buf);
168 /* make sure we unlock the whole thing */
169 lseek (fd, (off_t) 0, SEEK_SET);
170 lockf (fd, F_ULOCK, 0L);
174 lockname (file, &lkinfo, 0); /* get name of lock file */
175 #if !defined(HAVE_LIBLOCKFILE)
176 unlink (lkinfo.curlock); /* remove lock file */
178 lockfile_remove(lkinfo.curlock);
179 #endif /* HAVE_LIBLOCKFILE */
180 timerOFF (fd); /* turn off lock timer */
181 #endif /* DOT_LOCKING */
188 * Base routine to open and lock a file,
189 * and return a FILE pointer
193 lkfopen (char *file, char *mode)
198 if (strcmp (mode, "r") == 0)
203 if ((fd = lkopen (file, access, 0)) == -1)
206 if ((fp = fdopen (fd, mode)) == NULL) {
216 * Base routine to close and unlock a file,
217 * given a FILE pointer
221 lkfclose (FILE *fp, char *file)
228 struct lockinfo lkinfo;
235 buf.l_type = F_UNLCK;
236 buf.l_whence = SEEK_SET;
239 fcntl(fileno(fp), F_SETLK, &buf);
243 flock (fileno(fp), LOCK_UN);
247 /* make sure we unlock the whole thing */
248 fseek (fp, 0L, SEEK_SET);
249 lockf (fileno(fp), F_ULOCK, 0L);
253 lockname (file, &lkinfo, 0); /* get name of lock file */
254 #if !defined(HAVE_LIBLOCKFILE)
255 unlink (lkinfo.curlock); /* remove lock file */
257 lockfile_remove(lkinfo.curlock);
258 #endif /* HAVE_LIBLOCKFILE */
259 timerOFF (fileno(fp)); /* turn off lock timer */
260 #endif /* DOT_LOCKING */
262 return (fclose (fp));
266 #ifdef KERNEL_LOCKING
269 * open and lock a file, using kernel locking
273 lkopen_kernel (char *file, int access, mode_t mode)
277 # ifdef FCNTL_LOCKING
279 # endif /* FCNTL_LOCKING */
281 for (i = 0; i < 5; i++) {
283 # if defined(LOCKF_LOCKING) || defined(FCNTL_LOCKING)
284 /* remember the original mode */
287 /* make sure we open at the beginning */
291 * We MUST have write permission or
292 * lockf/fcntl() won't work
294 if ((access & 03) == O_RDONLY) {
298 # endif /* LOCKF_LOCKING || FCNTL_LOCKING */
300 if ((fd = open (file, access | O_NDELAY, mode)) == -1)
303 # ifdef FCNTL_LOCKING
304 buf.l_type = F_WRLCK;
305 buf.l_whence = SEEK_SET;
308 if (fcntl (fd, F_SETLK, &buf) != -1)
312 # ifdef FLOCK_LOCKING
313 if (flock (fd, LOCK_EX | LOCK_NB) != -1)
317 # ifdef LOCKF_LOCKING
318 if (lockf (fd, F_TLOCK, 0L) != -1) {
319 /* see if we should be at the end */
321 lseek (fd, (off_t) 0, SEEK_END);
336 #endif /* KERNEL_LOCKING */
342 * open and lock a file, using dot locking
346 lkopen_dot (char *file, int access, mode_t mode)
350 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)
365 /* attempt to create lock file */
366 if (lockit (&lkinfo) == 0) {
367 /* if successful, turn on timer and return */
368 timerON (lkinfo.curlock, fd);
372 * Abort locking, if we fail to lock after 5 attempts
373 * and are never able to stat the lock file.
375 if (stat (lkinfo.curlock, &st) == -1) {
383 /* check for stale lockfile, else sleep */
384 if (curtime > st.st_ctime + RSECS)
385 unlink (lkinfo.curlock);
392 if (lockfile_create(lkinfo.curlock, 5, 0) == L_SUCCESS) {
393 timerON(lkinfo.curlock, fd);
400 #endif /* HAVE_LIBLOCKFILE */
403 #if !defined(HAVE_LIBLOCKFILE)
405 * Routine that actually tries to create
410 lockit (struct lockinfo *li)
413 char *curlock, *tmplock;
419 curlock = li->curlock;
420 tmplock = li->tmplock;
422 /* create the temporary lock file */
423 if ((fd = creat(tmplock, 0600)) == -1)
427 /* write our process id into lock file */
428 snprintf (buffer, sizeof(buffer), "nmh lock: pid %d\n", (int) getpid());
429 write(fd, buffer, strlen(buffer) + 1);
435 * Now try to create the real lock file
436 * by linking to the temporary file.
438 fd = link(tmplock, curlock);
441 return (fd == -1 ? -1 : 0);
443 #endif /* HAVE_LIBLOCKFILE */
446 * Get name of lock file, and temporary lock file
450 lockname (char *file, struct lockinfo *li, int isnewlock)
459 if ((cp = strrchr (file, '/')) == NULL || *++cp == 0)
465 snprintf (bp, sizeof(li->curlock), "%s/", lockdir);
466 tmplen = strlen (bp);
471 snprintf (bp, sizeof(li->curlock), "%.*s", cp - file, file);
472 tmplen = strlen (bp);
480 * mmdf style dot locking. Currently not supported.
481 * If we start supporting mmdf style dot locking,
482 * we will need to change the return value of lockname
484 if (stat (file, &st) == -1)
487 snprintf (bp, sizeof(li->curlock) - bplen, "LCK%05d.%05d",
488 st.st_dev, st.st_ino);
491 snprintf (bp, sizeof(li->curlock) - bplen, "%s.lock", cp);
493 #if !defined(HAVE_LIBLOCKFILE)
495 * If this is for a new lock, create a name for
496 * the temporary lock file for lockit()
499 if ((cp = strrchr (li->curlock, '/')) == NULL || *++cp == 0)
500 strncpy (li->tmplock, ",LCK.XXXXXX", sizeof(li->tmplock));
502 snprintf (li->tmplock, sizeof(li->tmplock), "%.*s,LCK.XXXXXX",
503 cp - li->curlock, li->curlock);
505 Mkstemp work postponed until later -Doug
507 mkstemp (li->tmplock);
510 mktemp (li->tmplock);
515 unlink (li->tmplock); /* remove any stray */
522 * Add new lockfile to the list of open lockfiles
523 * and start the lock file timer.
527 timerON (char *curlock, int fd)
532 if (!(lp = (struct lock *) malloc (sizeof(*lp))))
535 len = strlen(curlock) + 1;
537 if (!(lp->l_lock = malloc (len))) {
541 memcpy (lp->l_lock, curlock, len);
545 /* perhaps SIGT{STP,TIN,TOU} ? */
546 SIGNAL (SIGALRM, alrmser);
555 * Search through the list of lockfiles for the
556 * current lockfile, and remove it from the list.
562 struct lock *pp, *lp;
567 for (pp = lp = l_top; lp; pp = lp, lp = lp->l_next) {
575 pp->l_next = lp->l_next;
582 /* if there are locks left, restart timer */
589 * If timer goes off, we update the ctime of all open
590 * lockfiles, so another command doesn't remove them.
600 #ifndef RELIABLE_SIGNALS
601 SIGNAL (SIGALRM, alrmser);
604 /* update the ctime of all the lock files */
605 for (lp = l_top; lp; lp = lp->l_next) {
606 lockfile = lp->l_lock;
607 #if !defined(HAVE_LIBLOCKFILE)
608 if (*lockfile && (j = creat (lockfile, 0600)) != -1)
611 lockfile_touch(lockfile);
615 /* restart the alarm */
619 #endif /* DOT_LOCKING */