xref: /illumos-gate/usr/src/cmd/fs.d/lofs/mount/mount.c (revision 132157d7fb25c120ae1deca2a65fa7c78e8fcfd0)
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  */
26 
27 #define	LOFS
28 #define	MNTTYPE_LOFS "lofs"
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <libintl.h>
33 #include <errno.h>
34 #include <sys/fstyp.h>
35 #include <sys/fsid.h>
36 #include <sys/mntent.h>
37 #include <sys/mnttab.h>
38 #include <sys/mount.h>
39 #include <sys/signal.h>
40 #include <sys/stat.h>
41 #include <fslib.h>
42 
43 #define	RET_OK		0
44 /*
45  * /sbin/mount and the fs-local method understand this exit code to
46  * mean that all the mount failures were related to lofs mounts. Since
47  * this program only attempts to mount lofs file systems, when it fails
48  * it returns this exit status.
49  */
50 #define	RET_ERR		111
51 
52 static void usage(void);
53 
54 static char  optbuf[MAX_MNTOPT_STR] = { '\0', };
55 static int   optsize = 0;
56 
57 static char fstype[] = MNTTYPE_LOFS;
58 
59 /*
60  * usage: mount [-Ormq] [-o options] special mountp
61  *
62  * This mount program is exec'ed by /usr/sbin/mount if '-F lofs' is
63  * specified.
64  */
65 int
66 main(int argc, char *argv[])
67 {
68 	int c;
69 	char *special;		/* Entity being mounted */
70 	char *mountp;		/* Entity being mounted on */
71 	char *savedoptbuf;
72 	char *myname;
73 	char typename[64];
74 	int flags = 0;
75 	int errflag = 0;
76 	int qflg = 0;
77 
78 	myname = strrchr(argv[0], '/');
79 	myname = myname ? myname+1 : argv[0];
80 	(void) snprintf(typename, sizeof (typename), "%s %s", fstype, myname);
81 	argv[0] = typename;
82 
83 	while ((c = getopt(argc, argv, "o:rmOq")) != EOF) {
84 		switch (c) {
85 		case '?':
86 			errflag++;
87 			break;
88 
89 		case 'o':
90 			if (strlcpy(optbuf, optarg, sizeof (optbuf)) >=
91 			    sizeof (optbuf)) {
92 				(void) fprintf(stderr,
93 				    gettext("%s: Invalid argument: %s\n"),
94 				    myname, optarg);
95 				return (2);
96 			}
97 			optsize = strlen(optbuf);
98 			break;
99 		case 'O':
100 			flags |= MS_OVERLAY;
101 			break;
102 		case 'r':
103 			flags |= MS_RDONLY;
104 			break;
105 
106 		case 'm':
107 			flags |= MS_NOMNTTAB;
108 			break;
109 
110 		case 'q':
111 			qflg = 1;
112 			break;
113 
114 		default:
115 			usage();
116 		}
117 	}
118 	if ((argc - optind != 2) || errflag) {
119 		usage();
120 	}
121 	special = argv[argc - 2];
122 	mountp = argv[argc - 1];
123 
124 	if ((savedoptbuf = strdup(optbuf)) == NULL) {
125 		(void) fprintf(stderr, gettext("%s: out of memory\n"),
126 		    myname);
127 		exit(2);
128 	}
129 	if (mount(special, mountp, flags | MS_OPTIONSTR, fstype, NULL, 0,
130 	    optbuf, MAX_MNTOPT_STR)) {
131 		(void) fprintf(stderr, "mount: ");
132 		perror(special);
133 		exit(RET_ERR);
134 	}
135 	if (optsize && !qflg)
136 		cmp_requested_to_actual_options(savedoptbuf, optbuf,
137 		    special, mountp);
138 	return (0);
139 }
140 
141 void
142 usage(void)
143 {
144 	(void) fprintf(stderr,
145 	    "Usage: mount [-Ormq] [-o options] special mountpoint\n");
146 	exit(RET_ERR);
147 }
148