1*2264Sjacobs /*
2*2264Sjacobs  * CDDL HEADER START
3*2264Sjacobs  *
4*2264Sjacobs  * The contents of this file are subject to the terms of the
5*2264Sjacobs  * Common Development and Distribution License (the "License").
6*2264Sjacobs  * You may not use this file except in compliance with the License.
7*2264Sjacobs  *
8*2264Sjacobs  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*2264Sjacobs  * or http://www.opensolaris.org/os/licensing.
10*2264Sjacobs  * See the License for the specific language governing permissions
11*2264Sjacobs  * and limitations under the License.
12*2264Sjacobs  *
13*2264Sjacobs  * When distributing Covered Code, include this CDDL HEADER in each
14*2264Sjacobs  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*2264Sjacobs  * If applicable, add the following below this CDDL HEADER, with the
16*2264Sjacobs  * fields enclosed by brackets "[]" replaced with your own identifying
17*2264Sjacobs  * information: Portions Copyright [yyyy] [name of copyright owner]
18*2264Sjacobs  *
19*2264Sjacobs  * CDDL HEADER END
20*2264Sjacobs  */
21*2264Sjacobs 
22*2264Sjacobs /*
23*2264Sjacobs  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24*2264Sjacobs  * Use is subject to license terms.
25*2264Sjacobs  *
26*2264Sjacobs  */
27*2264Sjacobs 
28*2264Sjacobs /* $Id: psm.c 146 2006-03-24 00:26:54Z njacobs $ */
29*2264Sjacobs 
30*2264Sjacobs #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*2264Sjacobs 
32*2264Sjacobs #include <stdio.h>
33*2264Sjacobs #include <unistd.h>
34*2264Sjacobs #include <errno.h>
35*2264Sjacobs #include <string.h>
36*2264Sjacobs #include <dlfcn.h>
37*2264Sjacobs #include <papi_impl.h>
38*2264Sjacobs 
39*2264Sjacobs #ifndef	RTLD_GROUP
40*2264Sjacobs #define	RTLD_GROUP 0
41*2264Sjacobs #endif	/* RTLD_GROUP */
42*2264Sjacobs 
43*2264Sjacobs #ifndef	PSM_DIR
44*2264Sjacobs #define	PSM_DIR	"/usr/lib/print"
45*2264Sjacobs #endif
46*2264Sjacobs 
47*2264Sjacobs papi_status_t
48*2264Sjacobs psm_open(service_t *svc, char *scheme)
49*2264Sjacobs {
50*2264Sjacobs 	papi_status_t result = PAPI_OK;
51*2264Sjacobs 	char *path;
52*2264Sjacobs 	char buf[BUFSIZ];
53*2264Sjacobs 
54*2264Sjacobs 	if (scheme == NULL)
55*2264Sjacobs 		return (PAPI_BAD_ARGUMENT);
56*2264Sjacobs 
57*2264Sjacobs 	if (strchr(scheme, '/') == NULL) {
58*2264Sjacobs 		snprintf(buf, sizeof (buf), PSM_DIR "/psm-%s.so", scheme);
59*2264Sjacobs 		path = buf;
60*2264Sjacobs 	} else
61*2264Sjacobs 		path = scheme;
62*2264Sjacobs 
63*2264Sjacobs 	svc->so_handle = dlopen(path, RTLD_LAZY|RTLD_LOCAL|RTLD_GROUP);
64*2264Sjacobs 	if (svc->so_handle == NULL) {	/* failed, set the result/message */
65*2264Sjacobs 		if ((access(path, F_OK) < 0) && (errno == ENOENT))
66*2264Sjacobs 			result = PAPI_URI_SCHEME;
67*2264Sjacobs 		else
68*2264Sjacobs 			result = PAPI_NOT_POSSIBLE;
69*2264Sjacobs #ifdef DEBUG
70*2264Sjacobs 		detailed_error(svc, "psm_open(%s): %s: %s", scheme, path,
71*2264Sjacobs 				dlerror());
72*2264Sjacobs #endif
73*2264Sjacobs 	}
74*2264Sjacobs 
75*2264Sjacobs 	return (result);
76*2264Sjacobs }
77*2264Sjacobs 
78*2264Sjacobs void
79*2264Sjacobs psm_close(void *handle)
80*2264Sjacobs {
81*2264Sjacobs 	dlclose(handle);
82*2264Sjacobs }
83*2264Sjacobs 
84*2264Sjacobs void *
85*2264Sjacobs psm_sym(service_t *svc, char *name)
86*2264Sjacobs {
87*2264Sjacobs 	char *error = "invalid input";
88*2264Sjacobs 	void *func = NULL;
89*2264Sjacobs 
90*2264Sjacobs 	if ((svc != NULL) && (svc->so_handle != NULL) && (name != NULL)) {
91*2264Sjacobs 		if ((func = dlsym(svc->so_handle, name)) == NULL)
92*2264Sjacobs 			error = dlerror();
93*2264Sjacobs 	}
94*2264Sjacobs #ifdef DEBUG
95*2264Sjacobs 	if (func == NULL)
96*2264Sjacobs 		detailed_error(svc, "psm_sym(%s): %s", name, error);
97*2264Sjacobs #endif
98*2264Sjacobs 
99*2264Sjacobs 	return (func);
100*2264Sjacobs }
101