]>
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>
35 #if defined(HAVE_LIBLOCKFILE)
40 char *lockdir
= LOCKDIR
;
43 /* struct for getting name of lock file to create */
46 #if !defined(HAVE_LIBLOCKFILE)
52 * Number of tries to retry locking
54 #define LOCK_RETRIES 5
57 * Amount of time to wait before
58 * updating ctime of lock file.
62 #if !defined(HAVE_LIBLOCKFILE)
64 * How old does a lock file need to be
65 * before we remove it.
68 #endif /* HAVE_LIBLOCKFILE */
70 /* struct for recording and updating locks */
77 enum locktype
{ FCNTL_LOCKING
, FLOCK_LOCKING
, LOCKF_LOCKING
, DOT_LOCKING
};
80 * Flags to indicate whether we've initialized the lock types, and
81 * our saved lock types
83 static int datalockinit
= 0;
84 static int spoollockinit
= 0;
85 static enum locktype datalocktype
, spoollocktype
;
88 /* top of list containing all open locks */
89 static struct lock
*l_top
= NULL
;
91 static int lkopen(const char *, int, mode_t
, enum locktype
);
92 static int str2accbits(const char *);
94 static int lkopen_fcntl (const char *, int, mode_t
);
96 static int lkopen_lockf (const char *, int, mode_t
);
97 #endif /* HAVE_LOCKF */
99 static int lkopen_flock (const char *, int, mode_t
);
100 #endif /* HAVE_FLOCK */
102 static enum locktype
init_locktype(const char *);
104 static int lkopen_dot (const char *, int, mode_t
);
105 static void lkclose_dot (int, const char *);
106 static void lockname (const char *, struct lockinfo
*, int);
107 static void timerON (char *, int);
108 static void timerOFF (int);
109 static void alrmser (int);
111 #if !defined(HAVE_LIBLOCKFILE)
112 static int lockit (struct lockinfo
*);
116 * Base functions: determine the data type used to lock files and
117 * call the underlying function.
121 lkopendata(const char *file
, int access
, mode_t mode
)
123 if (! datalockinit
) {
124 char *cp
= context_find("datalocking");
127 datalocktype
= init_locktype(cp
);
129 /* We default to fcntl locking for data files */
130 datalocktype
= FCNTL_LOCKING
;
136 return lkopen(file
, access
, mode
, datalocktype
);
141 * Locking using the spool locking algorithm
144 int lkopenspool(const char *file
, int access
, mode_t mode
)
146 if (! spoollockinit
) {
147 spoollocktype
= init_locktype(spoollocking
);
152 return lkopen(file
, access
, mode
, spoollocktype
);
157 * Versions of lkopen that return a FILE *
161 lkfopendata(const char *file
, const char *mode
)
164 int oflags
= str2accbits(mode
);
172 if ((fd
= lkopendata(file
, oflags
, 0666)) == -1)
175 if ((fp
= fdopen (fd
, mode
)) == NULL
) {
184 lkfopenspool(const char *file
, const char *mode
)
187 int oflags
= str2accbits(mode
);
195 if ((fd
= lkopenspool(file
, oflags
, 0666)) == -1)
198 if ((fp
= fdopen (fd
, mode
)) == NULL
) {
208 * Corresponding close functions.
210 * A note here: All of the kernel locking functions terminate the lock
211 * when the descriptor is closed, so why write the code to explicitly
212 * unlock the file? We only need to do this in the dot-locking case.
216 lkclosedata(int fd
, const char *name
)
220 if (datalocktype
== DOT_LOCKING
)
221 lkclose_dot(fd
, name
);
227 lkfclosedata(FILE *f
, const char *name
)
237 if (datalocktype
== DOT_LOCKING
)
238 lkclose_dot(fd
, name
);
244 lkclosespool(int fd
, const char *name
)
248 if (spoollocktype
== DOT_LOCKING
)
249 lkclose_dot(fd
, name
);
255 lkfclosespool(FILE *f
, const char *name
)
265 if (spoollocktype
== DOT_LOCKING
)
266 lkclose_dot(fd
, name
);
273 * Convert fopen() mode argument to open() bits
277 str2accbits(const char *mode
)
279 if (strcmp (mode
, "r") == 0)
281 else if (strcmp (mode
, "r+") == 0)
283 else if (strcmp (mode
, "w") == 0)
284 return O_WRONLY
| O_CREAT
| O_TRUNC
;
285 else if (strcmp (mode
, "w+") == 0)
286 return O_RDWR
| O_CREAT
| O_TRUNC
;
287 else if (strcmp (mode
, "a") == 0)
288 return O_WRONLY
| O_CREAT
| O_APPEND
;
289 else if (strcmp (mode
, "a+") == 0)
290 return O_RDWR
| O_CREAT
| O_APPEND
;
298 * Internal routine to switch between different locking types.
302 lkopen (const char *file
, int access
, mode_t mode
, enum locktype ltype
)
307 return lkopen_fcntl(file
, access
, mode
);
310 return lkopen_dot(file
, access
, mode
);
314 return lkopen_flock(file
, access
, mode
);
315 #endif /* HAVE_FLOCK */
319 return lkopen_lockf(file
, access
, mode
);
320 #endif /* HAVE_FLOCK */
323 adios(NULL
, "Internal locking error: unsupported lock type used!");
331 * Routine to clean up the dot locking file
335 lkclose_dot (int fd
, const char *file
)
337 struct lockinfo lkinfo
;
339 lockname (file
, &lkinfo
, 0); /* get name of lock file */
340 #if !defined(HAVE_LIBLOCKFILE)
341 unlink (lkinfo
.curlock
); /* remove lock file */
343 lockfile_remove(lkinfo
.curlock
);
344 #endif /* HAVE_LIBLOCKFILE */
345 timerOFF (fd
); /* turn off lock timer */
350 * Open and lock a file, using fcntl locking
354 lkopen_fcntl(const char *file
, int access
, mode_t mode
)
356 int fd
, i
, saved_errno
;
360 * The assumption here is that if you open the file for writing, you
361 * need an exclusive lock.
364 for (i
= 0; i
< LOCK_RETRIES
; i
++) {
365 if ((fd
= open(file
, access
, mode
)) == -1)
370 flk
.l_type
= (access
& O_ACCMODE
) == O_RDONLY
? F_RDLCK
: F_WRLCK
;
371 flk
.l_whence
= SEEK_SET
;
373 if (fcntl(fd
, F_SETLK
, &flk
) != -1)
387 * Open and lock a file, using flock locking
391 lkopen_flock(const char *file
, int access
, mode_t mode
)
393 int fd
, i
, saved_errno
, locktype
;
396 * The assumption here is that if you open the file for writing, you
397 * need an exclusive lock.
400 locktype
= (((access
& O_ACCMODE
) == O_RDONLY
) ? LOCK_SH
: LOCK_EX
) |
403 for (i
= 0; i
< LOCK_RETRIES
; i
++) {
404 if ((fd
= open(file
, access
, mode
)) == -1)
407 if (flock(fd
, locktype
) != -1)
421 * Open and lock a file, using lockf locking
425 lkopen_lockf(const char *file
, int access
, mode_t mode
)
427 int fd
, i
, saved_errno
, saved_access
;
432 * Because lockf locks start from the current offset, mask off O_APPEND
433 * and seek to the end of the file later if it was requested.
435 * lockf locks require write access to the file, so always add it
436 * even if it wasn't requested.
439 saved_access
= access
;
443 if ((access
& O_ACCMODE
) == O_RDONLY
) {
448 for (i
= 0; i
< LOCK_RETRIES
; i
++) {
449 if ((fd
= open(file
, access
, mode
)) == -1)
452 if (lockf(fd
, F_TLOCK
, 0) != -1) {
454 * Seek to end if requested
456 if (saved_access
& O_APPEND
) {
457 lseek(fd
, 0, SEEK_END
);
473 * open and lock a file, using dot locking
477 lkopen_dot (const char *file
, int access
, mode_t mode
)
480 struct lockinfo lkinfo
;
483 if ((fd
= open (file
, access
, mode
)) == -1)
487 * Get the name of the eventual lock file, as well
488 * as a name for a temporary lock file.
490 lockname (file
, &lkinfo
, 1);
492 #if !defined(HAVE_LIBLOCKFILE)
496 /* attempt to create lock file */
497 if (lockit (&lkinfo
) == 0) {
498 /* if successful, turn on timer and return */
499 timerON (lkinfo
.curlock
, fd
);
503 * Abort locking, if we fail to lock after 5 attempts
504 * and are never able to stat the lock file.
507 if (stat (lkinfo
.curlock
, &st
) == -1) {
516 /* check for stale lockfile, else sleep */
517 if (curtime
> st
.st_ctime
+ RSECS
)
518 unlink (lkinfo
.curlock
);
522 lockname (file
, &lkinfo
, 1);
527 if (lockfile_create(lkinfo
.curlock
, 5, 0) == L_SUCCESS
) {
528 timerON(lkinfo
.curlock
, fd
);
535 #endif /* HAVE_LIBLOCKFILE */
538 #if !defined(HAVE_LIBLOCKFILE)
540 * Routine that actually tries to create
545 lockit (struct lockinfo
*li
)
548 char *curlock
, *tmplock
;
554 curlock
= li
->curlock
;
555 tmplock
= li
->tmplock
;
557 if ((fd
= mkstemp(tmplock
)) == -1)
561 /* write our process id into lock file */
562 snprintf (buffer
, sizeof(buffer
), "nmh lock: pid %d\n", (int) getpid());
563 write(fd
, buffer
, strlen(buffer
) + 1);
569 * Now try to create the real lock file
570 * by linking to the temporary file.
572 fd
= link(tmplock
, curlock
);
575 return (fd
== -1 ? -1 : 0);
577 #endif /* HAVE_LIBLOCKFILE */
580 * Get name of lock file, and temporary lock file
584 lockname (const char *file
, struct lockinfo
*li
, int isnewlock
)
594 if ((cp
= strrchr (file
, '/')) == NULL
|| *++cp
== 0)
600 snprintf (bp
, sizeof(li
->curlock
), "%s/", lockdir
);
601 tmplen
= strlen (bp
);
606 snprintf (bp
, sizeof(li
->curlock
), "%.*s", (int)(cp
- file
), file
);
607 tmplen
= strlen (bp
);
615 * mmdf style dot locking. Currently not supported.
616 * If we start supporting mmdf style dot locking,
617 * we will need to change the return value of lockname
619 if (stat (file
, &st
) == -1)
622 snprintf (bp
, sizeof(li
->curlock
) - bplen
, "LCK%05d.%05d",
623 st
.st_dev
, st
.st_ino
);
626 snprintf (bp
, sizeof(li
->curlock
) - bplen
, "%s.lock", cp
);
628 #if !defined(HAVE_LIBLOCKFILE)
630 * If this is for a new lock, create a name for
631 * the temporary lock file for lockit()
634 if ((cp
= strrchr (li
->curlock
, '/')) == NULL
|| *++cp
== 0)
635 strncpy (li
->tmplock
, ",LCK.XXXXXX", sizeof(li
->tmplock
));
637 snprintf (li
->tmplock
, sizeof(li
->tmplock
), "%.*s,LCK.XXXXXX",
638 (int)(cp
- li
->curlock
), li
->curlock
);
645 * Add new lockfile to the list of open lockfiles
646 * and start the lock file timer.
650 timerON (char *curlock
, int fd
)
655 lp
= (struct lock
*) mh_xmalloc (sizeof(*lp
));
657 len
= strlen(curlock
) + 1;
659 lp
->l_lock
= mh_xmalloc (len
);
660 memcpy (lp
->l_lock
, curlock
, len
);
664 /* perhaps SIGT{STP,TIN,TOU} ? */
665 SIGNAL (SIGALRM
, alrmser
);
674 * Search through the list of lockfiles for the
675 * current lockfile, and remove it from the list.
681 struct lock
*pp
, *lp
;
686 for (pp
= lp
= l_top
; lp
; pp
= lp
, lp
= lp
->l_next
) {
694 pp
->l_next
= lp
->l_next
;
701 /* if there are locks left, restart timer */
708 * If timer goes off, we update the ctime of all open
709 * lockfiles, so another command doesn't remove them.
719 /* update the ctime of all the lock files */
720 for (lp
= l_top
; lp
; lp
= lp
->l_next
) {
721 lockfile
= lp
->l_lock
;
722 #if !defined(HAVE_LIBLOCKFILE)
725 if (*lockfile
&& (j
= creat (lockfile
, 0600)) != -1)
729 lockfile_touch(lockfile
);
733 /* restart the alarm */
739 * Return a locking algorithm based on the string name
743 init_locktype(const char *lockname
)
745 if (strcasecmp(lockname
, "fcntl") == 0) {
746 return FCNTL_LOCKING
;
747 } else if (strcasecmp(lockname
, "lockf") == 0) {
749 return LOCKF_LOCKING
;
750 #else /* ! HAVE_LOCKF */
751 adios(NULL
, "lockf not supported on this system");
752 #endif /* HAVE_LOCKF */
753 } else if (strcasecmp(lockname
, "flock") == 0) {
755 return FLOCK_LOCKING
;
756 #else /* ! HAVE_FLOCK */
757 adios(NULL
, "flock not supported on this system");
758 #endif /* HAVE_FLOCK */
759 } else if (strcasecmp(lockname
, "dot") == 0) {
762 adios(NULL
, "Unknown lock type: \"%s\"", lockname
);