]>
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>
22 #ifdef TIME_WITH_SYS_TIME
23 # include <sys/time.h>
26 # ifdef TM_IN_SYS_TIME
27 # include <sys/time.h>
37 # include <sys/file.h>
40 #if defined(LOCKF_LOCKING) || defined(FLOCK_LOCKING)
41 # include <sys/file.h>
46 #if defined(HAVE_LIBLOCKFILE)
51 char *lockdir
= LOCKDIR
;
54 /* Are we using any kernel locking? */
55 #if defined (FLOCK_LOCKING) || defined(LOCKF_LOCKING) || defined(FCNTL_LOCKING)
56 # define KERNEL_LOCKING
61 /* struct for getting name of lock file to create */
64 #if !defined(HAVE_LIBLOCKFILE)
70 * Amount of time to wait before
71 * updating ctime of lock file.
75 #if !defined(HAVE_LIBLOCKFILE)
77 * How old does a lock file need to be
78 * before we remove it.
81 #endif /* HAVE_LIBLOCKFILE */
83 /* struct for recording and updating locks */
90 /* top of list containing all open locks */
91 static struct lock
*l_top
= NULL
;
92 #endif /* DOT_LOCKING */
98 static int lkopen_kernel (char *, int, mode_t
);
102 static int lkopen_dot (char *, int, mode_t
);
103 static void lockname (char *, struct lockinfo
*, int);
104 static void timerON (char *, int);
105 static void timerOFF (int);
106 static RETSIGTYPE
alrmser (int);
108 #if !defined(HAVE_LIBLOCKFILE)
109 static int lockit (struct lockinfo
*);
114 * Base routine to open and lock a file,
115 * and return a file descriptor.
119 lkopen (char *file
, int access
, mode_t mode
)
121 #ifdef KERNEL_LOCKING
122 return lkopen_kernel(file
, access
, mode
);
126 return lkopen_dot(file
, access
, mode
);
132 * Base routine to close and unlock a file,
133 * given a file descriptor.
137 lkclose (int fd
, char *file
)
144 struct lockinfo lkinfo
;
151 buf
.l_type
= F_UNLCK
;
152 buf
.l_whence
= SEEK_SET
;
155 fcntl(fd
, F_SETLK
, &buf
);
163 /* make sure we unlock the whole thing */
164 lseek (fd
, (off_t
) 0, SEEK_SET
);
165 lockf (fd
, F_ULOCK
, 0L);
169 lockname (file
, &lkinfo
, 0); /* get name of lock file */
170 #if !defined(HAVE_LIBLOCKFILE)
171 unlink (lkinfo
.curlock
); /* remove lock file */
173 lockfile_remove(lkinfo
.curlock
);
174 #endif /* HAVE_LIBLOCKFILE */
175 timerOFF (fd
); /* turn off lock timer */
176 #endif /* DOT_LOCKING */
183 * Base routine to open and lock a file,
184 * and return a FILE pointer
188 lkfopen (char *file
, char *mode
)
193 if (strcmp (mode
, "r") == 0)
195 else if (strcmp (mode
, "r+") == 0)
197 else if (strcmp (mode
, "w") == 0)
198 access
= O_WRONLY
| O_CREAT
| O_TRUNC
;
199 else if (strcmp (mode
, "w+") == 0)
200 access
= O_RDWR
| O_CREAT
| O_TRUNC
;
201 else if (strcmp (mode
, "a") == 0)
202 access
= O_WRONLY
| O_CREAT
| O_APPEND
;
203 else if (strcmp (mode
, "a+") == 0)
204 access
= O_RDWR
| O_CREAT
| O_APPEND
;
210 if ((fd
= lkopen (file
, access
, 0666)) == -1)
213 if ((fp
= fdopen (fd
, mode
)) == NULL
) {
223 * Base routine to close and unlock a file,
224 * given a FILE pointer
228 lkfclose (FILE *fp
, char *file
)
235 struct lockinfo lkinfo
;
242 buf
.l_type
= F_UNLCK
;
243 buf
.l_whence
= SEEK_SET
;
246 fcntl(fileno(fp
), F_SETLK
, &buf
);
250 flock (fileno(fp
), LOCK_UN
);
254 /* make sure we unlock the whole thing */
255 fseek (fp
, 0L, SEEK_SET
);
256 lockf (fileno(fp
), F_ULOCK
, 0L);
260 lockname (file
, &lkinfo
, 0); /* get name of lock file */
261 #if !defined(HAVE_LIBLOCKFILE)
262 unlink (lkinfo
.curlock
); /* remove lock file */
264 lockfile_remove(lkinfo
.curlock
);
265 #endif /* HAVE_LIBLOCKFILE */
266 timerOFF (fileno(fp
)); /* turn off lock timer */
267 #endif /* DOT_LOCKING */
269 return (fclose (fp
));
273 #ifdef KERNEL_LOCKING
276 * open and lock a file, using kernel locking
280 lkopen_kernel (char *file
, int access
, mode_t mode
)
284 # ifdef FCNTL_LOCKING
286 # endif /* FCNTL_LOCKING */
288 for (i
= 0; i
< 5; i
++) {
290 # if defined(LOCKF_LOCKING) || defined(FCNTL_LOCKING)
291 /* remember the original mode */
294 /* make sure we open at the beginning */
298 * We MUST have write permission or
299 * lockf/fcntl() won't work
301 if ((access
& 03) == O_RDONLY
) {
305 # endif /* LOCKF_LOCKING || FCNTL_LOCKING */
307 if ((fd
= open (file
, access
| O_NDELAY
, mode
)) == -1)
310 # ifdef FCNTL_LOCKING
311 buf
.l_type
= F_WRLCK
;
312 buf
.l_whence
= SEEK_SET
;
315 if (fcntl (fd
, F_SETLK
, &buf
) != -1)
319 # ifdef FLOCK_LOCKING
320 if (flock (fd
, (((access
& 03) == O_RDONLY
) ? LOCK_SH
: LOCK_EX
)
325 # ifdef LOCKF_LOCKING
326 if (lockf (fd
, F_TLOCK
, 0L) != -1) {
327 /* see if we should be at the end */
329 lseek (fd
, (off_t
) 0, SEEK_END
);
344 #endif /* KERNEL_LOCKING */
350 * open and lock a file, using dot locking
354 lkopen_dot (char *file
, int access
, mode_t mode
)
357 struct lockinfo lkinfo
;
360 if ((fd
= open (file
, access
, mode
)) == -1)
364 * Get the name of the eventual lock file, as well
365 * as a name for a temporary lock file.
367 lockname (file
, &lkinfo
, 1);
369 #if !defined(HAVE_LIBLOCKFILE)
373 /* attempt to create lock file */
374 if (lockit (&lkinfo
) == 0) {
375 /* if successful, turn on timer and return */
376 timerON (lkinfo
.curlock
, fd
);
380 * Abort locking, if we fail to lock after 5 attempts
381 * and are never able to stat the lock file.
384 if (stat (lkinfo
.curlock
, &st
) == -1) {
393 /* check for stale lockfile, else sleep */
394 if (curtime
> st
.st_ctime
+ RSECS
)
395 unlink (lkinfo
.curlock
);
399 lockname (file
, &lkinfo
, 1);
404 if (lockfile_create(lkinfo
.curlock
, 5, 0) == L_SUCCESS
) {
405 timerON(lkinfo
.curlock
, fd
);
412 #endif /* HAVE_LIBLOCKFILE */
415 #if !defined(HAVE_LIBLOCKFILE)
417 * Routine that actually tries to create
422 lockit (struct lockinfo
*li
)
425 char *curlock
, *tmplock
;
431 curlock
= li
->curlock
;
432 tmplock
= li
->tmplock
;
434 if ((fd
= mkstemp(tmplock
)) == -1)
438 /* write our process id into lock file */
439 snprintf (buffer
, sizeof(buffer
), "nmh lock: pid %d\n", (int) getpid());
440 write(fd
, buffer
, strlen(buffer
) + 1);
446 * Now try to create the real lock file
447 * by linking to the temporary file.
449 fd
= link(tmplock
, curlock
);
452 return (fd
== -1 ? -1 : 0);
454 #endif /* HAVE_LIBLOCKFILE */
457 * Get name of lock file, and temporary lock file
461 lockname (char *file
, struct lockinfo
*li
, int isnewlock
)
470 if ((cp
= strrchr (file
, '/')) == NULL
|| *++cp
== 0)
476 snprintf (bp
, sizeof(li
->curlock
), "%s/", lockdir
);
477 tmplen
= strlen (bp
);
482 snprintf (bp
, sizeof(li
->curlock
), "%.*s", (int)(cp
- file
), file
);
483 tmplen
= strlen (bp
);
491 * mmdf style dot locking. Currently not supported.
492 * If we start supporting mmdf style dot locking,
493 * we will need to change the return value of lockname
495 if (stat (file
, &st
) == -1)
498 snprintf (bp
, sizeof(li
->curlock
) - bplen
, "LCK%05d.%05d",
499 st
.st_dev
, st
.st_ino
);
502 snprintf (bp
, sizeof(li
->curlock
) - bplen
, "%s.lock", cp
);
504 #if !defined(HAVE_LIBLOCKFILE)
506 * If this is for a new lock, create a name for
507 * the temporary lock file for lockit()
510 if ((cp
= strrchr (li
->curlock
, '/')) == NULL
|| *++cp
== 0)
511 strncpy (li
->tmplock
, ",LCK.XXXXXX", sizeof(li
->tmplock
));
513 snprintf (li
->tmplock
, sizeof(li
->tmplock
), "%.*s,LCK.XXXXXX",
514 (int)(cp
- li
->curlock
), li
->curlock
);
521 * Add new lockfile to the list of open lockfiles
522 * and start the lock file timer.
526 timerON (char *curlock
, int fd
)
531 lp
= (struct lock
*) mh_xmalloc (sizeof(*lp
));
533 len
= strlen(curlock
) + 1;
535 lp
->l_lock
= mh_xmalloc (len
);
536 memcpy (lp
->l_lock
, curlock
, len
);
540 /* perhaps SIGT{STP,TIN,TOU} ? */
541 SIGNAL (SIGALRM
, alrmser
);
550 * Search through the list of lockfiles for the
551 * current lockfile, and remove it from the list.
557 struct lock
*pp
, *lp
;
562 for (pp
= lp
= l_top
; lp
; pp
= lp
, lp
= lp
->l_next
) {
570 pp
->l_next
= lp
->l_next
;
577 /* if there are locks left, restart timer */
584 * If timer goes off, we update the ctime of all open
585 * lockfiles, so another command doesn't remove them.
594 #ifndef RELIABLE_SIGNALS
595 SIGNAL (SIGALRM
, alrmser
);
598 /* update the ctime of all the lock files */
599 for (lp
= l_top
; lp
; lp
= lp
->l_next
) {
600 lockfile
= lp
->l_lock
;
601 #if !defined(HAVE_LIBLOCKFILE)
604 if (*lockfile
&& (j
= creat (lockfile
, 0600)) != -1)
608 lockfile_touch(lockfile
);
612 /* restart the alarm */
616 #endif /* DOT_LOCKING */