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

« back to all changes in this revision

Viewing changes to .pc/autofs-5.0.5-fix-master-map-source-server-unavialable-handling.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
 
        struct mapent_cache *nc;
530
 
        int is_autofs_fs;
531
 
        int left;
532
 
 
533
 
        debug(ap->logopt, "path %s incl %d", path, incl);
534
 
 
535
 
        nc = ap->entry->master->nc;
536
 
        cache_readlock(nc);
537
 
        if (cache_lookup_distinct(nc, path)) {
538
 
                cache_unlock(nc);
539
 
                return 0;
540
 
        }
541
 
        cache_unlock(nc);
542
 
 
543
 
        is_autofs_fs = 0;
544
 
        if (master_find_submount(ap, path))
545
 
                is_autofs_fs = 1;
546
 
 
547
 
        left = 0;
548
 
 
549
 
        /*
550
 
         * If we are a submount we need to umount any offsets our
551
 
         * parent may have mounted over top of us.
552
 
         */
553
 
        /*if (ap->submount)
554
 
                left += umount_subtree_mounts(ap->parent, path, is_autofs_fs);*/
555
 
 
556
 
        left += umount_subtree_mounts(ap, path, is_autofs_fs);
557
 
 
558
 
        /* Delete detritus like unwanted mountpoints and symlinks */
559
 
        if (left == 0 && ap->state != ST_READMAP) {
560
 
                update_map_cache(ap, path);
561
 
                check_rm_dirs(ap, path, incl);
562
 
        }
563
 
 
564
 
        return left;
565
 
}
566
 
 
567
 
static int umount_all(struct autofs_point *ap, int force)
568
 
{
569
 
        int left;
570
 
 
571
 
        left = umount_multi(ap, ap->path, 0);
572
 
        if (force && left)
573
 
                warn(ap->logopt, "could not unmount %d dirs under %s",
574
 
                     left, ap->path);
575
 
 
576
 
        return left;
577
 
}
578
 
 
579
 
int umount_autofs(struct autofs_point *ap, const char *root, int force)
580
 
{
581
 
        int ret = 0;
582
 
 
583
 
        if (ap->state == ST_INIT)
584
 
                return -1;
585
 
 
586
 
        /*
587
 
         * Since lookup.c is lazy about closing lookup modules
588
 
         * to prevent unneeded opens, we need to clean them up
589
 
         * before umount.
590
 
         */
591
 
        lookup_close_lookup(ap);
592
 
 
593
 
        if (ap->type == LKP_INDIRECT) {
594
 
                if (umount_all(ap, force) && !force)
595
 
                        return -1;
596
 
                ret = umount_autofs_indirect(ap, root);
597
 
        } else
598
 
                ret = umount_autofs_direct(ap);
599
 
 
600
 
        return ret;
601
 
}
602
 
 
603
 
static size_t get_kpkt_len(void)
604
 
{
605
 
        size_t pkt_len = sizeof(struct autofs_v5_packet);
606
 
        struct utsname un;
607
 
 
608
 
        uname(&un);
609
 
 
610
 
        if (pkt_len % 8) {
611
 
                if (strcmp(un.machine, "alpha") == 0 ||
612
 
                    strcmp(un.machine, "ia64") == 0 ||
613
 
                    strcmp(un.machine, "x86_64") == 0 ||
614
 
                    strcmp(un.machine, "ppc64") == 0)
615
 
                        pkt_len += 4;
616
 
 
617
 
        }
618
 
 
619
 
        return pkt_len;
620
 
}
621
 
 
622
 
static int fullread(int fd, void *ptr, size_t len)
623
 
{
624
 
        char *buf = (char *) ptr;
625
 
 
626
 
        while (len > 0) {
627
 
                ssize_t r = read(fd, buf, len);
628
 
 
629
 
                if (r == -1) {
630
 
                        if (errno == EINTR)
631
 
                                continue;
632
 
                        break;
633
 
                }
634
 
 
635
 
                buf += r;
636
 
                len -= r;
637
 
        }
638
 
 
639
 
        return len;
640
 
}
641
 
 
642
 
static char *automount_path_to_fifo(unsigned logopt, const char *path)
643
 
{
644
 
        char *fifo_name, *p;
645
 
        int  name_len = strlen(path) + strlen(fifodir) + 1;
646
 
        int ret;
647
 
 
648
 
        fifo_name = malloc(name_len);
649
 
        if (!fifo_name)
650
 
                return NULL;
651
 
        ret = snprintf(fifo_name, name_len, "%s%s", fifodir, path);
652
 
        if (ret >= name_len) {
653
 
                info(logopt,
654
 
                     "fifo path for \"%s\" truncated to \"%s\".  This may "
655
 
                     "lead to --set-log-priority commands being sent to the "
656
 
                     "wrong automount daemon.", path, fifo_name);
657
 
        }
658
 
 
659
 
        /*
660
 
         *  An automount path can be made up of subdirectories.  So, to
661
 
         *  create the fifo name, we will just replace instances of '/' with
662
 
         *  '-'. 
663
 
         */
664
 
        p = fifo_name + strlen(fifodir);
665
 
        while (*p != '\0') {
666
 
                if (*p == '/')
667
 
                        *p = '-';
668
 
                p++;
669
 
        }
670
 
 
671
 
        debug(logopt, "fifo name %s",fifo_name);
672
 
 
673
 
        return fifo_name;
674
 
}
675
 
 
676
 
static int create_logpri_fifo(struct autofs_point *ap)
677
 
{
678
 
        int ret = -1;
679
 
        int fd;
680
 
        char *fifo_name;
681
 
        char buf[MAX_ERR_BUF];
682
 
 
683
 
        fifo_name = automount_path_to_fifo(ap->logopt, ap->path);
684
 
        if (!fifo_name) {
685
 
                crit(ap->logopt, "Failed to allocate memory!");
686
 
                goto out_free; /* free(NULL) is okay */
687
 
        }
688
 
 
689
 
        ret = unlink(fifo_name);
690
 
        if (ret != 0 && errno != ENOENT) {
691
 
                crit(ap->logopt,
692
 
                     "Failed to unlink FIFO. Is the automount daemon "
693
 
                     "already running?");
694
 
                goto out_free;
695
 
        }
696
 
 
697
 
        ret = mkfifo(fifo_name, S_IRUSR|S_IWUSR);
698
 
        if (ret != 0) {
699
 
                char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
700
 
                crit(ap->logopt,
701
 
                     "mkfifo for %s failed: %s", fifo_name, estr);
702
 
                goto out_free;
703
 
        }
704
 
 
705
 
        fd = open_fd(fifo_name, O_RDWR|O_NONBLOCK);
706
 
        if (fd < 0) {
707
 
                char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
708
 
                crit(ap->logopt,
709
 
                     "Failed to open %s: %s", fifo_name, estr);
710
 
                unlink(fifo_name);
711
 
                ret = -1;
712
 
                goto out_free;
713
 
        }
714
 
 
715
 
        ap->logpri_fifo = fd;
716
 
 
717
 
out_free:
718
 
        free(fifo_name);
719
 
        return ret;
720
 
}
721
 
 
722
 
int destroy_logpri_fifo(struct autofs_point *ap)
723
 
{
724
 
        int ret = -1;
725
 
        int fd = ap->logpri_fifo;
726
 
        char *fifo_name;
727
 
        char buf[MAX_ERR_BUF];
728
 
 
729
 
        if (fd == -1)
730
 
                return 0;
731
 
 
732
 
        fifo_name = automount_path_to_fifo(ap->logopt, ap->path);
733
 
        if (!fifo_name) {
734
 
                crit(ap->logopt, "Failed to allocate memory!");
735
 
                goto out_free; /* free(NULL) is okay */
736
 
        }
737
 
 
738
 
        ap->logpri_fifo = -1;
739
 
 
740
 
        ret = close(fd);
741
 
        if (ret != 0) {
742
 
                char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
743
 
                warn(ap->logopt,
744
 
                     "close for fifo %s: %s", fifo_name, estr);
745
 
        }
746
 
 
747
 
        ret = unlink(fifo_name);
748
 
        if (ret != 0) {
749
 
                warn(ap->logopt,
750
 
                     "Failed to unlink FIFO. Was the fifo created OK?");
751
 
        }
752
 
 
753
 
out_free:
754
 
        free(fifo_name);
755
 
        return ret;
756
 
}
757
 
 
758
 
