xref: /titanic_51/usr/src/lib/print/libpapi-dynamic/common/psm.c (revision a18dc42fc967d11feba9b8be61c6727dc6c56b48)
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 /*
23*a18dc42fSps29005  * 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31355b4669Sjacobs 
32355b4669Sjacobs #include <stdio.h>
33355b4669Sjacobs #include <unistd.h>
34355b4669Sjacobs #include <errno.h>
35355b4669Sjacobs #include <string.h>
36355b4669Sjacobs #include <dlfcn.h>
37355b4669Sjacobs #include <papi_impl.h>
38355b4669Sjacobs 
39355b4669Sjacobs #ifndef	RTLD_GROUP
40355b4669Sjacobs #define	RTLD_GROUP 0
41355b4669Sjacobs #endif	/* RTLD_GROUP */
42355b4669Sjacobs 
43355b4669Sjacobs #ifndef	PSM_DIR
44355b4669Sjacobs #define	PSM_DIR	"/usr/lib/print"
45355b4669Sjacobs #endif
46355b4669Sjacobs 
47355b4669Sjacobs papi_status_t
48355b4669Sjacobs psm_open(service_t *svc, char *scheme)
49355b4669Sjacobs {
50355b4669Sjacobs 	papi_status_t result = PAPI_OK;
51*a18dc42fSps29005 	char path[BUFSIZ];
52355b4669Sjacobs 
53*a18dc42fSps29005 	if ((scheme == NULL) || (strchr(scheme, '/') != NULL))
54355b4669Sjacobs 		return (PAPI_BAD_ARGUMENT);
55355b4669Sjacobs 
56*a18dc42fSps29005 	snprintf(path, sizeof (path), PSM_DIR "/psm-%s.so", scheme);
57355b4669Sjacobs 
58355b4669Sjacobs 	svc->so_handle = dlopen(path, RTLD_LAZY|RTLD_LOCAL|RTLD_GROUP);
59355b4669Sjacobs 	if (svc->so_handle == NULL) {	/* failed, set the result/message */
60355b4669Sjacobs 		if ((access(path, F_OK) < 0) && (errno == ENOENT))
61355b4669Sjacobs 			result = PAPI_URI_SCHEME;
62355b4669Sjacobs 		else
63355b4669Sjacobs 			result = PAPI_NOT_POSSIBLE;
64355b4669Sjacobs #ifdef DEBUG
65355b4669Sjacobs 		detailed_error(svc, "psm_open(%s): %s: %s", scheme, path,
66355b4669Sjacobs 				dlerror());
67355b4669Sjacobs #endif
68355b4669Sjacobs 	}
69355b4669Sjacobs 
70355b4669Sjacobs 	return (result);
71355b4669Sjacobs }
72355b4669Sjacobs 
73355b4669Sjacobs void
74355b4669Sjacobs psm_close(void *handle)
75355b4669Sjacobs {
76355b4669Sjacobs 	dlclose(handle);
77355b4669Sjacobs }
78355b4669Sjacobs 
79355b4669Sjacobs void *
80355b4669Sjacobs psm_sym(service_t *svc, char *name)
81355b4669Sjacobs {
82355b4669Sjacobs 	char *error = "invalid input";
83355b4669Sjacobs 	void *func = NULL;
84355b4669Sjacobs 
85355b4669Sjacobs 	if ((svc != NULL) && (svc->so_handle != NULL) && (name != NULL)) {
86355b4669Sjacobs 		if ((func = dlsym(svc->so_handle, name)) == NULL)
87355b4669Sjacobs 			error = dlerror();
88355b4669Sjacobs 	}
89355b4669Sjacobs #ifdef DEBUG
90355b4669Sjacobs 	if (func == NULL)
91355b4669Sjacobs 		detailed_error(svc, "psm_sym(%s): %s", name, error);
92355b4669Sjacobs #endif
93355b4669Sjacobs 
94355b4669Sjacobs 	return (func);
95355b4669Sjacobs }
96