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