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