~ubuntu-branches/ubuntu/trusty/util-linux/trusty-proposed

« back to all changes in this revision

Viewing changes to libmount/src/lock.c

  • Committer: Package Import Robot
  • Author(s): LaMont Jones
  • Date: 2011-11-03 15:38:23 UTC
  • mto: (4.5.5 sid) (1.6.4)
  • mto: This revision was merged to the branch mainline in revision 85.
  • Revision ID: package-import@ubuntu.com-20111103153823-10sx16jprzxlhkqf
ImportĀ upstreamĀ versionĀ 2.20.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2009 Karel Zak <kzak@redhat.com>
 
3
 *
 
4
 * This file may be redistributed under the terms of the
 
5
 * GNU Lesser General Public License.
 
6
 */
 
7
 
 
8
/**
 
9
 * SECTION: lock
 
10
 * @title: Locking
 
11
 * @short_description: locking methods for /etc/mtab or another libmount files
 
12
 *
 
13
 * The mtab lock is backwardly compatible with the standard linux /etc/mtab
 
14
 * locking.  Note, it's necessary to use the same locking schema in all
 
15
 * application that access the file.
 
16
 */
 
17
#include <sys/time.h>
 
18
#include <time.h>
 
19
#include <signal.h>
 
20
#include <fcntl.h>
 
21
#include <limits.h>
 
22
#include <sys/file.h>
 
23
 
 
24
#include "pathnames.h"
 
25
#include "mountP.h"
 
26
 
 
27
/*
 
28
 * lock handler
 
29
 */
 
30
struct libmnt_lock {
 
31
        char    *lockfile;      /* path to lock file (e.g. /etc/mtab~) */
 
32
        char    *linkfile;      /* path to link file (e.g. /etc/mtab~.<id>) */
 
33
        int     lockfile_fd;    /* lock file descriptor */
 
34
 
 
35
        unsigned int    locked :1,      /* do we own the lock? */
 
36
                        sigblock :1,    /* block signals when locked */
 
37
                        simplelock :1;  /* use flock rather than normal mtab lock */
 
38
 
 
39
        sigset_t oldsigmask;
 
40
};
 
41
 
 
42
 
 
43
/**
 
44
 * mnt_new_lock:
 
45
 * @datafile: the file that should be covered by the lock
 
46
 * @id: unique linkfile identifier or 0 (default is getpid())
 
47
 *
 
48
 * Returns: newly allocated lock handler or NULL on case of error.
 
49
 */
 
50
struct libmnt_lock *mnt_new_lock(const char *datafile, pid_t id)
 
51
{
 
52
        struct libmnt_lock *ml = NULL;
 
53
        char *lo = NULL, *ln = NULL;
 
54
        size_t losz;
 
55
 
 
56
        if (!datafile)
 
57
                return NULL;
 
58
 
 
59
        /* for flock we use "foo.lock, for mtab "foo~"
 
60
         */
 
61
        losz = strlen(datafile) + sizeof(".lock");
 
62
        lo = malloc(losz);
 
63
        if (!lo)
 
64
                goto err;
 
65
 
 
66
        /* default is mtab~ lock */
 
67
        snprintf(lo, losz, "%s~", datafile);
 
68
 
 
69
        if (asprintf(&ln, "%s~.%d", datafile, id ? : getpid()) == -1) {
 
70
                ln = NULL;
 
71
                goto err;
 
72
        }
 
73
        ml = calloc(1, sizeof(*ml) );
 
74
        if (!ml)
 
75
                goto err;
 
76
 
 
77
        ml->lockfile_fd = -1;
 
78
        ml->linkfile = ln;
 
79
        ml->lockfile = lo;
 
80
 
 
81
        DBG(LOCKS, mnt_debug_h(ml, "alloc: default linkfile=%s, lockfile=%s", ln, lo));
 
82
        return ml;
 
83
err:
 
84
        free(lo);
 
85
        free(ln);
 
86
        free(ml);
 
87
        return NULL;
 
88
}
 
89
 
 
90
 
 
91
/**
 
92
 * mnt_free_lock:
 
93
 * @ml: struct libmnt_lock handler
 
94
 *
 
95
 * Deallocates mnt_lock.
 
96
 */
 
