~ubuntu-branches/ubuntu/precise/autofs5/precise

« back to all changes in this revision

Viewing changes to .pc/autofs-5.0.5-fix-null-cache-race.patch/daemon/automount.c

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2011-07-03 14:35:46 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110703143546-nej26krjij0rf792
Tags: 5.0.6-0ubuntu1
* New upstream release:
  - Dropped upstream patches 
  - Refreshed debian/patches/17ld.patch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* ----------------------------------------------------------------------- *
2
 
 *
3
 
 *  automount.c - Linux automounter daemon
4
 
 *   
5
 
 *   Copyright 1997 Transmeta Corporation - All Rights Reserved
6
 
 *   Copyright 1999-2000 Jeremy Fitzhardinge <jeremy@goop.org>
7
 
 *   Copyright 2001-2005 Ian Kent <raven@themaw.net>
8
 
 *
9
 
 *   This program is free software; you can redistribute it and/or modify
10
 
 *   it under the terms of the GNU General Public License as published by
11
 
 *   the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
12
 
 *   USA; either version 2 of the License, or (at your option) any later
13
 
 *   version.
14
 
 *   
15
 
 *   This program is distributed in the hope that it will be useful,
16
 
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 
 *   GNU General Public License for more details.
19
 
 *
20
 
 * ----------------------------------------------------------------------- */
21
 
 
22
 
#include <dirent.h>
23
 
#include <getopt.h>
24
 
#include <signal.h>
25
 
#include <stdio.h>
26
 
#include <stdlib.h>
27
 
#include <string.h>
28
 
#include <sys/ioctl.h>
29
 
#include <ctype.h>
30
 
#include <sys/types.h>
31
 
#include <sys/wait.h>
32
 
#include <sys/stat.h>
33
 
#include <sys/time.h>
34
 
#include <sys/resource.h>
35
 
#include <sys/poll.h>
36
 
#include <dirent.h>
37
 
#include <sys/vfs.h>
38
 
#include <sys/utsname.h>
39
 
 
40
 
#include "automount.h"
41
 
#if defined(LIBXML2_WORKAROUND) || defined(TIRPC_WORKAROUND)
42
 
#include <dlfcn.h>
43
 
#ifdef WITH_LDAP
44
 
#include <libxml/parser.h>
45
 
#endif
46
 
#endif
47
 
 
48
 
const char *program;            /* Initialized with argv[0] */
49
 
const char *version = VERSION_STRING;   /* Program version */
50
 
const char *libdir = AUTOFS_LIB_DIR;    /* Location of library modules */
51
 
const char *mapdir = AUTOFS_MAP_DIR;    /* Location of mount maps */
52
 
const char *confdir = AUTOFS_CONF_DIR;  /* Location of autofs config file */
53
 
 
54
 
/* autofs fifo name prefix */
55
 
const char *fifodir = AUTOFS_FIFO_DIR "/autofs.fifo";
56
 
 
57
 
const char *global_options;             /* Global option, from command line */
58
 
 
59
 
static char *pid_file = NULL;           /* File in which to keep pid */
60
 
unsigned int global_random_selection;   /* use random policy when selecting
61
 
                                         * which multi-mount host to mount */
62
 
long global_negative_timeout = -1;
63
 
int do_force_unlink = 0;                /* Forceably unlink mount tree at startup */
64
 
 
65
 
static int start_pipefd[2];
66
 
static int st_stat = 1;
67
 
static int *pst_stat = &st_stat;
68
 
static pthread_t state_mach_thid;
69
 
 
70
 
static sigset_t block_sigs;
71
 
 
72
 
/* Pre-calculated kernel packet length */
73
 
static size_t kpkt_len;
74
 
 
75
 
/* Does kernel know about SOCK_CLOEXEC and friends */
76
 
static int cloexec_works = 0;
77
 
 
78
 
/* Attributes for creating detached and joinable threads */
79
 
pthread_attr_t th_attr;
80
 
pthread_attr_t th_attr_detached;
81
 
 
82
 
struct master_readmap_cond mrc = {
83
 
        PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, 0, NULL, 0, 0, 0, 0};
84
 
 
85
 
struct startup_cond suc = {
86
 
        PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, 0, 0};
87
 
 
88
 
pthread_key_t key_thread_stdenv_vars;
89
 
 
90
 
#define MAX_OPEN_FILES          10240
91
 
 
92
 
int aquire_flag_file(void);
93
 
void release_flag_file(void);
94
 
static int umount_all(struct autofs_point *ap, int force);
95
 
 
96
 
extern struct master *master_list;
97
 
 
98
 
static int do_mkdir(const char *parent, const char *path, mode_t mode)
99
 
{
100
 
        int status;
101
 
        struct stat st;
102
 
        struct statfs fs;
103
 
 
104
 
        /* If path exists we're done */
105
 
        status = stat(path, &st);
106
 
        if (status == 0) {
107
 
                if (!S_ISDIR(st.st_mode))
108
 
                        errno = ENOTDIR;
109
 
                errno = EEXIST;
110
 
                return 0;
111
 
        }
112
 
 
113
 
        /*
114
 
         * If we're trying to create a directory within an autofs fs
115
 
         * or the path is contained in a localy mounted fs go ahead.
116
 
         */
117
 
        status = -1;
118
 
        if (*parent)
119
 
                status = statfs(parent, &fs);
120
 
        if ((status != -1 && fs.f_type == (__SWORD_TYPE) AUTOFS_SUPER_MAGIC) ||
121
 
            contained_in_local_fs(path)) {
122
 
                if (mkdir(path, mode) == -1) {
123
 
                        errno = EACCES;
124
 
                        return 0;
125
 
                }
126
 
                return 1;
127
 
        }
128
 
 
129
 
        errno = EACCES;
130
 
        return 0;
131
 
}
132
 
 
133
 
int mkdir_path(const char *path, mode_t mode)
134
 
{
135
 
        char buf[PATH_MAX];
136
 
        char parent[PATH_MAX];
137
 
        const char *cp = path, *lcp = path;
138
 
        char *bp = buf, *pp = parent;
139
 
 
140
 
        *parent = '\0';
141
 
 
142
 
        do {
143
 
                if (cp != path && (*cp == '/' || *cp == '\0')) {
144
 
                        memcpy(bp, lcp, cp - lcp);
145
 
                        bp += cp - lcp;
146
 
                        *bp = '\0';
147
 
                        if (!do_mkdir(parent, buf, mode)) {
148
 
                                if (*cp != '\0') {
149
 
                                        memcpy(pp, lcp, cp - lcp);
150
 
                                        pp += cp - lcp;
151
 
                                        *pp = '\0';
152
 
                                        lcp = cp;
153
 
                                        continue;
154
 
                                }
155
 
                                return -1;
156
 
                        }
157
 
                        memcpy(pp, lcp, cp - lcp);
158
 
                        pp += cp - lcp;
159
 
                        *pp = '\0';
160
 
                        lcp = cp;
161
 
                }
162
 
        } while (*cp++ != '\0');
163
 
 
164
 
        return 0;
165
 
}
166
 
 
167
 
/* Remove as much as possible of a path */
168
 
int rmdir_path(struct autofs_point *ap, const char *path, dev_t dev)
169
 
{
170
 
        int len = strlen(path);
171
 
        char buf[PATH_MAX];
172
 
        char *cp;
173
 
        int first = 1;
174
 
        struct stat st;
175
 
        struct statfs fs;
176
 
 
177
 
        strcpy(buf, path);
178
 
        cp = buf + len;
179
 
 
180
 
        do {
181
 
                *cp = '\0';
182
 
 
183
 
                /*
184
 
                 *  Before removing anything, perform some sanity checks to
185
 
                 *  ensure that we are looking at files in the automount
186
 
                 *  file system.
187
 
                 */
188
 
                memset(&st, 0, sizeof(st));
189
 
                if (lstat(buf, &st) != 0) {
190
 
                        crit(ap->logopt, "lstat of %s failed", buf);
191
 
                        return -1;
192
 
                }
193
 
 
194
 
                /* Termination condition removing full path within autofs fs */
195
 
                if (st.st_dev != dev)
196
 
                        return 0;
197
 
 
198
 
                if (statfs(buf, &fs) != 0) {
199
 
                        error(ap->logopt, "could not stat fs of %s", buf);
200
 
                        return -1;
201
 
                }
202
 
 
203
 
                if (fs.f_type != (__SWORD_TYPE) AUTOFS_SUPER_MAGIC) {
204
 
                        crit(ap->logopt, "attempt to remove directory from a "
205
 
                             "non-autofs filesystem!");
206
 
                        crit(ap->logopt,
207
 
                             "requestor dev == %llu, \"%s\" owner dev == %llu",
208
 
                             dev, buf, st.st_dev);
209
 
                        return -1;
210
 
                }
211
 
                             
212
 
                /*
213
 
                 * Last element of path may be a symbolic link; all others
214
 
                 * are directories (and the last directory element is
215
 
                 * processed first, hence the variable name)
216
 
                 */
217
 
                if (rmdir(buf) == -1) {
218
 
                        if (first && errno == ENOTDIR) {
219
 
                                /*
220
 
                                 *  Ensure that we will only remove
221
 
                                 *  symbolic links.
222
 
                                 */
223
 
                                if (S_ISLNK(st.st_mode)) {
224
 
                                        if (unlink(buf) == -1)
225
 
                                                return -1;
226
 
                                } else {
227
 
                                        crit(ap->logopt,
228
 
                                           "file \"%s\" is neither a directory"
229
 
                                           " nor a symbolic link. mode %d",
230
 
                                           buf, st.st_mode);
231
 
                                        return -1;
232
 
                                }
233
 
                        }
234
 
 
235
 
                        /*
236
 
                         *  If we fail to remove a directory for any reason,
237
 
                         *  we need to return an error.
238
 
                         */
239
 
                        return -1;
240
 
                }
241
 
 
242
 
                first = 0;
243
 
        } while ((cp = strrchr(buf, '/')) != NULL && cp != buf);
244
 
 
245
 
        return 0;
246
 
}
247
 
 
248
 
/* Like ftw, except fn gets called twice: before a directory is
249
 
   entered, and after.  If the before call returns 0, the directory
250
 
   isn't entered. */
251
 
static int walk_tree(const char *base, int (*fn) (unsigned logopt,
252
 
                                                  const char *file,
253
 
                                                  const struct stat * st,
254
 
                                                  int, void *), int incl, unsigned logopt, void *arg)
255
 
