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)
48 char *lockdir = LOCKDIR;
51 /* Are we using any kernel locking? */
52 #if defined (FLOCK_LOCKING) || defined(LOCKF_LOCKING) || defined(FCNTL_LOCKING)
53 # define KERNEL_LOCKING
58 /* struct for getting name of lock file to create */
61 #if !defined(HAVE_LIBLOCKFILE)
67 ** Amount of time to wait before
68 ** updating ctime of lock file.
72 #if !defined(HAVE_LIBLOCKFILE)
74 ** How old does a lock file need to be
75 ** before we remove it.
78 #endif /* HAVE_LIBLOCKFILE */
80 /* struct for recording and updating locks */
87 /* top of list containing all open locks */
88 static struct lock *l_top = NULL;
89 #endif /* DOT_LOCKING */
95 static int lkopen_kernel(char *, int, mode_t);
99 static int lkopen_dot(char *, int, mode_t);
100 static void lockname(char *, struct lockinfo *, int);
101 static void timerON(char *, int);
102 static void timerOFF(int);
103 static void alrmser(int);
105 #if !defined(HAVE_LIBLOCKFILE)
106 static int lockit(struct lockinfo *);
111 ** Base routine to open and lock a file,
112 ** and return a file descriptor.
116 lkopen(char *file, int access, mode_t mode)
118 #ifdef KERNEL_LOCKING
119 return lkopen_kernel(file, access, mode);
123 return lkopen_dot(file, access, mode);
129 ** Base routine to close and unlock a file,
130 ** given a file descriptor.
134 lkclose(int fd, char *file)
141 struct lockinfo lkinfo;
148 buf.l_type = F_UNLCK;
149 buf.l_whence = SEEK_SET;
152 fcntl(fd, F_SETLK, &buf);
160 /* make sure we unlock the whole thing */
161 lseek(fd, (off_t) 0, SEEK_SET);
162 lockf(fd, F_ULOCK, 0L);
166 lockname(file, &lkinfo, 0); /* get name of lock file */
167 #if !defined(HAVE_LIBLOCKFILE)
168 unlink(lkinfo.curlock); /* remove lock file */
170 lockfile_remove(lkinfo.curlock);
171 #endif /* HAVE_LIBLOCKFILE */
172 timerOFF(fd); /* turn off lock timer */
173 #endif /* DOT_LOCKING */
180 ** Base routine to open and lock a file,
181 ** and return a FILE pointer
185 lkfopen(char *file, char *mode)
190 if (strcmp(mode, "r") == 0)
192 else if (strcmp(mode, "r+") == 0)
194 else if (strcmp(mode, "w") == 0)
195 access = O_WRONLY | O_CREAT | O_TRUNC;
196 else if (strcmp(mode, "w+") == 0)
197 access = O_RDWR | O_CREAT | O_TRUNC;
198 else if (strcmp(mode, "a") == 0)
199 access = O_WRONLY | O_CREAT | O_APPEND;
200 else if (strcmp(mode, "a+") == 0)
201 access = O_RDWR | O_CREAT | O_APPEND;
207 if ((fd = lkopen(file, access, 0666)) == -1)
210 if ((fp = fdopen(fd, mode)) == NULL) {
220 ** Base routine to close and unlock a file,
221 ** given a FILE pointer
225 lkfclose(FILE *fp, char *file)
232 struct lockinfo lkinfo;
239 buf.l_type = F_UNLCK;
240 buf.l_whence = SEEK_SET;
243 fcntl(fileno(fp), F_SETLK, &buf);
247 flock(fileno(fp), LOCK_UN);
251 /* make sure we unlock the whole thing */
252 fseek(fp, 0L, SEEK_SET);
253 lockf(fileno(fp), F_ULOCK, 0L);
257 lockname(file, &lkinfo, 0); /* get name of lock file */
258 #if !defined(HAVE_LIBLOCKFILE)
259 unlink(lkinfo.curlock); /* remove lock file */
261 lockfile_remove(lkinfo.curlock);
262 #endif /* HAVE_LIBLOCKFILE */
263 timerOFF(fileno(fp)); /* turn off lock timer */
264 #endif /* DOT_LOCKING */
270 #ifdef KERNEL_LOCKING
273 ** open and lock a file, using kernel locking
277 lkopen_kernel(char *file, int access, mode_t mode)
281 # ifdef FCNTL_LOCKING
283 # endif /* FCNTL_LOCKING */
285 for (i = 0; i < 5; i++) {
287 # if defined(LOCKF_LOCKING) || defined(FCNTL_LOCKING)
288 /* remember the original mode */
291 /* make sure we open at the beginning */
295 ** We MUST have write permission or
296 ** lockf/fcntl() won't work
298 if ((access & 03) == O_RDONLY) {
302 # endif /* LOCKF_LOCKING || FCNTL_LOCKING */
304 if ((fd = open(file, access | O_NDELAY, mode)) == -1)
307 # ifdef FCNTL_LOCKING
308 buf.l_type = F_WRLCK;
309 buf.l_whence = SEEK_SET;
312 if (fcntl(fd, F_SETLK, &buf) != -1)
316 # ifdef FLOCK_LOCKING
317 if (flock(fd, (((access & 03) == O_RDONLY) ? LOCK_SH :
318 LOCK_EX) | LOCK_NB) != -1)
322 # ifdef LOCKF_LOCKING
323 if (lockf(fd, F_TLOCK, 0L) != -1) {
324 /* see if we should be at the end */
326 lseek(fd, (off_t) 0, SEEK_END);
341 #endif /* KERNEL_LOCKING */
347 ** open and lock a file, using dot locking
351 lkopen_dot(char *file, int access, mode_t mode)
354 struct lockinfo lkinfo;
357 if ((fd = open(file, access, mode)) == -1)
361 ** Get the name of the eventual lock file, as well
362 ** as a name for a temporary lock file.
364 lockname(file, &lkinfo, 1);
366 #if !defined(HAVE_LIBLOCKFILE)
370 /* attempt to create lock file */
371 if (lockit(&lkinfo) == 0) {
372 /* if successful, turn on timer and return */
373 timerON(lkinfo.curlock, fd);
377 ** Abort locking, if we fail to lock after 5
378 ** attempts and are never able to stat the
382 if (stat(lkinfo.curlock, &st) == -1) {
392 ** check for stale lockfile,
395 if (curtime > st.st_ctime + RSECS)
396 unlink(lkinfo.curlock);
400 lockname(file, &lkinfo, 1);
405 if (lockfile_create(lkinfo.curlock, 5, 0) == L_SUCCESS) {
406 timerON(lkinfo.curlock, fd);
412 #endif /* HAVE_LIBLOCKFILE */
415 #if !defined(HAVE_LIBLOCKFILE)
417 ** Routine that actually tries to create
422 lockit(struct lockinfo *li)
425 char *curlock, *tmplock;
431 curlock = li->curlock;
432 tmplock = li->tmplock;
434 if ((fd = mkstemp(tmplock)) == -1) {
435 advise(NULL, "unable to create temporary file in %s", tmplock);
440 /* write our process id into lock file */
441 snprintf(buffer, sizeof(buffer), "nmh lock: pid %d\n", (int) getpid());
442 write(fd, buffer, strlen(buffer) + 1);
448 ** Now try to create the real lock file
449 ** by linking to the temporary file.
451 fd = link(tmplock, curlock);
454 return (fd == -1 ? -1 : 0);
456 #endif /* HAVE_LIBLOCKFILE */
459 ** Get name of lock file, and temporary lock file
463 lockname(char *file, struct lockinfo *li, int isnewlock)
468 if ((cp = strrchr(file, '/')) == NULL || *++cp == 0)
474 snprintf(bp, sizeof(li->curlock), "%s/", lockdir);
480 snprintf(bp, sizeof(li->curlock), "%.*s", (int)(cp - file), file);
487 snprintf(bp, sizeof(li->curlock) - bplen, "%s.lock", cp);
489 #if !defined(HAVE_LIBLOCKFILE)
491 ** If this is for a new lock, create a name for
492 ** the temporary lock file for lockit()
495 if ((cp = strrchr(li->curlock, '/')) == NULL || *++cp == 0)
496 strncpy(li->tmplock, ",LCK.XXXXXX", sizeof(li->tmplock));
498 snprintf(li->tmplock, sizeof(li->tmplock),
500 (int)(cp - li->curlock), li->curlock);
507 ** Add new lockfile to the list of open lockfiles
508 ** and start the lock file timer.
512 timerON(char *curlock, int fd)
517 lp = (struct lock *) mh_xmalloc(sizeof(*lp));
519 len = strlen(curlock) + 1;
521 lp->l_lock = mh_xmalloc(len);
522 memcpy(lp->l_lock, curlock, len);
526 /* perhaps SIGT{STP,TIN,TOU} ? */
527 SIGNAL(SIGALRM, alrmser);
536 ** Search through the list of lockfiles for the
537 ** current lockfile, and remove it from the list.
543 struct lock *pp, *lp;
548 for (pp = lp = l_top; lp; pp = lp, lp = lp->l_next) {
556 pp->l_next = lp->l_next;
563 /* if there are locks left, restart timer */
570 ** If timer goes off, we update the ctime of all open
571 ** lockfiles, so another command doesn't remove them.
580 /* update the ctime of all the lock files */
581 for (lp = l_top; lp; lp = lp->l_next) {
582 lockfile = lp->l_lock;
583 #if !defined(HAVE_LIBLOCKFILE)
586 if (*lockfile && (j = creat(lockfile, 0600)) != -1)
590 lockfile_touch(lockfile);
594 /* restart the alarm */
598 #endif /* DOT_LOCKING */