]> diplodocus.org Git - nmh/blob - sbr/lock_file.c
We're not using the .Bu macro anymore.
[nmh] / sbr / lock_file.c
1
2 /*
3 * lock.c -- routines to lock/unlock files
4 *
5 * $Id$
6 *
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
10 */
11
12 /* Modified by Ruud de Rooij to support Miquel van Smoorenburg's liblockfile
13 *
14 * Since liblockfile locking shares most of its code with dot locking, it
15 * is enabled by defining both DOT_LOCKING and HAVE_LIBLOCKFILE.
16 *
17 * Ruud de Rooij <ruud@debian.org> Sun, 28 Mar 1999 15:34:03 +0200
18 */
19
20 #include <h/mh.h>
21 #include <h/signals.h>
22 #include <h/utils.h>
23
24 #ifdef TIME_WITH_SYS_TIME
25 # include <sys/time.h>
26 # include <time.h>
27 #else
28 # ifdef TM_IN_SYS_TIME
29 # include <sys/time.h>
30 # else
31 # include <time.h>
32 # endif
33 #endif
34
35 #ifdef HAVE_ERRNO_H
36 # include <errno.h>
37 #endif
38
39 #ifdef HAVE_FCNTL_H
40 # include <fcntl.h>
41 #else
42 # include <sys/file.h>
43 #endif
44
45 #if defined(LOCKF_LOCKING) || defined(FLOCK_LOCKING)
46 # include <sys/file.h>
47 #endif
48
49 #include <signal.h>
50
51 #if defined(HAVE_LIBLOCKFILE)
52 #include <lockfile.h>
53 #endif
54
55 #ifdef LOCKDIR
56 char *lockdir = LOCKDIR;
57 #endif
58
59 /* Are we using any kernel locking? */
60 #if defined (FLOCK_LOCKING) || defined(LOCKF_LOCKING) || defined(FCNTL_LOCKING)
61 # define KERNEL_LOCKING
62 #endif
63
64 #ifdef DOT_LOCKING
65
66 /* struct for getting name of lock file to create */
67 struct lockinfo {
68 char curlock[BUFSIZ];
69 #if !defined(HAVE_LIBLOCKFILE)
70 char tmplock[BUFSIZ];
71 #endif
72 };
73
74 /*
75 * Amount of time to wait before
76 * updating ctime of lock file.
77 */
78 #define NSECS 20
79
80 #if !defined(HAVE_LIBLOCKFILE)
81 /*
82 * How old does a lock file need to be
83 * before we remove it.
84 */
85 #define RSECS 180
86 #endif /* HAVE_LIBLOCKFILE */
87
88 /* struct for recording and updating locks */
89 struct lock {
90 int l_fd;
91 char *l_lock;
92 struct lock *l_next;
93 };
94
95 /* top of list containing all open locks */
96 static struct lock *l_top = NULL;
97 #endif /* DOT_LOCKING */
98
99 /*
100 * static prototypes
101 */
102 #ifdef KERNEL_LOCKING
103 static int lkopen_kernel (char *, int, mode_t);
104 #endif
105
106 #ifdef DOT_LOCKING
107 static int lkopen_dot (char *, int, mode_t);
108 static int lockit (struct lockinfo *);
109 static void lockname (char *, struct lockinfo *, int);
110 static void timerON (char *, int);
111 static void timerOFF (int);
112 static RETSIGTYPE alrmser (int);
113 #endif
114
115
116 /*
117 * Base routine to open and lock a file,
118 * and return a file descriptor.
119 */
120
121 int
122 lkopen (char *file, int access, mode_t mode)
123 {
124 #ifdef KERNEL_LOCKING
125 return lkopen_kernel(file, access, mode);
126 #endif
127
128 #ifdef DOT_LOCKING
129 return lkopen_dot(file, access, mode);
130 #endif
131 }
132
133
134 /*
135 * Base routine to close and unlock a file,
136 * given a file descriptor.
137 */
138
139 int
140 lkclose (int fd, char *file)
141 {
142 #ifdef FCNTL_LOCKING
143 struct flock buf;
144 #endif
145
146 #ifdef DOT_LOCKING
147 struct lockinfo lkinfo;
148 #endif
149
150 if (fd == -1)
151 return 0;
152
153 #ifdef FCNTL_LOCKING
154 buf.l_type = F_UNLCK;
155 buf.l_whence = SEEK_SET;
156 buf.l_start = 0;
157 buf.l_len = 0;
158 fcntl(fd, F_SETLK, &buf);
159 #endif
160
161 #ifdef FLOCK_LOCKING
162 flock (fd, LOCK_UN);
163 #endif
164
165 #ifdef LOCKF_LOCKING
166 /* make sure we unlock the whole thing */
167 lseek (fd, (off_t) 0, SEEK_SET);
168 lockf (fd, F_ULOCK, 0L);
169 #endif
170
171 #ifdef DOT_LOCKING
172 lockname (file, &lkinfo, 0); /* get name of lock file */
173 #if !defined(HAVE_LIBLOCKFILE)
174 unlink (lkinfo.curlock); /* remove lock file */
175 #else
176 lockfile_remove(lkinfo.curlock);
177 #endif /* HAVE_LIBLOCKFILE */
178 timerOFF (fd); /* turn off lock timer */
179 #endif /* DOT_LOCKING */
180
181 return (close (fd));
182 }
183
184
185 /*
186 * Base routine to open and lock a file,
187 * and return a FILE pointer
188 */
189
190 FILE *
191 lkfopen (char *file, char *mode)
192 {
193 int fd, access;
194 FILE *fp;
195
196 if (strcmp (mode, "r") == 0)
197 access = O_RDONLY;
198 else if (strcmp (mode, "r+") == 0)
199 access = O_RDWR;
200 else if (strcmp (mode, "w") == 0)
201 access = O_WRONLY | O_CREAT | O_TRUNC;
202 else if (strcmp (mode, "w+") == 0)
203 access = O_RDWR | O_CREAT | O_TRUNC;
204 else if (strcmp (mode, "a") == 0)
205 access = O_WRONLY | O_CREAT | O_APPEND;
206 else if (strcmp (mode, "a+") == 0)
207 access = O_RDWR | O_CREAT | O_APPEND;
208 else {
209 errno = EINVAL;
210 return NULL;
211 }
212
213 if ((fd = lkopen (file, access, 0666)) == -1)
214 return NULL;
215
216 if ((fp = fdopen (fd, mode)) == NULL) {
217 close (fd);
218 return NULL;
219 }
220
221 return fp;
222 }
223
224
225 /*
226 * Base routine to close and unlock a file,
227 * given a FILE pointer
228 */
229
230 int
231 lkfclose (FILE *fp, char *file)
232 {
233 #ifdef FCNTL_LOCKING
234 struct flock buf;
235 #endif
236
237 #ifdef DOT_LOCKING
238 struct lockinfo lkinfo;
239 #endif
240
241 if (fp == NULL)
242 return 0;
243
244 #ifdef FCNTL_LOCKING
245 buf.l_type = F_UNLCK;
246 buf.l_whence = SEEK_SET;
247 buf.l_start = 0;
248 buf.l_len = 0;
249 fcntl(fileno(fp), F_SETLK, &buf);
250 #endif
251
252 #ifdef FLOCK_LOCKING
253 flock (fileno(fp), LOCK_UN);
254 #endif
255
256 #ifdef LOCKF_LOCKING
257 /* make sure we unlock the whole thing */
258 fseek (fp, 0L, SEEK_SET);
259 lockf (fileno(fp), F_ULOCK, 0L);
260 #endif
261
262 #ifdef DOT_LOCKING
263 lockname (file, &lkinfo, 0); /* get name of lock file */
264 #if !defined(HAVE_LIBLOCKFILE)
265 unlink (lkinfo.curlock); /* remove lock file */
266 #else
267 lockfile_remove(lkinfo.curlock);
268 #endif /* HAVE_LIBLOCKFILE */
269 timerOFF (fileno(fp)); /* turn off lock timer */
270 #endif /* DOT_LOCKING */
271
272 return (fclose (fp));
273 }
274
275
276 #ifdef KERNEL_LOCKING
277
278 /*
279 * open and lock a file, using kernel locking
280 */
281
282 static int
283 lkopen_kernel (char *file, int access, mode_t mode)
284 {
285 int fd, i, j;
286
287 # ifdef FCNTL_LOCKING
288 struct flock buf;
289 # endif /* FCNTL_LOCKING */
290
291 for (i = 0; i < 5; i++) {
292
293 # if defined(LOCKF_LOCKING) || defined(FCNTL_LOCKING)
294 /* remember the original mode */
295 j = access;
296
297 /* make sure we open at the beginning */
298 access &= ~O_APPEND;
299
300 /*
301 * We MUST have write permission or
302 * lockf/fcntl() won't work
303 */
304 if ((access & 03) == O_RDONLY) {
305 access &= ~O_RDONLY;
306 access |= O_RDWR;
307 }
308 # endif /* LOCKF_LOCKING || FCNTL_LOCKING */
309
310 if ((fd = open (file, access | O_NDELAY, mode)) == -1)
311 return -1;
312
313 # ifdef FCNTL_LOCKING
314 buf.l_type = F_WRLCK;
315 buf.l_whence = SEEK_SET;
316 buf.l_start = 0;
317 buf.l_len = 0;
318 if (fcntl (fd, F_SETLK, &buf) != -1)
319 return fd;
320 # endif
321
322 # ifdef FLOCK_LOCKING
323 if (flock (fd, (((access & 03) == O_RDONLY) ? LOCK_SH : LOCK_EX)
324 | LOCK_NB) != -1)
325 return fd;
326 # endif
327
328 # ifdef LOCKF_LOCKING
329 if (lockf (fd, F_TLOCK, 0L) != -1) {
330 /* see if we should be at the end */
331 if (j & O_APPEND)
332 lseek (fd, (off_t) 0, SEEK_END);
333 return fd;
334 }
335 # endif
336
337 j = errno;
338 close (fd);
339 sleep (5);
340 }
341
342 close (fd);
343 errno = j;
344 return -1;
345 }
346
347 #endif /* KERNEL_LOCKING */
348
349
350 #ifdef DOT_LOCKING
351
352 /*
353 * open and lock a file, using dot locking
354 */
355
356 static int
357 lkopen_dot (char *file, int access, mode_t mode)
358 {
359 int i, fd;
360 time_t curtime;
361 struct lockinfo lkinfo;
362 struct stat st;
363
364 /* open the file */
365 if ((fd = open (file, access, mode)) == -1)
366 return -1;
367
368 /*
369 * Get the name of the eventual lock file, as well
370 * as a name for a temporary lock file.
371 */
372 lockname (file, &lkinfo, 1);
373
374 #if !defined(HAVE_LIBLOCKFILE)
375 for (i = 0;;) {
376 /* attempt to create lock file */
377 if (lockit (&lkinfo) == 0) {
378 /* if successful, turn on timer and return */
379 timerON (lkinfo.curlock, fd);
380 return fd;
381 } else {
382 /*
383 * Abort locking, if we fail to lock after 5 attempts
384 * and are never able to stat the lock file.
385 */
386 if (stat (lkinfo.curlock, &st) == -1) {
387 if (i++ > 5)
388 return -1;
389 sleep (5);
390 } else {
391 i = 0;
392 time (&curtime);
393
394 /* check for stale lockfile, else sleep */
395 if (curtime > st.st_ctime + RSECS)
396 unlink (lkinfo.curlock);
397 else
398 sleep (5);
399 }
400 lockname (file, &lkinfo, 1);
401 }
402 }
403 #else
404 if (lockfile_create(lkinfo.curlock, 5, 0) == L_SUCCESS) {
405 timerON(lkinfo.curlock, fd);
406 return fd;
407 }
408 else {
409 close(fd);
410 return -1;
411 }
412 #endif /* HAVE_LIBLOCKFILE */
413 }
414
415 #if !defined(HAVE_LIBLOCKFILE)
416 /*
417 * Routine that actually tries to create
418 * the lock file.
419 */
420
421 static int
422 lockit (struct lockinfo *li)
423 {
424 int fd;
425 char *curlock, *tmplock;
426
427 #if 0
428 char buffer[128];
429 #endif
430
431 curlock = li->curlock;
432 tmplock = li->tmplock;
433
434 #ifdef HAVE_MKSTEMP
435 if ((fd = mkstemp(tmplock)) == -1)
436 return -1;
437 #else
438 if (mktemp(tmplock) == NULL)
439 return -1;
440 if (unlink(tmplock) == -1 && errno != ENOENT)
441 return -1;
442 /* create the temporary lock file */
443 if ((fd = creat(tmplock, 0600)) == -1)
444 return -1;
445 #endif
446
447 #if 0
448 /* write our process id into lock file */
449 snprintf (buffer, sizeof(buffer), "nmh lock: pid %d\n", (int) getpid());
450 write(fd, buffer, strlen(buffer) + 1);
451 #endif
452
453 close (fd);
454
455 /*
456 * Now try to create the real lock file
457 * by linking to the temporary file.
458 */
459 fd = link(tmplock, curlock);
460 unlink(tmplock);
461
462 return (fd == -1 ? -1 : 0);
463 }
464 #endif /* HAVE_LIBLOCKFILE */
465
466 /*
467 * Get name of lock file, and temporary lock file
468 */
469
470 static void
471 lockname (char *file, struct lockinfo *li, int isnewlock)
472 {
473 int bplen, tmplen;
474 char *bp, *cp;
475
476 #if 0
477 struct stat st;
478 #endif
479
480 if ((cp = strrchr (file, '/')) == NULL || *++cp == 0)
481 cp = file;
482
483 bp = li->curlock;
484 bplen = 0;
485 #ifdef LOCKDIR
486 snprintf (bp, sizeof(li->curlock), "%s/", lockdir);
487 tmplen = strlen (bp);
488 bp += tmplen;
489 bplen += tmplen;
490 #else
491 if (cp != file) {
492 snprintf (bp, sizeof(li->curlock), "%.*s", cp - file, file);
493 tmplen = strlen (bp);
494 bp += tmplen;
495 bplen += tmplen;
496 }
497 #endif
498
499 #if 0
500 /*
501 * mmdf style dot locking. Currently not supported.
502 * If we start supporting mmdf style dot locking,
503 * we will need to change the return value of lockname
504 */
505 if (stat (file, &st) == -1)
506 return -1;
507
508 snprintf (bp, sizeof(li->curlock) - bplen, "LCK%05d.%05d",
509 st.st_dev, st.st_ino);
510 #endif
511
512 snprintf (bp, sizeof(li->curlock) - bplen, "%s.lock", cp);
513
514 #if !defined(HAVE_LIBLOCKFILE)
515 /*
516 * If this is for a new lock, create a name for
517 * the temporary lock file for lockit()
518 */
519 if (isnewlock) {
520 if ((cp = strrchr (li->curlock, '/')) == NULL || *++cp == 0)
521 strncpy (li->tmplock, ",LCK.XXXXXX", sizeof(li->tmplock));
522 else
523 snprintf (li->tmplock, sizeof(li->tmplock), "%.*s,LCK.XXXXXX",
524 cp - li->curlock, li->curlock);
525 }
526 #endif
527 }
528
529
530 /*
531 * Add new lockfile to the list of open lockfiles
532 * and start the lock file timer.
533 */
534
535 static void
536 timerON (char *curlock, int fd)
537 {
538 struct lock *lp;
539 size_t len;
540
541 lp = (struct lock *) mh_xmalloc (sizeof(*lp));
542
543 len = strlen(curlock) + 1;
544 lp->l_fd = fd;
545 lp->l_lock = mh_xmalloc (len);
546 memcpy (lp->l_lock, curlock, len);
547 lp->l_next = l_top;
548
549 if (!l_top) {
550 /* perhaps SIGT{STP,TIN,TOU} ? */
551 SIGNAL (SIGALRM, alrmser);
552 alarm (NSECS);
553 }
554
555 l_top = lp;
556 }
557
558
559 /*
560 * Search through the list of lockfiles for the
561 * current lockfile, and remove it from the list.
562 */
563
564 static void
565 timerOFF (int fd)
566 {
567 struct lock *pp, *lp;
568
569 alarm(0);
570
571 if (l_top) {
572 for (pp = lp = l_top; lp; pp = lp, lp = lp->l_next) {
573 if (lp->l_fd == fd)
574 break;
575 }
576 if (lp) {
577 if (lp == l_top)
578 l_top = lp->l_next;
579 else
580 pp->l_next = lp->l_next;
581
582 free (lp->l_lock);
583 free (lp);
584 }
585 }
586
587 /* if there are locks left, restart timer */
588 if (l_top)
589 alarm (NSECS);
590 }
591
592
593 /*
594 * If timer goes off, we update the ctime of all open
595 * lockfiles, so another command doesn't remove them.
596 */
597
598 static RETSIGTYPE
599 alrmser (int sig)
600 {
601 int j;
602 char *lockfile;
603 struct lock *lp;
604
605 #ifndef RELIABLE_SIGNALS
606 SIGNAL (SIGALRM, alrmser);
607 #endif
608
609 /* update the ctime of all the lock files */
610 for (lp = l_top; lp; lp = lp->l_next) {
611 lockfile = lp->l_lock;
612 #if !defined(HAVE_LIBLOCKFILE)
613 if (*lockfile && (j = creat (lockfile, 0600)) != -1)
614 close (j);
615 #else
616 lockfile_touch(lockfile);
617 #endif
618 }
619
620 /* restart the alarm */
621 alarm (NSECS);
622 }
623
624 #endif /* DOT_LOCKING */