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 TIME_WITH_SYS_TIME
23 # include <sys/time.h>
26 # ifdef TM_IN_SYS_TIME
27 # include <sys/time.h>
40 # include <sys/file.h>
43 #if defined(LOCKF_LOCKING) || defined(FLOCK_LOCKING)
44 # include <sys/file.h>
49 #if defined(HAVE_LIBLOCKFILE)
54 char *lockdir = LOCKDIR;
57 /* Are we using any kernel locking? */
58 #if defined (FLOCK_LOCKING) || defined(LOCKF_LOCKING) || defined(FCNTL_LOCKING)
59 # define KERNEL_LOCKING
64 /* struct for getting name of lock file to create */
67 #if !defined(HAVE_LIBLOCKFILE)
73 ** Amount of time to wait before
74 ** updating ctime of lock file.
78 #if !defined(HAVE_LIBLOCKFILE)
80 ** How old does a lock file need to be
81 ** before we remove it.
84 #endif /* HAVE_LIBLOCKFILE */
86 /* struct for recording and updating locks */
93 /* top of list containing all open locks */
94 static struct lock *l_top = NULL;
95 #endif /* DOT_LOCKING */
100 #ifdef KERNEL_LOCKING
101 static int lkopen_kernel (char *, int, mode_t);
105 static int lkopen_dot (char *, int, mode_t);
106 static void lockname (char *, struct lockinfo *, int);
107 static void timerON (char *, int);
108 static void timerOFF (int);
109 static RETSIGTYPE alrmser (int);
112 #if !defined(HAVE_LIBLOCKFILE)
113 static int lockit (struct lockinfo *);
117 ** Base routine to open and lock a file,
118 ** and return a file descriptor.
122 lkopen (char *file, int access, mode_t mode)
124 #ifdef KERNEL_LOCKING
125 return lkopen_kernel(file, access, mode);
129 return lkopen_dot(file, access, mode);
135 ** Base routine to close and unlock a file,
136 ** given a file descriptor.
140 lkclose (int fd, char *file)
147 struct lockinfo lkinfo;
154 buf.l_type = F_UNLCK;
155 buf.l_whence = SEEK_SET;
158 fcntl(fd, F_SETLK, &buf);
166 /* make sure we unlock the whole thing */
167 lseek (fd, (off_t) 0, SEEK_SET);
168 lockf (fd, F_ULOCK, 0L);
172 lockname (file, &lkinfo, 0); /* get name of lock file */
173 #if !defined(HAVE_LIBLOCKFILE)
174 unlink (lkinfo.curlock); /* remove lock file */
176 lockfile_remove(lkinfo.curlock);
177 #endif /* HAVE_LIBLOCKFILE */
178 timerOFF (fd); /* turn off lock timer */
179 #endif /* DOT_LOCKING */
186 ** Base routine to open and lock a file,
187 ** and return a FILE pointer
191 lkfopen (char *file, char *mode)
196 if (strcmp (mode, "r") == 0)
198 else if (strcmp (mode, "r+") == 0)
200 else if (strcmp (mode, "w") == 0)
201 access = O_WRONLY | O_CREAT | O_TRUNC;
202 else if (strcmp (mode, "w+") == 0)
203 access = O_RDWR | O_CREAT | O_TRUNC;
204 else if (strcmp (mode, "a") == 0)
205 access = O_WRONLY | O_CREAT | O_APPEND;
206 else if (strcmp (mode, "a+") == 0)
207 access = O_RDWR | O_CREAT | O_APPEND;
213 if ((fd = lkopen (file, access, 0666)) == -1)
216 if ((fp = fdopen (fd, mode)) == NULL) {
226 ** Base routine to close and unlock a file,
227 ** given a FILE pointer
231 lkfclose (FILE *fp, char *file)
238 struct lockinfo lkinfo;
245 buf.l_type = F_UNLCK;
246 buf.l_whence = SEEK_SET;
249 fcntl(fileno(fp), F_SETLK, &buf);
253 flock (fileno(fp), LOCK_UN);
257 /* make sure we unlock the whole thing */
258 fseek (fp, 0L, SEEK_SET);
259 lockf (fileno(fp), F_ULOCK, 0L);
263 lockname (file, &lkinfo, 0); /* get name of lock file */
264 #if !defined(HAVE_LIBLOCKFILE)
265 unlink (lkinfo.curlock); /* remove lock file */
267 lockfile_remove(lkinfo.curlock);
268 #endif /* HAVE_LIBLOCKFILE */
269 timerOFF (fileno(fp)); /* turn off lock timer */
270 #endif /* DOT_LOCKING */
272 return (fclose (fp));
276 #ifdef KERNEL_LOCKING
279 ** open and lock a file, using kernel locking
283 lkopen_kernel (char *file, int access, mode_t mode)
287 # ifdef FCNTL_LOCKING
289 # endif /* FCNTL_LOCKING */
291 for (i = 0; i < 5; i++) {
293 # if defined(LOCKF_LOCKING) || defined(FCNTL_LOCKING)
294 /* remember the original mode */
297 /* make sure we open at the beginning */
301 ** We MUST have write permission or
302 ** lockf/fcntl() won't work
304 if ((access & 03) == O_RDONLY) {
308 # endif /* LOCKF_LOCKING || FCNTL_LOCKING */
310 if ((fd = open (file, access | O_NDELAY, mode)) == -1)
313 # ifdef FCNTL_LOCKING
314 buf.l_type = F_WRLCK;
315 buf.l_whence = SEEK_SET;
318 if (fcntl (fd, F_SETLK, &buf) != -1)
322 # ifdef FLOCK_LOCKING
323 if (flock (fd, (((access & 03) == O_RDONLY) ? LOCK_SH :
324 LOCK_EX) | LOCK_NB) != -1)
328 # ifdef LOCKF_LOCKING
329 if (lockf (fd, F_TLOCK, 0L) != -1) {
330 /* see if we should be at the end */
332 lseek (fd, (off_t) 0, SEEK_END);
347 #endif /* KERNEL_LOCKING */
353 ** open and lock a file, using dot locking
357 lkopen_dot (char *file, int access, mode_t mode)
360 struct lockinfo lkinfo;
363 if ((fd = open (file, access, mode)) == -1)
367 ** Get the name of the eventual lock file, as well
368 ** as a name for a temporary lock file.
370 lockname (file, &lkinfo, 1);
372 #if !defined(HAVE_LIBLOCKFILE)
376 /* attempt to create lock file */
377 if (lockit (&lkinfo) == 0) {
378 /* if successful, turn on timer and return */
379 timerON (lkinfo.curlock, fd);
383 ** Abort locking, if we fail to lock after 5
384 ** attempts and are never able to stat the
388 if (stat (lkinfo.curlock, &st) == -1) {
398 ** check for stale lockfile,
401 if (curtime > st.st_ctime + RSECS)
402 unlink (lkinfo.curlock);
406 lockname (file, &lkinfo, 1);
411 if (lockfile_create(lkinfo.curlock, 5, 0) == L_SUCCESS) {
412 timerON(lkinfo.curlock, fd);
418 #endif /* HAVE_LIBLOCKFILE */
421 #if !defined(HAVE_LIBLOCKFILE)
423 ** Routine that actually tries to create
428 lockit (struct lockinfo *li)
431 char *curlock, *tmplock;
437 curlock = li->curlock;
438 tmplock = li->tmplock;
441 if ((fd = mkstemp(tmplock)) == -1)
444 if (mktemp(tmplock) == NULL)
446 if (unlink(tmplock) == -1 && errno != ENOENT)
448 /* create the temporary lock file */
449 if ((fd = creat(tmplock, 0600)) == -1)
454 /* write our process id into lock file */
455 snprintf (buffer, sizeof(buffer), "nmh lock: pid %d\n", (int) getpid());
456 write(fd, buffer, strlen(buffer) + 1);
462 ** Now try to create the real lock file
463 ** by linking to the temporary file.
465 fd = link(tmplock, curlock);
468 return (fd == -1 ? -1 : 0);
470 #endif /* HAVE_LIBLOCKFILE */
473 ** Get name of lock file, and temporary lock file
477 lockname (char *file, struct lockinfo *li, int isnewlock)
486 if ((cp = strrchr (file, '/')) == NULL || *++cp == 0)
492 snprintf (bp, sizeof(li->curlock), "%s/", lockdir);
493 tmplen = strlen (bp);
498 snprintf (bp, sizeof(li->curlock), "%.*s", (int)(cp - file), file);
499 tmplen = strlen (bp);
507 ** mmdf style dot locking. Currently not supported.
508 ** If we start supporting mmdf style dot locking,
509 ** we will need to change the return value of lockname
511 if (stat (file, &st) == -1)
514 snprintf (bp, sizeof(li->curlock) - bplen, "LCK%05d.%05d",
515 st.st_dev, st.st_ino);
518 snprintf (bp, sizeof(li->curlock) - bplen, "%s.lock", cp);
520 #if !defined(HAVE_LIBLOCKFILE)
522 ** If this is for a new lock, create a name for
523 ** the temporary lock file for lockit()
526 if ((cp = strrchr (li->curlock, '/')) == NULL || *++cp == 0)
527 strncpy (li->tmplock, ",LCK.XXXXXX", sizeof(li->tmplock));
529 snprintf (li->tmplock, sizeof(li->tmplock), "%.*s,LCK.XXXXXX",
530 (int)(cp - li->curlock), li->curlock);
537 ** Add new lockfile to the list of open lockfiles
538 ** and start the lock file timer.
542 timerON (char *curlock, int fd)
547 lp = (struct lock *) mh_xmalloc (sizeof(*lp));
549 len = strlen(curlock) + 1;
551 lp->l_lock = mh_xmalloc (len);
552 memcpy (lp->l_lock, curlock, len);
556 /* perhaps SIGT{STP,TIN,TOU} ? */
557 SIGNAL (SIGALRM, alrmser);
566 ** Search through the list of lockfiles for the
567 ** current lockfile, and remove it from the list.
573 struct lock *pp, *lp;
578 for (pp = lp = l_top; lp; pp = lp, lp = lp->l_next) {
586 pp->l_next = lp->l_next;
593 /* if there are locks left, restart timer */
600 ** If timer goes off, we update the ctime of all open
601 ** lockfiles, so another command doesn't remove them.
610 #ifndef RELIABLE_SIGNALS
611 SIGNAL (SIGALRM, alrmser);
614 /* update the ctime of all the lock files */
615 for (lp = l_top; lp; lp = lp->l_next) {
616 lockfile = lp->l_lock;
617 #if !defined(HAVE_LIBLOCKFILE)
620 if (*lockfile && (j = creat (lockfile, 0600)) != -1)
624 lockfile_touch(lockfile);
628 /* restart the alarm */
632 #endif /* DOT_LOCKING */