2 * Copyright (c) 1999-2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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
22 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (C) 1989 by NeXT, Inc.
30 #include <mach/mach.h>
33 #include <rpc/types.h>
39 #include "_lu_types.h"
41 #include "lu_overrides.h"
43 static pthread_mutex_t _fstab_lock
= PTHREAD_MUTEX_INITIALIZER
;
50 free_fstab_data(struct fstab
*f
)
52 if (f
== NULL
) return;
54 if (f
->fs_spec
!= NULL
) free(f
->fs_spec
);
55 if (f
->fs_file
!= NULL
) free(f
->fs_file
);
56 if (f
->fs_vfstype
!= NULL
) free(f
->fs_vfstype
);
57 if (f
->fs_mntops
!= NULL
) free(f
->fs_mntops
);
58 if (f
->fs_type
!= NULL
) free(f
->fs_type
);
62 free_fstab(struct fstab
*f
)
64 if (f
== NULL
) return;
70 free_lu_thread_info_fstab(void *x
)
72 struct lu_thread_info
*tdata
;
74 if (x
== NULL
) return;
76 tdata
= (struct lu_thread_info
*)x
;
78 if (tdata
->lu_entry
!= NULL
)
80 free_fstab((struct fstab
*)tdata
->lu_entry
);
81 tdata
->lu_entry
= NULL
;
84 _lu_data_free_vm_xdr(tdata
);
90 extract_fstab(XDR
*xdr
)
92 int i
, j
, nkeys
, nvals
, status
;
96 if (xdr
== NULL
) return NULL
;
98 if (!xdr_int(xdr
, &nkeys
)) return NULL
;
100 f
= (struct fstab
*)calloc(1, sizeof(struct fstab
));
102 for (i
= 0; i
< nkeys
; i
++)
108 status
= _lu_xdr_attribute(xdr
, &key
, &vals
, &nvals
);
123 if ((f
->fs_spec
== NULL
) && (!strcmp("name", key
)))
125 f
->fs_spec
= vals
[0];
128 else if ((f
->fs_file
== NULL
) && (!strcmp("dir", key
)))
130 f
->fs_file
= vals
[0];
133 else if ((f
->fs_vfstype
== NULL
) && (!strcmp("vfstype", key
)))
135 f
->fs_vfstype
= vals
[0];
138 else if ((f
->fs_mntops
== NULL
) && (!strcmp("opts", key
)))
140 f
->fs_mntops
= vals
[0];
143 else if ((f
->fs_type
== NULL
) && (!strcmp("type", key
)))
145 f
->fs_type
= vals
[0];
148 else if (!strcmp("freq", key
))
150 f
->fs_freq
= atoi(vals
[0]);
152 else if (!strcmp("passno", key
))
154 f
->fs_passno
= atoi(vals
[0]);
160 for (; j
< nvals
; j
++) free(vals
[j
]);
165 if (f
->fs_spec
== NULL
) f
->fs_spec
= strdup("");
166 if (f
->fs_file
== NULL
) f
->fs_file
= strdup("");
167 if (f
->fs_vfstype
== NULL
) f
->fs_vfstype
= strdup("");
168 if (f
->fs_mntops
== NULL
) f
->fs_mntops
= strdup("");
169 if (f
->fs_type
== NULL
) f
->fs_type
= strdup("");
174 static struct fstab
*
175 copy_fstab(struct fstab
*in
)
179 if (in
== NULL
) return NULL
;
181 f
= (struct fstab
*)calloc(1, sizeof(struct fstab
));
183 f
->fs_spec
= LU_COPY_STRING(in
->fs_spec
);
184 f
->fs_file
= LU_COPY_STRING(in
->fs_file
);
185 f
->fs_vfstype
= LU_COPY_STRING(in
->fs_vfstype
);
186 f
->fs_mntops
= LU_COPY_STRING(in
->fs_mntops
);
187 f
->fs_type
= LU_COPY_STRING(in
->fs_type
);
189 f
->fs_freq
= in
->fs_freq
;
190 f
->fs_passno
= in
->fs_passno
;
196 recycle_fstab(struct lu_thread_info
*tdata
, struct fstab
*in
)
200 if (tdata
== NULL
) return;
201 f
= (struct fstab
*)tdata
->lu_entry
;
206 tdata
->lu_entry
= NULL
;
209 if (tdata
->lu_entry
== NULL
)
211 tdata
->lu_entry
= in
;
217 f
->fs_spec
= in
->fs_spec
;
218 f
->fs_file
= in
->fs_file
;
219 f
->fs_vfstype
= in
->fs_vfstype
;
220 f
->fs_mntops
= in
->fs_mntops
;
221 f
->fs_type
= in
->fs_type
;
222 f
->fs_freq
= in
->fs_freq
;
223 f
->fs_passno
= in
->fs_passno
;
228 static struct fstab
*
229 lu_getfsspec(const char *name
)
233 char namebuf
[_LU_MAXLUSTRLEN
+ BYTES_PER_XDR_UNIT
];
236 static int proc
= -1;
242 if (_lookup_link(_lu_port
, "getfsbyname", &proc
) != KERN_SUCCESS
)
248 xdrmem_create(&outxdr
, namebuf
, sizeof(namebuf
), XDR_ENCODE
);
249 if (!xdr__lu_string(&outxdr
, (_lu_string
*)&name
))
251 xdr_destroy(&outxdr
);
258 if (_lookup_all(_lu_port
, proc
, (unit
*)namebuf
, xdr_getpos(&outxdr
) / BYTES_PER_XDR_UNIT
, &lookup_buf
, &datalen
)
261 xdr_destroy(&outxdr
);
265 xdr_destroy(&outxdr
);
267 datalen
*= BYTES_PER_XDR_UNIT
;
268 if ((lookup_buf
== NULL
) || (datalen
== 0)) return NULL
;
270 xdrmem_create(&inxdr
, lookup_buf
, datalen
, XDR_DECODE
);
273 if (!xdr_int(&inxdr
, &count
))
276 vm_deallocate(mach_task_self(), (vm_address_t
)lookup_buf
, datalen
);
283 vm_deallocate(mach_task_self(), (vm_address_t
)lookup_buf
, datalen
);
287 f
= extract_fstab(&inxdr
);
289 vm_deallocate(mach_task_self(), (vm_address_t
)lookup_buf
, datalen
);
297 struct lu_thread_info
*tdata
;
299 tdata
= _lu_data_create_key(_lu_data_key_fstab
, free_lu_thread_info_fstab
);
300 _lu_data_free_vm_xdr(tdata
);
310 static struct fstab
*
313 static int proc
= -1;
314 struct lu_thread_info
*tdata
;
317 tdata
= _lu_data_create_key(_lu_data_key_fstab
, free_lu_thread_info_fstab
);
320 tdata
= (struct lu_thread_info
*)calloc(1, sizeof(struct lu_thread_info
));
321 _lu_data_set_key(_lu_data_key_fstab
, tdata
);
324 if (tdata
->lu_vm
== NULL
)
328 if (_lookup_link(_lu_port
, "getfsent", &proc
) != KERN_SUCCESS
)
335 if (_lookup_all(_lu_port
, proc
, NULL
, 0, &(tdata
->lu_vm
), &(tdata
->lu_vm_length
)) != KERN_SUCCESS
)
341 /* mig stubs measure size in words (4 bytes) */
342 tdata
->lu_vm_length
*= 4;
344 if (tdata
->lu_xdr
!= NULL
)
346 xdr_destroy(tdata
->lu_xdr
);
349 tdata
->lu_xdr
= (XDR
*)calloc(1, sizeof(XDR
));
351 xdrmem_create(tdata
->lu_xdr
, tdata
->lu_vm
, tdata
->lu_vm_length
, XDR_DECODE
);
352 if (!xdr_int(tdata
->lu_xdr
, &tdata
->lu_vm_cursor
))
359 if (tdata
->lu_vm_cursor
== 0)
365 f
= extract_fstab(tdata
->lu_xdr
);
372 tdata
->lu_vm_cursor
--;
377 static struct fstab
*
378 lu_getfsfile(const char *name
)
382 if (name
== NULL
) return (struct fstab
*)NULL
;
385 for (fs
= lu_getfsent(); fs
!= NULL
; fs
= lu_getfsent())
386 if (!strcmp(fs
->fs_file
, name
)) return fs
;
389 return (struct fstab
*)NULL
;
392 static struct fstab
*
393 getfs(const char *spec
, const char *file
, int source
)
395 struct fstab
*res
= NULL
;
396 struct lu_thread_info
*tdata
;
398 tdata
= _lu_data_create_key(_lu_data_key_fstab
, free_lu_thread_info_fstab
);
401 tdata
= (struct lu_thread_info
*)calloc(1, sizeof(struct lu_thread_info
));
402 _lu_data_set_key(_lu_data_key_fstab
, tdata
);
410 res
= lu_getfsspec(spec
);
413 res
= lu_getfsfile(file
);
423 pthread_mutex_lock(&_fstab_lock
);
427 res
= copy_fstab(_old_getfsspec(spec
));
430 res
= copy_fstab(_old_getfsfile(file
));
433 res
= copy_fstab(_old_getfsent());
437 pthread_mutex_unlock(&_fstab_lock
);
440 recycle_fstab(tdata
, res
);
441 return (struct fstab
*)tdata
->lu_entry
;
446 getfsbyname(const char *name
)
448 return getfs(name
, NULL
, FS_GET_SPEC
);
452 getfsspec(const char *name
)
454 return getfs(name
, NULL
, FS_GET_SPEC
);
458 getfsfile(const char *name
)
460 return getfs(NULL
, name
, FS_GET_FILE
);
466 return getfs(NULL
, NULL
, FS_GET_ENT
);
472 if (_lu_running()) return (lu_setfsent());
473 return (_old_setfsent());
479 if (_lu_running()) lu_endfsent();
480 else _old_endfsent();