xref: /titanic_51/usr/src/cmd/fs.d/bootfs/mount.c (revision d040c7642a81e4878a822db25cce20beb93585b4)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  * Copyright (c) 2014 Joyent, Inc.  All rights reserved.
26  */
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <libintl.h>
32 #include <errno.h>
33 #include <sys/fstyp.h>
34 #include <sys/fsid.h>
35 #include <sys/mntent.h>
36 #include <sys/mnttab.h>
37 #include <sys/mount.h>
38 #include <sys/signal.h>
39 #include <sys/stat.h>
40 #include <fslib.h>
41 
42 #define	MNTTYPE_BOOTFS "bootfs"
43 
44 static char  optbuf[MAX_MNTOPT_STR] = { '\0', };
45 static int   optsize = 0;
46 
47 static void
48 usage(void)
49 {
50 	(void) fprintf(stderr,
51 	    "Usage: mount [-Ormq] [-o options] special mountpoint\n");
52 	exit(2);
53 }
54 
55 /*
56  * usage: mount [-Ormq] [-o options] special mountp
57  *
58  * This mount program is exec'ed by /usr/sbin/mount if '-F bootfs' is
59  * specified.
60  */
61 int
62 main(int argc, char *argv[])
63 {
64 	int c;
65 	char *special;		/* Entity being mounted */
66 	char *mountp;		/* Entity being mounted on */
67 	char *savedoptbuf;
68 	char *myname;
69 	char typename[64];
70 	int flags = 0;
71 	int errflag = 0;
72 	int qflg = 0;
73 
74 	myname = strrchr(argv[0], '/');
75 	myname = myname ? myname+1 : argv[0];
76 	(void) snprintf(typename, sizeof (typename), "%s %s", MNTTYPE_BOOTFS,
77 	    myname);
78 	argv[0] = typename;
79 
80 	while ((c = getopt(argc, argv, "o:rmOq")) != EOF) {
81 		switch (c) {
82 		case '?':
83 			errflag++;
84 			break;
85 
86 		case 'o':
87 			if (strlcpy(optbuf, optarg, sizeof (optbuf)) >=
88 			    sizeof (optbuf)) {
89 				(void) fprintf(stderr,
90 				    gettext("%s: Invalid argument: %s\n"),
91 				    myname, optarg);
92 				return (2);
93 			}
94 			optsize = strlen(optbuf);
95 			break;
96 		case 'O':
97 			flags |= MS_OVERLAY;
98 			break;
99 		case 'r':
100 			flags |= MS_RDONLY;
101 			break;
102 
103 		case 'm':
104 			flags |= MS_NOMNTTAB;
105 			break;
106 
107 		case 'q':
108 			qflg = 1;
109 			break;
110 
111 		default:
112 			usage();
113 		}
114 	}
115 	if ((argc - optind != 2) || errflag) {
116 		usage();
117 	}
118 	special = argv[argc - 2];
119 	mountp = argv[argc - 1];
120 
121 	if ((savedoptbuf = strdup(optbuf)) == NULL) {
122 		(void) fprintf(stderr, gettext("%s: out of memory\n"),
123 		    myname);
124 		exit(2);
125 	}
126 
127 	if (mount(special, mountp, flags | MS_OPTIONSTR, MNTTYPE_BOOTFS, NULL,
128 	    0, optbuf, MAX_MNTOPT_STR)) {
129 		(void) fprintf(stderr, "mount: ");
130 		perror(special);
131 		exit(2);
132 	}
133 	if (optsize && !qflg) {
134 		cmp_requested_to_actual_options(savedoptbuf, optbuf,
135 		    special, mountp);
136 	}
137 
138 	return (0);
139 }
140