{
256
 
        char buf[PATH_MAX + 1];
257
 
        struct stat st, *pst = &st;
258
 
        int ret;
259
 
 
260
 
        if (!is_mounted(_PATH_MOUNTED, base, MNTS_REAL))
261
 
                ret = lstat(base, pst);
262
 
        else {
263
 
                pst = NULL;
264
 
                ret = 0;
265
 
        }
266
 
 
267
 
        if (ret != -1 && (fn) (logopt, base, pst, 0, arg)) {
268
 
                if (S_ISDIR(st.st_mode)) {
269
 
                        struct dirent **de;
270
 
                        int n;
271
 
 
272
 
                        n = scandir(base, &de, 0, alphasort);
273
 
                        if (n < 0)
274
 
                                return -1;
275
 
 
276
 
                        while (n--) {
277
 
                                int ret, size;
278
 
 
279
 
                                if (strcmp(de[n]->d_name, ".") == 0 ||
280
 
                                    strcmp(de[n]->d_name, "..") == 0) {
281
 
                                        free(de[n]);
282
 
                                        continue;
283
 
                                }
284
 
 
285
 
                                size = sizeof(buf);
286
 
                                ret = cat_path(buf, size, base, de[n]->d_name);
287
 
                                if (!ret) {
288
 
                                        do {
289
 
                                                free(de[n]);
290
 
                                        } while (n--);
291
 
                                        free(de);
292
 
                                        return -1;
293
 
                                }
294
 
 
295
 
                                walk_tree(buf, fn, 1, logopt, arg);
296
 
                                free(de[n]);
297
 
                        }
298
 
                        free(de);
299
 
                }
300
 
                if (incl)
301
 
                        (fn) (logopt, base, pst, 1, arg);
302
 
        }
303
 
        return 0;
304
 
}
305
 
 
306
 
static int rm_unwanted_fn(unsigned logopt, const char *file, const struct stat *st, int when, void *arg)
307
 
{
308
 
        dev_t dev = *(dev_t *) arg;
309
 
        char buf[MAX_ERR_BUF];
310
 
        struct stat newst;
311
 
 
312
 
        if (!st)
313
 
                return 0;
314
 
 
315
 
        if (when == 0) {
316
 
                if (st->st_dev != dev)
317
 
                        return 0;
318
 
                return 1;
319
 
        }
320
 
 
321
 
        if (lstat(file, &newst)) {
322
 
                crit(logopt, "unable to stat file, possible race condition");
323
 
                return 0;
324
 
        }
325
 
 
326
 
        if (newst.st_dev != dev) {
327
 
                crit(logopt, "file %s has the wrong device, possible race condition",
328
 
                     file);
329
 
                return 0;
330
 
        }
331
 
 
332
 
        if (S_ISDIR(newst.st_mode)) {
333
 
                debug(logopt, "removing directory %s", file);
334
 
                if (rmdir(file)) {
335
 
                        char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
336
 
                        warn(logopt,
337
 
                              "unable to remove directory %s: %s", file, estr);
338
 
                        return 0;
339
 
                }
340
 
        } else if (S_ISREG(newst.st_mode)) {
341
 
                crit(logopt, "attempting to remove files from a mounted "
342
 
                     "directory. file %s", file);
343
 
                return 0;
344
 
        } else if (S_ISLNK(newst.st_mode)) {
345
 
                debug(logopt, "removing symlink %s", file);
346
 
                unlink(file);
347
 
        }
348
 
        return 1;
349
 
}
350
 
 
351
 
void rm_unwanted(unsigned logopt, const char *path, int incl, dev_t dev)
352
 
{
353
 
        walk_tree(path, rm_unwanted_fn, incl, logopt, &dev);
354
 
}
355
 
 
356
 
struct counter_args {
357
 
        unsigned int count;
358
 
        dev_t dev;
359
 
};
360
 
 
361
 
static int counter_fn(unsigned logopt, const char *file, const struct stat *st, int when, void *arg)
362
 
{
363
 
        struct counter_args *counter = (struct counter_args *) arg;
364
 
 
365
 
        if (!st || (S_ISLNK(st->st_mode) || (S_ISDIR(st->st_mode)
366
 
                && st->st_dev != counter->dev))) {
367
 
                counter->count++;
368
 
                return 0;
369
 
        }
370
 
 
371
 
        return 1;
372
 
}
373
 
 
374
 
/* Count mounted filesystems and symlinks */
375
 
int count_mounts(unsigned logopt, const char *path, dev_t dev)
376
 
{
377
 
        struct counter_args counter;
378
 
 
379
 
        counter.count = 0;
380
 
        counter.dev = dev;
381
 
        
382
 
        if (walk_tree(path, counter_fn, 0, logopt, &counter) == -1)
383
 
                return -1;
384
 
 
385
 
        return counter.count;
386
 
}
387
 
 
388
 
static void check_rm_dirs(struct autofs_point *ap, const char *path, int incl)
389
 
{
390
 
        /*
391
 
         * If we're a submount the kernel can't know we're trying to
392
 
         * shutdown and so cannot block processes walking into the
393
 
         * mount point directory. If this is the call to umount_multi()
394
 
         * made during shutdown (incl == 0) we have to leave any mount
395
 
         * point directories in place so we can recover if needed. The
396
 
         * umount itself will clean these directories up for us
397
 
         * automagically.
398
 
         */
399
 
        if (!incl && ap->submount)
400
 
                return;
401
 
 
402
 
        if ((!(ap->flags & MOUNT_FLAG_GHOST)) ||
403
 
            (ap->state == ST_SHUTDOWN_PENDING ||
404
 
             ap->state == ST_SHUTDOWN_FORCE ||
405
 
             ap->state == ST_SHUTDOWN))
406
 
                rm_unwanted(ap->logopt, path, incl, ap->dev);
407
 
        else if ((ap->flags & MOUNT_FLAG_GHOST) && (ap->type == LKP_INDIRECT))
408
 
                rm_unwanted(ap->logopt, path, 0, ap->dev);
409
 
}
410
 
 
411
 
/* Try to purge cache entries kept around due to existing mounts */
412
 
static void update_map_cache(struct autofs_point *ap, const char *path)
413
 
{
414
 
        struct map_source *map;
415
 
        struct mapent_cache *mc;
416
 
        const char *key;
417
 
 
418
 
        if (ap->type == LKP_INDIRECT)
419
 
                key = strrchr(path, '/') + 1;
420
 
        else
421
 
                key = path;
422
 
 
423
 
        map = ap->entry->maps;
424
 
        while (map) {
425
 
                struct mapent *me = NULL;
426
 
 
427
 
                /* Skip current, in-use cache */
428
 
                if (ap->entry->age <= map->age) {
429
 
                        map = map->next;
430
 
                        continue;
431
 
                }
432
 
 
433
 
                mc = map->mc;
434
 
                /* If the lock is busy try later */
435
 
                if (cache_try_writelock(mc)) {
436
 
                        me = cache_lookup_distinct(mc, key);
437
 
                        if (me && me->ioctlfd == -1)
438
 
                                cache_delete(mc, key);
439
 
                        cache_unlock(mc);
440
 
                }
441
 
 
442
 
                map = map->next;
443
 
        }
444
 
 
445
 
        return;
446
 
}
447
 
 
448
 
static int umount_subtree_mounts(struct autofs_point *ap, const char *path, unsigned int is_autofs_fs)
449
 
{
450
 
        struct mapent_cache *mc;
451
 
        struct mapent *me;
452
 
        unsigned int is_mm_root;
453
 
        int left;
454
 
 
455
 
        me = lookup_source_mapent(ap, path, LKP_DISTINCT);
456
 
        if (!me) {
457
 
                char *ind_key;
458
 
 
459
 
                ind_key = strrchr(path, '/');
460
 
                if (ind_key)
461
 
                        ind_key++;
462
 
 
463
 
                me = lookup_source_mapent(ap, ind_key, LKP_NORMAL);
464
 
                if (!me)
465
 
                        return 0;
466
 
        }
467
 
 
468
 
        mc = me->mc;
469
 
        is_mm_root = (me->multi == me);
470
 
 
471
 
        left = 0;
472
 
 
473
 
        pthread_cleanup_push(cache_lock_cleanup, mc);
474
 
 
475
 
        if (me->multi) {
476
 
                char root[PATH_MAX];
477
 
                char *base;
478
 
                int cur_state;
479
 
 
480
 
                if (!strchr(me->multi->key, '/'))
481
 
                        /* Indirect multi-mount root */
482
 
                        /* sprintf okay - if it's mounted, it's
483
 
                         * PATH_MAX or less bytes */
484
 
                        sprintf(root, "%s/%s", ap->path, me->multi->key);
485
 
                else
486
 
                        strcpy(root, me->multi->key);
487
 
 
488
 
                if (is_mm_root)
489
 
                        base = NULL;
490
 
                else
491
 
                        base = me->key + strlen(root);
492
 
 
493
 
                pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cur_state);
494
 
                /* Lock the closest parent nesting point for umount */
495
 
                cache_multi_writelock(me->parent);
496
 
                if (umount_multi_triggers(ap, me, root, base)) {
497
 
                        warn(ap->logopt,
498
 
                             "some offset mounts still present under %s", path);
499
 
                        left++;
500
 
                }
501
 
                cache_multi_unlock(me->parent);
502
 
                pthread_setcancelstate(cur_state, NULL);
503
 
        }
504
 
 
505
 
        pthread_cleanup_pop(1);
506
 
 
507
 
        if (left || is_autofs_fs)
508
 
                return left;
509
 
 
510
 
        /*
511
 
         * If this is the root of a multi-mount we've had to umount
512
 
         * it already to ensure it's ok to remove any offset triggers.
513
 
         */
514
 
        if (!is_mm_root && is_mounted(_PATH_MOUNTED, path, MNTS_REAL)) {
515
 
                info(ap->logopt, "unmounting dir = %s", path);
516
 
                if (umount_ent(ap, path)) {
517
 
                        warn(ap->logopt, "could not umount dir %s", path);
518
 
                        left++;
519
 
                }
520
 
        }
521
 
 
522
 
        return left;
523
 
}
524
 
 
525
 
/* umount all filesystems mounted under path.  If incl is true, then
526
 
   it also tries to umount path itself */
527
 
int umount_multi(struct autofs_point *ap, const char *path, int incl)
528
 
