xref: /dragonfly/lib/libc/gen/getmntvinfo.c (revision 0f0f9bbd)
18dbc2111SMatthew Dillon /*
28dbc2111SMatthew Dillon  * Copyright (c) 1989, 1993
38dbc2111SMatthew Dillon  *	The Regents of the University of California.  All rights reserved.
48dbc2111SMatthew Dillon  * Copyright (c) 2008 The DragonFly Project.  All rights reserved.
58dbc2111SMatthew Dillon  *
68dbc2111SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
78dbc2111SMatthew Dillon  * modification, are permitted provided that the following conditions
88dbc2111SMatthew Dillon  * are met:
98dbc2111SMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
108dbc2111SMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
118dbc2111SMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
128dbc2111SMatthew Dillon  *    notice, this list of conditions and the following disclaimer in the
138dbc2111SMatthew Dillon  *    documentation and/or other materials provided with the distribution.
14*0f0f9bbdSzrj  * 3. Neither the name of the University nor the names of its contributors
158dbc2111SMatthew Dillon  *    may be used to endorse or promote products derived from this software
168dbc2111SMatthew Dillon  *    without specific prior written permission.
178dbc2111SMatthew Dillon  *
188dbc2111SMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
198dbc2111SMatthew Dillon  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
208dbc2111SMatthew Dillon  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
218dbc2111SMatthew Dillon  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
228dbc2111SMatthew Dillon  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
238dbc2111SMatthew Dillon  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
248dbc2111SMatthew Dillon  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
258dbc2111SMatthew Dillon  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
268dbc2111SMatthew Dillon  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
278dbc2111SMatthew Dillon  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
288dbc2111SMatthew Dillon  * SUCH DAMAGE.
298dbc2111SMatthew Dillon  *
308dbc2111SMatthew Dillon  * @(#)getmntinfo.c	8.1 (Berkeley) 6/4/93
318dbc2111SMatthew Dillon  */
328dbc2111SMatthew Dillon 
338dbc2111SMatthew Dillon #include <sys/param.h>
348dbc2111SMatthew Dillon #include <sys/ucred.h>
358dbc2111SMatthew Dillon #include <sys/mount.h>
368dbc2111SMatthew Dillon #include <sys/statvfs.h>
378dbc2111SMatthew Dillon #include <stdlib.h>
388dbc2111SMatthew Dillon 
398dbc2111SMatthew Dillon /*
408dbc2111SMatthew Dillon  * Return information about mounted filesystems.
418dbc2111SMatthew Dillon  */
428dbc2111SMatthew Dillon int
getmntvinfo(struct statfs ** mntbufp,struct statvfs ** mntvbufp,int flags)438dbc2111SMatthew Dillon getmntvinfo(struct statfs **mntbufp, struct statvfs **mntvbufp, int flags)
448dbc2111SMatthew Dillon {
458dbc2111SMatthew Dillon 	static struct statfs *mntsbuf;
468dbc2111SMatthew Dillon 	static struct statvfs *mntvbuf;
478dbc2111SMatthew Dillon 	static int mntsize;
488dbc2111SMatthew Dillon 	static int bufsize;
498dbc2111SMatthew Dillon 	static int vbufsize;
508dbc2111SMatthew Dillon 
518dbc2111SMatthew Dillon 	if (mntsize <= 0 && (mntsize = getvfsstat(NULL, NULL, 0, MNT_NOWAIT)) < 0)
528dbc2111SMatthew Dillon 		return (0);
538dbc2111SMatthew Dillon 	if (vbufsize > 0 && (mntsize = getvfsstat(mntsbuf, mntvbuf, vbufsize, flags)) < 0)
548dbc2111SMatthew Dillon 		return (0);
558dbc2111SMatthew Dillon 	while (vbufsize <= mntsize * (int)sizeof(struct statvfs)) {
568dbc2111SMatthew Dillon 		if (mntsbuf)
578dbc2111SMatthew Dillon 			free(mntsbuf);
588dbc2111SMatthew Dillon 		bufsize = (mntsize + 1) * sizeof(struct statfs);
598dbc2111SMatthew Dillon 		vbufsize = (mntsize + 1) * sizeof(struct statvfs);
60678e8cc6SSascha Wildner 		if ((mntsbuf = (struct statfs *)malloc(bufsize)) == NULL)
618dbc2111SMatthew Dillon 			return (0);
62678e8cc6SSascha Wildner 		if ((mntvbuf = (struct statvfs *)malloc(vbufsize)) == NULL)
638dbc2111SMatthew Dillon 			return (0);
648dbc2111SMatthew Dillon 		if ((mntsize = getvfsstat(mntsbuf, mntvbuf, vbufsize, flags)) < 0)
658dbc2111SMatthew Dillon 			return (0);
668dbc2111SMatthew Dillon 	}
678dbc2111SMatthew Dillon 	*mntbufp = mntsbuf;
688dbc2111SMatthew Dillon 	*mntvbufp = mntvbuf;
698dbc2111SMatthew Dillon 	return (mntsize);
708dbc2111SMatthew Dillon }
718dbc2111SMatthew Dillon 
72