Merge pull request #12860 from monojenkins/backport-pr-12856-to-2019-02
[mono-project.git] / support / fstab.c
blobd662ad97d96c341ed9544b8d16044443bb541f47
1 /*
2 * <fstab.h> wrapper functions.
4 * Authors:
5 * Jonathan Pryor (jonpryor@vt.edu)
7 * Copyright (C) 2004-2005 Jonathan Pryor
8 */
10 #include <errno.h>
11 #include <string.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <stddef.h>
16 #include "map.h"
17 #include "mph.h"
19 #if defined (HAVE_CHECKLIST_H)
20 #include <checklist.h>
21 #elif defined (HAVE_FSTAB_H)
22 #include <fstab.h>
23 #endif /* def HAVE_FSTAB_H */
25 #ifdef HAVE_SYS_VFSTAB_H
26 #include <sys/vfstab.h>
27 #endif /* def HAVE_SYS_VFSTAB_H */
29 G_BEGIN_DECLS
31 #ifdef HAVE_CHECKLIST_H
33 typedef struct checklist mph_fstab;
35 static const mph_string_offset_t
36 fstab_offsets[] = {
37 MPH_STRING_OFFSET (struct checklist, fs_spec, MPH_STRING_OFFSET_PTR),
38 MPH_STRING_OFFSET (struct checklist, fs_dir, MPH_STRING_OFFSET_PTR),
39 MPH_STRING_OFFSET (struct checklist, fs_type, MPH_STRING_OFFSET_PTR)
42 static const mph_string_offset_t
43 mph_fstab_offsets[] = {
44 MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Fstab, fs_spec, MPH_STRING_OFFSET_PTR),
45 MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Fstab, fs_file, MPH_STRING_OFFSET_PTR),
46 MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Fstab, fs_type, MPH_STRING_OFFSET_PTR)
49 #elif defined (HAVE_FSTAB_H) && defined(_AIX)
51 /* AIX defines fstab, but it has contents like checklist */
53 typedef struct fstab mph_fstab;
55 static const mph_string_offset_t
56 fstab_offsets[] = {
57 MPH_STRING_OFFSET (struct fstab, fs_spec, MPH_STRING_OFFSET_PTR),
58 MPH_STRING_OFFSET (struct fstab, fs_file, MPH_STRING_OFFSET_PTR),
59 MPH_STRING_OFFSET (struct fstab, fs_type, MPH_STRING_OFFSET_PTR)
62 static const mph_string_offset_t
63 mph_fstab_offsets[] = {
64 MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Fstab, fs_spec, MPH_STRING_OFFSET_PTR),
65 MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Fstab, fs_file, MPH_STRING_OFFSET_PTR),
66 MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Fstab, fs_type, MPH_STRING_OFFSET_PTR)
69 #elif defined (HAVE_FSTAB_H)
71 typedef struct fstab mph_fstab;
73 static const mph_string_offset_t
74 fstab_offsets[] = {
75 MPH_STRING_OFFSET (struct fstab, fs_spec, MPH_STRING_OFFSET_PTR),
76 MPH_STRING_OFFSET (struct fstab, fs_file, MPH_STRING_OFFSET_PTR),
77 MPH_STRING_OFFSET (struct fstab, fs_vfstype, MPH_STRING_OFFSET_PTR),
78 MPH_STRING_OFFSET (struct fstab, fs_mntops, MPH_STRING_OFFSET_PTR),
79 MPH_STRING_OFFSET (struct fstab, fs_type, MPH_STRING_OFFSET_PTR)
82 static const mph_string_offset_t
83 mph_fstab_offsets[] = {
84 MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Fstab, fs_spec, MPH_STRING_OFFSET_PTR),
85 MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Fstab, fs_file, MPH_STRING_OFFSET_PTR),
86 MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Fstab, fs_vfstype, MPH_STRING_OFFSET_PTR),
87 MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Fstab, fs_mntops, MPH_STRING_OFFSET_PTR),
88 MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Fstab, fs_type, MPH_STRING_OFFSET_PTR)
91 #endif /* def HAVE_FSTAB_H */
93 #if defined (HAVE_CHECKLIST_H) || defined (HAVE_FSTAB_H)
96 * Copy the native `fstab' structure to it's managed representation.
98 * To minimize separate mallocs, all the strings are allocated within the same
99 * memory block (stored in _fs_buf_).
101 static int
102 copy_fstab (struct Mono_Posix_Syscall__Fstab *to, mph_fstab *from)
104 char *buf;
106 memset (to, 0, sizeof(*to));
108 buf = _mph_copy_structure_strings (to, mph_fstab_offsets,
109 from, fstab_offsets, sizeof(fstab_offsets)/sizeof(fstab_offsets[0]));
111 to->fs_freq = from->fs_freq;
112 to->fs_passno = from->fs_passno;
114 to->_fs_buf_ = buf;
115 if (buf == NULL) {
116 return -1;
119 return 0;
122 #endif /* def HAVE_CHECKLIST_H || def HAVE_FSTAB_H */
124 #ifdef HAVE_SYS_VFSTAB_H
127 * Solaris doesn't provide <fstab.h> but has equivalent functionality in
128 * <sys/fstab.h> via getvfsent(3C) and company.
131 typedef struct vfstab mph_fstab;
133 static const mph_string_offset_t
134 vfstab_offsets[] = {
135 MPH_STRING_OFFSET (struct vfstab, vfs_special, MPH_STRING_OFFSET_PTR),
136 MPH_STRING_OFFSET (struct vfstab, vfs_mountp, MPH_STRING_OFFSET_PTR),
137 MPH_STRING_OFFSET (struct vfstab, vfs_fstype, MPH_STRING_OFFSET_PTR),
138 MPH_STRING_OFFSET (struct vfstab, vfs_mntopts, MPH_STRING_OFFSET_PTR)
141 static const mph_string_offset_t
142 mph_fstab_offsets[] = {
143 MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Fstab, fs_spec, MPH_STRING_OFFSET_PTR),
144 MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Fstab, fs_file, MPH_STRING_OFFSET_PTR),
145 MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Fstab, fs_vfstype, MPH_STRING_OFFSET_PTR),
146 MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Fstab, fs_mntops, MPH_STRING_OFFSET_PTR)
150 * Copy the native `vfstab' structure to it's managed representation.
152 * To minimize separate mallocs, all the strings are allocated within the same
153 * memory block (stored in _fs_buf_).
155 static int
156 copy_fstab (struct Mono_Posix_Syscall__Fstab *to, struct vfstab *from)
158 char *buf;
160 memset (to, 0, sizeof(*to));
162 buf = _mph_copy_structure_strings (to, mph_fstab_offsets,
163 from, vfstab_offsets, sizeof(vfstab_offsets)/sizeof(vfstab_offsets[0]));
165 to->fs_type = NULL;
166 to->fs_freq = -1;
167 to->fs_passno = -1;
169 to->_fs_buf_ = buf;
170 if (buf == NULL) {
171 return -1;
174 return 0;
178 * Implement Linux/BSD getfsent(3) in terms of Solaris getvfsent(3C)...
180 static FILE*
181 etc_fstab;
183 static int
184 setfsent (void)
186 /* protect from bad users calling setfsent(), setfsent(), ... endfsent() */
187 if (etc_fstab != NULL)
188 fclose (etc_fstab);
189 etc_fstab = fopen ("/etc/vfstab", "r");
190 if (etc_fstab != NULL)
191 return 1;
192 return 0;
195 static void
196 endfsent (void)
198 fclose (etc_fstab);
199 etc_fstab = NULL;
202 static struct vfstab
203 cur_vfstab_entry;
205 static struct vfstab*
206 getfsent (void)
208 int r;
209 r = getvfsent (etc_fstab, &cur_vfstab_entry);
210 if (r == 0)
211 return &cur_vfstab_entry;
212 return NULL;
215 static struct vfstab*
216 getfsfile (const char *mount_point)
218 int r;
219 int close = 0;
220 if (etc_fstab == 0) {
221 close = 1;
222 if (setfsent () != 1)
223 return NULL;
225 rewind (etc_fstab);
226 r = getvfsfile (etc_fstab, &cur_vfstab_entry, (char*) mount_point);
227 if (close)
228 endfsent ();
229 if (r == 0)
230 return &cur_vfstab_entry;
231 return NULL;
234 static struct vfstab*
235 getfsspec (const char *special_file)
237 int r;
238 int close = 0;
239 if (etc_fstab == 0) {
240 close = 1;
241 if (setfsent () != 1)
242 return NULL;
244 rewind (etc_fstab);
245 r = getvfsspec (etc_fstab, &cur_vfstab_entry, (char*) special_file);
246 if (close)
247 endfsent ();
248 if (r == 0)
249 return &cur_vfstab_entry;
250 return NULL;
253 #endif /* def HAVE_SYS_VFSTAB_H */
255 #if defined (HAVE_FSTAB_H) || defined (HAVE_CHECKPOINT_H) || defined (HAVE_SYS_VFSTAB_H)
258 Mono_Posix_Syscall_endfsent (void)
260 endfsent ();
261 return 0;
264 gint32
265 Mono_Posix_Syscall_getfsent (struct Mono_Posix_Syscall__Fstab *fsbuf)
267 mph_fstab *fs;
269 if (fsbuf == NULL) {
270 errno = EFAULT;
271 return -1;
274 fs = getfsent ();
275 if (fs == NULL)
276 return -1;
278 if (copy_fstab (fsbuf, fs) == -1) {
279 errno = ENOMEM;
280 return -1;
282 return 0;
285 gint32
286 Mono_Posix_Syscall_getfsfile (const char *mount_point,
287 struct Mono_Posix_Syscall__Fstab *fsbuf)
289 mph_fstab *fs;
291 if (fsbuf == NULL) {
292 errno = EFAULT;
293 return -1;
296 fs = getfsfile (mount_point);
297 if (fs == NULL)
298 return -1;
300 if (copy_fstab (fsbuf, fs) == -1) {
301 errno = ENOMEM;
302 return -1;
304 return 0;
307 gint32
308 Mono_Posix_Syscall_getfsspec (const char *special_file,
309 struct Mono_Posix_Syscall__Fstab *fsbuf)
311 mph_fstab *fs;
313 if (fsbuf == NULL) {
314 errno = EFAULT;
315 return -1;
318 fs = getfsspec (special_file);
319 if (fs == NULL)
320 return -1;
322 if (copy_fstab (fsbuf, fs) == -1) {
323 errno = ENOMEM;
324 return -1;
326 return 0;
329 gint32
330 Mono_Posix_Syscall_setfsent (void)
332 return setfsent ();
335 #endif /* def HAVE_FSTAB_H || def HAVE_CHECKPOINT_H || def HAVE_SYS_VFSTAB_H */
337 G_END_DECLS
340 * vim: noexpandtab