{
529
 
        int is_autofs_fs;
530
 
        int left;
531
 
 
532
 
        debug(ap->logopt, "path %s incl %d", path, incl);
533
 
 
534
 
        is_autofs_fs = 0;
535
 
        if (master_find_submount(ap, path))
536
 
                is_autofs_fs = 1;
537
 
 
538
 
        left = 0;
539
 
 
540
 
        /*
541
 
         * If we are a submount we need to umount any offsets our
542
 
         * parent may have mounted over top of us.
543
 
         */
544
 
        /*if (ap->submount)
545
 
                left += umount_subtree_mounts(ap->parent, path, is_autofs_fs);*/
546
 
 
547
 
        left += umount_subtree_mounts(ap, path, is_autofs_fs);
548
 
 
549
 
        /* Delete detritus like unwanted mountpoints and symlinks */
550
 
        if (left == 0 && ap->state != ST_READMAP) {
551
 
                update_map_cache(ap, path);
552
 
                check_rm_dirs(ap, path, incl);
553
 
        }
554
 
 
555
 
        return left;
556
 
}
557
 
 
558
 
static int umount_all(struct autofs_point *ap, int force)
559
 
{
560
 
        int left;
561
 
 
562
 
        left = umount_multi(ap, ap->path, 0);
563
 
        if (force && left)
564
 
                warn(ap->logopt, "could not unmount %d dirs under %s",
565
 
                     left, ap->path);
566
 
 
567
 
        return left;
568
 
}
569
 
 
570
 
int umount_autofs(struct autofs_point *ap, const char *root, int force)
571
 
{
572
 
        int ret = 0;
573
 
 
574
 
        if (ap->state == ST_INIT)
575
 
                return -1;
576
 
 
577
 
        /*
578
 
         * Since lookup.c is lazy about closing lookup modules
579
 
         * to prevent unneeded opens, we need to clean them up
580
 
         * before umount.
581
 
         */
582
 
        lookup_close_lookup(ap);
583
 
 
584
 
        if (ap->type == LKP_INDIRECT) {
585
 
                if (umount_all(ap, force) && !force)
586
 
                        return -1;
587
 
                ret = umount_autofs_indirect(ap, root);
588
 
        } else
589
 
                ret = umount_autofs_direct(ap);
590
 
 
591
 
        return ret;
592
 
}
593
 
 
594
 
static size_t get_kpkt_len(void)
595
 
{
596
 
        size_t pkt_len = sizeof(struct autofs_v5_packet);
597
 
        struct utsname un;
598
 
 
599
 
        uname(&un);
600
 
 
601
 
        if (pkt_len % 8) {
602
 
                if (strcmp(un.machine, "alpha") == 0 ||
603
 
                    strcmp(un.machine, "ia64") == 0 ||
604
 
                    strcmp(un.machine, "x86_64") == 0 ||
605
 
                    strcmp(un.machine, "ppc64") == 0)
606
 
                        pkt_len += 4;
607
 
 
608
 
        }
609
 
 
610
 
        return pkt_len;
611
 
}
612
 
 
613
 
static int fullread(int fd, void *ptr, size_t len)
614
 
{
615
 
        char *buf = (char *) ptr;
616
 
 
617
 
        while (len > 0) {
618
 
                ssize_t r = read(fd, buf, len);
619
 
 
620
 
                if (r == -1) {
621
 
                        if (errno == EINTR)
622
 
                                continue;
623
 
                        break;
624
 
                }
625
 
 
626
 
                buf += r;
627
 
                len -= r;
628
 
        }
629
 
 
630
 
        return len;
631
 
}
632
 
 
633
 
static char *automount_path_to_fifo(unsigned logopt, const char *path)
634
 
{
635
 
        char *fifo_name, *p;
636
 
        int  name_len = strlen(path) + strlen(fifodir) + 1;
637
 
        int ret;
638
 
 
639
 
        fifo_name = malloc(name_len);
640
 
        if (!fifo_name)
641
 
                return NULL;
642
 
        ret = snprintf(fifo_name, name_len, "%s%s", fifodir, path);
643
 
        if (ret >= name_len) {
644
 
                info(logopt,
645
 
                     "fifo path for \"%s\" truncated to \"%s\".  This may "
646
 
                     "lead to --set-log-priority commands being sent to the "
647
 
                     "wrong automount daemon.", path, fifo_name);
648
 
        }
649
 
 
650
 
        /*
651
 
         *  An automount path can be made up of subdirectories.  So, to
652
 
         *  create the fifo name, we will just replace instances of '/' with
653
 
         *  '-'. 
654
 
         */
655
 
        p = fifo_name + strlen(fifodir);
656
 
        while (*p != '\0') {
657
 
                if (*p == '/')
658
 
                        *p = '-';
659
 
                p++;
660
 
        }
661
 
 
662
 
        debug(logopt, "fifo name %s",fifo_name);
663
 
 
664
 
        return fifo_name;
665
 
}
666
 
 
667
 
static int create_logpri_fifo(struct autofs_point *ap)
668
 
{
669
 
        int ret = -1;
670
 
        int fd;
671
 
        char *fifo_name;
672
 
        char buf[MAX_ERR_BUF];
673
 
 
674
 
        fifo_name = automount_path_to_fifo(ap->logopt, ap->path);
675
 
        if (!fifo_name) {
676
 
                crit(ap->logopt, "Failed to allocate memory!");
677
 
                goto out_free; /* free(NULL) is okay */
678
 
        }
679
 
 
680
 
        ret = unlink(fifo_name);
681
 
        if (ret != 0 && errno != ENOENT) {
682
 
                crit(ap->logopt,
683
 
                     "Failed to unlink FIFO. Is the automount daemon "
684
 
                     "already running?");
685
 
                goto out_free;
686
 
        }
687
 
 
688
 
        ret = mkfifo(fifo_name, S_IRUSR|S_IWUSR);
689
 
        if (ret != 0) {
690
 
                char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
691
 
                crit(ap->logopt,
692
 
                     "mkfifo for %s failed: %s", fifo_name, estr);
693
 
                goto out_free;
694
 
        }
695
 
 
696
 
        fd = open_fd(fifo_name, O_RDWR|O_NONBLOCK);
697
 
        if (fd < 0) {
698
 
                char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
699
 
                crit(ap->logopt,
700
 
                     "Failed to open %s: %s", fifo_name, estr);
701
 
                unlink(fifo_name);
702
 
                ret = -1;
703
 
                goto out_free;
704
 
        }
705
 
 
706
 
        ap->logpri_fifo = fd;
707
 
 
708
 
out_free:
709
 
        free(fifo_name);
710
 
        return ret;
711
 
}
712
 
 
713
 
int destroy_logpri_fifo(struct autofs_point *ap)
714
 
{
715
 
        int ret = -1;
716
 
        int fd = ap->logpri_fifo;
717
 
        char *fifo_name;
718
 
        char buf[MAX_ERR_BUF];
719
 
 
720
 
        if (fd == -1)
721
 
                return 0;
722
 
 
723
 
        fifo_name = automount_path_to_fifo(ap->logopt, ap->path);
724
 
        if (!fifo_name) {
725
 
                crit(ap->logopt, "Failed to allocate memory!");
726
 
                goto out_free; /* free(NULL) is okay */
727
 
        }
728
 
 
729
 
        ap->logpri_fifo = -1;
730
 
 
731
 
        ret = close(fd);
732
 
        if (ret != 0) {
733
 
                char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
734
 
                warn(ap->logopt,
735
 
                     "close for fifo %s: %s", fifo_name, estr);
736
 
        }
737
 
 
738
 
        ret = unlink(fifo_name);
739
 
        if (ret != 0) {
740
 
                warn(ap->logopt,
741
 
                     "Failed to unlink FIFO. Was the fifo created OK?");
742
 
        }
743
 
 
744
 
out_free:
745
 
        free(fifo_name);
746
 
        return ret;
747
 
}
748
 
 
749
 
static void handle_fifo_message(struct autofs_point *ap, int fd)
750
 
{
751
 
        int ret;
752
 
        char buffer[PIPE_BUF];
753
 
        char *end;
754
 
        long pri;
755
 
        char buf[MAX_ERR_BUF];
756
 
 
757
 
        memset(buffer, 0, sizeof(buffer));
758
 
        ret = read(fd, &buffer, sizeof(buffer));
759
 
        if (ret < 0) {
760
 
                char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
761
 
                warn(ap->logopt, "read on fifo returned error: %s", estr);
762
 
                return;
763
 
        }
764
 
 
765
 
        if (ret != 2) {
766
 
                debug(ap->logopt, "expected 2 bytes, received %d.", ret);
767
 
                return;
768
 
        }
769
 
 
770
 
        errno = 0;
771
 
        pri = strtol(buffer, &end, 10);
772
 
        if ((pri == LONG_MIN || pri == LONG_MAX) && errno == ERANGE) {
773
 
                debug(ap->logopt, "strtol reported an %s.  Failed to set "
774
 
                      "log priority.", pri == LONG_MIN ? "underflow" : "overflow");
775
 
                return;
776
 
        }
777
 
        if ((pri == 0 && errno == EINVAL) || end == buffer) {
778
 
                debug(ap->logopt, "priority is expected to be an integer "
779
 
                      "in the range 0-7 inclusive.");
780
 
                return;
781
 
        }
782
 
 
783
 
        if (pri > LOG_DEBUG || pri < LOG_EMERG) {
784
 
                debug(ap->logopt, "invalid log priority (%ld) received "
785
 
                      "on fifo", pri);
786
 
                return;
787
 
        }
788
 
 
789
 
        /*
790
 
         * OK, the message passed all of the sanity checks.  The
791
 
         * automounter actually only supports three log priorities.
792
 
         * Everything is logged at log level debug, deamon messages
793
 
         * and everything except debug messages are logged with the
794
 
         * verbose setting and only error and critical messages are
795
 
         * logged when debugging isn't enabled.
796
 
         */
797
 
        if (pri >= LOG_WARNING) {
798
 
                if (pri == LOG_DEBUG) {
799
 
                        set_log_debug_ap(ap);
800
 
                        info(ap->logopt, "Debug logging set for %s", ap->path);
801
 
                } else {
802
 
                        set_log_verbose_ap(ap);
803
 
                        info(ap->logopt, "Verbose logging set for %s", ap->path);
804
 
                }
805
 
        } else {
806
 
                if (ap->logopt & LOGOPT_ANY)
807
 
                        info(ap->logopt, "Basic logging set for %s", ap->path);
808
 
                set_log_norm_ap(ap);
809
 
        }
810
 
}
811
 
 
812
 
static int set_log_priority(const char *path, int priority)
813
 
