xref: /titanic_44/usr/src/cmd/pools/poold/libjpool/jpool.c (revision 62a24de03df1f2399ceda704cb3874dabc98bbbd)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <errno.h>
30 #include <jni.h>
31 #include <pool.h>
32 #include <stdlib.h>
33 #include <string.h>
34 
35 #include <sys/time.h>
36 
37 #include "jpool.h"
38 
39 struct pool_callback {
40 	jobject	pc_user;
41 	jobject	pc_handler;
42 	jobject	pc_elem;
43 	JNIEnv	*pc_env;
44 };
45 
46 static void throwException(JNIEnv *, const char *, const char *);
47 static void throw_pe(JNIEnv *);
48 static jobject makeUnsignedInt64(JNIEnv *, uint64_t);
49 static int pool_property_walker(pool_conf_t *, pool_elem_t *p, const char *,
50     pool_value_t *, void *);
51 static jobject copyArray(JNIEnv *, void **);
52 
53 /*
54  * Cached class, method, and field IDs.
55  */
56 static jclass ui64class;
57 static jmethodID ui64cons_mid;
58 
59 /*
60  * Throw an exception of the specified class with the specified message.
61  */
62 void
63 throwException(JNIEnv *env, const char *class, const char *msg)
64 {
65 	jclass clazz;
66 
67 	clazz = (*env)->FindClass(env, class);
68 
69 	(*env)->ThrowNew(env, clazz, msg);
70 }
71 
72 /*
73  * Throw a PoolsException.
74  */
75 void
76 throw_pe(JNIEnv *jenv)
77 {
78 	jclass clazz;
79 	jmethodID mid;
80 	jthrowable throwObj;
81 
82 	clazz = (*jenv)->FindClass(jenv,
83 	    "com/sun/solaris/service/pools/PoolsException");
84 	mid = (*jenv)->GetMethodID(jenv, clazz, "<init>", "()V");
85 	throwObj = (*jenv)->NewObject(jenv, clazz, mid);
86 	(*jenv)->Throw(jenv, throwObj);
87 }
88 
89 /*
90  * Return an instance of an UnsignedInt64 class which encapsulates the
91  * supplied value.
92  */
93 jobject
94 makeUnsignedInt64(JNIEnv *env, uint64_t value)
95 {
96 	jobject valueObj;
97 	jobject byteArray;
98 	jbyte *bytes;
99 	int i;
100 
101 	if (!(byteArray = (*env)->NewByteArray(env, 9)))
102 		return (NULL); /* OutOfMemoryError thrown */
103 	if (!(bytes = (*env)->GetByteArrayElements(env, byteArray, NULL)))
104 		return (NULL); /* OutOfMemoryError thrown */
105 
106 	/*
107 	 * Interpret the uint64_t as a 9-byte big-endian signed quantity
108 	 * suitable for constructing an UnsignedInt64 or BigInteger.
109 	 */
110 	for (i = 8; i >= 1; i--) {
111 		bytes[i] = value & 0xff;
112 		value >>= 8;
113 	}
114 	bytes[0] = 0;
115 	(*env)->ReleaseByteArrayElements(env, byteArray, bytes, 0);
116 
117 	if (!(valueObj = (*env)->NewObject(env, ui64class, ui64cons_mid,
118 	    byteArray)))
119 		return (NULL); /* exception thrown */
120 
121 	return (valueObj);
122 }
123 
124 /*
125  * Create an array list and then copy the native array into it
126  */
127 jobject
128 copyArray(JNIEnv *jenv, void **nativeArray)
129 {
130 	int i;
131 	jobject jresult = NULL;
132 
133 	if (nativeArray != NULL) {
134 		jclass ALclazz;
135 		jmethodID ALinit, ALadd;
136 		jclass Lclazz;
137 		jmethodID Linit;
138 
139 		ALclazz = (*jenv)->FindClass(jenv,
140 		    "java/util/ArrayList");
141 		ALinit = (*jenv)->GetMethodID(jenv,
142 		    ALclazz, "<init>", "()V");
143 		ALadd = (*jenv)->GetMethodID(jenv,
144 		    ALclazz, "add", "(Ljava/lang/Object;)Z");
145 		jresult = (*jenv)->NewObject(jenv, ALclazz, ALinit);
146 		Lclazz = (*jenv)->FindClass(jenv, "java/lang/Long");
147 		Linit = (*jenv)->GetMethodID(jenv,
148 		    Lclazz, "<init>", "(J)V");
149 		for (i = 0; nativeArray[i] != NULL; i++) {
150 			jobject L;
151 			/* Build longs and add them */
152 			L = (*jenv)->NewObject(jenv,
153 			    Lclazz, Linit, (jlong)(uintptr_t)nativeArray[i]);
154 			(*jenv)->CallBooleanMethod(jenv,
155 			    jresult, ALadd, L);
156 		}
157 		free(nativeArray);
158 	}
159 	return (jresult);
160 }
161 
162 /*
163  * pool_version(3pool) wrapper
164  */
165 /*ARGSUSED*/
166 JNIEXPORT jlong JNICALL
167 Java_com_sun_solaris_service_pools_PoolInternal_pool_1version(JNIEnv *jenv,
168     jclass jcls, jlong jver) {
169 	return ((jlong)pool_version((uint_t)jver));
170 }
171 
172 /*
173  * native constant accessor
174  */
175 /*ARGSUSED*/
176 JNIEXPORT jint JNICALL
177 Java_com_sun_solaris_service_pools_PoolInternal_get_1POX_1NATIVE(JNIEnv *jenv,
178     jclass jcls) {
179 	return ((jint)POX_NATIVE);
180 }
181 
182 /*
183  * native constant accessor
184  */
185 /*ARGSUSED*/
186 JNIEXPORT jint JNICALL
187 Java_com_sun_solaris_service_pools_PoolInternal_get_1POX_1TEXT(JNIEnv *jenv,
188     jclass jcls) {
189 	return ((jint)POX_TEXT);
190 }
191 
192 /*
193  * native constant accessor
194  */
195 /*ARGSUSED*/
196 JNIEXPORT jint JNICALL
197 Java_com_sun_solaris_service_pools_PoolInternal_get_1POC_1INVAL(JNIEnv *jenv,
198     jclass jcls) {
199 	return ((jint)POC_INVAL);
200 }
201 
202 /*
203  * native constant accessor
204  */
205 /*ARGSUSED*/
206 JNIEXPORT jint JNICALL
207 Java_com_sun_solaris_service_pools_PoolInternal_get_1POC_1UINT(JNIEnv *jenv,
208     jclass jcls) {
209 	return ((jint)POC_UINT);
210 }
211 
212 /*
213  * native constant accessor
214  */
215 /*ARGSUSED*/
216 JNIEXPORT jint JNICALL
217 Java_com_sun_solaris_service_pools_PoolInternal_get_1POC_1INT(JNIEnv *jenv,
218     jclass jcls) {
219 	return ((jint)POC_INT);
220 }
221 
222 /*
223  * native constant accessor
224  */
225 /*ARGSUSED*/
226 JNIEXPORT jint JNICALL
227 Java_com_sun_solaris_service_pools_PoolInternal_get_1POC_1DOUBLE(JNIEnv *jenv,
228     jclass jcls) {
229 	return ((jint)POC_DOUBLE);
230 }
231 
232 /*
233  * native constant accessor
234  */
235 /*ARGSUSED*/
236 JNIEXPORT jint JNICALL
237 Java_com_sun_solaris_service_pools_PoolInternal_get_1POC_1BOOL(JNIEnv *jenv,
238     jclass jcls) {
239 	return ((jint)POC_BOOL);
240 }
241 
242 /*
243  * native constant accessor
244  */
245 /*ARGSUSED*/
246 JNIEXPORT jint JNICALL
247 Java_com_sun_solaris_service_pools_PoolInternal_get_1POC_1STRING(JNIEnv *jenv,
248     jclass jcls) {
249 	return ((jint)POC_STRING);
250 }
251 
252 /*
253  * native constant accessor
254  */
255 /*ARGSUSED*/
256 JNIEXPORT jint JNICALL
257 Java_com_sun_solaris_service_pools_PoolInternal_get_1POV_1NONE(JNIEnv *jenv,
258     jclass jcls) {
259 	return ((jint)POV_NONE);
260 }
261 
262 /*
263  * native constant accessor
264  */
265 /*ARGSUSED*/
266 JNIEXPORT jint JNICALL
267 Java_com_sun_solaris_service_pools_PoolInternal_get_1POV_1LOOSE(JNIEnv *jenv,
268     jclass jcls) {
269 	return ((jint)POV_LOOSE);
270 }
271 
272 /*
273  * native constant accessor
274  */
275 /*ARGSUSED*/
276 JNIEXPORT jint JNICALL
277 Java_com_sun_solaris_service_pools_PoolInternal_get_1POV_1STRICT(JNIEnv *jenv,
278     jclass jcls) {
279 	return ((jint)POV_STRICT);
280 }
281 
282 /*
283  * native constant accessor
284  */
285 /*ARGSUSED*/
286 JNIEXPORT jint JNICALL
287 Java_com_sun_solaris_service_pools_PoolInternal_get_1POV_1RUNTIME(JNIEnv *jenv,
288     jclass jcls) {
289 	return ((jint)POV_RUNTIME);
290 }
291 
292 /*
293  * native constant accessor
294  */
295 /*ARGSUSED*/
296 JNIEXPORT jint JNICALL
297 Java_com_sun_solaris_service_pools_PoolInternal_get_1POF_1INVALID(JNIEnv *jenv,
298     jclass jcls) {
299 	return ((jint)POF_INVALID);
300 }
301 
302 /*
303  * native constant accessor
304  */
305 /*ARGSUSED*/
306 JNIEXPORT jint JNICALL
307 Java_com_sun_solaris_service_pools_PoolInternal_get_1POF_1VALID(JNIEnv *jenv,
308     jclass jcls) {
309 	return ((jint)POF_VALID);
310 }
311 
312 /*
313  * native constant accessor
314  */
315 /*ARGSUSED*/
316 JNIEXPORT jint JNICALL
317 Java_com_sun_solaris_service_pools_PoolInternal_get_1POF_1DESTROY(JNIEnv *jenv,
318     jclass jcls) {
319 	return ((jint)POF_DESTROY);
320 }
321 
322 /*
323  * pool_error(3pool) wrapper
324  */
325 /*ARGSUSED*/
326 JNIEXPORT jint JNICALL
327 Java_com_sun_solaris_service_pools_PoolInternal_pool_1error(JNIEnv *jenv,
328     jclass jcls) {
329 	return ((jint)pool_error());
330 }
331 
332 /*
333  * pool_strerror(3pool) wrapper
334  */
335 /*ARGSUSED*/
336 JNIEXPORT jstring JNICALL
337 Java_com_sun_solaris_service_pools_PoolInternal_pool_1strerror(JNIEnv *jenv,
338     jclass jcls, jint jperr) {
339 	jstring jresult = NULL;
340 	char *result;
341 
342 	result = (char *)pool_strerror((int)jperr);
343 
344 	if (result)
345 		jresult = (*jenv)->NewStringUTF(jenv, result);
346 	return (jresult);
347 }
348 
349 /*
350  * strerror(3c) wrapper
351  */
352 /*ARGSUSED*/
353 JNIEXPORT jstring JNICALL
354 Java_com_sun_solaris_service_pools_PoolInternal_pool_1strerror_1sys(JNIEnv *
355     jenv, jclass jcls) {
356 	jstring jresult = NULL;
357 	char *result;
358 
359 	result = (char *)strerror(errno);
360 
361 	if (result)
362 		jresult = (*jenv)->NewStringUTF(jenv, result);
363 	return (jresult);
364 }
365 
366 /*
367  * errno(3c) accessor
368  */
369 /*ARGSUSED*/
370 JNIEXPORT jint JNICALL
371 Java_com_sun_solaris_service_pools_PoolsException_getErrno(JNIEnv *jenv,
372     jclass jcls) {
373 	return ((jint)errno);
374 }
375 
376 /*
377  * pool_resource_type_list(3pool) wrapper
378  */
379 /*ARGSUSED*/
380 JNIEXPORT jint JNICALL
381 Java_com_sun_solaris_service_pools_PoolInternal_pool_1resource_1type_1list(
382     JNIEnv *jenv, jclass jcls, jlong jreslist, jlong jnumres) {
383 	char **reslist = (char **)(uintptr_t)jreslist;
384 	uint_t *numres = (uint_t *)(uintptr_t)jnumres;
385 
386 	return ((jint)pool_resource_type_list((char const **)reslist, numres));
387 }
388 
389 /*
390  * pool_get_status(3pool) wrapper
391  */
392 /*ARGSUSED*/
393 JNIEXPORT jint JNICALL
394 Java_com_sun_solaris_service_pools_PoolInternal_pool_1get_1status(JNIEnv *jenv,
395     jclass jcls) {
396 	int status;
397 	int err;
398 
399 	err = pool_get_status(&status);
400 	if (err == -1)
401 		return ((jint)PO_FAIL);
402 	else
403 		return ((jint)status);
404 }
405 
406 /*
407  * pool_set_status(3pool) wrapper
408  */
409 /*ARGSUSED*/
410 JNIEXPORT jint JNICALL
411 Java_com_sun_solaris_service_pools_PoolInternal_pool_1set_1status(JNIEnv *jenv,
412     jclass jcls, jint jstate) {
413 	return ((jint)pool_set_status((int)jstate));
414 }
415 
416 /*
417  * pool_conf_alloc(3pool) wrapper
418  */
419 /*ARGSUSED*/
420 JNIEXPORT jlong JNICALL
421 Java_com_sun_solaris_service_pools_PoolInternal_pool_1conf_1alloc(JNIEnv *jenv,
422     jclass jcls) {
423 	return ((jlong)(uintptr_t)pool_conf_alloc());
424 }
425 
426 /*
427  * pool_conf_free(3pool) wrapper
428  */
429 /*ARGSUSED*/
430 JNIEXPORT void JNICALL
431 Java_com_sun_solaris_service_pools_PoolInternal_pool_1conf_1free(JNIEnv *jenv,
432     jclass jcls, jlong jconf) {
433 	pool_conf_free((pool_conf_t *)(uintptr_t)jconf);
434 }
435 
436 /*
437  * pool_conf_status(3pool) wrapper
438  */
439 /*ARGSUSED*/
440 JNIEXPORT jint JNICALL
441 Java_com_sun_solaris_service_pools_PoolInternal_pool_1conf_1status(JNIEnv *jenv,
442     jclass jcls, jlong jconf) {
443 	return ((jint)pool_conf_status((pool_conf_t *)(uintptr_t)jconf));
444 }
445 
446 /*
447  * pool_conf_close(3pool) wrapper
448  */
449 /*ARGSUSED*/
450 JNIEXPORT jint JNICALL
451 Java_com_sun_solaris_service_pools_PoolInternal_pool_1conf_1close(JNIEnv *jenv,
452     jclass jcls, jlong jconf) {
453 	return ((jint)pool_conf_close((pool_conf_t *)(uintptr_t)jconf));
454 }
455 
456 /*
457  * pool_conf_remove(3pool) wrapper
458  */
459 /*ARGSUSED*/
460 JNIEXPORT jint JNICALL
461 Java_com_sun_solaris_service_pools_PoolInternal_pool_1conf_1remove(JNIEnv *jenv,
462     jclass jcls, jlong jconf) {
463 	return ((jint)pool_conf_remove((pool_conf_t *)(uintptr_t)jconf));
464 }
465 
466 /*
467  * pool_conf_open(3pool) wrapper
468  */
469 /*ARGSUSED*/
470 JNIEXPORT jint JNICALL
471 Java_com_sun_solaris_service_pools_PoolInternal_pool_1conf_1open(JNIEnv *jenv,
472     jclass jcls, jlong jconf, jstring jlocation, jint jflags) {
473 	const char *location;
474 	int result;
475 
476 	location = (jlocation) ? (*jenv)->GetStringUTFChars(jenv,
477 	    jlocation, 0) : NULL;
478 	result = (int)pool_conf_open((pool_conf_t *)(uintptr_t)jconf, location,
479 	    (int)jflags);
480 
481 	if (location)
482 		(*jenv)->ReleaseStringUTFChars(jenv, jlocation, location);
483 	return ((jint)result);
484 }
485 
486 /*
487  * pool_conf_rollback(3pool) wrapper
488  */
489 /*ARGSUSED*/
490 JNIEXPORT jint JNICALL
491 Java_com_sun_solaris_service_pools_PoolInternal_pool_1conf_1rollback(
492     JNIEnv *jenv, jclass jcls, jlong jconf) {
493 	return ((jint)pool_conf_rollback((pool_conf_t *)(uintptr_t)jconf));
494 }
495 
496 /*
497  * pool_conf_commit(3pool) wrapper
498  */
499 /*ARGSUSED*/
500 JNIEXPORT jint JNICALL
501 Java_com_sun_solaris_service_pools_PoolInternal_pool_1conf_1commit(JNIEnv *jenv,
502     jclass jcls, jlong jconf, jint jactive) {
503 	return ((jint)pool_conf_commit(
504 	    (pool_conf_t *)(uintptr_t)jconf, (int)jactive));
505 }
506 
507 /*
508  * pool_conf_export(3pool) wrapper
509  */
510 /*ARGSUSED*/
511 JNIEXPORT jint JNICALL
512 Java_com_sun_solaris_service_pools_PoolInternal_pool_1conf_1export(JNIEnv *jenv,
513     jclass jcls, jlong jconf, jstring jlocation, jint jformat) {
514 	const char *location;
515 	int result;
516 
517 	location = (jlocation) ? (*jenv)->GetStringUTFChars(jenv,
518 	    jlocation, 0) : NULL;
519 	result = (int)pool_conf_export((pool_conf_t *)(uintptr_t)jconf,
520 	    location, (pool_export_format_t)jformat);
521 
522 	if (location)
523 		(*jenv)->ReleaseStringUTFChars(jenv, jlocation, location);
524 	return ((jint)result);
525 }
526 
527 /*
528  * pool_conf_validate(3pool) wrapper
529  */
530 /*ARGSUSED*/
531 JNIEXPORT jint JNICALL
532 Java_com_sun_solaris_service_pools_PoolInternal_pool_1conf_1validate(
533     JNIEnv *jenv, jclass jcls, jlong jconf, jint jlevel) {
534 	return ((jint)pool_conf_validate((pool_conf_t *)(uintptr_t)jconf,
535 		    (pool_valid_level_t)jlevel));
536 }
537 
538 /*
539  * pool_conf_update(3pool) wrapper
540  */
541 /*ARGSUSED*/
542 JNIEXPORT jint JNICALL
543 Java_com_sun_solaris_service_pools_PoolInternal_pool_1conf_1update(JNIEnv *jenv,
544     jclass jcls, jlong jconf) {
545 	int changed;
546 	int result;
547 
548 	result = pool_conf_update((pool_conf_t *)(uintptr_t)jconf, &changed);
549 
550 	if (result != PO_SUCCESS) {
551 		throw_pe(jenv);
552 	}
553 
554 	return ((jint)changed);
555 }
556 
557 /*
558  * pool_get_pool(3pool) wrapper
559  */
560 /*ARGSUSED*/
561 JNIEXPORT jlong JNICALL
562 Java_com_sun_solaris_service_pools_PoolInternal_pool_1get_1pool(JNIEnv *jenv,
563     jclass jcls, jlong jconf, jstring jname) {
564 	const char *name;
565 	pool_t *result;
566 
567 	name = (jname) ? (*jenv)->GetStringUTFChars(jenv, jname, 0) :
568 	    NULL;
569 	result = (pool_t *)pool_get_pool((pool_conf_t *)(uintptr_t)jconf, name);
570 
571 	if (name)
572 		(*jenv)->ReleaseStringUTFChars(jenv, jname, name);
573 	return ((jlong)(uintptr_t)result);
574 }
575 
576 /*
577  * pool_query_pools(3pool) wrapper
578  */
579 /*ARGSUSED*/
580 JNIEXPORT jobject JNICALL
581 Java_com_sun_solaris_service_pools_PoolInternal_pool_1query_1pools(JNIEnv *jenv,
582     jclass jcls, jlong jconf, jobject jprops) {
583 	pool_value_t **props;
584 	pool_t **result;
585 	jclass Lclazz;
586 	jmethodID Lsize;
587 	jint size;
588 	uint_t nelem;
589 	int i;
590 
591 
592 	/*
593 	 * Initialize the target parameter for case when input is null
594 	 */
595 	props = NULL;
596 	if (jprops != NULL) {
597 		Lclazz = (*jenv)->GetObjectClass(jenv, jprops);
598 		Lsize = (*jenv)->GetMethodID(jenv, Lclazz, "size", "()I");
599 		size = (*jenv)->CallIntMethod(jenv, jprops, Lsize);
600 
601 		if (size != 0) {
602 			jmethodID Lget;
603 
604 			Lget = (*jenv)->GetMethodID(jenv, Lclazz, "get",
605 			    "(I)Ljava/lang/Object;");
606 			/*
607 			 * Allocate space for the props array
608 			 */
609 
610 			if ((props = calloc(size + 1, sizeof (pool_value_t *)))
611 			    == NULL) {
612 				throwException(jenv, "java/lang/Exception",
613 				    "Could not allocate props array");
614 				return (NULL);
615 			}
616 			/*
617 			 * Copy in the array
618 			 */
619 			for (i = 0; i < size; i++) {
620 				jobject aVal;
621 				jclass Vclazz;
622 				jfieldID Vthis;
623 				jlong this;
624 
625 				aVal = (*jenv)->CallObjectMethod(jenv, jprops,
626 				    Lget, (jint) i);
627 				Vclazz = (*jenv)->GetObjectClass(jenv, aVal);
628 				Vthis = (*jenv)->GetFieldID(jenv, Vclazz,
629 				    "_this", "J");
630 				this = (*jenv)->GetLongField(jenv, aVal, Vthis);
631 				props[i] = (pool_value_t *)(uintptr_t)this;
632 			}
633 		}
634 	}
635 	result = pool_query_pools((pool_conf_t *)(uintptr_t)jconf, &nelem,
636 	    props);
637 	free(props);
638 	return (copyArray(jenv, (void **)result));
639 }
640 
641 /*
642  * pool_get_resource(3pool) wrapper
643  */
644 /*ARGSUSED*/
645 JNIEXPORT jlong JNICALL
646 Java_com_sun_solaris_service_pools_PoolInternal_pool_1get_1resource(
647     JNIEnv *jenv, jclass jcls, jlong jconf, jstring jtype, jstring jname) {
648 	const char *type;
649 	const char *name;
650 	pool_resource_t *result;
651 
652 	type = (jtype) ? (*jenv)->GetStringUTFChars(jenv, jtype, 0) :
653 	    NULL;
654 	name = (jname) ? (*jenv)->GetStringUTFChars(jenv, jname, 0) :
655 	    NULL;
656 	result = pool_get_resource((pool_conf_t *)(uintptr_t)jconf, type, name);
657 
658 	if (type)
659 		(*jenv)->ReleaseStringUTFChars(jenv, jtype, type);
660 	if (name)
661 		(*jenv)->ReleaseStringUTFChars(jenv, jname, name);
662 	return ((jlong)(uintptr_t)result);
663 }
664 
665 /*
666  * pool_query_resources(3pool) wrapper
667  */
668 /*ARGSUSED*/
669 JNIEXPORT jobject JNICALL
670 Java_com_sun_solaris_service_pools_PoolInternal_pool_1query_1resources(
671     JNIEnv *jenv, jclass jcls, jlong jconf, jobject jprops) {
672 	pool_value_t **props;
673 	pool_resource_t **result;
674 	jclass Lclazz;
675 	jmethodID Lsize;
676 	jint size;
677 	uint_t nelem;
678 	int i;
679 
680 	/*
681 	 * Initialize the target parameter for case when input is null
682 	 */
683 	props = NULL;
684 	if (jprops != NULL) {
685 		Lclazz = (*jenv)->GetObjectClass(jenv, jprops);
686 		Lsize = (*jenv)->GetMethodID(jenv, Lclazz, "size", "()I");
687 		size = (*jenv)->CallIntMethod(jenv, jprops, Lsize);
688 
689 		if (size != 0) {
690 			jmethodID Lget;
691 
692 			Lget = (*jenv)->GetMethodID(jenv, Lclazz, "get",
693 			    "(I)Ljava/lang/Object;");
694 			/*
695 			 * Allocate space for the props array
696 			 */
697 			if ((props = calloc(size + 1, sizeof (pool_value_t *)))
698 			    == NULL) {
699 				throwException(jenv, "java/lang/Exception",
700 				    "Could not allocate props array");
701 				return (NULL);
702 			}
703 			/*
704 			 * Copy in the array
705 			 */
706 			for (i = 0; i < size; i++) {
707 				jobject aVal;
708 				jclass Vclazz;
709 				jfieldID Vthis;
710 				jlong this;
711 
712 
713 				aVal = (*jenv)->CallObjectMethod(jenv, jprops,
714 				    Lget, (jint) i);
715 				Vclazz = (*jenv)->GetObjectClass(jenv, aVal);
716 				Vthis = (*jenv)->GetFieldID(jenv, Vclazz,
717 				    "_this", "J");
718 				this = (*jenv)->GetLongField(jenv, aVal, Vthis);
719 				props[i] = (pool_value_t *)(uintptr_t)this;
720 			}
721 		}
722 	}
723 	result = pool_query_resources((pool_conf_t *)(uintptr_t)jconf, &nelem,
724 	    props);
725 	free(props);
726 	return (copyArray(jenv, (void *)result));
727 }
728 
729 /*
730  * pool_query_components(3pool) wrapper
731  */
732 /*ARGSUSED*/
733 JNIEXPORT jobject JNICALL
734 Java_com_sun_solaris_service_pools_PoolInternal_pool_1query_1components(
735     JNIEnv *jenv, jclass jcls, jlong jconf, jobject jprops) {
736 	pool_value_t **props;
737 	pool_component_t **result;
738 	jclass Lclazz;
739 	jmethodID Lsize;
740 	jint size;
741 	uint_t nelem;
742 	int i;
743 
744 	/*
745 	 * Initialize the target parameter for case when input is null
746 	 */
747 	props = NULL;
748 	if (jprops != NULL) {
749 		Lclazz = (*jenv)->GetObjectClass(jenv, jprops);
750 		Lsize = (*jenv)->GetMethodID(jenv, Lclazz, "size", "()I");
751 		size = (*jenv)->CallIntMethod(jenv, jprops, Lsize);
752 
753 		if (size != 0) {
754 			jmethodID Lget;
755 
756 			Lget = (*jenv)->GetMethodID(jenv, Lclazz, "get",
757 			    "(I)Ljava/lang/Object;");
758 			/*
759 			 * Allocate space for the props array
760 			 */
761 
762 			if ((props = calloc(size + 1, sizeof (pool_value_t *)))
763 			    == NULL) {
764 				throwException(jenv, "java/lang/Exception",
765 				    "Could not allocate props array");
766 				return (NULL);
767 			}
768 			/*
769 			 * Copy in the array
770 			 */
771 			for (i = 0; i < size; i++) {
772 				jobject aVal;
773 				jclass Vclazz;
774 				jfieldID Vthis;
775 				jlong this;
776 
777 				aVal = (*jenv)->CallObjectMethod(jenv, jprops,
778 				    Lget, (jint) i);
779 				Vclazz = (*jenv)->GetObjectClass(jenv, aVal);
780 				Vthis = (*jenv)->GetFieldID(jenv, Vclazz,
781 				    "_this", "J");
782 				this = (*jenv)->GetLongField(jenv, aVal, Vthis);
783 				props[i] = (pool_value_t *)(uintptr_t)this;
784 			}
785 		}
786 	}
787 	result = pool_query_components((pool_conf_t *)(uintptr_t)jconf, &nelem,
788 	    props);
789 	free(props);
790 	return (copyArray(jenv, (void **)result));
791 }
792 
793 /*
794  * pool_conf_location(3pool) wrapper
795  */
796 /*ARGSUSED*/
797 JNIEXPORT jstring JNICALL
798 Java_com_sun_solaris_service_pools_PoolInternal_pool_1conf_1location(
799     JNIEnv *jenv, jclass jcls, jlong jconf) {
800 	jstring jresult = NULL;
801 	const char *result;
802 
803 	result = pool_conf_location((pool_conf_t *)(uintptr_t)jconf);
804 
805 	if (result)
806 		jresult = (*jenv)->NewStringUTF(jenv, result);
807 	return (jresult);
808 }
809 
810 /*
811  * pool_conf_info(3pool) wrapper
812  */
813 /*ARGSUSED*/
814 JNIEXPORT jstring JNICALL
815 Java_com_sun_solaris_service_pools_PoolInternal_pool_1conf_1info(JNIEnv *jenv,
816     jclass jcls, jlong jconf, jint jflags) {
817 	jstring jresult = NULL;
818 	const char *result;
819 
820 	result = pool_conf_info((pool_conf_t *)(uintptr_t)jconf, (int)jflags);
821 
822 	if (result)
823 		jresult = (*jenv)->NewStringUTF(jenv, result);
824 	free((void *)result);
825 	return (jresult);
826 }
827 
828 /*
829  * pool_resource_create(3pool) wrapper
830  */
831 /*ARGSUSED*/
832 JNIEXPORT jlong JNICALL
833 Java_com_sun_solaris_service_pools_PoolInternal_pool_1resource_1create(
834     JNIEnv *jenv, jclass jcls, jlong jconf, jstring jtype, jstring jname) {
835 	const char *type;
836 	const char *name;
837 	pool_resource_t *result;
838 
839 	type = (jtype) ? (*jenv)->GetStringUTFChars(jenv, jtype, 0) :
840 	    NULL;
841 	name = (jname) ? (*jenv)->GetStringUTFChars(jenv, jname, 0) :
842 	    NULL;
843 	result =
844 	    pool_resource_create((pool_conf_t *)(uintptr_t)jconf, type, name);
845 
846 	if (type)
847 		(*jenv)->ReleaseStringUTFChars(jenv, jtype, type);
848 	if (name)
849 		(*jenv)->ReleaseStringUTFChars(jenv, jname, name);
850 	return ((jlong)(uintptr_t)result);
851 }
852 
853 /*
854  * pool_resource_destroy(3pool) wrapper
855  */
856 /*ARGSUSED*/
857 JNIEXPORT jint JNICALL
858 Java_com_sun_solaris_service_pools_PoolInternal_pool_1resource_1destroy(
859     JNIEnv *jenv, jclass jcls, jlong jconf, jlong jresource) {
860 	return ((jint)pool_resource_destroy((pool_conf_t *)(uintptr_t)jconf,
861 		    (pool_resource_t *)(uintptr_t)jresource));
862 }
863 
864 /*
865  * pool_resource_transfer(3pool) wrapper
866  */
867 /*ARGSUSED*/
868 JNIEXPORT jint JNICALL
869 Java_com_sun_solaris_service_pools_PoolInternal_pool_1resource_1transfer(
870     JNIEnv *jenv, jclass jcls, jlong jconf, jlong jsource, jlong jtarget,
871     jlong jsize) {
872 	return (pool_resource_transfer((pool_conf_t *)(uintptr_t)jconf,
873 	    (pool_resource_t *)(uintptr_t)jsource,
874 	    (pool_resource_t *)(uintptr_t)jtarget,
875 	    (uint64_t)jsize));
876 }
877 
878 /*
879  * pool_resource_xtransfer(3pool) wrapper
880  */
881 /*ARGSUSED*/
882 JNIEXPORT jint JNICALL
883 Java_com_sun_solaris_service_pools_PoolInternal_pool_1resource_1xtransfer(
884     JNIEnv *jenv, jclass jcls, jlong jconf, jlong jsource, jlong jtarget,
885     jobject jcomponents) {
886 	pool_component_t **components;
887 	int result;
888 	jclass Lclazz;
889 	jmethodID Lsize;
890 	jint size;
891 
892 	/*
893 	 * Initialize the target parameter for case when input is null
894 	 */
895 	components = NULL;
896 	if (jcomponents != NULL) {
897 		Lclazz = (*jenv)->GetObjectClass(jenv, jcomponents);
898 		Lsize = (*jenv)->GetMethodID(jenv, Lclazz, "size", "()I");
899 		size = (*jenv)->CallIntMethod(jenv, jcomponents, Lsize);
900 
901 		if (size != 0) {
902 			jmethodID Lget;
903 			int i;
904 
905 			Lget = (*jenv)->GetMethodID(jenv,
906 			    Lclazz, "get", "(I)Ljava/lang/Object;");
907 			/* Allocate space for the components array */
908 
909 			if ((components = calloc(size + 1,
910 			    sizeof (pool_component_t *))) == NULL) {
911 				throwException(jenv, "java/lang/Exception",
912 				    "Could not allocate component array");
913 				return (NULL);
914 			}
915 			/*
916 			 * Copy in the array
917 			 */
918 			for (i = 0; i < size; i++) {
919 				jobject aVal;
920 				jclass Vclazz;
921 				jlong this;
922 				jmethodID Vthis;
923 
924 				aVal = (*jenv)->CallObjectMethod(jenv,
925 				    jcomponents, Lget, (jint) i);
926 				Vclazz = (*jenv)->GetObjectClass(jenv,
927 				    aVal);
928 				Vthis = (*jenv)->GetMethodID(jenv,
929 				    Vclazz, "getComponent", "()J");
930 				this = (*jenv)->CallLongMethod(jenv,
931 				    aVal, Vthis);
932 				components[i] =
933 				    (pool_component_t *)(uintptr_t)this;
934 			}
935 		}
936 	}
937 	result = (int)pool_resource_xtransfer((pool_conf_t *)(uintptr_t)jconf,
938 	    (pool_resource_t *)(uintptr_t)jsource,
939 	    (pool_resource_t *)(uintptr_t)jtarget,
940 	    components);
941 	free(components);
942 
943 	return ((jint)result);
944 }
945 
946 /*
947  * pool_query_resource_components(3pool) wrapper
948  */
949 /*ARGSUSED*/
950 JNIEXPORT jobject JNICALL
951 Java_com_sun_solaris_service_pools_PoolInternal_pool_1query_1resource_\
952 1components(JNIEnv *jenv, jclass jcls, jlong jconf, jlong jresource,
953     jobject jprops) {
954 	pool_value_t **props;
955 	pool_component_t **result;
956 	jclass Lclazz;
957 	jmethodID Lsize;
958 	uint_t nelem;
959 	jint size;
960 
961 	/*
962 	 * Initialize the target parameter for case when input is null
963 	 */
964 	props = NULL;
965 	if (jprops != NULL) {
966 		Lclazz = (*jenv)->GetObjectClass(jenv, jprops);
967 		Lsize = (*jenv)->GetMethodID(jenv, Lclazz, "size", "()I");
968 		size = (*jenv)->CallIntMethod(jenv, jprops, Lsize);
969 
970 		if (size != 0) {
971 			jmethodID Lget;
972 			int i;
973 
974 			Lget = (*jenv)->GetMethodID(jenv, Lclazz, "get",
975 			    "(I)Ljava/lang/Object;");
976 			/*
977 			 * Allocate space for the props array
978 			 */
979 			if ((props = calloc(size + 1, sizeof (pool_value_t *)))
980 			    == NULL) {
981 				throwException(jenv, "java/lang/Exception",
982 				    "Could not allocate props array");
983 				return (NULL);
984 			}
985 			/*
986 			 * Copy in the array
987 			 */
988 			for (i = 0; i < size; i++) {
989 				jobject aVal;
990 				jclass Vclazz;
991 				jfieldID Vthis;
992 				jlong this;
993 
994 				aVal = (*jenv)->CallObjectMethod(jenv, jprops,
995 				    Lget, (jint) i);
996 				Vclazz = (*jenv)->GetObjectClass(jenv, aVal);
997 				Vthis = (*jenv)->GetFieldID(jenv, Vclazz,
998 				    "_this", "J");
999 				this = (*jenv)->GetLongField(jenv, aVal, Vthis);
1000 				props[i] = (pool_value_t *)(uintptr_t)this;
1001 			}
1002 		}
1003 	}
1004 	result = pool_query_resource_components(
1005 	    (pool_conf_t *)(uintptr_t)jconf,
1006 	    (pool_resource_t *)(uintptr_t)jresource, &nelem, props);
1007 	free(props);
1008 	return (copyArray(jenv, (void **)result));
1009 }
1010 
1011 /*
1012  * pool_resource_info(3pool) wrapper
1013  */
1014 /*ARGSUSED*/
1015 JNIEXPORT jstring JNICALL
1016 Java_com_sun_solaris_service_pools_PoolInternal_pool_1resource_1info(
1017     JNIEnv *jenv, jclass jcls, jlong jconf, jlong jresource, jint jflags) {
1018 	jstring jresult = NULL;
1019 	const char *result;
1020 
1021 	result = pool_resource_info((pool_conf_t *)(uintptr_t)jconf,
1022 	    (pool_resource_t *)(uintptr_t)jresource, (int)jflags);
1023 
1024 	if (result)
1025 		jresult = (*jenv)->NewStringUTF(jenv, result);
1026 	free((void *)result);
1027 	return (jresult);
1028 }
1029 
1030 /*
1031  * pool_create(3pool) wrapper
1032  */
1033 /*ARGSUSED*/
1034 JNIEXPORT jlong JNICALL
1035 Java_com_sun_solaris_service_pools_PoolInternal_pool_1create(JNIEnv *jenv,
1036     jclass jcls, jlong jconf, jstring jname) {
1037 	const char *name;
1038 	pool_t *result;
1039 
1040 	name = (jname) ? (*jenv)->GetStringUTFChars(jenv, jname, 0) :
1041 	    NULL;
1042 	result = pool_create((pool_conf_t *)(uintptr_t)jconf, name);
1043 
1044 	if (name)
1045 		(*jenv)->ReleaseStringUTFChars(jenv, jname, name);
1046 	return ((jlong)(uintptr_t)result);
1047 }
1048 
1049 /*
1050  * pool_destroy(3pool) wrapper
1051  */
1052 /*ARGSUSED*/
1053 JNIEXPORT jint JNICALL
1054 Java_com_sun_solaris_service_pools_PoolInternal_pool_1destroy(JNIEnv *jenv,
1055     jclass jcls, jlong jconf, jlong jpool) {
1056 	return ((jint)pool_destroy((pool_conf_t *)(uintptr_t)jconf,
1057 	    (pool_t *)(uintptr_t)jpool));
1058 }
1059 
1060 /*
1061  * pool_associate(3pool) wrapper
1062  */
1063 /*ARGSUSED*/
1064 JNIEXPORT jint JNICALL
1065 Java_com_sun_solaris_service_pools_PoolInternal_pool_1associate(JNIEnv *jenv,
1066     jclass jcls, jlong jconf, jlong jpool, jlong jresource) {
1067 	return ((jint)pool_associate((pool_conf_t *)(uintptr_t)jconf,
1068 	    (pool_t *)(uintptr_t)jpool,
1069 	    (pool_resource_t *)(uintptr_t)jresource));
1070 }
1071 
1072 /*
1073  * pool_dissociate(3pool) wrapper
1074  */
1075 /*ARGSUSED*/
1076 JNIEXPORT jint JNICALL
1077 Java_com_sun_solaris_service_pools_PoolInternal_pool_1dissociate(JNIEnv *jenv,
1078     jclass jcls, jlong jconf, jlong jpool, jlong jresource) {
1079 	return ((jint)pool_dissociate((pool_conf_t *)(uintptr_t)jconf,
1080 	    (pool_t *)(uintptr_t)jpool,
1081 	    (pool_resource_t *)(uintptr_t)jresource));
1082 }
1083 
1084 /*
1085  * pool_info(3pool) wrapper
1086  */
1087 /*ARGSUSED*/
1088 JNIEXPORT jstring JNICALL
1089 Java_com_sun_solaris_service_pools_PoolInternal_pool_1info(JNIEnv *jenv,
1090     jclass jcls, jlong jconf, jlong jpool, jint jflags) {
1091 	jstring jresult = NULL;
1092 	const char *result;
1093 
1094 	result = pool_info((pool_conf_t *)(uintptr_t)jconf,
1095 	    (pool_t *)(uintptr_t)jpool, (int)jflags);
1096 
1097 	if (result)
1098 		jresult = (*jenv)->NewStringUTF(jenv, result);
1099 	free((void *)result);
1100 	return (jresult);
1101 }
1102 
1103 /*
1104  * pool_query_pool_resources(3pool) wrapper
1105  */
1106 /*ARGSUSED*/
1107 JNIEXPORT jobject JNICALL
1108 Java_com_sun_solaris_service_pools_PoolInternal_pool_1query_1pool_1resources(
1109     JNIEnv *jenv, jclass jcls, jlong jconf, jlong jpool, jobject jprops) {
1110 	pool_value_t **props;
1111 	pool_resource_t **result;
1112 	jclass Lclazz;
1113 	jmethodID Lsize;
1114 	uint_t nelem;
1115 	jint size;
1116 
1117 	/*
1118 	 * Initialize the target parameter for case when input is null
1119 	 */
1120 	props = NULL;
1121 	if (jprops != NULL) {
1122 		Lclazz = (*jenv)->GetObjectClass(jenv, jprops);
1123 		Lsize = (*jenv)->GetMethodID(jenv, Lclazz, "size", "()I");
1124 		size = (*jenv)->CallIntMethod(jenv, jprops, Lsize);
1125 
1126 		if (size != 0) {
1127 			jmethodID Lget;
1128 			int i;
1129 
1130 			Lget = (*jenv)->GetMethodID(jenv,
1131 			    Lclazz, "get", "(I)Ljava/lang/Object;");
1132 			/*
1133 			 * Allocate space for the props array
1134 			 */
1135 
1136 			if ((props = calloc(size + 1, sizeof (pool_value_t *)))
1137 			    == NULL) {
1138 				throwException(jenv, "java/lang/Exception",
1139 				    "Could not allocate props array");
1140 				return (NULL);
1141 			}
1142 			/*
1143 			 * Copy in the array
1144 			 */
1145 			for (i = 0; i < size; i++) {
1146 				jobject aVal;
1147 				jclass Vclazz;
1148 				jfieldID Vthis;
1149 				jlong this;
1150 
1151 				aVal = (*jenv)->CallObjectMethod(jenv, jprops,
1152 				    Lget, (jint) i);
1153 				Vclazz = (*jenv)->GetObjectClass(jenv, aVal);
1154 				Vthis = (*jenv)->GetFieldID(jenv, Vclazz,
1155 				    "_this", "J");
1156 				this = (*jenv)->GetLongField(jenv, aVal, Vthis);
1157 				props[i] = (pool_value_t *)(uintptr_t)this;
1158 			}
1159 		}
1160 	}
1161 	result = pool_query_pool_resources((pool_conf_t *)(uintptr_t)jconf,
1162 	    (pool_t *)(uintptr_t)jpool, &nelem, props);
1163 	free(props);
1164 	return (copyArray(jenv, (void **)result));
1165 }
1166 
1167 /*
1168  * pool_get_owning_resource(3pool) wrapper
1169  */
1170 /*ARGSUSED*/
1171 JNIEXPORT jlong JNICALL
1172 Java_com_sun_solaris_service_pools_PoolInternal_pool_1get_1owning_1resource(
1173     JNIEnv *jenv, jclass jcls, jlong jconf, jlong jcomponent) {
1174 	return ((jlong)(uintptr_t)pool_get_owning_resource(
1175 	    (pool_conf_t *)(uintptr_t)jconf,
1176 	    (pool_component_t *)(uintptr_t)jcomponent));
1177 }
1178 
1179 /*
1180  * pool_component_info(3pool) wrapper
1181  */
1182 /*ARGSUSED*/
1183 JNIEXPORT jstring JNICALL
1184 Java_com_sun_solaris_service_pools_PoolInternal_pool_1component_1info(
1185     JNIEnv *jenv, jclass jcls, jlong jconf, jlong jcomponent, jint jflags) {
1186 	jstring jresult = NULL;
1187 	const char *result;
1188 
1189 	result = pool_component_info((pool_conf_t *)(uintptr_t)jconf,
1190 	    (pool_component_t *)(uintptr_t)jcomponent, (int)jflags);
1191 
1192 	if (result)
1193 		jresult = (*jenv)->NewStringUTF(jenv, result);
1194 	free((void *)result);
1195 	return (jresult);
1196 }
1197 
1198 /*
1199  * pool_get_property(3pool) wrapper
1200  */
1201 /*ARGSUSED*/
1202 JNIEXPORT jint JNICALL
1203 Java_com_sun_solaris_service_pools_PoolInternal_pool_1get_1property(
1204     JNIEnv *jenv, jclass jcls, jlong jconf, jlong jelem, jstring jname,
1205     jlong jproperty) {
1206 	const char *name;
1207 	int result;
1208 
1209 	name = (jname) ? (*jenv)->GetStringUTFChars(jenv, jname, 0) :
1210 	    NULL;
1211 	result = pool_get_property((pool_conf_t *)(uintptr_t)jconf,
1212 	    (pool_elem_t *)(uintptr_t)jelem, name,
1213 	    (pool_value_t *)(uintptr_t)jproperty);
1214 
1215 	if (name)
1216 		(*jenv)->ReleaseStringUTFChars(jenv, jname, name);
1217 	return ((jint)result);
1218 }
1219 
1220 /*
1221  * pool_put_property(3pool) wrapper
1222  */
1223 /*ARGSUSED*/
1224 JNIEXPORT jint JNICALL
1225 Java_com_sun_solaris_service_pools_PoolInternal_pool_1put_1property(
1226     JNIEnv *jenv, jclass jcls, jlong jconf, jlong jelem, jstring jname,
1227     jlong jvalue) {
1228 	const char *name;
1229 	int result;
1230 
1231 	name = (jname) ? (*jenv)->GetStringUTFChars(jenv, jname, 0) : NULL;
1232 	result = (int)pool_put_property((pool_conf_t *)(uintptr_t)jconf,
1233 	    (pool_elem_t *)(uintptr_t)jelem, name,
1234 	    (pool_value_t *)(uintptr_t)jvalue);
1235 
1236 	if (name)
1237 		(*jenv)->ReleaseStringUTFChars(jenv, jname, name);
1238 	return ((jint)result);
1239 }
1240 
1241 /*
1242  * pool_rm_property(3pool) wrapper
1243  */
1244 /*ARGSUSED*/
1245 JNIEXPORT jint JNICALL
1246 Java_com_sun_solaris_service_pools_PoolInternal_pool_1rm_1property(JNIEnv *jenv,
1247     jclass jcls, jlong jconf, jlong jelem, jstring jname) {
1248 	const char *name;
1249 	int result;
1250 
1251 	name = (jname) ? (*jenv)->GetStringUTFChars(jenv, jname, 0) : NULL;
1252 	result = pool_rm_property((pool_conf_t *)(uintptr_t)jconf,
1253 	    (pool_elem_t *)(uintptr_t)jelem, name);
1254 
1255 	if (name)
1256 		(*jenv)->ReleaseStringUTFChars(jenv, jname, name);
1257 	return ((jint)result);
1258 }
1259 
1260 /*
1261  * pool_walk_properties(3pool) wrapper
1262  */
1263 /*ARGSUSED*/
1264 JNIEXPORT jint JNICALL
1265 Java_com_sun_solaris_service_pools_PoolInternal_pool_1walk_1properties(
1266     JNIEnv *jenv, jclass jcls, jlong jconf, jlong jelem, jlong jarg,
1267     jlong jcallback) {
1268 	int result;
1269 
1270 	result = (int)pool_walk_properties((pool_conf_t *)(uintptr_t)jconf,
1271 	    (pool_elem_t *)(uintptr_t)jelem, (void *)(uintptr_t)jarg,
1272 	    (int (*)(pool_conf_t *, pool_elem_t *, char const *,
1273 		pool_value_t *, void *))(uintptr_t)jcallback);
1274 
1275 	return ((jint)result);
1276 }
1277 
1278 /*
1279  * pool_conf_to_elem(3pool) wrapper
1280  */
1281 /*ARGSUSED*/
1282 JNIEXPORT jlong JNICALL
1283 Java_com_sun_solaris_service_pools_PoolInternal_pool_1conf_1to_1elem(
1284     JNIEnv *jenv, jclass jcls, jlong jconf) {
1285 	return ((jlong)(uintptr_t)pool_conf_to_elem(
1286 	    (pool_conf_t *)(uintptr_t)jconf));
1287 }
1288 
1289 /*
1290  * pool_to_elem(3pool) wrapper
1291  */
1292 /*ARGSUSED*/
1293 JNIEXPORT jlong JNICALL
1294 Java_com_sun_solaris_service_pools_PoolInternal_pool_1to_1elem(JNIEnv *jenv,
1295     jclass jcls, jlong jconf, jlong jpool) {
1296 	return ((jlong)(uintptr_t)pool_to_elem((pool_conf_t *)(uintptr_t)jconf,
1297 	    (pool_t *)(uintptr_t)jpool));
1298 }
1299 
1300 /*
1301  * pool_resource_to_elem(3pool) wrapper
1302  */
1303 /*ARGSUSED*/
1304 JNIEXPORT jlong JNICALL
1305 Java_com_sun_solaris_service_pools_PoolInternal_pool_1resource_1to_1elem(
1306     JNIEnv *jenv, jclass jcls, jlong jconf, jlong jresource) {
1307 	return ((jlong)(uintptr_t)pool_resource_to_elem(
1308 	    (pool_conf_t *)(uintptr_t)jconf,
1309 	    (pool_resource_t *)(uintptr_t)jresource));
1310 }
1311 
1312 /*
1313  * pool_component_to_elem(3pool) wrapper
1314  */
1315 /*ARGSUSED*/
1316 JNIEXPORT jlong JNICALL
1317 Java_com_sun_solaris_service_pools_PoolInternal_pool_1component_1to_1elem(
1318     JNIEnv *jenv, jclass jcls, jlong jconf, jlong jcomponent) {
1319 	return ((jlong)(uintptr_t)pool_component_to_elem(
1320 	    (pool_conf_t *)(uintptr_t)jconf,
1321 	    (pool_component_t *)(uintptr_t)jcomponent));
1322 }
1323 
1324 /*
1325  * pool_value_get_type(3pool) wrapper
1326  */
1327 /*ARGSUSED*/
1328 JNIEXPORT jint JNICALL
1329 Java_com_sun_solaris_service_pools_PoolInternal_pool_1value_1get_1type(
1330     JNIEnv *jenv, jclass jcls, jlong jvalue) {
1331 	return ((jint)pool_value_get_type((pool_value_t *)(uintptr_t)jvalue));
1332 }
1333 
1334 /*
1335  * pool_value_set_uint64(3pool) wrapper
1336  */
1337 /*ARGSUSED*/
1338 JNIEXPORT void JNICALL
1339 Java_com_sun_solaris_service_pools_PoolInternal_pool_1value_1set_1uint64(
1340     JNIEnv *jenv, jclass jcls, jlong jvalue, jlong jui64) {
1341 	pool_value_set_uint64(
1342 	    (pool_value_t *)(uintptr_t)jvalue, (uint64_t)jui64);
1343 }
1344 
1345 /*
1346  * pool_value_set_int64(3pool) wrapper
1347  */
1348 /*ARGSUSED*/
1349 JNIEXPORT void JNICALL
1350 Java_com_sun_solaris_service_pools_PoolInternal_pool_1value_1set_1int64(
1351     JNIEnv *jenv, jclass jcls, jlong jvalue, jlong ji64) {
1352 	pool_value_set_int64((pool_value_t *)(uintptr_t)jvalue, (int64_t)ji64);
1353 }
1354 
1355 /*
1356  * pool_value_set_double(3pool) wrapper
1357  */
1358 /*ARGSUSED*/
1359 JNIEXPORT void JNICALL
1360 Java_com_sun_solaris_service_pools_PoolInternal_pool_1value_1set_1double(
1361     JNIEnv *jenv, jclass jcls, jlong jvalue, jdouble jd) {
1362 	pool_value_set_double((pool_value_t *)(uintptr_t)jvalue, (double)jd);
1363 }
1364 
1365 /*
1366  * pool_value_set_bool(3pool) wrapper
1367  */
1368 /*ARGSUSED*/
1369 JNIEXPORT void JNICALL
1370 Java_com_sun_solaris_service_pools_PoolInternal_pool_1value_1set_1bool(
1371     JNIEnv *jenv, jclass jcls, jlong jvalue, jshort jb) {
1372 	pool_value_set_bool((pool_value_t *)(uintptr_t)jvalue, (uchar_t)jb);
1373 }
1374 
1375 /*
1376  * pool_value_set_string(3pool) wrapper
1377  */
1378 /*ARGSUSED*/
1379 JNIEXPORT jint JNICALL
1380 Java_com_sun_solaris_service_pools_PoolInternal_pool_1value_1set_1string(
1381     JNIEnv * jenv, jclass jcls, jlong jvalue, jstring jstr) {
1382 	const char *str;
1383 	int result;
1384 
1385 	str = (jstr) ? (*jenv)->GetStringUTFChars(jenv, jstr, 0) : NULL;
1386 	result = pool_value_set_string((pool_value_t *)(uintptr_t)jvalue, str);
1387 
1388 	if (str)
1389 		(*jenv)->ReleaseStringUTFChars(jenv, jstr, str);
1390 	return ((jint)result);
1391 }
1392 
1393 /*
1394  * pool_value_get_name(3pool) wrapper
1395  */
1396 /*ARGSUSED*/
1397 JNIEXPORT jstring JNICALL
1398 Java_com_sun_solaris_service_pools_PoolInternal_pool_1value_1get_1name(
1399     JNIEnv *jenv, jclass jcls, jlong jvalue) {
1400 	jstring jresult = NULL;
1401 	const char *result;
1402 
1403 	result = pool_value_get_name((pool_value_t *)(uintptr_t)jvalue);
1404 
1405 	if (result)
1406 		jresult = (*jenv)->NewStringUTF(jenv, result);
1407 	return (jresult);
1408 }
1409 
1410 /*
1411  * pool_value_set_name(3pool) wrapper
1412  */
1413 /*ARGSUSED*/
1414 JNIEXPORT jint JNICALL
1415 Java_com_sun_solaris_service_pools_PoolInternal_pool_1value_1set_1name(
1416     JNIEnv *jenv, jclass jcls, jlong jvalue, jstring jname) {
1417 	const char *name;
1418 	int result;
1419 
1420 	name = (jname) ? (*jenv)->GetStringUTFChars(jenv, jname, 0) : NULL;
1421 	result = pool_value_set_name((pool_value_t *)(uintptr_t)jvalue, name);
1422 
1423 	if (name)
1424 		(*jenv)->ReleaseStringUTFChars(jenv, jname, name);
1425 	return ((jint)result);
1426 }
1427 
1428 /*
1429  * pool_value_alloc(3pool) wrapper
1430  */
1431 /*ARGSUSED*/
1432 JNIEXPORT jlong JNICALL
1433 Java_com_sun_solaris_service_pools_PoolInternal_pool_1value_1alloc(JNIEnv *jenv,
1434     jclass jcls) {
1435 	return ((jlong)(uintptr_t)pool_value_alloc());
1436 }
1437 
1438 /*
1439  * pool_value_free(3pool) wrapper
1440  */
1441 /*ARGSUSED*/
1442 JNIEXPORT void JNICALL
1443 Java_com_sun_solaris_service_pools_PoolInternal_pool_1value_1free(JNIEnv *jenv,
1444     jclass jcls, jlong jvalue) {
1445 	pool_value_free((pool_value_t *)(uintptr_t)jvalue);
1446 }
1447 
1448 /*
1449  * pool_static_location(3pool) wrapper
1450  */
1451 /*ARGSUSED*/
1452 JNIEXPORT jstring JNICALL
1453 Java_com_sun_solaris_service_pools_PoolInternal_pool_1static_1location(
1454     JNIEnv *jenv, jclass jcls) {
1455 	jstring jresult = NULL;
1456 	const char *result;
1457 
1458 	result = pool_static_location();
1459 
1460 	if (result)
1461 		jresult = (*jenv)->NewStringUTF(jenv, result);
1462 	return (jresult);
1463 }
1464 
1465 /*
1466  * pool_dynamic_location(3pool) wrapper
1467  */
1468 /*ARGSUSED*/
1469 JNIEXPORT jstring JNICALL
1470 Java_com_sun_solaris_service_pools_PoolInternal_pool_1dynamic_1location(JNIEnv *
1471     jenv, jclass jcls) {
1472 	jstring jresult = NULL;
1473 	const char *result;
1474 
1475 	result = pool_dynamic_location();
1476 
1477 	if (result)
1478 		jresult = (*jenv)->NewStringUTF(jenv, result);
1479 	return (jresult);
1480 }
1481 
1482 /*
1483  * pool_set_binding(3pool) wrapper
1484  */
1485 /*ARGSUSED*/
1486 JNIEXPORT jint JNICALL
1487 Java_com_sun_solaris_service_pools_PoolInternal_pool_1set_1binding(JNIEnv *jenv,
1488     jclass jcls, jstring jpool, jint jidtype, jint jpid) {
1489 	const char *pool;
1490 	int result;
1491 
1492 	pool = (jpool) ? (*jenv)->GetStringUTFChars(jenv, jpool, 0) : NULL;
1493 	result = (int)pool_set_binding(pool, (idtype_t)jidtype, (id_t)jpid);
1494 
1495 	if (pool)
1496 		(*jenv)->ReleaseStringUTFChars(jenv, jpool, pool);
1497 	return ((jint)result);
1498 }
1499 
1500 /*
1501  * pool_get_binding(3pool) wrapper
1502  */
1503 /*ARGSUSED*/
1504 JNIEXPORT jstring JNICALL
1505 Java_com_sun_solaris_service_pools_PoolInternal_pool_1get_1binding(JNIEnv *jenv,
1506     jclass jcls, jint jpid) {
1507 	jstring jresult = NULL;
1508 	const char *result;
1509 
1510 	result = pool_get_binding((pid_t)jpid);
1511 
1512 	if (result)
1513 		jresult = (*jenv)->NewStringUTF(jenv, result);
1514 	free((void *)result);
1515 	return (jresult);
1516 }
1517 
1518 /*
1519  * pool_get_resource_binding(3pool) wrapper
1520  */
1521 /*ARGSUSED*/
1522 JNIEXPORT jstring JNICALL
1523 Java_com_sun_solaris_service_pools_PoolInternal_pool_1get_1resource_1binding(
1524     JNIEnv *jenv, jclass jcls, jstring jtype, jint jpid) {
1525 	jstring jresult = NULL;
1526 	const char *type;
1527 	const char *result;
1528 
1529 	type = (jtype) ? (*jenv)->GetStringUTFChars(jenv, jtype, 0) : NULL;
1530 	result = pool_get_resource_binding(type, (pid_t)jpid);
1531 
1532 	if (result)
1533 		jresult = (*jenv)->NewStringUTF(jenv, result);
1534 	free((void *)result);
1535 	if (type)
1536 		(*jenv)->ReleaseStringUTFChars(jenv, jtype, type);
1537 	return (jresult);
1538 }
1539 
1540 /*
1541  * pool_walk_pools(3pool) wrapper
1542  */
1543 /*ARGSUSED*/
1544 JNIEXPORT jint JNICALL
1545 Java_com_sun_solaris_service_pools_PoolInternal_pool_1walk_1pools(JNIEnv *jenv,
1546     jclass jcls, jlong jconf, jlong jarg, jlong jcallback) {
1547 	int result;
1548 
1549 	result = pool_walk_pools((pool_conf_t *)(uintptr_t)jconf,
1550 	    (void *)(uintptr_t)jarg,
1551 	    (int (*)(pool_conf_t *, pool_t *, void *))(uintptr_t)jcallback);
1552 	return ((jint)result);
1553 }
1554 
1555 /*
1556  * pool_walk_resources(3pool) wrapper
1557  */
1558 /*ARGSUSED*/
1559 JNIEXPORT jint JNICALL
1560 Java_com_sun_solaris_service_pools_PoolInternal_pool_1walk_1resources(
1561     JNIEnv *jenv, jclass jcls, jlong jconf, jlong jpool, jlong jarg,
1562     jlong jcallback) {
1563 	int result;
1564 
1565 	result = pool_walk_resources((pool_conf_t *)(uintptr_t)jconf,
1566 	    (pool_t *)(uintptr_t)jpool, (void *)(uintptr_t)jarg,
1567 	    (int (*)(pool_conf_t *, pool_resource_t *, void *))
1568 	    (uintptr_t)jcallback);
1569 	return ((jint)result);
1570 }
1571 
1572 /*
1573  * pool_walk_components(3pool) wrapper
1574  */
1575 /*ARGSUSED*/
1576 JNIEXPORT jint JNICALL
1577 Java_com_sun_solaris_service_pools_PoolInternal_pool_1walk_1components(
1578     JNIEnv *jenv, jclass jcls, jlong jconf, jlong jresource, jlong jarg,
1579     jlong jcallback) {
1580 	int result;
1581 
1582 	result = pool_walk_components((pool_conf_t *)(uintptr_t)jconf,
1583 	    (pool_resource_t *)(uintptr_t)jresource, (void *)(uintptr_t)jarg,
1584 	    (int (*)(pool_conf_t *, pool_component_t *, void *))
1585 	    (uintptr_t)jcallback);
1586 	return ((jint)result);
1587 }
1588 
1589 /*ARGSUSED*/
1590 JNIEXPORT jint JNICALL
1591 Java_com_sun_solaris_service_pools_Element_walkProps(JNIEnv *env,
1592 	jobject obj, jlong conf, jlong elem, jobject handler, jobject userobj)
1593 {
1594 	struct pool_callback pc;
1595 
1596 	pc.pc_user = userobj;
1597 	pc.pc_handler = handler;
1598 	pc.pc_elem = obj;
1599 	pc.pc_env = env;
1600 	return (pool_walk_properties((pool_conf_t *)*(void**)&conf,
1601 	    (pool_elem_t *)*(void**)&elem, (void *)&pc, pool_property_walker));
1602 }
1603 
1604 /*ARGSUSED*/
1605 static int
1606 pool_property_walker(pool_conf_t *conf, pool_elem_t *pe, const char *name,
1607 	pool_value_t *pv, void *user)
1608 {
1609 	jclass clazz, vclazz;
1610 	jmethodID mgetwalk, mvcon;
1611 	struct pool_callback *pc = (struct pool_callback *)user;
1612 	jobject valueObj;
1613 	pool_value_t *pv_new;
1614 	uint64_t uval;
1615 	int64_t ival;
1616 	double dval;
1617 	uchar_t bval;
1618 	const char *sval;
1619 
1620 	/*
1621 	 * Since we intend to embed our value into a Java Value object
1622 	 * and then reclaim the value when the object is garbage
1623 	 * collected we must create a new pool value and then pass this
1624 	 * to the constructor.  We must not use the pool value which is
1625 	 * passed to us.
1626 	 */
1627 
1628 	if ((pv_new = pool_value_alloc()) == NULL)
1629 		return (PO_FAIL);
1630 	switch (pool_value_get_type(pv)) {
1631 	case POC_UINT:
1632 		(void) pool_value_get_uint64(pv, &uval);
1633 		(void) pool_value_set_uint64(pv_new, uval);
1634 		break;
1635 	case POC_INT:
1636 		(void) pool_value_get_int64(pv, &ival);
1637 		(void) pool_value_set_int64(pv_new, ival);
1638 		break;
1639 	case POC_DOUBLE:
1640 		(void) pool_value_get_double(pv, &dval);
1641 		(void) pool_value_set_double(pv_new, dval);
1642 		break;
1643 	case POC_BOOL:
1644 		(void) pool_value_get_bool(pv, &bval);
1645 		(void) pool_value_set_bool(pv_new, bval);
1646 		break;
1647 	case POC_STRING:
1648 		(void) pool_value_get_string(pv, &sval);
1649 		(void) pool_value_set_string(pv_new, sval);
1650 		break;
1651 	default:
1652 		pool_value_free(pv_new);
1653 		return (PO_FAIL);
1654 	}
1655 	if (pool_value_set_name(pv_new, name) != PO_SUCCESS ||
1656 	    (vclazz = (*pc->pc_env)->FindClass(pc->pc_env,
1657 	    "com/sun/solaris/service/pools/Value")) == NULL ||
1658 	    (mvcon = (*pc->pc_env)->GetMethodID(pc->pc_env, vclazz,
1659 	    "<init>", "(J)V")) == NULL ||
1660 	    (valueObj = (*pc->pc_env)->NewObject(pc->pc_env, vclazz, mvcon,
1661 	    pv_new)) == NULL ||
1662 	    (clazz = (*pc->pc_env)->GetObjectClass(pc->pc_env, pc->pc_handler))
1663 	    == NULL ||
1664 	    (mgetwalk = (*pc->pc_env)->GetMethodID(pc->pc_env,
1665 	    clazz, "walk",
1666 	    "(Lcom/sun/solaris/service/pools/Element;Lcom/sun/solaris/"
1667 	    "service/pools/Value;Ljava/lang/Object;)I")) == NULL)
1668 		return (PO_FAIL);
1669 	return ((*pc->pc_env)->CallIntMethod(pc->pc_env,
1670 		pc->pc_handler, mgetwalk, pc->pc_elem, valueObj, pc->pc_user));
1671 }
1672 
1673 /*ARGSUSED*/
1674 JNIEXPORT jlong JNICALL
1675 Java_com_sun_solaris_service_pools_Value_getLongValue(JNIEnv *jenv,
1676 	jclass class, jlong pointer)
1677 {
1678 	int64_t arg2;
1679 	int result;
1680 
1681 	result =
1682 	    pool_value_get_int64((pool_value_t *)(uintptr_t)pointer, &arg2);
1683 
1684 	if (result != PO_SUCCESS) { /* it could be a uint64 */
1685 		result =
1686 		    pool_value_get_uint64((pool_value_t *)(uintptr_t)pointer,
1687 		    (uint64_t *)&arg2);
1688 		if (result != PO_SUCCESS) {
1689 			throw_pe(jenv);
1690 		}
1691 		/*
1692 		 * Unfortunately, Java has no unsigned types, so we lose some
1693 		 * precision by forcing the top bit clear
1694 		 */
1695 		arg2 &= 0x7fffffffffffffffULL;
1696 	}
1697 	return ((jlong)arg2);
1698 }
1699 
1700 /*ARGSUSED*/
1701 JNIEXPORT jstring JNICALL
1702 Java_com_sun_solaris_service_pools_Value_getStringValue(JNIEnv *jenv,
1703 	jclass class, jlong pointer)
1704 {
1705 	const char *arg2;
1706 	int result;
1707 
1708 	result =
1709 	    pool_value_get_string((pool_value_t *)(uintptr_t)pointer, &arg2);
1710 	if (result != PO_SUCCESS)
1711 		throw_pe(jenv);
1712 	return ((*jenv)->NewStringUTF(jenv, arg2));
1713 }
1714 
1715 /*ARGSUSED*/
1716 JNIEXPORT jboolean JNICALL
1717 Java_com_sun_solaris_service_pools_Value_getBoolValue(JNIEnv *jenv,
1718 	jclass class, jlong pointer)
1719 {
1720 	uchar_t arg2;
1721 	int result;
1722 
1723 	result = pool_value_get_bool((pool_value_t *)(uintptr_t)pointer, &arg2);
1724 
1725 	if (result != PO_SUCCESS) {
1726 		throw_pe(jenv);
1727 	}
1728 	if (arg2 == PO_TRUE)
1729 		return (JNI_TRUE);
1730 	else
1731 		return (JNI_FALSE);
1732 }
1733 
1734 /*ARGSUSED*/
1735 JNIEXPORT jdouble JNICALL
1736 Java_com_sun_solaris_service_pools_Value_getDoubleValue(JNIEnv *jenv,
1737 	jclass class, jlong pointer)
1738 {
1739 	double arg2;
1740 	int result;
1741 
1742 	result =
1743 	    pool_value_get_double((pool_value_t *)(uintptr_t)pointer, &arg2);
1744 
1745 	if (result != PO_SUCCESS) {
1746 		throw_pe(jenv);
1747 	}
1748 	return ((jdouble)arg2);
1749 }
1750 
1751 /*ARGSUSED*/
1752 JNIEXPORT jobject JNICALL
1753 Java_com_sun_solaris_service_pools_Value_getUnsignedInt64Value(JNIEnv *jenv,
1754     jclass class, jlong pointer)
1755 {
1756 	uint64_t arg2;
1757 	int result;
1758 
1759 	result =
1760 	    pool_value_get_uint64((pool_value_t *)(uintptr_t)pointer, &arg2);
1761 
1762 	if (result != PO_SUCCESS) {
1763 		throw_pe(jenv);
1764 	}
1765 	return (makeUnsignedInt64(jenv, arg2));
1766 }
1767 
1768 /*ARGSUSED*/
1769 JNIEXPORT jobject JNICALL
1770 Java_com_sun_solaris_service_pools_HRTime_timestamp(JNIEnv *env, jobject obj)
1771 {
1772 	return (makeUnsignedInt64(env, gethrtime()));
1773 }
1774 
1775 /*
1776  * Cache class, method, and field IDs.
1777  */
1778 /*ARGSUSED*/
1779 JNIEXPORT void JNICALL
1780 Java_com_sun_solaris_service_pools_PoolInternal_init(JNIEnv *env, jclass clazz)
1781 {
1782 	jclass ui64class_lref;
1783 
1784 	if (!(ui64class_lref = (*env)->FindClass(env,
1785 	    "com/sun/solaris/service/pools/UnsignedInt64")))
1786 		return; /* exception thrown */
1787 	if (!(ui64class = (*env)->NewGlobalRef(env, ui64class_lref)))
1788 		return; /* exception thrown */
1789 	ui64cons_mid = (*env)->GetMethodID(env, ui64class, "<init>", "([B)V");
1790 }
1791