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>
60 #include "autofs_ioctl.h"
64 #define AUTOMOUNTD_PIDFILE "/var/run/automountd.pid"
66 static int nchildren = 0;
68 static int request_id;
71 done(int request_error, bool wildcards)
73 struct autofs_daemon_done add;
76 memset(&add, 0, sizeof(add));
77 add.add_id = request_id;
78 add.add_wildcards = wildcards;
79 add.add_error = request_error;
81 log_debugx("completing request %d with error %d",
82 request_id, request_error);
84 error = ioctl(autofs_fd, AUTOFSDONE, &add);
86 log_warn("AUTOFSDONE");
90 * Remove "fstype=whatever" from optionsp and return the "whatever" part.
93 pick_option(const char *option, char **optionsp)
95 char *tofree, *pair, *newoptions;
101 newoptions = calloc(strlen(*optionsp) + 1, 1);
102 if (newoptions == NULL)
103 log_err(1, "calloc");
105 while ((pair = strsep(optionsp, ",")) != NULL) {
107 * XXX: strncasecmp(3) perhaps?
109 if (strncmp(pair, option, strlen(option)) == 0) {
110 picked = checked_strdup(pair + strlen(option));
113 strcat(newoptions, ",");
116 strcat(newoptions, pair);
121 *optionsp = newoptions;
127 create_subtree(const struct node *node, bool incomplete)
129 const struct node *child;
131 bool wildcard_found = false;
134 * Skip wildcard nodes.
136 if (strcmp(node->n_key, "*") == 0)
139 path = node_path(node);
140 log_debugx("creating subtree at %s", path);
141 create_directory(path);
144 TAILQ_FOREACH(child, &node->n_children, n_next) {
145 if (strcmp(child->n_key, "*") == 0) {
146 wildcard_found = true;
151 if (wildcard_found) {
152 log_debugx("node %s contains wildcard entry; "
153 "not creating its subdirectories due to -d flag",
162 TAILQ_FOREACH(child, &node->n_children, n_next)
163 create_subtree(child, incomplete);
174 handle_request(const struct autofs_daemon_request *adr, char *cmdline_options,
175 bool incomplete_hierarchy)
178 struct node *root, *parent, *node;
180 char *options, *fstype, *nobrowse, *retrycnt, *tmp;
184 log_debugx("got request %d: from %s, path %s, prefix \"%s\", "
185 "key \"%s\", options \"%s\"", adr->adr_id, adr->adr_from,
186 adr->adr_path, adr->adr_prefix, adr->adr_key, adr->adr_options);
189 * Try to notify the kernel about any problems.
191 request_id = adr->adr_id;
192 atexit(exit_callback);
194 if (strncmp(adr->adr_from, "map ", 4) != 0) {
195 log_errx(1, "invalid mountfrom \"%s\"; failing request",
199 map = adr->adr_from + 4; /* 4 for strlen("map "); */
200 root = node_new_root();
201 if (adr->adr_prefix[0] == '\0' || strcmp(adr->adr_prefix, "/") == 0) {
204 parent = node_new_map(root, checked_strdup(adr->adr_prefix),
205 checked_strdup(adr->adr_options), checked_strdup(map),
206 checked_strdup("[kernel request]"), lineno);
210 * "Wildcards" here actually means "make autofs(4) request
211 * automountd(8) action if the node being looked up does not
212 * exist, even though the parent is marked as cached". This
213 * needs to be done for maps with wildcard entries, but also
214 * for special and executable maps.
216 parse_map(parent, map, adr->adr_key[0] != '\0' ? adr->adr_key : NULL,
219 wildcards = node_has_wildcards(parent);
221 log_debugx("map may contain wildcard entries");
223 log_debugx("map does not contain wildcard entries");
225 if (adr->adr_key[0] != '\0')
226 node_expand_wildcard(root, adr->adr_key);
228 node = node_find(root, adr->adr_path);
230 log_errx(1, "map %s does not contain key for \"%s\"; "
231 "failing mount", map, adr->adr_path);
234 if (node->n_location == NULL) {
235 log_debugx("found node defined at %s:%d; not a mountpoint",
236 node->n_config_file, node->n_config_line);
238 options = node_options(node);
241 * Prepend options passed via automountd(8) command line.
243 if (cmdline_options != NULL) {
245 separated_concat(cmdline_options, options, ',');
248 nobrowse = pick_option("nobrowse", &options);
249 if (nobrowse != NULL && adr->adr_key[0] == '\0') {
250 log_debugx("skipping map %s due to \"nobrowse\" "
251 "option; exiting", map);
255 * Exit without calling exit_callback().
261 * Not a mountpoint; create directories in the autofs mount
262 * and complete the request.
264 create_subtree(node, incomplete_hierarchy);
266 if (incomplete_hierarchy && adr->adr_key[0] != '\0') {
268 * We still need to create the single subdirectory
269 * user is trying to access.
271 tmp = separated_concat(adr->adr_path,
273 node = node_find(root, tmp);
275 create_subtree(node, false);
278 log_debugx("nothing to mount; exiting");
282 * Exit without calling exit_callback().
287 log_debugx("found node defined at %s:%d; it is a mountpoint",
288 node->n_config_file, node->n_config_line);
290 node_expand_ampersand(node,
291 adr->adr_key[0] != '\0' ? adr->adr_key : NULL);
292 error = node_expand_defined(node);
294 log_errx(1, "variable expansion failed for %s; "
295 "failing mount", adr->adr_path);
298 options = node_options(node);
301 * Prepend options passed via automountd(8) command line.
303 if (cmdline_options != NULL)
304 options = separated_concat(cmdline_options, options, ',');
307 * Append "automounted".
309 options = separated_concat(options, "automounted", ',');
312 * Remove "nobrowse", mount(8) doesn't understand it.
314 pick_option("nobrowse", &options);
319 fstype = pick_option("fstype=", &options);
320 if (fstype == NULL) {
321 log_debugx("fstype not specified in options; "
322 "defaulting to \"nfs\"");
323 fstype = checked_strdup("nfs");
326 if (strcmp(fstype, "nfs") == 0) {
328 * The mount_nfs(8) command defaults to retry undefinitely.
329 * We do not want that behaviour, because it leaves mount_nfs(8)
330 * instances and automountd(8) children hanging forever.
331 * Disable retries unless the option was passed explicitly.
333 retrycnt = pick_option("retrycnt=", &options);
334 if (retrycnt == NULL) {
335 log_debugx("retrycnt not specified in options; "
337 options = separated_concat(options,
338 separated_concat("retrycnt", "1", '='), ',');
340 options = separated_concat(options,
341 separated_concat("retrycnt", retrycnt, '='), ',');
345 f = auto_popen("mount", "-t", fstype, "-o", options,
346 node->n_location, adr->adr_path, NULL);
348 error = auto_pclose(f);
350 log_errx(1, "mount failed");
352 log_debugx("mount done; exiting");
356 * Exit without calling exit_callback().
362 sigchld_handler(int dummy __unused)
366 * The only purpose of this handler is to make SIGCHLD
367 * interrupt the AUTOFSREQUEST ioctl(2), so we can call
368 * wait_for_children().
373 register_sigchld(void)
378 bzero(&sa, sizeof(sa));
379 sa.sa_handler = sigchld_handler;
380 sigfillset(&sa.sa_mask);
381 error = sigaction(SIGCHLD, &sa, NULL);
383 log_err(1, "sigaction");
389 wait_for_children(bool block)
397 * If "block" is true, wait for at least one process.
399 if (block && num == 0)
400 pid = wait4(-1, &status, 0, NULL);
402 pid = wait4(-1, &status, WNOHANG, NULL);
405 if (WIFSIGNALED(status)) {
406 log_warnx("child process %d terminated with signal %d",
407 pid, WTERMSIG(status));
408 } else if (WEXITSTATUS(status) != 0) {
409 log_debugx("child process %d terminated with exit status %d",
410 pid, WEXITSTATUS(status));
412 log_debugx("child process %d terminated gracefully", pid);
421 usage_automountd(void)
424 fprintf(stderr, "usage: automountd [-D name=value][-m maxproc]"
425 "[-o opts][-Tidv]\n");
430 main_automountd(int argc, char **argv)
434 const char *pidfile_path = AUTOMOUNTD_PIDFILE;
435 char *options = NULL;
436 struct autofs_daemon_request request;
437 int ch, debug = 0, error, maxproc = 30, retval, saved_errno;
438 bool dont_daemonize = false, incomplete_hierarchy = false;
442 while ((ch = getopt(argc, argv, "D:Tdim:o:v")) != -1) {
445 defined_parse_and_add(optarg);
449 * For compatibility with other implementations,
455 dont_daemonize = true;
459 incomplete_hierarchy = true;
462 maxproc = atoi(optarg);
465 if (options == NULL) {
466 options = checked_strdup(optarg);
469 separated_concat(options, optarg, ',');
486 pidfh = pidfile_open(pidfile_path, 0600, &otherpid);
488 if (errno == EEXIST) {
489 log_errx(1, "daemon already running, pid: %jd.",
492 log_err(1, "cannot open or create pidfile \"%s\"",
496 autofs_fd = open(AUTOFS_PATH, O_RDWR | O_CLOEXEC);
497 if (autofs_fd < 0 && errno == ENOENT) {
499 retval = kldload("autofs");
501 autofs_fd = open(AUTOFS_PATH, O_RDWR | O_CLOEXEC);
506 log_err(1, "failed to open %s", AUTOFS_PATH);
508 if (dont_daemonize == false) {
509 if (daemon(0, 0) == -1) {
510 log_warn("cannot daemonize");
511 pidfile_remove(pidfh);
518 pidfile_write(pidfh);
523 log_debugx("waiting for request from the kernel");
525 memset(&request, 0, sizeof(request));
526 error = ioctl(autofs_fd, AUTOFSREQUEST, &request);
528 if (errno == EINTR) {
529 nchildren -= wait_for_children(false);
530 assert(nchildren >= 0);
534 log_err(1, "AUTOFSREQUEST");
537 if (dont_daemonize) {
538 log_debugx("not forking due to -d flag; "
539 "will exit after servicing a single request");
541 nchildren -= wait_for_children(false);
542 assert(nchildren >= 0);
544 while (maxproc > 0 && nchildren >= maxproc) {
545 log_debugx("maxproc limit of %d child processes hit; "
546 "waiting for child process to exit", maxproc);
547 nchildren -= wait_for_children(true);
548 assert(nchildren >= 0);
550 log_debugx("got request; forking child process #%d",
561 pidfile_close(pidfh);
562 handle_request(&request, options, incomplete_hierarchy);
565 pidfile_close(pidfh);