{
814
 
        int fd;
815
 
        char *fifo_name;
816
 
        char buf[2];
817
 
 
818
 
        if (priority > LOG_DEBUG || priority < LOG_EMERG) {
819
 
                fprintf(stderr, "Log priority %d is invalid.\n", priority);
820
 
                fprintf(stderr, "Please spcify a number in the range 0-7.\n");
821
 
                return -1;
822
 
        }
823
 
 
824
 
        /*
825
 
         * This is an ascii based protocol, so we want the string
826
 
         * representation of the integer log priority.
827
 
         */
828
 
        snprintf(buf, sizeof(buf), "%d", priority);
829
 
 
830
 
        fifo_name = automount_path_to_fifo(LOGOPT_NONE, path);
831
 
        if (!fifo_name) {
832
 
                fprintf(stderr, "%s: Failed to allocate memory!\n",
833
 
                        __FUNCTION__);
834
 
                return -1;
835
 
        }
836
 
 
837
 
        /*
838
 
         * Specify O_NONBLOCK so that the open will fail if there is no
839
 
         * daemon reading from the other side of the FIFO.
840
 
         */
841
 
        fd = open(fifo_name, O_WRONLY|O_NONBLOCK);
842
 
        if (fd < 0) {
843
 
                fprintf(stderr, "%s: open of %s failed with %s\n",
844
 
                        __FUNCTION__, fifo_name, strerror(errno));
845
 
                fprintf(stderr, "%s: perhaps the fifo wasn't setup,"
846
 
                        " please check your log for more information\n", __FUNCTION__);
847
 
                free(fifo_name);
848
 
                return -1;
849
 
        }
850
 
 
851
 
        if (write(fd, buf, sizeof(buf)) != sizeof(buf)) {
852
 
                fprintf(stderr, "Failed to change logging priority.  ");
853
 
                fprintf(stderr, "write to fifo failed: %s.\n",
854
 
                        strerror(errno));
855
 
                close(fd);
856
 
                free(fifo_name);
857
 
                return -1;
858
 
        }
859
 
        close(fd);
860
 
        free(fifo_name);
861
 
        fprintf(stdout, "Successfully set log priority for %s.\n", path);
862
 
 
863
 
        return 0;
864
 
}
865
 
 
866
 
static int get_pkt(struct autofs_point *ap, union autofs_v5_packet_union *pkt)
867
 
{
868
 
        struct pollfd fds[3];
869
 
        int pollfds = 3;
870
 
        char buf[MAX_ERR_BUF];
871
 
 
872
 
        fds[0].fd = ap->pipefd;
873
 
        fds[0].events = POLLIN;
874
 
        fds[1].fd = ap->state_pipe[0];
875
 
        fds[1].events = POLLIN;
876
 
        fds[2].fd = ap->logpri_fifo;
877
 
        fds[2].events = POLLIN;
878
 
        if (fds[2].fd  == -1)
879
 
                pollfds--;
880
 
 
881
 
        for (;;) {
882
 
                if (poll(fds, pollfds, -1) == -1) {
883
 
                        char *estr;
884
 
                        if (errno == EINTR)
885
 
                                continue;
886
 
                        estr = strerror_r(errno, buf, MAX_ERR_BUF);
887
 
                        logerr("poll failed: %s", estr);
888
 
                        return -1;
889
 
                }
890
 
 
891
 
                if (fds[1].revents & POLLIN) {
892
 
                        enum states next_state;
893
 
                        size_t read_size = sizeof(next_state);
894
 
                        int state_pipe;
895
 
 
896
 
                        next_state = ST_INVAL;
897
 
 
898
 
                        st_mutex_lock();
899
 
 
900
 
                        state_pipe = ap->state_pipe[0];
901
 
 
902
 
                        if (fullread(state_pipe, &next_state, read_size)) {
903
 
                                st_mutex_unlock();
904
 
                                continue;
905
 
                        }
906
 
 
907
 
                        st_mutex_unlock();
908
 
 
909
 
                        if (next_state == ST_SHUTDOWN)
910
 
                                return -1;
911
 
                }
912
 
 
913
 
                if (fds[0].revents & POLLIN)
914
 
                        return fullread(ap->pipefd, pkt, kpkt_len);
915
 
 
916
 
                if (fds[2].fd != -1 && fds[2].revents & POLLIN) {
917
 
                        debug(ap->logopt, "message pending on control fifo.");
918
 
                        handle_fifo_message(ap, fds[2].fd);
919
 
                }
920
 
        }
921
 
}
922
 
 
923
 
int do_expire(struct autofs_point *ap, const char *name, int namelen)
924
 
{
925
 
        char buf[PATH_MAX];
926
 
        int len, ret;
927
 
 
928
 
        if (*name != '/') {
929
 
                len = ncat_path(buf, sizeof(buf), ap->path, name, namelen);
930
 
        } else {
931
 
                len = snprintf(buf, PATH_MAX, "%s", name);
932
 
                if (len >= PATH_MAX)
933
 
                        len = 0;
934
 
        }
935
 
 
936
 
        if (!len) {
937
 
                crit(ap->logopt, "path to long for buffer");
938
 
                return 1;
939
 
        }
940
 
 
941
 
        info(ap->logopt, "expiring path %s", buf);
942
 
 
943
 
        pthread_cleanup_push(master_source_lock_cleanup, ap->entry);
944
 
        master_source_readlock(ap->entry);
945
 
        ret = umount_multi(ap, buf, 1);
946
 
        if (ret == 0)
947
 
                info(ap->logopt, "expired %s", buf);
948
 
        else
949
 
                warn(ap->logopt, "couldn't complete expire of %s", buf);
950
 
        pthread_cleanup_pop(1);
951
 
 
952
 
        return ret;
953
 
}
954
 
 
955
 
static int autofs_init_ap(struct autofs_point *ap)
956
 
{
957
 
        int pipefd[2];
958
 
 
959
 
        if ((ap->state != ST_INIT)) {
960
 
                /* This can happen if an autofs process is already running*/
961
 
                error(ap->logopt, "bad state %d", ap->state);
962
 
                return -1;
963
 
        }
964
 
 
965
 
        ap->pipefd = ap->kpipefd = ap->ioctlfd = -1;
966
 
 
967
 
        /* Pipe for kernel communications */
968
 
        if (open_pipe(pipefd) < 0) {
969
 
                crit(ap->logopt,
970
 
                     "failed to create commumication pipe for autofs path %s",
971
 
                     ap->path);
972
 
                return -1;
973
 
        }
974
 
 
975
 
        ap->pipefd = pipefd[0];
976
 
        ap->kpipefd = pipefd[1];
977
 
 
978
 
        /* Pipe state changes from signal handler to main loop */
979
 
        if (open_pipe(ap->state_pipe) < 0) {
980
 
                crit(ap->logopt,
981
 
                     "failed create state pipe for autofs path %s", ap->path);
982
 
                close(ap->pipefd);
983
 
                close(ap->kpipefd);     /* Close kernel pipe end */
984
 
                return -1;
985
 
        }
986
 
 
987
 
        if (create_logpri_fifo(ap) < 0) {
988
 
                logmsg("could not create FIFO for path %s\n", ap->path);
989
 
                logmsg("dynamic log level changes not available for %s", ap->path);
990
 
        }
991
 
 
992
 
        return 0;
993
 
}
994
 
 
995
 
static int mount_autofs(struct autofs_point *ap, const char *root)
996
 
{
997
 
        int status = 0;
998
 
 
999
 
        if (autofs_init_ap(ap) != 0)
1000
 
                return -1;
1001
 
 
1002
 
        if (ap->type == LKP_DIRECT)
1003
 
                status = mount_autofs_direct(ap);
1004
 
        else
1005
 
                status = mount_autofs_indirect(ap, root);
1006
 
 
1007
 
        if (status < 0)
1008
 
                return -1;
1009
 
 
1010
 
        st_add_task(ap, ST_READY);
1011
 
 
1012
 
        return 0;
1013
 
}
1014
 
 
1015
 
static int handle_packet(struct autofs_point *ap)
1016
 
{
1017
 
        union autofs_v5_packet_union pkt;
1018
 
 
1019
 
        if (get_pkt(ap, &pkt))
1020
 
                return -1;
1021
 
 
1022
 
        debug(ap->logopt, "type = %d", pkt.hdr.type);
1023
 
 
1024
 
        switch (pkt.hdr.type) {
1025
 
        case autofs_ptype_missing_indirect:
1026
 
                return handle_packet_missing_indirect(ap, &pkt.v5_packet);
1027
 
 
1028
 
        case autofs_ptype_missing_direct:
1029
 
                return handle_packet_missing_direct(ap, &pkt.v5_packet);
1030
 
 
1031
 
        case autofs_ptype_expire_indirect:
1032
 
                return handle_packet_expire_indirect(ap, &pkt.v5_packet);
1033
 
 
1034
 
        case autofs_ptype_expire_direct:
1035
 
                return handle_packet_expire_direct(ap, &pkt.v5_packet);
1036
 
        }
1037
 
        error(ap->logopt, "unknown packet type %d", pkt.hdr.type);
1038
 
        return -1;
1039
 
}
1040
 
 
1041
 
static void become_daemon(unsigned foreground, unsigned daemon_check)
1042
 
