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
19 #include <h/signals.h>
22 #ifdef HAVE_SYS_TIME_H
23 # include <sys/time.h>
32 # include <sys/file.h>
35 #if defined(LOCKF_LOCKING) || defined(FLOCK_LOCKING)
36 # include <sys/file.h>
41 #if defined(HAVE_LIBLOCKFILE)
46 char *lockdir = LOCKDIR;
49 /* Are we using any kernel locking? */
50 #if defined (FLOCK_LOCKING) || defined(LOCKF_LOCKING) || defined(FCNTL_LOCKING)
51 # define KERNEL_LOCKING
56 /* struct for getting name of lock file to create */
59 #if !defined(HAVE_LIBLOCKFILE)
65 ** Amount of time to wait before
66 ** updating ctime of lock file.
70 #if !defined(HAVE_LIBLOCKFILE)
72 ** How old does a lock file need to be
73 ** before we remove it.
76 #endif /* HAVE_LIBLOCKFILE */
78 /* struct for recording and updating locks */
85 /* top of list containing all open locks */
86 static struct lock *l_top = NULL;
87 #endif /* DOT_LOCKING */
93 static int lkopen_kernel(char *, int, mode_t);
97 static int lkopen_dot(char *, int, mode_t);
98 static void lockname(char *, struct lockinfo *, int);
99 static void timerON(char *, int);
100 static void timerOFF(int);
101 static void alrmser(int);
103 #if !defined(HAVE_LIBLOCKFILE)
104 static int lockit(struct lockinfo *);
109 ** Base routine to open and lock a file,
110 ** and return a file descriptor.
114 lkopen(char *file, int access, mode_t mode)
116 #ifdef KERNEL_LOCKING
117 return lkopen_kernel(file, access, mode);
121 return lkopen_dot(file, access, mode);
127 ** Base routine to close and unlock a file,
128 ** given a file descriptor.
132 lkclose(int fd, char *file)
139 struct lockinfo lkinfo;
146 buf.l_type = F_UNLCK;
147 buf.l_whence = SEEK_SET;
150 fcntl(fd, F_SETLK, &buf);
158 /* make sure we unlock the whole thing */
159 lseek(fd, (off_t) 0, SEEK_SET);
160 lockf(fd, F_ULOCK, 0L);
164 lockname(file, &lkinfo, 0); /* get name of lock file */
165 #if !defined(HAVE_LIBLOCKFILE)
166 unlink(lkinfo.curlock); /* remove lock file */
168 lockfile_remove(lkinfo.curlock);
169 #endif /* HAVE_LIBLOCKFILE */
170 timerOFF(fd); /* turn off lock timer */
171 #endif /* DOT_LOCKING */
178 ** Base routine to open and lock a file,
179 ** and return a FILE pointer
183 lkfopen(char *file, char *mode)
188 if (strcmp(mode, "r") == 0)
190 else if (strcmp(mode, "r+") == 0)
192 else if (strcmp(mode, "w") == 0)
193 access = O_WRONLY | O_CREAT | O_TRUNC;
194 else if (strcmp(mode, "w+") == 0)
195 access = O_RDWR | O_CREAT | O_TRUNC;
196 else if (strcmp(mode, "a") == 0)
197 access = O_WRONLY | O_CREAT | O_APPEND;
198 else if (strcmp(mode, "a+") == 0)
199 access = O_RDWR | O_CREAT | O_APPEND;
205 if ((fd = lkopen(file, access, 0666)) == -1)
208 if ((fp = fdopen(fd, mode)) == NULL) {
218 ** Base routine to close and unlock a file,
219 ** given a FILE pointer
223 lkfclose(FILE *fp, char *file)
230 struct lockinfo lkinfo;
237 buf.l_type = F_UNLCK;
238 buf.l_whence = SEEK_SET;
241 fcntl(fileno(fp), F_SETLK, &buf);
245 flock(fileno(fp), LOCK_UN);
249 /* make sure we unlock the whole thing */
250 fseek(fp, 0L, SEEK_SET);
251 lockf(fileno(fp), F_ULOCK, 0L);
255 lockname(file, &lkinfo, 0); /* get name of lock file */
256 #if !defined(HAVE_LIBLOCKFILE)
257 unlink(lkinfo.curlock); /* remove lock file */
259 lockfile_remove(lkinfo.curlock);
260 #endif /* HAVE_LIBLOCKFILE */
261 timerOFF(fileno(fp)); /* turn off lock timer */
262 #endif /* DOT_LOCKING */
268 #ifdef KERNEL_LOCKING
271 ** open and lock a file, using kernel locking
275 lkopen_kernel(char *file, int access, mode_t mode)
279 # ifdef FCNTL_LOCKING
281 # endif /* FCNTL_LOCKING */
283 for (i = 0; i < 5; i++) {
285 # if defined(LOCKF_LOCKING) || defined(FCNTL_LOCKING)
286 /* remember the original mode */
289 /* make sure we open at the beginning */
293 ** We MUST have write permission or
294 ** lockf/fcntl() won't work
296 if ((access & 03) == O_RDONLY) {
300 # endif /* LOCKF_LOCKING || FCNTL_LOCKING */
302 if ((fd = open(file, access | O_NDELAY, mode)) == -1)
305 # ifdef FCNTL_LOCKING
306 buf.l_type = F_WRLCK;
307 buf.l_whence = SEEK_SET;
310 if (fcntl(fd, F_SETLK, &buf) != -1)
314 # ifdef FLOCK_LOCKING
315 if (flock(fd, (((access & 03) == O_RDONLY) ? LOCK_SH :
316 LOCK_EX) | LOCK_NB) != -1)
320 # ifdef LOCKF_LOCKING
321 if (lockf(fd, F_TLOCK, 0L) != -1) {
322 /* see if we should be at the end */
324 lseek(fd, (off_t) 0, SEEK_END);
339 #endif /* KERNEL_LOCKING */
345 ** open and lock a file, using dot locking
349 lkopen_dot(char *file, int access, mode_t mode)
352 struct lockinfo lkinfo;
355 if ((fd = open(file, access, mode)) == -1)
359 ** Get the name of the eventual lock file, as well
360 ** as a name for a temporary lock file.
362 lockname(file, &lkinfo, 1);
364 #if !defined(HAVE_LIBLOCKFILE)
368 /* attempt to create lock file */
369 if (lockit(&lkinfo) == 0) {
370 /* if successful, turn on timer and return */
371 timerON(lkinfo.curlock, fd);
375 ** Abort locking, if we fail to lock after 5
376 ** attempts and are never able to stat the
380 if (stat(lkinfo.curlock, &st) == -1) {
390 ** check for stale lockfile,
393 if (curtime > st.st_ctime + RSECS)
394 unlink(lkinfo.curlock);
398 lockname(file, &lkinfo, 1);
403 if (lockfile_create(lkinfo.curlock, 5, 0) == L_SUCCESS) {
404 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)
464 if ((cp = strrchr(file, '/')) == NULL || *++cp == 0)
470 snprintf(bp, sizeof(li->curlock), "%s/", lockdir);
476 snprintf(bp, sizeof(li->curlock), "%.*s", (int)(cp - file), file);
483 snprintf(bp, sizeof(li->curlock) - bplen, "%s.lock", cp);
485 #if !defined(HAVE_LIBLOCKFILE)
487 ** If this is for a new lock, create a name for
488 ** the temporary lock file for lockit()
491 if ((cp = strrchr(li->curlock, '/')) == NULL || *++cp == 0)
492 strncpy(li->tmplock, ",LCK.XXXXXX", sizeof(li->tmplock));
494 snprintf(li->tmplock, sizeof(li->tmplock),
496 (int)(cp - li->curlock), li->curlock);
503 ** Add new lockfile to the list of open lockfiles
504 ** and start the lock file timer.
508 timerON(char *curlock, int fd)
513 lp = (struct lock *) mh_xmalloc(sizeof(*lp));
515 len = strlen(curlock) + 1;
517 lp->l_lock = mh_xmalloc(len);
518 memcpy(lp->l_lock, curlock, len);
522 /* perhaps SIGT{STP,TIN,TOU} ? */
523 SIGNAL(SIGALRM, alrmser);
532 ** Search through the list of lockfiles for the
533 ** current lockfile, and remove it from the list.
539 struct lock *pp, *lp;
544 for (pp = lp = l_top; lp; pp = lp, lp = lp->l_next) {
552 pp->l_next = lp->l_next;
559 /* if there are locks left, restart timer */
566 ** If timer goes off, we update the ctime of all open
567 ** lockfiles, so another command doesn't remove them.
576 /* update the ctime of all the lock files */
577 for (lp = l_top; lp; lp = lp->l_next) {
578 lockfile = lp->l_lock;
579 #if !defined(HAVE_LIBLOCKFILE)
582 if (*lockfile && (j = creat(lockfile, 0600)) != -1)
586 lockfile_touch(lockfile);
590 /* restart the alarm */
594 #endif /* DOT_LOCKING */