Complete HAVE_MKSTEMP.
[mmh] / sbr / lock_file.c
1
2 /*
3  * lock.c -- routines to lock/unlock files
4  *
5  * $Id$
6  */
7
8 /* Modified by Ruud de Rooij to support Miquel van Smoorenburg's liblockfile
9  *
10  * Since liblockfile locking shares most of its code with dot locking, it
11  * is enabled by defining both DOT_LOCKING and HAVE_LIBLOCKFILE.
12  *
13  * Ruud de Rooij <ruud@debian.org>  Sun, 28 Mar 1999 15:34:03 +0200
14  */
15  
16 #include <h/mh.h>
17 #include <h/signals.h>
18
19 #ifdef TIME_WITH_SYS_TIME
20 # include <sys/time.h>
21 # include <time.h>
22 #else
23 # ifdef TM_IN_SYS_TIME
24 #  include <sys/time.h>
25 # else
26 #  include <time.h>
27 # endif
28 #endif
29
30 #ifdef HAVE_ERRNO_H
31 # include <errno.h>
32 #endif
33
34 #ifdef MMDFONLY
35 # include <mmdfonly.h>
36 # include <lockonly.h>
37 #endif /* MMDFONLY */
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 extern int errno;
56
57 #ifdef LOCKDIR
58 char *lockdir = LOCKDIR;
59 #endif
60
61 /* Are we using any kernel locking? */
62 #if defined (FLOCK_LOCKING) || defined(LOCKF_LOCKING) || defined(FCNTL_LOCKING)
63 # define KERNEL_LOCKING
64 #endif
65
66 #ifdef DOT_LOCKING
67
68 /* struct for getting name of lock file to create */
69 struct lockinfo {
70     char curlock[BUFSIZ];
71 #if !defined(HAVE_LIBLOCKFILE)
72     char tmplock[BUFSIZ];
73 #endif
74 };
75
76 /*
77  * Amount of time to wait before
78  * updating ctime of lock file.
79  */
80 #define NSECS 20
81
82 #if !defined(HAVE_LIBLOCKFILE)
83 /*
84  * How old does a lock file need to be
85  * before we remove it.
86  */
87 #define RSECS 180
88 #endif /* HAVE_LIBLOCKFILE */
89
90 /* struct for recording and updating locks */
91 struct lock {
92     int l_fd;
93     char *l_lock;
94     struct lock *l_next;
95 };
96
97 /* top of list containing all open locks */
98 static struct lock *l_top = NULL;
99 #endif /* DOT_LOCKING */
100
101 /*
102  * static prototypes
103  */
104 #ifdef KERNEL_LOCKING
105 static int lkopen_kernel (char *, int, mode_t);
106 #endif
107
108 #ifdef DOT_LOCKING
109 static int lkopen_dot (char *, int, mode_t);
110 static int lockit (struct lockinfo *);
111 static void lockname (char *, struct lockinfo *, int);
112 static void timerON (char *, int);
113 static void timerOFF (int);
114 static RETSIGTYPE alrmser (int);
115 #endif
116
117
118 /*
119  * Base routine to open and lock a file,
120  * and return a file descriptor.
121  */
122
123 int
124 lkopen (char *file, int access, mode_t mode)
125 {
126 #ifdef KERNEL_LOCKING
127     return lkopen_kernel(file, access, mode);
128 #endif
129
130 #ifdef DOT_LOCKING
131     return lkopen_dot(file, access, mode);
132 #endif
133 }
134
135
136 /*
137  * Base routine to close and unlock a file,
138  * given a file descriptor.
139  */
140
141 int
142 lkclose (int fd, char *file)
143 {
144 #ifdef FCNTL_LOCKING
145     struct flock buf;
146 #endif
147
148 #ifdef DOT_LOCKING
149     struct lockinfo lkinfo;
150 #endif
151
152     if (fd == -1)
153         return 0;
154
155 #ifdef FCNTL_LOCKING
156     buf.l_type   = F_UNLCK;
157     buf.l_whence = SEEK_SET;
158     buf.l_start  = 0;
159     buf.l_len    = 0;
160     fcntl(fd, F_SETLK, &buf);
161 #endif
162
163 #ifdef FLOCK_LOCKING
164     flock (fd, LOCK_UN);
165 #endif
166
167 #ifdef LOCKF_LOCKING
168     /* make sure we unlock the whole thing */
169     lseek (fd, (off_t) 0, SEEK_SET);
170     lockf (fd, F_ULOCK, 0L);
171 #endif  
172
173 #ifdef DOT_LOCKING
174     lockname (file, &lkinfo, 0);        /* get name of lock file */
175 #if !defined(HAVE_LIBLOCKFILE)
176     unlink (lkinfo.curlock);            /* remove lock file      */
177 #else
178     lockfile_remove(lkinfo.curlock);
179 #endif /* HAVE_LIBLOCKFILE */
180     timerOFF (fd);                      /* turn off lock timer   */
181 #endif /* DOT_LOCKING */
182
183     return (close (fd));
184 }
185
186
187 /*
188  * Base routine to open and lock a file,
189  * and return a FILE pointer
190  */
191
192 FILE *
193 lkfopen (char *file, char *mode)
194 {
195     int fd, access;
196     FILE *fp;
197
198     if (strcmp (mode, "r") == 0)
199         access = O_RDONLY;
200     else
201         access = O_RDWR;
202
203     if ((fd = lkopen (file, access, 0)) == -1)
204         return NULL;
205
206     if ((fp = fdopen (fd, mode)) == NULL) {
207         close (fd);
208         return NULL;
209     }
210
211     return fp;
212 }
213
214
215 /*
216  * Base routine to close and unlock a file,
217  * given a FILE pointer
218  */
219
220 int
221 lkfclose (FILE *fp, char *file)
222 {
223 #ifdef FCNTL_LOCKING
224     struct flock buf;
225 #endif
226
227 #ifdef DOT_LOCKING
228     struct lockinfo lkinfo;
229 #endif
230
231     if (fp == NULL)
232         return 0;
233
234 #ifdef FCNTL_LOCKING
235     buf.l_type   = F_UNLCK;
236     buf.l_whence = SEEK_SET;
237     buf.l_start  = 0;
238     buf.l_len    = 0;
239     fcntl(fileno(fp), F_SETLK, &buf);
240 #endif
241
242 #ifdef FLOCK_LOCKING
243     flock (fileno(fp), LOCK_UN);
244 #endif
245
246 #ifdef LOCKF_LOCKING
247     /* make sure we unlock the whole thing */
248     fseek (fp, 0L, SEEK_SET);
249     lockf (fileno(fp), F_ULOCK, 0L);
250 #endif
251
252 #ifdef DOT_LOCKING
253     lockname (file, &lkinfo, 0);        /* get name of lock file */
254 #if !defined(HAVE_LIBLOCKFILE)
255     unlink (lkinfo.curlock);            /* remove lock file      */
256 #else
257     lockfile_remove(lkinfo.curlock);
258 #endif /* HAVE_LIBLOCKFILE */
259     timerOFF (fileno(fp));              /* turn off lock timer   */
260 #endif /* DOT_LOCKING */
261
262     return (fclose (fp));
263 }
264
265
266 #ifdef KERNEL_LOCKING
267
268 /*
269  * open and lock a file, using kernel locking
270  */
271
272 static int
273 lkopen_kernel (char *file, int access, mode_t mode)
274 {
275     int fd, i, j;
276
277 # ifdef FCNTL_LOCKING
278     struct flock buf;
279 # endif /* FCNTL_LOCKING */
280
281     for (i = 0; i < 5; i++) {
282
283 # if defined(LOCKF_LOCKING) || defined(FCNTL_LOCKING)
284         /* remember the original mode */
285         j = access;
286
287         /* make sure we open at the beginning */
288         access &= ~O_APPEND;
289
290         /*
291          * We MUST have write permission or
292          * lockf/fcntl() won't work
293          */
294         if ((access & 03) == O_RDONLY) {
295             access &= ~O_RDONLY;
296             access |= O_RDWR;
297         }
298 # endif /* LOCKF_LOCKING || FCNTL_LOCKING */
299
300         if ((fd = open (file, access | O_NDELAY, mode)) == -1)
301             return -1;
302
303 # ifdef FCNTL_LOCKING
304         buf.l_type   = F_WRLCK;
305         buf.l_whence = SEEK_SET;
306         buf.l_start  = 0;
307         buf.l_len    = 0;
308         if (fcntl (fd, F_SETLK, &buf) != -1)
309             return fd;
310 # endif
311
312 # ifdef FLOCK_LOCKING
313         if (flock (fd, LOCK_EX | LOCK_NB) != -1)
314             return fd;
315 # endif
316
317 # ifdef LOCKF_LOCKING
318         if (lockf (fd, F_TLOCK, 0L) != -1) {
319             /* see if we should be at the end */
320             if (j & O_APPEND)
321                 lseek (fd, (off_t) 0, SEEK_END);
322             return fd;
323         }
324 # endif
325
326         j = errno;
327         close (fd);
328         sleep (5);
329     }
330
331     close (fd);
332     errno = j;
333     return -1;
334 }
335
336 #endif /* KERNEL_LOCKING */
337
338
339 #ifdef DOT_LOCKING
340
341 /*
342  * open and lock a file, using dot locking
343  */
344
345 static int
346 lkopen_dot (char *file, int access, mode_t mode)
347 {
348     int i, fd;
349     time_t curtime;
350     struct lockinfo lkinfo;
351     struct stat st;
352
353     /* open the file */
354     if ((fd = open (file, access, mode)) == -1)
355         return -1;
356
357     /*
358      * Get the name of the eventual lock file, as well
359      * as a name for a temporary lock file.
360      */
361     lockname (file, &lkinfo, 1);
362
363 #if !defined(HAVE_LIBLOCKFILE)
364     for (i = 0;;) {
365         /* attempt to create lock file */
366         if (lockit (&lkinfo) == 0) {
367             /* if successful, turn on timer and return */
368             timerON (lkinfo.curlock, fd);
369             return fd;
370         } else {
371             /*
372              * Abort locking, if we fail to lock after 5 attempts
373              * and are never able to stat the lock file.
374              */
375             if (stat (lkinfo.curlock, &st) == -1) {
376                 if (i++ > 5)
377                     return -1;
378                 sleep (5);
379             } else {
380                 i = 0;
381                 time (&curtime);
382
383                 /* check for stale lockfile, else sleep */
384                 if (curtime > st.st_ctime + RSECS)
385                     unlink (lkinfo.curlock);
386                 else
387                     sleep (5);
388             }
389         }
390     }
391 #else
392     if (lockfile_create(lkinfo.curlock, 5, 0) == L_SUCCESS) {
393         timerON(lkinfo.curlock, fd);
394         return fd;
395     }
396     else {
397         close(fd);
398         return -1;
399     }
400 #endif /* HAVE_LIBLOCKFILE */
401 }
402
403 #if !defined(HAVE_LIBLOCKFILE)
404 /*
405  * Routine that actually tries to create
406  * the lock file.
407  */
408
409 static int
410 lockit (struct lockinfo *li)
411 {
412     int fd;
413     char *curlock, *tmplock;
414
415 #if 0
416     char buffer[128];
417 #endif
418
419     curlock = li->curlock;
420     tmplock = li->tmplock;
421
422 #ifdef HAVE_MKSTEMP
423     if ((fd = mkstemp(tmplock)) == -1)
424         return -1;
425 #else
426     if (mktemp(tmplock) == NULL)
427         return -1;
428     if (unlink(tmplock) == -1 && errno != ENOENT)
429         return -1;
430     /* create the temporary lock file */
431     if ((fd = creat(tmplock, 0600)) == -1)
432         return -1;
433 #endif
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", 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                      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     if (!(lp = (struct lock *) malloc (sizeof(*lp))))
530         return;
531
532     len = strlen(curlock) + 1;
533     lp->l_fd = fd;
534     if (!(lp->l_lock = malloc (len))) {
535         free ((char *) lp);
536         return;
537     }
538     memcpy (lp->l_lock, curlock, len);
539     lp->l_next = l_top;
540
541     if (!l_top) {
542         /* perhaps SIGT{STP,TIN,TOU} ? */
543         SIGNAL (SIGALRM, alrmser);
544         alarm (NSECS);
545     }
546
547     l_top = lp;
548 }
549
550
551 /*
552  * Search through the list of lockfiles for the
553  * current lockfile, and remove it from the list.
554  */
555
556 static void
557 timerOFF (int fd)
558 {
559     struct lock *pp, *lp;
560
561     alarm(0);
562
563     if (l_top) {
564         for (pp = lp = l_top; lp; pp = lp, lp = lp->l_next) {
565             if (lp->l_fd == fd)
566                 break;
567         }
568         if (lp) {
569             if (lp == l_top)
570                 l_top = lp->l_next;
571             else
572                 pp->l_next = lp->l_next;
573
574             free (lp->l_lock);
575             free (lp);
576         }
577     }
578
579     /* if there are locks left, restart timer */
580     if (l_top)
581         alarm (NSECS);
582 }
583
584
585 /*
586  * If timer goes off, we update the ctime of all open
587  * lockfiles, so another command doesn't remove them.
588  */
589
590 static RETSIGTYPE
591 alrmser (int sig)
592 {
593     int j;
594     char *lockfile;
595     struct lock *lp;
596
597 #ifndef RELIABLE_SIGNALS
598     SIGNAL (SIGALRM, alrmser);
599 #endif
600
601     /* update the ctime of all the lock files */
602     for (lp = l_top; lp; lp = lp->l_next) {
603         lockfile = lp->l_lock;
604 #if !defined(HAVE_LIBLOCKFILE)
605         if (*lockfile && (j = creat (lockfile, 0600)) != -1)
606             close (j);
607 #else
608     lockfile_touch(lockfile);
609 #endif
610     }
611
612     /* restart the alarm */
613     alarm (NSECS);
614 }
615
616 #endif /* DOT_LOCKING */