1355b4669Sjacobs /*
2355b4669Sjacobs  * CDDL HEADER START
3355b4669Sjacobs  *
4355b4669Sjacobs  * The contents of this file are subject to the terms of the
5355b4669Sjacobs  * Common Development and Distribution License (the "License").
6355b4669Sjacobs  * You may not use this file except in compliance with the License.
7355b4669Sjacobs  *
8355b4669Sjacobs  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9355b4669Sjacobs  * or http://www.opensolaris.org/os/licensing.
10355b4669Sjacobs  * See the License for the specific language governing permissions
11355b4669Sjacobs  * and limitations under the License.
12355b4669Sjacobs  *
13355b4669Sjacobs  * When distributing Covered Code, include this CDDL HEADER in each
14355b4669Sjacobs  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15355b4669Sjacobs  * If applicable, add the following below this CDDL HEADER, with the
16355b4669Sjacobs  * fields enclosed by brackets "[]" replaced with your own identifying
17355b4669Sjacobs  * information: Portions Copyright [yyyy] [name of copyright owner]
18355b4669Sjacobs  *
19355b4669Sjacobs  * CDDL HEADER END
20355b4669Sjacobs  */
21355b4669Sjacobs 
22355b4669Sjacobs /*
23a18dc42fSps  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24355b4669Sjacobs  * Use is subject to license terms.
25355b4669Sjacobs  *
26355b4669Sjacobs  */
27355b4669Sjacobs 
28355b4669Sjacobs /* $Id: psm.c 146 2006-03-24 00:26:54Z njacobs $ */
29355b4669Sjacobs 
30355b4669Sjacobs #include <stdio.h>
31355b4669Sjacobs #include <unistd.h>
32355b4669Sjacobs #include <errno.h>
33355b4669Sjacobs #include <string.h>
34355b4669Sjacobs #include <dlfcn.h>
35355b4669Sjacobs #include <papi_impl.h>
36355b4669Sjacobs 
37355b4669Sjacobs #ifndef	RTLD_GROUP
38355b4669Sjacobs #define	RTLD_GROUP 0
39355b4669Sjacobs #endif	/* RTLD_GROUP */
40355b4669Sjacobs 
41355b4669Sjacobs #ifndef	PSM_DIR
42355b4669Sjacobs #define	PSM_DIR	"/usr/lib/print"
43355b4669Sjacobs #endif
44355b4669Sjacobs 
45355b4669Sjacobs papi_status_t
psm_open(service_t * svc,char * scheme)46355b4669Sjacobs psm_open(service_t *svc, char *scheme)
47355b4669Sjacobs {
48355b4669Sjacobs 	papi_status_t result = PAPI_OK;
49a18dc42fSps 	char path[BUFSIZ];
50355b4669Sjacobs 
51a18dc42fSps 	if ((scheme == NULL) || (strchr(scheme, '/') != NULL))
52355b4669Sjacobs 		return (PAPI_BAD_ARGUMENT);
53355b4669Sjacobs 
54a18dc42fSps 	snprintf(path, sizeof (path), PSM_DIR "/psm-%s.so", scheme);
55355b4669Sjacobs 
56355b4669Sjacobs 	svc->so_handle = dlopen(path, RTLD_LAZY|RTLD_LOCAL|RTLD_GROUP);
57355b4669Sjacobs 	if (svc->so_handle == NULL) {	/* failed, set the result/message */
58355b4669Sjacobs 		if ((access(path, F_OK) < 0) && (errno == ENOENT))
59355b4669Sjacobs 			result = PAPI_URI_SCHEME;
60355b4669Sjacobs 		else
61355b4669Sjacobs 			result = PAPI_NOT_POSSIBLE;
62355b4669Sjacobs #ifdef DEBUG
63355b4669Sjacobs 		detailed_error(svc, "psm_open(%s): %s: %s", scheme, path,
64*1d6eba5fSToomas Soome 		    dlerror());
65355b4669Sjacobs #endif
66355b4669Sjacobs 	}
67355b4669Sjacobs 
68355b4669Sjacobs 	return (result);
69355b4669Sjacobs }
70355b4669Sjacobs 
71355b4669Sjacobs void
psm_close(void * handle)72355b4669Sjacobs psm_close(void *handle)
73355b4669Sjacobs {
74355b4669Sjacobs 	dlclose(handle);
75355b4669Sjacobs }
76355b4669Sjacobs 
77355b4669Sjacobs void *
psm_sym(service_t * svc,char * name)78355b4669Sjacobs psm_sym(service_t *svc, char *name)
79355b4669Sjacobs {
80*1d6eba5fSToomas Soome #ifdef DEBUG
81355b4669Sjacobs 	char *error = "invalid input";
82*1d6eba5fSToomas Soome #endif
83355b4669Sjacobs 	void *func = NULL;
84355b4669Sjacobs 
85355b4669Sjacobs 	if ((svc != NULL) && (svc->so_handle != NULL) && (name != NULL)) {
86*1d6eba5fSToomas Soome 		if ((func = dlsym(svc->so_handle, name)) == NULL) {
87*1d6eba5fSToomas Soome #ifdef DEBUG
88355b4669Sjacobs 			error = dlerror();
89*1d6eba5fSToomas Soome #else
90*1d6eba5fSToomas Soome 			return (func);
91*1d6eba5fSToomas Soome #endif
92*1d6eba5fSToomas Soome 		}
93355b4669Sjacobs 	}
94355b4669Sjacobs #ifdef DEBUG
95355b4669Sjacobs 	if (func == NULL)
96355b4669Sjacobs 		detailed_error(svc, "psm_sym(%s): %s", name, error);
97355b4669Sjacobs #endif
98355b4669Sjacobs 
99355b4669Sjacobs 	return (func);
100355b4669Sjacobs }
101