1
/* ----------------------------------------------------------------------- *
3
* automount.c - Linux automounter daemon
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>
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
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.
20
* ----------------------------------------------------------------------- */
28
#include <sys/ioctl.h>
30
#include <sys/types.h>
34
#include <sys/resource.h>
38
#include <sys/utsname.h>
40
#include "automount.h"
41
#if defined(LIBXML2_WORKAROUND) || defined(TIRPC_WORKAROUND)
44
#include <libxml/parser.h>
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 */
54
/* autofs fifo name prefix */
55
const char *fifodir = AUTOFS_FIFO_DIR "/autofs.fifo";
57
const char *global_options; /* Global option, from command line */
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 */
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;
70
static sigset_t block_sigs;
72
/* Pre-calculated kernel packet length */
73
static size_t kpkt_len;
75
/* Does kernel know about SOCK_CLOEXEC and friends */
76
static int cloexec_works = 0;
78
/* Attributes for creating detached and joinable threads */
79
pthread_attr_t th_attr;
80
pthread_attr_t th_attr_detached;
82
struct master_readmap_cond mrc = {
83
PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, 0, NULL, 0, 0, 0, 0};
85
struct startup_cond suc = {
86
PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, 0, 0};
88
pthread_key_t key_thread_stdenv_vars;
90
#define MAX_OPEN_FILES 10240
92
int aquire_flag_file(void);
93
void release_flag_file(void);
94
static int umount_all(struct autofs_point *ap, int force);
96
extern struct master *master_list;
98
static int do_mkdir(const char *parent, const char *path, mode_t mode)
104
/* If path exists we're done */
105
status = stat(path, &st);
107
if (!S_ISDIR(st.st_mode))
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.
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) {
133
int mkdir_path(const char *path, mode_t mode)
136
char parent[PATH_MAX];
137
const char *cp = path, *lcp = path;
138
char *bp = buf, *pp = parent;
143
if (cp != path && (*cp == '/' || *cp == '\0')) {
144
memcpy(bp, lcp, cp - lcp);
147
if (!do_mkdir(parent, buf, mode)) {
149
memcpy(pp, lcp, cp - lcp);
157
memcpy(pp, lcp, cp - lcp);
162
} while (*cp++ != '\0');
167
/* Remove as much as possible of a path */
168
int rmdir_path(struct autofs_point *ap, const char *path, dev_t dev)
170
int len = strlen(path);
184
* Before removing anything, perform some sanity checks to
185
* ensure that we are looking at files in the automount
188
memset(&st, 0, sizeof(st));
189
if (lstat(buf, &st) != 0) {
190
crit(ap->logopt, "lstat of %s failed", buf);
194
/* Termination condition removing full path within autofs fs */
195
if (st.st_dev != dev)
198
if (statfs(buf, &fs) != 0) {
199
error(ap->logopt, "could not stat fs of %s", buf);
203
if (fs.f_type != (__SWORD_TYPE) AUTOFS_SUPER_MAGIC) {
204
crit(ap->logopt, "attempt to remove directory from a "
205
"non-autofs filesystem!");
207
"requestor dev == %llu, \"%s\" owner dev == %llu",
208
dev, buf, st.st_dev);
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)
217
if (rmdir(buf) == -1) {
218
if (first && errno == ENOTDIR) {
220
* Ensure that we will only remove
223
if (S_ISLNK(st.st_mode)) {
224
if (unlink(buf) == -1)
228
"file \"%s\" is neither a directory"
229
" nor a symbolic link. mode %d",
236
* If we fail to remove a directory for any reason,
237
* we need to return an error.
243
} while ((cp = strrchr(buf, '/')) != NULL && cp != buf);
248
/* Like ftw, except fn gets called twice: before a directory is
249
entered, and after. If the before call returns 0, the directory
251
static int walk_tree(const char *base, int (*fn) (unsigned logopt,
253
const struct stat * st,
254
int, void *), int incl, unsigned logopt, void *arg)
256
char buf[PATH_MAX + 1];
257
struct stat st, *pst = &st;
260
if (!is_mounted(_PATH_MOUNTED, base, MNTS_REAL))
261
ret = lstat(base, pst);
267
if (ret != -1 && (fn) (logopt, base, pst, 0, arg)) {
268
if (S_ISDIR(st.st_mode)) {
272
n = scandir(base, &de, 0, alphasort);
279
if (strcmp(de[n]->d_name, ".") == 0 ||
280
strcmp(de[n]->d_name, "..") == 0) {
286
ret = cat_path(buf, size, base, de[n]->d_name);
295
walk_tree(buf, fn, 1, logopt, arg);
301
(fn) (logopt, base, pst, 1, arg);
306
static int rm_unwanted_fn(unsigned logopt, const char *file, const struct stat *st, int when, void *arg)
308
dev_t dev = *(dev_t *) arg;
309
char buf[MAX_ERR_BUF];
316
if (st->st_dev != dev)
321
if (lstat(file, &newst)) {
322
crit(logopt, "unable to stat file, possible race condition");
326
if (newst.st_dev != dev) {
327
crit(logopt, "file %s has the wrong device, possible race condition",
332
if (S_ISDIR(newst.st_mode)) {
333
debug(logopt, "removing directory %s", file);
335
char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
337
"unable to remove directory %s: %s", file, estr);
340
} else if (S_ISREG(newst.st_mode)) {
341
crit(logopt, "attempting to remove files from a mounted "
342
"directory. file %s", file);
344
} else if (S_ISLNK(newst.st_mode)) {
345
debug(logopt, "removing symlink %s", file);
351
void rm_unwanted(unsigned logopt, const char *path, int incl, dev_t dev)
353
walk_tree(path, rm_unwanted_fn, incl, logopt, &dev);
356
struct counter_args {
361
static int counter_fn(unsigned logopt, const char *file, const struct stat *st, int when, void *arg)
363
struct counter_args *counter = (struct counter_args *) arg;
365
if (!st || (S_ISLNK(st->st_mode) || (S_ISDIR(st->st_mode)
366
&& st->st_dev != counter->dev))) {
374
/* Count mounted filesystems and symlinks */
375
int count_mounts(unsigned logopt, const char *path, dev_t dev)
377
struct counter_args counter;
382
if (walk_tree(path, counter_fn, 0, logopt, &counter) == -1)
385
return counter.count;
388
static void check_rm_dirs(struct autofs_point *ap, const char *path, int incl)
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
399
if (!incl && ap->submount)
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);
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)
414
struct map_source *map;
415
struct mapent_cache *mc;
418
if (ap->type == LKP_INDIRECT)
419
key = strrchr(path, '/') + 1;
423
map = ap->entry->maps;
425
struct mapent *me = NULL;
427
/* Skip current, in-use cache */
428
if (ap->entry->age <= map->age) {
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);
448
static int umount_subtree_mounts(struct autofs_point *ap, const char *path, unsigned int is_autofs_fs)
450
struct mapent_cache *mc;
452
unsigned int is_mm_root;
455
me = lookup_source_mapent(ap, path, LKP_DISTINCT);
459
ind_key = strrchr(path, '/');
463
me = lookup_source_mapent(ap, ind_key, LKP_NORMAL);
469
is_mm_root = (me->multi == me);
473
pthread_cleanup_push(cache_lock_cleanup, mc);
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);
486
strcpy(root, me->multi->key);
491
base = me->key + strlen(root);
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)) {
498
"some offset mounts still present under %s", path);
501
cache_multi_unlock(me->parent);
502
pthread_setcancelstate(cur_state, NULL);
505
pthread_cleanup_pop(1);
507
if (left || is_autofs_fs)
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.
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);
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)
529
struct mapent_cache *nc;
533
debug(ap->logopt, "path %s incl %d", path, incl);
535
nc = ap->entry->master->nc;
537
if (cache_lookup_distinct(nc, path)) {
544
if (master_find_submount(ap, path))
550
* If we are a submount we need to umount any offsets our
551
* parent may have mounted over top of us.
554
left += umount_subtree_mounts(ap->parent, path, is_autofs_fs);*/
556
left += umount_subtree_mounts(ap, path, is_autofs_fs);
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);
567
static int umount_all(struct autofs_point *ap, int force)
571
left = umount_multi(ap, ap->path, 0);
573
warn(ap->logopt, "could not unmount %d dirs under %s",
579
int umount_autofs(struct autofs_point *ap, const char *root, int force)
583
if (ap->state == ST_INIT)
587
* Since lookup.c is lazy about closing lookup modules
588
* to prevent unneeded opens, we need to clean them up
591
lookup_close_lookup(ap);
593
if (ap->type == LKP_INDIRECT) {
594
if (umount_all(ap, force) && !force)
596
ret = umount_autofs_indirect(ap, root);
598
ret = umount_autofs_direct(ap);
603
static size_t get_kpkt_len(void)
605
size_t pkt_len = sizeof(struct autofs_v5_packet);
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)
622
static int fullread(int fd, void *ptr, size_t len)
624
char *buf = (char *) ptr;
627
ssize_t r = read(fd, buf, len);
642
static char *automount_path_to_fifo(unsigned logopt, const char *path)
645
int name_len = strlen(path) + strlen(fifodir) + 1;
648
fifo_name = malloc(name_len);
651
ret = snprintf(fifo_name, name_len, "%s%s", fifodir, path);
652
if (ret >= name_len) {
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);
660
* An automount path can be made up of subdirectories. So, to
661
* create the fifo name, we will just replace instances of '/' with
664
p = fifo_name + strlen(fifodir);
671
debug(logopt, "fifo name %s",fifo_name);
676
static int create_logpri_fifo(struct autofs_point *ap)
681
char buf[MAX_ERR_BUF];
683
fifo_name = automount_path_to_fifo(ap->logopt, ap->path);
685
crit(ap->logopt, "Failed to allocate memory!");
686
goto out_free; /* free(NULL) is okay */
689
ret = unlink(fifo_name);
690
if (ret != 0 && errno != ENOENT) {
692
"Failed to unlink FIFO. Is the automount daemon "
697
ret = mkfifo(fifo_name, S_IRUSR|S_IWUSR);
699
char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
701
"mkfifo for %s failed: %s", fifo_name, estr);
705
fd = open_fd(fifo_name, O_RDWR|O_NONBLOCK);
707
char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
709
"Failed to open %s: %s", fifo_name, estr);
715
ap->logpri_fifo = fd;
722
int destroy_logpri_fifo(struct autofs_point *ap)
725
int fd = ap->logpri_fifo;
727
char buf[MAX_ERR_BUF];
732
fifo_name = automount_path_to_fifo(ap->logopt, ap->path);
734
crit(ap->logopt, "Failed to allocate memory!");
735
goto out_free; /* free(NULL) is okay */
738
ap->logpri_fifo = -1;
742
char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
744
"close for fifo %s: %s", fifo_name, estr);
747
ret = unlink(fifo_name);
750
"Failed to unlink FIFO. Was the fifo created OK?");
758
static void handle_fifo_message(struct autofs_point *ap, int fd)
761
char buffer[PIPE_BUF];
764
char buf[MAX_ERR_BUF];
766
memset(buffer, 0, sizeof(buffer));
767
ret = read(fd, &buffer, sizeof(buffer));
769
char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
770
warn(ap->logopt, "read on fifo returned error: %s", estr);
775
debug(ap->logopt, "expected 2 bytes, received %d.", ret);
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");
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.");
792
if (pri > LOG_DEBUG || pri < LOG_EMERG) {
793
debug(ap->logopt, "invalid log priority (%ld) received "
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.
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);
811
set_log_verbose_ap(ap);
812
info(ap->logopt, "Verbose logging set for %s", ap->path);
815
if (ap->logopt & LOGOPT_ANY)
816
info(ap->logopt, "Basic logging set for %s", ap->path);
821
static int set_log_priority(const char *path, int priority)
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");
834
* This is an ascii based protocol, so we want the string
835
* representation of the integer log priority.
837
snprintf(buf, sizeof(buf), "%d", priority);
839
fifo_name = automount_path_to_fifo(LOGOPT_NONE, path);
841
fprintf(stderr, "%s: Failed to allocate memory!\n",
847
* Specify O_NONBLOCK so that the open will fail if there is no
848
* daemon reading from the other side of the FIFO.
850
fd = open(fifo_name, O_WRONLY|O_NONBLOCK);
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__);
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",
870
fprintf(stdout, "Successfully set log priority for %s.\n", path);
875
static int get_pkt(struct autofs_point *ap, union autofs_v5_packet_union *pkt)
877
struct pollfd fds[3];
879
char buf[MAX_ERR_BUF];
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;
891
if (poll(fds, pollfds, -1) == -1) {
895
estr = strerror_r(errno, buf, MAX_ERR_BUF);
896
logerr("poll failed: %s", estr);
900
if (fds[1].revents & POLLIN) {
901
enum states next_state;
902
size_t read_size = sizeof(next_state);
905
next_state = ST_INVAL;
909
state_pipe = ap->state_pipe[0];
911
if (fullread(state_pipe, &next_state, read_size)) {
918
if (next_state == ST_SHUTDOWN)
922
if (fds[0].revents & POLLIN)
923
return fullread(ap->pipefd, pkt, kpkt_len);
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);
932
int do_expire(struct autofs_point *ap, const char *name, int namelen)
938
len = ncat_path(buf, sizeof(buf), ap->path, name, namelen);
940
len = snprintf(buf, PATH_MAX, "%s", name);
946
crit(ap->logopt, "path to long for buffer");
950
info(ap->logopt, "expiring path %s", buf);
952
pthread_cleanup_push(master_source_lock_cleanup, ap->entry);
953
master_source_readlock(ap->entry);
954
ret = umount_multi(ap, buf, 1);
956
info(ap->logopt, "expired %s", buf);
958
warn(ap->logopt, "couldn't complete expire of %s", buf);
959
pthread_cleanup_pop(1);
964
static int autofs_init_ap(struct autofs_point *ap)
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);
974
ap->pipefd = ap->kpipefd = ap->ioctlfd = -1;
976
/* Pipe for kernel communications */
977
if (open_pipe(pipefd) < 0) {
979
"failed to create commumication pipe for autofs path %s",
984
ap->pipefd = pipefd[0];
985
ap->kpipefd = pipefd[1];
987
/* Pipe state changes from signal handler to main loop */
988
if (open_pipe(ap->state_pipe) < 0) {
990
"failed create state pipe for autofs path %s", ap->path);
992
close(ap->kpipefd); /* Close kernel pipe end */
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);
1004
static int mount_autofs(struct autofs_point *ap, const char *root)
1008
if (autofs_init_ap(ap) != 0)
1011
if (ap->type == LKP_DIRECT)
1012
status = mount_autofs_direct(ap);
1014
status = mount_autofs_indirect(ap, root);
1019
st_add_task(ap, ST_READY);
1024
static int handle_packet(struct autofs_point *ap)
1026
union autofs_v5_packet_union pkt;
1028
if (get_pkt(ap, &pkt))
1031
debug(ap->logopt, "type = %d", pkt.hdr.type);
1033
switch (pkt.hdr.type) {
1034
case autofs_ptype_missing_indirect:
1035
return handle_packet_missing_indirect(ap, &pkt.v5_packet);
1037
case autofs_ptype_missing_direct:
1038
return handle_packet_missing_direct(ap, &pkt.v5_packet);
1040
case autofs_ptype_expire_indirect:
1041
return handle_packet_expire_indirect(ap, &pkt.v5_packet);
1043
case autofs_ptype_expire_direct:
1044
return handle_packet_expire_direct(ap, &pkt.v5_packet);
1046
error(ap->logopt, "unknown packet type %d", pkt.hdr.type);
1050
static void become_daemon(unsigned foreground, unsigned daemon_check)
1053
char buf[MAX_ERR_BUF];
1057
/* Don't BUSY any directories unnecessarily */
1059
fprintf(stderr, "%s: failed change working directory.\n",
1064
if (open_pipe(start_pipefd) < 0) {
1065
fprintf(stderr, "%s: failed to create start_pipefd.\n",
1070
/* Detach from foreground process */
1072
if (daemon_check && !aquire_flag_file()) {
1073
fprintf(stderr, "%s: program is already running.\n",
1081
close(start_pipefd[1]);
1082
res = read(start_pipefd[0], pst_stat, sizeof(*pst_stat));
1086
} else if (pid < 0) {
1087
fprintf(stderr, "%s: Could not detach process\n",
1091
close(start_pipefd[0]);
1093
if (daemon_check && !aquire_flag_file()) {
1094
fprintf(stderr, "%s: program is already running.\n",
1096
/* Return success if already running */
1098
res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
1101
close(start_pipefd[1]);
1106
* Make our own process group for "magic" reason: processes that share
1107
* our pgrp see the raw filesystem behind the magic.
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]);
1119
/* Write pid file if requested */
1121
if ((pidfp = fopen(pid_file, "wt"))) {
1122
fprintf(pidfp, "%lu\n", (unsigned long) getpid());
1125
char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
1126
logerr("failed to write pid file %s: %s",
1133
static unsigned long getnumopt(char *str, char option)
1138
val = strtoul(str, &end, 0);
1139
if (!*str || *end) {
1141
"%s: option -%c requires a numeric argument, got %s\n",
1142
program, option, str);
1148
static void do_master_cleanup_unlock(void *arg)
1152
status = pthread_mutex_unlock(&mrc.mutex);
1159
static void *do_notify_state(void *arg)
1161
struct master *master;
1167
status = pthread_mutex_lock(&mrc.mutex);
1171
master = mrc.master;
1173
debug(master->logopt, "signal %d", sig);
1176
status = pthread_cond_signal(&mrc.cond);
1178
error(master->logopt,
1179
"failed to signal state notify condition");
1180
status = pthread_mutex_unlock(&mrc.mutex);
1186
status = pthread_mutex_unlock(&mrc.mutex);
1190
master_notify_state_change(master, sig);
1195
static pthread_t do_signals(struct master *master, int sig)
1201
status = pthread_mutex_lock(&mrc.mutex);
1205
status = pthread_create(&thid, &th_attr_detached, do_notify_state, &r_sig);
1207
error(master->logopt,
1208
"mount state notify thread create failed");
1209
status = pthread_mutex_unlock(&mrc.mutex);
1216
mrc.master = master;
1218
pthread_cleanup_push(do_master_cleanup_unlock, NULL);
1221
while (!mrc.signaled) {
1222
status = pthread_cond_wait(&mrc.cond, &mrc.mutex);
1227
pthread_cleanup_pop(1);
1232
static void *do_read_master(void *arg)
1234
struct master *master;
1235
unsigned int logopt;
1240
status = pthread_mutex_lock(&mrc.mutex);
1244
master = mrc.master;
1246
logopt = master->logopt;
1249
status = pthread_cond_signal(&mrc.cond);
1252
"failed to signal master read map condition");
1253
master->reading = 0;
1254
status = pthread_mutex_unlock(&mrc.mutex);
1260
status = pthread_mutex_unlock(&mrc.mutex);
1264
defaults_read_config(1);
1266
info(logopt, "re-reading master map %s", master->name);
1268
status = master_read_master(master, age, readall);
1270
master->reading = 0;
1275
static int do_hup_signal(struct master *master, time_t age)
1277
unsigned int logopt = master->logopt;
1281
status = pthread_mutex_lock(&mrc.mutex);
1285
if (master->reading) {
1286
status = pthread_mutex_unlock(&mrc.mutex);
1292
master->reading = 1;
1294
status = pthread_create(&thid, &th_attr_detached, do_read_master, NULL);
1297
"master read map thread create failed");
1298
master->reading = 0;
1299
status = pthread_mutex_unlock(&mrc.mutex);
1306
mrc.master = master;
1309
pthread_cleanup_push(do_master_cleanup_unlock, NULL);
1312
while (!mrc.signaled) {
1313
status = pthread_cond_wait(&mrc.cond, &mrc.mutex);
1318
pthread_cleanup_pop(1);
1323
/* Deal with all the signal-driven events in the state machine */
1324
static void *statemachine(void *arg)
1329
memcpy(&signalset, &block_sigs, sizeof(signalset));
1330
sigdelset(&signalset, SIGCHLD);
1331
sigdelset(&signalset, SIGCONT);
1334
sigwait(&signalset, &sig);
1340
master_mutex_lock();
1341
if (list_empty(&master_list->completed)) {
1342
if (list_empty(&master_list->mounts)) {
1343
master_mutex_unlock();
1347
if (master_done(master_list)) {
1348
master_mutex_unlock();
1351
master_mutex_unlock();
1354
master_mutex_unlock();
1357
do_signals(master_list, sig);
1361
do_hup_signal(master_list, time(NULL));
1365
logerr("got unexpected signal %d!", sig);
1371
static void return_start_status(void *arg)
1373
struct startup_cond *sc;
1376
sc = (struct startup_cond *) arg;
1381
* Startup condition mutex must be locked during
1382
* the startup process.
1384
status = pthread_cond_signal(&sc->cond);
1388
status = pthread_mutex_unlock(&sc->mutex);
1393
int handle_mounts_startup_cond_init(struct startup_cond *suc)
1397
status = pthread_mutex_init(&suc->mutex, NULL);
1401
status = pthread_cond_init(&suc->cond, NULL);
1403
status = pthread_mutex_destroy(&suc->mutex);
1409
status = pthread_mutex_lock(&suc->mutex);
1411
status = pthread_mutex_destroy(&suc->mutex);
1414
status = pthread_cond_destroy(&suc->cond);
1422
void handle_mounts_startup_cond_destroy(void *arg)
1424
struct startup_cond *suc = (struct startup_cond *) arg;
1427
status = pthread_mutex_unlock(&suc->mutex);
1431
status = pthread_mutex_destroy(&suc->mutex);
1435
status = pthread_cond_destroy(&suc->cond);
1442
static void handle_mounts_cleanup(void *arg)
1444
struct autofs_point *ap;
1445
char path[PATH_MAX + 1];
1446
char buf[MAX_ERR_BUF];
1447
unsigned int clean = 0, submount, logopt;
1449
ap = (struct autofs_point *) arg;
1451
logopt = ap->logopt;
1452
submount = ap->submount;
1454
strcpy(path, ap->path);
1455
if (!submount && strcmp(ap->path, "/-") &&
1456
ap->flags & MOUNT_FLAG_DIR_CREATED)
1460
/* We are finishing up */
1461
ap->parent->submnt_count--;
1462
list_del_init(&ap->mounts);
1465
master_remove_mapent(ap->entry);
1466
master_source_unlock(ap->entry);
1468
destroy_logpri_fifo(ap);
1471
* Submounts are detached threads and don't belong to the
1472
* master map entry list so we need to free their resources
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);
1481
master_mutex_unlock();
1484
if (rmdir(path) == -1) {
1485
char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
1486
warn(logopt, "failed to remove dir %s: %s",
1491
info(logopt, "shut down path %s", path);
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.
1499
pthread_kill(state_mach_thid, SIGTERM);
1504
void *handle_mounts(void *arg)
1506
struct startup_cond *suc;
1507
struct autofs_point *ap;
1508
int cancel_state, status = 0;
1511
suc = (struct startup_cond *) arg;
1514
root = strdup(suc->root);
1516
pthread_cleanup_push(return_start_status, suc);
1517
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel_state);
1519
status = pthread_mutex_lock(&suc->mutex);
1521
logerr("failed to lock startup condition mutex!");
1526
crit(ap->logopt, "failed to alloc string root");
1528
pthread_setcancelstate(cancel_state, NULL);
1532
if (mount_autofs(ap, root) < 0) {
1533
crit(ap->logopt, "mount of %s failed!", ap->path);
1535
umount_autofs(ap, root, 1);
1537
pthread_setcancelstate(cancel_state, NULL);
1543
if (ap->flags & MOUNT_FLAG_GHOST && ap->type != LKP_DIRECT)
1544
info(ap->logopt, "ghosting enabled");
1547
pthread_cleanup_pop(1);
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);
1554
pthread_setcancelstate(cancel_state, NULL);
1556
while (ap->state != ST_SHUTDOWN) {
1557
if (handle_packet(ap)) {
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.
1566
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cur_state);
1568
master_mutex_lock();
1571
master_source_writelock(ap->parent->entry);
1572
mounts_mutex_lock(ap->parent);
1575
master_source_writelock(ap->entry);
1577
if (ap->state != ST_SHUTDOWN) {
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);
1584
mounts_mutex_unlock(ap->parent);
1585
master_source_unlock(ap->parent->entry);
1588
master_mutex_unlock();
1590
pthread_setcancelstate(cur_state, NULL);
1595
st_remove_tasks(ap);
1596
st_wait_task(ap, ST_ANY, 0);
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.
1604
if (ap->type == LKP_DIRECT) {
1605
umount_autofs(ap, NULL, 1);
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.
1615
ret = umount_autofs(ap, NULL, 1);
1619
/* Failed shutdown returns to ready */
1621
"can't shutdown: filesystem %s still busy",
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);
1629
mounts_mutex_unlock(ap->parent);
1630
master_source_unlock(ap->parent->entry);
1633
master_mutex_unlock();
1635
pthread_setcancelstate(cur_state, NULL);
1640
handle_mounts_cleanup(ap);
1645
static void key_thread_stdenv_vars_destroy(void *arg)
1647
struct thread_stdenv_vars *tsv;
1649
tsv = (struct thread_stdenv_vars *) arg;
1660
static void usage(void)
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"
1685
static void show_build_info(void)
1690
printf("\nLinux automount version %s\n", version);
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);
1697
printf("\nCompile options:\n ");
1699
memset(buf, 0, 2048);
1701
#ifndef ENABLE_MOUNT_LOCKING
1702
printf("DISABLE_MOUNT_LOCKING ");
1706
#ifdef ENABLE_FORCED_SHUTDOWN
1707
printf("ENABLE_FORCED_SHUTDOWN ");
1711
#ifdef ENABLE_IGNORE_BUSY_MOUNTS
1712
printf("ENABLE_IGNORE_BUSY_MOUNTS ");
1723
printf("WITH_HESIOD ");
1733
printf("WITH_LDAP ");
1743
printf("WITH_SASL ");
1753
printf("WITH_DMALLOC ");
1762
#ifdef LIBXML2_WORKAROUND
1763
printf("LIBXML2_WORKAROUND ");
1772
#ifdef WITH_LIBTIRPC
1773
printf("WITH_LIBTIRPC ");
1782
typedef struct _code {
1787
CODE prioritynames[] = {
1788
{ "alert", LOG_ALERT },
1789
{ "crit", LOG_CRIT },
1790
{ "debug", LOG_DEBUG },
1791
{ "emerg", LOG_EMERG },
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 },
1802
static int convert_log_priority(char *priority_name)
1804
CODE *priority_mapping;
1806
for (priority_mapping = prioritynames;
1807
priority_mapping->c_name != NULL;
1808
priority_mapping++) {
1810
if (!strcasecmp(priority_name, priority_mapping->c_name))
1811
return priority_mapping->c_val;
1817
int main(int argc, char *argv[])
1819
int res, opt, status;
1821
unsigned ghost, logging, daemon_check;
1822
unsigned foreground, have_global_options;
1824
time_t age = time(NULL);
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'},
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);
1856
defaults_read_config(0);
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;
1869
while ((opt = getopt_long(argc, argv, "+hp:t:vdD:fVrO:l:n:CF", long_options, NULL)) != EOF) {
1880
timeout = getnumopt(optarg, opt);
1884
logging |= LOGOPT_VERBOSE;
1888
logging |= LOGOPT_DEBUG;
1892
macro_parse_globalvar(optarg);
1904
global_random_selection = 1;
1908
global_negative_timeout = getnumopt(optarg, opt);
1912
if (!have_global_options) {
1913
global_options = strdup(optarg);
1914
have_global_options = 1;
1917
printf("%s: global options already specified.\n",
1922
if (isalpha(*optarg)) {
1923
logpri = convert_log_priority(optarg);
1925
fprintf(stderr, "Invalid log priority:"
1929
} else if (isdigit(*optarg)) {
1930
logpri = getnumopt(optarg, opt);
1932
fprintf(stderr, "non-alphanumeric character "
1933
"found in log priority. Aborting.\n");
1943
do_force_unlink = 1;
1948
printf("%s: Ambiguous or unknown options\n", program);
1953
if (logging & LOGOPT_VERBOSE)
1956
if (logging & LOGOPT_DEBUG)
1959
if (geteuid() != 0) {
1960
fprintf(stderr, "%s: this program must be run by root.\n",
1965
/* Remove the options */
1974
* The remaining argv elements are the paths for which
1975
* log priorities must be changed.
1977
for (i = 0; i < argc; i++) {
1978
if (set_log_priority(argv[i], logpri) < 0)
1983
"--set-log-priority requires a path.\n");
1990
if (!load_autofs4_module()) {
1991
fprintf(stderr, "%s: can't load %s filesystem module.\n",
1992
program, FS_MODULE_NAME);
1997
if (!query_kproto_ver() || get_kver_major() < 5) {
1999
"%s: test mount forbidden or "
2000
"incorrect kernel protocol version, "
2001
"kernel protocol version 5.00 or above required.\n",
2006
rlim.rlim_cur = MAX_OPEN_FILES;
2007
rlim.rlim_max = MAX_OPEN_FILES;
2008
res = setrlimit(RLIMIT_NOFILE, &rlim);
2011
"can't increase open file limit - continuing");
2014
rlim.rlim_cur = RLIM_INFINITY;
2015
rlim.rlim_max = RLIM_INFINITY;
2016
res = setrlimit(RLIMIT_CORE, &rlim);
2019
"can't increase core file limit - continuing");
2022
become_daemon(foreground, daemon_check);
2025
master_list = master_new(NULL, timeout, ghost);
2027
master_list = master_new(argv[0], timeout, ghost);
2030
logerr("%s: can't create master map %s",
2032
res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
2033
close(start_pipefd[1]);
2034
release_flag_file();
2038
if (pthread_attr_init(&th_attr)) {
2039
logerr("%s: failed to init thread attribute struct!",
2041
res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
2042
close(start_pipefd[1]);
2043
release_flag_file();
2047
if (pthread_attr_init(&th_attr_detached)) {
2048
logerr("%s: failed to init thread attribute struct!",
2050
res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
2051
close(start_pipefd[1]);
2052
release_flag_file();
2056
if (pthread_attr_setdetachstate(
2057
&th_attr_detached, PTHREAD_CREATE_DETACHED)) {
2058
logerr("%s: failed to set detached thread attribute!",
2060
res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
2061
close(start_pipefd[1]);
2062
release_flag_file();
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!",
2071
res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
2072
close(start_pipefd[1]);
2073
release_flag_file();
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());
2083
status = pthread_key_create(&key_thread_stdenv_vars,
2084
key_thread_stdenv_vars_destroy);
2086
logerr("%s: failed to create thread data key for std env vars!",
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();
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();
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();
2115
#if defined(WITH_LDAP) && defined(LIBXML2_WORKAROUND)
2116
void *dh_xml2 = dlopen("libxml2.so", RTLD_NOW);
2118
dh_xml2 = dlopen("libxml2.so.2", RTLD_NOW);
2122
#ifdef TIRPC_WORKAROUND
2123
void *dh_tirpc = dlopen("libitirpc.so", RTLD_NOW);
2125
dh_tirpc = dlopen("libitirpc.so.1", RTLD_NOW);
2128
if (!master_read_master(master_list, age, 0)) {
2129
master_kill(master_list);
2131
res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
2132
close(start_pipefd[1]);
2133
release_flag_file();
2138
* Mmm ... reset force unlink umount so we don't also do this
2139
* in future when we receive a HUP signal.
2141
do_force_unlink = 0;
2144
res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
2145
close(start_pipefd[1]);
2147
state_mach_thid = pthread_self();
2150
master_kill(master_list);
2157
release_flag_file();
2159
#ifdef TIRPC_WORKAROUND
2163
#if defined(WITH_LDAP) && defined( LIBXML2_WORKAROUND)
2171
info(logging, "autofs stopped");