xref: /titanic_52/usr/src/lib/print/libpapi-dynamic/common/psm.c (revision 96a62ada8aa6cb19b04270da282e7e21ba74b808)
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 #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[BUFSIZ];
52 
53 	if ((scheme == NULL) || (strchr(scheme, '/') != NULL))
54 		return (PAPI_BAD_ARGUMENT);
55 
56 	snprintf(path, sizeof (path), PSM_DIR "/psm-%s.so", scheme);
57 
58 	svc->so_handle = dlopen(path, RTLD_LAZY|RTLD_LOCAL|RTLD_GROUP);
59 	if (svc->so_handle == NULL) {	/* failed, set the result/message */
60 		if ((access(path, F_OK) < 0) && (errno == ENOENT))
61 			result = PAPI_URI_SCHEME;
62 		else
63 			result = PAPI_NOT_POSSIBLE;
64 #ifdef DEBUG
65 		detailed_error(svc, "psm_open(%s): %s: %s", scheme, path,
66 				dlerror());
67 #endif
68 	}
69 
70 	return (result);
71 }
72 
73 void
74 psm_close(void *handle)
75 {
76 	dlclose(handle);
77 }
78 
79 void *
80 psm_sym(service_t *svc, char *name)
81 {
82 	char *error = "invalid input";
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 			error = dlerror();
88 	}
89 #ifdef DEBUG
90 	if (func == NULL)
91 		detailed_error(svc, "psm_sym(%s): %s", name, error);
92 #endif
93 
94 	return (func);
95 }
96