static void handle_fifo_message(struct autofs_point *ap, int fd)
759
 
{
760
 
        int ret;
761
 
        char buffer[PIPE_BUF];
762
 
        char *end;
763
 
        long pri;
764
 
        char buf[MAX_ERR_BUF];
765
 
 
766
 
        memset(buffer, 0, sizeof(buffer));
767
 
        ret = read(fd, &buffer, sizeof(buffer));
768
 
        if (ret < 0) {
769
 
                char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
770
 
                warn(ap->logopt, "read on fifo returned error: %s", estr);
771
 
                return;
772
 
        }
773
 
 
774
 
        if (ret != 2) {
775
 
                debug(ap->logopt, "expected 2 bytes, received %d.", ret);
776
 
                return;
777
 
        }
778
 
 
779
 
        errno = 0;
780
 
        pri = strtol(buffer, &end, 10);
781
 
        if ((pri == LONG_MIN || pri == LONG_MAX) && errno == ERANGE) {
782
 
                debug(ap->logopt, "strtol reported an %s.  Failed to set "
783
 
                      "log priority.", pri == LONG_MIN ? "underflow" : "overflow");
784
 
                return;
785
 
        }
786
 
        if ((pri == 0 && errno == EINVAL) || end == buffer) {
787
 
                debug(ap->logopt, "priority is expected to be an integer "
788
 
                      "in the range 0-7 inclusive.");
789
 
                return;
790
 
        }
791
 
 
792
 
        if (pri > LOG_DEBUG || pri < LOG_EMERG) {
793
 
                debug(ap->logopt, "invalid log priority (%ld) received "
794
 
                      "on fifo", pri);
795
 
                return;
796
 
        }
797
 
 
798
 
        /*
799
 
         * OK, the message passed all of the sanity checks.  The
800
 
         * automounter actually only supports three log priorities.
801
 
         * Everything is logged at log level debug, deamon messages
802
 
         * and everything except debug messages are logged with the
803
 
         * verbose setting and only error and critical messages are
804
 
         * logged when debugging isn't enabled.
805
 
         */
806
 
        if (pri >= LOG_WARNING) {
807
 
                if (pri == LOG_DEBUG) {
808
 
                        set_log_debug_ap(ap);
809
 
                        info(ap->logopt, "Debug logging set for %s", ap->path);
810
 
                } else {
811
 
                        set_log_verbose_ap(ap);
812
 
                        info(ap->logopt, "Verbose logging set for %s", ap->path);
813
 
                }
814
 
        } else {
815
 
                if (ap->logopt & LOGOPT_ANY)
816
 
                        info(ap->logopt, "Basic logging set for %s", ap->path);
817
 
                set_log_norm_ap(ap);
818
 
        }
819
 
}
820
 
 
821
 
static int set_log_priority(const char *path, int priority)
822
 
{
823
 
        int fd;
824
 
        char *fifo_name;
825
 
        char buf[2];
826
 
 
827
 
        if (priority > LOG_DEBUG || priority < LOG_EMERG) {
828
 
                fprintf(stderr, "Log priority %d is invalid.\n", priority);
829
 
                fprintf(stderr, "Please spcify a number in the range 0-7.\n");
830
 
                return -1;
831
 
        }
832
 
 
833
 
        /*
834
 
         * This is an ascii based protocol, so we want the string
835
 
         * representation of the integer log priority.
836
 
         */
837
 
        snprintf(buf, sizeof(buf), "%d", priority);
838
 
 
839
 
        fifo_name = automount_path_to_fifo(LOGOPT_NONE, path);
840
 
        if (!fifo_name) {
841
 
                fprintf(stderr, "%s: Failed to allocate memory!\n",
842
 
                        __FUNCTION__);
843
 
                return -1;
844
 
        }
845
 
 
846
 
        /*
847
 
         * Specify O_NONBLOCK so that the open will fail if there is no
848
 
         * daemon reading from the other side of the FIFO.
849
 
         */
850
 
        fd = open(fifo_name, O_WRONLY|O_NONBLOCK);
851
 
        if (fd < 0) {
852
 
                fprintf(stderr, "%s: open of %s failed with %s\n",
853
 
                        __FUNCTION__, fifo_name, strerror(errno));
854
 
                fprintf(stderr, "%s: perhaps the fifo wasn't setup,"
855
 
                        " please check your log for more information\n", __FUNCTION__);
856
 
                free(fifo_name);
857
 
                return -1;
858
 
        }
859
 
 
860
 
        if (write(fd, buf, sizeof(buf)) != sizeof(buf)) {
861
 
                fprintf(stderr, "Failed to change logging priority.  ");
862
 
                fprintf(stderr, "write to fifo failed: %s.\n",
863
 
                        strerror(errno));
864
 
                close(fd);
865
 
                free(fifo_name);
866
 
                return -1;
867
 
        }
868
 
        close(fd);
869
 
        free(fifo_name);
870
 
        fprintf(stdout, "Successfully set log priority for %s.\n", path);
871
 
 
872
 
        return 0;
873
 
}
874
 
 
875
 
static int get_pkt(struct autofs_point *ap, union autofs_v5_packet_union *pkt)
876
 
{
877
 
        struct pollfd fds[3];
878
 
        int pollfds = 3;
879
 
        char buf[MAX_ERR_BUF];
880
 
 
881
 
        fds[0].fd = ap->pipefd;
882
 
        fds[0].events = POLLIN;
883
 
        fds[1].fd = ap->state_pipe[0];
884
 
        fds[1].events = POLLIN;
885
 
        fds[2].fd = ap->logpri_fifo;
886
 
        fds[2].events = POLLIN;
887
 
        if (fds[2].fd  == -1)
888
 
                pollfds--;
889
 
 
890
 
        for (;;) {
891
 
                if (poll(fds, pollfds, -1) == -1) {
892
 
                        char *estr;
893
 
                        if (errno == EINTR)
894
 
                                continue;
895
 
                        estr = strerror_r(errno, buf, MAX_ERR_BUF);
896
 
                        logerr("poll failed: %s", estr);
897
 
                        return -1;
898
 
                }
899
 
 
900
 
                if (fds[1].revents & POLLIN) {
901
 
                        enum states next_state;
902
 
                        size_t read_size = sizeof(next_state);
903
 
                        int state_pipe;
904
 
 
905
 
                        next_state = ST_INVAL;
906
 
 
907
 
                        st_mutex_lock();
908
 
 
909
 
                        state_pipe = ap->state_pipe[0];
910
 
 
911
 
                        if (fullread(state_pipe, &next_state, read_size)) {
912
 
                                st_mutex_unlock();
913
 
                                continue;
914
 
                        }
915
 
 
916
 
                        st_mutex_unlock();
917
 
 
918
 
                        if (next_state == ST_SHUTDOWN)
919
 
                                return -1;
920
 
                }
921
 
 
922
 
                if (fds[0].revents & POLLIN)
923
 
                        return fullread(ap->pipefd, pkt, kpkt_len);
924
 
 
925
 
                if (fds[2].fd != -1 && fds[2].revents & POLLIN) {
926
 
                        debug(ap->logopt, "message pending on control fifo.");
927
 
                        handle_fifo_message(ap, fds[2].fd);
928
 
                }
929
 
        }
930
 
}
931
 
 
932
 
int do_expire(struct autofs_point *ap, const char *name, int namelen)
933
 