{
1043
 
        FILE *pidfp;
1044
 
        char buf[MAX_ERR_BUF];
1045
 
        int res;
1046
 
        pid_t pid;
1047
 
 
1048
 
        /* Don't BUSY any directories unnecessarily */
1049
 
        if (chdir("/")) {
1050
 
                fprintf(stderr, "%s: failed change working directory.\n",
1051
 
                        program);
1052
 
                exit(0);
1053
 
        }
1054
 
 
1055
 
        if (open_pipe(start_pipefd) < 0) {
1056
 
                fprintf(stderr, "%s: failed to create start_pipefd.\n",
1057
 
                        program);
1058
 
                exit(0);
1059
 
        }
1060
 
 
1061
 
        /* Detach from foreground process */
1062
 
        if (foreground) {
1063
 
                if (daemon_check && !aquire_flag_file()) {
1064
 
                        fprintf(stderr, "%s: program is already running.\n",
1065
 
                                program);
1066
 
                        exit(1);
1067
 
                }
1068
 
                log_to_stderr();
1069
 
        } else {
1070
 
                pid = fork();
1071
 
                if (pid > 0) {
1072
 
                        close(start_pipefd[1]);
1073
 
                        res = read(start_pipefd[0], pst_stat, sizeof(*pst_stat));
1074
 
                        if (res < 0)
1075
 
                                exit(1);
1076
 
                        exit(*pst_stat);
1077
 
                } else if (pid < 0) {
1078
 
                        fprintf(stderr, "%s: Could not detach process\n",
1079
 
                                program);
1080
 
                        exit(1);
1081
 
                }
1082
 
                close(start_pipefd[0]);
1083
 
 
1084
 
                if (daemon_check && !aquire_flag_file()) {
1085
 
                        fprintf(stderr, "%s: program is already running.\n",
1086
 
                                program);
1087
 
                        /* Return success if already running */
1088
 
                        st_stat = 0;
1089
 
                        res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
1090
 
                        if (res < 0)
1091
 
                                exit(1);
1092
 
                        close(start_pipefd[1]);
1093
 
                        exit(*pst_stat);
1094
 
                }
1095
 
 
1096
 
                /*
1097
 
                 * Make our own process group for "magic" reason: processes that share
1098
 
                 * our pgrp see the raw filesystem behind the magic.
1099
 
                 */
1100
 
                if (setsid() == -1) {
1101
 
                        char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
1102
 
                        fprintf(stderr, "setsid: %s", estr);
1103
 
                        res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
1104
 
                        close(start_pipefd[1]);
1105
 
                        exit(*pst_stat);
1106
 
                }
1107
 
                log_to_syslog();
1108
 
        }
1109
 
 
1110
 
        /* Write pid file if requested */
1111
 
        if (pid_file) {
1112
 
                if ((pidfp = fopen(pid_file, "wt"))) {
1113
 
                        fprintf(pidfp, "%lu\n", (unsigned long) getpid());
1114
 
                        fclose(pidfp);
1115
 
                } else {
1116
 
                        char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
1117
 
                        logerr("failed to write pid file %s: %s",
1118
 
                               pid_file, estr);
1119
 
                        pid_file = NULL;
1120
 
                }
1121
 
        }
1122
 
}
1123
 
 
1124
 
static unsigned long getnumopt(char *str, char option)
1125
 
{
1126
 
        unsigned long val;
1127
 
        char *end;
1128
 
 
1129
 
        val = strtoul(str, &end, 0);
1130
 
        if (!*str || *end) {
1131
 
                fprintf(stderr,
1132
 
                        "%s: option -%c requires a numeric argument, got %s\n",
1133
 
                        program, option, str);
1134
 
                exit(1);
1135
 
        }
1136
 
        return val;
1137
 
}
1138
 
 
1139
 
static void do_master_cleanup_unlock(void *arg)
1140
 
{
1141
 
        int status;
1142
 
 
1143
 
        status = pthread_mutex_unlock(&mrc.mutex);
1144
 
        if (status)
1145
 
                fatal(status);
1146
 
 
1147
 
        return;
1148
 
}
1149
 
 
1150
 
static void *do_notify_state(void *arg)
1151
 
{
1152
 
        struct master *master;
1153
 
        int sig;
1154
 
        int status;
1155
 
 
1156
 
        sig = *(int *) arg;
1157
 
 
1158
 
        status = pthread_mutex_lock(&mrc.mutex);
1159
 
        if (status)
1160
 
                fatal(status);
1161
 
 
1162
 
        master = mrc.master;
1163
 
 
1164
 
        debug(master->logopt, "signal %d", sig);
1165
 
 
1166
 
        mrc.signaled = 1;
1167
 
        status = pthread_cond_signal(&mrc.cond);
1168
 
        if (status) {
1169
 
                error(master->logopt,
1170
 
                      "failed to signal state notify condition");
1171
 
                status = pthread_mutex_unlock(&mrc.mutex);
1172
 
                if (status)
1173
 
                        fatal(status);
1174
 
                pthread_exit(NULL);
1175
 
        }
1176
 
 
1177
 
        status = pthread_mutex_unlock(&mrc.mutex);
1178
 
        if (status)
1179
 
                fatal(status);
1180
 
 
1181
 
        master_notify_state_change(master, sig);
1182
 
 
1183
 
        return NULL;
1184
 
}
1185
 
 
1186
 
static pthread_t do_signals(struct master *master, int sig)
1187
 
{
1188
 
        pthread_t thid;
1189
 
        int r_sig = sig;
1190
 
        int status;
1191
 
 
1192
 
        status = pthread_mutex_lock(&mrc.mutex);
1193
 
        if (status)
1194
 
                fatal(status);
1195
 
 
1196
 
        status = pthread_create(&thid, &th_attr_detached, do_notify_state, &r_sig);
1197
 
        if (status) {
1198
 
                error(master->logopt,
1199
 
                      "mount state notify thread create failed");
1200
 
                status = pthread_mutex_unlock(&mrc.mutex);
1201
 
                if (status)
1202
 
                        fatal(status);
1203
 
                return 0;
1204
 
        }
1205
 
 
1206
 
        mrc.thid = thid;
1207
 
        mrc.master = master;
1208
 
 
1209
 
        pthread_cleanup_push(do_master_cleanup_unlock, NULL);
1210
 
 
1211
 
        mrc.signaled = 0;
1212
 
        while (!mrc.signaled) {
1213
 
                status = pthread_cond_wait(&mrc.cond, &mrc.mutex);
1214
 
                if (status)
1215
 
                        fatal(status);
1216
 
        }
1217
 
 
1218
 
        pthread_cleanup_pop(1);
1219
 
 
1220
 
        return thid;
1221
 
}
1222
 
 
1223
 
static void *do_read_master(void *arg)
1224
 
{
1225
 
        struct master *master;
1226
 
        unsigned int logopt;
1227
 
        time_t age;
1228
 
        int readall = 1;
1229
 
        int status;
1230
 
 
1231
 
        status = pthread_mutex_lock(&mrc.mutex);
1232
 
        if (status)
1233
 
                fatal(status);
1234
 
 
1235
 
        master = mrc.master;
1236
 
        age = mrc.age;
1237
 
        logopt = master->logopt;
1238
 
 
1239
 
        mrc.signaled = 1;
1240
 
        status = pthread_cond_signal(&mrc.cond);
1241
 
        if (status) {
1242
 
                error(logopt,
1243
 
                      "failed to signal master read map condition");
1244
 
                master->reading = 0;
1245
 
                status = pthread_mutex_unlock(&mrc.mutex);
1246
 
                if (status)
1247
 
                        fatal(status);
1248
 
                pthread_exit(NULL);
1249
 
        }
1250
 
 
1251
 
        status = pthread_mutex_unlock(&mrc.mutex);
1252
 
        if (status)
1253
 
                fatal(status);
1254
 
 
1255
 
        defaults_read_config(1);
1256
 
 
1257
 
        info(logopt, "re-reading master map %s", master->name);
1258
 
 
1259
 
        status = master_read_master(master, age, readall);
1260
 
 
1261
 
        master->reading = 0;
1262
 
 
1263
 
        return NULL;
1264
 
}
1265
 
 
1266
 
static int do_hup_signal(struct master *master, time_t age)
1267
 
{
1268
 
        unsigned int logopt = master->logopt;
1269
 
        pthread_t thid;
1270
 
        int status;
1271
 
 
1272
 
        status = pthread_mutex_lock(&mrc.mutex);
1273
 
        if (status)
1274
 
                fatal(status);
1275
 
 
1276
 
        if (master->reading) {
1277
 
                status = pthread_mutex_unlock(&mrc.mutex);
1278
 
                if (status)
1279
 
                        fatal(status);
1280
 
                return 1;
1281
 
        }
1282
 
 
1283
 
        master->reading = 1;
1284
 
 
1285
 
        status = pthread_create(&thid, &th_attr_detached, do_read_master, NULL);
1286
 
        if (status) {
1287
 
                error(logopt,
1288
 
                      "master read map thread create failed");
1289
 
                master->reading = 0;
1290
 
                status = pthread_mutex_unlock(&mrc.mutex);
1291
 
                if (status)
1292
 
                        fatal(status);
1293
 
                return 0;
1294
 
        }
1295
 
 
1296
 
        mrc.thid = thid;
1297
 
        mrc.master = master;
1298
 
        mrc.age = age;
1299
 
 
1300
 
        pthread_cleanup_push(do_master_cleanup_unlock, NULL);
1301
 
 
1302
 
        mrc.signaled = 0;
1303
 
        while (!mrc.signaled) {
1304
 
                status = pthread_cond_wait(&mrc.cond, &mrc.mutex);
1305
 
                if (status)
1306
 
                        fatal(status);
1307
 
        }
1308
 
 
1309
 
        pthread_cleanup_pop(1);
1310
 
 
1311
 
        return 1;
1312
 
}
1313
 
 
1314
 
/* Deal with all the signal-driven events in the state machine */
1315
 
static void *statemachine(void *arg)
1316
 
{
1317
 
        sigset_t signalset;
1318
 
        int sig;
1319
 
 
1320
 
        memcpy(&signalset, &block_sigs, sizeof(signalset));
1321
 
        sigdelset(&signalset, SIGCHLD);
1322
 
        sigdelset(&signalset, SIGCONT);
1323
 
 
1324
 
        while (1) {
1325
 
                sigwait(&signalset, &sig);
1326
 
 
1327
 
                switch (sig) {
1328
 
                case SIGTERM:
1329
 
                case SIGINT:
1330
 
                case SIGUSR2:
1331
 
                        master_mutex_lock();
1332
 
                        if (list_empty(&master_list->completed)) {
1333
 
                                if (list_empty(&master_list->mounts)) {
1334
 
                                        master_mutex_unlock();
1335
 
                                        return NULL;
1336
 
                                }
1337
 
                        } else {
1338
 
                                if (master_done(master_list)) {
1339
 
                                        master_mutex_unlock();
1340
 
                                        return NULL;
1341
 
                                }
1342
 
                                master_mutex_unlock();
1343
 
                                break;
1344
 
                        }
1345
 
                        master_mutex_unlock();
1346
 
 
1347
 
                case SIGUSR1:
1348
 
                        do_signals(master_list, sig);
1349
 
                        break;
1350
 
 
1351
 
                case SIGHUP:
1352
 
                        do_hup_signal(master_list, time(NULL));
1353
 
                        break;
1354
 
 
1355
 
                default:
1356
 
                        logerr("got unexpected signal %d!", sig);
1357
 
                        continue;
1358
 
                }
1359
 
        }
1360
 
}
1361
 
 
1362
 
static void return_start_status(void *arg)
1363
 