97
void mnt_free_lock(struct libmnt_lock *ml)
 
98
{
 
99
        if (!ml)
 
100
                return;
 
101
        DBG(LOCKS, mnt_debug_h(ml, "free%s", ml->locked ? " !!! LOCKED !!!" : ""));
 
102
        free(ml->lockfile);
 
103
        free(ml->linkfile);
 
104
        free(ml);
 
105
}
 
106
 
 
107
/**
 
108
 * mnt_lock_block_signals:
 
109
 * @ml: struct libmnt_lock handler
 
110
 * @enable: TRUE/FALSE
 
111
 *
 
112
 * Block/unblock signals when the lock is locked, the signals are not blocked
 
113
 * by default.
 
114
 *
 
115
 * Returns: <0 on error, 0 on success.
 
116
 */
 
117
int mnt_lock_block_signals(struct libmnt_lock *ml, int enable)
 
118
{
 
119
        if (!ml)
 
120
                return -EINVAL;
 
121
        DBG(LOCKS, mnt_debug_h(ml, "signals: %s", enable ? "BLOCKED" : "UNBLOCKED"));
 
122
        ml->sigblock = enable ? 1 : 0;
 
123
        return 0;
 
124
}
 
125
 
 
126
/* don't export this to API
 
127
 */
 
128
int mnt_lock_use_simplelock(struct libmnt_lock *ml, int enable)
 
129
{
 
130
        size_t sz;
 
131
 
 
132
        if (!ml)
 
133
                return -EINVAL;
 
134
 
 
135
        assert(ml->lockfile);
 
136
 
 
137
        DBG(LOCKS, mnt_debug_h(ml, "flock: %s", enable ? "ENABLED" : "DISABLED"));
 
138
        ml->simplelock = enable ? 1 : 0;
 
139
 
 
140
        sz = strlen(ml->lockfile);
 
141
 
 
142
        /* Change lock name:
 
143
         *
 
144
         *      flock:     "<name>.lock"
 
145
         *      mtab lock: "<name>~"
 
146
         */
 
147
        if (ml->simplelock && endswith(ml->lockfile, "~"))
 
148
                memcpy(ml->lockfile + sz - 1, ".lock", 6);
 
149
 
 
150
        else if (!ml->simplelock && endswith(ml->lockfile, ".lock"))
 
151
                 memcpy(ml->lockfile + sz - 5, "~", 2);
 
152
 
 
153
        DBG(LOCKS, mnt_debug_h(ml, "new lock filename: '%s'", ml->lockfile));
 
154
        return 0;
 
155
}
 
156
 
 
157
/*
 
158
 * Returns path to lockfile.
 
159
 */
 
160
static const char *mnt_lock_get_lockfile(struct libmnt_lock *ml)
 
161
{
 
162
        return ml ? ml->lockfile : NULL;
 
163
}
 
164
 
 
165
/*
 
166
 * Note that the filename is generated by mnt_new_lock() and depends on
 
167
 * getpid() or 'id' argument of the mnt_new_lock() function.
 
168
 *
 
169
 * Returns: unique (per process/thread) path to linkfile.
 
170
 */
 
171
static const char *mnt_lock_get_linkfile(struct libmnt_lock *ml)
 
172
{
 
173
        return ml ? ml->linkfile : NULL;
 
174
}
 
175
 
 
176
/*
 
177
 * Simple flocking
 
178
 */
 
179
static void unlock_simplelock(struct libmnt_lock *ml)
 
180
{
 
181
        assert(ml);
 
182
        assert(ml->simplelock);
 
183
 
 
184
        if (ml->lockfile_fd >= 0) {
 
185
                DBG(LOCKS, mnt_debug_h(ml, "%s: unflocking",
 
186
                                        mnt_lock_get_lockfile(ml)));
 
187
                close(ml->lockfile_fd);
 
188
        }
 
189
}
 
190
 
 
191
static int lock_simplelock(struct libmnt_lock *ml)
 
