xref: /original-bsd/usr.sbin/amd/amd/mount_fs.c (revision c3e32dec)
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  * %sccs.include.redist.c%
11  *
12  *	@(#)mount_fs.c	8.1 (Berkeley) 06/06/93
13  *
14  * $Id: mount_fs.c,v 5.2.2.2 1992/05/31 16:35:45 jsp Exp $
15  *
16  */
17 
18 #include "am.h"
19 #ifdef NFS_3
20 typedef nfs_fh fhandle_t;
21 #endif /* NFS_3 */
22 #include <sys/mount.h>
23 
24 #include <sys/stat.h>
25 
26 /*
27  * Standard mount flags
28  */
29 #ifdef hpux
30 /*
31  * HP-UX has an annoying feature of printing
32  * error msgs on /dev/console
33  */
34 #undef M_NOSUID
35 #endif /* hpux */
36 
37 struct opt_tab mnt_flags[] = {
38 	{ "ro", M_RDONLY },
39 #ifdef M_CACHE
40 	{ "nocache", M_NOCACHE },
41 #endif /* M_CACHE */
42 #ifdef M_GRPID
43 	{ "grpid", M_GRPID },
44 #endif /* M_GRPID */
45 #ifdef M_MULTI
46 	{ "multi", M_MULTI },
47 #endif /* M_MULTI */
48 #ifdef M_NODEV
49 	{ "nodev", M_NODEV },
50 #endif /* M_NODEV */
51 #ifdef M_NOEXEC
52 	{ "noexec", M_NOEXEC },
53 #endif /* M_NOEXEC */
54 #ifdef M_NOSUB
55 	{ "nosub", M_NOSUB },
56 #endif /* M_NOSUB */
57 #ifdef M_NOSUID
58 	{ "nosuid", M_NOSUID },
59 #endif /* M_NOSUID */
60 #ifdef M_SYNC
61 	{ "sync", M_SYNC },
62 #endif /* M_SYNC */
63 	{ 0, 0 }
64 };
65 
66 int compute_mount_flags(mnt)
67 struct mntent *mnt;
68 {
69 	struct opt_tab *opt;
70 	int flags;
71 #ifdef NFS_4
72 	flags = M_NEWTYPE;
73 #else
74 	flags = 0;
75 #endif /* NFS_4 */
76 
77 	/*
78 	 * Crack basic mount options
79 	 */
80 	for (opt = mnt_flags; opt->opt; opt++)
81 		flags |= hasmntopt(mnt, opt->opt) ? opt->flag : 0;
82 
83 	return flags;
84 }
85 
86 int mount_fs P((struct mntent *mnt, int flags, caddr_t mnt_data, int retry, MTYPE_TYPE type));
87 int mount_fs(mnt, flags, mnt_data, retry, type)
88 struct mntent *mnt;
89 int flags;
90 caddr_t mnt_data;
91 int retry;
92 MTYPE_TYPE type;
93 {
94 	int error = 0;
95 #ifdef MNTINFO_DEV
96 	struct stat stb;
97 	char *xopts = 0;
98 #endif /* MNTINFO_DEV */
99 
100 #ifdef DEBUG
101 #ifdef NFS_4
102 	dlog("%s fstype %s (%s) flags %#x (%s)",
103 		mnt->mnt_dir, type, mnt->mnt_type, flags, mnt->mnt_opts);
104 #else
105 	dlog("%s fstype %d (%s) flags %#x (%s)",
106 		mnt->mnt_dir, type, mnt->mnt_type, flags, mnt->mnt_opts);
107 #endif /* NFS_4 */
108 #endif /* DEBUG */
109 
110 	/*
111 	 * Fake some mount table entries for the automounter
112 	 */
113 #ifdef FASCIST_DF_COMMAND
114 	/*
115 	 * Some systems have a df command which blows up when
116 	 * presented with an unknown mount type.
117 	 */
118 	if (STREQ(mnt->mnt_type, MNTTYPE_AUTO)) {
119 		/*
120 		 * Try it with the normal name
121 		 */
122 		mnt->mnt_type = FASCIST_DF_COMMAND;
123 	}
124 #endif /* FASCIST_DF_COMMAND */
125 
126 again:
127 	clock_valid = 0;
128 	error = MOUNT_TRAP(type, mnt, flags, mnt_data);
129 	if (error < 0)
130 		plog(XLOG_ERROR, "%s: mount: %m", mnt->mnt_dir);
131 	if (error < 0 && --retry > 0) {
132 		sleep(1);
133 		goto again;
134 	}
135 	if (error < 0) {
136 #ifdef notdef
137 		if (automount)
138 			going_down(errno);
139 #endif
140 		return errno;
141 	}
142 
143 #ifdef UPDATE_MTAB
144 #ifdef MNTINFO_DEV
145 	/*
146 	 * Add the extra dev= field to the mount table.
147 	 */
148 	if (lstat(mnt->mnt_dir, &stb) == 0) {
149 		char *zopts = (char *) xmalloc(strlen(mnt->mnt_opts) + 32);
150 		xopts = mnt->mnt_opts;
151 		if (sizeof(stb.st_dev) == 2) {
152 			/* e.g. SunOS 4.1 */
153 			sprintf(zopts, "%s,%s=%s%04lx", xopts, MNTINFO_DEV,
154 					MNTINFO_PREF, (u_long) stb.st_dev & 0xffff);
155 		} else {
156 			/* e.g. System Vr4 */
157 			sprintf(zopts, "%s,%s=%s%08lx", xopts, MNTINFO_DEV,
158 					MNTINFO_PREF, (u_long) stb.st_dev);
159 		}
160 		mnt->mnt_opts = zopts;
161 	}
162 #endif /* MNTINFO_DEV */
163 
164 #ifdef FIXUP_MNTENT
165 	/*
166 	 * Additional fields in struct mntent
167 	 * are fixed up here
168 	 */
169 	FIXUP_MNTENT(mnt);
170 #endif
171 
172 	write_mntent(mnt);
173 #ifdef MNTINFO_DEV
174 	if (xopts) {
175 		free(mnt->mnt_opts);
176 		mnt->mnt_opts = xopts;
177 	}
178 #endif /* MNTINFO_DEV */
179 #endif /* UPDATE_MTAB */
180 
181 	return 0;
182 }
183 
184 #ifdef NEED_MNTOPT_PARSER
185 /*
186  * Some systems don't provide these to the user,
187  * but amd needs them, so...
188  *
189  * From: Piete Brooks <pb@cl.cam.ac.uk>
190  */
191 
192 #include <ctype.h>
193 
194 static char *nextmntopt(p)
195 char **p;
196 {
197 	char *cp = *p;
198 	char *rp;
199 	/*
200 	 * Skip past white space
201 	 */
202 	while (*cp && isspace(*cp))
203 		cp++;
204 	/*
205 	 * Word starts here
206 	 */
207 	rp = cp;
208 	/*
209 	 * Scan to send of string or separator
210 	 */
211 	while (*cp && *cp != ',')
212 		cp++;
213 	/*
214 	 * If separator found the overwrite with nul char.
215 	 */
216 	if (*cp) {
217 		*cp = '\0';
218 		cp++;
219 	}
220 	/*
221 	 * Return value for next call
222 	 */
223 	*p = cp;
224 	return rp;
225 }
226 
227 char *hasmntopt(mnt, opt)
228 struct mntent *mnt;
229 char *opt;
230 {
231 	char t[MNTMAXSTR];
232 	char *f;
233 	char *o = t;
234 	int l = strlen(opt);
235 	strcpy(t, mnt->mnt_opts);
236 
237 	while (*(f = nextmntopt(&o)))
238 		if (strncmp(opt, f, l) == 0)
239 			return f - t + mnt->mnt_opts;
240 
241 	return 0;
242 }
243 #endif /* NEED_MNTOPT_PARSER */
244 
245 #ifdef MOUNT_HELPER_SOURCE
246 #include MOUNT_HELPER_SOURCE
247 #endif /* MOUNT_HELPER_SOURCE */
248