{
934
 
        char buf[PATH_MAX];
935
 
        int len, ret;
936
 
 
937
 
        if (*name != '/') {
938
 
                len = ncat_path(buf, sizeof(buf), ap->path, name, namelen);
939
 
        } else {
940
 
                len = snprintf(buf, PATH_MAX, "%s", name);
941
 
                if (len >= PATH_MAX)
942
 
                        len = 0;
943
 
        }
944
 
 
945
 
        if (!len) {
946
 
                crit(ap->logopt, "path to long for buffer");
947
 
                return 1;
948
 
        }
949
 
 
950
 
        info(ap->logopt, "expiring path %s", buf);
951
 
 
952
 
        pthread_cleanup_push(master_source_lock_cleanup, ap->entry);
953
 
        master_source_readlock(ap->entry);
954
 
        ret = umount_multi(ap, buf, 1);
955
 
        if (ret == 0)
956
 
                info(ap->logopt, "expired %s", buf);
957
 
        else
958
 
                warn(ap->logopt, "couldn't complete expire of %s", buf);
959
 
        pthread_cleanup_pop(1);
960
 
 
961
 
        return ret;
962
 
}
963
 
 
964
 
static int autofs_init_ap(struct autofs_point *ap)
965
 
{
966
 
        int pipefd[2];
967
 
 
968
 
        if ((ap->state != ST_INIT)) {
969
 
                /* This can happen if an autofs process is already running*/
970
 
                error(ap->logopt, "bad state %d", ap->state);
971
 
                return -1;
972
 
        }
973
 
 
974
 
        ap->pipefd = ap->kpipefd = ap->ioctlfd = -1;
975
 
 
976
 
        /* Pipe for kernel communications */
977
 
        if (open_pipe(pipefd) < 0) {
978
 
                crit(ap->logopt,
979
 
                     "failed to create commumication pipe for autofs path %s",
980
 
                     ap->path);
981
 
                return -1;
982
 
        }
983
 
 
984
 
        ap->pipefd = pipefd[0];
985
 
        ap->kpipefd = pipefd[1];
986
 
 
987
 
        /* Pipe state changes from signal handler to main loop */
988
 
        if (open_pipe(ap->state_pipe) < 0) {
989
 
                crit(ap->logopt,
990
 
                     "failed create state pipe for autofs path %s", ap->path);
991
 
                close(ap->pipefd);
992
 
                close(ap->kpipefd);     /* Close kernel pipe end */
993
 
                return -1;
994
 
        }
995
 
 
996
 
        if (create_logpri_fifo(ap) < 0) {
997
 
                logmsg("could not create FIFO for path %s\n", ap->path);
998
 
                logmsg("dynamic log level changes not available for %s", ap->path);
999
 
        }
1000
 
 
1001
 
        return 0;
1002
 
}
1003
 
 
1004
 
static int mount_autofs(struct autofs_point *ap, const char *root)
1005
 
{
1006
 
        int status = 0;
1007
 
 
1008
 
        if (autofs_init_ap(ap) != 0)
1009
 
                return -1;
1010
 
 
1011
 
        if (ap->type == LKP_DIRECT)
1012
 
                status = mount_autofs_direct(ap);
1013
 
        else
1014
 
                status = mount_autofs_indirect(ap, root);
1015
 
 
1016
 
        if (status < 0)
1017
 
                return -1;
1018
 
 
1019
 
        st_add_task(ap, ST_READY);
1020
 
 
1021
 
        return 0;
1022
 
}
1023
 
 
1024
 
static int handle_packet(struct autofs_point *ap)
1025
 
{
1026
 
        union autofs_v5_packet_union pkt;
1027
 
 
1028
 
        if (get_pkt(ap, &pkt))
1029
 
                return -1;
1030
 
 
1031
 
        debug(ap->logopt, "type = %d", pkt.hdr.type);
1032
 
 
1033
 
        switch (pkt.hdr.type) {
1034
 
        case autofs_ptype_missing_indirect:
1035
 
                return handle_packet_missing_indirect(ap, &pkt.v5_packet);
1036
 
 
1037
 
        case autofs_ptype_missing_direct:
1038
 
                return handle_packet_missing_direct(ap, &pkt.v5_packet);
1039
 
 
1040
 
        case autofs_ptype_expire_indirect:
1041
 
                return handle_packet_expire_indirect(ap, &pkt.v5_packet);
1042
 
 
1043
 
        case autofs_ptype_expire_direct:
1044
 
                return handle_packet_expire_direct(ap, &pkt.v5_packet);
1045
 
        }
1046
 
        error(ap->logopt, "unknown packet type %d", pkt.hdr.type);
1047
 
        return -1;
1048
 
}
1049
 
 
1050
 
static void become_daemon(unsigned foreground, unsigned daemon_check)
1051
 
{
1052
 
        FILE *pidfp;
1053
 
        char buf[MAX_ERR_BUF];
1054
 
        int res;
1055
 
        pid_t pid;
1056
 
 
1057
 
        /* Don't BUSY any directories unnecessarily */
1058
 
        if (chdir("/")) {
1059
 
                fprintf(stderr, "%s: failed change working directory.\n",
1060
 
                        program);
1061
 
                exit(0);
1062
 
        }
1063
 
 
1064
 
        if (open_pipe(start_pipefd) < 0) {
1065
 
                fprintf(stderr, "%s: failed to create start_pipefd.\n",
1066
 
                        program);
1067
 
                exit(0);
1068
 
        }
1069
 
 
1070
 
        /* Detach from foreground process */
1071
 
        if (foreground) {
1072
 
                if (daemon_check && !aquire_flag_file()) {
1073
 
                        fprintf(stderr, "%s: program is already running.\n",
1074
 
                                program);
1075
 
                        exit(1);
1076
 
                }
1077
 
                log_to_stderr();
1078
 
        } else {
1079
 
                pid = fork();
1080
 
                if (pid > 0) {
1081
 
                        close(start_pipefd[1]);
1082
 
                        res = read(start_pipefd[0], pst_stat, sizeof(*pst_stat));
1083
 
                        if (res < 0)
1084
 
                                exit(1);
1085
 
                        exit(*pst_stat);
1086
 
                } else if (pid < 0) {
1087
 
                        fprintf(stderr, "%s: Could not detach process\n",
1088
 
                                program);
1089
 
                        exit(1);
1090
 
                }
1091
 
                close(start_pipefd[0]);
1092
 
 
1093
 
                if (daemon_check && !aquire_flag_file()) {
1094
 
                        fprintf(stderr, "%s: program is already running.\n",
1095
 
                                program);
1096
 
                        /* Return success if already running */
1097
 
                        st_stat = 0;
1098
 
                        res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
1099
 
                        if (res < 0)
1100
 
                                exit(1);
1101
 
                        close(start_pipefd[1]);
1102
 
                        exit(*pst_stat);
1103
 
                }
1104
 
 
1105
 
                /*
1106
 
                 * Make our own process group for "magic" reason: processes that share
1107
 
                 * our pgrp see the raw filesystem behind the magic.
1108
 
                 */
1109
 
                if (setsid() == -1) {
1110
 
                        char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
1111
 
                        fprintf(stderr, "setsid: %s", estr);
1112
 
                        res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
1113
 
                        close(start_pipefd[1]);
1114
 
                        exit(*pst_stat);
1115
 
                }
1116
 
                log_to_syslog();
1117
 
        }
1118
 
 
1119
 
        /* Write pid file if requested */
1120
 
        if (pid_file) {
1121
 
                if ((pidfp = fopen(pid_file, "wt"))) {
1122
 
                        fprintf(pidfp, "%lu\n", (unsigned long) getpid());
1123
 
                        fclose(pidfp);
1124
 
                } else {
1125
 
                        char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
1126
 
                        logerr("failed to write pid file %s: %s",
1127
 
                               pid_file, estr);
1128
 
                        pid_file = NULL;
1129
 
                }
1130
 
        }
1131
 
}
1132
 
 
1133
 
static unsigned long getnumopt(char *str, char option)
1134
 
{
1135
 
        unsigned long val;
1136
 
        char *end;
1137
 
 
1138
 
        val = strtoul(str, &end, 0);
1139
 
        if (!*str || *end) {
1140
 
                fprintf(stderr,
1141
 
                        "%s: option -%c requires a numeric argument, got %s\n",
1142
 
                        program, option, str);
1143
 
                exit(1);
1144
 
        }
1145
 
        return val;
1146
 
}
1147
 
 
1148
 
