]>
diplodocus.org Git - nmh/blob - sbr/lock_file.c
6653a28bac3cd3cf3548e381c3640ea7ba4e3e7c
3 * lock.c -- routines to lock/unlock files
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * 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>
29 # include <sys/file.h>
37 #if defined(HAVE_LIBLOCKFILE)
42 char *lockdir
= LOCKDIR
;
45 /* struct for getting name of lock file to create */
48 #if !defined(HAVE_LIBLOCKFILE)
54 * Amount of time to wait before
55 * updating ctime of lock file.
59 #if !defined(HAVE_LIBLOCKFILE)
61 * How old does a lock file need to be
62 * before we remove it.
65 #endif /* HAVE_LIBLOCKFILE */
67 /* struct for recording and updating locks */
74 /* top of list containing all open locks */
75 static struct lock
*l_top
= NULL
;
77 static int lkopen_fcntl (char *, int, mode_t
, int);
79 static int lkopen_lockf (char *, int, mode_t
, int);
80 #endif /* HAVE_LOCKF */
83 static int lkopen_dot (char *, int, mode_t
);
84 static void lockname (char *, struct lockinfo
*, int);
85 static void timerON (char *, int);
86 static void timerOFF (int);
87 static void alrmser (int);
89 #if !defined(HAVE_LIBLOCKFILE)
90 static int lockit (struct lockinfo
*);
95 * Base routine to open and lock a file,
96 * and return a file descriptor.
100 lkopen (char *file
, int access
, mode_t mode
)
102 #ifdef KERNEL_LOCKING
103 return lkopen_kernel(file
, access
, mode
);
107 return lkopen_dot(file
, access
, mode
);
113 * Base routine to close and unlock a file,
114 * given a file descriptor.
118 lkclose (int fd
, char *file
)
125 struct lockinfo lkinfo
;
132 buf
.l_type
= F_UNLCK
;
133 buf
.l_whence
= SEEK_SET
;
136 fcntl(fd
, F_SETLK
, &buf
);
144 /* make sure we unlock the whole thing */
145 lseek (fd
, (off_t
) 0, SEEK_SET
);
146 lockf (fd
, F_ULOCK
, 0L);
150 lockname (file
, &lkinfo
, 0); /* get name of lock file */
151 #if !defined(HAVE_LIBLOCKFILE)
152 unlink (lkinfo
.curlock
); /* remove lock file */
154 lockfile_remove(lkinfo
.curlock
);
155 #endif /* HAVE_LIBLOCKFILE */
156 timerOFF (fd
); /* turn off lock timer */
157 #else /* DOT_LOCKING */
159 #endif /* DOT_LOCKING */
166 * Base routine to open and lock a file,
167 * and return a FILE pointer
171 lkfopen (char *file
, char *mode
)
176 if (strcmp (mode
, "r") == 0)
178 else if (strcmp (mode
, "r+") == 0)
180 else if (strcmp (mode
, "w") == 0)
181 access
= O_WRONLY
| O_CREAT
| O_TRUNC
;
182 else if (strcmp (mode
, "w+") == 0)
183 access
= O_RDWR
| O_CREAT
| O_TRUNC
;
184 else if (strcmp (mode
, "a") == 0)
185 access
= O_WRONLY
| O_CREAT
| O_APPEND
;
186 else if (strcmp (mode
, "a+") == 0)
187 access
= O_RDWR
| O_CREAT
| O_APPEND
;
193 if ((fd
= lkopen (file
, access
, 0666)) == -1)
196 if ((fp
= fdopen (fd
, mode
)) == NULL
) {
206 * Base routine to close and unlock a file,
207 * given a FILE pointer
211 lkfclose (FILE *fp
, char *file
)
218 struct lockinfo lkinfo
;
225 buf
.l_type
= F_UNLCK
;
226 buf
.l_whence
= SEEK_SET
;
229 fcntl(fileno(fp
), F_SETLK
, &buf
);
233 flock (fileno(fp
), LOCK_UN
);
237 /* make sure we unlock the whole thing */
238 fseek (fp
, 0L, SEEK_SET
);
239 lockf (fileno(fp
), F_ULOCK
, 0L);
243 lockname (file
, &lkinfo
, 0); /* get name of lock file */
244 #if !defined(HAVE_LIBLOCKFILE)
245 unlink (lkinfo
.curlock
); /* remove lock file */
247 lockfile_remove(lkinfo
.curlock
);
248 #endif /* HAVE_LIBLOCKFILE */
249 timerOFF (fileno(fp
)); /* turn off lock timer */
250 #else /* DOT_LOCKING */
252 #endif /* DOT_LOCKING */
254 return (fclose (fp
));
258 #ifdef KERNEL_LOCKING
261 * open and lock a file, using kernel locking
265 lkopen_kernel (char *file
, int access
, mode_t mode
)
269 # ifdef FCNTL_LOCKING
271 # endif /* FCNTL_LOCKING */
273 for (i
= 0; i
< 5; i
++) {
275 # if defined(LOCKF_LOCKING) || defined(FCNTL_LOCKING)
276 /* remember the original mode */
279 /* make sure we open at the beginning */
283 * We MUST have write permission or
284 * lockf/fcntl() won't work
286 if ((access
& 03) == O_RDONLY
) {
290 # endif /* LOCKF_LOCKING || FCNTL_LOCKING */
292 if ((fd
= open (file
, access
| O_NDELAY
, mode
)) == -1)
295 # ifdef FCNTL_LOCKING
296 buf
.l_type
= F_WRLCK
;
297 buf
.l_whence
= SEEK_SET
;
300 if (fcntl (fd
, F_SETLK
, &buf
) != -1)
304 # ifdef FLOCK_LOCKING
305 if (flock (fd
, (((access
& 03) == O_RDONLY
) ? LOCK_SH
: LOCK_EX
)
310 # ifdef LOCKF_LOCKING
311 if (lockf (fd
, F_TLOCK
, 0L) != -1) {
312 /* see if we should be at the end */
314 lseek (fd
, (off_t
) 0, SEEK_END
);
329 #endif /* KERNEL_LOCKING */
335 * open and lock a file, using dot locking
339 lkopen_dot (char *file
, int access
, mode_t mode
)
342 struct lockinfo lkinfo
;
345 if ((fd
= open (file
, access
, mode
)) == -1)
349 * Get the name of the eventual lock file, as well
350 * as a name for a temporary lock file.
352 lockname (file
, &lkinfo
, 1);
354 #if !defined(HAVE_LIBLOCKFILE)
358 /* attempt to create lock file */
359 if (lockit (&lkinfo
) == 0) {
360 /* if successful, turn on timer and return */
361 timerON (lkinfo
.curlock
, fd
);
365 * Abort locking, if we fail to lock after 5 attempts
366 * and are never able to stat the lock file.
369 if (stat (lkinfo
.curlock
, &st
) == -1) {
378 /* check for stale lockfile, else sleep */
379 if (curtime
> st
.st_ctime
+ RSECS
)
380 unlink (lkinfo
.curlock
);
384 lockname (file
, &lkinfo
, 1);
389 if (lockfile_create(lkinfo
.curlock
, 5, 0) == L_SUCCESS
) {
390 timerON(lkinfo
.curlock
, fd
);
397 #endif /* HAVE_LIBLOCKFILE */
400 #if !defined(HAVE_LIBLOCKFILE)
402 * Routine that actually tries to create
407 lockit (struct lockinfo
*li
)
410 char *curlock
, *tmplock
;
416 curlock
= li
->curlock
;
417 tmplock
= li
->tmplock
;
419 if ((fd
= mkstemp(tmplock
)) == -1)
423 /* write our process id into lock file */
424 snprintf (buffer
, sizeof(buffer
), "nmh lock: pid %d\n", (int) getpid());
425 write(fd
, buffer
, strlen(buffer
) + 1);
431 * Now try to create the real lock file
432 * by linking to the temporary file.
434 fd
= link(tmplock
, curlock
);
437 return (fd
== -1 ? -1 : 0);
439 #endif /* HAVE_LIBLOCKFILE */
442 * Get name of lock file, and temporary lock file
446 lockname (char *file
, struct lockinfo
*li
, int isnewlock
)
455 if ((cp
= strrchr (file
, '/')) == NULL
|| *++cp
== 0)
461 snprintf (bp
, sizeof(li
->curlock
), "%s/", lockdir
);
462 tmplen
= strlen (bp
);
467 snprintf (bp
, sizeof(li
->curlock
), "%.*s", (int)(cp
- file
), file
);
468 tmplen
= strlen (bp
);
476 * mmdf style dot locking. Currently not supported.
477 * If we start supporting mmdf style dot locking,
478 * we will need to change the return value of lockname
480 if (stat (file
, &st
) == -1)
483 snprintf (bp
, sizeof(li
->curlock
) - bplen
, "LCK%05d.%05d",
484 st
.st_dev
, st
.st_ino
);
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
), "%.*s,LCK.XXXXXX",
499 (int)(cp
- li
->curlock
), li
->curlock
);
506 * Add new lockfile to the list of open lockfiles
507 * and start the lock file timer.
511 timerON (char *curlock
, int fd
)
516 lp
= (struct lock
*) mh_xmalloc (sizeof(*lp
));
518 len
= strlen(curlock
) + 1;
520 lp
->l_lock
= mh_xmalloc (len
);
521 memcpy (lp
->l_lock
, curlock
, len
);
525 /* perhaps SIGT{STP,TIN,TOU} ? */
526 SIGNAL (SIGALRM
, alrmser
);
535 * Search through the list of lockfiles for the
536 * current lockfile, and remove it from the list.
542 struct lock
*pp
, *lp
;
547 for (pp
= lp
= l_top
; lp
; pp
= lp
, lp
= lp
->l_next
) {
555 pp
->l_next
= lp
->l_next
;
562 /* if there are locks left, restart timer */
569 * If timer goes off, we update the ctime of all open
570 * 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 */
601 * Return a locking algorithm based on the string name
605 init_locktype(const char *lockname
)
607 if (mh_strcasecmp(lockname
, "fcntl") == 0) {
608 return FCNTL_LOCKING
;
609 } else if (mh_strcasecmp(lockname
, "lockf") == 0) {
611 return LOCKF_LOCKING
;
612 #else /* ! HAVE_LOCKF */
613 adios(NULL
, "lockf not supported on this system");
614 #endif /* HAVE_LOCKF */
615 } else if (mh_strcasecmp(lockname
, "flock") == 0) {
617 return FLOCK_LOCKING
;
618 #else /* ! HAVE_FLOCK */
619 adios(NULL
, "flock not supported on this system");
620 #endif /* HAVE_FLOCK */
621 } else if (mh_strcasecmp(lockname
, "dot") == 0) {
624 adios(NULL
, "Unknown lock type: \"%s\"", lockname
)