2 * Copyright (c) 1999-2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
27 * Copyright (C) 1989 by NeXT, Inc.
31 #include <mach/mach.h>
34 #include <rpc/types.h>
40 #include "_lu_types.h"
42 #include "lu_overrides.h"
44 static pthread_mutex_t _fstab_lock
= PTHREAD_MUTEX_INITIALIZER
;
51 free_fstab_data(struct fstab
*f
)
53 if (f
== NULL
) return;
55 if (f
->fs_spec
!= NULL
) free(f
->fs_spec
);
56 if (f
->fs_file
!= NULL
) free(f
->fs_file
);
57 if (f
->fs_vfstype
!= NULL
) free(f
->fs_vfstype
);
58 if (f
->fs_mntops
!= NULL
) free(f
->fs_mntops
);
59 if (f
->fs_type
!= NULL
) free(f
->fs_type
);
63 free_fstab(struct fstab
*f
)
65 if (f
== NULL
) return;
71 free_lu_thread_info_fstab(void *x
)
73 struct lu_thread_info
*tdata
;
75 if (x
== NULL
) return;
77 tdata
= (struct lu_thread_info
*)x
;
79 if (tdata
->lu_entry
!= NULL
)
81 free_fstab((struct fstab
*)tdata
->lu_entry
);
82 tdata
->lu_entry
= NULL
;
85 _lu_data_free_vm_xdr(tdata
);
91 extract_fstab(XDR
*xdr
)
93 int i
, j
, nkeys
, nvals
, status
;
97 if (xdr
== NULL
) return NULL
;
99 if (!xdr_int(xdr
, &nkeys
)) return NULL
;
101 f
= (struct fstab
*)calloc(1, sizeof(struct fstab
));
103 for (i
= 0; i
< nkeys
; i
++)
109 status
= _lu_xdr_attribute(xdr
, &key
, &vals
, &nvals
);
124 if ((f
->fs_spec
== NULL
) && (!strcmp("name", key
)))
126 f
->fs_spec
= vals
[0];
129 else if ((f
->fs_file
== NULL
) && (!strcmp("dir", key
)))
131 f
->fs_file
= vals
[0];
134 else if ((f
->fs_vfstype
== NULL
) && (!strcmp("vfstype", key
)))
136 f
->fs_vfstype
= vals
[0];
139 else if ((f
->fs_mntops
== NULL
) && (!strcmp("opts", key
)))
141 f
->fs_mntops
= vals
[0];
144 else if ((f
->fs_type
== NULL
) && (!strcmp("type", key
)))
146 f
->fs_type
= vals
[0];
149 else if (!strcmp("freq", key
))
151 f
->fs_freq
= atoi(vals
[0]);
153 else if (!strcmp("passno", key
))
155 f
->fs_passno
= atoi(vals
[0]);
161 for (; j
< nvals
; j
++) free(vals
[j
]);
166 if (f
->fs_spec
== NULL
) f
->fs_spec
= strdup("");
167 if (f
->fs_file
== NULL
) f
->fs_file
= strdup("");
168 if (f
->fs_vfstype
== NULL
) f
->fs_vfstype
= strdup("");
169 if (f
->fs_mntops
== NULL
) f
->fs_mntops
= strdup("");
170 if (f
->fs_type
== NULL
) f
->fs_type
= strdup("");
175 static struct fstab
*
176 copy_fstab(struct fstab
*in
)
180 if (in
== NULL
) return NULL
;
182 f
= (struct fstab
*)calloc(1, sizeof(struct fstab
));
184 f
->fs_spec
= LU_COPY_STRING(in
->fs_spec
);
185 f
->fs_file
= LU_COPY_STRING(in
->fs_file
);
186 f
->fs_vfstype
= LU_COPY_STRING(in
->fs_vfstype
);
187 f
->fs_mntops
= LU_COPY_STRING(in
->fs_mntops
);
188 f
->fs_type
= LU_COPY_STRING(in
->fs_type
);
190 f
->fs_freq
= in
->fs_freq
;
191 f
->fs_passno
= in
->fs_passno
;
197 recycle_fstab(struct lu_thread_info
*tdata
, struct fstab
*in
)
201 if (tdata
== NULL
) return;
202 f
= (struct fstab
*)tdata
->lu_entry
;
207 tdata
->lu_entry
= NULL
;
210 if (tdata
->lu_entry
== NULL
)
212 tdata
->lu_entry
= in
;
218 f
->fs_spec
= in
->fs_spec
;
219 f
->fs_file
= in
->fs_file
;
220 f
->fs_vfstype
= in
->fs_vfstype
;
221 f
->fs_mntops
= in
->fs_mntops
;
222 f
->fs_type
= in
->fs_type
;
223 f
->fs_freq
= in
->fs_freq
;
224 f
->fs_passno
= in
->fs_passno
;
229 static struct fstab
*
230 lu_getfsspec(const char *name
)
234 char namebuf
[_LU_MAXLUSTRLEN
+ BYTES_PER_XDR_UNIT
];
237 static int proc
= -1;
243 if (_lookup_link(_lu_port
, "getfsbyname", &proc
) != KERN_SUCCESS
)
249 xdrmem_create(&outxdr
, namebuf
, sizeof(namebuf
), XDR_ENCODE
);
250 if (!xdr__lu_string(&outxdr
, (_lu_string
*)&name
))
252 xdr_destroy(&outxdr
);
259 if (_lookup_all(_lu_port
, proc
, (unit
*)namebuf
, xdr_getpos(&outxdr
) / BYTES_PER_XDR_UNIT
, &lookup_buf
, &datalen
)
262 xdr_destroy(&outxdr
);
266 xdr_destroy(&outxdr
);
268 datalen
*= BYTES_PER_XDR_UNIT
;
269 if ((lookup_buf
== NULL
) || (datalen
== 0)) return NULL
;
271 xdrmem_create(&inxdr
, lookup_buf
, datalen
, XDR_DECODE
);
274 if (!xdr_int(&inxdr
, &count
))
277 vm_deallocate(mach_task_self(), (vm_address_t
)lookup_buf
, datalen
);
284 vm_deallocate(mach_task_self(), (vm_address_t
)lookup_buf
, datalen
);
288 f
= extract_fstab(&inxdr
);
290 vm_deallocate(mach_task_self(), (vm_address_t
)lookup_buf
, datalen
);
298 struct lu_thread_info
*tdata
;
300 tdata
= _lu_data_create_key(_lu_data_key_fstab
, free_lu_thread_info_fstab
);
301 _lu_data_free_vm_xdr(tdata
);
311 static struct fstab
*
314 static int proc
= -1;
315 struct lu_thread_info
*tdata
;
318 tdata
= _lu_data_create_key(_lu_data_key_fstab
, free_lu_thread_info_fstab
);
321 tdata
= (struct lu_thread_info
*)calloc(1, sizeof(struct lu_thread_info
));
322 _lu_data_set_key(_lu_data_key_fstab
, tdata
);
325 if (tdata
->lu_vm
== NULL
)
329 if (_lookup_link(_lu_port
, "getfsent", &proc
) != KERN_SUCCESS
)
336 if (_lookup_all(_lu_port
, proc
, NULL
, 0, &(tdata
->lu_vm
), &(tdata
->lu_vm_length
)) != KERN_SUCCESS
)
342 /* mig stubs measure size in words (4 bytes) */
343 tdata
->lu_vm_length
*= 4;
345 if (tdata
->lu_xdr
!= NULL
)
347 xdr_destroy(tdata
->lu_xdr
);
350 tdata
->lu_xdr
= (XDR
*)calloc(1, sizeof(XDR
));
352 xdrmem_create(tdata
->lu_xdr
, tdata
->lu_vm
, tdata
->lu_vm_length
, XDR_DECODE
);
353 if (!xdr_int(tdata
->lu_xdr
, &tdata
->lu_vm_cursor
))
360 if (tdata
->lu_vm_cursor
== 0)
366 f
= extract_fstab(tdata
->lu_xdr
);
373 tdata
->lu_vm_cursor
--;
378 static struct fstab
*
379 lu_getfsfile(const char *name
)
383 if (name
== NULL
) return (struct fstab
*)NULL
;
386 for (fs
= lu_getfsent(); fs
!= NULL
; fs
= lu_getfsent())
387 if (!strcmp(fs
->fs_file
, name
)) return fs
;
390 return (struct fstab
*)NULL
;
393 static struct fstab
*
394 getfs(const char *spec
, const char *file
, int source
)
396 struct fstab
*res
= NULL
;
397 struct lu_thread_info
*tdata
;
399 tdata
= _lu_data_create_key(_lu_data_key_fstab
, free_lu_thread_info_fstab
);
402 tdata
= (struct lu_thread_info
*)calloc(1, sizeof(struct lu_thread_info
));
403 _lu_data_set_key(_lu_data_key_fstab
, tdata
);
411 res
= lu_getfsspec(spec
);
414 res
= lu_getfsfile(file
);
424 pthread_mutex_lock(&_fstab_lock
);
428 res
= copy_fstab(_old_getfsspec(spec
));
431 res
= copy_fstab(_old_getfsfile(file
));
434 res
= copy_fstab(_old_getfsent());
438 pthread_mutex_unlock(&_fstab_lock
);
441 recycle_fstab(tdata
, res
);
442 return (struct fstab
*)tdata
->lu_entry
;
447 getfsbyname(const char *name
)
449 return getfs(name
, NULL
, FS_GET_SPEC
);
453 getfsspec(const char *name
)
455 return getfs(name
, NULL
, FS_GET_SPEC
);
459 getfsfile(const char *name
)
461 return getfs(NULL
, name
, FS_GET_FILE
);
467 return getfs(NULL
, NULL
, FS_GET_ENT
);
473 if (_lu_running()) return (lu_setfsent());
474 return (_old_setfsent());
480 if (_lu_running()) lu_endfsent();
481 else _old_endfsent();