2 * Copyright (c) 1999 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@
27 #include <mach/mach.h>
29 #include "_lu_types.h"
33 #define LONG_STRING_LENGTH 8192
34 #define _LU_MAXLUSTRLEN 256
37 lookupd_process_dictionary(XDR
*inxdr
)
39 int i
, nkeys
, j
, nvals
;
43 if (!xdr_int(inxdr
, &nkeys
)) return NULL
;
45 l
= (ni_proplist
*)malloc(sizeof(ni_proplist
));
48 l
->ni_proplist_len
= nkeys
;
49 l
->ni_proplist_val
= NULL
;
52 i
= nkeys
* sizeof(ni_property
);
53 l
->ni_proplist_val
= (ni_property
*)malloc(i
);
54 memset(l
->ni_proplist_val
, 0, i
);
57 for (i
= 0; i
< nkeys
; i
++)
61 if (!xdr_string(inxdr
, &key
, LONG_STRING_LENGTH
))
67 l
->ni_proplist_val
[i
].nip_name
= key
;
69 if (!xdr_int(inxdr
, &nvals
))
75 l
->ni_proplist_val
[i
].nip_val
.ni_namelist_len
= nvals
;
78 j
= nvals
* sizeof(ni_name
);
79 l
->ni_proplist_val
[i
].nip_val
.ni_namelist_val
= (ni_name
*)malloc(j
);
80 memset(l
->ni_proplist_val
[i
].nip_val
.ni_namelist_val
, 0 , j
);
83 for (j
= 0; j
< nvals
; j
++)
86 if (!xdr_string(inxdr
, &val
, LONG_STRING_LENGTH
))
92 l
->ni_proplist_val
[i
].nip_val
.ni_namelist_val
[j
] = val
;
100 lookupd_query(ni_proplist
*l
, ni_proplist
***out
)
107 char databuf
[_LU_MAXLUSTRLEN
* BYTES_PER_XDR_UNIT
];
109 kern_return_t status
;
112 if (l
== NULL
) return 0;
113 if (out
== NULL
) return 0;
115 if (_lu_port
== NULL
) return 0;
117 status
= _lookup_link(_lu_port
, "query", &proc
);
118 if (status
!= KERN_SUCCESS
) return 0;
120 xdrmem_create(&outxdr
, databuf
, sizeof(databuf
), XDR_ENCODE
);
122 na
= l
->ni_proplist_len
;
124 /* Encode attribute count */
125 if (!xdr_int(&outxdr
, &na
))
127 xdr_destroy(&outxdr
);
131 for (i
= 0; i
< l
->ni_proplist_len
; i
++)
133 p
= &(l
->ni_proplist_val
[i
]);
134 if (!xdr_string(&outxdr
, &(p
->nip_name
), _LU_MAXLUSTRLEN
))
136 xdr_destroy(&outxdr
);
140 if (!xdr_int(&outxdr
, &(p
->nip_val
.ni_namelist_len
)))
142 xdr_destroy(&outxdr
);
146 for (j
= 0; j
< p
->nip_val
.ni_namelist_len
; j
++)
148 if (!xdr_string(&outxdr
, &(p
->nip_val
.ni_namelist_val
[j
]), _LU_MAXLUSTRLEN
))
150 xdr_destroy(&outxdr
);
159 n
= xdr_getpos(&outxdr
) / BYTES_PER_XDR_UNIT
;
160 status
= _lookup_all(_lu_port
, proc
, (unit
*)databuf
, n
, &listbuf
, &datalen
);
161 if (status
!= KERN_SUCCESS
)
163 xdr_destroy(&outxdr
);
167 xdr_destroy(&outxdr
);
170 /* NOTDEF because OOL buffers are counted in bytes with untyped IPC */
171 datalen
*= BYTES_PER_XDR_UNIT
;
174 xdrmem_create(&inxdr
, listbuf
, datalen
, XDR_DECODE
);
176 if (!xdr_int(&inxdr
, &n
))
188 *out
= (ni_proplist
**)malloc(n
* sizeof(ni_proplist
*));
190 for (i
= 0; i
< n
; i
++)
192 (*out
)[i
] = lookupd_process_dictionary(&inxdr
);
197 vm_deallocate(mach_task_self(), (vm_address_t
)listbuf
, datalen
);
203 lookupd_make_query(char *cat
, char *fmt
, ...)
211 if (fmt
== NULL
) return NULL
;
212 if (fmt
[0] != 'k') return NULL
;
214 l
= (ni_proplist
*)malloc(sizeof(ni_proplist
));
222 l
->ni_proplist_val
= (ni_property
*)malloc(sizeof(ni_property
));
223 p
= &(l
->ni_proplist_val
[0]);
224 arg
= "_lookup_category";
225 p
->nip_name
= strdup(arg
);
226 p
->nip_val
.ni_namelist_len
= 1;
227 p
->nip_val
.ni_namelist_val
= (ni_name
*)malloc(sizeof(ni_name
));
228 p
->nip_val
.ni_namelist_val
[0] = strdup(cat
);
230 l
->ni_proplist_len
++;
235 for (f
= fmt
; *f
!= NULL
; f
++)
237 arg
= va_arg(ap
, char *);
240 l
->ni_proplist_val
= (ni_property
*)realloc(l
->ni_proplist_val
, (l
->ni_proplist_len
+ 1) * sizeof(ni_property
));
242 p
= &(l
->ni_proplist_val
[l
->ni_proplist_len
]);
243 p
->nip_name
= strdup(arg
);
244 p
->nip_val
.ni_namelist_len
= 0;
245 p
->nip_val
.ni_namelist_val
= NULL
;
247 l
->ni_proplist_len
++;
252 p
= &(l
->ni_proplist_val
[x
]);
253 if (p
->nip_val
.ni_namelist_len
== 0)
255 p
->nip_val
.ni_namelist_val
= (ni_name
*)malloc(sizeof(ni_name
));
259 p
->nip_val
.ni_namelist_val
= (ni_name
*)realloc(p
->nip_val
.ni_namelist_val
, (p
->nip_val
.ni_namelist_len
+ 1) * sizeof(ni_name
));
261 p
->nip_val
.ni_namelist_val
[p
->nip_val
.ni_namelist_len
] = strdup(arg
);
262 p
->nip_val
.ni_namelist_len
++;
271 ni_property_merge(ni_property
*a
, ni_property
*b
)
275 if (a
== NULL
) return;
276 if (b
== NULL
) return;
278 for (j
= 0; j
< b
->nip_val
.ni_namelist_len
; j
++)
281 for (i
= 0; i
< (a
->nip_val
.ni_namelist_len
) && (addme
== 1); i
++)
283 if (!strcmp(a
->nip_val
.ni_namelist_val
[i
], b
->nip_val
.ni_namelist_val
[j
])) addme
= 0;
288 a
->nip_val
.ni_namelist_val
= (ni_name
*)realloc(a
->nip_val
.ni_namelist_val
, (a
->nip_val
.ni_namelist_len
+ 1) * sizeof(ni_name
));
289 a
->nip_val
.ni_namelist_val
[a
->nip_val
.ni_namelist_len
] = strdup(b
->nip_val
.ni_namelist_val
[j
]);
290 a
->nip_val
.ni_namelist_len
++;
296 ni_proplist_merge(ni_proplist
*a
, ni_proplist
*b
)
301 if (a
== NULL
) return;
302 if (b
== NULL
) return;
304 for (wb
= 0; wb
< b
->ni_proplist_len
; wb
++)
307 for (wa
= 0; (wa
< a
->ni_proplist_len
) && (addme
== 1) ; wa
++)
309 if (!strcmp(a
->ni_proplist_val
[wa
].nip_name
, b
->ni_proplist_val
[wb
].nip_name
)) addme
= 0;
313 a
->ni_proplist_val
= (ni_property
*)realloc(a
->ni_proplist_val
, (a
->ni_proplist_len
+ 1) * sizeof(ni_property
));
314 a
->ni_proplist_val
[a
->ni_proplist_len
].nip_name
= strdup(b
->ni_proplist_val
[wb
].nip_name
);
315 a
->ni_proplist_val
[a
->ni_proplist_len
].nip_val
.ni_namelist_len
= 0;
316 a
->ni_proplist_val
[a
->ni_proplist_len
].nip_val
.ni_namelist_val
= NULL
;
317 a
->ni_proplist_len
++;
321 for (wb
= 0; wb
< b
->ni_proplist_len
; wb
++)
323 for (wa
= 0; wa
< a
->ni_proplist_len
; wa
++)
325 if (!strcmp(a
->ni_proplist_val
[wa
].nip_name
, b
->ni_proplist_val
[wb
].nip_name
))
327 ni_property_merge(&(a
->ni_proplist_val
[wa
]), &(b
->ni_proplist_val
[wb
]));