summaryrefslogtreecommitdiff
path: root/usr/src/cmd/fs.d/dev/mount.c
blob: 5064d849f771d6f121787413f01b109936dd7714 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <sys/types.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <locale.h>
#include <sys/stat.h>
#include <sys/mount.h>
#include <sys/mntent.h>
#include <sys/fs/sdev_impl.h>


#define	READFLAG_RO	1
#define	READFLAG_RW	2


extern int	optind;
extern char	*optarg;

static char	typename[64], *myname;
static char	fstype[] = MNTTYPE_DEV;

static int	readflag;
static int	overlay;
static int	remount;

static char	*special;
static char	*mountpt;
static struct sdev_mountargs	mountargs;

static char	*myopts[] = {
#define	SUBOPT_READONLY		0
	"ro",
#define	SUBOPT_READWRITE	1
	"rw",
#define	SUBOPT_ATTRIBDIR	2
	"attrdir",
#define	SUBOPT_REMOUNT		3
	"remount",
	NULL
};


static void
usage(void)
{
	(void) fprintf(stderr, gettext(
	    "%s usage:\n%s [-F %s] [-r] [-o specific_options]"
	    " {special | mount_point}\n%s [-F %s] [-r] [-o specific_options]"
	    " special mount_point\n"), fstype, myname, fstype, myname, fstype);
	exit(1);
}


static int
do_mount(void)
{
	int	flags = MS_DATA;

	if (readflag == READFLAG_RO)
		flags |= MS_RDONLY;
	if (overlay)
		flags |= MS_OVERLAY;
	if (remount)
		flags |= MS_REMOUNT;

	if (mount(special, mountpt, flags, fstype, &mountargs,
	    sizeof (mountargs), NULL, 0)) {
		switch (errno) {
		case EPERM:
			(void) fprintf(stderr, gettext("%s: not super user\n"),
			    typename);
			break;
		case ENXIO:
			(void) fprintf(stderr, gettext("%s: %s no such "
			    "device\n"), typename, special);
			break;
		case ENOTDIR:
			(void) fprintf(stderr, gettext("%s: %s "
			    "not a directory\n"
			    "\tor a component of %s is not a directory\n"),
			    typename, mountpt, special);
			break;
		case ENOENT:
			(void) fprintf(stderr, gettext("%s: %s or %s, no such "
			    "file or directory\n"),
			    typename, special, mountpt);
			break;
		case EINVAL:
			(void) fprintf(stderr, gettext("%s: %s is not this "
			    "filesystem type.\n"), typename, special);
			break;
		case EBUSY:
			(void) fprintf(stderr, gettext("%s: %s "
			    "is already mounted, %s is busy,\n"
			    "\tor allowable number of mount points exceeded\n"),
			    typename, special, mountpt);
			break;
		case ENOTBLK:
			(void) fprintf(stderr, gettext("%s: %s not a block "
			    "device\n"), typename, special);
			break;
		case EROFS:
			(void) fprintf(stderr, gettext("%s: %s read-only "
			    "filesystem\n"), typename, special);
			break;
		case ENOSPC:
			(void) fprintf(stderr, gettext("%s: the state of %s "
			    "is not okay\n"
			    "\tand read/write mount was attempted\n"),
			    typename, special);
			break;
		default:
			(void) fprintf(stderr, gettext("%s: cannot mount %s: "
			    "%s\n"), typename, special, strerror(errno));
			break;
		}
		return (-1);
	}
	return (0);
}


/*
 * Wrapper around strdup().
 */
static char *
do_strdup(const char *s1)
{
	char	*str;

	str = strdup(s1);
	if (str == NULL) {
		(void) fprintf(stderr, gettext("%s: strdup failed: %s\n"),
		    typename, strerror(errno));
	}
	return (str);
}


/*
 * Wrapper around stat().
 */
static int
do_stat(const char *path, struct stat *buf)
{
	int	ret;

	ret = stat(path, buf);
	if (ret < 0) {
		(void) fprintf(stderr, gettext("%s: can't stat %s: %s\n"),
		    typename, path, strerror(errno));
	}
	return (ret);
}


/*
 * Wraper around realpath()
 */
static char *
do_realpath(const char *path, char *resolved_path)
{
	char	*ret;

	ret = realpath(path, resolved_path);
	if (ret == NULL) {
		(void) fprintf(stderr, gettext("%s: realpath %s failed: %s\n"),
		    typename, path, strerror(errno));
	}
	return (ret);
}


