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)
73 struct autofs_daemon_done add;
76 memset(&add, 0, sizeof(add));
77 add.add_id = request_id;
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);
86 * Do this instead of log_err() to avoid calling
87 * done() again with error, from atexit handler.
89 log_warn("AUTOFSDONE");
95 * Remove "fstype=whatever" from optionsp and return the "whatever" part.
98 pick_option(const char *option, char **optionsp)
100 char *tofree, *pair, *newoptions;
106 newoptions = calloc(strlen(*optionsp) + 1, 1);
107 if (newoptions == NULL)
108 log_err(1, "calloc");
110 while ((pair = strsep(optionsp, ",")) != NULL) {
112 * XXX: strncasecmp(3) perhaps?
114 if (strncmp(pair, option, strlen(option)) == 0) {
115 picked = checked_strdup(pair + strlen(option));
118 strcat(newoptions, ",");
121 strcat(newoptions, pair);
126 *optionsp = newoptions;
132 create_subtree(const struct node *node, bool incomplete)
134 const struct node *child;
136 bool wildcard_found = false;
139 * Skip wildcard nodes.
141 if (strcmp(node->n_key, "*") == 0)
144 path = node_path(node);
145 log_debugx("creating subtree at %s", path);
146 create_directory(path);
149 TAILQ_FOREACH(child, &node->n_children, n_next) {
150 if (strcmp(child->n_key, "*") == 0) {
151 wildcard_found = true;
156 if (wildcard_found) {
157 log_debugx("node %s contains wildcard entry; "
158 "not creating its subdirectories due to -d flag",
167 TAILQ_FOREACH(child, &node->n_children, n_next)
168 create_subtree(child, incomplete);
179 handle_request(const struct autofs_daemon_request *adr, char *cmdline_options,
180 bool incomplete_hierarchy)
183 struct node *root, *parent, *node;
185 char *options, *fstype, *retrycnt, *tmp;
188 log_debugx("got request %d: from %s, path %s, prefix \"%s\", "
189 "key \"%s\", options \"%s\"", adr->adr_id, adr->adr_from,
190 adr->adr_path, adr->adr_prefix, adr->adr_key, adr->adr_options);
193 * Try to notify the kernel about any problems.
195 request_id = adr->adr_id;
196 atexit(exit_callback);
198 if (strncmp(adr->adr_from, "map ", 4) != 0) {
199 log_errx(1, "invalid mountfrom \"%s\"; failing request",
203 map = adr->adr_from + 4; /* 4 for strlen("map "); */
204 root = node_new_root();
205 if (adr->adr_prefix[0] == '\0' || strcmp(adr->adr_prefix, "/") == 0) {
208 parent = node_new_map(root, checked_strdup(adr->adr_prefix),
209 checked_strdup(adr->adr_options), checked_strdup(map),
210 checked_strdup("[kernel request]"), lineno);
212 parse_map(parent, map, adr->adr_key[0] != '\0' ? adr->adr_key : NULL);
213 if (adr->adr_key[0] != '\0')
214 node_expand_wildcard(root, adr->adr_key);
215 node = node_find(root, adr->adr_path);
217 log_errx(1, "map %s does not contain key for \"%s\"; "
218 "failing mount", map, adr->adr_path);
221 if (node->n_location == NULL) {
222 log_debugx("found node defined at %s:%d; not a mountpoint",
223 node->n_config_file, node->n_config_line);
226 * Not a mountpoint; create directories in the autofs mount
227 * and complete the request.
229 create_subtree(node, incomplete_hierarchy);
231 if (incomplete_hierarchy && adr->adr_key[0] != '\0') {
233 * We still need to create the single subdirectory
234 * user is trying to access.
236 tmp = separated_concat(adr->adr_path,
238 node = node_find(root, tmp);
240 create_subtree(node, false);
244 log_debugx("nothing to mount; exiting");
247 * Exit without calling exit_callback().
252 log_debugx("found node defined at %s:%d; it is a mountpoint",
253 node->n_config_file, node->n_config_line);
255 node_expand_ampersand(node,
256 adr->adr_key[0] != '\0' ? adr->adr_key : NULL);
257 error = node_expand_defined(node);
259 log_errx(1, "variable expansion failed for %s; "
260 "failing mount", adr->adr_path);
263 options = node_options(node);
266 * Prepend options passed via automountd(8) command line.
268 if (cmdline_options != NULL)
269 options = separated_concat(cmdline_options, options, ',');
272 * Append "automounted".
274 options = separated_concat(options, "automounted", ',');
279 fstype = pick_option("fstype=", &options);
280 if (fstype == NULL) {
281 log_debugx("fstype not specified in options; "
282 "defaulting to \"nfs\"");
283 fstype = checked_strdup("nfs");
286 if (strcmp(fstype, "nfs") == 0) {
288 * The mount_nfs(8) command defaults to retry undefinitely.
289 * We do not want that behaviour, because it leaves mount_nfs(8)
290 * instances and automountd(8) children hanging forever.
291 * Disable retries unless the option was passed explicitly.
293 retrycnt = pick_option("retrycnt=", &options);
294 if (retrycnt == NULL) {
295 log_debugx("retrycnt not specified in options; "
297 options = separated_concat(options,
298 separated_concat("retrycnt", "1", '='), ',');
300 options = separated_concat(options,
301 separated_concat("retrycnt", retrycnt, '='), ',');
305 f = auto_popen("mount", "-t", fstype, "-o", options,
306 node->n_location, adr->adr_path, NULL);
308 error = auto_pclose(f);
310 log_errx(1, "mount failed");
313 log_debugx("mount done; exiting");
316 * Exit without calling exit_callback().
322 wait_for_children(bool block)
330 * If "block" is true, wait for at least one process.
332 if (block && num == 0)
333 pid = wait4(-1, &status, 0, NULL);
335 pid = wait4(-1, &status, WNOHANG, NULL);
338 if (WIFSIGNALED(status)) {
339 log_warnx("child process %d terminated with signal %d",
340 pid, WTERMSIG(status));
341 } else if (WEXITSTATUS(status) != 0) {
342 log_warnx("child process %d terminated with exit status %d",
343 pid, WEXITSTATUS(status));
345 log_debugx("child process %d terminated gracefully", pid);
354 usage_automountd(void)
357 fprintf(stderr, "usage: automountd [-D name=value][-m maxproc]"
358 "[-o opts][-Tidv]\n");
363 main_automountd(int argc, char **argv)
367 const char *pidfile_path = AUTOMOUNTD_PIDFILE;
368 char *options = NULL;
369 struct autofs_daemon_request request;
370 int ch, debug = 0, error, maxproc = 30, retval, saved_errno;
371 bool dont_daemonize = false, incomplete_hierarchy = false;
375 while ((ch = getopt(argc, argv, "D:Tdim:o:v")) != -1) {
378 defined_parse_and_add(optarg);
382 * For compatibility with other implementations,
388 dont_daemonize = true;
392 incomplete_hierarchy = true;
395 maxproc = atoi(optarg);
398 if (options == NULL) {
399 options = checked_strdup(optarg);
402 separated_concat(options, optarg, ',');
419 pidfh = pidfile_open(pidfile_path, 0600, &otherpid);
421 if (errno == EEXIST) {
422 log_errx(1, "daemon already running, pid: %jd.",
425 log_err(1, "cannot open or create pidfile \"%s\"",
429 autofs_fd = open(AUTOFS_PATH, O_RDWR | O_CLOEXEC);
430 if (autofs_fd < 0 && errno == ENOENT) {
432 retval = kldload("autofs");
434 autofs_fd = open(AUTOFS_PATH, O_RDWR | O_CLOEXEC);
439 log_err(1, "failed to open %s", AUTOFS_PATH);
441 if (dont_daemonize == false) {
442 if (daemon(0, 0) == -1) {
443 log_warn("cannot daemonize");
444 pidfile_remove(pidfh);
451 pidfile_write(pidfh);
454 log_debugx("waiting for request from the kernel");
456 memset(&request, 0, sizeof(request));
457 error = ioctl(autofs_fd, AUTOFSREQUEST, &request);
459 if (errno == EINTR) {
460 nchildren -= wait_for_children(false);
461 assert(nchildren >= 0);
465 log_err(1, "AUTOFSREQUEST");
468 if (dont_daemonize) {
469 log_debugx("not forking due to -d flag; "
470 "will exit after servicing a single request");
472 nchildren -= wait_for_children(false);
473 assert(nchildren >= 0);
475 while (maxproc > 0 && nchildren >= maxproc) {
476 log_debugx("maxproc limit of %d child processes hit; "
477 "waiting for child process to exit", maxproc);
478 nchildren -= wait_for_children(true);
479 assert(nchildren >= 0);
481 log_debugx("got request; forking child process #%d",
492 pidfile_close(pidfh);
493 handle_request(&request, options, incomplete_hierarchy);
496 pidfile_close(pidfh);