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