mount_tmpfs.c 8.36 KB
Newer Older
longpanda's avatar
longpanda committed
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
362
363
364
365
366
367
368
369
370
371
/*	$NetBSD: mount_tmpfs.c,v 1.24 2008/08/05 20:57:45 pooka Exp $	*/

/*
 * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
 * 2005 program.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/param.h>
#include <sys/mount.h>
#include <sys/stat.h>

#include <vfs/tmpfs/tmpfs_mount.h>

#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <grp.h>
#include <mntopts.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>
#include <inttypes.h>
#include <libutil.h>

//#include "defs.h"
#include "mount_tmpfs.h"

/* --------------------------------------------------------------------- */

#define MOPT_TMPFSOPTS	\
	{ "gid=",	0,	MNT_GID, 1},	\
	{ "uid=",	0,	MNT_UID, 1},	\
	{ "mode=",	0,	MNT_MODE, 1},	\
	{ "inodes=",	0,	MNT_INODES, 1},	\
	{ "size=",	0,	MNT_SIZE, 1},	\
	{ "maxfilesize=",	0,	MNT_MAXFSIZE, 1}


static const struct mntopt mopts[] = {
	MOPT_STDOPTS,
	MOPT_TMPFSOPTS,
	MOPT_NULL
};

static int Cflag;

/* --------------------------------------------------------------------- */

static gid_t	a_gid(char *);
static uid_t	a_uid(char *);
static mode_t	a_mask(char *);
static int64_t a_number(char *s);
static void	usage(void) __dead2;

/* --------------------------------------------------------------------- */

void
mount_tmpfs_parseargs(int argc, char *argv[],
	struct tmpfs_mount_info *args, int *mntflags,
	char *canon_dev, char *canon_dir)
{
	int gidset, modeset, uidset; /* Ought to be 'bool'. */
	int ch;
	gid_t gid;
	uid_t uid;
	mode_t mode;
	struct stat sb;
	int extend_flags = 0;
	char *ptr, *delim;

	/* Set default values for mount point arguments. */
	memset(args, 0, sizeof(*args));
	args->ta_version = TMPFS_ARGS_VERSION;
	args->ta_size_max = 0;
	args->ta_nodes_max = 0;
	args->ta_maxfsize_max = 0;
	*mntflags = 0;

	gidset = 0; gid = 0;
	uidset = 0; uid = 0;
	modeset = 0; mode = 0;

	optind = optreset = 1;
	while ((ch = getopt(argc, argv, "Cf:g:m:n:o:s:u:")) != -1 ) {
		switch (ch) {
		case 'C':
			Cflag = 1;
			break;
		case 'f':
			args->ta_maxfsize_max = a_number(optarg);
			break;

		case 'g':
			gid = a_gid(optarg);
			gidset = 1;
			break;

		case 'm':
			mode = a_mask(optarg);
			modeset = 1;
			break;

		case 'n':
			args->ta_nodes_max = a_number(optarg);
			break;

		case 'o':
			getmntopts(optarg, mopts, mntflags, &extend_flags);
			if (extend_flags & MNT_GID) {
				ptr = strstr(optarg, "gid=");
				if(ptr) {
					delim = strstr(ptr, ",");
					if (delim) {
						*delim = '\0';
						gid = a_gid(ptr + 4);
						*delim = ',';
					} else
						gid = a_gid(ptr + 4);
					gidset = 1;
				}
				extend_flags ^= MNT_GID;
			}
			if (extend_flags & MNT_UID) {
				ptr = strstr(optarg, "uid=");
				if(ptr) {
					delim = strstr(ptr, ",");
					if (delim) {
						*delim = '\0';
						uid = a_uid(ptr + 4);
						*delim = ',';
					} else
						uid = a_uid(ptr + 4);
					uidset = 1;
				}
				extend_flags ^= MNT_UID;
			}
			if (extend_flags & MNT_MODE) {
				ptr = strstr(optarg, "mode=");
				if(ptr) {
					delim = strstr(ptr, ",");
					if (delim) {
						*delim = '\0';
						mode = a_mask(ptr + 5);
						*delim = ',';
					} else
						mode = a_mask(ptr + 5);
					modeset = 1;
				}
				extend_flags ^= MNT_MODE;
			}
			if (extend_flags & MNT_INODES) {
				ptr = strstr(optarg, "inodes=");
				if(ptr) {
					delim = strstr(ptr, ",");
					if (delim) {
						*delim = '\0';
						args->ta_nodes_max = a_number(ptr + 7);
						*delim = ',';
					} else
						args->ta_nodes_max = a_number(ptr + 7);
				}
				extend_flags ^= MNT_INODES;
			}
			if (extend_flags & MNT_SIZE) {
				ptr = strstr(optarg, "size=");
				if(ptr) {
					delim = strstr(ptr, ",");
					if (delim) {
						*delim = '\0';
						args->ta_size_max = a_number(ptr + 5);
						*delim = ',';
					} else
						args->ta_size_max = a_number(ptr + 5);
				}
				extend_flags ^= MNT_SIZE;
			}
			if (extend_flags & MNT_MAXFSIZE) {
				ptr = strstr(optarg, "maxfilesize=");
				if(ptr) {
					delim = strstr(ptr, ",");
					if (delim) {
						*delim = '\0';
						args->ta_maxfsize_max = a_number(ptr + 12);
						*delim = ',';
					} else
						args->ta_maxfsize_max = a_number(ptr + 12);
				}
				extend_flags ^= MNT_MAXFSIZE;
			}
			break;

		case 's':
			args->ta_size_max = a_number(optarg);
			break;

		case 'u':
			uid = a_uid(optarg);
			uidset = 1;
			break;

		case '?':
		default:
			usage();
		}
	}
	argc -= optind;
	argv += optind;

	if (argc != 2)
		usage();

	strlcpy(canon_dev, argv[0], MAXPATHLEN);
	strlcpy(canon_dir, argv[1], MAXPATHLEN);

	if (stat(canon_dir, &sb) == -1)
		err(EXIT_FAILURE, "cannot stat `%s'", canon_dir);

	args->ta_root_uid = uidset ? uid : sb.st_uid;
	args->ta_root_gid = gidset ? gid : sb.st_gid;
	args->ta_root_mode = modeset ? mode : sb.st_mode;
}

