2 * Copyright (c) 1999-2006 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>
36 #include "lu_overrides.h"
38 #define ENTRY_SIZE sizeof(struct aliasent)
39 #define ENTRY_KEY _li_data_key_alias
41 static pthread_mutex_t _alias_lock
= PTHREAD_MUTEX_INITIALIZER
;
43 static struct aliasent
*
44 copy_alias(struct aliasent
*in
)
46 if (in
== NULL
) return NULL
;
48 return (struct aliasent
*)LI_ils_create("s4*4", in
->alias_name
, in
->alias_members_len
, in
->alias_members
, in
->alias_local
);
52 * Extract the next alias entry from a kvarray.
55 extract_alias(kvarray_t
*in
)
58 uint32_t d
, k
, kcount
;
61 if (in
== NULL
) return NULL
;
66 if (d
>= in
->count
) return NULL
;
69 memset(&tmp
, 0, ENTRY_SIZE
);
71 kcount
= in
->dict
[d
].kcount
;
73 for (k
= 0; k
< kcount
; k
++)
75 if (!strcmp(in
->dict
[d
].key
[k
], "alias_name"))
77 if (tmp
.alias_name
!= NULL
) continue;
78 if (in
->dict
[d
].vcount
[k
] == 0) continue;
80 tmp
.alias_name
= (char *)in
->dict
[d
].val
[k
][0];
82 else if (!strcmp(in
->dict
[d
].key
[k
], "alias_members"))
84 if (tmp
.alias_members
!= NULL
) continue;
85 if (in
->dict
[d
].vcount
[k
] == 0) continue;
87 tmp
.alias_members_len
= in
->dict
[d
].vcount
[k
];
88 tmp
.alias_members
= (char **)in
->dict
[d
].val
[k
];
90 else if (!strcmp(in
->dict
[d
].key
[k
], "alias_local"))
92 if (in
->dict
[d
].vcount
[k
] == 0) continue;
93 tmp
.alias_local
= atoi(in
->dict
[d
].val
[k
][0]);
97 if (tmp
.alias_name
== NULL
) tmp
.alias_name
= "";
98 if (tmp
.alias_members
== NULL
) tmp
.alias_members
= empty
;
100 return copy_alias(&tmp
);
104 * Send a query to the system information daemon.
106 static struct aliasent
*
107 ds_alias_getbyname(const char *name
)
109 static int proc
= -1;
111 return (struct aliasent
*)LI_getone("alias_getbyname", &proc
, extract_alias
, "name", name
);
115 * Clean up / initialize / reinitialize the kvarray used to hold a list of all rpc entries.
118 ds_alias_endent(void)
120 LI_data_free_kvarray(LI_data_find_key(ENTRY_KEY
));
124 ds_alias_setent(void)
130 * Get an entry from the getrpcent kvarray.
131 * Calls the system information daemon if the list doesn't exist (first call),
132 * or extracts the next entry if the list has been fetched.
134 static struct aliasent
*
135 ds_alias_getent(void)
137 static int proc
= -1;
139 return (struct aliasent
*)LI_getent("alias_getent", &proc
, extract_alias
, ENTRY_KEY
, ENTRY_SIZE
);
143 alias_getbyname(const char *name
)
145 struct aliasent
*res
= NULL
;
146 struct li_thread_info
*tdata
;
148 tdata
= LI_data_create_key(ENTRY_KEY
, ENTRY_SIZE
);
149 if (tdata
== NULL
) return NULL
;
153 res
= ds_alias_getbyname(name
);
157 pthread_mutex_lock(&_alias_lock
);
158 res
= copy_alias(_old_alias_getbyname(name
));
159 pthread_mutex_unlock(&_alias_lock
);
162 LI_data_recycle(tdata
, res
, ENTRY_SIZE
);
163 return (struct aliasent
*)tdata
->li_entry
;
170 struct aliasent
*res
= NULL
;
171 struct li_thread_info
*tdata
;
173 tdata
= LI_data_create_key(ENTRY_KEY
, ENTRY_SIZE
);
174 if (tdata
== NULL
) return NULL
;
178 res
= ds_alias_getent();
182 pthread_mutex_lock(&_alias_lock
);
183 res
= copy_alias(_old_alias_getent());
184 pthread_mutex_unlock(&_alias_lock
);
187 LI_data_recycle(tdata
, res
, ENTRY_SIZE
);
188 return (struct aliasent
*)tdata
->li_entry
;
195 if (_ds_running()) ds_alias_setent();
196 else _old_alias_setent();
202 if (_ds_running()) ds_alias_endent();
203 else _old_alias_endent();