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