{
1364
 
        struct startup_cond *sc;
1365
 
        int status;
1366
 
 
1367
 
        sc = (struct startup_cond *) arg;
1368
 
 
1369
 
        sc->done = 1;
1370
 
 
1371
 
        /*
1372
 
         * Startup condition mutex must be locked during 
1373
 
         * the startup process.
1374
 
         */
1375
 
        status = pthread_cond_signal(&sc->cond);
1376
 
        if (status)
1377
 
                fatal(status);
1378
 
 
1379
 
        status = pthread_mutex_unlock(&sc->mutex);
1380
 
        if (status)
1381
 
                fatal(status);
1382
 
}
1383
 
 
1384
 
int handle_mounts_startup_cond_init(struct startup_cond *suc)
1385
 
{
1386
 
        int status;
1387
 
 
1388
 
        status = pthread_mutex_init(&suc->mutex, NULL);
1389
 
        if (status)
1390
 
                return status;
1391
 
 
1392
 
        status = pthread_cond_init(&suc->cond, NULL);
1393
 
        if (status) {
1394
 
                status = pthread_mutex_destroy(&suc->mutex);
1395
 
                if (status)
1396
 
                        fatal(status);
1397
 
                return status;
1398
 
        }
1399
 
 
1400
 
        status = pthread_mutex_lock(&suc->mutex);
1401
 
        if (status) {
1402
 
                status = pthread_mutex_destroy(&suc->mutex);
1403
 
                if (status)
1404
 
                        fatal(status);
1405
 
                status = pthread_cond_destroy(&suc->cond);
1406
 
                if (status)
1407
 
                        fatal(status);
1408
 
        }
1409
 
 
1410
 
        return 0;
1411
 
}
1412
 
 
1413
 
void handle_mounts_startup_cond_destroy(void *arg)
1414
 
{
1415
 
        struct startup_cond *suc = (struct startup_cond *) arg;
1416
 
        int status;
1417
 
 
1418
 
        status = pthread_mutex_unlock(&suc->mutex);
1419
 
        if (status)
1420
 
                fatal(status);
1421
 
 
1422
 
        status = pthread_mutex_destroy(&suc->mutex);
1423
 
        if (status)
1424
 
                fatal(status);
1425
 
 
1426
 
        status = pthread_cond_destroy(&suc->cond);
1427
 
        if (status)
1428
 
                fatal(status);
1429
 
 
1430
 
        return;
1431
 
}
1432
 
 
1433
 
static void handle_mounts_cleanup(void *arg)
1434
 
{
1435
 
        struct autofs_point *ap;
1436
 
        char path[PATH_MAX + 1];
1437
 
        char buf[MAX_ERR_BUF];
1438
 
        unsigned int clean = 0, submount, logopt;
1439
 
 
1440
 
        ap = (struct autofs_point *) arg;
1441
 
 
1442
 
        logopt = ap->logopt;
1443
 
        submount = ap->submount;
1444
 
 
1445
 
        strcpy(path, ap->path);
1446
 
        if (!submount && strcmp(ap->path, "/-") &&
1447
 
            ap->flags & MOUNT_FLAG_DIR_CREATED)
1448
 
                clean = 1;
1449
 
 
1450
 
        if (submount) {
1451
 
                /* We are finishing up */
1452
 
                ap->parent->submnt_count--;
1453
 
                list_del_init(&ap->mounts);
1454
 
        }
1455
 
 
1456
 
        master_remove_mapent(ap->entry);
1457
 
        master_source_unlock(ap->entry);
1458
 
 
1459
 
        destroy_logpri_fifo(ap);
1460
 
 
1461
 
        /*
1462
 
         * Submounts are detached threads and don't belong to the
1463
 
         * master map entry list so we need to free their resources
1464
 
         * here.
1465
 
         */
1466
 
        if (submount) {
1467
 
                mounts_mutex_unlock(ap->parent);
1468
 
                master_source_unlock(ap->parent->entry);
1469
 
                master_free_mapent_sources(ap->entry, 1);
1470
 
                master_free_mapent(ap->entry);
1471
 
        }
1472
 
 
1473
 
        if (clean) {
1474
 
                if (rmdir(path) == -1) {
1475
 
                        char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
1476
 
                        warn(logopt, "failed to remove dir %s: %s",
1477
 
                             path, estr);
1478
 
                }
1479
 
        }
1480
 
 
1481
 
        info(logopt, "shut down path %s", path);
1482
 
 
1483
 
        /*
1484
 
         * If we are not a submount send a signal to the signal handler
1485
 
         * so it can join with any completed handle_mounts() threads and
1486
 
         * perform final cleanup.
1487
 
         */
1488
 
        if (!submount)
1489
 
                pthread_kill(state_mach_thid, SIGTERM);
1490
 
 
1491
 
        master_mutex_unlock();
1492
 
 
1493
 
        return;
1494
 
}
1495
 
 
1496
 
void *handle_mounts(void *arg)
1497
 
{
1498
 
        struct startup_cond *suc;
1499
 
        struct autofs_point *ap;
1500
 
        int cancel_state, status = 0;
1501
 
        char *root;
1502
 
 
1503
 
        suc = (struct startup_cond *) arg;
1504
 
 
1505
 
        ap = suc->ap;
1506
 
        root = strdup(suc->root);
1507
 
 
1508
 
        pthread_cleanup_push(return_start_status, suc);
1509
 
        pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel_state);
1510
 
 
1511
 
        status = pthread_mutex_lock(&suc->mutex);
1512
 
        if (status) {
1513
 
                logerr("failed to lock startup condition mutex!");
1514
 
                fatal(status);
1515
 
        }
1516
 
 
1517
 
        if (!root) {
1518
 
                crit(ap->logopt, "failed to alloc string root");
1519
 
                suc->status = 1;
1520
 
                pthread_setcancelstate(cancel_state, NULL);
1521
 
                pthread_exit(NULL);
1522
 
        }
1523
 
 
1524
 
        if (mount_autofs(ap, root) < 0) {
1525
 
                crit(ap->logopt, "mount of %s failed!", ap->path);
1526
 
                suc->status = 1;
1527
 
                umount_autofs(ap, root, 1);
1528
 
                free(root);
1529
 
                pthread_setcancelstate(cancel_state, NULL);
1530
 
                pthread_exit(NULL);
1531
 
        }
1532
 
 
1533
 
        free(root);
1534
 
 
1535
 
        if (ap->flags & MOUNT_FLAG_GHOST && ap->type != LKP_DIRECT)
1536
 
                info(ap->logopt, "ghosting enabled");
1537
 
 
1538
 
        suc->status = 0;
1539
 
        pthread_cleanup_pop(1);
1540
 
 
1541
 
        /* We often start several automounters at the same time.  Add some
1542
 
           randomness so we don't all expire at the same time. */
1543
 
        if (!ap->submount && ap->exp_timeout)
1544
 
                alarm_add(ap, ap->exp_runfreq + rand() % ap->exp_runfreq);
1545
 
 
1546
 
        pthread_setcancelstate(cancel_state, NULL);
1547
 
 
1548
 
        while (ap->state != ST_SHUTDOWN) {
1549
 
                if (handle_packet(ap)) {
1550
 
                        int ret, cur_state;
1551
 
 
1552
 
                        /*
1553
 
                         * If we're a submount we need to ensure our parent
1554
 
                         * doesn't try to mount us again until our shutdown
1555
 
                         * is complete and that any outstanding mounts are
1556
 
                         * completed before we try to shutdown.
1557
 
                         */
1558
 
                        pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cur_state);
1559
 
 
1560
 
                        master_mutex_lock();
1561
 
 
1562
 
                        if (ap->submount) {
1563
 
                                master_source_writelock(ap->parent->entry);
1564
 
                                mounts_mutex_lock(ap->parent);
1565
 
                        }
1566
 
 
1567
 
                        master_source_writelock(ap->entry);
1568
 
 
1569
 
                        if (ap->state != ST_SHUTDOWN) {
1570
 
                                if (!ap->submount)
1571
 
                                        alarm_add(ap, ap->exp_runfreq);
1572
 
                                /* Return to ST_READY is done immediately */
1573
 
                                st_add_task(ap, ST_READY);
1574
 
                                master_source_unlock(ap->entry);
1575
 
                                if (ap->submount) {
1576
 
                                        mounts_mutex_unlock(ap->parent);
1577
 
                                        master_source_unlock(ap->parent->entry);
1578
 
                                }
1579
 
 
1580
 
                                master_mutex_unlock();
1581
 
 
1582
 
                                pthread_setcancelstate(cur_state, NULL);
1583
 
                                continue;
1584
 
                        }
1585
 
 
1586
 
                        alarm_delete(ap);
1587
 
                        st_remove_tasks(ap);
1588
 
                        st_wait_task(ap, ST_ANY, 0);
1589
 
 
1590
 
                        /*
1591
 
                         * For a direct mount map all mounts have already gone
1592
 
                         * by the time we get here and since we only ever
1593
 
                         * umount direct mounts at shutdown there is no need
1594
 
                         * to check for possible recovery.
1595
 
                         */
1596
 
                        if (ap->type == LKP_DIRECT) {
1597
 
                                umount_autofs(ap, NULL, 1);
1598
 
                                break;
1599
 
                        }
1600
 
 
1601
 
                        /*
1602
 
                         * If umount_autofs returns non-zero it wasn't able
1603
 
                         * to complete the umount and has left the mount intact
1604
 
                         * so we can continue. This can happen if a lookup
1605
 
                         * occurs while we're trying to umount.
1606
 
                         */
1607
 
                        ret = umount_autofs(ap, NULL, 1);
1608
 
                        if (!ret)
1609
 
                                break;
1610
 
 
1611
 
                        /* Failed shutdown returns to ready */
1612
 
                        warn(ap->logopt,
1613
 
                             "can't shutdown: filesystem %s still busy",
1614
 
                             ap->path);
1615
 
                        if (!ap->submount)
1616
 
                                alarm_add(ap, ap->exp_runfreq);
1617
 
                        /* Return to ST_READY is done immediately */
1618
 
                        st_add_task(ap, ST_READY);
1619
 
                        master_source_unlock(ap->entry);
1620
 
                        if (ap->submount) {
1621
 
                                mounts_mutex_unlock(ap->parent);
1622
 
                                master_source_unlock(ap->parent->entry);
1623
 
                        }
1624
 
 
1625
 
                        master_mutex_unlock();
1626
 
 
1627
 
                        pthread_setcancelstate(cur_state, NULL);
1628
 
 
1629
 
                }
1630
 
        }
1631
 
 
1632
 
        handle_mounts_cleanup(ap);