static int
parse_subopts(char *subopts)
{
	char	*value;
	char	path[PATH_MAX + 1];

	while (*subopts != '\0') {
		switch (getsubopt(&subopts, myopts, &value)) {
		case SUBOPT_READONLY:
			if (readflag == READFLAG_RW) {
				(void) fprintf(stderr, gettext("%s: both "
				    "read-only and read-write options "
				    "specified\n"), typename);
				return (-1);
			}
			readflag = READFLAG_RO;
			break;

		case SUBOPT_READWRITE:
			if (readflag == READFLAG_RO) {
				(void) fprintf(stderr, gettext("%s: both "
				    "read-only and read-write options "
				    "specified\n"), typename);
				return (-1);
			}
			readflag = READFLAG_RW;
			break;

		case SUBOPT_ATTRIBDIR:
			if (value == NULL) {
				(void) fprintf(stderr, gettext("%s: no "
				    "attribute directory\n"), typename);
				return (-1);
			} else {
				if (do_realpath(value, path) == NULL)
					return (-1);
				mountargs.sdev_attrdir =
				    (uint64_t)(uintptr_t)do_strdup(path);
				if (mountargs.sdev_attrdir == 0)
					return (-1);
			}
			break;

		case SUBOPT_REMOUNT:
			remount = 1;
			break;

		default:
			(void) fprintf(stderr, gettext("%s: illegal -o "
			    "suboption: %s\n"), typename, value);
			return (-1);
		}
	}
	return (0);
}


int
main(int argc, char **argv)
{
	struct stat	st;
	char		mntpath[PATH_MAX + 1];
	int		cc;

	(void) setlocale(LC_ALL, "");

#if !defined(TEXT_DOMAIN)
#define	TEXT_DOMAIN "SYS_TEST"
#endif
	(void) textdomain(TEXT_DOMAIN);

	if (myname = strrchr(argv[0], '/'))
		myname++;
	else
		myname = argv[0];
	(void) snprintf(typename, sizeof (typename), "%s %s", fstype, myname);
	argv[0] = typename;

	while ((cc = getopt(argc, argv, "?o:rmO")) != -1) {
		switch (cc) {
		case 'r':
			if (readflag == READFLAG_RW) {
				(void) fprintf(stderr, gettext("%s: both "
				    "read-only and read-write options "
				    "specified\n"), typename);
				return (1);
			}
			readflag = READFLAG_RO;
			break;

		case 'O':
			overlay = 1;
			break;

		case 'o':
			if (parse_subopts(optarg))
				return (1);
			break;

		default:
			usage();
			break;
		}
	}

	/*
	 * There must be at least 2 more arguments, the
	 * special file and the directory.
	 */
	if ((argc - optind) != 2)
		usage();

	special = argv[optind++];

	if (do_realpath(argv[optind++], mntpath) == NULL)
		return (1);
	mountpt = mntpath;

	if (mountpt) {
		if (do_stat(mountpt, &st) < 0)
			return (1);
		if (! S_ISDIR(st.st_mode)) {
			(void) fprintf(stderr, gettext("%s: %s is not a "
			    "directory\n"), typename, mountpt);
			return (1);
		}
	}

	if (mountargs.sdev_attrdir) {
		if (do_stat((const char *)(uintptr_t)mountargs.sdev_attrdir,
		    &st) < 0)
			return (1);
		if (! S_ISDIR(st.st_mode)) {
			(void) fprintf(stderr, gettext("%s: %s is not a "
			    "directory\n"), typename, mountargs.sdev_attrdir);
			return (1);
		}
	}

	/* Special checks if /dev is the mount point */
	/* Remount of /dev requires an attribute directory */
	if (strcmp(mountpt, "/dev") == 0 && remount &&
	    mountargs.sdev_attrdir == 0) {
		(void) fprintf(stderr, gettext("%s: missing attribute "
		    "directory\n"), typename);
		return (1);
	}

	(void) signal(SIGHUP,  SIG_IGN);
	(void) signal(SIGQUIT, SIG_IGN);
	(void) signal(SIGINT,  SIG_IGN);

	/* Perform the mount  */
	if (do_mount())
		return (1);

	return (0);
}