static void do_master_cleanup_unlock(void *arg)
1149
 
{
1150
 
        int status;
1151
 
 
1152
 
        status = pthread_mutex_unlock(&mrc.mutex);
1153
 
        if (status)
1154
 
                fatal(status);
1155
 
 
1156
 
        return;
1157
 
}
1158
 
 
1159
 
static void *do_notify_state(void *arg)
1160
 
{
1161
 
        struct master *master;
1162
 
        int sig;
1163
 
        int status;
1164
 
 
1165
 
        sig = *(int *) arg;
1166
 
 
1167
 
        status = pthread_mutex_lock(&mrc.mutex);
1168
 
        if (status)
1169
 
                fatal(status);
1170
 
 
1171
 
        master = mrc.master;
1172
 
 
1173
 
        debug(master->logopt, "signal %d", sig);
1174
 
 
1175
 
        mrc.signaled = 1;
1176
 
        status = pthread_cond_signal(&mrc.cond);
1177
 
        if (status) {
1178
 
                error(master->logopt,
1179
 
                      "failed to signal state notify condition");
1180
 
                status = pthread_mutex_unlock(&mrc.mutex);
1181
 
                if (status)
1182
 
                        fatal(status);
1183
 
                pthread_exit(NULL);
1184
 
        }
1185
 
 
1186
 
        status = pthread_mutex_unlock(&mrc.mutex);
1187
 
        if (status)
1188
 
                fatal(status);
1189
 
 
1190
 
        master_notify_state_change(master, sig);
1191
 
 
1192
 
        return NULL;
1193
 
}
1194
 
 
1195
 
static pthread_t do_signals(struct master *master, int sig)
1196
 
{
1197
 
        pthread_t thid;
1198
 
        int r_sig = sig;
1199
 
        int status;
1200
 
 
1201
 
        status = pthread_mutex_lock(&mrc.mutex);
1202
 
        if (status)
1203
 
                fatal(status);
1204
 
 
1205
 
        status = pthread_create(&thid, &th_attr_detached, do_notify_state, &r_sig);
1206
 
        if (status) {
1207
 
                error(master->logopt,
1208
 
                      "mount state notify thread create failed");
1209
 
                status = pthread_mutex_unlock(&mrc.mutex);
1210
 
                if (status)
1211
 
                        fatal(status);
1212
 
                return 0;
1213
 
        }
1214
 
 
1215
 
        mrc.thid = thid;
1216
 
        mrc.master = master;
1217
 
 
1218
 
        pthread_cleanup_push(do_master_cleanup_unlock, NULL);
1219
 
 
1220
 
        mrc.signaled = 0;
1221
 
        while (!mrc.signaled) {
1222
 
                status = pthread_cond_wait(&mrc.cond, &mrc.mutex);
1223
 
                if (status)
1224
 
                        fatal(status);
1225
 
        }
1226
 
 
1227
 
        pthread_cleanup_pop(1);
1228
 
 
1229
 
        return thid;
1230
 
}
1231
 
 
1232
 
static void *do_read_master(void *arg)
1233
 
{
1234
 
        struct master *master;
1235
 
        unsigned int logopt;
1236
 
        time_t age;
1237
 
        int readall = 1;
1238
 
        int status;
1239
 
 
1240
 
        status = pthread_mutex_lock(&mrc.mutex);
1241
 
        if (status)
1242
 
                fatal(status);
1243
 
 
1244
 
        master = mrc.master;
1245
 
        age = mrc.age;
1246
 
        logopt = master->logopt;
1247
 
 
1248
 
        mrc.signaled = 1;
1249
 
        status = pthread_cond_signal(&mrc.cond);
1250
 
        if (status) {
1251
 
                error(logopt,
1252
 
                      "failed to signal master read map condition");
1253
 
                master->reading = 0;
1254
 
                status = pthread_mutex_unlock(&mrc.mutex);
1255
 
                if (status)
1256
 
                        fatal(status);
1257
 
                pthread_exit(NULL);
1258
 
        }
1259
 
 
1260
 
        status = pthread_mutex_unlock(&mrc.mutex);
1261
 
        if (status)
1262
 
                fatal(status);
1263
 
 
1264
 
        defaults_read_config(1);
1265
 
 
1266
 
        info(logopt, "re-reading master map %s", master->name);
1267
 
 
1268
 
        status = master_read_master(master, age, readall);
1269
 
 
1270
 
        master->reading = 0;
1271
 
 
1272
 
        return NULL;
1273
 
}
1274
 
 
1275
 
static int do_hup_signal(struct master *master, time_t age)
1276
 
{
1277
 
        unsigned int logopt = master->logopt;
1278
 
        pthread_t thid;
1279
 
        int status;
1280
 
 
1281
 
        status = pthread_mutex_lock(&mrc.mutex);
1282
 
        if (status)
1283
 
                fatal(status);
1284
 
 
1285
 
        if (master->reading) {
1286
 
                status = pthread_mutex_unlock(&mrc.mutex);
1287
 
                if (status)
1288
 
                        fatal(status);
1289
 
                return 1;
1290
 
        }
1291
 
 
1292
 
        master->reading = 1;
1293
 
 
1294
 
        status = pthread_create(&thid, &th_attr_detached, do_read_master, NULL);
1295
 
        if (status) {
1296
 
                error(logopt,
1297
 
                      "master read map thread create failed");
1298
 
                master->reading = 0;
1299
 
                status = pthread_mutex_unlock(&mrc.mutex);
1300
 
                if (status)
1301
 
                        fatal(status);
1302
 
                return 0;
1303
 
        }
1304
 
 
1305
 
        mrc.thid = thid;
1306
 
        mrc.master = master;
1307
 
        mrc.age = age;
1308
 
 
1309
 
        pthread_cleanup_push(do_master_cleanup_unlock, NULL);
1310
 
 
1311
 
        mrc.signaled = 0;
1312
 
        while (!mrc.signaled) {
1313
 
                status = pthread_cond_wait(&mrc.cond, &mrc.mutex);
1314
 
                if (status)
1315
 
                        fatal(status);
1316
 
        }
1317
 
 
1318
 
        pthread_cleanup_pop(1);
1319
 
 
1320
 
        return 1;
1321
 
}
1322
 
 
1323
 
/* Deal with all the signal-driven events in the state machine */
1324
 
static void *statemachine(void *arg)
1325
 
{
1326
 
        sigset_t signalset;
1327
 
        int sig;
1328
 
 
1329
 
        memcpy(&signalset, &block_sigs, sizeof(signalset));
1330
 
        sigdelset(&signalset, SIGCHLD);
1331
 
        sigdelset(&signalset, SIGCONT);
1332
 
 
1333
 
        while (1) {
1334
 
                sigwait(&signalset, &sig);
1335
 
 
1336
 
                switch (sig) {
1337
 
                case SIGTERM:
1338
 
                case SIGINT:
1339
 
                case SIGUSR2:
1340
 
                        master_mutex_lock();
1341
 
                        if (list_empty(&master_list->completed)) {
1342
 
                                if (list_empty(&master_list->mounts)) {
1343
 
                                        master_mutex_unlock();
1344
 
                                        return NULL;
1345
 
                                }
1346
 
                        } else {
1347
 
                                if (master_done(master_list)) {
1348
 
                                        master_mutex_unlock();
1349
 
                                        return NULL;
1350
 
                                }
1351
 
                                master_mutex_unlock();
1352
 
                                break;
1353
 
                        }
1354
 
                        master_mutex_unlock();
1355
 
 
1356
 
                case SIGUSR1:
1357
 
                        do_signals(master_list, sig);
1358
 
                        break;
1359
 
 
1360
 
                case SIGHUP:
1361
 
                        do_hup_signal(master_list, time(NULL));
1362
 
                        break;
1363
 
 
1364
 
                default:
1365
 
                        logerr("got unexpected signal %d!", sig);
1366
 
                        continue;
1367
 
                }
1368
 
        }
1369
 
}
1370
 
 
1371
 
