8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / fs.d / autofs / autod_mount.c
blob31055371ef3928c7ba4606661207d16db6c27d7c
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * autod_mount.c
24 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
28 #pragma ident "%Z%%M% %I% %E% SMI"
30 #include <stdio.h>
31 #include <ctype.h>
32 #include <string.h>
33 #include <syslog.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <sys/param.h>
37 #include <errno.h>
38 #include <pwd.h>
39 #include <netinet/in.h>
40 #include <netdb.h>
41 #include <sys/tiuser.h>
42 #include <locale.h>
43 #include <stdlib.h>
44 #include <unistd.h>
45 #include <sys/mntent.h>
46 #include <sys/mnttab.h>
47 #include <sys/wait.h>
48 #include <sys/mount.h>
49 #include <sys/fs/autofs.h>
50 #include <nfs/nfs.h>
51 #include <thread.h>
52 #include <limits.h>
53 #include <assert.h>
54 #include <fcntl.h>
55 #include <strings.h>
57 #include "automount.h"
58 #include "replica.h"
60 static int unmount_mntpnt(struct mnttab *);
61 static int call_fork_exec(char *, char *, char **, int);
62 static void remove_browse_options(char *);
63 static int inherit_options(char *, char **);
65 int
66 do_mount1(
67 char *mapname,
68 char *key,
69 char *subdir,
70 char *mapopts,
71 char *path,
72 uint_t isdirect,
73 uid_t uid,
74 action_list **alpp,
75 int flags)
77 struct mapline ml;
78 struct mapent *me, *mapents = NULL;
79 char mntpnt[MAXPATHLEN];
80 char spec_mntpnt[MAXPATHLEN];
81 int err = 0;
82 char *private; /* fs specific data. eg prevhost in case of nfs */
83 int mount_ok = 0;
84 ssize_t len;
85 action_list *alp, *prev, *tmp;
86 char root[MAXPATHLEN];
87 int overlay = 1;
88 char next_subdir[MAXPATHLEN];
89 bool_t mount_access = TRUE;
90 bool_t iswildcard;
91 bool_t isrestricted = hasrestrictopt(mapopts);
92 char *stack[STACKSIZ];
93 char **stkptr = stack;
95 retry:
96 iswildcard = FALSE;
98 /* initialize the stack of open files for this thread */
99 stack_op(INIT, NULL, stack, &stkptr);
101 err = getmapent(key, mapname, &ml, stack, &stkptr, &iswildcard,
102 isrestricted);
103 if (err == 0) {
104 mapents = parse_entry(key, mapname, mapopts, &ml,
105 subdir, isdirect, mount_access);
108 if (trace) {
109 struct mapfs *mfs;
110 trace_prt(1, " do_mount1:\n");
111 for (me = mapents; me; me = me->map_next) {
112 trace_prt(1, " (%s,%s)\t%s%s%s\n",
113 me->map_fstype ? me->map_fstype : "",
114 me->map_mounter ? me->map_mounter : "",
115 path ? path : "",
116 me->map_root ? me->map_root : "",
117 me->map_mntpnt ? me->map_mntpnt : "");
118 trace_prt(0, "\t\t-%s\n",
119 me->map_mntopts ? me->map_mntopts : "");
121 for (mfs = me->map_fs; mfs; mfs = mfs->mfs_next)
122 trace_prt(0, "\t\t%s:%s\tpenalty=%d\n",
123 mfs->mfs_host ? mfs->mfs_host: "",
124 mfs->mfs_dir ? mfs->mfs_dir : "",
125 mfs->mfs_penalty);
129 *alpp = NULL;
132 * Each mapent in the list describes a mount to be done.
133 * Normally there's just a single entry, though in the
134 * case of /net mounts there may be many entries, that
135 * must be mounted as a hierarchy. For each mount the
136 * automountd must make sure the required mountpoint
137 * exists and invoke the appropriate mount command for
138 * the fstype.
140 private = "";
141 for (me = mapents; me && !err; me = me->map_next) {
142 len = snprintf(mntpnt, sizeof (mntpnt), "%s%s%s", path,
143 mapents->map_root, me->map_mntpnt);
145 if (len >= sizeof (mntpnt)) {
146 free_mapent(mapents);
147 return (ENAMETOOLONG);
150 * remove trailing /'s from mountpoint to avoid problems
151 * stating a directory with two or more trailing slashes.
152 * This will let us mount directories from machines
153 * which export with two or more slashes (apollo for instance).
155 len -= 1;
156 while (mntpnt[len] == '/')
157 mntpnt[len--] = '\0';
159 (void) strcpy(spec_mntpnt, mntpnt);
161 if (isrestricted &&
162 inherit_options(mapopts, &me->map_mntopts) != 0) {
163 syslog(LOG_ERR, "malloc of options failed");
164 free_mapent(mapents);
165 return (EAGAIN);
168 if (strcmp(me->map_fstype, MNTTYPE_NFS) == 0) {
169 remove_browse_options(me->map_mntopts);
170 if (flags == DOMOUNT_KERNEL) {
171 alp = (action_list *)malloc(
172 sizeof (action_list));
173 if (alp == NULL) {
174 syslog(LOG_ERR,
175 "malloc of alp failed");
176 continue;
178 memset(alp, 0, sizeof (action_list));
179 } else
180 alp = NULL;
181 err =
182 mount_nfs(me, spec_mntpnt, private, overlay, uid,
183 &alp);
185 * We must retry if we don't have access to the
186 * root file system and there are other
187 * following mapents. The reason we can't
188 * continue because the rest of the mapent list
189 * depends on whether mount_access is TRUE or FALSE.
191 if (err == NFSERR_ACCES && me->map_next != NULL) {
193 * don't expect mount_access to be
194 * FALSE here, but we do a check
195 * anyway.
197 if (mount_access == TRUE) {
198 mount_access = FALSE;
199 err = 0;
200 free_mapent(mapents);
201 if (alp) {
202 free(alp);
203 alp = NULL;
205 goto retry;
208 if (alp) {
209 if (*alpp == NULL)
210 *alpp = alp;
211 else {
212 for (tmp = *alpp; tmp != NULL;
213 tmp = tmp->next)
214 prev = tmp;
215 prev->next = alp;
218 mount_ok = !err;
219 } else if (strcmp(me->map_fstype, MNTTYPE_AUTOFS) == 0) {
220 if (isdirect) {
221 len = strlcpy(root, path, sizeof (root));
222 } else {
223 len = snprintf(root, sizeof (root), "%s/%s",
224 path, key);
226 if (len >= sizeof (root)) {
227 free_mapent(mapents);
228 return (ENAMETOOLONG);
231 alp = (action_list *)malloc(sizeof (action_list));
232 if (alp == NULL) {
233 syslog(LOG_ERR, "malloc of alp failed");
234 continue;
236 memset(alp, 0, sizeof (action_list));
239 * get the next subidr, but only if its a modified
240 * or faked autofs mount
242 if (me->map_modified || me->map_faked) {
243 len = snprintf(next_subdir,
244 sizeof (next_subdir), "%s%s", subdir,
245 me->map_mntpnt);
246 } else {
247 next_subdir[0] = '\0';
248 len = 0;
251 if (trace > 2)
252 trace_prt(1, " root=%s\t next_subdir=%s\n",
253 root, next_subdir);
254 if (len < sizeof (next_subdir)) {
255 err = mount_autofs(me, spec_mntpnt, alp,
256 root, next_subdir, key);
257 } else {
258 err = ENAMETOOLONG;
260 if (err == 0) {
262 * append to action list
264 mount_ok++;
265 if (*alpp == NULL)
266 *alpp = alp;
267 else {
268 for (tmp = *alpp; tmp != NULL;
269 tmp = tmp->next)
270 prev = tmp;
271 prev->next = alp;
273 } else {
274 free(alp);
275 mount_ok = 0;
277 } else if (strcmp(me->map_fstype, MNTTYPE_LOFS) == 0) {
278 remove_browse_options(me->map_mntopts);
279 err = loopbackmount(me->map_fs->mfs_dir, spec_mntpnt,
280 me->map_mntopts, overlay);
281 mount_ok = !err;
282 } else {
283 remove_browse_options(me->map_mntopts);
284 err = mount_generic(me->map_fs->mfs_dir,
285 me->map_fstype, me->map_mntopts,
286 spec_mntpnt, overlay);
287 mount_ok = !err;
290 if (mapents)
291 free_mapent(mapents);
294 * If an error occurred,
295 * the filesystem doesn't exist, or could not be
296 * mounted. Return EACCES to autofs indicating that
297 * the mountpoint can not be accessed if this is not
298 * a wildcard access. If it is a wildcard access we
299 * return ENOENT since the lookup that triggered
300 * this mount request will fail and the entry will not
301 * be available.
303 if (mount_ok) {
305 * No error occurred, return 0 to indicate success.
307 err = 0;
308 } else {
310 * The filesystem does not exist or could not be mounted.
311 * Return ENOENT if the lookup was triggered by a wildcard
312 * access. Wildcard entries only exist if they can be
313 * mounted. They can not be listed otherwise (through
314 * a readdir(2)).
315 * Return EACCES if the lookup was not triggered by a
316 * wildcard access. Map entries that are explicitly defined
317 * in maps are visible via readdir(2), therefore we return
318 * EACCES to indicate that the entry exists, but the directory
319 * can not be opened. This is the same behavior of a Unix
320 * directory that exists, but has its execute bit turned off.
321 * The directory is there, but the user does not have access
322 * to it.
324 if (iswildcard)
325 err = ENOENT;
326 else
327 err = EACCES;
329 return (err);
332 #define ARGV_MAX 16
333 #define VFS_PATH "/usr/lib/fs"
336 mount_generic(special, fstype, opts, mntpnt, overlay)
337 char *special, *fstype, *opts, *mntpnt;
338 int overlay;
340 struct mnttab m;
341 struct stat stbuf;
342 int i, res;
343 char *newargv[ARGV_MAX];
345 if (trace > 1) {
346 trace_prt(1, " mount: %s %s %s %s\n",
347 special, mntpnt, fstype, opts);
350 if (stat(mntpnt, &stbuf) < 0) {
351 syslog(LOG_ERR, "Couldn't stat %s: %m", mntpnt);
352 return (ENOENT);
355 i = 2;
357 if (overlay)
358 newargv[i++] = "-O";
361 * Use "quiet" option to suppress warnings about unsupported
362 * mount options.
364 newargv[i++] = "-q";
366 if (opts && *opts) {
367 m.mnt_mntopts = opts;
368 if (hasmntopt(&m, MNTOPT_RO) != NULL)
369 newargv[i++] = "-r";
370 newargv[i++] = "-o";
371 newargv[i++] = opts;
373 newargv[i++] = "--";
374 newargv[i++] = special;
375 newargv[i++] = mntpnt;
376 newargv[i] = NULL;
377 res = call_fork_exec(fstype, "mount", newargv, verbose);
378 if (res == 0 && trace > 1) {
379 if (stat(mntpnt, &stbuf) == 0) {
380 trace_prt(1, " mount of %s dev=%x rdev=%x OK\n",
381 mntpnt, stbuf.st_dev, stbuf.st_rdev);
382 } else {
383 trace_prt(1, " failed to stat %s\n", mntpnt);
386 return (res);
389 void
390 automountd_do_fork_exec(void *cookie, char *argp, size_t arg_size,
391 door_desc_t *dfd, uint_t n_desc)
393 int stat_loc;
394 int fd = 0;
395 struct stat stbuf;
396 int res;
397 int child_pid;
398 command_t *command;
399 char *newargv[ARGV_MAX];
400 int i;
403 command = (command_t *)argp;
404 if (sizeof (*command) != arg_size) {
405 res = EINVAL;
406 door_return((char *)&res, sizeof (res), NULL, 0);
409 switch ((child_pid = fork1())) {
410 case -1:
411 syslog(LOG_ERR, "Cannot fork: %m");
412 res = errno;
413 break;
414 case 0:
416 * Child
418 (void) setsid();
419 fd = open(command->console ? "/dev/console" : "/dev/null",
420 O_WRONLY);
421 if (fd != -1) {
422 (void) dup2(fd, 1);
423 (void) dup2(fd, 2);
424 (void) close(fd);
427 for (i = 0; *command->argv[i]; i++) {
428 newargv[i] = strdup(command->argv[i]);
429 if (newargv[i] == (char *)NULL) {
430 syslog(LOG_ERR, "failed to copy argument '%s'"
431 " of %s: %m", command->argv[i],
432 command->file);
433 _exit(errno);
436 newargv[i] = NULL;
438 (void) execv(command->file, newargv);
439 if (errno == EACCES)
440 syslog(LOG_ERR, "exec %s: %m", command->file);
442 _exit(errno);
443 default:
445 * Parent
447 (void) waitpid(child_pid, &stat_loc, WUNTRACED);
449 if (WIFEXITED(stat_loc)) {
450 if (trace > 1) {
451 trace_prt(1,
452 " fork_exec: returns exit status %d\n",
453 WEXITSTATUS(stat_loc));
456 res = WEXITSTATUS(stat_loc);
457 } else if (WIFSIGNALED(stat_loc)) {
458 if (trace > 1)
459 trace_prt(1,
460 " fork_exec: returns signal status %d\n",
461 WTERMSIG(stat_loc));
462 res = 1;
463 } else {
464 if (trace > 1)
465 trace_prt(1,
466 " fork_exec: returns unknown status\n");
467 res = 1;
471 door_return((char *)&res, sizeof (res), NULL, 0);
472 trace_prt(1, "automountd_do_fork_exec, door return failed %s, %s\n",
473 command->file, strerror(errno));
474 door_return(NULL, 0, NULL, 0);
478 do_unmount1(ur)
479 umntrequest *ur;
482 struct mnttab m;
483 int res = 0;
485 m.mnt_special = ur->mntresource;
486 m.mnt_mountp = ur->mntpnt;
487 m.mnt_fstype = ur->fstype;
488 m.mnt_mntopts = ur->mntopts;
490 * Special case for NFS mounts.
491 * Don't want to attempt unmounts from
492 * a dead server. If any member of a
493 * hierarchy belongs to a dead server
494 * give up (try later).
496 if (strcmp(ur->fstype, MNTTYPE_NFS) == 0) {
497 struct replica *list;
498 int i, n;
499 bool_t pubopt = FALSE;
500 int nfs_port;
501 int got_port;
504 * See if a port number was specified. If one was
505 * specified that is too large to fit in 16 bits, truncate
506 * the high-order bits (for historical compatibility). Use
507 * zero to indicate "no port specified".
509 got_port = nopt(&m, MNTOPT_PORT, &nfs_port);
510 if (!got_port)
511 nfs_port = 0;
512 nfs_port &= USHRT_MAX;
514 if (hasmntopt(&m, MNTOPT_PUBLIC))
515 pubopt = TRUE;
517 list = parse_replica(ur->mntresource, &n);
518 if (list == NULL) {
519 if (n >= 0)
520 syslog(LOG_ERR, "Memory allocation failed: %m");
521 res = 1;
522 goto done;
525 for (i = 0; i < n; i++) {
526 if (pingnfs(list[i].host, 1, NULL, 0, nfs_port,
527 pubopt, list[i].path, NULL) != RPC_SUCCESS) {
528 res = 1;
529 free_replica(list, n);
530 goto done;
533 free_replica(list, n);
536 res = unmount_mntpnt(&m);
538 done: return (res);
541 static int
542 unmount_mntpnt(mnt)
543 struct mnttab *mnt;
545 char *fstype = mnt->mnt_fstype;
546 char *mountp = mnt->mnt_mountp;
547 char *newargv[ARGV_MAX];
548 int res;
550 if (strcmp(fstype, MNTTYPE_NFS) == 0) {
551 res = nfsunmount(mnt);
552 } else if (strcmp(fstype, MNTTYPE_LOFS) == 0) {
553 if ((res = umount(mountp)) < 0)
554 res = errno;
555 } else {
556 newargv[2] = mountp;
557 newargv[3] = NULL;
559 res = call_fork_exec(fstype, "umount", newargv, verbose);
560 if (res == ENOENT) {
562 * filesystem specific unmount command not found
564 if ((res = umount(mountp)) < 0)
565 res = errno;
569 if (trace > 1)
570 trace_prt(1, " unmount %s %s\n",
571 mountp, res ? "failed" : "OK");
572 return (res);
576 * Remove the autofs specific options 'browse', 'nobrowse' and
577 * 'restrict' from 'opts'.
579 static void
580 remove_browse_options(char *opts)
582 char *p, *pb;
583 char buf[MAXOPTSLEN], new[MAXOPTSLEN];
584 char *placeholder;
586 new[0] = '\0';
587 (void) strcpy(buf, opts);
588 pb = buf;
589 while (p = (char *)strtok_r(pb, ",", &placeholder)) {
590 pb = NULL;
591 if (strcmp(p, MNTOPT_NOBROWSE) != 0 &&
592 strcmp(p, MNTOPT_BROWSE) != 0 &&
593 strcmp(p, MNTOPT_RESTRICT) != 0) {
594 if (new[0] != '\0')
595 (void) strcat(new, ",");
596 (void) strcat(new, p);
599 (void) strcpy(opts, new);
602 static const char *restropts[] = {
603 RESTRICTED_MNTOPTS
605 #define NROPTS (sizeof (restropts)/sizeof (restropts[0]))
607 static int
608 inherit_options(char *opts, char **mapentopts)
610 int i;
611 char *new;
612 struct mnttab mtmap;
613 struct mnttab mtopt;
615 size_t len = strlen(*mapentopts);
617 for (i = 0; i < NROPTS; i++)
618 len += strlen(restropts[i]);
620 /* "," for each new option plus the trailing NUL */
621 len += NROPTS + 1;
623 new = malloc(len);
624 if (new == 0)
625 return (-1);
627 (void) strcpy(new, *mapentopts);
629 mtmap.mnt_mntopts = *mapentopts;
630 mtopt.mnt_mntopts = opts;
632 for (i = 0; i < NROPTS; i++) {
633 if (hasmntopt(&mtopt, (char *)restropts[i]) != NULL &&
634 hasmntopt(&mtmap, (char *)restropts[i]) == NULL) {
635 if (*new != '\0')
636 (void) strcat(new, ",");
637 (void) strcat(new, restropts[i]);
640 free(*mapentopts);
641 *mapentopts = new;
642 return (0);
645 bool_t
646 hasrestrictopt(char *opts)
648 struct mnttab mt;
650 mt.mnt_mntopts = opts;
652 return (hasmntopt(&mt, MNTOPT_RESTRICT) != NULL);
655 static int
656 call_fork_exec(fstype, cmd, newargv, console)
657 char *fstype;
658 char *cmd;
659 char **newargv;
660 int console;
662 command_t command;
663 door_arg_t darg;
664 char path[MAXPATHLEN];
665 struct stat stbuf;
666 int ret;
667 int sz;
668 int status;
669 int i;
671 bzero(&command, sizeof (command));
672 /* build the full path name of the fstype dependent command */
673 (void) snprintf(path, MAXPATHLEN, "%s/%s/%s", VFS_PATH, fstype, cmd);
675 if (stat(path, &stbuf) != 0) {
676 ret = errno;
677 return (ret);
680 strlcpy(command.file, path, MAXPATHLEN);
681 strlcpy(command.argv[0], path, MAXOPTSLEN);
682 for (i = 2; newargv[i]; i++) {
683 strlcpy(command.argv[i-1], newargv[i], MAXOPTSLEN);
685 if (trace > 1) {
686 trace_prt(1, " call_fork_exec: %s ", command.file);
687 for (i = 0; *command.argv[i]; i++)
688 trace_prt(0, "%s ", command.argv[i]);
689 trace_prt(0, "\n");
692 command.console = console;
694 darg.data_ptr = (char *)&command;
695 darg.data_size = sizeof (command);
696 darg.desc_ptr = NULL;
697 darg.desc_num = 0;
698 darg.rbuf = (char *)&status;
699 darg.rsize = sizeof (status);
701 ret = door_call(did_fork_exec, &darg);
702 if (trace > 1) {
703 trace_prt(1, " call_fork_exec: door_call failed %d\n", ret);
706 return (status);