192
{
 
193
        const char *lfile;
 
194
        int rc;
 
195
 
 
196
        assert(ml);
 
197
        assert(ml->simplelock);
 
198
 
 
199
        lfile = mnt_lock_get_lockfile(ml);
 
200
 
 
201
        DBG(LOCKS, mnt_debug_h(ml, "%s: locking", lfile));
 
202
 
 
203
        if (ml->sigblock) {
 
204
                sigset_t sigs;
 
205
                sigemptyset(&ml->oldsigmask);
 
206
                sigfillset(&sigs);
 
207
                sigprocmask(SIG_BLOCK, &sigs, &ml->oldsigmask);
 
208
        }
 
209
 
 
210
        ml->lockfile_fd = open(lfile, O_RDONLY|O_CREAT|O_CLOEXEC,
 
211
                                      S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH);
 
212
        if (ml->lockfile_fd < 0) {
 
213
                rc = -errno;
 
214
                goto err;
 
215
        }
 
216
 
 
217
        while (flock(ml->lockfile_fd, LOCK_EX) < 0) {
 
218
                int errsv;
 
219
                if ((errno == EAGAIN) || (errno == EINTR))
 
220
                        continue;
 
221
                errsv = errno;
 
222
                close(ml->lockfile_fd);
 
223
                ml->lockfile_fd = -1;
 
224
                rc = -errsv;
 
225
                goto err;
 
226
        }
 
227
        ml->locked = 1;
 
228
        return 0;
 
229
err:
 
230
        if (ml->sigblock)
 
231
                sigprocmask(SIG_SETMASK, &ml->oldsigmask, NULL);
 
232
        return rc;
 
233
}
 
234
 
 
235
/*
 
236
 * traditional mtab locking
 
237
 */
 
238
 
 
239
static void mnt_lockalrm_handler(int sig __attribute__((__unused__)))
 
240
{
 
241
        /* do nothing, say nothing, be nothing */
 
242
}
 
243
 
 
244
/*
 
245
 * Waits for F_SETLKW, unfortunately we have to use SIGALRM here to interrupt
 
246
 * fcntl() to avoid never ending waiting.
 
247
 *
 
248
 * Returns: 0 on success, 1 on timeout, -errno on error.
 
249
 */
 
250
static int mnt_wait_mtab_lock(struct libmnt_lock *ml, struct flock *fl, time_t maxtime)
 
251
{
 
252
        struct timeval now;
 
253
        struct sigaction sa, osa;
 
254
        int ret = 0;
 
255
 
 
256
        gettimeofday(&now, NULL);
 
257
 
 
258
        if (now.tv_sec >= maxtime)
 
259
                return 1;               /* timeout */
 
260
 
 
261
        /* setup ALARM handler -- we don't want to wait forever */
 
262
        sa.sa_flags = 0;
 
263
        sa.sa_handler = mnt_lockalrm_handler;
 
264
        sigfillset (&sa.sa_mask);
 
265
 
 
266
        sigaction(SIGALRM, &sa, &osa);
 
267
 
 
268
        DBG(LOCKS, mnt_debug_h(ml, "(%d) waiting for F_SETLKW", getpid()));
 
269
 
 
270
        alarm(maxtime - now.tv_sec);
 
271
        if (fcntl(ml->lockfile_fd, F_SETLKW, fl) == -1)
 
272
                ret = errno == EINTR ? 1 : -errno;
 
273
        alarm(0);
 
274
 
 
275
        /* restore old sigaction */
 
276
        sigaction(SIGALRM, &osa, NULL);
 
277
 
 
278
        DBG(LOCKS, mnt_debug_h(ml, "(%d) leaving mnt_wait_setlkw(), rc=%d",
 
279
                                getpid(), ret));
 
280
        return ret;
 
281
}
 