/* --------------------------------------------------------------------- */

static gid_t
a_gid(char *s)
{
	struct group *gr;
	char *gname;
	gid_t gid;

	if ((gr = getgrnam(s)) != NULL)
		gid = gr->gr_gid;
	else {
		for (gname = s; *s && isdigit(*s); ++s);
		if (!*s)
			gid = atoi(gname);
		else
			errx(EX_NOUSER, "unknown group id: %s", gname);
	}
	return (gid);
}

static uid_t
a_uid(char *s)
{
	struct passwd *pw;
	char *uname;
	uid_t uid;

	if ((pw = getpwnam(s)) != NULL)
		uid = pw->pw_uid;
	else {
		for (uname = s; *s && isdigit(*s); ++s);
		if (!*s)
			uid = atoi(uname);
		else
			errx(EX_NOUSER, "unknown user id: %s", uname);
	}
	return (uid);
}

static mode_t
a_mask(char *s)
{
	int done, rv = 0;
	char *ep;

	done = 0;
	if (*s >= '0' && *s <= '7') {
		done = 1;
		rv = strtol(s, &ep, 8);
	}
	if (!done || rv < 0 || *ep)
		errx(EX_USAGE, "invalid file mode: %s", s);
	return (rv);
}

static int64_t
a_number(char *s)
{
	int64_t rv = 0;

	if (dehumanize_number(s, &rv) < 0 || rv < 0)
		errx(EX_USAGE, "bad number for option: %s", s);
	return (rv);
}

static void
usage(void)
{
	fprintf(stderr,
	    "Usage: %s [-C] [-g group] [-m mode] [-n nodes] [-o options] [-s size]\n"
	    "           [-u user] [-f maxfilesize] tmpfs mountpoint\n", getprogname());
	exit(1);
}

/* --------------------------------------------------------------------- */

int
mount_tmpfs(int argc, char *argv[])
{
	struct tmpfs_mount_info args;
	char canon_dev[MAXPATHLEN], canon_dir[MAXPATHLEN];
	int mntflags;
	struct vfsconf vfc;
	int error;
	//fsnode_t copyroot = NULL;
	//fsnode_t copyhlinks = NULL;

	mount_tmpfs_parseargs(argc, argv, &args, &mntflags,
	    canon_dev, canon_dir);

	error = getvfsbyname("tmpfs", &vfc);
	if (error && vfsisloadable("tmpfs")) {
		if(vfsload("tmpfs"))
			err(EX_OSERR, "vfsload(%s)", "tmpfs");
		endvfsent();
		error = getvfsbyname("tmpfs", &vfc);
	}
	if (error)
		errx(EX_OSERR, "%s filesystem not available", "tmpfs");

	//if (Cflag)
	//	copyroot = FSCopy(&copyhlinks, canon_dir);

	if (mount(vfc.vfc_name, canon_dir, mntflags, &args) == -1)
		err(EXIT_FAILURE, "tmpfs on %s", canon_dir);

	//if (Cflag)
	//	FSPaste(canon_dir, copyroot, copyhlinks);

	return EXIT_SUCCESS;
}

#ifndef MOUNT_NOMAIN
int
main(int argc, char *argv[])
{
	setprogname(argv[0]);
	return mount_tmpfs(argc, argv);
}
#endif