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