282
 
 
283
/*
 
284
 * Create the mtab lock file.
 
285
 *
 
286
 * The old code here used flock on a lock file /etc/mtab~ and deleted
 
287
 * this lock file afterwards. However, as rgooch remarks, that has a
 
288
 * race: a second mount may be waiting on the lock and proceed as
 
289
 * soon as the lock file is deleted by the first mount, and immediately
 
290
 * afterwards a third mount comes, creates a new /etc/mtab~, applies
 
291
 * flock to that, and also proceeds, so that the second and third mount
 
292
 * now both are scribbling in /etc/mtab.
 
293
 *
 
294
 * The new code uses a link() instead of a creat(), where we proceed
 
295
 * only if it was us that created the lock, and hence we always have
 
296
 * to delete the lock afterwards. Now the use of flock() is in principle
 
297
 * superfluous, but avoids an arbitrary sleep().
 
298
 *
 
299
 * Where does the link point to? Obvious choices are mtab and mtab~~.
 
300
 * HJLu points out that the latter leads to races. Right now we use
 
301
 * mtab~.<pid> instead.
 
302
 *
 
303
 *
 
304
 * The original mount locking code has used sleep(1) between attempts and
 
305
 * maximal number of attempts has been 5.
 
306
 *
 
307
 * There was very small number of attempts and extremely long waiting (1s)
 
308
 * that is useless on machines with large number of mount processes.
 
309
 *
 
310
 * Now we wait few thousand microseconds between attempts and we have a global
 
311
 * time limit (30s) rather than limit for number of attempts. The advantage
 
312
 * is that this method also counts time which we spend in fcntl(F_SETLKW) and
 
313
 * number of attempts is not restricted.
 
314
 * -- kzak@redhat.com [Mar-2007]
 
315
 *
 
316
 *
 
317
 * This mtab locking code has been refactored and moved to libmount. The mtab
 
318
 * locking is really not perfect (e.g. SIGALRM), but it's stable, reliable and
 
319
 * backwardly compatible code.
 
320
 *
 
321
 * Don't forget that this code has to be compatible with 3rd party mounts
 
322
 * (/sbin/mount.<foo>) and has to work with NFS.
 
323
 * -- kzak@redhat.com [May-2009]
 
324
 */
 
325
 
 
326
/* maximum seconds between first and last attempt */
 
327
#define MOUNTLOCK_MAXTIME               30
 
328
 
 
329
/* sleep time (in microseconds, max=999999) between attempts */
 
330
#define MOUNTLOCK_WAITTIME              5000
 
331
 
 
332
static void unlock_mtab(struct libmnt_lock *ml)
 
333
{
 
334
        if (!ml)
 
335
                return;
 
336
 
 
337
        if (!ml->locked && ml->lockfile && ml->linkfile)
 
338
        {
 
339
                /* We have (probably) all files, but we don't own the lock,
 
340
                 * Really? Check it! Maybe ml->locked wasn't set properly
 
341
                 * because code was interrupted by signal. Paranoia? Yes.
 
342
                 *
 
343
                 * We own the lock when linkfile == lockfile.
 
344
                 */
 
345
                struct stat lo, li;
 
346
 
 
347
                if (!stat(ml->lockfile, &lo) && !stat(ml->linkfile, &li) &&
 
348
                    lo.st_dev == li.st_dev && lo.st_ino == li.st_ino)
 
349
                        ml->locked = 1;
 
350
        }
 
351
 
 
352
        if (ml->linkfile)
 
353
                unlink(ml->linkfile);
 
354
        if (ml->lockfile_fd >= 0)
 
355
                close(ml->lockfile_fd);
 
356
        if (ml->locked && ml->lockfile) {
 
357
                unlink(ml->lockfile);
 
358
                DBG(LOCKS, mnt_debug_h(ml, "unlink %s", ml->lockfile));
 
359
        }
 
360
}
 
361
 
 
362
static int lock_mtab(struct libmnt_lock *ml)
 
