xref: /openbsd-src/usr.bin/rdistd/filesys-os.c (revision 4a4368796d51fb921b4bf302d28fa07b80b2108c)
1 /*	$OpenBSD: filesys-os.c,v 1.4 1997/06/17 20:37:53 kstailey 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 static char RCSid[] =
38 "$OpenBSD: filesys-os.c,v 1.4 1997/06/17 20:37:53 kstailey Exp $";
39 
40 static char sccsid[] = "@(#)filesys-os.c";
41 
42 static char copyright[] =
43 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
44  All rights reserved.\n";
45 #endif /* not lint */
46 
47 /*
48  * OS specific file system routines
49  */
50 
51 #include "defs.h"
52 #include "filesys.h"
53 
54 #if 	FSI_TYPE == FSI_GETFSSTAT
55 static struct statfs   *mnt = NULL;
56 #if	FSTYPENAME
57 #define f_type_eq(a, b) (! strcmp (((struct statfs *) (a))->f_fstypename, (b)))
58 #else	/* !FSTYPENAME */
59 #define	f_type_eq(a, b) (((struct statfs *) a)->f_type == (b))
60 #endif /* !FSTYPENAME */
61 #endif	/* FSI_GETFSSTAT */
62 
63 #if	FSI_TYPE == FSI_MNTCTL
64 static struct vmount   *mnt = NULL;
65 #endif	/* FSI_MNTCTL */
66 
67 #if	(FSI_TYPE == FSI_MNTCTL) || (FSI_TYPE == FSI_GETFSSTAT)
68 static char 	       *mntbuf = NULL;
69 static int 		entries_left;
70 #endif	/* FSI_MNTCTL || FSI_GETFSSTAT */
71 
72 #if	FSI_TYPE == FSI_MNTCTL
73 /*
74  * AIX version of setmountent()
75  */
76 FILE *setmountent(file, mode)
77 	/*ARGSUSED*/
78 	char *file;
79 	char *mode;
80 {
81 	u_int size;
82 
83 	if (mntbuf)
84 		(void) free(mntbuf);
85 
86 	mntctl(MCTL_QUERY, sizeof(size), &size);
87 	mntbuf = (char *) xmalloc(size);
88 
89 	entries_left = mntctl(MCTL_QUERY, size, mntbuf);
90 	if (!entries_left)
91 		return(NULL);
92 
93 	mnt = (struct vmount *)mntbuf;
94 	return((FILE *) 1);
95 }
96 #endif	/* FSI_MNTCTL */
97 
98 #if	FSI_TYPE == FSI_GETFSSTAT
99 /*
100  * getfsstat() version of get mount info routines.
101  */
102 FILE *setmountent(file, mode)
103 	/*ARGSUSED*/
104 	char *file;
105 	char *mode;
106 {
107 	int size;
108 
109 	if (mntbuf)
110 		(void) free(mntbuf);
111 
112 	size = getfsstat(NULL, 0, MNT_WAIT);
113 	if (size == -1)
114 		return (NULL);
115 	size *= sizeof(struct statfs);
116 	mntbuf = (char *) xmalloc(size);
117 
118 	entries_left = getfsstat((struct statfs *)mntbuf, size, MNT_WAIT);
119 	if (entries_left == -1)
120 		return(NULL);
121 
122 	mnt = (struct statfs *) mntbuf;
123 
124 	return((FILE *) 1);
125 }
126 #endif	/* FSI_GETFSSTAT */
127 
128 #if	FSI_TYPE == FSI_MNTCTL
129 /*
130  * AIX version of getmountent()
131  */
132 /*
133  * Iterate over mount entries
134  */
135 mntent_t *getmountent(fptr)
136 	/*ARGSUSED*/
137 	FILE *fptr;
138 {
139 	static mntent_t mntstruct;
140 
141 	if (!entries_left)
142 		return((mntent_t*)0);
143 
144 	bzero((char *) &mntstruct, sizeof(mntstruct));
145 
146 	if (mnt->vmt_flags & MNT_READONLY)
147 		mntstruct.me_flags |= MEFLAG_READONLY;
148 
149 	mntstruct.me_path = vmt2dataptr(mnt, VMT_STUB);
150 	switch ((struct vmount*)mnt->vmt_gfstype) {
151 	      case MNT_NFS:
152 		mntstruct.me_type = METYPE_NFS;
153 		break;
154 	      default:
155 		mntstruct.me_type = METYPE_OTHER;
156 		break;
157 	}
158 
159 	mnt = (struct vmount*)((mnt->vmt_length)+(char *)mnt);
160 	entries_left--;
161 
162 	return(&mntstruct);
163 }
164 #endif	/* FSI_MNTCTL */
165 
166 #if	FSI_TYPE == FSI_GETFSSTAT
167 /*
168  * getfsstat() version of getmountent()
169  */
170 mntent_t *getmountent(fptr)
171 	/*ARGSUSED*/
172 	FILE *fptr;
173 {
174 	static mntent_t mntstruct;
175 	static char remote_dev[MAXHOSTNAMELEN+MAXPATHLEN+1];
176 
177 	if (!entries_left)
178 		return((mntent_t*)0);
179 
180 	bzero((char *) &mntstruct, sizeof(mntstruct));
181 
182 #if	defined(MNT_RDONLY)
183 	if (mnt->f_flags & MNT_RDONLY)
184 		mntstruct.me_flags |= MEFLAG_READONLY;
185 #endif
186 #if	defined(M_RDONLY)
187 	if (mnt->f_flags & M_RDONLY)
188 		mntstruct.me_flags |= MEFLAG_READONLY;
189 #endif
190 	if (f_type_eq (mnt, MOUNT_NFS)) {
191 		(void) sprintf(remote_dev, "%s", mnt->f_mntfromname);
192 		mntstruct.me_path = remote_dev;
193 		mntstruct.me_type = METYPE_NFS;
194 	}
195 	else {
196 		mntstruct.me_path = mnt->f_mntonname;
197 		mntstruct.me_type = METYPE_OTHER;
198 	}
199 
200 	mnt++;
201 	entries_left--;
202 
203 	return(&mntstruct);
204 }
205 #endif
206 
207 #if	(FSI_TYPE == FSI_MNTCTL) || (FSI_TYPE == FSI_GETFSSTAT)
208 /*
209  * Done with iterations
210  */
211 void endmountent(fptr)
212 	/*ARGSUSED*/
213 	FILE *fptr;
214 {
215 	mnt = NULL;
216 
217 	if (mntbuf) {
218 		(void) free(mntbuf);
219 		mntbuf = NULL;
220 	}
221 }
222 #endif	/* FSI_MNTCTL || FSI_GETFSSTAT */
223 
224 #if	FSI_TYPE == FSI_GETMNTENT2
225 /*
226  * Prepare to iterate over mounted filesystem list
227  */
228 FILE *setmountent(file, mode)
229 	/*ARGSUSED*/
230 	char *file;
231 	char *mode;
232 {
233 	return(fopen(file, mode));
234 }
235 
236 /*
237  * Done with iteration
238  */
239 void endmountent(fptr)
240 	/*ARGSUSED*/
241 	FILE *fptr;
242 {
243 	fclose(fptr);
244 }
245 
246 /*
247  * Iterate over mount entries
248  */
249 mntent_t *getmountent(fptr)
250 	FILE *fptr;
251 {
252 	static mntent_t me;
253 	static struct mnttab mntent;
254 
255 	bzero((char *)&me, sizeof(mntent_t));
256 
257 #if     defined(UNICOS)
258         if (getmntent(fptr, &mntent) != NULL) {
259 #else
260         if (getmntent(fptr, &mntent) != -1) {
261 #endif
262 		me.me_path = mntent.mnt_mountp;
263 		me.me_type = mntent.mnt_fstype;
264 		if (mntent.mnt_mntopts && hasmntopt(&mntent, MNTOPT_RO))
265 			me.me_flags |= MEFLAG_READONLY;
266 
267 #if	defined(MNTTYPE_IGNORE)
268 		if (strcmp(mntent.mnt_fstype, MNTTYPE_IGNORE) == 0)
269 			me.me_flags |= MEFLAG_IGNORE;
270 #endif	/* MNTTYPE_IGNORE */
271 #if	defined(MNTTYPE_SWAP)
272 		if (strcmp(mntent.mnt_fstype, MNTTYPE_SWAP) == 0)
273 			me.me_flags |= MEFLAG_IGNORE;
274 #endif	/* MNTTYPE_SWAP */
275 
276 		return(&me);
277 	} else
278 		return(NULL);
279 }
280 #endif	/* FSI_GETMNTNET2 */
281 
282 #if	FSI_TYPE == FSI_GETMNTENT
283 /*
284  * Prepare to iterate over mounted filesystem list
285  */
286 FILE *setmountent(file, mode)
287 	/*ARGSUSED*/
288 	char *file;
289 	char *mode;
290 {
291 	return(setmntent(file, mode));
292 }
293 
294 /*
295  * Done with iteration
296  */
297 void endmountent(fptr)
298 	/*ARGSUSED*/
299 	FILE *fptr;
300 {
301 	endmntent(fptr);
302 }
303 
304 /*
305  * Iterate over mount entries
306  */
307 mntent_t *getmountent(fptr)
308 	FILE *fptr;
309 {
310 	static mntent_t me;
311 	struct mntent *mntent;
312 
313 	bzero((char *)&me, sizeof(mntent_t));
314 
315 	if (mntent = getmntent(fptr)) {
316 		me.me_path = mntent->mnt_dir;
317 		me.me_type = mntent->mnt_type;
318 		if (mntent->mnt_opts && hasmntopt(mntent, MNTOPT_RO))
319 			me.me_flags |= MEFLAG_READONLY;
320 
321 #if	defined(MNTTYPE_IGNORE)
322 		if (strcmp(mntent->mnt_type, MNTTYPE_IGNORE) == 0)
323 			me.me_flags |= MEFLAG_IGNORE;
324 #endif	/* MNTTYPE_IGNORE */
325 #if	defined(MNTTYPE_SWAP)
326 		if (strcmp(mntent->mnt_type, MNTTYPE_SWAP) == 0)
327 			me.me_flags |= MEFLAG_IGNORE;
328 #endif	/* MNTTYPE_SWAP */
329 
330 		return(&me);
331 	} else
332 		return(NULL);
333 }
334 #endif	/* FSI_GETMNTNET */
335 
336 #if	FSI_TYPE == FSI_GETMNT
337 /*
338  * getmnt() interface (Ultrix)
339  */
340 
341 #include <sys/fs_types.h>
342 
343 static int startmounts = 0;
344 
345 FILE *setmountent(file, mode)
346 	/*ARGSUSED*/
347 	char *file;
348 	char *mode;
349 {
350 	startmounts = 0;
351 }
352 
353 void endmountent(fptr)
354 	/*ARGSUSED*/
355 	FILE *fptr;
356 {
357 	/* NOOP */
358 }
359 
360 /*
361  * Iterate over mounted filesystems using getmnt()
362  */
363 mntent_t *getmountent(fptr)
364 	/*ARGSUSED*/
365 	FILE *fptr;
366 {
367 	struct fs_data fs_data;
368 	static mntent_t me;
369 
370 	if (getmnt(&startmounts, &fs_data, sizeof(fs_data), NOSTAT_MANY,
371 		   NULL) <= 0)
372 		return(NULL);
373 
374 	bzero((char *)&me, sizeof(mntent_t));
375 	me.me_path = fs_data.fd_path;
376 	if (fs_data.fd_fstype == GT_NFS)
377 		me.me_type = METYPE_NFS;
378 	else
379 		me.me_type = METYPE_OTHER;
380 
381 	if (fs_data.fd_flags & M_RONLY)
382 		me.me_flags |= MEFLAG_READONLY;
383 
384 	return(&me);
385 }
386 #endif	/* FSI_GETMNT */
387 
388 /*
389  * Make a new (copy) of a mntent structure.
390  */
391 mntent_t *newmountent(old)
392 	mntent_t *old;
393 {
394 	mntent_t *new;
395 
396 	if (!old)
397 		return(NULL);
398 
399 	new = (mntent_t *) xcalloc(1, sizeof(mntent_t));
400 	new->me_path = strdup(old->me_path);
401 	new->me_type = strdup(old->me_type);
402 	new->me_flags = old->me_flags;
403 
404 	return(new);
405 }
406