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