363
{
 
364
        int i, rc = -1;
 
365
        struct timespec waittime;
 
366
        struct timeval maxtime;
 
367
        const char *lockfile, *linkfile;
 
368
 
 
369
        if (!ml)
 
370
                return -EINVAL;
 
371
        if (ml->locked)
 
372
                return 0;
 
373
 
 
374
        lockfile = mnt_lock_get_lockfile(ml);
 
375
        if (!lockfile)
 
376
                return -EINVAL;
 
377
        linkfile = mnt_lock_get_linkfile(ml);
 
378
        if (!linkfile)
 
379
                return -EINVAL;
 
380
 
 
381
        if (ml->sigblock) {
 
382
                /*
 
383
                 * Block all signals when locked, mnt_unlock_file() will
 
384
                 * restore the old mask.
 
385
                 */
 
386
                sigset_t sigs;
 
387
 
 
388
                sigemptyset(&ml->oldsigmask);
 
389
                sigfillset(&sigs);
 
390
                sigdelset(&sigs, SIGTRAP);
 
391
                sigdelset(&sigs, SIGALRM);
 
392
                sigprocmask(SIG_BLOCK, &sigs, &ml->oldsigmask);
 
393
        }
 
394
 
 
395
        i = open(linkfile, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);
 
396
        if (i < 0) {
 
397
                /* linkfile does not exist (as a file) and we cannot create it.
 
398
                 * Read-only or full filesystem? Too many files open in the system?
 
399
                 */
 
400
                if (errno > 0)
 
401
                        rc = -errno;
 
402
                goto failed;
 
403
        }
 
404
        close(i);
 
405
 
 
406
        gettimeofday(&maxtime, NULL);
 
407
        maxtime.tv_sec += MOUNTLOCK_MAXTIME;
 
408
 
 
409
        waittime.tv_sec = 0;
 
410
        waittime.tv_nsec = (1000 * MOUNTLOCK_WAITTIME);
 
411
 
 
412
        /* Repeat until it was us who made the link */
 
413
        while (!ml->locked) {
 
414
                struct timeval now;
 
415
                struct flock flock;
 
416
                int j;
 
417
 
 
418
                j = link(linkfile, lockfile);
 
419
                if (j == 0)
 
420
                        ml->locked = 1;
 
421
 
 
422
                if (j < 0 && errno != EEXIST) {
 
423
                        if (errno > 0)
 
424
                                rc = -errno;
 
425
                        goto failed;
 
426
                }
 
427
                ml->lockfile_fd = open(lockfile, O_WRONLY);
 
428
 
 
429
                if (ml->lockfile_fd < 0) {
 
430
                        /* Strange... Maybe the file was just deleted? */
 
431
                        int errsv = errno;
 
432
                        gettimeofday(&now, NULL);
 
433
                        if (errsv == ENOENT && now.tv_sec < maxtime.tv_sec) {
 
434
                                ml->locked = 0;
 
435
                                continue;
 
436
                        }
 
437
                        if (errsv > 0)
 
438
                                rc = -errsv;
 
439
                        goto failed;
 
440
                }
 
441
 
 
442
                flock.l_type = F_WRLCK;
 
443
                flock.l_whence = SEEK_SET;
 
444
                flock.l_start = 0;
 
445
                flock.l_len = 0;
 
446
 
 
447
                if (ml->locked) {
 
448
                        /* We made the link. Now claim the lock. */
 
449
                        if (fcntl (ml->lockfile_fd, F_SETLK, &flock) == -1) {
 
450
                                DBG(LOCKS, mnt_debug_h(ml,
 
451
                                        "%s: can't F_SETLK lockfile, errno=%d\n",
 
452
                                        lockfile, errno));
 
453
                                /* proceed, since it was us who created the lockfile anyway */
 
454
                        }
 
455
                        break;
 
456
                } else {
 
457
                        /* Someone else made the link. Wait. */
 
458
                        int err = mnt_wait_mtab_lock(ml, &flock, maxtime.tv_sec);
 
459
 
 
460
                        if (err == 1) {
 
461
                                DBG(LOCKS, mnt_debug_h(ml,
 
462
                                        "%s: can't create link: time out (perhaps "
 
463
                                        "there is a stale lock file?)", lockfile));
 
464
                                rc = -ETIMEDOUT;
 
465
                                goto failed;
 
466
 
 
467
                        } else if (err < 0) {
 
468
                                rc = err;
 
469
                                goto failed;
 
470
                        }
 
471
                        nanosleep(&waittime, NULL);
 
472
                        close(ml->lockfile_fd);
 
473
                        ml->lockfile_fd = -1;
 
474
                }
 
475
        }
 
476
        DBG(LOCKS, mnt_debug_h(ml, "%s: (%d) successfully locked",
 
477
                                        lockfile, getpid()));
 
478
        unlink(linkfile);
 
479
        return 0;
 
480
 
 
481
failed:
 
482
        mnt_unlock_file(ml);
 
483
        return rc;
 
484
}
 
