]> git.saurik.com Git - apple/libinfo.git/blame - lookup.subproj/lu_fstab.c
Libinfo-89.1.tar.gz
[apple/libinfo.git] / lookup.subproj / lu_fstab.c
CommitLineData
03fb6eb0
A
1/*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24/*
25 * fstab entry lookup
26 * Copyright (C) 1989 by NeXT, Inc.
27 */
28
29#include <stdlib.h>
30#include <mach/mach.h>
31#include <stdio.h>
32#include <string.h>
33#include <rpc/types.h>
34#include <rpc/xdr.h>
35#include <fstab.h>
36
37#include "lookup.h"
38#include "_lu_types.h"
39#include "lu_utils.h"
40#include "lu_overrides.h"
41
42static struct fstab global_fs;
43static int global_free = 1;
44static char *fs_data = NULL;
45static unsigned fs_datalen = 0;
46static int fs_nentries = 0;
47static int fs_start = 1;
48static XDR fs_xdr = { 0 };
49
50static void
51freeold(void)
52{
53 if (global_free == 1) return;
54
55 free(global_fs.fs_spec);
56 free(global_fs.fs_file);
57 free(global_fs.fs_type);
58 free(global_fs.fs_vfstype);
59 free(global_fs.fs_mntops);
60
61 global_free = 1;
62}
63
64static void
65convert_fs(_lu_fsent *lu_fs)
66{
67 freeold();
68
69 global_fs.fs_spec = strdup(lu_fs->fs_spec);
70 global_fs.fs_file = strdup(lu_fs->fs_file);
71
72 /*
73 * Special case - if vfstype is unknown and spec is
74 * of the form foo:bar, then assume nfs.
75 */
76 if (lu_fs->fs_vfstype[0] == '\0')
77 {
78 if (strchr(lu_fs->fs_spec, ':') != NULL)
79 {
80 global_fs.fs_vfstype = malloc(4);
81 strcpy(global_fs.fs_vfstype, "nfs");
82 }
83 else global_fs.fs_vfstype = strdup(lu_fs->fs_vfstype);
84 }
85 else
86 {
87 global_fs.fs_vfstype = strdup(lu_fs->fs_vfstype);
88 }
89
90 global_fs.fs_mntops = strdup(lu_fs->fs_mntops);
91 global_fs.fs_type = strdup(lu_fs->fs_type);
92 global_fs.fs_freq = lu_fs->fs_freq;
93 global_fs.fs_passno = lu_fs->fs_passno;
94
95 global_free = 0;
96}
97
98static struct fstab *
99lu_getfsbyname(const char *name)
100{
101 unsigned datalen;
102 char namebuf[_LU_MAXLUSTRLEN + BYTES_PER_XDR_UNIT];
103 XDR outxdr;
104 XDR inxdr;
105 _lu_fsent_ptr lu_fs;
106 static int proc = -1;
107 unit lookup_buf[MAX_INLINE_UNITS];
108
109 if (proc < 0)
110 {
111 if (_lookup_link(_lu_port, "getfsbyname", &proc) != KERN_SUCCESS)
112 {
113 return (NULL);
114 }
115 }
116
117 xdrmem_create(&outxdr, namebuf, sizeof(namebuf), XDR_ENCODE);
118 if (!xdr__lu_string(&outxdr, &name))
119 {
120 xdr_destroy(&outxdr);
121 return (NULL);
122 }
123
124 datalen = MAX_INLINE_UNITS;
125 if (_lookup_one(_lu_port, proc, (unit *)namebuf,
126 xdr_getpos(&outxdr) / BYTES_PER_XDR_UNIT, lookup_buf, &datalen)
127 != KERN_SUCCESS)
128 {
129 xdr_destroy(&outxdr);
130 return (NULL);
131 }
132
133 xdr_destroy(&outxdr);
134
135 datalen *= BYTES_PER_XDR_UNIT;
136 xdrmem_create(&inxdr, lookup_buf, datalen,
137 XDR_DECODE);
138 lu_fs = NULL;
139 if (!xdr__lu_fsent_ptr(&inxdr, &lu_fs) || (lu_fs == NULL))
140 {
141 xdr_destroy(&inxdr);
142 return (NULL);
143 }
144
145 xdr_destroy(&inxdr);
146
147 convert_fs(lu_fs);
148 xdr_free(xdr__lu_fsent_ptr, &lu_fs);
149 return (&global_fs);
150}
151
152static void
153lu_endfsent(void)
154{
155 fs_nentries = 0;
156 if (fs_data != NULL)
157 {
158 freeold();
159 vm_deallocate(mach_task_self(), (vm_address_t)fs_data, fs_datalen);
160 fs_data = NULL;
161 }
162}
163
164static int
165lu_setfsent(void)
166{
167 lu_endfsent();
168 fs_start = 1;
169 return (1);
170}
171
172static struct fstab *
173lu_getfsent()
174{
175 static int proc = -1;
176 _lu_fsent lu_fs;
177
178 if (fs_start == 1)
179 {
180 fs_start = 0;
181
182 if (proc < 0)
183 {
184 if (_lookup_link(_lu_port, "getfsent", &proc) !=
185 KERN_SUCCESS)
186 {
187 lu_endfsent();
188 return (NULL);
189 }
190 }
191
192 if (_lookup_all(_lu_port, proc, NULL, 0, &fs_data, &fs_datalen)
193 != KERN_SUCCESS)
194 {
195 lu_endfsent();
196 return (NULL);
197 }
198
199#ifdef NOTDEF
200/* NOTDEF because OOL buffers are counted in bytes with untyped IPC */
201 fs_datalen *= BYTES_PER_XDR_UNIT;
202#endif
203 xdrmem_create(&fs_xdr, fs_data,
204 fs_datalen, XDR_DECODE);
205 if (!xdr_int(&fs_xdr, &fs_nentries))
206 {
207 xdr_destroy(&fs_xdr);
208 lu_endfsent();
209 return (NULL);
210 }
211 }
212
213 if (fs_nentries == 0)
214 {
215 xdr_destroy(&fs_xdr);
216 lu_endfsent();
217 return (NULL);
218 }
219
220 bzero(&lu_fs, sizeof(lu_fs));
221 if (!xdr__lu_fsent(&fs_xdr, &lu_fs))
222 {
223 xdr_destroy(&fs_xdr);
224 lu_endfsent();
225 return (NULL);
226 }
227
228 fs_nentries--;
229 convert_fs(&lu_fs);
230 xdr_free(xdr__lu_fsent, &lu_fs);
231 return (&global_fs);
232}
233
234struct fstab *
235lu_getfsspec(const char *name)
236{
237 if (name == NULL) return (struct fstab *)NULL;
238 return lu_getfsbyname(name);
239}
240
241struct fstab *
242lu_getfsfile(const char *name)
243{
244 struct fstab *fs;
245
246 if (name == NULL) return (struct fstab *)NULL;
247
248 setfsent();
249 for (fs = lu_getfsent(); fs != NULL; fs = lu_getfsent())
250 if (!strcmp(fs->fs_file, name)) return fs;
251
252 endfsent();
253 return (struct fstab *)NULL;
254}
255
256struct fstab *
257getfsbyname(const char *name)
258{
259 if (_lu_running()) return (lu_getfsbyname(name));
260 return (NULL);
261}
262
263struct fstab *
264getfsent(void)
265{
266 if (_lu_running()) return (lu_getfsent());
267 return (_old_getfsent());
268}
269
270int
271setfsent(void)
272{
273 if (_lu_running()) return (lu_setfsent());
274 return (_old_setfsent());
275}
276
277void
278endfsent(void)
279{
280 if (_lu_running()) lu_endfsent();
281 else _old_endfsent();
282}
283
284struct fstab *
285getfsspec(const char *name)
286{
287 if (_lu_running()) return (lu_getfsspec(name));
288 return (_old_getfsspec(name));
289}
290
291struct fstab *
292getfsfile(const char *name)
293{
294 if (_lu_running()) return (lu_getfsfile(name));
295 return (_old_getfsfile(name));
296}