]>
diplodocus.org Git - nmh/blob - sbr/lock_file.c
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>
32 #if defined(HAVE_LIBLOCKFILE)
33 # include <lockfile.h>
37 char *lockdir
= LOCKDIR
;
40 /* struct for getting name of lock file to create */
43 #if !defined(HAVE_LIBLOCKFILE)
49 * Number of tries to retry locking
51 #define LOCK_RETRIES 60
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 enum locktype
{ FCNTL_LOCKING
, FLOCK_LOCKING
, LOCKF_LOCKING
, DOT_LOCKING
};
77 * Flags to indicate whether we've initialized the lock types, and
78 * our saved lock types
80 static int datalockinit
= 0;
81 static int spoollockinit
= 0;
82 static enum locktype datalocktype
, spoollocktype
;
85 /* top of list containing all open locks */
86 static struct lock
*l_top
= NULL
;
88 static int lkopen(const char *, int, mode_t
, enum locktype
, int *);
89 static int str2accbits(const char *);
91 static int lkopen_fcntl (const char *, int, mode_t
, int *);
93 static int lkopen_lockf (const char *, int, mode_t
, int *);
94 #endif /* HAVE_LOCKF */
96 static int lkopen_flock (const char *, int, mode_t
, int *);
97 #endif /* HAVE_FLOCK */
99 static enum locktype
init_locktype(const char *);
101 static int lkopen_dot (const char *, int, mode_t
, int *);
102 static void lkclose_dot (int, const char *);
103 static void lockname (const char *, struct lockinfo
*, int);
104 static void timerON (char *, int);
105 static void timerOFF (int);
106 static void alrmser (int);
108 #if !defined(HAVE_LIBLOCKFILE)
109 static int lockit (struct lockinfo
*);
113 * Base functions: determine the data type used to lock files and
114 * call the underlying function.
118 lkopendata(const char *file
, int access
, mode_t mode
, int *failed_to_lock
)
120 if (! datalockinit
) {
121 char *cp
= context_find("datalocking");
124 datalocktype
= init_locktype(cp
);
126 /* We default to fcntl locking for data files */
127 datalocktype
= FCNTL_LOCKING
;
133 return lkopen(file
, access
, mode
, datalocktype
, failed_to_lock
);
138 * Locking using the spool locking algorithm
141 int lkopenspool(const char *file
, int access
, mode_t mode
, int *failed_to_lock
)
143 if (! spoollockinit
) {
144 spoollocktype
= init_locktype(spoollocking
);
149 return lkopen(file
, access
, mode
, spoollocktype
, failed_to_lock
);
154 * Versions of lkopen that return a FILE *
158 lkfopendata(const char *file
, const char *mode
, int *failed_to_lock
)
161 int oflags
= str2accbits(mode
);
169 if ((fd
= lkopendata(file
, oflags
, 0666, failed_to_lock
)) == -1)
172 if ((fp
= fdopen (fd
, mode
)) == NULL
) {
181 lkfopenspool(const char *file
, const char *mode
)
184 int oflags
= str2accbits(mode
);
185 int failed_to_lock
= 0;
193 if ((fd
= lkopenspool(file
, oflags
, 0666, &failed_to_lock
)) == -1)
196 if ((fp
= fdopen (fd
, mode
)) == NULL
) {
206 * Corresponding close functions.
208 * A note here: All of the kernel locking functions terminate the lock
209 * when the descriptor is closed, so why write the code to explicitly
210 * unlock the file? We only need to do this in the dot-locking case.
214 lkclosedata(int fd
, const char *name
)
218 if (datalocktype
== DOT_LOCKING
)
219 lkclose_dot(fd
, name
);
225 lkfclosedata(FILE *f
, const char *name
)
235 if (datalocktype
== DOT_LOCKING
)
236 lkclose_dot(fd
, name
);
242 lkclosespool(int fd
, const char *name
)
246 if (spoollocktype
== DOT_LOCKING
)
247 lkclose_dot(fd
, name
);
253 lkfclosespool(FILE *f
, const char *name
)
263 if (spoollocktype
== DOT_LOCKING
)
264 lkclose_dot(fd
, name
);
271 * Convert fopen() mode argument to open() bits
275 str2accbits(const char *mode
)
277 if (strcmp (mode
, "r") == 0)
279 else if (strcmp (mode
, "r+") == 0)
281 else if (strcmp (mode
, "w") == 0)
282 return O_WRONLY
| O_CREAT
| O_TRUNC
;
283 else if (strcmp (mode
, "w+") == 0)
284 return O_RDWR
| O_CREAT
| O_TRUNC
;
285 else if (strcmp (mode
, "a") == 0)
286 return O_WRONLY
| O_CREAT
| O_APPEND
;
287 else if (strcmp (mode
, "a+") == 0)
288 return O_RDWR
| O_CREAT
| O_APPEND
;
296 * Internal routine to switch between different locking types.
300 lkopen (const char *file
, int access
, mode_t mode
, enum locktype ltype
,
306 return lkopen_fcntl(file
, access
, mode
, failed_to_lock
);
309 return lkopen_dot(file
, access
, mode
, failed_to_lock
);
313 return lkopen_flock(file
, access
, mode
, failed_to_lock
);
314 #endif /* HAVE_FLOCK */
318 return lkopen_lockf(file
, access
, mode
, failed_to_lock
);
319 #endif /* HAVE_FLOCK */
322 adios(NULL
, "Internal locking error: unsupported lock type used!");
330 * Routine to clean up the dot locking file
334 lkclose_dot (int fd
, const char *file
)
336 struct lockinfo lkinfo
;
338 lockname (file
, &lkinfo
, 0); /* get name of lock file */
339 #if !defined(HAVE_LIBLOCKFILE)
340 (void) m_unlink (lkinfo
.curlock
); /* remove lock file */
342 lockfile_remove(lkinfo
.curlock
);
343 #endif /* HAVE_LIBLOCKFILE */
344 timerOFF (fd
); /* turn off lock timer */
349 * Open and lock a file, using fcntl locking
353 lkopen_fcntl(const char *file
, int access
, mode_t mode
, int *failed_to_lock
)
355 int fd
, i
, saved_errno
;
359 * The assumption here is that if you open the file for writing, you
360 * need an exclusive lock.
363 for (i
= 0; i
< LOCK_RETRIES
; i
++) {
364 if ((fd
= open(file
, access
, mode
)) == -1)
369 flk
.l_type
= (access
& O_ACCMODE
) == O_RDONLY
? F_RDLCK
: F_WRLCK
;
370 flk
.l_whence
= SEEK_SET
;
372 if (fcntl(fd
, F_SETLK
, &flk
) != -1)
388 * Open and lock a file, using flock locking
392 lkopen_flock(const char *file
, int access
, mode_t mode
, int *failed_to_lock
)
394 int fd
, i
, saved_errno
, locktype
;
397 * The assumption here is that if you open the file for writing, you
398 * need an exclusive lock.
401 locktype
= (((access
& O_ACCMODE
) == O_RDONLY
) ? LOCK_SH
: LOCK_EX
) |
404 for (i
= 0; i
< LOCK_RETRIES
; i
++) {
405 if ((fd
= open(file
, access
, mode
)) == -1)
408 if (flock(fd
, locktype
) != -1)
420 #endif /* HAVE_FLOCK */
423 * Open and lock a file, using lockf locking
427 lkopen_lockf(const char *file
, int access
, mode_t mode
, int *failed_to_lock
)
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
);
476 * open and lock a file, using dot locking
480 lkopen_dot (const char *file
, int access
, mode_t mode
, int *failed_to_lock
)
483 struct lockinfo lkinfo
;
486 if ((fd
= open (file
, access
, mode
)) == -1)
490 * Get the name of the eventual lock file, as well
491 * as a name for a temporary lock file.
493 lockname (file
, &lkinfo
, 1);
495 #if !defined(HAVE_LIBLOCKFILE)
498 for (i
= 0; i
< LOCK_RETRIES
; ++i
) {
499 /* attempt to create lock file */
500 if (lockit (&lkinfo
) == 0) {
501 /* if successful, turn on timer and return */
502 timerON (lkinfo
.curlock
, fd
);
506 * Abort locking, if we fail to lock after 5 attempts
507 * and are never able to stat the lock file. Or, if
508 * we can stat the lockfile but exceed LOCK_RETRIES
509 * seconds waiting for it (by falling out of the loop).
512 if (stat (lkinfo
.curlock
, &st
) == -1) {
519 /* check for stale lockfile, else sleep */
520 if (curtime
> st
.st_ctime
+ RSECS
)
521 (void) m_unlink (lkinfo
.curlock
);
525 lockname (file
, &lkinfo
, 1);
533 if (lockfile_create(lkinfo
.curlock
, 5, 0) == L_SUCCESS
) {
534 timerON(lkinfo
.curlock
, fd
);
542 #endif /* HAVE_LIBLOCKFILE */
545 #if !defined(HAVE_LIBLOCKFILE)
547 * Routine that actually tries to create
552 lockit (struct lockinfo
*li
)
555 char *curlock
, *tmpfile
;
561 curlock
= li
->curlock
;
563 if ((tmpfile
= m_mktemp(li
->tmplock
, &fd
, NULL
)) == NULL
) {
564 advise(NULL
, "unable to create temporary file in %s", li
->tmplock
);
569 /* write our process id into lock file */
570 snprintf (buffer
, sizeof(buffer
), "nmh lock: pid %d\n", (int) getpid());
571 write(fd
, buffer
, strlen(buffer
) + 1);
577 * Now try to create the real lock file
578 * by linking to the temporary file.
580 fd
= link(tmpfile
, curlock
);
581 (void) m_unlink(tmpfile
);
583 return (fd
== -1 ? -1 : 0);
585 #endif /* HAVE_LIBLOCKFILE */
588 * Get name of lock file, and temporary lock file
592 lockname (const char *file
, struct lockinfo
*li
, int isnewlock
)
602 if ((cp
= strrchr (file
, '/')) == NULL
|| *++cp
== 0)
608 snprintf (bp
, sizeof(li
->curlock
), "%s/", lockdir
);
609 tmplen
= strlen (bp
);
614 snprintf (bp
, sizeof(li
->curlock
), "%.*s", (int)(cp
- file
), file
);
615 tmplen
= strlen (bp
);
623 * mmdf style dot locking. Currently not supported.
624 * If we start supporting mmdf style dot locking,
625 * we will need to change the return value of lockname
627 if (stat (file
, &st
) == -1)
630 snprintf (bp
, sizeof(li
->curlock
) - bplen
, "LCK%05d.%05d",
631 st
.st_dev
, st
.st_ino
);
634 snprintf (bp
, sizeof(li
->curlock
) - bplen
, "%s.lock", cp
);
636 #if !defined(HAVE_LIBLOCKFILE)
638 * If this is for a new lock, create a name for
639 * the temporary lock file for lockit()
642 if ((cp
= strrchr (li
->curlock
, '/')) == NULL
|| *++cp
== 0)
643 strncpy (li
->tmplock
, ",LCK.XXXXXX", sizeof(li
->tmplock
));
645 snprintf (li
->tmplock
, sizeof(li
->tmplock
), "%.*s,LCK.XXXXXX",
646 (int)(cp
- li
->curlock
), li
->curlock
);
653 * Add new lockfile to the list of open lockfiles
654 * and start the lock file timer.
658 timerON (char *curlock
, int fd
)
663 lp
= (struct lock
*) mh_xmalloc (sizeof(*lp
));
665 len
= strlen(curlock
) + 1;
667 lp
->l_lock
= mh_xmalloc (len
);
668 memcpy (lp
->l_lock
, curlock
, len
);
672 /* perhaps SIGT{STP,TIN,TOU} ? */
673 SIGNAL (SIGALRM
, alrmser
);
682 * Search through the list of lockfiles for the
683 * current lockfile, and remove it from the list.
689 struct lock
*pp
, *lp
;
694 for (pp
= lp
= l_top
; lp
; pp
= lp
, lp
= lp
->l_next
) {
702 pp
->l_next
= lp
->l_next
;
709 /* if there are locks left, restart timer */
716 * If timer goes off, we update the ctime of all open
717 * lockfiles, so another command doesn't remove them.
727 /* update the ctime of all the lock files */
728 for (lp
= l_top
; lp
; lp
= lp
->l_next
) {
729 lockfile
= lp
->l_lock
;
730 #if !defined(HAVE_LIBLOCKFILE)
733 if (*lockfile
&& (j
= creat (lockfile
, 0600)) != -1)
737 lockfile_touch(lockfile
);
741 /* restart the alarm */
747 * Return a locking algorithm based on the string name
751 init_locktype(const char *lockname
)
753 if (strcasecmp(lockname
, "fcntl") == 0) {
754 return FCNTL_LOCKING
;
755 } else if (strcasecmp(lockname
, "lockf") == 0) {
757 return LOCKF_LOCKING
;
758 #else /* ! HAVE_LOCKF */
759 adios(NULL
, "lockf not supported on this system");
760 #endif /* HAVE_LOCKF */
761 } else if (strcasecmp(lockname
, "flock") == 0) {
763 return FLOCK_LOCKING
;
764 #else /* ! HAVE_FLOCK */
765 adios(NULL
, "flock not supported on this system");
766 #endif /* HAVE_FLOCK */
767 } else if (strcasecmp(lockname
, "dot") == 0) {
770 adios(NULL
, "Unknown lock type: \"%s\"", lockname
);