2 * Copyright (c) 2014 The FreeBSD Foundation
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/types.h>
36 #include <sys/ioctl.h>
37 #include <sys/param.h>
38 #include <sys/linker.h>
39 #include <sys/mount.h>
40 #include <sys/socket.h>
43 #include <sys/utsname.h>
59 #include "autofs_ioctl.h"
63 #define AUTOMOUNTD_PIDFILE "/var/run/automountd.pid"
65 static int nchildren = 0;
67 static int request_id;
70 done(int request_error, bool wildcards)
72 struct autofs_daemon_done add;
75 memset(&add, 0, sizeof(add));
76 add.add_id = request_id;
77 add.add_wildcards = wildcards;
78 add.add_error = request_error;
80 log_debugx("completing request %d with error %d",
81 request_id, request_error);
83 error = ioctl(autofs_fd, AUTOFSDONE, &add);
85 log_warn("AUTOFSDONE");
89 * Remove "fstype=whatever" from optionsp and return the "whatever" part.
92 pick_option(const char *option, char **optionsp)
94 char *tofree, *pair, *newoptions;
100 newoptions = calloc(1, strlen(*optionsp) + 1);
101 if (newoptions == NULL)
102 log_err(1, "calloc");
104 while ((pair = strsep(optionsp, ",")) != NULL) {
106 * XXX: strncasecmp(3) perhaps?
108 if (strncmp(pair, option, strlen(option)) == 0) {
109 picked = checked_strdup(pair + strlen(option));
112 strcat(newoptions, ",");
115 strcat(newoptions, pair);
120 *optionsp = newoptions;
126 create_subtree(const struct node *node, bool incomplete)
128 const struct node *child;
130 bool wildcard_found = false;
133 * Skip wildcard nodes.
135 if (strcmp(node->n_key, "*") == 0)
138 path = node_path(node);
139 log_debugx("creating subtree at %s", path);
140 create_directory(path);
143 TAILQ_FOREACH(child, &node->n_children, n_next) {
144 if (strcmp(child->n_key, "*") == 0) {
145 wildcard_found = true;
150 if (wildcard_found) {
151 log_debugx("node %s contains wildcard entry; "
152 "not creating its subdirectories due to -d flag",
161 TAILQ_FOREACH(child, &node->n_children, n_next)
162 create_subtree(child, incomplete);
173 handle_request(const struct autofs_daemon_request *adr, char *cmdline_options,
174 bool incomplete_hierarchy)
177 struct node *root, *parent, *node;
179 char *key, *options, *fstype, *nobrowse, *retrycnt, *tmp;
183 log_debugx("got request %d: from %s, path %s, prefix \"%s\", "
184 "key \"%s\", options \"%s\"", adr->adr_id, adr->adr_from,
185 adr->adr_path, adr->adr_prefix, adr->adr_key, adr->adr_options);
188 * Try to notify the kernel about any problems.
190 request_id = adr->adr_id;
191 atexit(exit_callback);
193 if (strncmp(adr->adr_from, "map ", 4) != 0) {
194 log_errx(1, "invalid mountfrom \"%s\"; failing request",
198 map = adr->adr_from + 4; /* 4 for strlen("map "); */
199 root = node_new_root();
200 if (adr->adr_prefix[0] == '\0' || strcmp(adr->adr_prefix, "/") == 0) {
202 * Direct map. autofs(4) doesn't have a way to determine
203 * correct map key, but since it's a direct map, we can just
204 * use adr_path instead.
207 key = checked_strdup(adr->adr_path);
212 parent = node_new_map(root, checked_strdup(adr->adr_prefix),
213 NULL, checked_strdup(map),
214 checked_strdup("[kernel request]"), lineno);
216 if (adr->adr_key[0] == '\0')
219 key = checked_strdup(adr->adr_key);
223 * "Wildcards" here actually means "make autofs(4) request
224 * automountd(8) action if the node being looked up does not
225 * exist, even though the parent is marked as cached". This
226 * needs to be done for maps with wildcard entries, but also
227 * for special and executable maps.
229 parse_map(parent, map, key, &wildcards);
231 wildcards = node_has_wildcards(parent);
233 log_debugx("map may contain wildcard entries");
235 log_debugx("map does not contain wildcard entries");
238 node_expand_wildcard(root, key);
240 node = node_find(root, adr->adr_path);
242 log_errx(1, "map %s does not contain key for \"%s\"; "
243 "failing mount", map, adr->adr_path);
246 options = node_options(node);
249 * Append options from auto_master.
251 options = concat(options, ',', adr->adr_options);
254 * Prepend options passed via automountd(8) command line.
256 options = concat(cmdline_options, ',', options);
258 if (node->n_location == NULL) {
259 log_debugx("found node defined at %s:%d; not a mountpoint",
260 node->n_config_file, node->n_config_line);
262 nobrowse = pick_option("nobrowse", &options);
263 if (nobrowse != NULL && key == NULL) {
264 log_debugx("skipping map %s due to \"nobrowse\" "
265 "option; exiting", map);
269 * Exit without calling exit_callback().
275 * Not a mountpoint; create directories in the autofs mount
276 * and complete the request.
278 create_subtree(node, incomplete_hierarchy);
280 if (incomplete_hierarchy && key != NULL) {
282 * We still need to create the single subdirectory
283 * user is trying to access.
285 tmp = concat(adr->adr_path, '/', key);
286 node = node_find(root, tmp);
288 create_subtree(node, false);
291 log_debugx("nothing to mount; exiting");
295 * Exit without calling exit_callback().
300 log_debugx("found node defined at %s:%d; it is a mountpoint",
301 node->n_config_file, node->n_config_line);
304 node_expand_ampersand(node, key);
305 error = node_expand_defined(node);
307 log_errx(1, "variable expansion failed for %s; "
308 "failing mount", adr->adr_path);
312 * Append "automounted".
314 options = concat(options, ',', "automounted");
317 * Remove "nobrowse", mount(8) doesn't understand it.
319 pick_option("nobrowse", &options);
324 fstype = pick_option("fstype=", &options);
325 if (fstype == NULL) {
326 log_debugx("fstype not specified in options; "
327 "defaulting to \"nfs\"");
328 fstype = checked_strdup("nfs");
331 if (strcmp(fstype, "nfs") == 0) {
333 * The mount_nfs(8) command defaults to retry undefinitely.
334 * We do not want that behaviour, because it leaves mount_nfs(8)
335 * instances and automountd(8) children hanging forever.
336 * Disable retries unless the option was passed explicitly.
338 retrycnt = pick_option("retrycnt=", &options);
339 if (retrycnt == NULL) {
340 log_debugx("retrycnt not specified in options; "
342 options = concat(options, ',', "retrycnt=1");
344 options = concat(options, ',',
345 concat("retrycnt", '=', retrycnt));
349 f = auto_popen("mount", "-t", fstype, "-o", options,
350 node->n_location, adr->adr_path, NULL);
352 error = auto_pclose(f);
354 log_errx(1, "mount failed");
356 log_debugx("mount done; exiting");
360 * Exit without calling exit_callback().
366 sigchld_handler(int dummy __unused)
370 * The only purpose of this handler is to make SIGCHLD
371 * interrupt the AUTOFSREQUEST ioctl(2), so we can call
372 * wait_for_children().
377 register_sigchld(void)
382 bzero(&sa, sizeof(sa));
383 sa.sa_handler = sigchld_handler;
384 sigfillset(&sa.sa_mask);
385 error = sigaction(SIGCHLD, &sa, NULL);
387 log_err(1, "sigaction");
393 wait_for_children(bool block)
401 * If "block" is true, wait for at least one process.
403 if (block && num == 0)
404 pid = wait4(-1, &status, 0, NULL);
406 pid = wait4(-1, &status, WNOHANG, NULL);
409 if (WIFSIGNALED(status)) {
410 log_warnx("child process %d terminated with signal %d",
411 pid, WTERMSIG(status));
412 } else if (WEXITSTATUS(status) != 0) {
413 log_debugx("child process %d terminated with exit status %d",
414 pid, WEXITSTATUS(status));
416 log_debugx("child process %d terminated gracefully", pid);
425 usage_automountd(void)
428 fprintf(stderr, "usage: automountd [-D name=value][-m maxproc]"
429 "[-o opts][-Tidv]\n");
434 main_automountd(int argc, char **argv)
438 const char *pidfile_path = AUTOMOUNTD_PIDFILE;
439 char *options = NULL;
440 struct autofs_daemon_request request;
441 int ch, debug = 0, error, maxproc = 30, retval, saved_errno;
442 bool dont_daemonize = false, incomplete_hierarchy = false;
446 while ((ch = getopt(argc, argv, "D:Tdim:o:v")) != -1) {
449 defined_parse_and_add(optarg);
453 * For compatibility with other implementations,
459 dont_daemonize = true;
463 incomplete_hierarchy = true;
466 maxproc = atoi(optarg);
469 options = concat(options, ',', optarg);
485 pidfh = pidfile_open(pidfile_path, 0600, &otherpid);
487 if (errno == EEXIST) {
488 log_errx(1, "daemon already running, pid: %jd.",
491 log_err(1, "cannot open or create pidfile \"%s\"",
495 autofs_fd = open(AUTOFS_PATH, O_RDWR | O_CLOEXEC);
496 if (autofs_fd < 0 && errno == ENOENT) {
498 retval = kldload("autofs");
500 autofs_fd = open(AUTOFS_PATH, O_RDWR | O_CLOEXEC);
505 log_err(1, "failed to open %s", AUTOFS_PATH);
507 if (dont_daemonize == false) {
508 if (daemon(0, 0) == -1) {
509 log_warn("cannot daemonize");
510 pidfile_remove(pidfh);
517 pidfile_write(pidfh);
522 log_debugx("waiting for request from the kernel");
524 memset(&request, 0, sizeof(request));
525 error = ioctl(autofs_fd, AUTOFSREQUEST, &request);
527 if (errno == EINTR) {
528 nchildren -= wait_for_children(false);
529 assert(nchildren >= 0);
533 log_err(1, "AUTOFSREQUEST");
536 if (dont_daemonize) {
537 log_debugx("not forking due to -d flag; "
538 "will exit after servicing a single request");
540 nchildren -= wait_for_children(false);
541 assert(nchildren >= 0);
543 while (maxproc > 0 && nchildren >= maxproc) {
544 log_debugx("maxproc limit of %d child processes hit; "
545 "waiting for child process to exit", maxproc);
546 nchildren -= wait_for_children(true);
547 assert(nchildren >= 0);
549 log_debugx("got request; forking child process #%d",
560 pidfile_close(pidfh);
561 handle_request(&request, options, incomplete_hierarchy);
564 pidfile_close(pidfh);