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 #else /* DOT_LOCKING */
172 #endif /* DOT_LOCKING */
179 * Base routine to open and lock a file,
180 * and return a FILE pointer
184 lkfopen (char *file, char *mode)
189 if (strcmp (mode, "r") == 0)
191 else if (strcmp (mode, "r+") == 0)
193 else if (strcmp (mode, "w") == 0)
194 access = O_WRONLY | O_CREAT | O_TRUNC;
195 else if (strcmp (mode, "w+") == 0)
196 access = O_RDWR | O_CREAT | O_TRUNC;
197 else if (strcmp (mode, "a") == 0)
198 access = O_WRONLY | O_CREAT | O_APPEND;
199 else if (strcmp (mode, "a+") == 0)
200 access = O_RDWR | O_CREAT | O_APPEND;
206 if ((fd = lkopen (file, access, 0666)) == -1)
209 if ((fp = fdopen (fd, mode)) == NULL) {
219 * Base routine to close and unlock a file,
220 * given a FILE pointer
224 lkfclose (FILE *fp, char *file)
231 struct lockinfo lkinfo;
238 buf.l_type = F_UNLCK;
239 buf.l_whence = SEEK_SET;
242 fcntl(fileno(fp), F_SETLK, &buf);
246 flock (fileno(fp), LOCK_UN);
250 /* make sure we unlock the whole thing */
251 fseek (fp, 0L, SEEK_SET);
252 lockf (fileno(fp), F_ULOCK, 0L);
256 lockname (file, &lkinfo, 0); /* get name of lock file */
257 #if !defined(HAVE_LIBLOCKFILE)
258 unlink (lkinfo.curlock); /* remove lock file */
260 lockfile_remove(lkinfo.curlock);
261 #endif /* HAVE_LIBLOCKFILE */
262 timerOFF (fileno(fp)); /* turn off lock timer */
263 #else /* DOT_LOCKING */
265 #endif /* DOT_LOCKING */
267 return (fclose (fp));
271 #ifdef KERNEL_LOCKING
274 * open and lock a file, using kernel locking
278 lkopen_kernel (char *file, int access, mode_t mode)
282 # ifdef FCNTL_LOCKING
284 # endif /* FCNTL_LOCKING */
286 for (i = 0; i < 5; i++) {
288 # if defined(LOCKF_LOCKING) || defined(FCNTL_LOCKING)
289 /* remember the original mode */
292 /* make sure we open at the beginning */
296 * We MUST have write permission or
297 * lockf/fcntl() won't work
299 if ((access & 03) == O_RDONLY) {
303 # endif /* LOCKF_LOCKING || FCNTL_LOCKING */
305 if ((fd = open (file, access | O_NDELAY, mode)) == -1)
308 # ifdef FCNTL_LOCKING
309 buf.l_type = F_WRLCK;
310 buf.l_whence = SEEK_SET;
313 if (fcntl (fd, F_SETLK, &buf) != -1)
317 # ifdef FLOCK_LOCKING
318 if (flock (fd, (((access & 03) == O_RDONLY) ? LOCK_SH : LOCK_EX)
323 # ifdef LOCKF_LOCKING
324 if (lockf (fd, F_TLOCK, 0L) != -1) {
325 /* see if we should be at the end */
327 lseek (fd, (off_t) 0, SEEK_END);
342 #endif /* KERNEL_LOCKING */
348 * open and lock a file, using dot locking
352 lkopen_dot (char *file, int access, mode_t mode)
355 struct lockinfo lkinfo;
358 if ((fd = open (file, access, mode)) == -1)
362 * Get the name of the eventual lock file, as well
363 * as a name for a temporary lock file.
365 lockname (file, &lkinfo, 1);
367 #if !defined(HAVE_LIBLOCKFILE)
371 /* attempt to create lock file */
372 if (lockit (&lkinfo) == 0) {
373 /* if successful, turn on timer and return */
374 timerON (lkinfo.curlock, fd);
378 * Abort locking, if we fail to lock after 5 attempts
379 * and are never able to stat the lock file.
382 if (stat (lkinfo.curlock, &st) == -1) {
391 /* check for stale lockfile, else sleep */
392 if (curtime > st.st_ctime + RSECS)
393 unlink (lkinfo.curlock);
397 lockname (file, &lkinfo, 1);
402 if (lockfile_create(lkinfo.curlock, 5, 0) == L_SUCCESS) {
403 timerON(lkinfo.curlock, fd);
410 #endif /* HAVE_LIBLOCKFILE */
413 #if !defined(HAVE_LIBLOCKFILE)
415 * Routine that actually tries to create
420 lockit (struct lockinfo *li)
423 char *curlock, *tmplock;
429 curlock = li->curlock;
430 tmplock = li->tmplock;
432 if ((fd = mkstemp(tmplock)) == -1)
436 /* write our process id into lock file */
437 snprintf (buffer, sizeof(buffer), "nmh lock: pid %d\n", (int) getpid());
438 write(fd, buffer, strlen(buffer) + 1);
444 * Now try to create the real lock file
445 * by linking to the temporary file.
447 fd = link(tmplock, curlock);
450 return (fd == -1 ? -1 : 0);
452 #endif /* HAVE_LIBLOCKFILE */
455 * Get name of lock file, and temporary lock file
459 lockname (char *file, struct lockinfo *li, int isnewlock)
468 if ((cp = strrchr (file, '/')) == NULL || *++cp == 0)
474 snprintf (bp, sizeof(li->curlock), "%s/", lockdir);
475 tmplen = strlen (bp);
480 snprintf (bp, sizeof(li->curlock), "%.*s", (int)(cp - file), file);
481 tmplen = strlen (bp);
489 * mmdf style dot locking. Currently not supported.
490 * If we start supporting mmdf style dot locking,
491 * we will need to change the return value of lockname
493 if (stat (file, &st) == -1)
496 snprintf (bp, sizeof(li->curlock) - bplen, "LCK%05d.%05d",
497 st.st_dev, st.st_ino);
500 snprintf (bp, sizeof(li->curlock) - bplen, "%s.lock", cp);
502 #if !defined(HAVE_LIBLOCKFILE)
504 * If this is for a new lock, create a name for
505 * the temporary lock file for lockit()
508 if ((cp = strrchr (li->curlock, '/')) == NULL || *++cp == 0)
509 strncpy (li->tmplock, ",LCK.XXXXXX", sizeof(li->tmplock));
511 snprintf (li->tmplock, sizeof(li->tmplock), "%.*s,LCK.XXXXXX",
512 (int)(cp - li->curlock), li->curlock);
519 * Add new lockfile to the list of open lockfiles
520 * and start the lock file timer.
524 timerON (char *curlock, int fd)
529 lp = (struct lock *) mh_xmalloc (sizeof(*lp));
531 len = strlen(curlock) + 1;
533 lp->l_lock = mh_xmalloc (len);
534 memcpy (lp->l_lock, curlock, len);
538 /* perhaps SIGT{STP,TIN,TOU} ? */
539 SIGNAL (SIGALRM, alrmser);
548 * Search through the list of lockfiles for the
549 * current lockfile, and remove it from the list.
555 struct lock *pp, *lp;
560 for (pp = lp = l_top; lp; pp = lp, lp = lp->l_next) {
568 pp->l_next = lp->l_next;
575 /* if there are locks left, restart timer */
582 * If timer goes off, we update the ctime of all open
583 * lockfiles, so another command doesn't remove them.
594 /* update the ctime of all the lock files */
595 for (lp = l_top; lp; lp = lp->l_next) {
596 lockfile = lp->l_lock;
597 #if !defined(HAVE_LIBLOCKFILE)
600 if (*lockfile && (j = creat (lockfile, 0600)) != -1)
604 lockfile_touch(lockfile);
608 /* restart the alarm */
612 #endif /* DOT_LOCKING */