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)
532
debug(ap->logopt, "path %s incl %d", path, incl);
535
if (master_find_submount(ap, path))
541
* If we are a submount we need to umount any offsets our
542
* parent may have mounted over top of us.
545
left += umount_subtree_mounts(ap->parent, path, is_autofs_fs);*/
547
left += umount_subtree_mounts(ap, path, is_autofs_fs);
549
/* Delete detritus like unwanted mountpoints and symlinks */
550
if (left == 0 && ap->state != ST_READMAP) {
551
update_map_cache(ap, path);
552
check_rm_dirs(ap, path, incl);
558
static int umount_all(struct autofs_point *ap, int force)
562
left = umount_multi(ap, ap->path, 0);
564
warn(ap->logopt, "could not unmount %d dirs under %s",
570
int umount_autofs(struct autofs_point *ap, const char *root, int force)
574
if (ap->state == ST_INIT)
578
* Since lookup.c is lazy about closing lookup modules
579
* to prevent unneeded opens, we need to clean them up
582
lookup_close_lookup(ap);
584
if (ap->type == LKP_INDIRECT) {
585
if (umount_all(ap, force) && !force)
587
ret = umount_autofs_indirect(ap, root);
589
ret = umount_autofs_direct(ap);
594
static size_t get_kpkt_len(void)
596
size_t pkt_len = sizeof(struct autofs_v5_packet);
602
if (strcmp(un.machine, "alpha") == 0 ||
603
strcmp(un.machine, "ia64") == 0 ||
604
strcmp(un.machine, "x86_64") == 0 ||
605
strcmp(un.machine, "ppc64") == 0)
613
static int fullread(int fd, void *ptr, size_t len)
615
char *buf = (char *) ptr;
618
ssize_t r = read(fd, buf, len);
633
static char *automount_path_to_fifo(unsigned logopt, const char *path)
636
int name_len = strlen(path) + strlen(fifodir) + 1;
639
fifo_name = malloc(name_len);
642
ret = snprintf(fifo_name, name_len, "%s%s", fifodir, path);
643
if (ret >= name_len) {
645
"fifo path for \"%s\" truncated to \"%s\". This may "
646
"lead to --set-log-priority commands being sent to the "
647
"wrong automount daemon.", path, fifo_name);
651
* An automount path can be made up of subdirectories. So, to
652
* create the fifo name, we will just replace instances of '/' with
655
p = fifo_name + strlen(fifodir);
662
debug(logopt, "fifo name %s",fifo_name);
667
static int create_logpri_fifo(struct autofs_point *ap)
672
char buf[MAX_ERR_BUF];
674
fifo_name = automount_path_to_fifo(ap->logopt, ap->path);
676
crit(ap->logopt, "Failed to allocate memory!");
677
goto out_free; /* free(NULL) is okay */
680
ret = unlink(fifo_name);
681
if (ret != 0 && errno != ENOENT) {
683
"Failed to unlink FIFO. Is the automount daemon "
688
ret = mkfifo(fifo_name, S_IRUSR|S_IWUSR);
690
char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
692
"mkfifo for %s failed: %s", fifo_name, estr);
696
fd = open_fd(fifo_name, O_RDWR|O_NONBLOCK);
698
char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
700
"Failed to open %s: %s", fifo_name, estr);
706
ap->logpri_fifo = fd;
713
int destroy_logpri_fifo(struct autofs_point *ap)
716
int fd = ap->logpri_fifo;
718
char buf[MAX_ERR_BUF];
723
fifo_name = automount_path_to_fifo(ap->logopt, ap->path);
725
crit(ap->logopt, "Failed to allocate memory!");
726
goto out_free; /* free(NULL) is okay */
729
ap->logpri_fifo = -1;
733
char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
735
"close for fifo %s: %s", fifo_name, estr);
738
ret = unlink(fifo_name);
741
"Failed to unlink FIFO. Was the fifo created OK?");
749
static void handle_fifo_message(struct autofs_point *ap, int fd)
752
char buffer[PIPE_BUF];
755
char buf[MAX_ERR_BUF];
757
memset(buffer, 0, sizeof(buffer));
758
ret = read(fd, &buffer, sizeof(buffer));
760
char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
761
warn(ap->logopt, "read on fifo returned error: %s", estr);
766
debug(ap->logopt, "expected 2 bytes, received %d.", ret);
771
pri = strtol(buffer, &end, 10);
772
if ((pri == LONG_MIN || pri == LONG_MAX) && errno == ERANGE) {
773
debug(ap->logopt, "strtol reported an %s. Failed to set "
774
"log priority.", pri == LONG_MIN ? "underflow" : "overflow");
777
if ((pri == 0 && errno == EINVAL) || end == buffer) {
778
debug(ap->logopt, "priority is expected to be an integer "
779
"in the range 0-7 inclusive.");
783
if (pri > LOG_DEBUG || pri < LOG_EMERG) {
784
debug(ap->logopt, "invalid log priority (%ld) received "
790
* OK, the message passed all of the sanity checks. The
791
* automounter actually only supports three log priorities.
792
* Everything is logged at log level debug, deamon messages
793
* and everything except debug messages are logged with the
794
* verbose setting and only error and critical messages are
795
* logged when debugging isn't enabled.
797
if (pri >= LOG_WARNING) {
798
if (pri == LOG_DEBUG) {
799
set_log_debug_ap(ap);
800
info(ap->logopt, "Debug logging set for %s", ap->path);
802
set_log_verbose_ap(ap);
803
info(ap->logopt, "Verbose logging set for %s", ap->path);
806
if (ap->logopt & LOGOPT_ANY)
807
info(ap->logopt, "Basic logging set for %s", ap->path);
812
static int set_log_priority(const char *path, int priority)
818
if (priority > LOG_DEBUG || priority < LOG_EMERG) {
819
fprintf(stderr, "Log priority %d is invalid.\n", priority);
820
fprintf(stderr, "Please spcify a number in the range 0-7.\n");
825
* This is an ascii based protocol, so we want the string
826
* representation of the integer log priority.
828
snprintf(buf, sizeof(buf), "%d", priority);
830
fifo_name = automount_path_to_fifo(LOGOPT_NONE, path);
832
fprintf(stderr, "%s: Failed to allocate memory!\n",
838
* Specify O_NONBLOCK so that the open will fail if there is no
839
* daemon reading from the other side of the FIFO.
841
fd = open(fifo_name, O_WRONLY|O_NONBLOCK);
843
fprintf(stderr, "%s: open of %s failed with %s\n",
844
__FUNCTION__, fifo_name, strerror(errno));
845
fprintf(stderr, "%s: perhaps the fifo wasn't setup,"
846
" please check your log for more information\n", __FUNCTION__);
851
if (write(fd, buf, sizeof(buf)) != sizeof(buf)) {
852
fprintf(stderr, "Failed to change logging priority. ");
853
fprintf(stderr, "write to fifo failed: %s.\n",
861
fprintf(stdout, "Successfully set log priority for %s.\n", path);
866
static int get_pkt(struct autofs_point *ap, union autofs_v5_packet_union *pkt)
868
struct pollfd fds[3];
870
char buf[MAX_ERR_BUF];
872
fds[0].fd = ap->pipefd;
873
fds[0].events = POLLIN;
874
fds[1].fd = ap->state_pipe[0];
875
fds[1].events = POLLIN;
876
fds[2].fd = ap->logpri_fifo;
877
fds[2].events = POLLIN;
882
if (poll(fds, pollfds, -1) == -1) {
886
estr = strerror_r(errno, buf, MAX_ERR_BUF);
887
logerr("poll failed: %s", estr);
891
if (fds[1].revents & POLLIN) {
892
enum states next_state;
893
size_t read_size = sizeof(next_state);
896
next_state = ST_INVAL;
900
state_pipe = ap->state_pipe[0];
902
if (fullread(state_pipe, &next_state, read_size)) {
909
if (next_state == ST_SHUTDOWN)
913
if (fds[0].revents & POLLIN)
914
return fullread(ap->pipefd, pkt, kpkt_len);
916
if (fds[2].fd != -1 && fds[2].revents & POLLIN) {
917
debug(ap->logopt, "message pending on control fifo.");
918
handle_fifo_message(ap, fds[2].fd);
923
int do_expire(struct autofs_point *ap, const char *name, int namelen)
929
len = ncat_path(buf, sizeof(buf), ap->path, name, namelen);
931
len = snprintf(buf, PATH_MAX, "%s", name);
937
crit(ap->logopt, "path to long for buffer");
941
info(ap->logopt, "expiring path %s", buf);
943
pthread_cleanup_push(master_source_lock_cleanup, ap->entry);
944
master_source_readlock(ap->entry);
945
ret = umount_multi(ap, buf, 1);
947
info(ap->logopt, "expired %s", buf);
949
warn(ap->logopt, "couldn't complete expire of %s", buf);
950
pthread_cleanup_pop(1);
955
static int autofs_init_ap(struct autofs_point *ap)
959
if ((ap->state != ST_INIT)) {
960
/* This can happen if an autofs process is already running*/
961
error(ap->logopt, "bad state %d", ap->state);
965
ap->pipefd = ap->kpipefd = ap->ioctlfd = -1;
967
/* Pipe for kernel communications */
968
if (open_pipe(pipefd) < 0) {
970
"failed to create commumication pipe for autofs path %s",
975
ap->pipefd = pipefd[0];
976
ap->kpipefd = pipefd[1];
978
/* Pipe state changes from signal handler to main loop */
979
if (open_pipe(ap->state_pipe) < 0) {
981
"failed create state pipe for autofs path %s", ap->path);
983
close(ap->kpipefd); /* Close kernel pipe end */
987
if (create_logpri_fifo(ap) < 0) {
988
logmsg("could not create FIFO for path %s\n", ap->path);
989
logmsg("dynamic log level changes not available for %s", ap->path);
995
static int mount_autofs(struct autofs_point *ap, const char *root)
999
if (autofs_init_ap(ap) != 0)
1002
if (ap->type == LKP_DIRECT)
1003
status = mount_autofs_direct(ap);
1005
status = mount_autofs_indirect(ap, root);
1010
st_add_task(ap, ST_READY);
1015
static int handle_packet(struct autofs_point *ap)
1017
union autofs_v5_packet_union pkt;
1019
if (get_pkt(ap, &pkt))
1022
debug(ap->logopt, "type = %d", pkt.hdr.type);
1024
switch (pkt.hdr.type) {
1025
case autofs_ptype_missing_indirect:
1026
return handle_packet_missing_indirect(ap, &pkt.v5_packet);
1028
case autofs_ptype_missing_direct:
1029
return handle_packet_missing_direct(ap, &pkt.v5_packet);
1031
case autofs_ptype_expire_indirect:
1032
return handle_packet_expire_indirect(ap, &pkt.v5_packet);
1034
case autofs_ptype_expire_direct:
1035
return handle_packet_expire_direct(ap, &pkt.v5_packet);
1037
error(ap->logopt, "unknown packet type %d", pkt.hdr.type);
1041
static void become_daemon(unsigned foreground, unsigned daemon_check)
1044
char buf[MAX_ERR_BUF];
1048
/* Don't BUSY any directories unnecessarily */
1050
fprintf(stderr, "%s: failed change working directory.\n",
1055
if (open_pipe(start_pipefd) < 0) {
1056
fprintf(stderr, "%s: failed to create start_pipefd.\n",
1061
/* Detach from foreground process */
1063
if (daemon_check && !aquire_flag_file()) {
1064
fprintf(stderr, "%s: program is already running.\n",
1072
close(start_pipefd[1]);
1073
res = read(start_pipefd[0], pst_stat, sizeof(*pst_stat));
1077
} else if (pid < 0) {
1078
fprintf(stderr, "%s: Could not detach process\n",
1082
close(start_pipefd[0]);
1084
if (daemon_check && !aquire_flag_file()) {
1085
fprintf(stderr, "%s: program is already running.\n",
1087
/* Return success if already running */
1089
res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
1092
close(start_pipefd[1]);
1097
* Make our own process group for "magic" reason: processes that share
1098
* our pgrp see the raw filesystem behind the magic.
1100
if (setsid() == -1) {
1101
char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
1102
fprintf(stderr, "setsid: %s", estr);
1103
res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
1104
close(start_pipefd[1]);
1110
/* Write pid file if requested */
1112
if ((pidfp = fopen(pid_file, "wt"))) {
1113
fprintf(pidfp, "%lu\n", (unsigned long) getpid());
1116
char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
1117
logerr("failed to write pid file %s: %s",
1124
static unsigned long getnumopt(char *str, char option)
1129
val = strtoul(str, &end, 0);
1130
if (!*str || *end) {
1132
"%s: option -%c requires a numeric argument, got %s\n",
1133
program, option, str);
1139
static void do_master_cleanup_unlock(void *arg)
1143
status = pthread_mutex_unlock(&mrc.mutex);
1150
static void *do_notify_state(void *arg)
1152
struct master *master;
1158
status = pthread_mutex_lock(&mrc.mutex);
1162
master = mrc.master;
1164
debug(master->logopt, "signal %d", sig);
1167
status = pthread_cond_signal(&mrc.cond);
1169
error(master->logopt,
1170
"failed to signal state notify condition");
1171
status = pthread_mutex_unlock(&mrc.mutex);
1177
status = pthread_mutex_unlock(&mrc.mutex);
1181
master_notify_state_change(master, sig);
1186
static pthread_t do_signals(struct master *master, int sig)
1192
status = pthread_mutex_lock(&mrc.mutex);
1196
status = pthread_create(&thid, &th_attr_detached, do_notify_state, &r_sig);
1198
error(master->logopt,
1199
"mount state notify thread create failed");
1200
status = pthread_mutex_unlock(&mrc.mutex);
1207
mrc.master = master;
1209
pthread_cleanup_push(do_master_cleanup_unlock, NULL);
1212
while (!mrc.signaled) {
1213
status = pthread_cond_wait(&mrc.cond, &mrc.mutex);
1218
pthread_cleanup_pop(1);
1223
static void *do_read_master(void *arg)
1225
struct master *master;
1226
unsigned int logopt;
1231
status = pthread_mutex_lock(&mrc.mutex);
1235
master = mrc.master;
1237
logopt = master->logopt;
1240
status = pthread_cond_signal(&mrc.cond);
1243
"failed to signal master read map condition");
1244
master->reading = 0;
1245
status = pthread_mutex_unlock(&mrc.mutex);
1251
status = pthread_mutex_unlock(&mrc.mutex);
1255
defaults_read_config(1);
1257
info(logopt, "re-reading master map %s", master->name);
1259
status = master_read_master(master, age, readall);
1261
master->reading = 0;
1266
static int do_hup_signal(struct master *master, time_t age)
1268
unsigned int logopt = master->logopt;
1272
status = pthread_mutex_lock(&mrc.mutex);
1276
if (master->reading) {
1277
status = pthread_mutex_unlock(&mrc.mutex);
1283
master->reading = 1;
1285
status = pthread_create(&thid, &th_attr_detached, do_read_master, NULL);
1288
"master read map thread create failed");
1289
master->reading = 0;
1290
status = pthread_mutex_unlock(&mrc.mutex);
1297
mrc.master = master;
1300
pthread_cleanup_push(do_master_cleanup_unlock, NULL);
1303
while (!mrc.signaled) {
1304
status = pthread_cond_wait(&mrc.cond, &mrc.mutex);
1309
pthread_cleanup_pop(1);
1314
/* Deal with all the signal-driven events in the state machine */
1315
static void *statemachine(void *arg)
1320
memcpy(&signalset, &block_sigs, sizeof(signalset));
1321
sigdelset(&signalset, SIGCHLD);
1322
sigdelset(&signalset, SIGCONT);
1325
sigwait(&signalset, &sig);
1331
master_mutex_lock();
1332
if (list_empty(&master_list->completed)) {
1333
if (list_empty(&master_list->mounts)) {
1334
master_mutex_unlock();
1338
if (master_done(master_list)) {
1339
master_mutex_unlock();
1342
master_mutex_unlock();
1345
master_mutex_unlock();
1348
do_signals(master_list, sig);
1352
do_hup_signal(master_list, time(NULL));
1356
logerr("got unexpected signal %d!", sig);
1362
static void return_start_status(void *arg)
1364
struct startup_cond *sc;
1367
sc = (struct startup_cond *) arg;
1372
* Startup condition mutex must be locked during
1373
* the startup process.
1375
status = pthread_cond_signal(&sc->cond);
1379
status = pthread_mutex_unlock(&sc->mutex);
1384
int handle_mounts_startup_cond_init(struct startup_cond *suc)
1388
status = pthread_mutex_init(&suc->mutex, NULL);
1392
status = pthread_cond_init(&suc->cond, NULL);
1394
status = pthread_mutex_destroy(&suc->mutex);
1400
status = pthread_mutex_lock(&suc->mutex);
1402
status = pthread_mutex_destroy(&suc->mutex);
1405
status = pthread_cond_destroy(&suc->cond);
1413
void handle_mounts_startup_cond_destroy(void *arg)
1415
struct startup_cond *suc = (struct startup_cond *) arg;
1418
status = pthread_mutex_unlock(&suc->mutex);
1422
status = pthread_mutex_destroy(&suc->mutex);
1426
status = pthread_cond_destroy(&suc->cond);
1433
static void handle_mounts_cleanup(void *arg)
1435
struct autofs_point *ap;
1436
char path[PATH_MAX + 1];
1437
char buf[MAX_ERR_BUF];
1438
unsigned int clean = 0, submount, logopt;
1440
ap = (struct autofs_point *) arg;
1442
logopt = ap->logopt;
1443
submount = ap->submount;
1445
strcpy(path, ap->path);
1446
if (!submount && strcmp(ap->path, "/-") &&
1447
ap->flags & MOUNT_FLAG_DIR_CREATED)
1451
/* We are finishing up */
1452
ap->parent->submnt_count--;
1453
list_del_init(&ap->mounts);
1456
master_remove_mapent(ap->entry);
1457
master_source_unlock(ap->entry);
1459
destroy_logpri_fifo(ap);
1462
* Submounts are detached threads and don't belong to the
1463
* master map entry list so we need to free their resources
1467
mounts_mutex_unlock(ap->parent);
1468
master_source_unlock(ap->parent->entry);
1469
master_free_mapent_sources(ap->entry, 1);
1470
master_free_mapent(ap->entry);
1474
if (rmdir(path) == -1) {
1475
char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
1476
warn(logopt, "failed to remove dir %s: %s",
1481
info(logopt, "shut down path %s", path);
1484
* If we are not a submount send a signal to the signal handler
1485
* so it can join with any completed handle_mounts() threads and
1486
* perform final cleanup.
1489
pthread_kill(state_mach_thid, SIGTERM);
1491
master_mutex_unlock();
1496
void *handle_mounts(void *arg)
1498
struct startup_cond *suc;
1499
struct autofs_point *ap;
1500
int cancel_state, status = 0;
1503
suc = (struct startup_cond *) arg;
1506
root = strdup(suc->root);
1508
pthread_cleanup_push(return_start_status, suc);
1509
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel_state);
1511
status = pthread_mutex_lock(&suc->mutex);
1513
logerr("failed to lock startup condition mutex!");
1518
crit(ap->logopt, "failed to alloc string root");
1520
pthread_setcancelstate(cancel_state, NULL);
1524
if (mount_autofs(ap, root) < 0) {
1525
crit(ap->logopt, "mount of %s failed!", ap->path);
1527
umount_autofs(ap, root, 1);
1529
pthread_setcancelstate(cancel_state, NULL);
1535
if (ap->flags & MOUNT_FLAG_GHOST && ap->type != LKP_DIRECT)
1536
info(ap->logopt, "ghosting enabled");
1539
pthread_cleanup_pop(1);
1541
/* We often start several automounters at the same time. Add some
1542
randomness so we don't all expire at the same time. */
1543
if (!ap->submount && ap->exp_timeout)
1544
alarm_add(ap, ap->exp_runfreq + rand() % ap->exp_runfreq);
1546
pthread_setcancelstate(cancel_state, NULL);
1548
while (ap->state != ST_SHUTDOWN) {
1549
if (handle_packet(ap)) {
1553
* If we're a submount we need to ensure our parent
1554
* doesn't try to mount us again until our shutdown
1555
* is complete and that any outstanding mounts are
1556
* completed before we try to shutdown.
1558
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cur_state);
1560
master_mutex_lock();
1563
master_source_writelock(ap->parent->entry);
1564
mounts_mutex_lock(ap->parent);
1567
master_source_writelock(ap->entry);
1569
if (ap->state != ST_SHUTDOWN) {
1571
alarm_add(ap, ap->exp_runfreq);
1572
/* Return to ST_READY is done immediately */
1573
st_add_task(ap, ST_READY);
1574
master_source_unlock(ap->entry);
1576
mounts_mutex_unlock(ap->parent);
1577
master_source_unlock(ap->parent->entry);
1580
master_mutex_unlock();
1582
pthread_setcancelstate(cur_state, NULL);
1587
st_remove_tasks(ap);
1588
st_wait_task(ap, ST_ANY, 0);
1591
* For a direct mount map all mounts have already gone
1592
* by the time we get here and since we only ever
1593
* umount direct mounts at shutdown there is no need
1594
* to check for possible recovery.
1596
if (ap->type == LKP_DIRECT) {
1597
umount_autofs(ap, NULL, 1);
1602
* If umount_autofs returns non-zero it wasn't able
1603
* to complete the umount and has left the mount intact
1604
* so we can continue. This can happen if a lookup
1605
* occurs while we're trying to umount.
1607
ret = umount_autofs(ap, NULL, 1);
1611
/* Failed shutdown returns to ready */
1613
"can't shutdown: filesystem %s still busy",
1616
alarm_add(ap, ap->exp_runfreq);
1617
/* Return to ST_READY is done immediately */
1618
st_add_task(ap, ST_READY);
1619
master_source_unlock(ap->entry);
1621
mounts_mutex_unlock(ap->parent);
1622
master_source_unlock(ap->parent->entry);
1625
master_mutex_unlock();
1627
pthread_setcancelstate(cur_state, NULL);
1632
handle_mounts_cleanup(ap);
1637
static void key_thread_stdenv_vars_destroy(void *arg)
1639
struct thread_stdenv_vars *tsv;
1641
tsv = (struct thread_stdenv_vars *) arg;
1652
static void usage(void)
1655
"Usage: %s [options] [master_map_name]\n"
1656
" -h --help this text\n"
1657
" -p --pid-file f write process id to file f\n"
1658
" -t --timeout n auto-unmount in n seconds (0-disable)\n"
1659
" -v --verbose be verbose\n"
1660
" -d --debug log debuging info\n"
1661
" -D --define define global macro variable\n"
1662
" -f --foreground do not fork into background\n"
1663
" -r --random-multimount-selection\n"
1664
" use ramdom replicated server selection\n"
1665
" -n --negative-timeout n\n"
1666
" set the timeout for failed key lookups.\n"
1667
" -O --global-options\n"
1668
" specify global mount options\n"
1669
" -l --set-log-priority priority path [path,...]\n"
1670
" set daemon log verbosity\n"
1671
" -C --dont-check-daemon\n"
1672
" don't check if daemon is already running\n"
1673
" -V --version print version, build config and exit\n"
1677
static void show_build_info(void)
1682
printf("\nLinux automount version %s\n", version);
1684
printf("\nDirectories:\n");
1685
printf("\tconfig dir:\t%s\n", confdir);
1686
printf("\tmaps dir:\t%s\n", mapdir);
1687
printf("\tmodules dir:\t%s\n", libdir);
1689
printf("\nCompile options:\n ");
1691
memset(buf, 0, 2048);
1693
#ifndef ENABLE_MOUNT_LOCKING
1694
printf("DISABLE_MOUNT_LOCKING ");
1698
#ifdef ENABLE_FORCED_SHUTDOWN
1699
printf("ENABLE_FORCED_SHUTDOWN ");
1703
#ifdef ENABLE_IGNORE_BUSY_MOUNTS
1704
printf("ENABLE_IGNORE_BUSY_MOUNTS ");
1715
printf("WITH_HESIOD ");
1725
printf("WITH_LDAP ");
1735
printf("WITH_SASL ");
1745
printf("WITH_DMALLOC ");
1754
#ifdef LIBXML2_WORKAROUND
1755
printf("LIBXML2_WORKAROUND ");
1764
#ifdef WITH_LIBTIRPC
1765
printf("WITH_LIBTIRPC ");
1774
typedef struct _code {
1779
CODE prioritynames[] = {
1780
{ "alert", LOG_ALERT },
1781
{ "crit", LOG_CRIT },
1782
{ "debug", LOG_DEBUG },
1783
{ "emerg", LOG_EMERG },
1785
{ "error", LOG_ERR }, /* DEPRECATED */
1786
{ "info", LOG_INFO },
1787
{ "notice", LOG_NOTICE },
1788
{ "panic", LOG_EMERG }, /* DEPRECATED */
1789
{ "warn", LOG_WARNING }, /* DEPRECATED */
1790
{ "warning", LOG_WARNING },
1794
static int convert_log_priority(char *priority_name)
1796
CODE *priority_mapping;
1798
for (priority_mapping = prioritynames;
1799
priority_mapping->c_name != NULL;
1800
priority_mapping++) {
1802
if (!strcasecmp(priority_name, priority_mapping->c_name))
1803
return priority_mapping->c_val;
1809
int main(int argc, char *argv[])
1811
int res, opt, status;
1813
unsigned ghost, logging, daemon_check;
1814
unsigned foreground, have_global_options;
1816
time_t age = time(NULL);
1818
static const struct option long_options[] = {
1819
{"help", 0, 0, 'h'},
1820
{"pid-file", 1, 0, 'p'},
1821
{"timeout", 1, 0, 't'},
1822
{"verbose", 0, 0, 'v'},
1823
{"debug", 0, 0, 'd'},
1824
{"define", 1, 0, 'D'},
1825
{"foreground", 0, 0, 'f'},
1826
{"random-multimount-selection", 0, 0, 'r'},
1827
{"negative-timeout", 1, 0, 'n'},
1828
{"global-options", 1, 0, 'O'},
1829
{"version", 0, 0, 'V'},
1830
{"set-log-priority", 1, 0, 'l'},
1831
{"dont-check-daemon", 0, 0, 'C'},
1832
{"force", 0, 0, 'F'},
1836
sigfillset(&block_sigs);
1837
/* allow for the dropping of core files */
1838
sigdelset(&block_sigs, SIGABRT);
1839
sigdelset(&block_sigs, SIGBUS);
1840
sigdelset(&block_sigs, SIGSEGV);
1841
sigdelset(&block_sigs, SIGILL);
1842
sigdelset(&block_sigs, SIGFPE);
1843
sigdelset(&block_sigs, SIGTRAP);
1844
sigprocmask(SIG_BLOCK, &block_sigs, NULL);
1848
defaults_read_config(0);
1850
kpkt_len = get_kpkt_len();
1851
timeout = defaults_get_timeout();
1852
ghost = defaults_get_browse_mode();
1853
logging = defaults_get_logging();
1854
global_random_selection = 0;
1855
global_options = NULL;
1856
have_global_options = 0;
1861
while ((opt = getopt_long(argc, argv, "+hp:t:vdD:fVrO:l:n:CF", long_options, NULL)) != EOF) {
1872
timeout = getnumopt(optarg, opt);
1876
logging |= LOGOPT_VERBOSE;
1880
logging |= LOGOPT_DEBUG;
1884
macro_parse_globalvar(optarg);
1896
global_random_selection = 1;
1900
global_negative_timeout = getnumopt(optarg, opt);
1904
if (!have_global_options) {
1905
global_options = strdup(optarg);
1906
have_global_options = 1;
1909
printf("%s: global options already specified.\n",
1914
if (isalpha(*optarg)) {
1915
logpri = convert_log_priority(optarg);
1917
fprintf(stderr, "Invalid log priority:"
1921
} else if (isdigit(*optarg)) {
1922
logpri = getnumopt(optarg, opt);
1924
fprintf(stderr, "non-alphanumeric character "
1925
"found in log priority. Aborting.\n");
1935
do_force_unlink = 1;
1940
printf("%s: Ambiguous or unknown options\n", program);
1945
if (logging & LOGOPT_VERBOSE)
1948
if (logging & LOGOPT_DEBUG)
1951
if (geteuid() != 0) {
1952
fprintf(stderr, "%s: this program must be run by root.\n",
1957
/* Remove the options */
1966
* The remaining argv elements are the paths for which
1967
* log priorities must be changed.
1969
for (i = 0; i < argc; i++) {
1970
if (set_log_priority(argv[i], logpri) < 0)
1975
"--set-log-priority requires a path.\n");
1982
if (!load_autofs4_module()) {
1983
fprintf(stderr, "%s: can't load %s filesystem module.\n",
1984
program, FS_MODULE_NAME);
1989
if (!query_kproto_ver() || get_kver_major() < 5) {
1991
"%s: test mount forbidden or "
1992
"incorrect kernel protocol version, "
1993
"kernel protocol version 5.00 or above required.\n",
1998
rlim.rlim_cur = MAX_OPEN_FILES;
1999
rlim.rlim_max = MAX_OPEN_FILES;
2000
res = setrlimit(RLIMIT_NOFILE, &rlim);
2003
"can't increase open file limit - continuing");
2006
rlim.rlim_cur = RLIM_INFINITY;
2007
rlim.rlim_max = RLIM_INFINITY;
2008
res = setrlimit(RLIMIT_CORE, &rlim);
2011
"can't increase core file limit - continuing");
2014
become_daemon(foreground, daemon_check);
2017
master_list = master_new(NULL, timeout, ghost);
2019
master_list = master_new(argv[0], timeout, ghost);
2022
logerr("%s: can't create master map %s",
2024
res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
2025
close(start_pipefd[1]);
2026
release_flag_file();
2030
if (pthread_attr_init(&th_attr)) {
2031
logerr("%s: failed to init thread attribute struct!",
2033
res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
2034
close(start_pipefd[1]);
2035
release_flag_file();
2039
if (pthread_attr_init(&th_attr_detached)) {
2040
logerr("%s: failed to init thread attribute struct!",
2042
res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
2043
close(start_pipefd[1]);
2044
release_flag_file();
2048
if (pthread_attr_setdetachstate(
2049
&th_attr_detached, PTHREAD_CREATE_DETACHED)) {
2050
logerr("%s: failed to set detached thread attribute!",
2052
res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
2053
close(start_pipefd[1]);
2054
release_flag_file();
2058
#ifdef _POSIX_THREAD_ATTR_STACKSIZE
2059
if (pthread_attr_setstacksize(
2060
&th_attr_detached, PTHREAD_STACK_MIN*64)) {
2061
logerr("%s: failed to set stack size thread attribute!",
2063
res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
2064
close(start_pipefd[1]);
2065
release_flag_file();
2070
info(logging, "Starting automounter version %s, master map %s",
2071
version, master_list->name);
2072
info(logging, "using kernel protocol version %d.%02d",
2073
get_kver_major(), get_kver_minor());
2075
status = pthread_key_create(&key_thread_stdenv_vars,
2076
key_thread_stdenv_vars_destroy);
2078
logerr("%s: failed to create thread data key for std env vars!",
2080
master_kill(master_list);
2081
res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
2082
close(start_pipefd[1]);
2083
release_flag_file();
2089
if (!alarm_start_handler()) {
2090
logerr("%s: failed to create alarm handler thread!", program);
2091
master_kill(master_list);
2092
res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
2093
close(start_pipefd[1]);
2094
release_flag_file();
2098
if (!st_start_handler()) {
2099
logerr("%s: failed to create FSM handler thread!", program);
2100
master_kill(master_list);
2101
res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
2102
close(start_pipefd[1]);
2103
release_flag_file();
2107
#if defined(WITH_LDAP) && defined(LIBXML2_WORKAROUND)
2108
void *dh_xml2 = dlopen("libxml2.so", RTLD_NOW);
2110
dh_xml2 = dlopen("libxml2.so.2", RTLD_NOW);
2114
#ifdef TIRPC_WORKAROUND
2115
void *dh_tirpc = dlopen("libitirpc.so", RTLD_NOW);
2117
dh_tirpc = dlopen("libitirpc.so.1", RTLD_NOW);
2120
if (!master_read_master(master_list, age, 0)) {
2121
master_kill(master_list);
2123
res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
2124
close(start_pipefd[1]);
2125
release_flag_file();
2130
* Mmm ... reset force unlink umount so we don't also do this
2131
* in future when we receive a HUP signal.
2133
do_force_unlink = 0;
2136
res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
2137
close(start_pipefd[1]);
2139
state_mach_thid = pthread_self();
2142
master_kill(master_list);
2149
release_flag_file();
2151
#ifdef TIRPC_WORKAROUND
2155
#if defined(WITH_LDAP) && defined( LIBXML2_WORKAROUND)
2163
info(logging, "autofs stopped");