static void return_start_status(void *arg)
1372
 
{
1373
 
        struct startup_cond *sc;
1374
 
        int status;
1375
 
 
1376
 
        sc = (struct startup_cond *) arg;
1377
 
 
1378
 
        sc->done = 1;
1379
 
 
1380
 
        /*
1381
 
         * Startup condition mutex must be locked during 
1382
 
         * the startup process.
1383
 
         */
1384
 
        status = pthread_cond_signal(&sc->cond);
1385
 
        if (status)
1386
 
                fatal(status);
1387
 
 
1388
 
        status = pthread_mutex_unlock(&sc->mutex);
1389
 
        if (status)
1390
 
                fatal(status);
1391
 
}
1392
 
 
1393
 
int handle_mounts_startup_cond_init(struct startup_cond *suc)
1394
 
{
1395
 
        int status;
1396
 
 
1397
 
        status = pthread_mutex_init(&suc->mutex, NULL);
1398
 
        if (status)
1399
 
                return status;
1400
 
 
1401
 
        status = pthread_cond_init(&suc->cond, NULL);
1402
 
        if (status) {
1403
 
                status = pthread_mutex_destroy(&suc->mutex);
1404
 
                if (status)
1405
 
                        fatal(status);
1406
 
                return status;
1407
 
        }
1408
 
 
1409
 
        status = pthread_mutex_lock(&suc->mutex);
1410
 
        if (status) {
1411
 
                status = pthread_mutex_destroy(&suc->mutex);
1412
 
                if (status)
1413
 
                        fatal(status);
1414
 
                status = pthread_cond_destroy(&suc->cond);
1415
 
                if (status)
1416
 
                        fatal(status);
1417
 
        }
1418
 
 
1419
 
        return 0;
1420
 
}
1421
 
 
1422
 
void handle_mounts_startup_cond_destroy(void *arg)
1423
 
{
1424
 
        struct startup_cond *suc = (struct startup_cond *) arg;
1425
 
        int status;
1426
 
 
1427
 
        status = pthread_mutex_unlock(&suc->mutex);
1428
 
        if (status)
1429
 
                fatal(status);
1430
 
 
1431
 
        status = pthread_mutex_destroy(&suc->mutex);
1432
 
        if (status)
1433
 
                fatal(status);
1434
 
 
1435
 
        status = pthread_cond_destroy(&suc->cond);
1436
 
        if (status)
1437
 
                fatal(status);
1438
 
 
1439
 
        return;
1440
 
}
1441
 
 
1442
 
static void handle_mounts_cleanup(void *arg)
1443
 
{
1444
 
        struct autofs_point *ap;
1445
 
        char path[PATH_MAX + 1];
1446
 
        char buf[MAX_ERR_BUF];
1447
 
        unsigned int clean = 0, submount, logopt;
1448
 
 
1449
 
        ap = (struct autofs_point *) arg;
1450
 
 
1451
 
        logopt = ap->logopt;
1452
 
        submount = ap->submount;
1453
 
 
1454
 
        strcpy(path, ap->path);
1455
 
        if (!submount && strcmp(ap->path, "/-") &&
1456
 
            ap->flags & MOUNT_FLAG_DIR_CREATED)
1457
 
                clean = 1;
1458
 
 
1459
 
        if (submount) {
1460
 
                /* We are finishing up */
1461
 
                ap->parent->submnt_count--;
1462
 
                list_del_init(&ap->mounts);
1463
 
        }
1464
 
 
1465
 
        master_remove_mapent(ap->entry);
1466
 
        master_source_unlock(ap->entry);
1467
 
 
1468
 
        destroy_logpri_fifo(ap);
1469
 
 
1470
 
        /*
1471
 
         * Submounts are detached threads and don't belong to the
1472
 
         * master map entry list so we need to free their resources
1473
 
         * here.
1474
 
         */
1475
 
        if (submount) {
1476
 
                mounts_mutex_unlock(ap->parent);
1477
 
                master_source_unlock(ap->parent->entry);
1478
 
                master_free_mapent_sources(ap->entry, 1);
1479
 
                master_free_mapent(ap->entry);
1480
 
        }
1481
 
        master_mutex_unlock();
1482
 
 
1483
 
        if (clean) {
1484
 
                if (rmdir(path) == -1) {
1485
 
                        char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
1486
 
                        warn(logopt, "failed to remove dir %s: %s",
1487
 
                             path, estr);
1488
 
                }
1489
 
        }
1490
 
 
1491
 
        info(logopt, "shut down path %s", path);
1492
 
 
1493
 
        /*
1494
 
         * If we are not a submount send a signal to the signal handler
1495
 
         * so it can join with any completed handle_mounts() threads and
1496
 
         * perform final cleanup.
1497
 
         */
1498
 
        if (!submount)
1499
 
                pthread_kill(state_mach_thid, SIGTERM);
1500
 
        
1501
 
        return;
1502
 
}
1503
 
 
1504
 
void *handle_mounts(void *arg)
1505
 
{
1506
 
        struct startup_cond *suc;
1507
 
        struct autofs_point *ap;
1508
 
        int cancel_state, status = 0;
1509
 
        char *root;
1510
 
 
1511
 
        suc = (struct startup_cond *) arg;
1512
 
 
1513
 
        ap = suc->ap;
1514
 
        root = strdup(suc->root);
1515
 
 
1516
 
        pthread_cleanup_push(return_start_status, suc);
1517
 
        pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel_state);
1518
 
 
1519
 
        status = pthread_mutex_lock(&suc->mutex);
1520
 
        if (status) {
1521
 
                logerr("failed to lock startup condition mutex!");
1522
 
                fatal(status);
1523
 
        }
1524
 
 
1525
 
        if (!root) {
1526
 
                crit(ap->logopt, "failed to alloc string root");
1527
 
                suc->status = 1;
1528
 
                pthread_setcancelstate(cancel_state, NULL);
1529
 
                pthread_exit(NULL);
1530
 
        }
1531
 
 
1532
 
        if (mount_autofs(ap, root) < 0) {
1533
 
                crit(ap->logopt, "mount of %s failed!", ap->path);
1534
 
                suc->status = 1;
1535
 
                umount_autofs(ap, root, 1);
1536
 
                free(root);
1537
 
                pthread_setcancelstate(cancel_state, NULL);
1538
 
                pthread_exit(NULL);
1539
 
        }
1540
 
 
1541
 
        free(root);
1542
 
 
1543
 
        if (ap->flags & MOUNT_FLAG_GHOST && ap->type != LKP_DIRECT)
1544
 
                info(ap->logopt, "ghosting enabled");
1545
 
 
1546
 
        suc->status = 0;
1547
 
        pthread_cleanup_pop(1);
1548
 
 
1549
 
        /* We often start several automounters at the same time.  Add some
1550
 
           randomness so we don't all expire at the same time. */
1551
 
        if (!ap->submount && ap->exp_timeout)
1552
 
                alarm_add(ap, ap->exp_runfreq + rand() % ap->exp_runfreq);
1553
 
 
1554
 
        pthread_setcancelstate(cancel_state, NULL);