485
 
 
486
 
 
487
/**
 
488
 * mnt_lock_file
 
489
 * @ml: pointer to struct libmnt_lock instance
 
490
 *
 
491
 * Creates lock file (e.g. /etc/mtab~). Note that this function may
 
492
 * use alarm().
 
493
 *
 
494
 * Your application has to always call mnt_unlock_file() before exit.
 
495
 *
 
496
 * Traditional mtab locking scheme:
 
497
 *
 
498
 *   1. create linkfile (e.g. /etc/mtab~.$PID)
 
499
 *   2. link linkfile --> lockfile (e.g. /etc/mtab~.$PID --> /etc/mtab~)
 
500
 *   3. a) link() success: setups F_SETLK lock (see fcnlt(2))
 
501
 *      b) link() failed:  wait (max 30s) on F_SETLKW lock, goto 2.
 
502
 *
 
503
 * Returns: 0 on success or negative number in case of error (-ETIMEOUT is case
 
504
 * of stale lock file).
 
505
 */
 
506
int mnt_lock_file(struct libmnt_lock *ml)
 
507
{
 
508
        if (!ml)
 
509
                return -EINVAL;
 
510
 
 
511
        if (ml->simplelock)
 
512
                return lock_simplelock(ml);
 
513
 
 
514
        return lock_mtab(ml);
 
515
}
 
516
 
 
517
/**
 
518
 * mnt_unlock_file:
 
519
 * @ml: lock struct
 
520
 *
 
521
 * Unlocks the file. The function could be called independently on the
 
522
 * lock status (for example from exit(3)).
 
523
 */
 
524
void mnt_unlock_file(struct libmnt_lock *ml)
 
525
{
 
526
        if (!ml)
 
527
                return;
 
528
 
 
529
        DBG(LOCKS, mnt_debug_h(ml, "(%d) %s", getpid(),
 
530
                        ml->locked ? "unlocking" : "cleaning"));
 
531
 
 
532
        if (ml->simplelock)
 
533
                unlock_simplelock(ml);
 
534
        else
 
535
                unlock_mtab(ml);
 
536
 
 
537
        ml->locked = 0;
 
538
        ml->lockfile_fd = -1;
 
539
 
 
540
        if (ml->sigblock) {
 
541
                DBG(LOCKS, mnt_debug_h(ml, "restoring sigmask"));
 
542
                sigprocmask(SIG_SETMASK, &ml->oldsigmask, NULL);
 
543
        }
 
544
}
 
545
 
 
546
#ifdef TEST_PROGRAM
 
547
 
 
548
struct libmnt_lock *lock;
 
549
 
 
550
/*
 
551
 * read number from @filename, increment the number and
 
552
 * write the number back to the file
 
553
 */
 
554
void increment_data(const char *filename, int verbose, int loopno)
 
555
{
 
556
        long num;
 
557
        FILE *f;
 
558
        char buf[256];
 
559
 
 
560
        if (!(f = fopen(filename, "r")))
 
561
                err(EXIT_FAILURE, "%d: failed to open: %s", getpid(), filename);
 
562
 
 
563
        if (!fgets(buf, sizeof(buf), f))
 
564
                err(EXIT_FAILURE, "%d failed read: %s", getpid(), filename);
 
565
 
 
566
        fclose(f);
 
567
        num = atol(buf) + 1;
 
568
 
 
569
        if (!(f = fopen(filename, "w")))
 
570
                err(EXIT_FAILURE, "%d: failed to open: %s", getpid(), filename);
 
571
 
 
572
        fprintf(f, "%ld", num);
 
573
        fclose(f);
 
574
 
 
575
        if (verbose)
 
576
                fprintf(stderr, "%d: %s: %ld --> %ld (loop=%d)\n", getpid(),
 
577
                                filename, num - 1, num, loopno);
 
578
}
 
579
 
 
580
void clean_lock(void)
 
581
{
 
582
        if (!lock)
 
583
                return;
 
584
        mnt_unlock_file(lock);
 
585
        mnt_free_lock(lock);
 
586
}
 
587
 
 
588
void sig_handler(int sig)
 
