]>
diplodocus.org Git - nmh/blob - sbr/lock_file.c
3 * lock.c -- routines to lock/unlock files
11 #ifdef TIME_WITH_SYS_TIME
12 # include <sys/time.h>
15 # ifdef TM_IN_SYS_TIME
16 # include <sys/time.h>
27 # include <mmdfonly.h>
28 # include <lockonly.h>
34 # include <sys/file.h>
37 #if defined(LOCKF_LOCKING) || defined(FLOCK_LOCKING)
38 # include <sys/file.h>
46 char *lockdir
= LOCKDIR
;
49 /* Are we using any kernel locking? */
50 #if defined (FLOCK_LOCKING) || defined(LOCKF_LOCKING) || defined(FCNTL_LOCKING)
51 # define KERNEL_LOCKING
56 /* struct for getting name of lock file to create */
63 * Amount of time to wait before
64 * updating ctime of lock file.
69 * How old does a lock file need to be
70 * before we remove it.
74 /* struct for recording and updating locks */
81 /* top of list containing all open locks */
82 static struct lock
*l_top
= NULL
;
83 #endif /* DOT_LOCKING */
89 static int lkopen_kernel (char *, int, mode_t
);
93 static int lkopen_dot (char *, int, mode_t
);
94 static int lockit (struct lockinfo
*);
95 static void lockname (char *, struct lockinfo
*, int);
96 static void timerON (char *, int);
97 static void timerOFF (int);
98 static RETSIGTYPE
alrmser (int);
103 * Base routine to open and lock a file,
104 * and return a file descriptor.
108 lkopen (char *file
, int access
, mode_t mode
)
110 #ifdef KERNEL_LOCKING
111 return lkopen_kernel(file
, access
, mode
);
115 return lkopen_dot(file
, access
, mode
);
121 * Base routine to close and unlock a file,
122 * given a file descriptor.
126 lkclose (int fd
, char *file
)
133 struct lockinfo lkinfo
;
140 buf
.l_type
= F_UNLCK
;
141 buf
.l_whence
= SEEK_SET
;
144 fcntl(fd
, F_SETLK
, &buf
);
152 /* make sure we unlock the whole thing */
153 lseek (fd
, (off_t
) 0, SEEK_SET
);
154 lockf (fd
, F_ULOCK
, 0L);
158 lockname (file
, &lkinfo
, 0); /* get name of lock file */
159 unlink (lkinfo
.curlock
); /* remove lock file */
160 timerOFF (fd
); /* turn off lock timer */
168 * Base routine to open and lock a file,
169 * and return a FILE pointer
173 lkfopen (char *file
, char *mode
)
178 if (strcmp (mode
, "r") == 0)
183 if ((fd
= lkopen (file
, access
, 0)) == -1)
186 if ((fp
= fdopen (fd
, mode
)) == NULL
) {
196 * Base routine to close and unlock a file,
197 * given a FILE pointer
201 lkfclose (FILE *fp
, char *file
)
208 struct lockinfo lkinfo
;
215 buf
.l_type
= F_UNLCK
;
216 buf
.l_whence
= SEEK_SET
;
219 fcntl(fileno(fp
), F_SETLK
, &buf
);
223 flock (fileno(fp
), LOCK_UN
);
227 /* make sure we unlock the whole thing */
228 fseek (fp
, 0L, SEEK_SET
);
229 lockf (fileno(fp
), F_ULOCK
, 0L);
233 lockname (file
, &lkinfo
, 0); /* get name of lock file */
234 unlink (lkinfo
.curlock
); /* remove lock file */
235 timerOFF (fileno(fp
)); /* turn off lock timer */
238 return (fclose (fp
));
242 #ifdef KERNEL_LOCKING
245 * open and lock a file, using kernel locking
249 lkopen_kernel (char *file
, int access
, mode_t mode
)
253 # ifdef FCNTL_LOCKING
255 # endif /* FCNTL_LOCKING */
257 for (i
= 0; i
< 5; i
++) {
259 # if defined(LOCKF_LOCKING) || defined(FCNTL_LOCKING)
260 /* remember the original mode */
263 /* make sure we open at the beginning */
267 * We MUST have write permission or
268 * lockf/fcntl() won't work
270 if ((access
& 03) == O_RDONLY
) {
274 # endif /* LOCKF_LOCKING || FCNTL_LOCKING */
276 if ((fd
= open (file
, access
| O_NDELAY
, mode
)) == -1)
279 # ifdef FCNTL_LOCKING
280 buf
.l_type
= F_WRLCK
;
281 buf
.l_whence
= SEEK_SET
;
284 if (fcntl (fd
, F_SETLK
, &buf
) != -1)
288 # ifdef FLOCK_LOCKING
289 if (flock (fd
, LOCK_EX
| LOCK_NB
) != -1)
293 # ifdef LOCKF_LOCKING
294 if (lockf (fd
, F_TLOCK
, 0L) != -1) {
295 /* see if we should be at the end */
297 lseek (fd
, (off_t
) 0, SEEK_END
);
312 #endif /* KERNEL_LOCKING */
318 * open and lock a file, using dot locking
322 lkopen_dot (char *file
, int access
, mode_t mode
)
326 struct lockinfo lkinfo
;
330 if ((fd
= open (file
, access
, mode
)) == -1)
334 * Get the name of the eventual lock file, as well
335 * as a name for a temporary lock file.
337 lockname (file
, &lkinfo
, 1);
340 /* attempt to create lock file */
341 if (lockit (&lkinfo
) == 0) {
342 /* if successful, turn on timer and return */
343 timerON (lkinfo
.curlock
, fd
);
347 * Abort locking, if we fail to lock after 5 attempts
348 * and are never able to stat the lock file.
350 if (stat (lkinfo
.curlock
, &st
) == -1) {
358 /* check for stale lockfile, else sleep */
359 if (curtime
> st
.st_ctime
+ RSECS
)
360 unlink (lkinfo
.curlock
);
369 * Routine that actually tries to create
374 lockit (struct lockinfo
*li
)
377 char *curlock
, *tmplock
;
383 curlock
= li
->curlock
;
384 tmplock
= li
->tmplock
;
386 /* create the temporary lock file */
387 if ((fd
= creat(tmplock
, 0600)) == -1)
391 /* write our process id into lock file */
392 snprintf (buffer
, sizeof(buffer
), "nmh lock: pid %d\n", (int) getpid());
393 write(fd
, buffer
, strlen(buffer
) + 1);
399 * Now try to create the real lock file
400 * by linking to the temporary file.
402 fd
= link(tmplock
, curlock
);
405 return (fd
== -1 ? -1 : 0);
409 * Get name of lock file, and temporary lock file
413 lockname (char *file
, struct lockinfo
*li
, int isnewlock
)
422 if ((cp
= strrchr (file
, '/')) == NULL
|| *++cp
== 0)
428 snprintf (bp
, sizeof(li
->curlock
), "%s/", lockdir
);
429 tmplen
= strlen (bp
);
434 snprintf (bp
, sizeof(li
->curlock
), "%.*s", cp
- file
, file
);
435 tmplen
= strlen (bp
);
443 * mmdf style dot locking. Currently not supported.
444 * If we start supporting mmdf style dot locking,
445 * we will need to change the return value of lockname
447 if (stat (file
, &st
) == -1)
450 snprintf (bp
, sizeof(li
->curlock
) - bplen
, "LCK%05d.%05d",
451 st
.st_dev
, st
.st_ino
);
454 snprintf (bp
, sizeof(li
->curlock
) - bplen
, "%s.lock", cp
);
457 * If this is for a new lock, create a name for
458 * the temporary lock file for lockit()
461 if ((cp
= strrchr (li
->curlock
, '/')) == NULL
|| *++cp
== 0)
462 strncpy (li
->tmplock
, ",LCK.XXXXXX", sizeof(li
->tmplock
));
464 snprintf (li
->tmplock
, sizeof(li
->tmplock
), "%.*s,LCK.XXXXXX",
465 cp
- li
->curlock
, li
->curlock
);
467 Mkstemp work postponed until later -Doug
469 mkstemp (li->tmplock);
472 mktemp (li
->tmplock
);
477 unlink (li
->tmplock
); /* remove any stray */
483 * Add new lockfile to the list of open lockfiles
484 * and start the lock file timer.
488 timerON (char *curlock
, int fd
)
493 if (!(lp
= (struct lock
*) malloc (sizeof(*lp
))))
496 len
= strlen(curlock
) + 1;
498 if (!(lp
->l_lock
= malloc (len
))) {
502 memcpy (lp
->l_lock
, curlock
, len
);
506 /* perhaps SIGT{STP,TIN,TOU} ? */
507 SIGNAL (SIGALRM
, alrmser
);
516 * Search through the list of lockfiles for the
517 * current lockfile, and remove it from the list.
523 struct lock
*pp
, *lp
;
528 for (pp
= lp
= l_top
; lp
; pp
= lp
, lp
= lp
->l_next
) {
536 pp
->l_next
= lp
->l_next
;
543 /* if there are locks left, restart timer */
550 * If timer goes off, we update the ctime of all open
551 * lockfiles, so another command doesn't remove them.
561 #ifndef RELIABLE_SIGNALS
562 SIGNAL (SIGALRM
, alrmser
);
565 /* update the ctime of all the lock files */
566 for (lp
= l_top
; lp
; lp
= lp
->l_next
) {
567 lockfile
= lp
->l_lock
;
568 if (*lockfile
&& (j
= creat (lockfile
, 0600)) != -1)
572 /* restart the alarm */
576 #endif /* DOT_LOCKING */