1633
 
 
1634
 
        return NULL;
1635
 
}
1636
 
 
1637
 
static void key_thread_stdenv_vars_destroy(void *arg)
1638
 
{
1639
 
        struct thread_stdenv_vars *tsv;
1640
 
 
1641
 
        tsv = (struct thread_stdenv_vars *) arg;
1642
 
        if (tsv->user)
1643
 
                free(tsv->user);
1644
 
        if (tsv->group)
1645
 
                free(tsv->group);
1646
 
        if (tsv->home)
1647
 
                free(tsv->home);
1648
 
        free(tsv);
1649
 
        return;
1650
 
}
1651
 
 
1652
 
static void usage(void)
1653
 
{
1654
 
        fprintf(stderr,
1655
 
                "Usage: %s [options] [master_map_name]\n"
1656
 
                "       -h --help       this text\n"
1657
 
                "       -p --pid-file f write process id to file f\n"
1658
 
                "       -t --timeout n  auto-unmount in n seconds (0-disable)\n"
1659
 
                "       -v --verbose    be verbose\n"
1660
 
                "       -d --debug      log debuging info\n"
1661
 
                "       -D --define     define global macro variable\n"
1662
 
                "       -f --foreground do not fork into background\n"
1663
 
                "       -r --random-multimount-selection\n"
1664
 
                "                       use ramdom replicated server selection\n"
1665
 
                "       -n --negative-timeout n\n"
1666
 
                "                       set the timeout for failed key lookups.\n"
1667
 
                "       -O --global-options\n"
1668
 
                "                       specify global mount options\n"
1669
 
                "       -l --set-log-priority priority path [path,...]\n"
1670
 
                "                       set daemon log verbosity\n"
1671
 
                "       -C --dont-check-daemon\n"
1672
 
                "                       don't check if daemon is already running\n"
1673
 
                "       -V --version    print version, build config and exit\n"
1674
 
                , program);
1675
 
}
1676
 
 
1677
 
static void show_build_info(void)
1678
 
{
1679
 
        char buf[2048];
1680
 
        int count = 0;
1681
 
 
1682
 
        printf("\nLinux automount version %s\n", version);
1683
 
 
1684
 
        printf("\nDirectories:\n");
1685
 
        printf("\tconfig dir:\t%s\n", confdir);
1686
 
        printf("\tmaps dir:\t%s\n", mapdir);
1687
 
        printf("\tmodules dir:\t%s\n", libdir);
1688
 
 
1689
 
        printf("\nCompile options:\n  ");
1690
 
 
1691
 
        memset(buf, 0, 2048);
1692
 
 
1693
 
#ifndef ENABLE_MOUNT_LOCKING
1694
 
        printf("DISABLE_MOUNT_LOCKING ");
1695
 
        count = 22;
1696
 
#endif
1697
 
 
1698
 
#ifdef ENABLE_FORCED_SHUTDOWN
1699
 
        printf("ENABLE_FORCED_SHUTDOWN ");
1700
 
        count = count + 23;
1701
 
#endif
1702
 
 
1703
 
#ifdef ENABLE_IGNORE_BUSY_MOUNTS
1704
 
        printf("ENABLE_IGNORE_BUSY_MOUNTS ");
1705
 
        count = count + 26;
1706
 
 
1707
 
        if (count > 60) {
1708
 
                printf("\n  ");
1709
 
                count = 0;
1710
 
        }
1711
 
#endif
1712
 
 
1713
 
 
1714
 
#ifdef WITH_HESIOD
1715
 
        printf("WITH_HESIOD ");
1716
 
        count = count + 12;
1717
 
 
1718
 
        if (count > 60) {
1719
 
                printf("\n  ");
1720
 
                count = 0;
1721
 
        }
1722
 
#endif
1723
 
 
1724
 
#ifdef WITH_LDAP
1725
 
        printf("WITH_LDAP ");
1726
 
        count = count + 10;
1727
 
 
1728
 
        if (count > 60) {
1729
 
                printf("\n  ");
1730
 
                count = 0;
1731
 
        }
1732
 
#endif
1733
 
 
1734
 
#ifdef WITH_SASL
1735
 
        printf("WITH_SASL ");
1736
 
        count = count + 10;
1737
 
 
1738
 
        if (count > 60) {
1739
 
                printf("\n  ");
1740
 
                count = 0;
1741
 
        }
1742
 
#endif
1743
 
 
1744
 
#ifdef WITH_DMALLOC
1745
 
        printf("WITH_DMALLOC ");
1746
 
        count = count + 13;
1747
 
 
1748
 
        if (count > 60) {
1749
 
                printf("\n  ");
1750
 
                count = 0;
1751
 
        }
1752
 
#endif
1753
 
 
1754
 
#ifdef LIBXML2_WORKAROUND
1755
 
        printf("LIBXML2_WORKAROUND ");
1756
 
        count = count + 19;
1757
 
 
1758
 
        if (count > 60) {
1759
 
                printf("\n  ");
1760
 
                count = 0;
1761
 
        }
1762
 
#endif
1763
 
 
1764
 
#ifdef WITH_LIBTIRPC
1765
 
        printf("WITH_LIBTIRPC ");
1766
 
        count = count + 14;
1767
 
#endif
1768
 
 
1769
 
        printf("\n\n");
1770
 
 
1771
 
        return;
1772
 
}
1773
 
 
1774
 
typedef struct _code {
1775
 
        char    *c_name;
1776
 
        int     c_val;
1777
 
} CODE;
1778
 
 
1779
 
CODE prioritynames[] = {
1780
 
        { "alert",      LOG_ALERT },
1781
 
        { "crit",       LOG_CRIT },
1782
 
        { "debug",      LOG_DEBUG },
1783
 
        { "emerg",      LOG_EMERG },
1784
 
        { "err",        LOG_ERR },
1785
 
        { "error",      LOG_ERR },              /* DEPRECATED */
1786
 
        { "info",       LOG_INFO },
1787
 
        { "notice",     LOG_NOTICE },
1788
 
        { "panic",      LOG_EMERG },            /* DEPRECATED */
1789
 
        { "warn",       LOG_WARNING },          /* DEPRECATED */
1790
 
        { "warning",    LOG_WARNING },
1791
 
        { NULL,         -1 },
1792
 
};
1793
 
 
1794
 
static int convert_log_priority(char *priority_name)
1795
 
{
1796
 
        CODE *priority_mapping;
1797
 
 
1798
 
        for (priority_mapping = prioritynames;
1799
 
             priority_mapping->c_name != NULL;
1800
 
             priority_mapping++) {
1801
 
 
1802
 
                if (!strcasecmp(priority_name, priority_mapping->c_name))
1803
 
                        return priority_mapping->c_val;
1804
 
        }
1805
 
 
1806
 
        return -1;
1807
 
}
1808
 
 
1809
 
int main(int argc, char *argv[])
1810
 
