]>
diplodocus.org Git - nmh/blob - sbr/lock_file.c
3 * lock.c -- routines to lock/unlock files
8 /* Modified by Ruud de Rooij to support Miquel van Smoorenburg's liblockfile
10 * Since liblockfile locking shares most of its code with dot locking, it
11 * is enabled by defining both DOT_LOCKING and HAVE_LIBLOCKFILE.
13 * Ruud de Rooij <ruud@debian.org> Sun, 28 Mar 1999 15:34:03 +0200
17 #include <h/signals.h>
19 #ifdef TIME_WITH_SYS_TIME
20 # include <sys/time.h>
23 # ifdef TM_IN_SYS_TIME
24 # include <sys/time.h>
35 # include <mmdfonly.h>
36 # include <lockonly.h>
42 # include <sys/file.h>
45 #if defined(LOCKF_LOCKING) || defined(FLOCK_LOCKING)
46 # include <sys/file.h>
51 #if defined(HAVE_LIBLOCKFILE)
58 char *lockdir
= LOCKDIR
;
61 /* Are we using any kernel locking? */
62 #if defined (FLOCK_LOCKING) || defined(LOCKF_LOCKING) || defined(FCNTL_LOCKING)
63 # define KERNEL_LOCKING
68 /* struct for getting name of lock file to create */
71 #if !defined(HAVE_LIBLOCKFILE)
77 * Amount of time to wait before
78 * updating ctime of lock file.
82 #if !defined(HAVE_LIBLOCKFILE)
84 * How old does a lock file need to be
85 * before we remove it.
88 #endif /* HAVE_LIBLOCKFILE */
90 /* struct for recording and updating locks */
97 /* top of list containing all open locks */
98 static struct lock
*l_top
= NULL
;
99 #endif /* DOT_LOCKING */
104 #ifdef KERNEL_LOCKING
105 static int lkopen_kernel (char *, int, mode_t
);
109 static int lkopen_dot (char *, int, mode_t
);
110 static int lockit (struct lockinfo
*);
111 static void lockname (char *, struct lockinfo
*, int);
112 static void timerON (char *, int);
113 static void timerOFF (int);
114 static RETSIGTYPE
alrmser (int);
119 * Base routine to open and lock a file,
120 * and return a file descriptor.
124 lkopen (char *file
, int access
, mode_t mode
)
126 #ifdef KERNEL_LOCKING
127 return lkopen_kernel(file
, access
, mode
);
131 return lkopen_dot(file
, access
, mode
);
137 * Base routine to close and unlock a file,
138 * given a file descriptor.
142 lkclose (int fd
, char *file
)
149 struct lockinfo lkinfo
;
156 buf
.l_type
= F_UNLCK
;
157 buf
.l_whence
= SEEK_SET
;
160 fcntl(fd
, F_SETLK
, &buf
);
168 /* make sure we unlock the whole thing */
169 lseek (fd
, (off_t
) 0, SEEK_SET
);
170 lockf (fd
, F_ULOCK
, 0L);
174 lockname (file
, &lkinfo
, 0); /* get name of lock file */
175 #if !defined(HAVE_LIBLOCKFILE)
176 unlink (lkinfo
.curlock
); /* remove lock file */
178 lockfile_remove(lkinfo
.curlock
);
179 #endif /* HAVE_LIBLOCKFILE */
180 timerOFF (fd
); /* turn off lock timer */
181 #endif /* DOT_LOCKING */
188 * Base routine to open and lock a file,
189 * and return a FILE pointer
193 lkfopen (char *file
, char *mode
)
198 if (strcmp (mode
, "r") == 0)
203 if ((fd
= lkopen (file
, access
, 0)) == -1)
206 if ((fp
= fdopen (fd
, mode
)) == NULL
) {
216 * Base routine to close and unlock a file,
217 * given a FILE pointer
221 lkfclose (FILE *fp
, char *file
)
228 struct lockinfo lkinfo
;
235 buf
.l_type
= F_UNLCK
;
236 buf
.l_whence
= SEEK_SET
;
239 fcntl(fileno(fp
), F_SETLK
, &buf
);
243 flock (fileno(fp
), LOCK_UN
);
247 /* make sure we unlock the whole thing */
248 fseek (fp
, 0L, SEEK_SET
);
249 lockf (fileno(fp
), F_ULOCK
, 0L);
253 lockname (file
, &lkinfo
, 0); /* get name of lock file */
254 #if !defined(HAVE_LIBLOCKFILE)
255 unlink (lkinfo
.curlock
); /* remove lock file */
257 lockfile_remove(lkinfo
.curlock
);
258 #endif /* HAVE_LIBLOCKFILE */
259 timerOFF (fileno(fp
)); /* turn off lock timer */
260 #endif /* DOT_LOCKING */
262 return (fclose (fp
));
266 #ifdef KERNEL_LOCKING
269 * open and lock a file, using kernel locking
273 lkopen_kernel (char *file
, int access
, mode_t mode
)
277 # ifdef FCNTL_LOCKING
279 # endif /* FCNTL_LOCKING */
281 for (i
= 0; i
< 5; i
++) {
283 # if defined(LOCKF_LOCKING) || defined(FCNTL_LOCKING)
284 /* remember the original mode */
287 /* make sure we open at the beginning */
291 * We MUST have write permission or
292 * lockf/fcntl() won't work
294 if ((access
& 03) == O_RDONLY
) {
298 # endif /* LOCKF_LOCKING || FCNTL_LOCKING */
300 if ((fd
= open (file
, access
| O_NDELAY
, mode
)) == -1)
303 # ifdef FCNTL_LOCKING
304 buf
.l_type
= F_WRLCK
;
305 buf
.l_whence
= SEEK_SET
;
308 if (fcntl (fd
, F_SETLK
, &buf
) != -1)
312 # ifdef FLOCK_LOCKING
313 if (flock (fd
, LOCK_EX
| LOCK_NB
) != -1)
317 # ifdef LOCKF_LOCKING
318 if (lockf (fd
, F_TLOCK
, 0L) != -1) {
319 /* see if we should be at the end */
321 lseek (fd
, (off_t
) 0, SEEK_END
);
336 #endif /* KERNEL_LOCKING */
342 * open and lock a file, using dot locking
346 lkopen_dot (char *file
, int access
, mode_t mode
)
350 struct lockinfo lkinfo
;
354 if ((fd
= open (file
, access
, mode
)) == -1)
358 * Get the name of the eventual lock file, as well
359 * as a name for a temporary lock file.
361 lockname (file
, &lkinfo
, 1);
363 #if !defined(HAVE_LIBLOCKFILE)
365 /* attempt to create lock file */
366 if (lockit (&lkinfo
) == 0) {
367 /* if successful, turn on timer and return */
368 timerON (lkinfo
.curlock
, fd
);
372 * Abort locking, if we fail to lock after 5 attempts
373 * and are never able to stat the lock file.
375 if (stat (lkinfo
.curlock
, &st
) == -1) {
383 /* check for stale lockfile, else sleep */
384 if (curtime
> st
.st_ctime
+ RSECS
)
385 unlink (lkinfo
.curlock
);
392 if (lockfile_create(lkinfo
.curlock
, 5, 0) == L_SUCCESS
) {
393 timerON(lkinfo
.curlock
, fd
);
400 #endif /* HAVE_LIBLOCKFILE */
403 #if !defined(HAVE_LIBLOCKFILE)
405 * Routine that actually tries to create
410 lockit (struct lockinfo
*li
)
413 char *curlock
, *tmplock
;
419 curlock
= li
->curlock
;
420 tmplock
= li
->tmplock
;
423 if ((fd
= mkstemp(tmplock
)) == -1)
426 if (mktemp(tmplock
) == NULL
)
428 if (unlink(tmplock
) == -1 && errno
!= ENOENT
)
430 /* create the temporary lock file */
431 if ((fd
= creat(tmplock
, 0600)) == -1)
436 /* write our process id into lock file */
437 snprintf (buffer
, sizeof(buffer
), "nmh lock: pid %d\n", (int) getpid());
438 write(fd
, buffer
, strlen(buffer
) + 1);
444 * Now try to create the real lock file
445 * by linking to the temporary file.
447 fd
= link(tmplock
, curlock
);
450 return (fd
== -1 ? -1 : 0);
452 #endif /* HAVE_LIBLOCKFILE */
455 * Get name of lock file, and temporary lock file
459 lockname (char *file
, struct lockinfo
*li
, int isnewlock
)
468 if ((cp
= strrchr (file
, '/')) == NULL
|| *++cp
== 0)
474 snprintf (bp
, sizeof(li
->curlock
), "%s/", lockdir
);
475 tmplen
= strlen (bp
);
480 snprintf (bp
, sizeof(li
->curlock
), "%.*s", cp
- file
, file
);
481 tmplen
= strlen (bp
);
489 * mmdf style dot locking. Currently not supported.
490 * If we start supporting mmdf style dot locking,
491 * we will need to change the return value of lockname
493 if (stat (file
, &st
) == -1)
496 snprintf (bp
, sizeof(li
->curlock
) - bplen
, "LCK%05d.%05d",
497 st
.st_dev
, st
.st_ino
);
500 snprintf (bp
, sizeof(li
->curlock
) - bplen
, "%s.lock", cp
);
502 #if !defined(HAVE_LIBLOCKFILE)
504 * If this is for a new lock, create a name for
505 * the temporary lock file for lockit()
508 if ((cp
= strrchr (li
->curlock
, '/')) == NULL
|| *++cp
== 0)
509 strncpy (li
->tmplock
, ",LCK.XXXXXX", sizeof(li
->tmplock
));
511 snprintf (li
->tmplock
, sizeof(li
->tmplock
), "%.*s,LCK.XXXXXX",
512 cp
- li
->curlock
, li
->curlock
);
519 * Add new lockfile to the list of open lockfiles
520 * and start the lock file timer.
524 timerON (char *curlock
, int fd
)
529 if (!(lp
= (struct lock
*) malloc (sizeof(*lp
))))
532 len
= strlen(curlock
) + 1;
534 if (!(lp
->l_lock
= malloc (len
))) {
538 memcpy (lp
->l_lock
, curlock
, len
);
542 /* perhaps SIGT{STP,TIN,TOU} ? */
543 SIGNAL (SIGALRM
, alrmser
);
552 * Search through the list of lockfiles for the
553 * current lockfile, and remove it from the list.
559 struct lock
*pp
, *lp
;
564 for (pp
= lp
= l_top
; lp
; pp
= lp
, lp
= lp
->l_next
) {
572 pp
->l_next
= lp
->l_next
;
579 /* if there are locks left, restart timer */
586 * If timer goes off, we update the ctime of all open
587 * lockfiles, so another command doesn't remove them.
597 #ifndef RELIABLE_SIGNALS
598 SIGNAL (SIGALRM
, alrmser
);
601 /* update the ctime of all the lock files */
602 for (lp
= l_top
; lp
; lp
= lp
->l_next
) {
603 lockfile
= lp
->l_lock
;
604 #if !defined(HAVE_LIBLOCKFILE)
605 if (*lockfile
&& (j
= creat (lockfile
, 0600)) != -1)
608 lockfile_touch(lockfile
);
612 /* restart the alarm */
616 #endif /* DOT_LOCKING */