2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2014 The FreeBSD Foundation
6 * This software was developed by Edward Tomasz Napierala under sponsorship
7 * from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/types.h>
34 #include <sys/ioctl.h>
35 #include <sys/param.h>
36 #include <sys/linker.h>
37 #include <sys/mount.h>
38 #include <sys/socket.h>
41 #include <sys/utsname.h>
57 #include "autofs_ioctl.h"
61 #define AUTOMOUNTD_PIDFILE "/var/run/automountd.pid"
63 static int nchildren = 0;
65 static int request_id;
68 done(int request_error, bool wildcards)
70 struct autofs_daemon_done add;
73 memset(&add, 0, sizeof(add));
74 add.add_id = request_id;
75 add.add_wildcards = wildcards;
76 add.add_error = request_error;
78 log_debugx("completing request %d with error %d",
79 request_id, request_error);
81 error = ioctl(autofs_fd, AUTOFSDONE, &add);
83 log_warn("AUTOFSDONE");
87 * Remove "fstype=whatever" from optionsp and return the "whatever" part.
90 pick_option(const char *option, char **optionsp)
92 char *tofree, *pair, *newoptions;
98 newoptions = calloc(1, strlen(*optionsp) + 1);
99 if (newoptions == NULL)
100 log_err(1, "calloc");
102 while ((pair = strsep(optionsp, ",")) != NULL) {
104 * XXX: strncasecmp(3) perhaps?
106 if (strncmp(pair, option, strlen(option)) == 0) {
107 picked = checked_strdup(pair + strlen(option));
110 strcat(newoptions, ",");
113 strcat(newoptions, pair);
118 *optionsp = newoptions;
124 create_subtree(const struct node *node, bool incomplete)
126 const struct node *child;
128 bool wildcard_found = false;
131 * Skip wildcard nodes.
133 if (strcmp(node->n_key, "*") == 0)
136 path = node_path(node);
137 log_debugx("creating subtree at %s", path);
138 create_directory(path);
141 TAILQ_FOREACH(child, &node->n_children, n_next) {
142 if (strcmp(child->n_key, "*") == 0) {
143 wildcard_found = true;
148 if (wildcard_found) {
149 log_debugx("node %s contains wildcard entry; "
150 "not creating its subdirectories due to -d flag",
159 TAILQ_FOREACH(child, &node->n_children, n_next)
160 create_subtree(child, incomplete);
171 handle_request(const struct autofs_daemon_request *adr, char *cmdline_options,
172 bool incomplete_hierarchy)
175 struct node *root, *parent, *node;
177 char *key, *options, *fstype, *nobrowse, *retrycnt, *tmp;
181 log_debugx("got request %d: from %s, path %s, prefix \"%s\", "
182 "key \"%s\", options \"%s\"", adr->adr_id, adr->adr_from,
183 adr->adr_path, adr->adr_prefix, adr->adr_key, adr->adr_options);
186 * Try to notify the kernel about any problems.
188 request_id = adr->adr_id;
189 atexit(exit_callback);
191 if (strncmp(adr->adr_from, "map ", 4) != 0) {
192 log_errx(1, "invalid mountfrom \"%s\"; failing request",
196 map = adr->adr_from + 4; /* 4 for strlen("map "); */
197 root = node_new_root();
198 if (adr->adr_prefix[0] == '\0' || strcmp(adr->adr_prefix, "/") == 0) {
200 * Direct map. autofs(4) doesn't have a way to determine
201 * correct map key, but since it's a direct map, we can just
202 * use adr_path instead.
205 key = checked_strdup(adr->adr_path);
210 parent = node_new_map(root, checked_strdup(adr->adr_prefix),
211 NULL, checked_strdup(map),
212 checked_strdup("[kernel request]"), lineno);
214 if (adr->adr_key[0] == '\0')
217 key = checked_strdup(adr->adr_key);
221 * "Wildcards" here actually means "make autofs(4) request
222 * automountd(8) action if the node being looked up does not
223 * exist, even though the parent is marked as cached". This
224 * needs to be done for maps with wildcard entries, but also
225 * for special and executable maps.
227 parse_map(parent, map, key, &wildcards);
229 wildcards = node_has_wildcards(parent);
231 log_debugx("map may contain wildcard entries");
233 log_debugx("map does not contain wildcard entries");
236 node_expand_wildcard(root, key);
238 node = node_find(root, adr->adr_path);
240 log_errx(1, "map %s does not contain key for \"%s\"; "
241 "failing mount", map, adr->adr_path);
244 options = node_options(node);
247 * Append options from auto_master.
249 options = concat(options, ',', adr->adr_options);
252 * Prepend options passed via automountd(8) command line.
254 options = concat(cmdline_options, ',', options);
256 if (node->n_location == NULL) {
257 log_debugx("found node defined at %s:%d; not a mountpoint",
258 node->n_config_file, node->n_config_line);
260 nobrowse = pick_option("nobrowse", &options);
261 if (nobrowse != NULL && key == NULL) {
262 log_debugx("skipping map %s due to \"nobrowse\" "
263 "option; exiting", map);
267 * Exit without calling exit_callback().
273 * Not a mountpoint; create directories in the autofs mount
274 * and complete the request.
276 create_subtree(node, incomplete_hierarchy);
278 if (incomplete_hierarchy && key != NULL) {
280 * We still need to create the single subdirectory
281 * user is trying to access.
283 tmp = concat(adr->adr_path, '/', key);
284 node = node_find(root, tmp);
286 create_subtree(node, false);
289 log_debugx("nothing to mount; exiting");
293 * Exit without calling exit_callback().
298 log_debugx("found node defined at %s:%d; it is a mountpoint",
299 node->n_config_file, node->n_config_line);
302 node_expand_ampersand(node, key);
303 error = node_expand_defined(node);
305 log_errx(1, "variable expansion failed for %s; "
306 "failing mount", adr->adr_path);
310 * Append "automounted".
312 options = concat(options, ',', "automounted");
315 * Remove "nobrowse", mount(8) doesn't understand it.
317 pick_option("nobrowse", &options);
322 fstype = pick_option("fstype=", &options);
323 if (fstype == NULL) {
324 log_debugx("fstype not specified in options; "
325 "defaulting to \"nfs\"");
326 fstype = checked_strdup("nfs");
329 if (strcmp(fstype, "nfs") == 0) {
331 * The mount_nfs(8) command defaults to retry undefinitely.
332 * We do not want that behaviour, because it leaves mount_nfs(8)
333 * instances and automountd(8) children hanging forever.
334 * Disable retries unless the option was passed explicitly.
336 retrycnt = pick_option("retrycnt=", &options);
337 if (retrycnt == NULL) {
338 log_debugx("retrycnt not specified in options; "
340 options = concat(options, ',', "retrycnt=1");
342 options = concat(options, ',',
343 concat("retrycnt", '=', retrycnt));
347 f = auto_popen("mount", "-t", fstype, "-o", options,
348 node->n_location, adr->adr_path, NULL);
350 error = auto_pclose(f);
352 log_errx(1, "mount failed");
354 log_debugx("mount done; exiting");
358 * Exit without calling exit_callback().
364 sigchld_handler(int dummy __unused)
368 * The only purpose of this handler is to make SIGCHLD
369 * interrupt the AUTOFSREQUEST ioctl(2), so we can call
370 * wait_for_children().
375 register_sigchld(void)
380 bzero(&sa, sizeof(sa));
381 sa.sa_handler = sigchld_handler;
382 sigfillset(&sa.sa_mask);
383 error = sigaction(SIGCHLD, &sa, NULL);
385 log_err(1, "sigaction");
391 wait_for_children(bool block)
399 * If "block" is true, wait for at least one process.
401 if (block && num == 0)
402 pid = wait4(-1, &status, 0, NULL);
404 pid = wait4(-1, &status, WNOHANG, NULL);
407 if (WIFSIGNALED(status)) {
408 log_warnx("child process %d terminated with signal %d",
409 pid, WTERMSIG(status));
410 } else if (WEXITSTATUS(status) != 0) {
411 log_debugx("child process %d terminated with exit status %d",
412 pid, WEXITSTATUS(status));
414 log_debugx("child process %d terminated gracefully", pid);
423 usage_automountd(void)
426 fprintf(stderr, "usage: automountd [-D name=value][-m maxproc]"
427 "[-o opts][-Tidv]\n");
432 main_automountd(int argc, char **argv)
436 const char *pidfile_path = AUTOMOUNTD_PIDFILE;
437 char *options = NULL;
438 struct autofs_daemon_request request;
439 int ch, debug = 0, error, maxproc = 30, retval, saved_errno;
440 bool dont_daemonize = false, incomplete_hierarchy = false;
444 while ((ch = getopt(argc, argv, "D:Tdim:o:v")) != -1) {
447 defined_parse_and_add(optarg);
451 * For compatibility with other implementations,
457 dont_daemonize = true;
461 incomplete_hierarchy = true;
464 maxproc = atoi(optarg);
467 options = concat(options, ',', optarg);
483 pidfh = pidfile_open(pidfile_path, 0600, &otherpid);
485 if (errno == EEXIST) {
486 log_errx(1, "daemon already running, pid: %jd.",
489 log_err(1, "cannot open or create pidfile \"%s\"",
493 autofs_fd = open(AUTOFS_PATH, O_RDWR | O_CLOEXEC);
494 if (autofs_fd < 0 && errno == ENOENT) {
496 retval = kldload("autofs");
498 autofs_fd = open(AUTOFS_PATH, O_RDWR | O_CLOEXEC);
503 log_err(1, "failed to open %s", AUTOFS_PATH);
505 if (dont_daemonize == false) {
506 if (daemon(0, 0) == -1) {
507 log_warn("cannot daemonize");
508 pidfile_remove(pidfh);
515 pidfile_write(pidfh);
520 log_debugx("waiting for request from the kernel");
522 memset(&request, 0, sizeof(request));
523 error = ioctl(autofs_fd, AUTOFSREQUEST, &request);
525 if (errno == EINTR) {
526 nchildren -= wait_for_children(false);
527 assert(nchildren >= 0);
531 log_err(1, "AUTOFSREQUEST");
534 if (dont_daemonize) {
535 log_debugx("not forking due to -d flag; "
536 "will exit after servicing a single request");
538 nchildren -= wait_for_children(false);
539 assert(nchildren >= 0);
541 while (maxproc > 0 && nchildren >= maxproc) {
542 log_debugx("maxproc limit of %d child processes hit; "
543 "waiting for child process to exit", maxproc);
544 nchildren -= wait_for_children(true);
545 assert(nchildren >= 0);
547 log_debugx("got request; forking child process #%d",
558 pidfile_close(pidfh);
559 handle_request(&request, options, incomplete_hierarchy);
562 pidfile_close(pidfh);