xref: /illumos-gate/usr/src/lib/print/libpapi-dynamic/common/psm.c (revision e121b61f5e8ffbeb2f6b373c967c80351333ee21)
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 2008 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 #include <stdio.h>
31 #include <unistd.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <dlfcn.h>
35 #include <papi_impl.h>
36 
37 #ifndef	RTLD_GROUP
38 #define	RTLD_GROUP 0
39 #endif	/* RTLD_GROUP */
40 
41 #ifndef	PSM_DIR
42 #define	PSM_DIR	"/usr/lib/print"
43 #endif
44 
45 papi_status_t
46 psm_open(service_t *svc, char *scheme)
47 {
48 	papi_status_t result = PAPI_OK;
49 	char path[BUFSIZ];
50 
51 	if ((scheme == NULL) || (strchr(scheme, '/') != NULL))
52 		return (PAPI_BAD_ARGUMENT);
53 
54 	snprintf(path, sizeof (path), PSM_DIR "/psm-%s.so", scheme);
55 
56 	svc->so_handle = dlopen(path, RTLD_LAZY|RTLD_LOCAL|RTLD_GROUP);
57 	if (svc->so_handle == NULL) {	/* failed, set the result/message */
58 		if ((access(path, F_OK) < 0) && (errno == ENOENT))
59 			result = PAPI_URI_SCHEME;
60 		else
61 			result = PAPI_NOT_POSSIBLE;
62 #ifdef DEBUG
63 		detailed_error(svc, "psm_open(%s): %s: %s", scheme, path,
64 		    dlerror());
65 #endif
66 	}
67 
68 	return (result);
69 }
70 
71 void
72 psm_close(void *handle)
73 {
74 	dlclose(handle);
75 }
76 
77 void *
78 psm_sym(service_t *svc, char *name)
79 {
80 #ifdef DEBUG
81 	char *error = "invalid input";
82 #endif
83 	void *func = NULL;
84 
85 	if ((svc != NULL) && (svc->so_handle != NULL) && (name != NULL)) {
86 		if ((func = dlsym(svc->so_handle, name)) == NULL) {
87 #ifdef DEBUG
88 			error = dlerror();
89 #else
90 			return (func);
91 #endif
92 		}
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