1555
 
 
1556
 
        while (ap->state != ST_SHUTDOWN) {
1557
 
                if (handle_packet(ap)) {
1558
 
                        int ret, cur_state;
1559
 
 
1560
 
                        /*
1561
 
                         * If we're a submount we need to ensure our parent
1562
 
                         * doesn't try to mount us again until our shutdown
1563
 
                         * is complete and that any outstanding mounts are
1564
 
                         * completed before we try to shutdown.
1565
 
                         */
1566
 
                        pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cur_state);
1567
 
 
1568
 
                        master_mutex_lock();
1569
 
 
1570
 
                        if (ap->submount) {
1571
 
                                master_source_writelock(ap->parent->entry);
1572
 
                                mounts_mutex_lock(ap->parent);
1573
 
                        }
1574
 
 
1575
 
                        master_source_writelock(ap->entry);
1576
 
 
1577
 
                        if (ap->state != ST_SHUTDOWN) {
1578
 
                                if (!ap->submount)
1579
 
                                        alarm_add(ap, ap->exp_runfreq);
1580
 
                                /* Return to ST_READY is done immediately */
1581
 
                                st_add_task(ap, ST_READY);
1582
 
                                master_source_unlock(ap->entry);
1583
 
                                if (ap->submount) {
1584
 
                                        mounts_mutex_unlock(ap->parent);
1585
 
                                        master_source_unlock(ap->parent->entry);
1586
 
                                }
1587
 
 
1588
 
                                master_mutex_unlock();
1589
 
 
1590
 
                                pthread_setcancelstate(cur_state, NULL);
1591
 
                                continue;
1592
 
                        }
1593
 
 
1594
 
                        alarm_delete(ap);
1595
 
                        st_remove_tasks(ap);
1596
 
                        st_wait_task(ap, ST_ANY, 0);
1597
 
 
1598
 
                        /*
1599
 
                         * For a direct mount map all mounts have already gone
1600
 
                         * by the time we get here and since we only ever
1601
 
                         * umount direct mounts at shutdown there is no need
1602
 
                         * to check for possible recovery.
1603
 
                         */
1604
 
                        if (ap->type == LKP_DIRECT) {
1605
 
                                umount_autofs(ap, NULL, 1);
1606
 
                                break;
1607
 
                        }
1608
 
 
1609
 
                        /*
1610
 
                         * If umount_autofs returns non-zero it wasn't able
1611
 
                         * to complete the umount and has left the mount intact
1612
 
                         * so we can continue. This can happen if a lookup
1613
 
                         * occurs while we're trying to umount.
1614
 
                         */
1615
 
                        ret = umount_autofs(ap, NULL, 1);
1616
 
                        if (!ret)
1617
 
                                break;
1618
 
 
1619
 
                        /* Failed shutdown returns to ready */
1620
 
                        warn(ap->logopt,
1621
 
                             "can't shutdown: filesystem %s still busy",
1622
 
                             ap->path);
1623
 
                        if (!ap->submount)
1624
 
                                alarm_add(ap, ap->exp_runfreq);
1625
 
                        /* Return to ST_READY is done immediately */
1626
 
                        st_add_task(ap, ST_READY);
1627
 
                        master_source_unlock(ap->entry);
1628
 
                        if (ap->submount) {
1629
 
                                mounts_mutex_unlock(ap->parent);
1630
 
                                master_source_unlock(ap->parent->entry);
1631
 
                        }
1632
 
 
1633
 
                        master_mutex_unlock();
1634
 
 
1635
 
                        pthread_setcancelstate(cur_state, NULL);
1636
 
 
1637
 
                }
1638
 
        }
1639
 
 
1640
 
        handle_mounts_cleanup(ap);
1641
 
 
1642
 
        return NULL;
1643
 
}
1644
 
 
1645
 
static void key_thread_stdenv_vars_destroy(void *arg)
1646
 
{
1647
 
        struct thread_stdenv_vars *tsv;
1648
 
 
1649
 
        tsv = (struct thread_stdenv_vars *) arg;
1650
 
        if (tsv->user)
1651
 
                free(tsv->user);
1652
 
        if (tsv->group)
1653
 
                free(tsv->group);
1654
 
        if (tsv->home)
1655
 
                free(tsv->home);
1656
 
        free(tsv);
1657
 
        return;
1658
 
}
1659
 
 
1660
 
static void usage(void)
1661
 
{
1662
 
        fprintf(stderr,
1663
 
                "Usage: %s [options] [master_map_name]\n"
1664
 
                "       -h --help       this text\n"
1665
 
                "       -p --pid-file f write process id to file f\n"
1666
 
                "       -t --timeout n  auto-unmount in n seconds (0-disable)\n"
1667
 
                "       -v --verbose    be verbose\n"
1668
 
                "       -d --debug      log debuging info\n"
1669
 
                "       -D --define     define global macro variable\n"
1670
 
                "       -f --foreground do not fork into background\n"
1671
 
                "       -r --random-multimount-selection\n"
1672
 
                "                       use ramdom replicated server selection\n"
1673
 
                "       -n --negative-timeout n\n"
1674
 
                "                       set the timeout for failed key lookups.\n"
1675
 
                "       -O --global-options\n"
1676
 
                "                       specify global mount options\n"
1677
 
                "       -l --set-log-priority priority path [path,...]\n"
1678
 
                "                       set daemon log verbosity\n"
1679
 
                "       -C --dont-check-daemon\n"
1680
 
                "                       don't check if daemon is already running\n"
1681
 
                "       -V --version    print version, build config and exit\n"
1682
 
                , program);
1683
 
}
1684
 
 
1685
 
static void show_build_info(void)
1686
 
{
1687
 
        char buf[2048];
1688
 
        int count = 0;
1689
 
 
1690
 
        printf("\nLinux automount version %s\n", version);
1691
 
 
1692
 
        printf("\nDirectories:\n");
1693
 
        printf("\tconfig dir:\t%s\n", confdir);
1694
 
        printf("\tmaps dir:\t%s\n", mapdir);
1695
 
        printf("\tmodules dir:\t%s\n", libdir);
1696
 
 
1697
 
        printf("\nCompile options:\n  ");
1698
 
 
1699
 
        memset(buf, 0, 2048);
1700
 
 
1701
 
#ifndef ENABLE_MOUNT_LOCKING
1702
 
        printf("DISABLE_MOUNT_LOCKING ");
1703
 
        count = 22;
1704
 
#endif
1705
 
 
1706
 
#ifdef ENABLE_FORCED_SHUTDOWN
1707
 
        printf("ENABLE_FORCED_SHUTDOWN ");
1708
 
        count = count + 23;
1709
 
#endif
1710
 
 
1711
 
#ifdef ENABLE_IGNORE_BUSY_MOUNTS
1712
 
        printf("ENABLE_IGNORE_BUSY_MOUNTS ");
1713
 
        count = count + 26;
1714
 
 
1715
 
        if (count > 60) {
1716
 
                printf("\n  ");
1717
 
                count = 0;
1718
 
        }
1719
 
#endif
1720
 
 
1721
 
 
1722
 
#ifdef WITH_HESIOD
1723
 
        printf("WITH_HESIOD ");
1724
 
        count = count + 12;
1725
 
 
1726
 
        if (count > 60) {
1727
 
                printf("\n  ");
1728
 
                count = 0;
1729
 
        }
1730
 
#endif
1731
 
 
1732
 
#ifdef WITH_LDAP
1733
 
        printf("WITH_LDAP ");
1734
 
        count = count + 10;
1735
 
 
1736
 
        if (count > 60) {
1737
 
                printf("\n  ");
1738
 
                count = 0;
1739
 
        }
1740
 
#endif
1741
 
 
1742
 
#ifdef WITH_SASL
1743
 
        printf("WITH_SASL ");
1744
 
        count = count + 10;
1745
 
 
1746
 
        if (count > 60) {
1747
 
                printf("\n  ");
1748
 
                count = 0;
1749
 
        }
1750
 
#endif
1751
 
 
1752
 
#ifdef WITH_DMALLOC
1753
 
        printf("WITH_DMALLOC ");
1754
 
        count = count + 13;
1755
 
 
1756
 
        if (count > 60) {
1757
 
                printf("\n  ");
1758
 
                count = 0;
1759
 
        }
1760
 
#endif
1761
 
 
1762
 
#ifdef LIBXML2_WORKAROUND
1763
 
        printf("LIBXML2_WORKAROUND ");
1764
 
        count = count + 19;
1765
 
 
1766
 
        if (count > 60) {
1767
 
                printf("\n  ");
1768
 
                count = 0;
1769
 
        }
1770
 
#endif
1771
 
 
1772
 
#ifdef WITH_LIBTIRPC
1773
 
        printf("WITH_LIBTIRPC ");
1774
 
        count = count + 14;
1775
 
#endif
1776
 
 
1777
 
        printf("\n\n");
1778
 
 
1779
 
        return;
1780
 
}
1781
 
 
1782
 
