locker.c
13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Library Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <utime.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <mailutils/errno.h>
#include <mailutils/locker.h>
#include <mailutils/mutil.h>
#define LOCKFILE_ATTR 0444
/* First draft by Brian Edmond. */
/* For subsequent modifications, see the GNU mailutils ChangeLog. */
struct _locker
{
int refcnt;
char *file;
char *dotlock;
char *nfslock;
int flags;
int expire_time;
int retries;
int retry_sleep;
char *external;
};
/* Assert that we're managing the refcnt and fd correctly, either
* we have a lock, and the fd is valid, or refcnt is 0 and fd is -1.
* And refcnt can never be less than 0.
*/
#define INVARIANT(l) assert((l)->refcnt >= 0);
static void expire_stale_lock (locker_t lock);
static int stat_check (const char *file, int fd, int links);
static int check_file_permissions (const char *file);
static int lock_external(locker_t l, int lock);
int
locker_create (locker_t *plocker, const char *filename, int flags)
{
locker_t l;
if (plocker == NULL)
return MU_ERR_OUT_PTR_NULL;
if(filename == NULL)
return EINVAL;
l = calloc (1, sizeof (*l));
if (l == NULL)
return ENOMEM;
/* Should make l->file be the resulting of following the symlinks. */
l->file = strdup(filename);
if (l->file == NULL)
{
free (l);
return ENOMEM;
}
l->dotlock = malloc(strlen(l->file) + 5 /*strlen(".lock")*/ + 1);
if(!l->dotlock)
{
free (l->file);
free (l);
return ENOMEM;
}
sprintf(l->dotlock, "%s.lock", l->file);
if (strcmp (filename, "/dev/null") == 0)
l->flags = MU_LOCKER_NULL;
else if (flags)
l->flags = flags;
else
l->flags = MU_LOCKER_DEFAULT;
l->expire_time = MU_LOCKER_EXPIRE_TIME;
l->retries = MU_LOCKER_RETRIES;
l->retry_sleep = MU_LOCKER_RETRY_SLEEP;
if(flags & MU_LOCKER_EXTERNAL)
{
if(!(l->external = strdup(MU_LOCKER_EXTERNAL_PROGRAM)))
{
locker_destroy(&l);
return ENOMEM;
}
}
INVARIANT(l);
*plocker = l;
return 0;
}
void
locker_destroy (locker_t *plocker)
{
if (plocker && *plocker)
{
free ((*plocker)->file);
free ((*plocker)->dotlock);
free ((*plocker)->nfslock);
free ((*plocker)->external);
free (*plocker);
*plocker = NULL;
}
}
int locker_set_flags (locker_t locker, int flags)
{
if (!locker)
return MU_ERR_LOCKER_NULL;
locker->flags = flags;
return 0;
}
int
locker_set_expire_time (locker_t locker, int etime)
{
if (!locker)
return MU_ERR_LOCKER_NULL;
if(etime <= 0)
return EINVAL;
locker->expire_time = etime;
return 0;
}
int
locker_set_retries (locker_t locker, int retries)
{
if (!locker)
return MU_ERR_LOCKER_NULL;
if(retries <= 0)
return EINVAL;
locker->retries = retries;
return 0;
}
int
locker_set_retry_sleep (locker_t locker, int retry_sleep)
{
if (!locker)
return MU_ERR_LOCKER_NULL;
if(retry_sleep <= 0)
return EINVAL;
locker->retry_sleep = retry_sleep;
return 0;
}
int
locker_set_external (locker_t locker, const char* program)
{
char* p = NULL;
if (!locker)
return MU_ERR_LOCKER_NULL;
/* program can be NULL */
if(program != 0)
{
p = strdup(program);
if(!p)
return ENOMEM;
}
free(locker->external);
locker->external = p;
return 0;
}
int locker_get_flags (locker_t locker, int *flags)
{
if (!locker)
return MU_ERR_LOCKER_NULL;
if(!flags)
return EINVAL;
*flags = locker->flags;
return 0;
}
int
locker_get_expire_time (locker_t locker, int *ptime)
{
if (!locker)
return MU_ERR_LOCKER_NULL;
if(!ptime)
return EINVAL;
*ptime = locker->expire_time;
return 0;
}
int
locker_get_retries (locker_t locker, int *retries)
{
if (!locker)
return MU_ERR_LOCKER_NULL;
if(!retries)
return EINVAL;
*retries = locker->retries;
return 0;
}
int
locker_get_retry_sleep (locker_t locker, int *retry_sleep)
{
if (!locker)
return MU_ERR_LOCKER_NULL;
if(!retry_sleep)
return EINVAL;
*retry_sleep = locker->retry_sleep;
return 0;
}
int
locker_lock (locker_t lock)
{
int err = 0;
int fd = -1;
int retries = 1;
if (lock == NULL)
return EINVAL;
if (lock->flags == MU_LOCKER_NULL)
return 0;
INVARIANT (lock)
/* Is the lock already applied? */
if (lock->refcnt > 0)
{
lock->refcnt++;
return 0;
}
/* Do the lock with an external program, if requested. */
if (lock->flags & MU_LOCKER_EXTERNAL)
{
return lock_external(lock, 1);
}
/* Check we are trying to lock a regular file, with a link count
of 1, that we have permission to read, etc., or don't lock it. */
if((err = check_file_permissions(lock->file)))
return err;
if (lock->flags & MU_LOCKER_RETRY)
{
retries = lock->retries;
}
while (retries--)
{
/* cleanup after last loop */
close (fd);
fd = -1;
if (lock->nfslock)
{
unlink (lock->nfslock);
free (lock->nfslock);
lock->nfslock = 0;
}
expire_stale_lock (lock);
/* build the NFS hitching-post to the lock file */
{
char host[MAXHOSTNAMELEN + 1] = "localhost";
char pid[11]; /* 10 is strlen(2^32 = 4294967296) */
char now[11];
size_t sz = 0;
gethostname (host, sizeof (host));
host[MAXHOSTNAMELEN] = 0;
snprintf (pid, sizeof (pid), "%d", getpid ());
pid[sizeof (pid) - 1] = 0;
snprintf (now, sizeof (now), "%d", time (0));
now[sizeof (now) - 1] = 0;
sz = strlen (lock->file) + 1 /* "." */ +
strlen (pid) + 1 /* "." */ +
strlen (now) + 1 /* "." */ +
strlen (host) + 1;
lock->nfslock = malloc (sz);
if (!lock->nfslock)
return ENOMEM;
snprintf (lock->nfslock, sz, "%s.%s.%s.%s", lock->file, pid, now,
host);
lock->nfslock[sz - 1] = 0;
}
fd = open (lock->nfslock, O_WRONLY | O_CREAT | O_EXCL, LOCKFILE_ATTR);
if (fd == -1)
{
if (errno == EEXIST)
continue; /* Try with another lockfile name. */
else
return errno;
}
close (fd);
fd = -1;
/* Try to link to the lockfile. */
if (link (lock->nfslock, lock->dotlock) == -1)
{
err = errno;
if (err != EEXIST)
break; /* fatal */
}
else
{
if ((fd = open (lock->dotlock, O_RDONLY)) == -1)
{
err = errno;
break; /* fatal, we just made it, we should be able to open! */
}
err = stat_check (lock->nfslock, fd, 2);
if (err == 0)
{
/* We got the lock! */
break;
}
else
{
unlink (lock->dotlock);
unlink (lock->nfslock);
/* If there was something invalid about the file/fd, return
a more descriptive code. */
if (err == EINVAL)
err = MU_ERR_LOCK_BAD_LOCK;
break;
}
}
if (retries)
sleep (lock->retry_sleep);
}
unlink (lock->nfslock);
/* EEXIST means somebody else has the lock, anything else is an
error. */
if (err == EEXIST)
err = MU_ERR_LOCK_CONFLICT;
if (err)
return err;
/* If no errors, we have the lock. */
assert (lock->refcnt == 0);
if (lock->flags & MU_LOCKER_PID)
{
char buf[16];
sprintf (buf, "%ld", (long) getpid ());
write (fd, buf, strlen (buf));
}
close(fd);
lock->refcnt = 1;
return 0;
}
int
locker_touchlock (locker_t lock)
{
if (!lock)
return MU_ERR_LOCKER_NULL;
if (lock->flags == MU_LOCKER_NULL)
return 0;
if(lock->refcnt > 0)
return utime (lock->dotlock, NULL);
return MU_ERR_LOCK_NOT_HELD;
}
int
locker_unlock (locker_t lock)
{
int err = 0;
if (!lock)
return MU_ERR_LOCKER_NULL;
if (lock->flags == MU_LOCKER_NULL)
return 0;
if (lock->refcnt == 0)
return MU_ERR_LOCK_NOT_HELD;
/* Do the lock with an external program, if requested. */
if (lock->flags & MU_LOCKER_EXTERNAL)
{
return lock_external (lock, 0);
}
if (lock->refcnt > 1)
{
lock->refcnt--;
return 0;
}
if ((err = check_file_permissions (lock->file)))
return err;
if (unlink (lock->dotlock) == -1)
{
err = errno;
if (err == ENOENT)
{
lock->refcnt = 0;
err = MU_ERR_LOCK_NOT_HELD;
return err;
}
return err;
}
lock->refcnt = 0;
return 0;
}
int
locker_remove_lock (locker_t lock)
{
int err;
if (!lock)
return MU_ERR_LOCKER_NULL;
if (lock->flags == MU_LOCKER_NULL)
return 0;
/* Force the reference count to 1 to unlock the file. */
lock->refcnt = 1;
err = locker_unlock(lock);
return err;
}
/* expire a stale lock (if MU_LOCKER_PID or MU_LOCKER_TIME) */
static void
expire_stale_lock (locker_t lock)
{
int stale = 0;
int fd = open (lock->dotlock, O_RDONLY);
if (fd == -1)
return;
/* Check to see if this process is still running. */
if (lock->flags & MU_LOCKER_PID)
{
char buf[16];
pid_t pid;
int nread = read (fd, buf, sizeof (buf) - 1);
if (nread > 0)
{
buf[nread] = '\0';
pid = strtol (buf, NULL, 10);
if (pid > 0)
{
/* Process is gone so we try to remove the lock. */
if (kill (pid, 0) == -1)
stale = 1;
}
else
stale = 1; /* Corrupted file, remove the lock. */
}
}
/* Check to see if the lock expired. */
if (lock->flags & MU_LOCKER_TIME)
{
struct stat stbuf;
fstat (fd, &stbuf);
/* The lock has expired. */
if ((time (NULL) - stbuf.st_mtime) > lock->expire_time)
stale = 1;
}
close (fd);
if (stale)
unlink (lock->dotlock);
}
static int
stat_check (const char *file, int fd, int links)
{
struct stat fn_stat;
struct stat fd_stat;
int err = 0;
int localfd = -1;
if(fd == -1)
{
localfd = open(file, O_RDONLY);
if(localfd == -1)
return errno;
fd = localfd;
}
/* We should always be able to stat a valid fd, so this
is an error condition. */
if (lstat (file, &fn_stat) || fstat (fd, &fd_stat))
err = errno;
else
{
/* If the link and stat don't report the same info, or the
file is a symlink, fail the locking. */
#define CHK(X) if(X) err = EINVAL
CHK (!S_ISREG (fn_stat.st_mode));
CHK (!S_ISREG (fd_stat.st_mode));
CHK (fn_stat.st_nlink != links);
CHK (fn_stat.st_dev != fd_stat.st_dev);
CHK (fn_stat.st_ino != fd_stat.st_ino);
CHK (fn_stat.st_mode != fd_stat.st_mode);
CHK (fn_stat.st_nlink != fd_stat.st_nlink);
CHK (fn_stat.st_uid != fd_stat.st_uid);
CHK (fn_stat.st_gid != fd_stat.st_gid);
CHK (fn_stat.st_rdev != fd_stat.st_rdev);
#undef CHK
}
if(localfd != -1)
close(localfd);
return err;
}
static int
check_file_permissions (const char *file)
{
int fd = -1;
int err = 0;
if ((fd = open (file, O_RDONLY)) == -1)
return errno;
err = stat_check (file, fd, 1);
close (fd);
fd = -1;
if (err)
{
if (err == EINVAL)
err = MU_ERR_LOCK_BAD_FILE;
return err;
}
return 0;
}
/*
Estimate 1 decimal digit per 3 bits, + 1 for round off.
*/
#define DEC_DIGS_PER_INT (sizeof(int) * 8 / 3 + 1)
static int
lock_external (locker_t l, int lock)
{
int err = 0;
const char *av[6];
int ac = 0;
char aforce[3 + DEC_DIGS_PER_INT + 1];
char aretry[3 + DEC_DIGS_PER_INT + 1];
int status = 0;
assert (l);
assert (l->flags & MU_LOCKER_EXTERNAL);
assert (lock == !l->refcnt);
/* lock is true, refcnt is 0 or lock is false and refcnt is 1 */
av[ac++] = l->external ? l->external : MU_LOCKER_EXTERNAL_PROGRAM;
if (l->flags & MU_LOCKER_TIME)
{
snprintf (aforce, sizeof (aforce), "-f%d", l->expire_time);
aforce[sizeof (aforce) - 1] = 0;
av[ac++] = aforce;
}
if (l->flags & MU_LOCKER_RETRY)
{
snprintf (aretry, sizeof (aretry), "-r%d", l->retries);
aretry[sizeof (aretry) - 1] = 0;
av[ac++] = aretry;
}
if (lock == 0)
{
av[ac++] = "-u";
}
av[ac++] = l->file;
av[ac++] = NULL;
if ((err = mu_spawnvp (av[0], av, &status)))
return err;
if (!WIFEXITED (status))
{
err = MU_ERR_LOCK_EXT_KILLED;
}
else
{
switch (WEXITSTATUS (status))
{
case 127:
err = MU_ERR_LOCK_EXT_FAIL;
break;
case MU_DL_EX_OK:
err = 0;
break;
case MU_DL_EX_NEXIST:
err = MU_ERR_LOCK_NOT_HELD;
break;
case MU_DL_EX_EXIST:
err = MU_ERR_LOCK_CONFLICT;
break;
case MU_DL_EX_PERM:
err = EPERM;
break;
default:
case MU_DL_EX_ERROR:
err = MU_ERR_LOCK_EXT_ERR;
break;
}
}
return err;
}