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.
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
20 #include <h/signals.h>
24 #ifdef HAVE_SYS_TIME_H
25 # include <sys/time.h>
34 # include <sys/file.h>
37 #if defined(LOCKF_LOCKING) || defined(FLOCK_LOCKING)
38 # include <sys/file.h>
43 #if defined(HAVE_LIBLOCKFILE)
49 char *lockdir = LOCKDIR;
53 /* Are we using any kernel locking? */
54 #if defined (FLOCK_LOCKING) || defined(LOCKF_LOCKING) || defined(FCNTL_LOCKING)
55 # define KERNEL_LOCKING
60 /* struct for getting name of lock file to create */
63 #if !defined(HAVE_LIBLOCKFILE)
69 ** Amount of time to wait before
70 ** updating ctime of lock file.
74 #if !defined(HAVE_LIBLOCKFILE)
76 ** How old does a lock file need to be
77 ** before we remove it.
80 #endif /* HAVE_LIBLOCKFILE */
82 /* struct for recording and updating locks */
89 /* top of list containing all open locks */
90 static struct lock *l_top = NULL;
91 #endif /* DOT_LOCKING */
97 static int lkopen_kernel(char *, int, mode_t);
101 static int lkopen_dot(char *, int, mode_t);
102 static void lockname(char *, struct lockinfo *, int);
103 static void timerON(char *, int);
104 static void timerOFF(int);
105 static void alrmser(int);
107 #if !defined(HAVE_LIBLOCKFILE)
108 static int lockit(struct lockinfo *);
113 ** Base routine to open and lock a file,
114 ** and return a file descriptor.
118 lkopen(char *file, int access, mode_t mode)
120 #ifdef KERNEL_LOCKING
121 return lkopen_kernel(file, access, mode);
125 return lkopen_dot(file, access, mode);
131 ** Base routine to close and unlock a file,
132 ** given a file descriptor.
136 lkclose(int fd, char *file)
143 struct lockinfo lkinfo;
150 buf.l_type = F_UNLCK;
151 buf.l_whence = SEEK_SET;
154 fcntl(fd, F_SETLK, &buf);
162 /* make sure we unlock the whole thing */
163 lseek(fd, (off_t) 0, SEEK_SET);
164 lockf(fd, F_ULOCK, 0L);
168 lockname(file, &lkinfo, 0); /* get name of lock file */
169 #if !defined(HAVE_LIBLOCKFILE)
170 unlink(lkinfo.curlock); /* remove lock file */
172 lockfile_remove(lkinfo.curlock);
173 #endif /* HAVE_LIBLOCKFILE */
174 timerOFF(fd); /* turn off lock timer */
175 #endif /* DOT_LOCKING */
182 ** Base routine to open and lock a file,
183 ** and return a FILE pointer
187 lkfopen(char *file, char *mode)
192 if (strcmp(mode, "r") == 0)
194 else if (strcmp(mode, "r+") == 0)
196 else if (strcmp(mode, "w") == 0)
197 access = O_WRONLY | O_CREAT | O_TRUNC;
198 else if (strcmp(mode, "w+") == 0)
199 access = O_RDWR | O_CREAT | O_TRUNC;
200 else if (strcmp(mode, "a") == 0)
201 access = O_WRONLY | O_CREAT | O_APPEND;
202 else if (strcmp(mode, "a+") == 0)
203 access = O_RDWR | O_CREAT | O_APPEND;
209 if ((fd = lkopen(file, access, 0666)) == -1)
212 if ((fp = fdopen(fd, mode)) == NULL) {
222 ** Base routine to close and unlock a file,
223 ** given a FILE pointer
227 lkfclose(FILE *fp, char *file)
234 struct lockinfo lkinfo;
241 buf.l_type = F_UNLCK;
242 buf.l_whence = SEEK_SET;
245 fcntl(fileno(fp), F_SETLK, &buf);
249 flock(fileno(fp), LOCK_UN);
253 /* make sure we unlock the whole thing */
254 fseek(fp, 0L, SEEK_SET);
255 lockf(fileno(fp), F_ULOCK, 0L);
259 lockname(file, &lkinfo, 0); /* get name of lock file */
260 #if !defined(HAVE_LIBLOCKFILE)
261 unlink(lkinfo.curlock); /* remove lock file */
263 lockfile_remove(lkinfo.curlock);
264 #endif /* HAVE_LIBLOCKFILE */
265 timerOFF(fileno(fp)); /* turn off lock timer */
266 #endif /* DOT_LOCKING */
272 #ifdef KERNEL_LOCKING
275 ** open and lock a file, using kernel locking
279 lkopen_kernel(char *file, int access, mode_t mode)
283 # ifdef FCNTL_LOCKING
285 # endif /* FCNTL_LOCKING */
287 for (i = 0; i < 5; i++) {
289 # if defined(LOCKF_LOCKING) || defined(FCNTL_LOCKING)
290 /* remember the original mode */
293 /* make sure we open at the beginning */
297 ** We MUST have write permission or
298 ** lockf/fcntl() won't work
300 if ((access & 03) == O_RDONLY) {
304 # endif /* LOCKF_LOCKING || FCNTL_LOCKING */
306 if ((fd = open(file, access | O_NDELAY, mode)) == -1)
309 # ifdef FCNTL_LOCKING
310 buf.l_type = F_WRLCK;
311 buf.l_whence = SEEK_SET;
314 if (fcntl(fd, F_SETLK, &buf) != -1)
318 # ifdef FLOCK_LOCKING
319 if (flock(fd, (((access & 03) == O_RDONLY) ? LOCK_SH :
320 LOCK_EX) | LOCK_NB) != -1)
324 # ifdef LOCKF_LOCKING
325 if (lockf(fd, F_TLOCK, 0L) != -1) {
326 /* see if we should be at the end */
328 lseek(fd, (off_t) 0, SEEK_END);
343 #endif /* KERNEL_LOCKING */
349 ** open and lock a file, using dot locking
353 lkopen_dot(char *file, int access, mode_t mode)
356 struct lockinfo lkinfo;
359 if ((fd = open(file, access, mode)) == -1)
363 ** Get the name of the eventual lock file, as well
364 ** as a name for a temporary lock file.
366 lockname(file, &lkinfo, 1);
368 #if !defined(HAVE_LIBLOCKFILE)
372 /* attempt to create lock file */
373 if (lockit(&lkinfo) == 0) {
374 /* if successful, turn on timer and return */
375 timerON(lkinfo.curlock, fd);
379 ** Abort locking, if we fail to lock after 5
380 ** attempts and are never able to stat the
384 if (stat(lkinfo.curlock, &st) == -1) {
394 ** check for stale lockfile,
397 if (curtime > st.st_ctime + RSECS)
398 unlink(lkinfo.curlock);
402 lockname(file, &lkinfo, 1);
407 if (lockfile_create(lkinfo.curlock, 5, 0) == L_SUCCESS) {
408 timerON(lkinfo.curlock, fd);
414 #endif /* HAVE_LIBLOCKFILE */
417 #if !defined(HAVE_LIBLOCKFILE)
419 ** Routine that actually tries to create
424 lockit(struct lockinfo *li)
427 char *curlock, *tmplock;
433 curlock = li->curlock;
434 tmplock = li->tmplock;
436 if ((fd = mkstemp(tmplock)) == -1) {
437 advise(NULL, "unable to create temporary file in %s", tmplock);
442 /* write our process id into lock file */
443 snprintf(buffer, sizeof(buffer), "nmh lock: pid %d\n", (int) getpid());
444 write(fd, buffer, strlen(buffer) + 1);
450 ** Now try to create the real lock file
451 ** by linking to the temporary file.
453 fd = link(tmplock, curlock);
456 return (fd == -1 ? -1 : 0);
458 #endif /* HAVE_LIBLOCKFILE */
461 ** Get name of lock file, and temporary lock file
465 lockname(char *file, struct lockinfo *li, int isnewlock)
470 if ((cp = strrchr(file, '/')) == NULL || *++cp == 0)
476 snprintf(bp, sizeof(li->curlock), "%s/", lockdir);
482 snprintf(bp, sizeof(li->curlock), "%.*s", (int)(cp - file), file);
489 snprintf(bp, sizeof(li->curlock) - bplen, "%s.lock", cp);
491 #if !defined(HAVE_LIBLOCKFILE)
493 ** If this is for a new lock, create a name for
494 ** the temporary lock file for lockit()
497 if ((cp = strrchr(li->curlock, '/')) == NULL || *++cp == 0)
498 strncpy(li->tmplock, ",LCK.XXXXXX", sizeof(li->tmplock));
500 snprintf(li->tmplock, sizeof(li->tmplock),
502 (int)(cp - li->curlock), li->curlock);
509 ** Add new lockfile to the list of open lockfiles
510 ** and start the lock file timer.
514 timerON(char *curlock, int fd)
519 lp = mh_xcalloc(1, sizeof(*lp));
521 len = strlen(curlock) + 1;
523 lp->l_lock = mh_xcalloc(len, sizeof(char));
524 memcpy(lp->l_lock, curlock, len);
528 /* perhaps SIGT{STP,TIN,TOU} ? */
529 SIGNAL(SIGALRM, alrmser);
538 ** Search through the list of lockfiles for the
539 ** current lockfile, and remove it from the list.
545 struct lock *pp, *lp;
550 for (pp = lp = l_top; lp; pp = lp, lp = lp->l_next) {
558 pp->l_next = lp->l_next;
560 mh_free0(&(lp->l_lock));
565 /* if there are locks left, restart timer */
572 ** If timer goes off, we update the ctime of all open
573 ** lockfiles, so another command doesn't remove them.
582 /* update the ctime of all the lock files */
583 for (lp = l_top; lp; lp = lp->l_next) {
584 lockfile = lp->l_lock;
585 #if !defined(HAVE_LIBLOCKFILE)
588 if (*lockfile && (j = creat(lockfile, 0600)) != -1)
592 lockfile_touch(lockfile);
596 /* restart the alarm */
600 #endif /* DOT_LOCKING */