typedef struct _code {
1783
 
        char    *c_name;
1784
 
        int     c_val;
1785
 
} CODE;
1786
 
 
1787
 
CODE prioritynames[] = {
1788
 
        { "alert",      LOG_ALERT },
1789
 
        { "crit",       LOG_CRIT },
1790
 
        { "debug",      LOG_DEBUG },
1791
 
        { "emerg",      LOG_EMERG },
1792
 
        { "err",        LOG_ERR },
1793
 
        { "error",      LOG_ERR },              /* DEPRECATED */
1794
 
        { "info",       LOG_INFO },
1795
 
        { "notice",     LOG_NOTICE },
1796
 
        { "panic",      LOG_EMERG },            /* DEPRECATED */
1797
 
        { "warn",       LOG_WARNING },          /* DEPRECATED */
1798
 
        { "warning",    LOG_WARNING },
1799
 
        { NULL,         -1 },
1800
 
};
1801
 
 
1802
 
static int convert_log_priority(char *priority_name)
1803
 
{
1804
 
        CODE *priority_mapping;
1805
 
 
1806
 
        for (priority_mapping = prioritynames;
1807
 
             priority_mapping->c_name != NULL;
1808
 
             priority_mapping++) {
1809
 
 
1810
 
                if (!strcasecmp(priority_name, priority_mapping->c_name))
1811
 
                        return priority_mapping->c_val;
1812
 
        }
1813
 
 
1814
 
        return -1;
1815
 
}
1816
 
 
1817
 
int main(int argc, char *argv[])
1818
 
