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 /*
32  *
33  * MODULE: dat_osd.c
34  *
35  * PURPOSE: Operating System Dependent layer
36  * Description:
37  *	Provide OS dependent functions with a canonical DAPL
38  *	interface. Designed to be portable and hide OS specific quirks
39  *	of common functions.
40  *
41  * $Id: dat_osd.c,v 1.8 2003/08/15 20:09:52 jlentini Exp $
42  */
43 
44 #include "dat_osd.h"
45 
46 
47 /*
48  *
49  * Constants
50  *
51  */
52 
53 #define	DAT_DBG_TYPE_ENV 	"DAT_DBG_TYPE"
54 #define	DAT_DBG_DEST_ENV 	"DAT_DBG_DEST"
55 
56 
57 /*
58  *
59  * Enumerations
60  *
61  */
62 
63 typedef int 			DAT_OS_DBG_DEST;
64 
65 typedef enum
66 {
67     DAT_OS_DBG_DEST_STDOUT  		= 0x1,
68     DAT_OS_DBG_DEST_SYSLOG  		= 0x2,
69     DAT_OS_DBG_DEST_ALL  		= 0x3
70 } DAT_OS_DBG_DEST_TYPE;
71 
72 
73 /*
74  *
75  * Global Variables
76  *
77  */
78 
79 static DAT_OS_DBG_TYPE_VAL 	g_dbg_type = 0;
80 static DAT_OS_DBG_DEST 		g_dbg_dest = DAT_OS_DBG_DEST_STDOUT;
81 
82 
83 /*
84  * Function: dat_os_dbg_init
85  */
86 
87 void
dat_os_dbg_init(void)88 dat_os_dbg_init(void)
89 {
90 	char *dbg_type;
91 	char *dbg_dest;
92 
93 	if (NULL != (dbg_type = dat_os_getenv(DAT_DBG_TYPE_ENV))) {
94 		g_dbg_type = dat_os_strtol(dbg_type, NULL, 0);
95 	}
96 
97 	if (NULL != (dbg_dest = dat_os_getenv(DAT_DBG_DEST_ENV))) {
98 		g_dbg_dest = dat_os_strtol(dbg_dest, NULL, 0);
99 	}
100 }
101 
102 
103 /*
104  * Function: dat_os_dbg_print
105  */
106 
107 void
dat_os_dbg_print(DAT_OS_DBG_TYPE_VAL type,const char * fmt,...)108 dat_os_dbg_print(
109 	DAT_OS_DBG_TYPE_VAL		type,
110 	const char			*fmt,
111 	...)
112 {
113 	if ((DAT_OS_DBG_TYPE_ERROR == type) || (type & g_dbg_type)) {
114 		va_list args;
115 
116 		va_start(args, fmt);
117 
118 		if (DAT_OS_DBG_DEST_STDOUT & g_dbg_dest) {
119 			(void) vfprintf(stdout, fmt, args);
120 			(void) fflush(stdout);
121 		}
122 
123 		if (DAT_OS_DBG_DEST_SYSLOG & g_dbg_dest) {
124 			vsyslog(LOG_USER | LOG_DEBUG, fmt, args);
125 		}
126 
127 		va_end(args);
128 	}
129 }
130 
131 
132 /*
133  * Function: dat_os_library_load
134  */
135 
136 DAT_RETURN
dat_os_library_load(const char * library_path,DAT_OS_LIBRARY_HANDLE * library_handle_ptr)137 dat_os_library_load(
138     const char 			*library_path,
139     DAT_OS_LIBRARY_HANDLE 	*library_handle_ptr)
140 {
141 	DAT_OS_LIBRARY_HANDLE	library_handle;
142 
143 	if (NULL != (library_handle = dlopen(library_path, RTLD_NOW))) {
144 		if (NULL != library_handle_ptr) {
145 			*library_handle_ptr = library_handle;
146 		}
147 
148 		return (DAT_SUCCESS);
149 	} else {
150 		dat_os_dbg_print(DAT_OS_DBG_TYPE_ERROR,
151 		    "DAT: library load failure: %s\n",
152 		    dlerror());
153 		return (DAT_INTERNAL_ERROR);
154 	}
155 }
156 
157 
158 /*
159  * Function: dat_os_library_unload
160  */
161 
162 DAT_RETURN
dat_os_library_unload(const DAT_OS_LIBRARY_HANDLE library_handle)163 dat_os_library_unload(
164 	const DAT_OS_LIBRARY_HANDLE library_handle)
165 {
166 	if (0 != dlclose(library_handle)) {
167 		return (DAT_INTERNAL_ERROR);
168 	} else {
169 		return (DAT_SUCCESS);
170 	}
171 }
172