xref: /titanic_52/usr/src/cmd/pools/poold/libjpool/jpool.c (revision 554ff184129088135ad2643c1c9832174a17be88)
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 2004 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 **)jreslist;
384 	uint_t *numres = (uint_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, jlong jstatep) {
396 	return ((jint)pool_get_status((int *)jstatep));
397 }
398 
399 /*
400  * pool_set_status(3pool) wrapper
401  */
402 /*ARGSUSED*/
403 JNIEXPORT jint JNICALL
404 Java_com_sun_solaris_service_pools_PoolInternal_pool_1set_1status(JNIEnv *jenv,
405     jclass jcls, jint jstate) {
406 	return ((jint)pool_set_status((int)jstate));
407 }
408 
409 /*
410  * pool_conf_alloc(3pool) wrapper
411  */
412 /*ARGSUSED*/
413 JNIEXPORT jlong JNICALL
414 Java_com_sun_solaris_service_pools_PoolInternal_pool_1conf_1alloc(JNIEnv *jenv,
415     jclass jcls) {
416 	return ((jlong)(uintptr_t)pool_conf_alloc());
417 }
418 
419 /*
420  * pool_conf_free(3pool) wrapper
421  */
422 /*ARGSUSED*/
423 JNIEXPORT void JNICALL
424 Java_com_sun_solaris_service_pools_PoolInternal_pool_1conf_1free(JNIEnv *jenv,
425     jclass jcls, jlong jconf) {
426 	pool_conf_free((pool_conf_t *)jconf);
427 }
428 
429 /*
430  * pool_conf_status(3pool) wrapper
431  */
432 /*ARGSUSED*/
433 JNIEXPORT jint JNICALL
434 Java_com_sun_solaris_service_pools_PoolInternal_pool_1conf_1status(JNIEnv *jenv,
435     jclass jcls, jlong jconf) {
436 	return ((jint)pool_conf_status((pool_conf_t *)jconf));
437 }
438 
439 /*
440  * pool_conf_close(3pool) wrapper
441  */
442 /*ARGSUSED*/
443 JNIEXPORT jint JNICALL
444 Java_com_sun_solaris_service_pools_PoolInternal_pool_1conf_1close(JNIEnv *jenv,
445     jclass jcls, jlong jconf) {
446 	return ((jint)pool_conf_close((pool_conf_t *)jconf));
447 }
448 
449 /*
450  * pool_conf_remove(3pool) wrapper
451  */
452 /*ARGSUSED*/
453 JNIEXPORT jint JNICALL
454 Java_com_sun_solaris_service_pools_PoolInternal_pool_1conf_1remove(JNIEnv *jenv,
455     jclass jcls, jlong jconf) {
456 	return ((jint)pool_conf_remove((pool_conf_t *)jconf));
457 }
458 
459 /*
460  * pool_conf_open(3pool) wrapper
461  */
462 /*ARGSUSED*/
463 JNIEXPORT jint JNICALL
464 Java_com_sun_solaris_service_pools_PoolInternal_pool_1conf_1open(JNIEnv *jenv,
465     jclass jcls, jlong jconf, jstring jlocation, jint jflags) {
466 	const char *location;
467 	int result;
468 
469 	location = (jlocation) ? (*jenv)->GetStringUTFChars(jenv,
470 	    jlocation, 0) : NULL;
471 	result = (int)pool_conf_open((pool_conf_t *)jconf, location,
472 	    (int)jflags);
473 
474 	if (location)
475 		(*jenv)->ReleaseStringUTFChars(jenv, jlocation, location);
476 	return ((jint)result);
477 }
478 
479 /*
480  * pool_conf_rollback(3pool) wrapper
481  */
482 /*ARGSUSED*/
483 JNIEXPORT jint JNICALL
484 Java_com_sun_solaris_service_pools_PoolInternal_pool_1conf_1rollback(
485     JNIEnv *jenv, jclass jcls, jlong jconf) {
486 	return ((jint)pool_conf_rollback((pool_conf_t *)jconf));
487 }
488 
489 /*
490  * pool_conf_commit(3pool) wrapper
491  */
492 /*ARGSUSED*/
493 JNIEXPORT jint JNICALL
494 Java_com_sun_solaris_service_pools_PoolInternal_pool_1conf_1commit(JNIEnv *jenv,
495     jclass jcls, jlong jconf, jint jactive) {
496 	return ((jint)pool_conf_commit((pool_conf_t *)jconf, (int)jactive));
497 }
498 
499 /*
500  * pool_conf_export(3pool) wrapper
501  */
502 /*ARGSUSED*/
503 JNIEXPORT jint JNICALL
504 Java_com_sun_solaris_service_pools_PoolInternal_pool_1conf_1export(JNIEnv *jenv,
505     jclass jcls, jlong jconf, jstring jlocation, jint jformat) {
506 	const char *location;
507 	int result;
508 
509 	location = (jlocation) ? (*jenv)->GetStringUTFChars(jenv,
510 	    jlocation, 0) : NULL;
511 	result = (int)pool_conf_export((pool_conf_t *)jconf,
512 	    location, (pool_export_format_t)jformat);
513 
514 	if (location)
515 		(*jenv)->ReleaseStringUTFChars(jenv, jlocation, location);
516 	return ((jint)result);
517 }
518 
519 /*
520  * pool_conf_validate(3pool) wrapper
521  */
522 /*ARGSUSED*/
523 JNIEXPORT jint JNICALL
524 Java_com_sun_solaris_service_pools_PoolInternal_pool_1conf_1validate(
525     JNIEnv *jenv, jclass jcls, jlong jconf, jint jlevel) {
526 	return ((jint)pool_conf_validate((pool_conf_t *)jconf,
527 		    (pool_valid_level_t)jlevel));
528 }
529 
530 /*
531  * pool_conf_update(3pool) wrapper
532  */
533 /*ARGSUSED*/
534 JNIEXPORT jint JNICALL
535 Java_com_sun_solaris_service_pools_PoolInternal_pool_1conf_1update(JNIEnv *jenv,
536     jclass jcls, jlong jconf) {
537 	int changed;
538 	int result;
539 
540 	result = pool_conf_update((pool_conf_t *)jconf, &changed);
541 
542 	if (result != PO_SUCCESS) {
543 		throw_pe(jenv);
544 	}
545 
546 	return ((jint)changed);
547 }
548 
549 /*
550  * pool_get_pool(3pool) wrapper
551  */
552 /*ARGSUSED*/
553 JNIEXPORT jlong JNICALL
554 Java_com_sun_solaris_service_pools_PoolInternal_pool_1get_1pool(JNIEnv *jenv,
555     jclass jcls, jlong jconf, jstring jname) {
556 	const char *name;
557 	pool_t *result;
558 
559 	name = (jname) ? (*jenv)->GetStringUTFChars(jenv, jname, 0) :
560 	    NULL;
561 	result = (pool_t *)pool_get_pool((pool_conf_t *)jconf, name);
562 
563 	if (name)
564 		(*jenv)->ReleaseStringUTFChars(jenv, jname, name);
565 	return ((jlong)(uintptr_t)result);
566 }
567 
568 /*
569  * pool_query_pools(3pool) wrapper
570  */
571 /*ARGSUSED*/
572 JNIEXPORT jobject JNICALL
573 Java_com_sun_solaris_service_pools_PoolInternal_pool_1query_1pools(JNIEnv *jenv,
574     jclass jcls, jlong jconf, jobject jprops) {
575 	pool_value_t **props;
576 	pool_t **result;
577 	jclass Lclazz;
578 	jmethodID Lsize;
579 	jint size;
580 	uint_t nelem;
581 	int i;
582 
583 
584 	/*
585 	 * Initialize the target parameter for case when input is null
586 	 */
587 	props = NULL;
588 	if (jprops != NULL) {
589 		Lclazz = (*jenv)->GetObjectClass(jenv, jprops);
590 		Lsize = (*jenv)->GetMethodID(jenv, Lclazz, "size", "()I");
591 		size = (*jenv)->CallIntMethod(jenv, jprops, Lsize);
592 
593 		if (size != 0) {
594 			jmethodID Lget;
595 
596 			Lget = (*jenv)->GetMethodID(jenv, Lclazz, "get",
597 			    "(I)Ljava/lang/Object;");
598 			/*
599 			 * Allocate space for the props array
600 			 */
601 
602 			if ((props = calloc(size + 1, sizeof (pool_value_t *)))
603 			    == NULL) {
604 				throwException(jenv, "java/lang/Exception",
605 				    "Could not allocate props array");
606 				return (NULL);
607 			}
608 			/*
609 			 * Copy in the array
610 			 */
611 			for (i = 0; i < size; i++) {
612 				jobject aVal;
613 				jclass Vclazz;
614 				jfieldID Vthis;
615 				jlong this;
616 
617 				aVal = (*jenv)->CallObjectMethod(jenv, jprops,
618 				    Lget, (jint) i);
619 				Vclazz = (*jenv)->GetObjectClass(jenv, aVal);
620 				Vthis = (*jenv)->GetFieldID(jenv, Vclazz,
621 				    "_this", "J");
622 				this = (*jenv)->GetLongField(jenv, aVal, Vthis);
623 				props[i] = (pool_value_t *)this;
624 			}
625 		}
626 	}
627 	result = pool_query_pools((pool_conf_t *)jconf, &nelem,
628 	    props);
629 	free(props);
630 	return (copyArray(jenv, (void **)result));
631 }
632 
633 /*
634  * pool_get_resource(3pool) wrapper
635  */
636 /*ARGSUSED*/
637 JNIEXPORT jlong JNICALL
638 Java_com_sun_solaris_service_pools_PoolInternal_pool_1get_1resource(
639     JNIEnv *jenv, jclass jcls, jlong jconf, jstring jtype, jstring jname) {
640 	const char *type;
641 	const char *name;
642 	pool_resource_t *result;
643 
644 	type = (jtype) ? (*jenv)->GetStringUTFChars(jenv, jtype, 0) :
645 	    NULL;
646 	name = (jname) ? (*jenv)->GetStringUTFChars(jenv, jname, 0) :
647 	    NULL;
648 	result = pool_get_resource((pool_conf_t *)jconf, type, name);
649 
650 	if (type)
651 		(*jenv)->ReleaseStringUTFChars(jenv, jtype, type);
652 	if (name)
653 		(*jenv)->ReleaseStringUTFChars(jenv, jname, name);
654 	return ((jlong)(uintptr_t)result);
655 }
656 
657 /*
658  * pool_query_resources(3pool) wrapper
659  */
660 /*ARGSUSED*/
661 JNIEXPORT jobject JNICALL
662 Java_com_sun_solaris_service_pools_PoolInternal_pool_1query_1resources(
663     JNIEnv *jenv, jclass jcls, jlong jconf, jobject jprops) {
664 	pool_value_t **props;
665 	pool_resource_t **result;
666 	jclass Lclazz;
667 	jmethodID Lsize;
668 	jint size;
669 	uint_t nelem;
670 	int i;
671 
672 	/*
673 	 * Initialize the target parameter for case when input is null
674 	 */
675 	props = NULL;
676 	if (jprops != NULL) {
677 		Lclazz = (*jenv)->GetObjectClass(jenv, jprops);
678 		Lsize = (*jenv)->GetMethodID(jenv, Lclazz, "size", "()I");
679 		size = (*jenv)->CallIntMethod(jenv, jprops, Lsize);
680 
681 		if (size != 0) {
682 			jmethodID Lget;
683 
684 			Lget = (*jenv)->GetMethodID(jenv, Lclazz, "get",
685 			    "(I)Ljava/lang/Object;");
686 			/*
687 			 * Allocate space for the props array
688 			 */
689 			if ((props = calloc(size + 1, sizeof (pool_value_t *)))
690 			    == NULL) {
691 				throwException(jenv, "java/lang/Exception",
692 				    "Could not allocate props array");
693 				return (NULL);
694 			}
695 			/*
696 			 * Copy in the array
697 			 */
698 			for (i = 0; i < size; i++) {
699 				jobject aVal;
700 				jclass Vclazz;
701 				jfieldID Vthis;
702 				jlong this;
703 
704 
705 				aVal = (*jenv)->CallObjectMethod(jenv, jprops,
706 				    Lget, (jint) i);
707 				Vclazz = (*jenv)->GetObjectClass(jenv, aVal);
708 				Vthis = (*jenv)->GetFieldID(jenv, Vclazz,
709 				    "_this", "J");
710 				this = (*jenv)->GetLongField(jenv, aVal, Vthis);
711 				props[i] = (pool_value_t *)this;
712 			}
713 		}
714 	}
715 	result = pool_query_resources((pool_conf_t *)jconf, &nelem,
716 	    props);
717 	free(props);
718 	return (copyArray(jenv, (void *)result));
719 }
720 
721 /*
722  * pool_query_components(3pool) wrapper
723  */
724 /*ARGSUSED*/
725 JNIEXPORT jobject JNICALL
726 Java_com_sun_solaris_service_pools_PoolInternal_pool_1query_1components(
727     JNIEnv *jenv, jclass jcls, jlong jconf, jobject jprops) {
728 	pool_value_t **props;
729 	pool_component_t **result;
730 	jclass Lclazz;
731 	jmethodID Lsize;
732 	jint size;
733 	uint_t nelem;
734 	int i;
735 
736 	/*
737 	 * Initialize the target parameter for case when input is null
738 	 */
739 	props = NULL;
740 	if (jprops != NULL) {
741 		Lclazz = (*jenv)->GetObjectClass(jenv, jprops);
742 		Lsize = (*jenv)->GetMethodID(jenv, Lclazz, "size", "()I");
743 		size = (*jenv)->CallIntMethod(jenv, jprops, Lsize);
744 
745 		if (size != 0) {
746 			jmethodID Lget;
747 
748 			Lget = (*jenv)->GetMethodID(jenv, Lclazz, "get",
749 			    "(I)Ljava/lang/Object;");
750 			/*
751 			 * Allocate space for the props array
752 			 */
753 
754 			if ((props = calloc(size + 1, sizeof (pool_value_t *)))
755 			    == NULL) {
756 				throwException(jenv, "java/lang/Exception",
757 				    "Could not allocate props array");
758 				return (NULL);
759 			}
760 			/*
761 			 * Copy in the array
762 			 */
763 			for (i = 0; i < size; i++) {
764 				jobject aVal;
765 				jclass Vclazz;
766 				jfieldID Vthis;
767 				jlong this;
768 
769 				aVal = (*jenv)->CallObjectMethod(jenv, jprops,
770 				    Lget, (jint) i);
771 				Vclazz = (*jenv)->GetObjectClass(jenv, aVal);
772 				Vthis = (*jenv)->GetFieldID(jenv, Vclazz,
773 				    "_this", "J");
774 				this = (*jenv)->GetLongField(jenv, aVal, Vthis);
775 				props[i] = (pool_value_t *)this;
776 			}
777 		}
778 	}
779 	result = pool_query_components((pool_conf_t *)jconf, &nelem,
780 	    props);
781 	free(props);
782 	return (copyArray(jenv, (void **)result));
783 }
784 
785 /*
786  * pool_conf_location(3pool) wrapper
787  */
788 /*ARGSUSED*/
789 JNIEXPORT jstring JNICALL
790 Java_com_sun_solaris_service_pools_PoolInternal_pool_1conf_1location(
791     JNIEnv *jenv, jclass jcls, jlong jconf) {
792 	jstring jresult = NULL;
793 	const char *result;
794 
795 	result = pool_conf_location((pool_conf_t *)jconf);
796 
797 	if (result)
798 		jresult = (*jenv)->NewStringUTF(jenv, result);
799 	return (jresult);
800 }
801 
802 /*
803  * pool_conf_info(3pool) wrapper
804  */
805 /*ARGSUSED*/
806 JNIEXPORT jstring JNICALL
807 Java_com_sun_solaris_service_pools_PoolInternal_pool_1conf_1info(JNIEnv *jenv,
808     jclass jcls, jlong jconf, jint jflags) {
809 	jstring jresult = NULL;
810 	const char *result;
811 
812 	result = pool_conf_info((pool_conf_t *)jconf, (int)jflags);
813 
814 	if (result)
815 		jresult = (*jenv)->NewStringUTF(jenv, result);
816 	free((void *)result);
817 	return (jresult);
818 }
819 
820 /*
821  * pool_resource_create(3pool) wrapper
822  */
823 /*ARGSUSED*/
824 JNIEXPORT jlong JNICALL
825 Java_com_sun_solaris_service_pools_PoolInternal_pool_1resource_1create(
826     JNIEnv *jenv, jclass jcls, jlong jconf, jstring jtype, jstring jname) {
827 	const char *type;
828 	const char *name;
829 	pool_resource_t *result;
830 
831 	type = (jtype) ? (*jenv)->GetStringUTFChars(jenv, jtype, 0) :
832 	    NULL;
833 	name = (jname) ? (*jenv)->GetStringUTFChars(jenv, jname, 0) :
834 	    NULL;
835 	result = pool_resource_create((pool_conf_t *)jconf, type, name);
836 
837 	if (type)
838 		(*jenv)->ReleaseStringUTFChars(jenv, jtype, type);
839 	if (name)
840 		(*jenv)->ReleaseStringUTFChars(jenv, jname, name);
841 	return ((jlong)(uintptr_t)result);
842 }
843 
844 /*
845  * pool_resource_destroy(3pool) wrapper
846  */
847 /*ARGSUSED*/
848 JNIEXPORT jint JNICALL
849 Java_com_sun_solaris_service_pools_PoolInternal_pool_1resource_1destroy(
850     JNIEnv *jenv, jclass jcls, jlong jconf, jlong jresource) {
851 	return ((jint)pool_resource_destroy((pool_conf_t *)jconf,
852 		    (pool_resource_t *)jresource));
853 }
854 
855 /*
856  * pool_resource_transfer(3pool) wrapper
857  */
858 /*ARGSUSED*/
859 JNIEXPORT jint JNICALL
860 Java_com_sun_solaris_service_pools_PoolInternal_pool_1resource_1transfer(
861     JNIEnv *jenv, jclass jcls, jlong jconf, jlong jsource, jlong jtarget,
862     jlong jsize) {
863 	return (pool_resource_transfer((pool_conf_t *)jconf,
864 		    (pool_resource_t *)jsource, (pool_resource_t *)jtarget,
865 		    (uint64_t)jsize));
866 }
867 
868 /*
869  * pool_resource_xtransfer(3pool) wrapper
870  */
871 /*ARGSUSED*/
872 JNIEXPORT jint JNICALL
873 Java_com_sun_solaris_service_pools_PoolInternal_pool_1resource_1xtransfer(
874     JNIEnv *jenv, jclass jcls, jlong jconf, jlong jsource, jlong jtarget,
875     jobject jcomponents) {
876 	pool_component_t **components;
877 	int result;
878 	jclass Lclazz;
879 	jmethodID Lsize;
880 	jint size;
881 
882 	/*
883 	 * Initialize the target parameter for case when input is null
884 	 */
885 	components = NULL;
886 	if (jcomponents != NULL) {
887 		Lclazz = (*jenv)->GetObjectClass(jenv, jcomponents);
888 		Lsize = (*jenv)->GetMethodID(jenv, Lclazz, "size", "()I");
889 		size = (*jenv)->CallIntMethod(jenv, jcomponents, Lsize);
890 
891 		if (size != 0) {
892 			jmethodID Lget;
893 			int i;
894 
895 			Lget = (*jenv)->GetMethodID(jenv,
896 			    Lclazz, "get", "(I)Ljava/lang/Object;");
897 			/* Allocate space for the components array */
898 
899 			if ((components = calloc(size + 1,
900 			    sizeof (pool_component_t *))) == NULL) {
901 				throwException(jenv, "java/lang/Exception",
902 				    "Could not allocate component array");
903 				return (NULL);
904 			}
905 			/*
906 			 * Copy in the array
907 			 */
908 			for (i = 0; i < size; i++) {
909 				jobject aVal;
910 				jclass Vclazz;
911 				jlong this;
912 				jmethodID Vthis;
913 
914 				aVal = (*jenv)->CallObjectMethod(jenv,
915 				    jcomponents, Lget, (jint) i);
916 				Vclazz = (*jenv)->GetObjectClass(jenv,
917 				    aVal);
918 				Vthis = (*jenv)->GetMethodID(jenv,
919 				    Vclazz, "getComponent", "()J");
920 				this = (*jenv)->CallLongMethod(jenv,
921 				    aVal, Vthis);
922 				components[i] = (pool_component_t *)this;
923 			}
924 		}
925 	}
926 	result = (int)pool_resource_xtransfer((pool_conf_t *)jconf,
927 	    (pool_resource_t *)jsource, (pool_resource_t *)jtarget,
928 	    components);
929 	free(components);
930 
931 	return ((jint)result);
932 }
933 
934 /*
935  * pool_query_resource_components(3pool) wrapper
936  */
937 /*ARGSUSED*/
938 JNIEXPORT jobject JNICALL
939 Java_com_sun_solaris_service_pools_PoolInternal_pool_1query_1resource_\
940 1components(JNIEnv *jenv, jclass jcls, jlong jconf, jlong jresource,
941     jobject jprops) {
942 	pool_value_t **props;
943 	pool_component_t **result;
944 	jclass Lclazz;
945 	jmethodID Lsize;
946 	uint_t nelem;
947 	jint size;
948 
949 	/*
950 	 * Initialize the target parameter for case when input is null
951 	 */
952 	props = NULL;
953 	if (jprops != NULL) {
954 		Lclazz = (*jenv)->GetObjectClass(jenv, jprops);
955 		Lsize = (*jenv)->GetMethodID(jenv, Lclazz, "size", "()I");
956 		size = (*jenv)->CallIntMethod(jenv, jprops, Lsize);
957 
958 		if (size != 0) {
959 			jmethodID Lget;
960 			int i;
961 
962 			Lget = (*jenv)->GetMethodID(jenv, Lclazz, "get",
963 			    "(I)Ljava/lang/Object;");
964 			/*
965 			 * Allocate space for the props array
966 			 */
967 			if ((props = calloc(size + 1, sizeof (pool_value_t *)))
968 			    == NULL) {
969 				throwException(jenv, "java/lang/Exception",
970 				    "Could not allocate props array");
971 				return (NULL);
972 			}
973 			/*
974 			 * Copy in the array
975 			 */
976 			for (i = 0; i < size; i++) {
977 				jobject aVal;
978 				jclass Vclazz;
979 				jfieldID Vthis;
980 				jlong this;
981 
982 				aVal = (*jenv)->CallObjectMethod(jenv, jprops,
983 				    Lget, (jint) i);
984 				Vclazz = (*jenv)->GetObjectClass(jenv, aVal);
985 				Vthis = (*jenv)->GetFieldID(jenv, Vclazz,
986 				    "_this", "J");
987 				this = (*jenv)->GetLongField(jenv, aVal, Vthis);
988 				props[i] = (pool_value_t *)this;
989 			}
990 		}
991 	}
992 	result = pool_query_resource_components((pool_conf_t *)jconf,
993 	    (pool_resource_t *)jresource, &nelem, props);
994 	free(props);
995 	return (copyArray(jenv, (void **)result));
996 }
997 
998 /*
999  * pool_resource_info(3pool) wrapper
1000  */
1001 /*ARGSUSED*/
1002 JNIEXPORT jstring JNICALL
1003 Java_com_sun_solaris_service_pools_PoolInternal_pool_1resource_1info(
1004     JNIEnv *jenv, jclass jcls, jlong jconf, jlong jresource, jint jflags) {
1005 	jstring jresult = NULL;
1006 	const char *result;
1007 
1008 	result = (char *)pool_resource_info((pool_conf_t *)jconf,
1009 	    (pool_resource_t *)jresource, (int)jflags);
1010 
1011 	if (result)
1012 		jresult = (*jenv)->NewStringUTF(jenv, result);
1013 	free((void *)result);
1014 	return (jresult);
1015 }
1016 
1017 /*
1018  * pool_create(3pool) wrapper
1019  */
1020 /*ARGSUSED*/
1021 JNIEXPORT jlong JNICALL
1022 Java_com_sun_solaris_service_pools_PoolInternal_pool_1create(JNIEnv *jenv,
1023     jclass jcls, jlong jconf, jstring jname) {
1024 	const char *name;
1025 	pool_t *result;
1026 
1027 	name = (jname) ? (*jenv)->GetStringUTFChars(jenv, jname, 0) :
1028 	    NULL;
1029 	result = pool_create((pool_conf_t *)jconf, name);
1030 
1031 	if (name)
1032 		(*jenv)->ReleaseStringUTFChars(jenv, jname, name);
1033 	return ((jlong)(uintptr_t)result);
1034 }
1035 
1036 /*
1037  * pool_destroy(3pool) wrapper
1038  */
1039 /*ARGSUSED*/
1040 JNIEXPORT jint JNICALL
1041 Java_com_sun_solaris_service_pools_PoolInternal_pool_1destroy(JNIEnv *jenv,
1042     jclass jcls, jlong jconf, jlong jpool) {
1043 	return ((jint)pool_destroy((pool_conf_t *)jconf, (pool_t *)jpool));
1044 }
1045 
1046 /*
1047  * pool_associate(3pool) wrapper
1048  */
1049 /*ARGSUSED*/
1050 JNIEXPORT jint JNICALL
1051 Java_com_sun_solaris_service_pools_PoolInternal_pool_1associate(JNIEnv *jenv,
1052     jclass jcls, jlong jconf, jlong jpool, jlong jresource) {
1053 	return ((jint)pool_associate((pool_conf_t *)jconf, (pool_t *)jpool,
1054 	    (pool_resource_t *)jresource));
1055 }
1056 
1057 /*
1058  * pool_dissociate(3pool) wrapper
1059  */
1060 /*ARGSUSED*/
1061 JNIEXPORT jint JNICALL
1062 Java_com_sun_solaris_service_pools_PoolInternal_pool_1dissociate(JNIEnv *jenv,
1063     jclass jcls, jlong jconf, jlong jpool, jlong jresource) {
1064 	return ((jint)pool_dissociate((pool_conf_t *)jconf, (pool_t *)jpool,
1065 	    (pool_resource_t *)jresource));
1066 }
1067 
1068 /*
1069  * pool_info(3pool) wrapper
1070  */
1071 /*ARGSUSED*/
1072 JNIEXPORT jstring JNICALL
1073 Java_com_sun_solaris_service_pools_PoolInternal_pool_1info(JNIEnv *jenv,
1074     jclass jcls, jlong jconf, jlong jpool, jint jflags) {
1075 	jstring jresult = NULL;
1076 	const char *result;
1077 
1078 	result = pool_info((pool_conf_t *)jconf,
1079 	    (pool_t *)jpool, (int)jflags);
1080 
1081 	if (result)
1082 		jresult = (*jenv)->NewStringUTF(jenv, result);
1083 	free((void *)result);
1084 	return (jresult);
1085 }
1086 
1087 /*
1088  * pool_query_pool_resources(3pool) wrapper
1089  */
1090 /*ARGSUSED*/
1091 JNIEXPORT jobject JNICALL
1092 Java_com_sun_solaris_service_pools_PoolInternal_pool_1query_1pool_1resources(
1093     JNIEnv *jenv, jclass jcls, jlong jconf, jlong jpool, jobject jprops) {
1094 	pool_value_t **props;
1095 	pool_resource_t **result;
1096 	jclass Lclazz;
1097 	jmethodID Lsize;
1098 	uint_t nelem;
1099 	jint size;
1100 
1101 	/*
1102 	 * Initialize the target parameter for case when input is null
1103 	 */
1104 	props = NULL;
1105 	if (jprops != NULL) {
1106 		Lclazz = (*jenv)->GetObjectClass(jenv, jprops);
1107 		Lsize = (*jenv)->GetMethodID(jenv, Lclazz, "size", "()I");
1108 		size = (*jenv)->CallIntMethod(jenv, jprops, Lsize);
1109 
1110 		if (size != 0) {
1111 			jmethodID Lget;
1112 			int i;
1113 
1114 			Lget = (*jenv)->GetMethodID(jenv,
1115 			    Lclazz, "get", "(I)Ljava/lang/Object;");
1116 			/*
1117 			 * Allocate space for the props array
1118 			 */
1119 
1120 			if ((props = calloc(size + 1, sizeof (pool_value_t *)))
1121 			    == NULL) {
1122 				throwException(jenv, "java/lang/Exception",
1123 				    "Could not allocate props array");
1124 				return (NULL);
1125 			}
1126 			/*
1127 			 * Copy in the array
1128 			 */
1129 			for (i = 0; i < size; i++) {
1130 				jobject aVal;
1131 				jclass Vclazz;
1132 				jfieldID Vthis;
1133 				jlong this;
1134 
1135 				aVal = (*jenv)->CallObjectMethod(jenv, jprops,
1136 				    Lget, (jint) i);
1137 				Vclazz = (*jenv)->GetObjectClass(jenv, aVal);
1138 				Vthis = (*jenv)->GetFieldID(jenv, Vclazz,
1139 				    "_this", "J");
1140 				this = (*jenv)->GetLongField(jenv, aVal, Vthis);
1141 				props[i] = (pool_value_t *)this;
1142 			}
1143 		}
1144 	}
1145 	result = pool_query_pool_resources((pool_conf_t *)jconf,
1146 	    (pool_t *)jpool, &nelem, props);
1147 	free(props);
1148 	return (copyArray(jenv, (void **)result));
1149 }
1150 
1151 /*
1152  * pool_get_owning_resource(3pool) wrapper
1153  */
1154 /*ARGSUSED*/
1155 JNIEXPORT jlong JNICALL
1156 Java_com_sun_solaris_service_pools_PoolInternal_pool_1get_1owning_1resource(
1157     JNIEnv *jenv, jclass jcls, jlong jconf, jlong jcomponent) {
1158 	return ((jlong)(uintptr_t)pool_get_owning_resource((pool_conf_t *)jconf,
1159 	    (pool_component_t *)jcomponent));
1160 }
1161 
1162 /*
1163  * pool_component_info(3pool) wrapper
1164  */
1165 /*ARGSUSED*/
1166 JNIEXPORT jstring JNICALL
1167 Java_com_sun_solaris_service_pools_PoolInternal_pool_1component_1info(
1168     JNIEnv *jenv, jclass jcls, jlong jconf, jlong jcomponent, jint jflags) {
1169 	jstring jresult = NULL;
1170 	const char *result;
1171 
1172 	result = pool_component_info((pool_conf_t *)jconf,
1173 	    (pool_component_t *)jcomponent, (int)jflags);
1174 
1175 	if (result)
1176 		jresult = (*jenv)->NewStringUTF(jenv, result);
1177 	free((void *)result);
1178 	return (jresult);
1179 }
1180 
1181 /*
1182  * pool_get_property(3pool) wrapper
1183  */
1184 /*ARGSUSED*/
1185 JNIEXPORT jint JNICALL
1186 Java_com_sun_solaris_service_pools_PoolInternal_pool_1get_1property(
1187     JNIEnv *jenv, jclass jcls, jlong jconf, jlong jelem, jstring jname,
1188     jlong jproperty) {
1189 	const char *name;
1190 	int result;
1191 
1192 	name = (jname) ? (*jenv)->GetStringUTFChars(jenv, jname, 0) :
1193 	    NULL;
1194 	result = pool_get_property((pool_conf_t *)jconf,
1195 	    (pool_elem_t *)jelem, name, (pool_value_t *)jproperty);
1196 
1197 	if (name)
1198 		(*jenv)->ReleaseStringUTFChars(jenv, jname, name);
1199 	return ((jint)result);
1200 }
1201 
1202 /*
1203  * pool_put_property(3pool) wrapper
1204  */
1205 /*ARGSUSED*/
1206 JNIEXPORT jint JNICALL
1207 Java_com_sun_solaris_service_pools_PoolInternal_pool_1put_1property(
1208     JNIEnv *jenv, jclass jcls, jlong jconf, jlong jelem, jstring jname,
1209     jlong jvalue) {
1210 	const char *name;
1211 	int result;
1212 
1213 	name = (jname) ? (*jenv)->GetStringUTFChars(jenv, jname, 0) : NULL;
1214 	result = (int)pool_put_property((pool_conf_t *)jconf,
1215 	    (pool_elem_t *)jelem, name, (pool_value_t *)jvalue);
1216 
1217 	if (name)
1218 		(*jenv)->ReleaseStringUTFChars(jenv, jname, name);
1219 	return ((jint)result);
1220 }
1221 
1222 /*
1223  * pool_rm_property(3pool) wrapper
1224  */
1225 /*ARGSUSED*/
1226 JNIEXPORT jint JNICALL
1227 Java_com_sun_solaris_service_pools_PoolInternal_pool_1rm_1property(JNIEnv *jenv,
1228     jclass jcls, jlong jconf, jlong jelem, jstring jname) {
1229 	const char *name;
1230 	int result;
1231 
1232 	name = (jname) ? (*jenv)->GetStringUTFChars(jenv, jname, 0) : NULL;
1233 	result = pool_rm_property((pool_conf_t *)jconf, (pool_elem_t *)jelem,
1234 	    name);
1235 
1236 	if (name)
1237 		(*jenv)->ReleaseStringUTFChars(jenv, jname, name);
1238 	return ((jint)result);
1239 }
1240 
1241 /*
1242  * pool_walk_properties(3pool) wrapper
1243  */
1244 /*ARGSUSED*/
1245 JNIEXPORT jint JNICALL
1246 Java_com_sun_solaris_service_pools_PoolInternal_pool_1walk_1properties(
1247     JNIEnv *jenv, jclass jcls, jlong jconf, jlong jelem, jlong jarg,
1248     jlong jcallback) {
1249 	int result;
1250 
1251 	result = (int)pool_walk_properties((pool_conf_t *)jconf,
1252 	    (pool_elem_t *)jelem, (void *)jarg,
1253 	    (int (*)(pool_conf_t *, pool_elem_t *, char const *,
1254 		pool_value_t *, void *))jcallback);
1255 
1256 	return ((jint)result);
1257 }
1258 
1259 /*
1260  * pool_conf_to_elem(3pool) wrapper
1261  */
1262 /*ARGSUSED*/
1263 JNIEXPORT jlong JNICALL
1264 Java_com_sun_solaris_service_pools_PoolInternal_pool_1conf_1to_1elem(
1265     JNIEnv *jenv, jclass jcls, jlong jconf) {
1266 	return ((jlong)(uintptr_t)pool_conf_to_elem((pool_conf_t *)jconf));
1267 }
1268 
1269 /*
1270  * pool_to_elem(3pool) wrapper
1271  */
1272 /*ARGSUSED*/
1273 JNIEXPORT jlong JNICALL
1274 Java_com_sun_solaris_service_pools_PoolInternal_pool_1to_1elem(JNIEnv *jenv,
1275     jclass jcls, jlong jconf, jlong jpool) {
1276 	return ((jlong)(uintptr_t)pool_to_elem((pool_conf_t *)jconf,
1277 	    (pool_t *)jpool));
1278 }
1279 
1280 /*
1281  * pool_resource_to_elem(3pool) wrapper
1282  */
1283 /*ARGSUSED*/
1284 JNIEXPORT jlong JNICALL
1285 Java_com_sun_solaris_service_pools_PoolInternal_pool_1resource_1to_1elem(
1286     JNIEnv *jenv, jclass jcls, jlong jconf, jlong jresource) {
1287 	return ((jlong)(uintptr_t)pool_resource_to_elem((pool_conf_t *)jconf,
1288 	    (pool_resource_t *)jresource));
1289 }
1290 
1291 /*
1292  * pool_component_to_elem(3pool) wrapper
1293  */
1294 /*ARGSUSED*/
1295 JNIEXPORT jlong JNICALL
1296 Java_com_sun_solaris_service_pools_PoolInternal_pool_1component_1to_1elem(
1297     JNIEnv *jenv, jclass jcls, jlong jconf, jlong jcomponent) {
1298 	return ((jlong)(uintptr_t)pool_component_to_elem((pool_conf_t *)jconf,
1299 	    (pool_component_t *)jcomponent));
1300 }
1301 
1302 /*
1303  * pool_value_get_type(3pool) wrapper
1304  */
1305 /*ARGSUSED*/
1306 JNIEXPORT jint JNICALL
1307 Java_com_sun_solaris_service_pools_PoolInternal_pool_1value_1get_1type(
1308     JNIEnv *jenv, jclass jcls, jlong jvalue) {
1309 	return ((jint)pool_value_get_type((pool_value_t *)jvalue));
1310 }
1311 
1312 /*
1313  * pool_value_set_uint64(3pool) wrapper
1314  */
1315 /*ARGSUSED*/
1316 JNIEXPORT void JNICALL
1317 Java_com_sun_solaris_service_pools_PoolInternal_pool_1value_1set_1uint64(
1318     JNIEnv *jenv, jclass jcls, jlong jvalue, jlong jui64) {
1319 	pool_value_set_uint64((pool_value_t *)jvalue, (uint64_t)jui64);
1320 }
1321 
1322 /*
1323  * pool_value_set_int64(3pool) wrapper
1324  */
1325 /*ARGSUSED*/
1326 JNIEXPORT void JNICALL
1327 Java_com_sun_solaris_service_pools_PoolInternal_pool_1value_1set_1int64(
1328     JNIEnv *jenv, jclass jcls, jlong jvalue, jlong ji64) {
1329 	pool_value_set_int64((pool_value_t *)jvalue, (int64_t)ji64);
1330 }
1331 
1332 /*
1333  * pool_value_set_double(3pool) wrapper
1334  */
1335 /*ARGSUSED*/
1336 JNIEXPORT void JNICALL
1337 Java_com_sun_solaris_service_pools_PoolInternal_pool_1value_1set_1double(
1338     JNIEnv *jenv, jclass jcls, jlong jvalue, jdouble jd) {
1339 	pool_value_set_double((pool_value_t *)jvalue, (double)jd);
1340 }
1341 
1342 /*
1343  * pool_value_set_bool(3pool) wrapper
1344  */
1345 /*ARGSUSED*/
1346 JNIEXPORT void JNICALL
1347 Java_com_sun_solaris_service_pools_PoolInternal_pool_1value_1set_1bool(
1348     JNIEnv *jenv, jclass jcls, jlong jvalue, jshort jb) {
1349 	pool_value_set_bool((pool_value_t *)jvalue, (uchar_t)jb);
1350 }
1351 
1352 /*
1353  * pool_value_set_string(3pool) wrapper
1354  */
1355 /*ARGSUSED*/
1356 JNIEXPORT jint JNICALL
1357 Java_com_sun_solaris_service_pools_PoolInternal_pool_1value_1set_1string(
1358     JNIEnv * jenv, jclass jcls, jlong jvalue, jstring jstr) {
1359 	const char *str;
1360 	int result;
1361 
1362 	str = (jstr) ? (*jenv)->GetStringUTFChars(jenv, jstr, 0) : NULL;
1363 	result = pool_value_set_string((pool_value_t *)jvalue, str);
1364 
1365 	if (str)
1366 		(*jenv)->ReleaseStringUTFChars(jenv, jstr, str);
1367 	return ((jint)result);
1368 }
1369 
1370 /*
1371  * pool_value_get_name(3pool) wrapper
1372  */
1373 /*ARGSUSED*/
1374 JNIEXPORT jstring JNICALL
1375 Java_com_sun_solaris_service_pools_PoolInternal_pool_1value_1get_1name(
1376     JNIEnv *jenv, jclass jcls, jlong jvalue) {
1377 	jstring jresult = NULL;
1378 	const char *result;
1379 
1380 	result = pool_value_get_name((pool_value_t *)jvalue);
1381 
1382 	if (result)
1383 		jresult = (*jenv)->NewStringUTF(jenv, result);
1384 	return (jresult);
1385 }
1386 
1387 /*
1388  * pool_value_set_name(3pool) wrapper
1389  */
1390 /*ARGSUSED*/
1391 JNIEXPORT jint JNICALL
1392 Java_com_sun_solaris_service_pools_PoolInternal_pool_1value_1set_1name(
1393     JNIEnv *jenv, jclass jcls, jlong jvalue, jstring jname) {
1394 	const char *name;
1395 	int result;
1396 
1397 	name = (jname) ? (*jenv)->GetStringUTFChars(jenv, jname, 0) : NULL;
1398 	result = pool_value_set_name((pool_value_t *)jvalue, name);
1399 
1400 	if (name)
1401 		(*jenv)->ReleaseStringUTFChars(jenv, jname, name);
1402 	return ((jint)result);
1403 }
1404 
1405 /*
1406  * pool_value_alloc(3pool) wrapper
1407  */
1408 /*ARGSUSED*/
1409 JNIEXPORT jlong JNICALL
1410 Java_com_sun_solaris_service_pools_PoolInternal_pool_1value_1alloc(JNIEnv *jenv,
1411     jclass jcls) {
1412 	return ((jlong)(uintptr_t)pool_value_alloc());
1413 }
1414 
1415 /*
1416  * pool_value_free(3pool) wrapper
1417  */
1418 /*ARGSUSED*/
1419 JNIEXPORT void JNICALL
1420 Java_com_sun_solaris_service_pools_PoolInternal_pool_1value_1free(JNIEnv *jenv,
1421     jclass jcls, jlong jvalue) {
1422 	pool_value_free((pool_value_t *)jvalue);
1423 }
1424 
1425 /*
1426  * pool_static_location(3pool) wrapper
1427  */
1428 /*ARGSUSED*/
1429 JNIEXPORT jstring JNICALL
1430 Java_com_sun_solaris_service_pools_PoolInternal_pool_1static_1location(
1431     JNIEnv *jenv, jclass jcls) {
1432 	jstring jresult = NULL;
1433 	const char *result;
1434 
1435 	result = pool_static_location();
1436 
1437 	if (result)
1438 		jresult = (*jenv)->NewStringUTF(jenv, result);
1439 	return (jresult);
1440 }
1441 
1442 /*
1443  * pool_dynamic_location(3pool) wrapper
1444  */
1445 /*ARGSUSED*/
1446 JNIEXPORT jstring JNICALL
1447 Java_com_sun_solaris_service_pools_PoolInternal_pool_1dynamic_1location(JNIEnv *
1448     jenv, jclass jcls) {
1449 	jstring jresult = NULL;
1450 	const char *result;
1451 
1452 	result = pool_dynamic_location();
1453 
1454 	if (result)
1455 		jresult = (*jenv)->NewStringUTF(jenv, result);
1456 	return (jresult);
1457 }
1458 
1459 /*
1460  * pool_set_binding(3pool) wrapper
1461  */
1462 /*ARGSUSED*/
1463 JNIEXPORT jint JNICALL
1464 Java_com_sun_solaris_service_pools_PoolInternal_pool_1set_1binding(JNIEnv *jenv,
1465     jclass jcls, jstring jpool, jint jidtype, jint jpid) {
1466 	const char *pool;
1467 	int result;
1468 
1469 	pool = (jpool) ? (*jenv)->GetStringUTFChars(jenv, jpool, 0) : NULL;
1470 	result = (int)pool_set_binding(pool, (idtype_t)jidtype, (id_t)jpid);
1471 
1472 	if (pool)
1473 		(*jenv)->ReleaseStringUTFChars(jenv, jpool, pool);
1474 	return ((jint)result);
1475 }
1476 
1477 /*
1478  * pool_get_binding(3pool) wrapper
1479  */
1480 /*ARGSUSED*/
1481 JNIEXPORT jstring JNICALL
1482 Java_com_sun_solaris_service_pools_PoolInternal_pool_1get_1binding(JNIEnv *jenv,
1483     jclass jcls, jint jpid) {
1484 	jstring jresult = NULL;
1485 	const char *result;
1486 
1487 	result = pool_get_binding((pid_t)jpid);
1488 
1489 	if (result)
1490 		jresult = (*jenv)->NewStringUTF(jenv, result);
1491 	free((void *)result);
1492 	return (jresult);
1493 }
1494 
1495 /*
1496  * pool_get_resource_binding(3pool) wrapper
1497  */
1498 /*ARGSUSED*/
1499 JNIEXPORT jstring JNICALL
1500 Java_com_sun_solaris_service_pools_PoolInternal_pool_1get_1resource_1binding(
1501     JNIEnv *jenv, jclass jcls, jstring jtype, jint jpid) {
1502 	jstring jresult = NULL;
1503 	const char *type;
1504 	const char *result;
1505 
1506 	type = (jtype) ? (*jenv)->GetStringUTFChars(jenv, jtype, 0) : NULL;
1507 	result = pool_get_resource_binding(type, (pid_t)jpid);
1508 
1509 	if (result)
1510 		jresult = (*jenv)->NewStringUTF(jenv, result);
1511 	free((void *)result);
1512 	if (type)
1513 		(*jenv)->ReleaseStringUTFChars(jenv, jtype, type);
1514 	return (jresult);
1515 }
1516 
1517 /*
1518  * pool_walk_pools(3pool) wrapper
1519  */
1520 /*ARGSUSED*/
1521 JNIEXPORT jint JNICALL
1522 Java_com_sun_solaris_service_pools_PoolInternal_pool_1walk_1pools(JNIEnv *jenv,
1523     jclass jcls, jlong jconf, jlong jarg, jlong jcallback) {
1524 	int result;
1525 
1526 	result = pool_walk_pools((pool_conf_t *)jconf, (void *)jarg,
1527 	    (int (*)(pool_conf_t *, pool_t *, void *))jcallback);
1528 	return ((jint)result);
1529 }
1530 
1531 /*
1532  * pool_walk_resources(3pool) wrapper
1533  */
1534 /*ARGSUSED*/
1535 JNIEXPORT jint JNICALL
1536 Java_com_sun_solaris_service_pools_PoolInternal_pool_1walk_1resources(
1537     JNIEnv *jenv, jclass jcls, jlong jconf, jlong jpool, jlong jarg,
1538     jlong jcallback) {
1539 	int result;
1540 
1541 	result = pool_walk_resources((pool_conf_t *)jconf, (pool_t *)jpool,
1542 	    (void *)jarg,
1543 	    (int (*)(pool_conf_t *, pool_resource_t *, void *))jcallback);
1544 	return ((jint)result);
1545 }
1546 
1547 /*
1548  * pool_walk_components(3pool) wrapper
1549  */
1550 /*ARGSUSED*/
1551 JNIEXPORT jint JNICALL
1552 Java_com_sun_solaris_service_pools_PoolInternal_pool_1walk_1components(
1553     JNIEnv *jenv, jclass jcls, jlong jconf, jlong jresource, jlong jarg,
1554     jlong jcallback) {
1555 	int result;
1556 
1557 	result = pool_walk_components((pool_conf_t *)jconf,
1558 	    (pool_resource_t *)jresource, (void *)jarg,
1559 	    (int (*)(pool_conf_t *, pool_component_t *, void *))jcallback);
1560 	return ((jint)result);
1561 }
1562 
1563 /*ARGSUSED*/
1564 JNIEXPORT jint JNICALL
1565 Java_com_sun_solaris_service_pools_Element_walkProps(JNIEnv *env,
1566 	jobject obj, jlong conf, jlong elem, jobject handler, jobject userobj)
1567 {
1568 	struct pool_callback pc;
1569 
1570 	pc.pc_user = userobj;
1571 	pc.pc_handler = handler;
1572 	pc.pc_elem = obj;
1573 	pc.pc_env = env;
1574 	return (pool_walk_properties((pool_conf_t *)*(void**)&conf,
1575 	    (pool_elem_t *)*(void**)&elem, (void *)&pc, pool_property_walker));
1576 }
1577 
1578 /*ARGSUSED*/
1579 static int
1580 pool_property_walker(pool_conf_t *conf, pool_elem_t *pe, const char *name,
1581 	pool_value_t *pv, void *user)
1582 {
1583 	jclass clazz, vclazz;
1584 	jmethodID mgetwalk, mvcon;
1585 	struct pool_callback *pc = (struct pool_callback *)user;
1586 	jobject valueObj;
1587 	pool_value_t *pv_new;
1588 	uint64_t uval;
1589 	int64_t ival;
1590 	double dval;
1591 	uchar_t bval;
1592 	const char *sval;
1593 
1594 	/*
1595 	 * Since we intend to embed our value into a Java Value object
1596 	 * and then reclaim the value when the object is garbage
1597 	 * collected we must create a new pool value and then pass this
1598 	 * to the constructor.  We must not use the pool value which is
1599 	 * passed to us.
1600 	 */
1601 
1602 	if ((pv_new = pool_value_alloc()) == NULL)
1603 		return (PO_FAIL);
1604 	switch (pool_value_get_type(pv)) {
1605 	case POC_UINT:
1606 		(void) pool_value_get_uint64(pv, &uval);
1607 		(void) pool_value_set_uint64(pv_new, uval);
1608 		break;
1609 	case POC_INT:
1610 		(void) pool_value_get_int64(pv, &ival);
1611 		(void) pool_value_set_int64(pv_new, ival);
1612 		break;
1613 	case POC_DOUBLE:
1614 		(void) pool_value_get_double(pv, &dval);
1615 		(void) pool_value_set_double(pv_new, dval);
1616 		break;
1617 	case POC_BOOL:
1618 		(void) pool_value_get_bool(pv, &bval);
1619 		(void) pool_value_set_bool(pv_new, bval);
1620 		break;
1621 	case POC_STRING:
1622 		(void) pool_value_get_string(pv, &sval);
1623 		(void) pool_value_set_string(pv_new, sval);
1624 		break;
1625 	default:
1626 		pool_value_free(pv_new);
1627 		return (PO_FAIL);
1628 	}
1629 	if (pool_value_set_name(pv_new, name) != PO_SUCCESS ||
1630 	    (vclazz = (*pc->pc_env)->FindClass(pc->pc_env,
1631 	    "com/sun/solaris/service/pools/Value")) == NULL ||
1632 	    (mvcon = (*pc->pc_env)->GetMethodID(pc->pc_env, vclazz,
1633 	    "<init>", "(J)V")) == NULL ||
1634 	    (valueObj = (*pc->pc_env)->NewObject(pc->pc_env, vclazz, mvcon,
1635 	    pv_new)) == NULL ||
1636 	    (clazz = (*pc->pc_env)->GetObjectClass(pc->pc_env, pc->pc_handler))
1637 	    == NULL ||
1638 	    (mgetwalk = (*pc->pc_env)->GetMethodID(pc->pc_env,
1639 	    clazz, "walk",
1640 	    "(Lcom/sun/solaris/service/pools/Element;Lcom/sun/solaris/"
1641 	    "service/pools/Value;Ljava/lang/Object;)I")) == NULL)
1642 		return (PO_FAIL);
1643 	return ((*pc->pc_env)->CallIntMethod(pc->pc_env,
1644 		pc->pc_handler, mgetwalk, pc->pc_elem, valueObj, pc->pc_user));
1645 }
1646 
1647 /*ARGSUSED*/
1648 JNIEXPORT jlong JNICALL
1649 Java_com_sun_solaris_service_pools_Value_getLongValue(JNIEnv *jenv,
1650 	jclass class, jlong pointer)
1651 {
1652 	int64_t arg2;
1653 	int result;
1654 
1655 	result = pool_value_get_int64((pool_value_t *)pointer, &arg2);
1656 
1657 	if (result != PO_SUCCESS) { /* it could be a uint64 */
1658 		result = pool_value_get_uint64((pool_value_t *)pointer,
1659 		    (uint64_t *)&arg2);
1660 		if (result != PO_SUCCESS) {
1661 			throw_pe(jenv);
1662 		}
1663 		/*
1664 		 * Unfortunately, Java has no unsigned types, so we lose some
1665 		 * precision by forcing the top bit clear
1666 		 */
1667 		arg2 &= 0x7fffffffffffffff;
1668 	}
1669 	return ((jlong)arg2);
1670 }
1671 
1672 /*ARGSUSED*/
1673 JNIEXPORT jstring JNICALL
1674 Java_com_sun_solaris_service_pools_Value_getStringValue(JNIEnv *jenv,
1675 	jclass class, jlong pointer)
1676 {
1677 	const char *arg2;
1678 	int result;
1679 
1680 	result = pool_value_get_string((pool_value_t *)pointer, &arg2);
1681 	if (result != PO_SUCCESS)
1682 		throw_pe(jenv);
1683 	return ((*jenv)->NewStringUTF(jenv, arg2));
1684 }
1685 
1686 /*ARGSUSED*/
1687 JNIEXPORT jboolean JNICALL
1688 Java_com_sun_solaris_service_pools_Value_getBoolValue(JNIEnv *jenv,
1689 	jclass class, jlong pointer)
1690 {
1691 	uchar_t arg2;
1692 	int result;
1693 
1694 	result = pool_value_get_bool((pool_value_t *)pointer, &arg2);
1695 
1696 	if (result != PO_SUCCESS) {
1697 		throw_pe(jenv);
1698 	}
1699 	if (arg2 == PO_TRUE)
1700 		return (JNI_TRUE);
1701 	else
1702 		return (JNI_FALSE);
1703 }
1704 
1705 /*ARGSUSED*/
1706 JNIEXPORT jdouble JNICALL
1707 Java_com_sun_solaris_service_pools_Value_getDoubleValue(JNIEnv *jenv,
1708 	jclass class, jlong pointer)
1709 {
1710 	double arg2;
1711 	int result;
1712 
1713 	result = pool_value_get_double((pool_value_t *)pointer, &arg2);
1714 
1715 	if (result != PO_SUCCESS) {
1716 		throw_pe(jenv);
1717 	}
1718 	return ((jdouble)arg2);
1719 }
1720 
1721 /*ARGSUSED*/
1722 JNIEXPORT jobject JNICALL
1723 Java_com_sun_solaris_service_pools_Value_getUnsignedInt64Value(JNIEnv *jenv,
1724     jclass class, jlong pointer)
1725 {
1726 	uint64_t arg2;
1727 	int result;
1728 
1729 	result = pool_value_get_uint64((pool_value_t *)pointer, &arg2);
1730 
1731 	if (result != PO_SUCCESS) {
1732 		throw_pe(jenv);
1733 	}
1734 	return (makeUnsignedInt64(jenv, arg2));
1735 }
1736 
1737 /*ARGSUSED*/
1738 JNIEXPORT jobject JNICALL
1739 Java_com_sun_solaris_service_pools_HRTime_timestamp(JNIEnv *env, jobject obj)
1740 {
1741 	return (makeUnsignedInt64(env, gethrtime()));
1742 }
1743 
1744 /*
1745  * Cache class, method, and field IDs.
1746  */
1747 /*ARGSUSED*/
1748 JNIEXPORT void JNICALL
1749 Java_com_sun_solaris_service_pools_PoolInternal_init(JNIEnv *env, jclass clazz)
1750 {
1751 	jclass ui64class_lref;
1752 
1753 	if (!(ui64class_lref = (*env)->FindClass(env,
1754 	    "com/sun/solaris/service/pools/UnsignedInt64")))
1755 		return; /* exception thrown */
1756 	if (!(ui64class = (*env)->NewGlobalRef(env, ui64class_lref)))
1757 		return; /* exception thrown */
1758 	ui64cons_mid = (*env)->GetMethodID(env, ui64class, "<init>", "([B)V");
1759 }
1760