{
1811
 
        int res, opt, status;
1812
 
        int logpri = -1;
1813
 
        unsigned ghost, logging, daemon_check;
1814
 
        unsigned foreground, have_global_options;
1815
 
        time_t timeout;
1816
 
        time_t age = time(NULL);
1817
 
        struct rlimit rlim;
1818
 
        static const struct option long_options[] = {
1819
 
                {"help", 0, 0, 'h'},
1820
 
                {"pid-file", 1, 0, 'p'},
1821
 
                {"timeout", 1, 0, 't'},
1822
 
                {"verbose", 0, 0, 'v'},
1823
 
                {"debug", 0, 0, 'd'},
1824
 
                {"define", 1, 0, 'D'},
1825
 
                {"foreground", 0, 0, 'f'},
1826
 
                {"random-multimount-selection", 0, 0, 'r'},
1827
 
                {"negative-timeout", 1, 0, 'n'},
1828
 
                {"global-options", 1, 0, 'O'},
1829
 
                {"version", 0, 0, 'V'},
1830
 
                {"set-log-priority", 1, 0, 'l'},
1831
 
                {"dont-check-daemon", 0, 0, 'C'},
1832
 
                {"force", 0, 0, 'F'},
1833
 
                {0, 0, 0, 0}
1834
 
        };
1835
 
 
1836
 
        sigfillset(&block_sigs);
1837
 
        /* allow for the dropping of core files */
1838
 
        sigdelset(&block_sigs, SIGABRT);
1839
 
        sigdelset(&block_sigs, SIGBUS);
1840
 
        sigdelset(&block_sigs, SIGSEGV);
1841
 
        sigdelset(&block_sigs, SIGILL);
1842
 
        sigdelset(&block_sigs, SIGFPE);
1843
 
        sigdelset(&block_sigs, SIGTRAP);
1844
 
        sigprocmask(SIG_BLOCK, &block_sigs, NULL);
1845
 
 
1846
 
        program = argv[0];
1847
 
 
1848
 
        defaults_read_config(0);
1849
 
 
1850
 
        kpkt_len = get_kpkt_len();
1851
 
        timeout = defaults_get_timeout();
1852
 
        ghost = defaults_get_browse_mode();
1853
 
        logging = defaults_get_logging();
1854
 
        global_random_selection = 0;
1855
 
        global_options = NULL;
1856
 
        have_global_options = 0;
1857
 
        foreground = 0;
1858
 
        daemon_check = 1;
1859
 
 
1860
 
        opterr = 0;
1861
 
        while ((opt = getopt_long(argc, argv, "+hp:t:vdD:fVrO:l:n:CF", long_options, NULL)) != EOF) {
1862
 
                switch (opt) {
1863
 
                case 'h':
1864
 
                        usage();
1865
 
                        exit(0);
1866
 
 
1867
 
                case 'p':
1868
 
                        pid_file = optarg;
1869
 
                        break;
1870
 
 
1871
 
                case 't':
1872
 
                        timeout = getnumopt(optarg, opt);
1873
 
                        break;
1874
 
 
1875
 
                case 'v':
1876
 
                        logging |= LOGOPT_VERBOSE;
1877
 
                        break;
1878
 
 
1879
 
                case 'd':
1880
 
                        logging |= LOGOPT_DEBUG;
1881
 
                        break;
1882
 
 
1883
 
                case 'D':
1884
 
                        macro_parse_globalvar(optarg);
1885
 
                        break;
1886
 
 
1887
 
                case 'f':
1888
 
                        foreground = 1;
1889
 
                        break;
1890
 
 
1891
 
                case 'V':
1892
 
                        show_build_info();
1893
 
                        exit(0);
1894
 
 
1895
 
                case 'r':
1896
 
                        global_random_selection = 1;
1897
 
                        break;
1898
 
 
1899
 
                case 'n':
1900
 
                        global_negative_timeout = getnumopt(optarg, opt);
1901
 
                        break;
1902
 
 
1903
 
                case 'O':
1904
 
                        if (!have_global_options) {
1905
 
                                global_options = strdup(optarg);
1906
 
                                have_global_options = 1;
1907
 
                                break;
1908
 
                        }
1909
 
                        printf("%s: global options already specified.\n",
1910
 
                                program);
1911
 
                        break;
1912
 
 
1913
 
                case 'l':
1914
 
                        if (isalpha(*optarg)) {
1915
 
                                logpri = convert_log_priority(optarg);
1916
 
                                if (logpri < 0) {
1917
 
                                        fprintf(stderr, "Invalid log priority:"
1918
 
                                                " %s\n", optarg);
1919
 
                                        exit(1);
1920
 
                                }
1921
 
                        } else if (isdigit(*optarg)) {
1922
 
                                logpri = getnumopt(optarg, opt);
1923
 
                        } else {
1924
 
                                fprintf(stderr, "non-alphanumeric character "
1925
 
                                        "found in log priority.  Aborting.\n");
1926
 
                                exit(1);
1927
 
                        }
1928
 
                        break;
1929
 
 
1930
 
                case 'C':
1931
 
                        daemon_check = 0;
1932
 
                        break;
1933
 
 
1934
 
                case 'F':
1935
 
                        do_force_unlink = 1;
1936
 
                        break;
1937
 
 
1938
 
                case '?':
1939
 
                case ':':
1940
 
                        printf("%s: Ambiguous or unknown options\n", program);
1941
 
                        exit(1);
1942
 
                }
1943
 
        }
1944
 
 
1945
 
        if (logging & LOGOPT_VERBOSE)
1946
 
                set_log_verbose();
1947
 
 
1948
 
        if (logging & LOGOPT_DEBUG)
1949
 
                set_log_debug();
1950
 
 
1951
 
        if (geteuid() != 0) {
1952
 
                fprintf(stderr, "%s: this program must be run by root.\n",
1953
 
                        program);
1954
 
                exit(1);
1955
 
        }
1956
 
 
1957
 
        /* Remove the options */
1958
 
        argv += optind;
1959
 
        argc -= optind;
1960
 
 
1961
 
        if (logpri >= 0) {
1962
 
                int exit_code = 0;
1963
 
                int i;
1964
 
 
1965
 
                /*
1966
 
                 * The remaining argv elements are the paths for which
1967
 
                 * log priorities must be changed.
1968
 
                 */
1969
 
                for (i = 0; i < argc; i++) {
1970
 
                        if (set_log_priority(argv[i], logpri) < 0)
1971
 
                                exit_code = 1;
1972
 
                }
1973
 
                if (argc < 1) {
1974
 
                        fprintf(stderr,
1975
 
                                "--set-log-priority requires a path.\n");
1976
 
                        exit_code = 1;
1977
 
                }
1978
 
                exit(exit_code);
1979
 
        }
1980
 
 
1981
 
#if 0
1982
 
        if (!load_autofs4_module()) {
1983
 
                fprintf(stderr, "%s: can't load %s filesystem module.\n",
1984
 
                        program, FS_MODULE_NAME);
1985
 
                exit(1);
1986
 
        }
1987
 
#endif
1988
 
 
1989
 
        if (!query_kproto_ver() || get_kver_major() < 5) {
1990
 
                fprintf(stderr,
1991
 
                        "%s: test mount forbidden or "
1992
 
                        "incorrect kernel protocol version, "
1993
 
                        "kernel protocol version 5.00 or above required.\n",
1994
 
                        program);
1995
 
                exit(1);
1996
 
        }
1997
 
 
1998
 
        rlim.rlim_cur = MAX_OPEN_FILES;
1999
 
        rlim.rlim_max = MAX_OPEN_FILES;
2000
 
        res = setrlimit(RLIMIT_NOFILE, &rlim);
2001
 
        if (res)
2002
 
                warn(logging,
2003
 
                     "can't increase open file limit - continuing");
2004
 
 
2005
 
#if ENABLE_CORES
2006
 
        rlim.rlim_cur = RLIM_INFINITY;
2007
 
        rlim.rlim_max = RLIM_INFINITY;
2008
 
        res = setrlimit(RLIMIT_CORE, &rlim);
2009
 
        if (res)
2010
 
                warn(logging,
2011
 
                     "can't increase core file limit - continuing");
2012
 
#endif
2013
 
 
2014
 
        become_daemon(foreground, daemon_check);
2015
 
 
2016
 
        if (argc == 0)
2017
 
                master_list = master_new(NULL, timeout, ghost);
2018
 
        else
2019
 
                master_list = master_new(argv[0], timeout, ghost);
2020
 
 
2021
 
        if (!master_list) {
2022
 
                logerr("%s: can't create master map %s",
2023
 
                        program, argv[0]);
2024
 
                res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
2025
 
                close(start_pipefd[1]);
2026
 
                release_flag_file();
2027
 
                exit(1);
2028
 
        }
2029
 
 
2030
 
        if (pthread_attr_init(&th_attr)) {
2031
 
                logerr("%s: failed to init thread attribute struct!",
2032
 
                     program);
2033
 
                res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
2034
 
                close(start_pipefd[1]);
2035
 
                release_flag_file();
2036
 
                exit(1);
2037
 
        }
2038
 
 
2039
 
        if (pthread_attr_init(&th_attr_detached)) {
2040
 
                logerr("%s: failed to init thread attribute struct!",
2041
 
                     program);
2042
 
                res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
2043
 
                close(start_pipefd[1]);
2044
 
                release_flag_file();
2045
 
                exit(1);
2046
 
        }
2047
 
 
2048
 
        if (pthread_attr_setdetachstate(
2049
 
                        &th_attr_detached, PTHREAD_CREATE_DETACHED)) {
2050
 
                logerr("%s: failed to set detached thread attribute!",
2051
 
                     program);
2052
 
                res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
2053
 
                close(start_pipefd[1]);
2054
 
                release_flag_file();
2055
 
                exit(1);
2056
 
        }
2057
 
 
2058
 
#ifdef _POSIX_THREAD_ATTR_STACKSIZE
2059
 
        if (pthread_attr_setstacksize(
2060
 
                        &th_attr_detached, PTHREAD_STACK_MIN*64)) {
2061
 
                logerr("%s: failed to set stack size thread attribute!",
2062
 
                       program);
2063
 
                res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
2064
 
                close(start_pipefd[1]);
2065
 
                release_flag_file();
2066
 
                exit(1);
2067
 
        }
2068
 
#endif
2069
 
 
2070
 
        info(logging, "Starting automounter version %s, master map %s",
2071
 
                version, master_list->name);
2072
 
        info(logging, "using kernel protocol version %d.%02d",
2073
 
                get_kver_major(), get_kver_minor());
2074
 
 
2075
 
        status = pthread_key_create(&key_thread_stdenv_vars,
2076
 
                                key_thread_stdenv_vars_destroy);
2077
 
        if (status) {
2078
 
                logerr("%s: failed to create thread data key for std env vars!",
2079
 
                       program);
2080
 
                master_kill(master_list);
2081
 
                res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
2082
 
                close(start_pipefd[1]);
2083
 
                release_flag_file();
2084
 
                exit(1);
2085
 
        }
2086
 
 
2087
 
        init_ioctl_ctl();
2088
 
 
2089
 
        if (!alarm_start_handler()) {
2090
 
                logerr("%s: failed to create alarm handler thread!", program);
2091
 
                master_kill(master_list);
2092
 
                res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
2093
 
                close(start_pipefd[1]);
2094
 
                release_flag_file();
2095
 
                exit(1);
2096
 
        }
2097
 
 
2098
 
        if (!st_start_handler()) {
2099
 
                logerr("%s: failed to create FSM handler thread!", program);
2100
 
                master_kill(master_list);
2101
 
                res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
2102
 
                close(start_pipefd[1]);
2103
 
                release_flag_file();
2104
 
                exit(1);
2105
 
        }
2106
 
 
2107
 
#if defined(WITH_LDAP) && defined(LIBXML2_WORKAROUND)
2108
 
        void *dh_xml2 = dlopen("libxml2.so", RTLD_NOW);
2109
 
        if (!dh_xml2)
2110
 
                dh_xml2 = dlopen("libxml2.so.2", RTLD_NOW);
2111
 
        if (dh_xml2)
2112
 
                xmlInitParser();
2113
 
#endif
2114
 
#ifdef TIRPC_WORKAROUND
2115
 
        void *dh_tirpc = dlopen("libitirpc.so", RTLD_NOW);
2116
 
        if (!dh_tirpc)
2117
 
                dh_tirpc = dlopen("libitirpc.so.1", RTLD_NOW);
2118
 
#endif
2119
 
 
2120
 
        if (!master_read_master(master_list, age, 0)) {
2121
 
                master_kill(master_list);
2122
 
                *pst_stat = 3;
2123
 
                res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
2124
 
                close(start_pipefd[1]);
2125
 
                release_flag_file();
2126
 
                exit(3);
2127
 
        }
2128
 
 
2129
 
        /*
2130
 
         * Mmm ... reset force unlink umount so we don't also do this
2131
 
         * in future when we receive a HUP signal.
2132
 
         */
2133
 
        do_force_unlink = 0;
2134
 
 
2135
 
        st_stat = 0;
2136
 
        res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
2137
 
        close(start_pipefd[1]);
2138
 
 
2139
 
        state_mach_thid = pthread_self();
2140
 
        statemachine(NULL);
2141
 
 
2142
 
        master_kill(master_list);
2143
 
 
2144
 
        if (pid_file) {
2145
 
                unlink(pid_file);
2146
 
                pid_file = NULL;
2147
 
        }
2148
 
        closelog();
2149
 
        release_flag_file();
2150
 
 
2151
 
#ifdef TIRPC_WORKAROUND
2152
 
        if (dh_tirpc)
2153
 
                dlclose(dh_tirpc);
2154
 
#endif
2155
 
#if defined(WITH_LDAP) && defined( LIBXML2_WORKAROUND)
2156
 
        if (dh_xml2) {
2157
 
                xmlCleanupParser();
2158
 
                dlclose(dh_xml2);
2159
 
        }
2160
 
#endif
2161
 
        close_ioctl_ctl();
2162
 
 
2163
 
        info(logging, "autofs stopped");
2164
 
 
2165
 
        exit(0);
2166
 
}