xref: /openbsd-src/usr.bin/rdistd/filesys-os.c (revision e06ba6779e10d29d6c233e03dbbf2408bc89da66)
1*e06ba677Sguenther /*	$OpenBSD: filesys-os.c,v 1.13 2015/01/20 09:00:16 guenther Exp $	*/
21258a77dSderaadt 
3fdd8ad51Sdm /*
4fdd8ad51Sdm  * Copyright (c) 1983 Regents of the University of California.
5fdd8ad51Sdm  * All rights reserved.
6fdd8ad51Sdm  *
7fdd8ad51Sdm  * Redistribution and use in source and binary forms, with or without
8fdd8ad51Sdm  * modification, are permitted provided that the following conditions
9fdd8ad51Sdm  * are met:
10fdd8ad51Sdm  * 1. Redistributions of source code must retain the above copyright
11fdd8ad51Sdm  *    notice, this list of conditions and the following disclaimer.
12fdd8ad51Sdm  * 2. Redistributions in binary form must reproduce the above copyright
13fdd8ad51Sdm  *    notice, this list of conditions and the following disclaimer in the
14fdd8ad51Sdm  *    documentation and/or other materials provided with the distribution.
15f75387cbSmillert  * 3. Neither the name of the University nor the names of its contributors
16fdd8ad51Sdm  *    may be used to endorse or promote products derived from this software
17fdd8ad51Sdm  *    without specific prior written permission.
18fdd8ad51Sdm  *
19fdd8ad51Sdm  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20fdd8ad51Sdm  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21fdd8ad51Sdm  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22fdd8ad51Sdm  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23fdd8ad51Sdm  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24fdd8ad51Sdm  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25fdd8ad51Sdm  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26fdd8ad51Sdm  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27fdd8ad51Sdm  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28fdd8ad51Sdm  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29fdd8ad51Sdm  * SUCH DAMAGE.
30fdd8ad51Sdm  */
31fdd8ad51Sdm 
32b9fc9a72Sderaadt #include <sys/types.h>
33a59aa421Sguenther #include <sys/mount.h>
34*e06ba677Sguenther #include <stdlib.h>
35*e06ba677Sguenther #include <string.h>
36a59aa421Sguenther 
37*e06ba677Sguenther #include "server.h"
38fdd8ad51Sdm 
39fdd8ad51Sdm /*
40fdd8ad51Sdm  * OS specific file system routines
41fdd8ad51Sdm  */
42fdd8ad51Sdm 
43fdd8ad51Sdm static struct statfs   *mnt = NULL;
44fdd8ad51Sdm static int 		entries_left;
45fdd8ad51Sdm 
46fdd8ad51Sdm /*
47fdd8ad51Sdm  * getfsstat() version of get mount info routines.
48fdd8ad51Sdm  */
49a59aa421Sguenther int
setmountent(void)50a59aa421Sguenther setmountent(void)
51fdd8ad51Sdm {
52b0f9517bSmillert 	long size;
53fdd8ad51Sdm 
544a436879Skstailey 	size = getfsstat(NULL, 0, MNT_WAIT);
557b7f0d4dSderaadt 	if (size == -1)
56a59aa421Sguenther 		return (0);
57a59aa421Sguenther 
58a59aa421Sguenther 	free(mnt);
59fdd8ad51Sdm 	size *= sizeof(struct statfs);
60a59aa421Sguenther 	mnt = xmalloc(size);
61fdd8ad51Sdm 
62a59aa421Sguenther 	entries_left = getfsstat(mnt, size, MNT_WAIT);
63fdd8ad51Sdm 	if (entries_left == -1)
64a59aa421Sguenther 		return (0);
65fdd8ad51Sdm 
66a59aa421Sguenther 	return (1);
67fdd8ad51Sdm }
68fdd8ad51Sdm 
69fdd8ad51Sdm /*
70fdd8ad51Sdm  * getfsstat() version of getmountent()
71fdd8ad51Sdm  */
7240934732Smillert mntent_t *
getmountent(void)73a59aa421Sguenther getmountent(void)
74fdd8ad51Sdm {
75fdd8ad51Sdm 	static mntent_t mntstruct;
76b9fc9a72Sderaadt 	static char remote_dev[HOST_NAME_MAX+1 + PATH_MAX + 1];
77fdd8ad51Sdm 
78fdd8ad51Sdm 	if (!entries_left)
79a59aa421Sguenther 		return (NULL);
80fdd8ad51Sdm 
81a59aa421Sguenther 	memset(&mntstruct, 0, sizeof(mntstruct));
82fdd8ad51Sdm 
83fdd8ad51Sdm 	if (mnt->f_flags & MNT_RDONLY)
84fdd8ad51Sdm 		mntstruct.me_flags |= MEFLAG_READONLY;
8540934732Smillert 
86a59aa421Sguenther 	if (strcmp(mnt->f_fstypename, "nfs") == 0) {
8740934732Smillert 		strlcpy(remote_dev, mnt->f_mntfromname, sizeof(remote_dev));
88fdd8ad51Sdm 		mntstruct.me_path = remote_dev;
89*e06ba677Sguenther 		mntstruct.me_flags |= MEFLAG_NFS;
90*e06ba677Sguenther 	} else
91fdd8ad51Sdm 		mntstruct.me_path = mnt->f_mntonname;
92fdd8ad51Sdm 
93fdd8ad51Sdm 	mnt++;
94fdd8ad51Sdm 	entries_left--;
95fdd8ad51Sdm 
96fdd8ad51Sdm 	return (&mntstruct);
97fdd8ad51Sdm }
98fdd8ad51Sdm 
99fdd8ad51Sdm /*
100fdd8ad51Sdm  * Done with iterations
101fdd8ad51Sdm  */
10240934732Smillert void
endmountent(void)103a59aa421Sguenther endmountent(void)
104fdd8ad51Sdm {
105a59aa421Sguenther 	free(mnt);
106fdd8ad51Sdm 	mnt = NULL;
107fdd8ad51Sdm }
108fdd8ad51Sdm 
109fdd8ad51Sdm /*
110fdd8ad51Sdm  * Make a new (copy) of a mntent structure.
111fdd8ad51Sdm  */
11240934732Smillert mntent_t *
newmountent(const mntent_t * old)11340934732Smillert newmountent(const mntent_t *old)
114fdd8ad51Sdm {
115fdd8ad51Sdm 	mntent_t *new;
116fdd8ad51Sdm 
117a59aa421Sguenther 	new = xmalloc(sizeof *new);
118fbb35f5fSmillert 	new->me_path = xstrdup(old->me_path);
119fdd8ad51Sdm 	new->me_flags = old->me_flags;
120fdd8ad51Sdm 
121fdd8ad51Sdm 	return (new);
122fdd8ad51Sdm }
123