xref: /openbsd-src/usr.bin/rdistd/filesys-os.c (revision b9fc9a728fce9c4289b7e9a992665e28d5629a54)
1 /*	$OpenBSD: filesys-os.c,v 1.12 2015/01/16 06:40:11 deraadt 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. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/types.h>
33 #include <sys/mount.h>
34 
35 #include "defs.h"
36 
37 /*
38  * OS specific file system routines
39  */
40 
41 static struct statfs   *mnt = NULL;
42 static int 		entries_left;
43 
44 /*
45  * getfsstat() version of get mount info routines.
46  */
47 int
48 setmountent(void)
49 {
50 	long size;
51 
52 	size = getfsstat(NULL, 0, MNT_WAIT);
53 	if (size == -1)
54 		return (0);
55 
56 	free(mnt);
57 	size *= sizeof(struct statfs);
58 	mnt = xmalloc(size);
59 
60 	entries_left = getfsstat(mnt, size, MNT_WAIT);
61 	if (entries_left == -1)
62 		return (0);
63 
64 	return (1);
65 }
66 
67 /*
68  * getfsstat() version of getmountent()
69  */
70 mntent_t *
71 getmountent(void)
72 {
73 	static mntent_t mntstruct;
74 	static char remote_dev[HOST_NAME_MAX+1 + PATH_MAX + 1];
75 
76 	if (!entries_left)
77 		return (NULL);
78 
79 	memset(&mntstruct, 0, sizeof(mntstruct));
80 
81 	if (mnt->f_flags & MNT_RDONLY)
82 		mntstruct.me_flags |= MEFLAG_READONLY;
83 
84 	if (strcmp(mnt->f_fstypename, "nfs") == 0) {
85 		strlcpy(remote_dev, mnt->f_mntfromname, sizeof(remote_dev));
86 		mntstruct.me_path = remote_dev;
87 		mntstruct.me_type = METYPE_NFS;
88 	} else {
89 		mntstruct.me_path = mnt->f_mntonname;
90 		mntstruct.me_type = METYPE_OTHER;
91 	}
92 
93 	mnt++;
94 	entries_left--;
95 
96 	return (&mntstruct);
97 }
98 
99 /*
100  * Done with iterations
101  */
102 void
103 endmountent(void)
104 {
105 	free(mnt);
106 	mnt = NULL;
107 }
108 
109 /*
110  * Make a new (copy) of a mntent structure.
111  */
112 mntent_t *
113 newmountent(const mntent_t *old)
114 {
115 	mntent_t *new;
116 
117 	new = xmalloc(sizeof *new);
118 	new->me_path = xstrdup(old->me_path);
119 	new->me_type = xstrdup(old->me_type);
120 	new->me_flags = old->me_flags;
121 
122 	return (new);
123 }
124