Fixed the error message in lockit() when it fails to create tmp file.
[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                 advise(NULL, "unable to create temporary file in %s", tmplock);
434                 return -1;
435         }
436
437 #if 0
438         /* write our process id into lock file */
439         snprintf(buffer, sizeof(buffer), "nmh lock: pid %d\n", (int) getpid());
440         write(fd, buffer, strlen(buffer) + 1);
441 #endif
442
443         close(fd);
444
445         /*
446         ** Now try to create the real lock file
447         ** by linking to the temporary file.
448         */
449         fd = link(tmplock, curlock);
450         unlink(tmplock);
451
452         return (fd == -1 ? -1 : 0);
453 }
454 #endif /* HAVE_LIBLOCKFILE */
455
456 /*
457 ** Get name of lock file, and temporary lock file
458 */
459
460 static void
461 lockname(char *file, struct lockinfo *li, int isnewlock)
462 {
463         int bplen, tmplen;
464         char *bp, *cp;
465
466         if ((cp = strrchr(file, '/')) == NULL || *++cp == 0)
467                 cp = file;
468
469         bp = li->curlock;
470         bplen = 0;
471 #ifdef LOCKDIR
472         snprintf(bp, sizeof(li->curlock), "%s/", lockdir);
473         tmplen = strlen(bp);
474         bp += tmplen;
475         bplen += tmplen;
476 #else
477         if (cp != file) {
478                 snprintf(bp, sizeof(li->curlock), "%.*s", (int)(cp - file), file);
479                 tmplen = strlen(bp);
480                 bp += tmplen;
481                 bplen += tmplen;
482         }
483 #endif
484
485         snprintf(bp, sizeof(li->curlock) - bplen, "%s.lock", cp);
486
487 #if !defined(HAVE_LIBLOCKFILE)
488         /*
489         ** If this is for a new lock, create a name for
490         ** the temporary lock file for lockit()
491         */
492         if (isnewlock) {
493                 if ((cp = strrchr(li->curlock, '/')) == NULL || *++cp == 0)
494                         strncpy(li->tmplock, ",LCK.XXXXXX", sizeof(li->tmplock));
495                 else
496                         snprintf(li->tmplock, sizeof(li->tmplock),
497                                         "%.*s,LCK.XXXXXX",
498                                         (int)(cp - li->curlock), li->curlock);
499         }
500 #endif
501 }
502
503
504 /*
505 ** Add new lockfile to the list of open lockfiles
506 ** and start the lock file timer.
507 */
508
509 static void
510 timerON(char *curlock, int fd)
511 {
512         struct lock *lp;
513         size_t len;
514
515         lp = (struct lock *) mh_xmalloc(sizeof(*lp));
516
517         len = strlen(curlock) + 1;
518         lp->l_fd = fd;
519         lp->l_lock = mh_xmalloc(len);
520         memcpy(lp->l_lock, curlock, len);
521         lp->l_next = l_top;
522
523         if (!l_top) {
524                 /* perhaps SIGT{STP,TIN,TOU} ? */
525                 SIGNAL(SIGALRM, alrmser);
526                 alarm(NSECS);
527         }
528
529         l_top = lp;
530 }
531
532
533 /*
534 ** Search through the list of lockfiles for the
535 ** current lockfile, and remove it from the list.
536 */
537
538 static void
539 timerOFF(int fd)
540 {
541         struct lock *pp, *lp;
542
543         alarm(0);
544
545         if (l_top) {
546                 for (pp = lp = l_top; lp; pp = lp, lp = lp->l_next) {
547                         if (lp->l_fd == fd)
548                                 break;
549                 }
550                 if (lp) {
551                         if (lp == l_top)
552                                 l_top = lp->l_next;
553                         else
554                                 pp->l_next = lp->l_next;
555
556                         free(lp->l_lock);
557                         free(lp);
558                 }
559         }
560
561         /* if there are locks left, restart timer */
562         if (l_top)
563                 alarm(NSECS);
564 }
565
566
567 /*
568 ** If timer goes off, we update the ctime of all open
569 ** lockfiles, so another command doesn't remove them.
570 */
571
572 static void
573 alrmser(int sig)
574 {
575         char *lockfile;
576         struct lock *lp;
577
578         /* update the ctime of all the lock files */
579         for (lp = l_top; lp; lp = lp->l_next) {
580                 lockfile = lp->l_lock;
581 #if !defined(HAVE_LIBLOCKFILE)
582                 {
583                         int j;
584                         if (*lockfile && (j = creat(lockfile, 0600)) != -1)
585                                 close(j);
586                 }
587 #else
588         lockfile_touch(lockfile);
589 #endif
590         }
591
592         /* restart the alarm */
593         alarm(NSECS);
594 }
595
596 #endif /* DOT_LOCKING */