xref: /openbsd/usr.sbin/amd/amd/mount_fs.c (revision a6445c1d)
1 /*
2  * Copyright (c) 1990 Jan-Simon Pendry
3  * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
4  * Copyright (c) 1990, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Jan-Simon Pendry at Imperial College, London.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	from: @(#)mount_fs.c	8.1 (Berkeley) 6/6/93
35  *	$Id: mount_fs.c,v 1.14 2014/10/20 06:55:59 guenther Exp $
36  */
37 
38 #include "am.h"
39 
40 #include <unistd.h>
41 #include <sys/stat.h>
42 
43 /*
44  * Standard mount flags
45  */
46 
47 struct opt_tab mnt_flags[] = {
48 	{ "ro",		MNT_RDONLY },
49 	{ "nodev",	MNT_NODEV },
50 	{ "noexec",	MNT_NOEXEC },
51 	{ "nosuid",	MNT_NOSUID },
52 	{ "sync",	MNT_SYNCHRONOUS },
53 	{ 0, 0 }
54 };
55 
56 int
57 compute_mount_flags(struct mntent *mnt)
58 {
59 	struct opt_tab *opt;
60 	int flags;
61 	flags = 0;
62 
63 	/*
64 	 * Crack basic mount options
65 	 */
66 	for (opt = mnt_flags; opt->opt; opt++)
67 		flags |= hasmntopt(mnt, opt->opt) ? opt->flag : 0;
68 
69 	return flags;
70 }
71 
72 int
73 mount_fs(struct mntent *mnt, int flags, caddr_t mnt_data, int retry,
74     const char *type)
75 {
76 	int error = 0;
77 
78 #ifdef DEBUG
79 	dlog("%s fstype %s (%s) flags %#x (%s)",
80 		mnt->mnt_dir, type, mnt->mnt_type, flags, mnt->mnt_opts);
81 #endif /* DEBUG */
82 
83 	/*
84 	 * Fake some mount table entries for the automounter
85 	 */
86 
87 again:
88 	clock_valid = 0;
89 	error = mount(type, mnt->mnt_dir, flags, mnt_data);
90 
91 	if (error < 0)
92 		plog(XLOG_ERROR, "%s: mount: %m", mnt->mnt_dir);
93 	if (error < 0 && --retry > 0) {
94 		sleep(1);
95 		goto again;
96 	}
97 	if (error < 0) {
98 #ifdef notdef
99 		if (automount)
100 			going_down(errno);
101 #endif
102 		return errno;
103 	}
104 
105 
106 	return 0;
107 }
108 
109 /*
110  * Some systems don't provide these to the user,
111  * but amd needs them, so...
112  *
113  * From: Piete Brooks <pb@cl.cam.ac.uk>
114  */
115 
116 #include <ctype.h>
117 
118 static char *
119 nextmntopt(char **p)
120 {
121 	char *cp = *p;
122 	char *rp;
123 	/*
124 	 * Skip past white space
125 	 */
126 	while (isspace((unsigned char)*cp))
127 		cp++;
128 	/*
129 	 * Word starts here
130 	 */
131 	rp = cp;
132 	/*
133 	 * Scan to send of string or separator
134 	 */
135 	while (*cp && *cp != ',')
136 		cp++;
137 	/*
138 	 * If separator found the overwrite with nul char.
139 	 */
140 	if (*cp) {
141 		*cp = '\0';
142 		cp++;
143 	}
144 	/*
145 	 * Return value for next call
146 	 */
147 	*p = cp;
148 	return rp;
149 }
150 
151 char *
152 hasmntopt(struct mntent *mnt, char *opt)
153 {
154 	char t[MNTMAXSTR];
155 	char *f;
156 	char *o = t;
157 	int l = strlen(opt);
158 
159 	strlcpy(t, mnt->mnt_opts, sizeof(t));
160 
161 	while (*(f = nextmntopt(&o)))
162 		if (strncmp(opt, f, l) == 0)
163 			return f - t + mnt->mnt_opts;
164 
165 	return 0;
166 }
167