589
{
 
590
        errx(EXIT_FAILURE, "\n%d: catch signal: %s\n", getpid(), strsignal(sig));
 
591
}
 
592
 
 
593
int test_lock(struct libmnt_test *ts, int argc, char *argv[])
 
594
{
 
595
        time_t synctime = 0;
 
596
        unsigned int usecs;
 
597
        struct timeval tv;
 
598
        const char *datafile = NULL;
 
599
        int verbose = 0, loops = 0, l, idx = 1;
 
600
 
 
601
        if (argc < 3)
 
602
                return -EINVAL;
 
603
 
 
604
        if (strcmp(argv[idx], "--synctime") == 0) {
 
605
                synctime = (time_t) atol(argv[idx + 1]);
 
606
                idx += 2;
 
607
        }
 
608
        if (idx < argc && strcmp(argv[idx], "--verbose") == 0) {
 
609
                verbose = 1;
 
610
                idx++;
 
611
        }
 
612
 
 
613
        if (idx < argc)
 
614
                datafile = argv[idx++];
 
615
        if (idx < argc)
 
616
                loops = atoi(argv[idx++]);
 
617
 
 
618
        if (!datafile || !loops)
 
619
                return -EINVAL;
 
620
 
 
621
        if (verbose)
 
622
                fprintf(stderr, "%d: start: synctime=%u, datafile=%s, loops=%d\n",
 
623
                         getpid(), (int) synctime, datafile, loops);
 
624
 
 
625
        atexit(clean_lock);
 
626
 
 
627
        /* be paranoid and call exit() (=clean_lock()) for all signals */
 
628
        {
 
629
                int sig = 0;
 
630
                struct sigaction sa;
 
631
 
 
632
                sa.sa_handler = sig_handler;
 
633
                sa.sa_flags = 0;
 
634
                sigfillset(&sa.sa_mask);
 
635
 
 
636
                while (sigismember(&sa.sa_mask, ++sig) != -1 && sig != SIGCHLD)
 
637
                        sigaction (sig, &sa, (struct sigaction *) 0);
 
638
        }
 
639
 
 
640
        /* start the test in exactly defined time */
 
641
        if (synctime) {
 
642
                gettimeofday(&tv, NULL);
 
643
                if (synctime && synctime - tv.tv_sec > 1) {
 
644
                        usecs = ((synctime - tv.tv_sec) * 1000000UL) -
 
645
                                                (1000000UL - tv.tv_usec);
 
646
                        usleep(usecs);
 
647
                }
 
648
        }
 
649
 
 
650
        for (l = 0; l < loops; l++) {
 
651
                lock = mnt_new_lock(datafile, 0);
 
652
                if (!lock)
 
653
                        return -1;
 
654
 
 
655
                if (mnt_lock_file(lock) != 0) {
 
656
                        fprintf(stderr, "%d: failed to lock %s file\n",
 
657
                                        getpid(), datafile);
 
658
                        return -1;
 
659
                }
 
660
 
 
661
                increment_data(datafile, verbose, l);
 
662
 
 
663
                mnt_unlock_file(lock);
 
664
                mnt_free_lock(lock);
 
665
                lock = NULL;
 
666
 
 
667
                /* The mount command usually finish after mtab update. We
 
668
                 * simulate this via short sleep -- it's also enough to make
 
669
                 * concurrent processes happy.
 
670
                 */
 
671
                if (synctime)
 
672
                        usleep(25000);
 
673
        }
 
674
 
 
675
        return 0;
 
676
}
 
677
 
 
678
/*
 
679
 * Note that this test should be executed from a script that creates many
 
680
 * parallel processes, otherwise this test does not make sense.
 
681
 */
 
682
int main(int argc, char *argv[])
 
683
{
 
684
        struct libmnt_test tss[] = {
 
685
        { "--lock", test_lock,  " [--synctime <time_t>] [--verbose] <datafile> <loops> "
 
686
                                "increment a number in datafile" },
 
687
        { NULL }
 
688
        };
 
689
 
 
690
        return mnt_run_test(tss, argc, argv);
 
691
}
 
692
 
 
693
#endif /* TEST_PROGRAM */