{
1819
 
        int res, opt, status;
1820
 
        int logpri = -1;
1821
 
        unsigned ghost, logging, daemon_check;
1822
 
        unsigned foreground, have_global_options;
1823
 
        time_t timeout;
1824
 
        time_t age = time(NULL);
1825
 
        struct rlimit rlim;
1826
 
        static const struct option long_options[] = {
1827
 
                {"help", 0, 0, 'h'},
1828
 
                {"pid-file", 1, 0, 'p'},
1829
 
                {"timeout", 1, 0, 't'},
1830
 
                {"verbose", 0, 0, 'v'},
1831
 
                {"debug", 0, 0, 'd'},
1832
 
                {"define", 1, 0, 'D'},
1833
 
                {"foreground", 0, 0, 'f'},
1834
 
                {"random-multimount-selection", 0, 0, 'r'},
1835
 
                {"negative-timeout", 1, 0, 'n'},
1836
 
                {"global-options", 1, 0, 'O'},
1837
 
                {"version", 0, 0, 'V'},
1838
 
                {"set-log-priority", 1, 0, 'l'},
1839
 
                {"dont-check-daemon", 0, 0, 'C'},
1840
 
                {"force", 0, 0, 'F'},
1841
 
                {0, 0, 0, 0}
1842
 
        };
1843
 
 
1844
 
        sigfillset(&block_sigs);
1845
 
        /* allow for the dropping of core files */
1846
 
        sigdelset(&block_sigs, SIGABRT);
1847
 
        sigdelset(&block_sigs, SIGBUS);
1848
 
        sigdelset(&block_sigs, SIGSEGV);
1849
 
        sigdelset(&block_sigs, SIGILL);
1850
 
        sigdelset(&block_sigs, SIGFPE);
1851
 
        sigdelset(&block_sigs, SIGTRAP);
1852
 
        sigprocmask(SIG_BLOCK, &block_sigs, NULL);
1853
 
 
1854
 
        program = argv[0];
1855
 
 
1856
 
        defaults_read_config(0);
1857
 
 
1858
 
        kpkt_len = get_kpkt_len();
1859
 
        timeout = defaults_get_timeout();
1860
 
        ghost = defaults_get_browse_mode();
1861
 
        logging = defaults_get_logging();
1862
 
        global_random_selection = 0;
1863
 
        global_options = NULL;
1864
 
        have_global_options = 0;
1865
 
        foreground = 0;
1866
 
        daemon_check = 1;
1867
 
 
1868
 
        opterr = 0;
1869
 
        while ((opt = getopt_long(argc, argv, "+hp:t:vdD:fVrO:l:n:CF", long_options, NULL)) != EOF) {
1870
 
                switch (opt) {
1871
 
                case 'h':
1872
 
                        usage();
1873
 
                        exit(0);
1874
 
 
1875
 
                case 'p':
1876
 
                        pid_file = optarg;
1877
 
                        break;
1878
 
 
1879
 
                case 't':
1880
 
                        timeout = getnumopt(optarg, opt);
1881
 
                        break;
1882
 
 
1883
 
                case 'v':
1884
 
                        logging |= LOGOPT_VERBOSE;
1885
 
                        break;
1886
 
 
1887
 
                case 'd':
1888
 
                        logging |= LOGOPT_DEBUG;
1889
 
                        break;
1890
 
 
1891
 
                case 'D':
1892
 
                        macro_parse_globalvar(optarg);
1893
 
                        break;
1894
 
 
1895
 
                case 'f':
1896
 
                        foreground = 1;
1897
 
                        break;
1898
 
 
1899
 
                case 'V':
1900
 
                        show_build_info();
1901
 
                        exit(0);
1902
 
 
1903
 
                case 'r':
1904
 
                        global_random_selection = 1;
1905
 
                        break;
1906
 
 
1907
 
                case 'n':
1908
 
                        global_negative_timeout = getnumopt(optarg, opt);
1909
 
                        break;
1910
 
 
1911
 
                case 'O':
1912
 
                        if (!have_global_options) {
1913
 
                                global_options = strdup(optarg);
1914
 
                                have_global_options = 1;
1915
 
                                break;
1916
 
                        }
1917
 
                        printf("%s: global options already specified.\n",
1918
 
                                program);
1919
 
                        break;
1920
 
 
1921
 
                case 'l':
1922
 
                        if (isalpha(*optarg)) {
1923
 
                                logpri = convert_log_priority(optarg);
1924
 
                                if (logpri < 0) {
1925
 
                                        fprintf(stderr, "Invalid log priority:"
1926
 
                                                " %s\n", optarg);
1927
 
                                        exit(1);
1928
 
                                }
1929
 
                        } else if (isdigit(*optarg)) {
1930
 
                                logpri = getnumopt(optarg, opt);
1931
 
                        } else {
1932
 
                                fprintf(stderr, "non-alphanumeric character "
1933
 
                                        "found in log priority.  Aborting.\n");
1934
 
                                exit(1);
1935
 
                        }
1936
 
                        break;
1937
 
 
1938
 
                case 'C':
1939
 
                        daemon_check = 0;
1940
 
                        break;
1941
 
 
1942
 
                case 'F':
1943
 
                        do_force_unlink = 1;
1944
 
                        break;
1945
 
 
1946
 
                case '?':
1947
 
                case ':':
1948
 
                        printf("%s: Ambiguous or unknown options\n", program);
1949
 
                        exit(1);
1950
 
                }
1951
 
        }
1952
 
 
1953
 
        if (logging & LOGOPT_VERBOSE)
1954
 
                set_log_verbose();
1955
 
 
1956
 
        if (logging & LOGOPT_DEBUG)
1957
 
                set_log_debug();
1958
 
 
1959
 
        if (geteuid() != 0) {
1960
 
                fprintf(stderr, "%s: this program must be run by root.\n",
1961
 
                        program);
1962
 
                exit(1);
1963
 
        }
1964
 
 
1965
 
        /* Remove the options */
1966
 
        argv += optind;
1967
 
        argc -= optind;
1968
 
 
1969
 
        if (logpri >= 0) {
1970
 
                int exit_code = 0;
1971
 
                int i;
1972
 
 
1973
 
                /*
1974
 
                 * The remaining argv elements are the paths for which
1975
 
                 * log priorities must be changed.
1976
 
                 */
1977
 
                for (i = 0; i < argc; i++) {
1978
 
                        if (set_log_priority(argv[i], logpri) < 0)
1979
 
                                exit_code = 1;
1980
 
                }
1981
 
                if (argc < 1) {
1982
 
                        fprintf(stderr,
1983
 
                                "--set-log-priority requires a path.\n");
1984
 
                        exit_code = 1;
1985
 
                }
1986
 
                exit(exit_code);
1987
 
        }
1988
 
 
1989
 
#if 0
1990
 
        if (!load_autofs4_module()) {
1991
 
                fprintf(stderr, "%s: can't load %s filesystem module.\n",
1992
 
                        program, FS_MODULE_NAME);
1993
 
                exit(1);
1994
 
        }
1995
 
#endif
1996
 
 
1997
 
        if (!query_kproto_ver() || get_kver_major() < 5) {
1998
 
                fprintf(stderr,
1999
 
                        "%s: test mount forbidden or "
2000
 
                        "incorrect kernel protocol version, "
2001
 
                        "kernel protocol version 5.00 or above required.\n",
2002
 
                        program);
2003
 
                exit(1);
2004
 
        }
2005
 
 
2006
 
        rlim.rlim_cur = MAX_OPEN_FILES;
2007
 
        rlim.rlim_max = MAX_OPEN_FILES;
2008
 
        res = setrlimit(RLIMIT_NOFILE, &rlim);
2009
 
        if (res)
2010
 
                warn(logging,
2011
 
                     "can't increase open file limit - continuing");
2012
 
 
2013
 
#if ENABLE_CORES
2014
 
        rlim.rlim_cur = RLIM_INFINITY;
2015
 
        rlim.rlim_max = RLIM_INFINITY;
2016
 
        res = setrlimit(RLIMIT_CORE, &rlim);
2017
 
        if (res)
2018
 
                warn(logging,
2019
 
                     "can't increase core file limit - continuing");
2020
 
#endif
2021
 
 
2022
 
        become_daemon(foreground, daemon_check);
2023
 
 
2024
 
        if (argc == 0)
2025
 
                master_list = master_new(NULL, timeout, ghost);
2026
 
        else
2027
 
                master_list = master_new(argv[0], timeout, ghost);
2028
 
 
2029
 
        if (!master_list) {
2030
 
                logerr("%s: can't create master map %s",
2031
 
                        program, argv[0]);
2032
 
                res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
2033
 
                close(start_pipefd[1]);
2034
 
                release_flag_file();
2035
 
                exit(1);
2036
 
        }
2037
 
 
2038
 
        if (pthread_attr_init(&th_attr)) {
2039
 
                logerr("%s: failed to init thread attribute struct!",
2040
 
                     program);
2041
 
                res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
2042
 
                close(start_pipefd[1]);
2043
 
                release_flag_file();
2044
 
                exit(1);
2045
 
        }
2046
 
 
2047
 
        if (pthread_attr_init(&th_attr_detached)) {
2048
 
                logerr("%s: failed to init thread attribute struct!",
2049
 
                     program);
2050
 
                res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
2051
 
                close(start_pipefd[1]);
2052
 
                release_flag_file();
2053
 
                exit(1);
2054
 
        }
2055
 
 
2056
 
        if (pthread_attr_setdetachstate(
2057
 
                        &th_attr_detached, PTHREAD_CREATE_DETACHED)) {
2058
 
                logerr("%s: failed to set detached thread attribute!",
2059
 
                     program);
2060
 
                res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
2061
 
                close(start_pipefd[1]);
2062
 
                release_flag_file();
2063
 
                exit(1);
2064
 
        }
2065
 
 
2066
 
#ifdef _POSIX_THREAD_ATTR_STACKSIZE
2067
 
        if (pthread_attr_setstacksize(
2068
 
                        &th_attr_detached, PTHREAD_STACK_MIN*64)) {
2069
 
                logerr("%s: failed to set stack size thread attribute!",
2070
 
                       program);
2071
 
                res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
2072
 
                close(start_pipefd[1]);
2073
 
                release_flag_file();
2074
 
                exit(1);
2075
 
        }
2076
 
#endif
2077
 
 
2078
 
        info(logging, "Starting automounter version %s, master map %s",
2079
 
                version, master_list->name);
2080
 
        info(logging, "using kernel protocol version %d.%02d",
2081
 
                get_kver_major(), get_kver_minor());
2082
 
 
2083
 
        status = pthread_key_create(&key_thread_stdenv_vars,
2084
 
                                key_thread_stdenv_vars_destroy);
2085
 
        if (status) {
2086
 
                logerr("%s: failed to create thread data key for std env vars!",
2087
 
                       program);
2088
 
                master_kill(master_list);
2089
 
                res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
2090
 
                close(start_pipefd[1]);
2091
 
                release_flag_file();
2092
 
                exit(1);
2093
 
        }
2094
 
 
2095
 
        init_ioctl_ctl();
2096
 
 
2097
 
        if (!alarm_start_handler()) {
2098
 
                logerr("%s: failed to create alarm handler thread!", program);
2099
 
                master_kill(master_list);
2100
 
                res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
2101
 
                close(start_pipefd[1]);
2102
 
                release_flag_file();
2103
 
                exit(1);
2104
 
        }
2105
 
 
2106
 
        if (!st_start_handler()) {
2107
 
                logerr("%s: failed to create FSM handler thread!", program);
2108
 
                master_kill(master_list);
2109
 
                res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
2110
 
                close(start_pipefd[1]);
2111
 
                release_flag_file();
2112
 
                exit(1);
2113
 
        }
2114
 
 
2115
 
#if defined(WITH_LDAP) && defined(LIBXML2_WORKAROUND)
2116
 
        void *dh_xml2 = dlopen("libxml2.so", RTLD_NOW);
2117
 
        if (!dh_xml2)
2118
 
                dh_xml2 = dlopen("libxml2.so.2", RTLD_NOW);
2119
 
        if (dh_xml2)
2120
 
                xmlInitParser();
2121
 
#endif
2122
 
#ifdef TIRPC_WORKAROUND
2123
 
        void *dh_tirpc = dlopen("libitirpc.so", RTLD_NOW);
2124
 
        if (!dh_tirpc)
2125
 
                dh_tirpc = dlopen("libitirpc.so.1", RTLD_NOW);
2126
 
#endif
2127
 
 
2128
 
        if (!master_read_master(master_list, age, 0)) {
2129
 
                master_kill(master_list);
2130
 
                *pst_stat = 3;
2131
 
                res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
2132
 
                close(start_pipefd[1]);
2133
 
                release_flag_file();
2134
 
                exit(3);
2135
 
        }
2136
 
 
2137
 
        /*
2138
 
         * Mmm ... reset force unlink umount so we don't also do this
2139
 
         * in future when we receive a HUP signal.
2140
 
         */
2141
 
        do_force_unlink = 0;
2142
 
 
2143
 
        st_stat = 0;
2144
 
        res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
2145
 
        close(start_pipefd[1]);
2146
 
 
2147
 
        state_mach_thid = pthread_self();
2148
 
        statemachine(NULL);
2149
 
 
2150
 
        master_kill(master_list);
2151
 
 
2152
 
        if (pid_file) {
2153
 
                unlink(pid_file);
2154
 
                pid_file = NULL;
2155
 
        }
2156
 
        closelog();
2157
 
        release_flag_file();
2158
 
 
2159
 
#ifdef TIRPC_WORKAROUND
2160
 
        if (dh_tirpc)
2161
 
                dlclose(dh_tirpc);
2162
 
#endif
2163
 
#if defined(WITH_LDAP) && defined( LIBXML2_WORKAROUND)
2164
 
        if (dh_xml2) {
2165
 
                xmlCleanupParser();
2166
 
                dlclose(dh_xml2);
2167
 
        }
2168
 
#endif
2169
 
        close_ioctl_ctl();
2170
 
 
2171
 
        info(logging, "autofs stopped");
2172
 
 
2173
 
        exit(0);
2174
 
}