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>
38 #include "_lu_types.h"
41 #include "lu_overrides.h"
43 static pthread_mutex_t _alias_lock
= PTHREAD_MUTEX_INITIALIZER
;
46 free_alias_data(struct aliasent
*a
)
50 if (a
== NULL
) return;
52 if (a
->alias_name
!= NULL
) free(a
->alias_name
);
53 for (i
= 0; i
< a
->alias_members_len
; i
++) free(a
->alias_members
[i
]);
54 if (a
->alias_members
!= NULL
) free(a
->alias_members
);
58 free_alias(struct aliasent
*a
)
60 if (a
== NULL
) return;
66 free_lu_thread_info_alias(void *x
)
68 struct lu_thread_info
*tdata
;
70 if (x
== NULL
) return;
72 tdata
= (struct lu_thread_info
*)x
;
74 if (tdata
->lu_entry
!= NULL
)
76 free_alias((struct aliasent
*)tdata
->lu_entry
);
77 tdata
->lu_entry
= NULL
;
80 _lu_data_free_vm_xdr(tdata
);
85 static struct aliasent
*
86 extract_alias(XDR
*xdr
)
88 int i
, j
, nkeys
, nvals
, status
;
92 if (xdr
== NULL
) return NULL
;
94 if (!xdr_int(xdr
, &nkeys
)) return NULL
;
96 a
= (struct aliasent
*)calloc(1, sizeof(struct aliasent
));
98 for (i
= 0; i
< nkeys
; i
++)
104 status
= _lu_xdr_attribute(xdr
, &key
, &vals
, &nvals
);
119 if ((a
->alias_name
== NULL
) && (!strcmp("name", key
)))
121 a
->alias_name
= vals
[0];
124 else if (!strcmp("alias_local", key
))
126 a
->alias_local
= atoi(vals
[0]);
128 else if ((a
->alias_members
== NULL
) && (!strcmp("members", key
)))
130 a
->alias_members_len
= nvals
;
131 a
->alias_members
= vals
;
139 for (; j
< nvals
; j
++) free(vals
[j
]);
144 if (a
->alias_name
== NULL
) a
->alias_name
= strdup("");
145 if (a
->alias_members
== NULL
) a
->alias_members
= (char **)calloc(1, sizeof(char *));
150 static struct aliasent
*
151 copy_alias(struct aliasent
*in
)
156 if (in
== NULL
) return NULL
;
158 a
= (struct aliasent
*)calloc(1, sizeof(struct aliasent
));
160 a
->alias_name
= LU_COPY_STRING(in
->alias_name
);
162 a
->alias_members_len
= in
->alias_members_len
;
164 if (a
->alias_members_len
== 0)
166 a
->alias_members
= (char **)calloc(1, sizeof(char *));
170 a
->alias_members
= (char **)calloc(a
->alias_members_len
, sizeof(char *));
173 for (i
= 0; i
< a
->alias_members_len
; i
++)
175 a
->alias_members
[i
] = strdup(in
->alias_members
[i
]);
178 a
->alias_local
= in
->alias_local
;
184 recycle_alias(struct lu_thread_info
*tdata
, struct aliasent
*in
)
188 if (tdata
== NULL
) return;
189 a
= (struct aliasent
*)tdata
->lu_entry
;
194 tdata
->lu_entry
= NULL
;
197 if (tdata
->lu_entry
== NULL
)
199 tdata
->lu_entry
= in
;
205 a
->alias_name
= in
->alias_name
;
206 a
->alias_members_len
= in
->alias_members_len
;
207 a
->alias_members
= in
->alias_members
;
208 a
->alias_local
= in
->alias_local
;
213 static struct aliasent
*
214 lu_alias_getbyname(const char *name
)
217 unsigned int datalen
;
218 char namebuf
[_LU_MAXLUSTRLEN
+ BYTES_PER_XDR_UNIT
];
222 static int proc
= -1;
227 if (_lookup_link(_lu_port
, "alias_getbyname", &proc
) != KERN_SUCCESS
)
233 xdrmem_create(&outxdr
, namebuf
, sizeof(namebuf
), XDR_ENCODE
);
234 if (!xdr__lu_string(&outxdr
, (_lu_string
*)&name
))
236 xdr_destroy(&outxdr
);
243 if (_lookup_all(_lu_port
, proc
, (unit
*)namebuf
,
244 xdr_getpos(&outxdr
) / BYTES_PER_XDR_UNIT
, &lookup_buf
, &datalen
)
247 xdr_destroy(&outxdr
);
251 xdr_destroy(&outxdr
);
253 datalen
*= BYTES_PER_XDR_UNIT
;
254 if ((lookup_buf
== NULL
) || (datalen
== 0)) return NULL
;
256 xdrmem_create(&inxdr
, lookup_buf
, datalen
, XDR_DECODE
);
259 if (!xdr_int(&inxdr
, &count
))
262 vm_deallocate(mach_task_self(), (vm_address_t
)lookup_buf
, datalen
);
269 vm_deallocate(mach_task_self(), (vm_address_t
)lookup_buf
, datalen
);
273 a
= extract_alias(&inxdr
);
275 vm_deallocate(mach_task_self(), (vm_address_t
)lookup_buf
, datalen
);
281 lu_alias_endent(void)
283 struct lu_thread_info
*tdata
;
285 tdata
= _lu_data_create_key(_lu_data_key_alias
, free_lu_thread_info_alias
);
286 _lu_data_free_vm_xdr(tdata
);
290 lu_alias_setent(void)
295 static struct aliasent
*
296 lu_alias_getent(void)
298 static int proc
= -1;
299 struct lu_thread_info
*tdata
;
302 tdata
= _lu_data_create_key(_lu_data_key_alias
, free_lu_thread_info_alias
);
305 tdata
= (struct lu_thread_info
*)calloc(1, sizeof(struct lu_thread_info
));
306 _lu_data_set_key(_lu_data_key_alias
, tdata
);
309 if (tdata
->lu_vm
== NULL
)
313 if (_lookup_link(_lu_port
, "alias_getent", &proc
) != KERN_SUCCESS
)
320 if (_lookup_all(_lu_port
, proc
, NULL
, 0, &(tdata
->lu_vm
), &(tdata
->lu_vm_length
)) != KERN_SUCCESS
)
326 /* mig stubs measure size in words (4 bytes) */
327 tdata
->lu_vm_length
*= 4;
329 if (tdata
->lu_xdr
!= NULL
)
331 xdr_destroy(tdata
->lu_xdr
);
334 tdata
->lu_xdr
= (XDR
*)calloc(1, sizeof(XDR
));
336 xdrmem_create(tdata
->lu_xdr
, tdata
->lu_vm
, tdata
->lu_vm_length
, XDR_DECODE
);
337 if (!xdr_int(tdata
->lu_xdr
, &tdata
->lu_vm_cursor
))
344 if (tdata
->lu_vm_cursor
== 0)
351 a
= extract_alias(tdata
->lu_xdr
);
358 tdata
->lu_vm_cursor
--;
364 alias_getbyname(const char *name
)
366 struct aliasent
*res
= NULL
;
367 struct lu_thread_info
*tdata
;
369 tdata
= _lu_data_create_key(_lu_data_key_alias
, free_lu_thread_info_alias
);
372 tdata
= (struct lu_thread_info
*)calloc(1, sizeof(struct lu_thread_info
));
373 _lu_data_set_key(_lu_data_key_alias
, tdata
);
378 res
= lu_alias_getbyname(name
);
382 pthread_mutex_lock(&_alias_lock
);
383 res
= copy_alias(_old_alias_getbyname(name
));
384 pthread_mutex_unlock(&_alias_lock
);
387 recycle_alias(tdata
, res
);
388 return (struct aliasent
*)tdata
->lu_entry
;
395 struct aliasent
*res
= NULL
;
396 struct lu_thread_info
*tdata
;
398 tdata
= _lu_data_create_key(_lu_data_key_alias
, free_lu_thread_info_alias
);
401 tdata
= (struct lu_thread_info
*)calloc(1, sizeof(struct lu_thread_info
));
402 _lu_data_set_key(_lu_data_key_alias
, tdata
);
407 res
= lu_alias_getent();
411 pthread_mutex_lock(&_alias_lock
);
412 res
= copy_alias(_old_alias_getent());
413 pthread_mutex_unlock(&_alias_lock
);
416 recycle_alias(tdata
, res
);
417 return (struct aliasent
*)tdata
->lu_entry
;
424 if (_lu_running()) lu_alias_setent();
425 else _old_alias_setent();
431 if (_lu_running()) lu_alias_endent();
432 else _old_alias_endent();