xref: /openbsd-src/usr.bin/rdistd/filesys-os.c (revision 7b7f0d4d29b5b38a0036f77d71ada2936e03c955)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 static char RCSid[] =
36 "$Id: filesys-os.c,v 1.2 1996/06/13 22:23:06 deraadt Exp $";
37 
38 static char sccsid[] = "@(#)filesys-os.c";
39 
40 static char copyright[] =
41 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
42  All rights reserved.\n";
43 #endif /* not lint */
44 
45 /*
46  * OS specific file system routines
47  */
48 
49 #include "defs.h"
50 #include "filesys.h"
51 
52 #if 	FSI_TYPE == FSI_GETFSSTAT
53 static struct statfs   *mnt = NULL;
54 #if	FSTYPENAME
55 #define f_type_eq(a, b) (! strcmp (((struct statfs *) (a))->f_fstypename, (b)))
56 #else	/* !FSTYPENAME */
57 #define	f_type_eq(a, b) (((struct statfs *) a)->f_type == (b))
58 #endif /* !FSTYPENAME */
59 #endif	/* FSI_GETFSSTAT */
60 
61 #if	FSI_TYPE == FSI_MNTCTL
62 static struct vmount   *mnt = NULL;
63 #endif	/* FSI_MNTCTL */
64 
65 #if	(FSI_TYPE == FSI_MNTCTL) || (FSI_TYPE == FSI_GETFSSTAT)
66 static char 	       *mntbuf = NULL;
67 static int 		entries_left;
68 #endif	/* FSI_MNTCTL || FSI_GETFSSTAT */
69 
70 #if	FSI_TYPE == FSI_MNTCTL
71 /*
72  * AIX version of setmountent()
73  */
74 FILE *setmountent(file, mode)
75 	/*ARGSUSED*/
76 	char *file;
77 	char *mode;
78 {
79 	u_int size;
80 
81 	if (mntbuf)
82 		(void) free(mntbuf);
83 
84 	mntctl(MCTL_QUERY, sizeof(size), &size);
85 	mntbuf = (char *) xmalloc(size);
86 
87 	entries_left = mntctl(MCTL_QUERY, size, mntbuf);
88 	if (!entries_left)
89 		return((FILE *)NULL);
90 
91 	mnt = (struct vmount *)mntbuf;
92 	return((FILE *) 1);
93 }
94 #endif	/* FSI_MNTCTL */
95 
96 #if	FSI_TYPE == FSI_GETFSSTAT
97 /*
98  * getfsstat() version of get mount info routines.
99  */
100 FILE *setmountent(file, mode)
101 	/*ARGSUSED*/
102 	char *file;
103 	char *mode;
104 {
105 	int size;
106 
107 	if (mntbuf)
108 		(void) free(mntbuf);
109 
110 	size = getfsstat((struct statfs *) NULL, 0, MNT_WAIT);
111 	if (size == -1)
112 		return ((FILE *)NULL);
113 	size *= sizeof(struct statfs);
114 	mntbuf = (char *) xmalloc(size);
115 
116 	entries_left = getfsstat((struct statfs *)mntbuf, size, MNT_WAIT);
117 	if (entries_left == -1)
118 		return((FILE *) NULL);
119 
120 	mnt = (struct statfs *) mntbuf;
121 
122 	return((FILE *) 1);
123 }
124 #endif	/* FSI_GETFSSTAT */
125 
126 #if	FSI_TYPE == FSI_MNTCTL
127 /*
128  * AIX version of getmountent()
129  */
130 /*
131  * Iterate over mount entries
132  */
133 mntent_t *getmountent(fptr)
134 	/*ARGSUSED*/
135 	FILE *fptr;
136 {
137 	static mntent_t mntstruct;
138 
139 	if (!entries_left)
140 		return((mntent_t*)0);
141 
142 	bzero((char *) &mntstruct, sizeof(mntstruct));
143 
144 	if (mnt->vmt_flags & MNT_READONLY)
145 		mntstruct.me_flags |= MEFLAG_READONLY;
146 
147 	mntstruct.me_path = vmt2dataptr(mnt, VMT_STUB);
148 	switch ((struct vmount*)mnt->vmt_gfstype) {
149 	      case MNT_NFS:
150 		mntstruct.me_type = METYPE_NFS;
151 		break;
152 	      default:
153 		mntstruct.me_type = METYPE_OTHER;
154 		break;
155 	}
156 
157 	mnt = (struct vmount*)((mnt->vmt_length)+(char *)mnt);
158 	entries_left--;
159 
160 	return(&mntstruct);
161 }
162 #endif	/* FSI_MNTCTL */
163 
164 #if	FSI_TYPE == FSI_GETFSSTAT
165 /*
166  * getfsstat() version of getmountent()
167  */
168 mntent_t *getmountent(fptr)
169 	/*ARGSUSED*/
170 	FILE *fptr;
171 {
172 	static mntent_t mntstruct;
173 	static char remote_dev[MAXHOSTNAMELEN+MAXPATHLEN+1];
174 
175 	if (!entries_left)
176 		return((mntent_t*)0);
177 
178 	bzero((char *) &mntstruct, sizeof(mntstruct));
179 
180 #if	defined(MNT_RDONLY)
181 	if (mnt->f_flags & MNT_RDONLY)
182 		mntstruct.me_flags |= MEFLAG_READONLY;
183 #endif
184 #if	defined(M_RDONLY)
185 	if (mnt->f_flags & M_RDONLY)
186 		mntstruct.me_flags |= MEFLAG_READONLY;
187 #endif
188 	if (f_type_eq (mnt, MOUNT_NFS)) {
189 		(void) sprintf(remote_dev, "%s", mnt->f_mntfromname);
190 		mntstruct.me_path = remote_dev;
191 		mntstruct.me_type = METYPE_NFS;
192 	}
193 	else {
194 		mntstruct.me_path = mnt->f_mntonname;
195 		mntstruct.me_type = METYPE_OTHER;
196 	}
197 
198 	mnt++;
199 	entries_left--;
200 
201 	return(&mntstruct);
202 }
203 #endif
204 
205 #if	(FSI_TYPE == FSI_MNTCTL) || (FSI_TYPE == FSI_GETFSSTAT)
206 /*
207  * Done with iterations
208  */
209 void endmountent(fptr)
210 	/*ARGSUSED*/
211 	FILE *fptr;
212 {
213 	mnt = NULL;
214 
215 	if (mntbuf) {
216 		(void) free(mntbuf);
217 		mntbuf = (char *) NULL;
218 	}
219 }
220 #endif	/* FSI_MNTCTL || FSI_GETFSSTAT */
221 
222 #if	FSI_TYPE == FSI_GETMNTENT2
223 /*
224  * Prepare to iterate over mounted filesystem list
225  */
226 FILE *setmountent(file, mode)
227 	/*ARGSUSED*/
228 	char *file;
229 	char *mode;
230 {
231 	return(fopen(file, mode));
232 }
233 
234 /*
235  * Done with iteration
236  */
237 void endmountent(fptr)
238 	/*ARGSUSED*/
239 	FILE *fptr;
240 {
241 	fclose(fptr);
242 }
243 
244 /*
245  * Iterate over mount entries
246  */
247 mntent_t *getmountent(fptr)
248 	FILE *fptr;
249 {
250 	static mntent_t me;
251 	static struct mnttab mntent;
252 
253 	bzero((char *)&me, sizeof(mntent_t));
254 
255 #if     defined(UNICOS)
256         if (getmntent(fptr, &mntent) != NULL) {
257 #else
258         if (getmntent(fptr, &mntent) != -1) {
259 #endif
260 		me.me_path = mntent.mnt_mountp;
261 		me.me_type = mntent.mnt_fstype;
262 		if (mntent.mnt_mntopts && hasmntopt(&mntent, MNTOPT_RO))
263 			me.me_flags |= MEFLAG_READONLY;
264 
265 #if	defined(MNTTYPE_IGNORE)
266 		if (strcmp(mntent.mnt_fstype, MNTTYPE_IGNORE) == 0)
267 			me.me_flags |= MEFLAG_IGNORE;
268 #endif	/* MNTTYPE_IGNORE */
269 #if	defined(MNTTYPE_SWAP)
270 		if (strcmp(mntent.mnt_fstype, MNTTYPE_SWAP) == 0)
271 			me.me_flags |= MEFLAG_IGNORE;
272 #endif	/* MNTTYPE_SWAP */
273 
274 		return(&me);
275 	} else
276 		return((mntent_t *) NULL);
277 }
278 #endif	/* FSI_GETMNTNET2 */
279 
280 #if	FSI_TYPE == FSI_GETMNTENT
281 /*
282  * Prepare to iterate over mounted filesystem list
283  */
284 FILE *setmountent(file, mode)
285 	/*ARGSUSED*/
286 	char *file;
287 	char *mode;
288 {
289 	return(setmntent(file, mode));
290 }
291 
292 /*
293  * Done with iteration
294  */
295 void endmountent(fptr)
296 	/*ARGSUSED*/
297 	FILE *fptr;
298 {
299 	endmntent(fptr);
300 }
301 
302 /*
303  * Iterate over mount entries
304  */
305 mntent_t *getmountent(fptr)
306 	FILE *fptr;
307 {
308 	static mntent_t me;
309 	struct mntent *mntent;
310 
311 	bzero((char *)&me, sizeof(mntent_t));
312 
313 	if (mntent = getmntent(fptr)) {
314 		me.me_path = mntent->mnt_dir;
315 		me.me_type = mntent->mnt_type;
316 		if (mntent->mnt_opts && hasmntopt(mntent, MNTOPT_RO))
317 			me.me_flags |= MEFLAG_READONLY;
318 
319 #if	defined(MNTTYPE_IGNORE)
320 		if (strcmp(mntent->mnt_type, MNTTYPE_IGNORE) == 0)
321 			me.me_flags |= MEFLAG_IGNORE;
322 #endif	/* MNTTYPE_IGNORE */
323 #if	defined(MNTTYPE_SWAP)
324 		if (strcmp(mntent->mnt_type, MNTTYPE_SWAP) == 0)
325 			me.me_flags |= MEFLAG_IGNORE;
326 #endif	/* MNTTYPE_SWAP */
327 
328 		return(&me);
329 	} else
330 		return((mntent_t *) NULL);
331 }
332 #endif	/* FSI_GETMNTNET */
333 
334 #if	FSI_TYPE == FSI_GETMNT
335 /*
336  * getmnt() interface (Ultrix)
337  */
338 
339 #include <sys/fs_types.h>
340 
341 static int startmounts = 0;
342 
343 FILE *setmountent(file, mode)
344 	/*ARGSUSED*/
345 	char *file;
346 	char *mode;
347 {
348 	startmounts = 0;
349 }
350 
351 void endmountent(fptr)
352 	/*ARGSUSED*/
353 	FILE *fptr;
354 {
355 	/* NOOP */
356 }
357 
358 /*
359  * Iterate over mounted filesystems using getmnt()
360  */
361 mntent_t *getmountent(fptr)
362 	/*ARGSUSED*/
363 	FILE *fptr;
364 {
365 	struct fs_data fs_data;
366 	static mntent_t me;
367 
368 	if (getmnt(&startmounts, &fs_data, sizeof(fs_data), NOSTAT_MANY,
369 		   (char *) NULL) <= 0)
370 		return((mntent_t *) NULL);
371 
372 	bzero((char *)&me, sizeof(mntent_t));
373 	me.me_path = fs_data.fd_path;
374 	if (fs_data.fd_fstype == GT_NFS)
375 		me.me_type = METYPE_NFS;
376 	else
377 		me.me_type = METYPE_OTHER;
378 
379 	if (fs_data.fd_flags & M_RONLY)
380 		me.me_flags |= MEFLAG_READONLY;
381 
382 	return(&me);
383 }
384 #endif	/* FSI_GETMNT */
385 
386 /*
387  * Make a new (copy) of a mntent structure.
388  */
389 mntent_t *newmountent(old)
390 	mntent_t *old;
391 {
392 	mntent_t *new;
393 
394 	if (!old)
395 		return((mntent_t *) NULL);
396 
397 	new = (mntent_t *) xcalloc(1, sizeof(mntent_t));
398 	new->me_path = strdup(old->me_path);
399 	new->me_type = strdup(old->me_type);
400 	new->me_flags = old->me_flags;
401 
402 	return(new);
403 }
404