mount_fs.c revision 1.2
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. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *	This product includes software developed by the University of
21 *	California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 *    may be used to endorse or promote products derived from this software
24 *    without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 *	from: @(#)mount_fs.c	8.1 (Berkeley) 6/6/93
39 *	$Id: mount_fs.c,v 1.2 1996/04/03 14:13:05 dm Exp $
40 */
41
42#include "am.h"
43#ifdef NFS_3
44typedef nfs_fh fhandle_t;
45#endif /* NFS_3 */
46
47#include <sys/stat.h>
48
49/*
50 * Standard mount flags
51 */
52#ifdef hpux
53/*
54 * HP-UX has an annoying feature of printing
55 * error msgs on /dev/console
56 */
57#undef M_NOSUID
58#endif /* hpux */
59
60struct opt_tab mnt_flags[] = {
61	{ "ro", M_RDONLY },
62#ifdef M_CACHE
63	{ "nocache", M_NOCACHE },
64#endif /* M_CACHE */
65#ifdef M_GRPID
66	{ "grpid", M_GRPID },
67#endif /* M_GRPID */
68#ifdef M_MULTI
69	{ "multi", M_MULTI },
70#endif /* M_MULTI */
71#ifdef M_NODEV
72	{ "nodev", M_NODEV },
73#endif /* M_NODEV */
74#ifdef M_NOEXEC
75	{ "noexec", M_NOEXEC },
76#endif /* M_NOEXEC */
77#ifdef M_NOSUB
78	{ "nosub", M_NOSUB },
79#endif /* M_NOSUB */
80#ifdef M_NOSUID
81	{ "nosuid", M_NOSUID },
82#endif /* M_NOSUID */
83#ifdef M_SYNC
84	{ "sync", M_SYNC },
85#endif /* M_SYNC */
86	{ 0, 0 }
87};
88
89int compute_mount_flags(mnt)
90struct mntent *mnt;
91{
92	struct opt_tab *opt;
93	int flags;
94#ifdef NFS_4
95	flags = M_NEWTYPE;
96#else
97	flags = 0;
98#endif /* NFS_4 */
99
100	/*
101	 * Crack basic mount options
102	 */
103	for (opt = mnt_flags; opt->opt; opt++)
104		flags |= hasmntopt(mnt, opt->opt) ? opt->flag : 0;
105
106	return flags;
107}
108
109int mount_fs P((struct mntent *mnt, int flags, caddr_t mnt_data, int retry, MTYPE_TYPE type));
110int mount_fs(mnt, flags, mnt_data, retry, type)
111struct mntent *mnt;
112int flags;
113caddr_t mnt_data;
114int retry;
115MTYPE_TYPE type;
116{
117	int error = 0;
118#ifdef MNTINFO_DEV
119	struct stat stb;
120	char *xopts = 0;
121#endif /* MNTINFO_DEV */
122
123#ifdef DEBUG
124#ifdef NFS_4
125	dlog("%s fstype %s (%s) flags %#x (%s)",
126		mnt->mnt_dir, type, mnt->mnt_type, flags, mnt->mnt_opts);
127#else
128	dlog("%s fstype %d (%s) flags %#x (%s)",
129		mnt->mnt_dir, type, mnt->mnt_type, flags, mnt->mnt_opts);
130#endif /* NFS_4 */
131#endif /* DEBUG */
132
133	/*
134	 * Fake some mount table entries for the automounter
135	 */
136#ifdef FASCIST_DF_COMMAND
137	/*
138	 * Some systems have a df command which blows up when
139	 * presented with an unknown mount type.
140	 */
141	if (STREQ(mnt->mnt_type, MNTTYPE_AUTO)) {
142		/*
143		 * Try it with the normal name
144		 */
145		mnt->mnt_type = FASCIST_DF_COMMAND;
146	}
147#endif /* FASCIST_DF_COMMAND */
148
149again:
150	clock_valid = 0;
151	error = MOUNT_TRAP(type, mnt, flags, mnt_data);
152	if (error < 0)
153		plog(XLOG_ERROR, "%s: mount: %m", mnt->mnt_dir);
154	if (error < 0 && --retry > 0) {
155		sleep(1);
156		goto again;
157	}
158	if (error < 0) {
159#ifdef notdef
160		if (automount)
161			going_down(errno);
162#endif
163		return errno;
164	}
165
166#ifdef UPDATE_MTAB
167#ifdef MNTINFO_DEV
168	/*
169	 * Add the extra dev= field to the mount table.
170	 */
171	if (lstat(mnt->mnt_dir, &stb) == 0) {
172		char *zopts = (char *) xmalloc(strlen(mnt->mnt_opts) + 32);
173		xopts = mnt->mnt_opts;
174		if (sizeof(stb.st_dev) == 2) {
175			/* e.g. SunOS 4.1 */
176			sprintf(zopts, "%s,%s=%s%04lx", xopts, MNTINFO_DEV,
177					MNTINFO_PREF, (u_long) stb.st_dev & 0xffff);
178		} else {
179			/* e.g. System Vr4 */
180			sprintf(zopts, "%s,%s=%s%08lx", xopts, MNTINFO_DEV,
181					MNTINFO_PREF, (u_long) stb.st_dev);
182		}
183		mnt->mnt_opts = zopts;
184	}
185#endif /* MNTINFO_DEV */
186
187#ifdef FIXUP_MNTENT
188	/*
189	 * Additional fields in struct mntent
190	 * are fixed up here
191	 */
192	FIXUP_MNTENT(mnt);
193#endif
194
195	write_mntent(mnt);
196#ifdef MNTINFO_DEV
197	if (xopts) {
198		free(mnt->mnt_opts);
199		mnt->mnt_opts = xopts;
200	}
201#endif /* MNTINFO_DEV */
202#endif /* UPDATE_MTAB */
203
204	return 0;
205}
206
207#ifdef NEED_MNTOPT_PARSER
208/*
209 * Some systems don't provide these to the user,
210 * but amd needs them, so...
211 *
212 * From: Piete Brooks <pb@cl.cam.ac.uk>
213 */
214
215#include <ctype.h>
216
217static char *nextmntopt(p)
218char **p;
219{
220	char *cp = *p;
221	char *rp;
222	/*
223	 * Skip past white space
224	 */
225	while (*cp && isspace(*cp))
226		cp++;
227	/*
228	 * Word starts here
229	 */
230	rp = cp;
231	/*
232	 * Scan to send of string or separator
233	 */
234	while (*cp && *cp != ',')
235		cp++;
236	/*
237	 * If separator found the overwrite with nul char.
238	 */
239	if (*cp) {
240		*cp = '\0';
241		cp++;
242	}
243	/*
244	 * Return value for next call
245	 */
246	*p = cp;
247	return rp;
248}
249
250char *hasmntopt(mnt, opt)
251struct mntent *mnt;
252char *opt;
253{
254	char t[MNTMAXSTR];
255	char *f;
256	char *o = t;
257	int l = strlen(opt);
258	strcpy(t, mnt->mnt_opts);
259
260	while (*(f = nextmntopt(&o)))
261		if (strncmp(opt, f, l) == 0)
262			return f - t + mnt->mnt_opts;
263
264	return 0;
265}
266#endif /* NEED_MNTOPT_PARSER */
267
268#ifdef MOUNT_HELPER_SOURCE
269#include MOUNT_HELPER_SOURCE
270#endif /* MOUNT_HELPER_SOURCE */
271