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>
39 #include "_lu_types.h"
42 #include "lu_overrides.h"
44 static pthread_mutex_t _alias_lock
= PTHREAD_MUTEX_INITIALIZER
;
47 free_alias_data(struct aliasent
*a
)
51 if (a
== NULL
) return;
53 if (a
->alias_name
!= NULL
) free(a
->alias_name
);
54 for (i
= 0; i
< a
->alias_members_len
; i
++) free(a
->alias_members
[i
]);
55 if (a
->alias_members
!= NULL
) free(a
->alias_members
);
59 free_alias(struct aliasent
*a
)
61 if (a
== NULL
) return;
67 free_lu_thread_info_alias(void *x
)
69 struct lu_thread_info
*tdata
;
71 if (x
== NULL
) return;
73 tdata
= (struct lu_thread_info
*)x
;
75 if (tdata
->lu_entry
!= NULL
)
77 free_alias((struct aliasent
*)tdata
->lu_entry
);
78 tdata
->lu_entry
= NULL
;
81 _lu_data_free_vm_xdr(tdata
);
86 static struct aliasent
*
87 extract_alias(XDR
*xdr
)
89 int i
, j
, nkeys
, nvals
, status
;
93 if (xdr
== NULL
) return NULL
;
95 if (!xdr_int(xdr
, &nkeys
)) return NULL
;
97 a
= (struct aliasent
*)calloc(1, sizeof(struct aliasent
));
99 for (i
= 0; i
< nkeys
; i
++)
105 status
= _lu_xdr_attribute(xdr
, &key
, &vals
, &nvals
);
120 if ((a
->alias_name
== NULL
) && (!strcmp("name", key
)))
122 a
->alias_name
= vals
[0];
125 else if (!strcmp("alias_local", key
))
127 a
->alias_local
= atoi(vals
[0]);
129 else if ((a
->alias_members
== NULL
) && (!strcmp("members", key
)))
131 a
->alias_members_len
= nvals
;
132 a
->alias_members
= vals
;
140 for (; j
< nvals
; j
++) free(vals
[j
]);
145 if (a
->alias_name
== NULL
) a
->alias_name
= strdup("");
146 if (a
->alias_members
== NULL
) a
->alias_members
= (char **)calloc(1, sizeof(char *));
151 static struct aliasent
*
152 copy_alias(struct aliasent
*in
)
157 if (in
== NULL
) return NULL
;
159 a
= (struct aliasent
*)calloc(1, sizeof(struct aliasent
));
161 a
->alias_name
= LU_COPY_STRING(in
->alias_name
);
163 a
->alias_members_len
= in
->alias_members_len
;
165 if (a
->alias_members_len
== 0)
167 a
->alias_members
= (char **)calloc(1, sizeof(char *));
171 a
->alias_members
= (char **)calloc(a
->alias_members_len
, sizeof(char *));
174 for (i
= 0; i
< a
->alias_members_len
; i
++)
176 a
->alias_members
[i
] = strdup(in
->alias_members
[i
]);
179 a
->alias_local
= in
->alias_local
;
185 recycle_alias(struct lu_thread_info
*tdata
, struct aliasent
*in
)
189 if (tdata
== NULL
) return;
190 a
= (struct aliasent
*)tdata
->lu_entry
;
195 tdata
->lu_entry
= NULL
;
198 if (tdata
->lu_entry
== NULL
)
200 tdata
->lu_entry
= in
;
206 a
->alias_name
= in
->alias_name
;
207 a
->alias_members_len
= in
->alias_members_len
;
208 a
->alias_members
= in
->alias_members
;
209 a
->alias_local
= in
->alias_local
;
214 static struct aliasent
*
215 lu_alias_getbyname(const char *name
)
218 unsigned int datalen
;
219 char namebuf
[_LU_MAXLUSTRLEN
+ BYTES_PER_XDR_UNIT
];
223 static int proc
= -1;
228 if (_lookup_link(_lu_port
, "alias_getbyname", &proc
) != KERN_SUCCESS
)
234 xdrmem_create(&outxdr
, namebuf
, sizeof(namebuf
), XDR_ENCODE
);
235 if (!xdr__lu_string(&outxdr
, (_lu_string
*)&name
))
237 xdr_destroy(&outxdr
);
244 if (_lookup_all(_lu_port
, proc
, (unit
*)namebuf
,
245 xdr_getpos(&outxdr
) / BYTES_PER_XDR_UNIT
, &lookup_buf
, &datalen
)
248 xdr_destroy(&outxdr
);
252 xdr_destroy(&outxdr
);
254 datalen
*= BYTES_PER_XDR_UNIT
;
255 if ((lookup_buf
== NULL
) || (datalen
== 0)) return NULL
;
257 xdrmem_create(&inxdr
, lookup_buf
, datalen
, XDR_DECODE
);
260 if (!xdr_int(&inxdr
, &count
))
263 vm_deallocate(mach_task_self(), (vm_address_t
)lookup_buf
, datalen
);
270 vm_deallocate(mach_task_self(), (vm_address_t
)lookup_buf
, datalen
);
274 a
= extract_alias(&inxdr
);
276 vm_deallocate(mach_task_self(), (vm_address_t
)lookup_buf
, datalen
);
282 lu_alias_endent(void)
284 struct lu_thread_info
*tdata
;
286 tdata
= _lu_data_create_key(_lu_data_key_alias
, free_lu_thread_info_alias
);
287 _lu_data_free_vm_xdr(tdata
);
291 lu_alias_setent(void)
296 static struct aliasent
*
297 lu_alias_getent(void)
299 static int proc
= -1;
300 struct lu_thread_info
*tdata
;
303 tdata
= _lu_data_create_key(_lu_data_key_alias
, free_lu_thread_info_alias
);
306 tdata
= (struct lu_thread_info
*)calloc(1, sizeof(struct lu_thread_info
));
307 _lu_data_set_key(_lu_data_key_alias
, tdata
);
310 if (tdata
->lu_vm
== NULL
)
314 if (_lookup_link(_lu_port
, "alias_getent", &proc
) != KERN_SUCCESS
)
321 if (_lookup_all(_lu_port
, proc
, NULL
, 0, &(tdata
->lu_vm
), &(tdata
->lu_vm_length
)) != KERN_SUCCESS
)
327 /* mig stubs measure size in words (4 bytes) */
328 tdata
->lu_vm_length
*= 4;
330 if (tdata
->lu_xdr
!= NULL
)
332 xdr_destroy(tdata
->lu_xdr
);
335 tdata
->lu_xdr
= (XDR
*)calloc(1, sizeof(XDR
));
337 xdrmem_create(tdata
->lu_xdr
, tdata
->lu_vm
, tdata
->lu_vm_length
, XDR_DECODE
);
338 if (!xdr_int(tdata
->lu_xdr
, &tdata
->lu_vm_cursor
))
345 if (tdata
->lu_vm_cursor
== 0)
352 a
= extract_alias(tdata
->lu_xdr
);
359 tdata
->lu_vm_cursor
--;
365 alias_getbyname(const char *name
)
367 struct aliasent
*res
= NULL
;
368 struct lu_thread_info
*tdata
;
370 tdata
= _lu_data_create_key(_lu_data_key_alias
, free_lu_thread_info_alias
);
373 tdata
= (struct lu_thread_info
*)calloc(1, sizeof(struct lu_thread_info
));
374 _lu_data_set_key(_lu_data_key_alias
, tdata
);
379 res
= lu_alias_getbyname(name
);
383 pthread_mutex_lock(&_alias_lock
);
384 res
= copy_alias(_old_alias_getbyname(name
));
385 pthread_mutex_unlock(&_alias_lock
);
388 recycle_alias(tdata
, res
);
389 return (struct aliasent
*)tdata
->lu_entry
;
396 struct aliasent
*res
= NULL
;
397 struct lu_thread_info
*tdata
;
399 tdata
= _lu_data_create_key(_lu_data_key_alias
, free_lu_thread_info_alias
);
402 tdata
= (struct lu_thread_info
*)calloc(1, sizeof(struct lu_thread_info
));
403 _lu_data_set_key(_lu_data_key_alias
, tdata
);
408 res
= lu_alias_getent();
412 pthread_mutex_lock(&_alias_lock
);
413 res
= copy_alias(_old_alias_getent());
414 pthread_mutex_unlock(&_alias_lock
);
417 recycle_alias(tdata
, res
);
418 return (struct aliasent
*)tdata
->lu_entry
;
425 if (_lu_running()) lu_alias_setent();
426 else _old_alias_setent();
432 if (_lu_running()) lu_alias_endent();
433 else _old_alias_endent();