]>
diplodocus.org Git - nmh/blob - sbr/lock_file.c
079f089640788f8ba6556e8c68140ed3941fdf1f
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>
23 #ifdef HAVE_SYS_TIME_H
24 # 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 * Number of tries to retry locking
56 #define LOCK_RETRIES 5
59 * Amount of time to wait before
60 * updating ctime of lock file.
64 #if !defined(HAVE_LIBLOCKFILE)
66 * How old does a lock file need to be
67 * before we remove it.
70 #endif /* HAVE_LIBLOCKFILE */
72 /* struct for recording and updating locks */
79 enum locktype
{ FCNTL_LOCKING
, FLOCK_LOCKING
, LOCKF_LOCKING
, DOT_LOCKING
};
82 * Flags to indicate whether we've initialized the lock types, and
83 * our saved lock types
85 static int datalockinit
= 0;
86 static int spoollockinit
= 0;
87 static enum locktype datalocktype
, spoollocktype
;
90 /* top of list containing all open locks */
91 static struct lock
*l_top
= NULL
;
93 static int lkopen(const char *, int, mode_t
, enum locktype
);
94 static int str2accbits(const char *);
96 static int lkopen_fcntl (const char *, int, mode_t
);
98 static int lkopen_lockf (const char *, int, mode_t
);
99 #endif /* HAVE_LOCKF */
101 static int lkopen_flock (const char *, int, mode_t
);
102 #endif /* HAVE_FLOCK */
104 static enum locktype
init_locktype(const char *);
106 static int lkopen_dot (const char *, int, mode_t
);
107 static void lkclose_dot (int, const char *);
108 static void lockname (const char *, struct lockinfo
*, int);
109 static void timerON (char *, int);
110 static void timerOFF (int);
111 static void alrmser (int);
113 #if !defined(HAVE_LIBLOCKFILE)
114 static int lockit (struct lockinfo
*);
118 * Base functions: determine the data type used to lock files and
119 * call the underlying function.
123 lkopendata(const char *file
, int access
, mode_t mode
)
125 if (! datalockinit
) {
126 char *cp
= context_find("datalocking");
129 datalocktype
= init_locktype(cp
);
131 /* We default to fcntl locking for data files */
132 datalocktype
= FCNTL_LOCKING
;
138 return lkopen(file
, access
, mode
, datalocktype
);
143 * Locking using the spool locking algorithm
146 int lkopenspool(const char *file
, int access
, mode_t mode
)
148 if (! spoollockinit
) {
149 spoollocktype
= init_locktype(spoollocking
);
154 return lkopen(file
, access
, mode
, spoollocktype
);
159 * Versions of lkopen that return a FILE *
163 lkfopendata(const char *file
, const char *mode
)
166 int oflags
= str2accbits(mode
);
174 if ((fd
= lkopendata(file
, oflags
, 0666)) == -1)
177 if ((fp
= fdopen (fd
, mode
)) == NULL
) {
186 lkfopenspool(const char *file
, const char *mode
)
189 int oflags
= str2accbits(mode
);
197 if ((fd
= lkopenspool(file
, oflags
, 0666)) == -1)
200 if ((fp
= fdopen (fd
, mode
)) == NULL
) {
210 * Corresponding close functions.
212 * A note here: All of the kernel locking functions terminate the lock
213 * when the descriptor is closed, so why write the code to explicitly
214 * unlock the file? We only need to do this in the dot-locking case.
218 lkclosedata(int fd
, const char *name
)
222 if (datalocktype
== DOT_LOCKING
)
223 lkclose_dot(fd
, name
);
229 lkfclosedata(FILE *f
, const char *name
)
239 if (datalocktype
== DOT_LOCKING
)
240 lkclose_dot(fd
, name
);
246 lkclosespool(int fd
, const char *name
)
250 if (spoollocktype
== DOT_LOCKING
)
251 lkclose_dot(fd
, name
);
257 lkfclosespool(FILE *f
, const char *name
)
267 if (spoollocktype
== DOT_LOCKING
)
268 lkclose_dot(fd
, name
);
275 * Convert fopen() mode argument to open() bits
279 str2accbits(const char *mode
)
281 if (strcmp (mode
, "r") == 0)
283 else if (strcmp (mode
, "r+") == 0)
285 else if (strcmp (mode
, "w") == 0)
286 return O_WRONLY
| O_CREAT
| O_TRUNC
;
287 else if (strcmp (mode
, "w+") == 0)
288 return O_RDWR
| O_CREAT
| O_TRUNC
;
289 else if (strcmp (mode
, "a") == 0)
290 return O_WRONLY
| O_CREAT
| O_APPEND
;
291 else if (strcmp (mode
, "a+") == 0)
292 return O_RDWR
| O_CREAT
| O_APPEND
;
300 * Internal routine to switch between different locking types.
304 lkopen (const char *file
, int access
, mode_t mode
, enum locktype ltype
)
309 return lkopen_fcntl(file
, access
, mode
);
312 return lkopen_dot(file
, access
, mode
);
316 return lkopen_flock(file
, access
, mode
);
317 #endif /* HAVE_FLOCK */
321 return lkopen_lockf(file
, access
, mode
);
322 #endif /* HAVE_FLOCK */
325 adios(NULL
, "Internal locking error: unsupported lock type used!");
333 * Routine to clean up the dot locking file
337 lkclose_dot (int fd
, const char *file
)
339 struct lockinfo lkinfo
;
341 lockname (file
, &lkinfo
, 0); /* get name of lock file */
342 #if !defined(HAVE_LIBLOCKFILE)
343 unlink (lkinfo
.curlock
); /* remove lock file */
345 lockfile_remove(lkinfo
.curlock
);
346 #endif /* HAVE_LIBLOCKFILE */
347 timerOFF (fd
); /* turn off lock timer */
352 * Open and lock a file, using fcntl locking
356 lkopen_fcntl(const char *file
, int access
, mode_t mode
)
358 int fd
, i
, saved_errno
;
362 * The assumption here is that if you open the file for writing, you
363 * need an exclusive lock.
366 for (i
= 0; i
< LOCK_RETRIES
; i
++) {
367 if ((fd
= open(file
, access
, mode
)) == -1)
372 flk
.l_type
= (access
& O_ACCMODE
) == O_RDONLY
? F_RDLCK
: F_WRLCK
;
373 flk
.l_whence
= SEEK_SET
;
375 if (fcntl(fd
, F_SETLK
, &flk
) != -1)
389 * Open and lock a file, using flock locking
393 lkopen_flock(const char *file
, int access
, mode_t mode
)
395 int fd
, i
, saved_errno
, locktype
;
398 * The assumption here is that if you open the file for writing, you
399 * need an exclusive lock.
402 locktype
= (((access
& O_ACCMODE
) == O_RDONLY
) ? LOCK_SH
: LOCK_EX
) |
405 for (i
= 0; i
< LOCK_RETRIES
; i
++) {
406 if ((fd
= open(file
, access
, mode
)) == -1)
409 if (flock(fd
, locktype
) != -1)
423 * Open and lock a file, using lockf locking
427 lkopen_lockf(const char *file
, int access
, mode_t mode
)
429 int fd
, i
, saved_errno
, saved_access
;
434 * Because lockf locks start from the current offset, mask off O_APPEND
435 * and seek to the end of the file later if it was requested.
437 * lockf locks require write access to the file, so always add it
438 * even if it wasn't requested.
441 saved_access
= access
;
445 if ((access
& O_ACCMODE
) == O_RDONLY
) {
450 for (i
= 0; i
< LOCK_RETRIES
; i
++) {
451 if ((fd
= open(file
, access
, mode
)) == -1)
454 if (lockf(fd
, F_TLOCK
, 0) != -1) {
456 * Seek to end if requested
458 if (saved_access
& O_APPEND
) {
459 lseek(fd
, 0, SEEK_END
);
475 * open and lock a file, using dot locking
479 lkopen_dot (const char *file
, int access
, mode_t mode
)
482 struct lockinfo lkinfo
;
485 if ((fd
= open (file
, access
, mode
)) == -1)
489 * Get the name of the eventual lock file, as well
490 * as a name for a temporary lock file.
492 lockname (file
, &lkinfo
, 1);
494 #if !defined(HAVE_LIBLOCKFILE)
498 /* attempt to create lock file */
499 if (lockit (&lkinfo
) == 0) {
500 /* if successful, turn on timer and return */
501 timerON (lkinfo
.curlock
, fd
);
505 * Abort locking, if we fail to lock after 5 attempts
506 * and are never able to stat the lock file.
509 if (stat (lkinfo
.curlock
, &st
) == -1) {
518 /* check for stale lockfile, else sleep */
519 if (curtime
> st
.st_ctime
+ RSECS
)
520 unlink (lkinfo
.curlock
);
524 lockname (file
, &lkinfo
, 1);
529 if (lockfile_create(lkinfo
.curlock
, 5, 0) == L_SUCCESS
) {
530 timerON(lkinfo
.curlock
, fd
);
537 #endif /* HAVE_LIBLOCKFILE */
540 #if !defined(HAVE_LIBLOCKFILE)
542 * Routine that actually tries to create
547 lockit (struct lockinfo
*li
)
550 char *curlock
, *tmplock
;
556 curlock
= li
->curlock
;
557 tmplock
= li
->tmplock
;
559 if ((fd
= mkstemp(tmplock
)) == -1)
563 /* write our process id into lock file */
564 snprintf (buffer
, sizeof(buffer
), "nmh lock: pid %d\n", (int) getpid());
565 write(fd
, buffer
, strlen(buffer
) + 1);
571 * Now try to create the real lock file
572 * by linking to the temporary file.
574 fd
= link(tmplock
, curlock
);
577 return (fd
== -1 ? -1 : 0);
579 #endif /* HAVE_LIBLOCKFILE */
582 * Get name of lock file, and temporary lock file
586 lockname (const char *file
, struct lockinfo
*li
, int isnewlock
)
596 if ((cp
= strrchr (file
, '/')) == NULL
|| *++cp
== 0)
602 snprintf (bp
, sizeof(li
->curlock
), "%s/", lockdir
);
603 tmplen
= strlen (bp
);
608 snprintf (bp
, sizeof(li
->curlock
), "%.*s", (int)(cp
- file
), file
);
609 tmplen
= strlen (bp
);
617 * mmdf style dot locking. Currently not supported.
618 * If we start supporting mmdf style dot locking,
619 * we will need to change the return value of lockname
621 if (stat (file
, &st
) == -1)
624 snprintf (bp
, sizeof(li
->curlock
) - bplen
, "LCK%05d.%05d",
625 st
.st_dev
, st
.st_ino
);
628 snprintf (bp
, sizeof(li
->curlock
) - bplen
, "%s.lock", cp
);
630 #if !defined(HAVE_LIBLOCKFILE)
632 * If this is for a new lock, create a name for
633 * the temporary lock file for lockit()
636 if ((cp
= strrchr (li
->curlock
, '/')) == NULL
|| *++cp
== 0)
637 strncpy (li
->tmplock
, ",LCK.XXXXXX", sizeof(li
->tmplock
));
639 snprintf (li
->tmplock
, sizeof(li
->tmplock
), "%.*s,LCK.XXXXXX",
640 (int)(cp
- li
->curlock
), li
->curlock
);
647 * Add new lockfile to the list of open lockfiles
648 * and start the lock file timer.
652 timerON (char *curlock
, int fd
)
657 lp
= (struct lock
*) mh_xmalloc (sizeof(*lp
));
659 len
= strlen(curlock
) + 1;
661 lp
->l_lock
= mh_xmalloc (len
);
662 memcpy (lp
->l_lock
, curlock
, len
);
666 /* perhaps SIGT{STP,TIN,TOU} ? */
667 SIGNAL (SIGALRM
, alrmser
);
676 * Search through the list of lockfiles for the
677 * current lockfile, and remove it from the list.
683 struct lock
*pp
, *lp
;
688 for (pp
= lp
= l_top
; lp
; pp
= lp
, lp
= lp
->l_next
) {
696 pp
->l_next
= lp
->l_next
;
703 /* if there are locks left, restart timer */
710 * If timer goes off, we update the ctime of all open
711 * lockfiles, so another command doesn't remove them.
721 /* update the ctime of all the lock files */
722 for (lp
= l_top
; lp
; lp
= lp
->l_next
) {
723 lockfile
= lp
->l_lock
;
724 #if !defined(HAVE_LIBLOCKFILE)
727 if (*lockfile
&& (j
= creat (lockfile
, 0600)) != -1)
731 lockfile_touch(lockfile
);
735 /* restart the alarm */
741 * Return a locking algorithm based on the string name
745 init_locktype(const char *lockname
)
747 if (strcasecmp(lockname
, "fcntl") == 0) {
748 return FCNTL_LOCKING
;
749 } else if (strcasecmp(lockname
, "lockf") == 0) {
751 return LOCKF_LOCKING
;
752 #else /* ! HAVE_LOCKF */
753 adios(NULL
, "lockf not supported on this system");
754 #endif /* HAVE_LOCKF */
755 } else if (strcasecmp(lockname
, "flock") == 0) {
757 return FLOCK_LOCKING
;
758 #else /* ! HAVE_FLOCK */
759 adios(NULL
, "flock not supported on this system");
760 #endif /* HAVE_FLOCK */
761 } else if (strcasecmp(lockname
, "dot") == 0) {
764 adios(NULL
, "Unknown lock type: \"%s\"", lockname
);