3 * lock.c -- routines to lock/unlock files
16 # include <mmdfonly.h>
17 # include <lockonly.h>
23 # include <sys/file.h>
26 #if defined(LOCKF_LOCKING) || defined(FLOCK_LOCKING)
27 # include <sys/file.h>
35 char *lockdir = LOCKDIR;
38 /* Are we using any kernel locking? */
39 #if defined (FLOCK_LOCKING) || defined(LOCKF_LOCKING) || defined(FCNTL_LOCKING)
40 # define KERNEL_LOCKING
45 /* struct for getting name of lock file to create */
52 * Amount of time to wait before
53 * updating ctime of lock file.
58 * How old does a lock file need to be
59 * before we remove it.
63 /* struct for recording and updating locks */
70 /* top of list containing all open locks */
71 static struct lock *l_top = NULL;
72 #endif /* DOT_LOCKING */
78 static int lkopen_kernel (char *, int, mode_t);
82 static int lkopen_dot (char *, int, mode_t);
83 static int lockit (struct lockinfo *);
84 static void lockname (char *, struct lockinfo *, int);
85 static void timerON (char *, int);
86 static void timerOFF (int);
87 static RETSIGTYPE alrmser (int);
92 * Base routine to open and lock a file,
93 * and return a file descriptor.
97 lkopen (char *file, int access, mode_t mode)
100 return lkopen_kernel(file, access, mode);
104 return lkopen_dot(file, access, mode);
110 * Base routine to close and unlock a file,
111 * given a file descriptor.
115 lkclose (int fd, char *file)
122 struct lockinfo lkinfo;
129 buf.l_type = F_UNLCK;
130 buf.l_whence = SEEK_SET;
133 fcntl(fd, F_SETLK, &buf);
141 /* make sure we unlock the whole thing */
142 lseek (fd, (off_t) 0, SEEK_SET);
143 lockf (fd, F_ULOCK, 0L);
147 lockname (file, &lkinfo, 0); /* get name of lock file */
148 unlink (lkinfo.curlock); /* remove lock file */
149 timerOFF (fd); /* turn off lock timer */
157 * Base routine to open and lock a file,
158 * and return a FILE pointer
162 lkfopen (char *file, char *mode)
167 if (strcmp (mode, "r") == 0)
172 if ((fd = lkopen (file, access, 0)) == -1)
175 if ((fp = fdopen (fd, mode)) == NULL) {
185 * Base routine to close and unlock a file,
186 * given a FILE pointer
190 lkfclose (FILE *fp, char *file)
197 struct lockinfo lkinfo;
204 buf.l_type = F_UNLCK;
205 buf.l_whence = SEEK_SET;
208 fcntl(fileno(fp), F_SETLK, &buf);
212 flock (fileno(fp), LOCK_UN);
216 /* make sure we unlock the whole thing */
217 fseek (fp, 0L, SEEK_SET);
218 lockf (fileno(fp), F_ULOCK, 0L);
222 lockname (file, &lkinfo, 0); /* get name of lock file */
223 unlink (lkinfo.curlock); /* remove lock file */
224 timerOFF (fileno(fp)); /* turn off lock timer */
227 return (fclose (fp));
231 #ifdef KERNEL_LOCKING
234 * open and lock a file, using kernel locking
238 lkopen_kernel (char *file, int access, mode_t mode)
242 # ifdef FCNTL_LOCKING
244 # endif /* FCNTL_LOCKING */
246 for (i = 0; i < 5; i++) {
248 # if defined(LOCKF_LOCKING) || defined(FCNTL_LOCKING)
249 /* remember the original mode */
252 /* make sure we open at the beginning */
256 * We MUST have write permission or
257 * lockf/fcntl() won't work
259 if ((access & 03) == O_RDONLY) {
263 # endif /* LOCKF_LOCKING || FCNTL_LOCKING */
265 if ((fd = open (file, access | O_NDELAY, mode)) == -1)
268 # ifdef FCNTL_LOCKING
269 buf.l_type = F_WRLCK;
270 buf.l_whence = SEEK_SET;
273 if (fcntl (fd, F_SETLK, &buf) != -1)
277 # ifdef FLOCK_LOCKING
278 if (flock (fd, LOCK_EX | LOCK_NB) != -1)
282 # ifdef LOCKF_LOCKING
283 if (lockf (fd, F_TLOCK, 0L) != -1) {
284 /* see if we should be at the end */
286 lseek (fd, (off_t) 0, SEEK_END);
301 #endif /* KERNEL_LOCKING */
307 * open and lock a file, using dot locking
311 lkopen_dot (char *file, int access, mode_t mode)
315 struct lockinfo lkinfo;
319 if ((fd = open (file, access, mode)) == -1)
323 * Get the name of the eventual lock file, as well
324 * as a name for a temporary lock file.
326 lockname (file, &lkinfo, 1);
329 /* attempt to create lock file */
330 if (lockit (&lkinfo) == 0) {
331 /* if successful, turn on timer and return */
332 timerON (lkinfo.curlock, fd);
336 * Abort locking, if we fail to lock after 5 attempts
337 * and are never able to stat the lock file.
339 if (stat (lkinfo.curlock, &st) == -1) {
347 /* check for stale lockfile, else sleep */
348 if (curtime > st.st_ctime + RSECS)
349 unlink (lkinfo.curlock);
358 * Routine that actually tries to create
363 lockit (struct lockinfo *li)
366 char *curlock, *tmplock;
372 curlock = li->curlock;
373 tmplock = li->tmplock;
375 /* create the temporary lock file */
376 if ((fd = creat(tmplock, 0600)) == -1)
380 /* write our process id into lock file */
381 snprintf (buffer, sizeof(buffer), "nmh lock: pid %d\n", (int) getpid());
382 write(fd, buffer, strlen(buffer) + 1);
388 * Now try to create the real lock file
389 * by linking to the temporary file.
391 fd = link(tmplock, curlock);
394 return (fd == -1 ? -1 : 0);
398 * Get name of lock file, and temporary lock file
402 lockname (char *file, struct lockinfo *li, int isnewlock)
411 if ((cp = strrchr (file, '/')) == NULL || *++cp == 0)
417 snprintf (bp, sizeof(li->curlock), "%s/", lockdir);
418 tmplen = strlen (bp);
423 snprintf (bp, sizeof(li->curlock), "%.*s", cp - file, file);
424 tmplen = strlen (bp);
432 * mmdf style dot locking. Currently not supported.
433 * If we start supporting mmdf style dot locking,
434 * we will need to change the return value of lockname
436 if (stat (file, &st) == -1)
439 snprintf (bp, sizeof(li->curlock) - bplen, "LCK%05d.%05d",
440 st.st_dev, st.st_ino);
443 snprintf (bp, sizeof(li->curlock) - bplen, "%s.lock", cp);
446 * If this is for a new lock, create a name for
447 * the temporary lock file for lockit()
450 if ((cp = strrchr (li->curlock, '/')) == NULL || *++cp == 0)
451 strncpy (li->tmplock, ",LCK.XXXXXX", sizeof(li->tmplock));
453 snprintf (li->tmplock, sizeof(li->tmplock), "%.*s,LCK.XXXXXX",
454 cp - li->curlock, li->curlock);
455 mktemp (li->tmplock);
456 unlink (li->tmplock); /* remove any stray */
462 * Add new lockfile to the list of open lockfiles
463 * and start the lock file timer.
467 timerON (char *curlock, int fd)
472 if (!(lp = (struct lock *) malloc (sizeof(*lp))))
475 len = strlen(curlock) + 1;
477 if (!(lp->l_lock = malloc (len))) {
481 memcpy (lp->l_lock, curlock, len);
485 /* perhaps SIGT{STP,TIN,TOU} ? */
486 SIGNAL (SIGALRM, alrmser);
495 * Search through the list of lockfiles for the
496 * current lockfile, and remove it from the list.
502 struct lock *pp, *lp;
507 for (pp = lp = l_top; lp; pp = lp, lp = lp->l_next) {
515 pp->l_next = lp->l_next;
522 /* if there are locks left, restart timer */
529 * If timer goes off, we update the ctime of all open
530 * lockfiles, so another command doesn't remove them.
540 #ifndef RELIABLE_SIGNALS
541 SIGNAL (SIGALRM, alrmser);
544 /* update the ctime of all the lock files */
545 for (lp = l_top; lp; lp = lp->l_next) {
546 lockfile = lp->l_lock;
547 if (*lockfile && (j = creat (lockfile, 0600)) != -1)
551 /* restart the alarm */
555 #endif /* DOT_LOCKING */