xref: /illumos-gate/usr/src/lib/udapl/libdat/common/dat_osd.c (revision 986fd29a0dc13f7608ef7f508f6e700bd7bc2720)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 2002-2003, Network Appliance, Inc. All rights reserved.
24  */
25 
26 /*
27  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 /*
34  *
35  * MODULE: dat_osd.c
36  *
37  * PURPOSE: Operating System Dependent layer
38  * Description:
39  *	Provide OS dependent functions with a canonical DAPL
40  *	interface. Designed to be portable and hide OS specific quirks
41  *	of common functions.
42  *
43  * $Id: dat_osd.c,v 1.8 2003/08/15 20:09:52 jlentini Exp $
44  */
45 
46 #include "dat_osd.h"
47 
48 
49 /*
50  *
51  * Constants
52  *
53  */
54 
55 #define	DAT_DBG_TYPE_ENV 	"DAT_DBG_TYPE"
56 #define	DAT_DBG_DEST_ENV 	"DAT_DBG_DEST"
57 
58 
59 /*
60  *
61  * Enumerations
62  *
63  */
64 
65 typedef int 			DAT_OS_DBG_DEST;
66 
67 typedef enum
68 {
69     DAT_OS_DBG_DEST_STDOUT  		= 0x1,
70     DAT_OS_DBG_DEST_SYSLOG  		= 0x2,
71     DAT_OS_DBG_DEST_ALL  		= 0x3
72 } DAT_OS_DBG_DEST_TYPE;
73 
74 
75 /*
76  *
77  * Global Variables
78  *
79  */
80 
81 static DAT_OS_DBG_TYPE_VAL 	g_dbg_type = 0;
82 static DAT_OS_DBG_DEST 		g_dbg_dest = DAT_OS_DBG_DEST_STDOUT;
83 
84 
85 /*
86  * Function: dat_os_dbg_init
87  */
88 
89 void
90 dat_os_dbg_init(void)
91 {
92 	char *dbg_type;
93 	char *dbg_dest;
94 
95 	if (NULL != (dbg_type = dat_os_getenv(DAT_DBG_TYPE_ENV))) {
96 		g_dbg_type = dat_os_strtol(dbg_type, NULL, 0);
97 	}
98 
99 	if (NULL != (dbg_dest = dat_os_getenv(DAT_DBG_DEST_ENV))) {
100 		g_dbg_dest = dat_os_strtol(dbg_dest, NULL, 0);
101 	}
102 }
103 
104 
105 /*
106  * Function: dat_os_dbg_print
107  */
108 
109 void
110 dat_os_dbg_print(
111 	DAT_OS_DBG_TYPE_VAL		type,
112 	const char			*fmt,
113 	...)
114 {
115 	if ((DAT_OS_DBG_TYPE_ERROR == type) || (type & g_dbg_type)) {
116 		va_list args;
117 
118 		va_start(args, fmt);
119 
120 		if (DAT_OS_DBG_DEST_STDOUT & g_dbg_dest) {
121 			(void) vfprintf(stdout, fmt, args);
122 			(void) fflush(stdout);
123 		}
124 
125 		if (DAT_OS_DBG_DEST_SYSLOG & g_dbg_dest) {
126 			vsyslog(LOG_USER | LOG_DEBUG, fmt, args);
127 		}
128 
129 		va_end(args);
130 	}
131 }
132 
133 
134 /*
135  * Function: dat_os_library_load
136  */
137 
138 DAT_RETURN
139 dat_os_library_load(
140     const char 			*library_path,
141     DAT_OS_LIBRARY_HANDLE 	*library_handle_ptr)
142 {
143 	DAT_OS_LIBRARY_HANDLE	library_handle;
144 
145 	if (NULL != (library_handle = dlopen(library_path, RTLD_NOW))) {
146 		if (NULL != library_handle_ptr) {
147 			*library_handle_ptr = library_handle;
148 		}
149 
150 		return (DAT_SUCCESS);
151 	} else {
152 		dat_os_dbg_print(DAT_OS_DBG_TYPE_ERROR,
153 		    "DAT: library load failure: %s\n",
154 		    dlerror());
155 		return (DAT_INTERNAL_ERROR);
156 	}
157 }
158 
159 
160 /*
161  * Function: dat_os_library_unload
162  */
163 
164 DAT_RETURN
165 dat_os_library_unload(
166 	const DAT_OS_LIBRARY_HANDLE library_handle)
167 {
168 	if (0 != dlclose(library_handle)) {
169 		return (DAT_INTERNAL_ERROR);
170 	} else {
171 		return (DAT_SUCCESS);
172 	}
173 }
174