Debian Codegraph @

dat/udat/windows/dat_osd.c

raw
1 /*
2 * Copyright (c) 2002-2003, Network Appliance, Inc. All rights reserved.
3 *
4 * This Software is licensed under either one of the following two licenses:
5 *
6 * 1) under the terms of the "Common Public License 1.0" a copy of which is
7 * in the file LICENSE.txt in the root directory. The license is also
8 * available from the Open Source Initiative, see
9 * http://www.opensource.org/licenses/cpl.php.
10 * OR
11 *
12 * 2) under the terms of the "The BSD License" a copy of which is in the file
13 * LICENSE2.txt in the root directory. The license is also available from
14 * the Open Source Initiative, see
15 * http://www.opensource.org/licenses/bsd-license.php.
16 *
17 * Licensee has the right to choose either one of the above two licenses.
18 *
19 * Redistributions of source code must retain both the above copyright
20 * notice and either one of the license notices.
21 *
22 * Redistributions in binary form must reproduce both the above copyright
23 * notice, either one of the license notices in the documentation
24 * and/or other materials provided with the distribution.
25 */
26
27 /**********************************************************************
28 *
29 * MODULE: dat_osd.c
30 *
31 * PURPOSE: Operating System Dependent layer
32 * Description:
33 * Provide OS dependent functions with a canonical DAPL
34 * interface. Designed to be portable and hide OS specific quirks
35 * of common functions.
36 *
37 * $Id: dat_osd.c 33 2005-07-11 19:51:17Z ftillier $
38 **********************************************************************/
39
40 #include "dat_osd.h"
41 #include "dat_init.h"
42
43
44 /*********************************************************************
45 * *
46 * Constants *
47 * *
48 *********************************************************************/
49
50 #define DAT_DBG_LEVEL_ENV "DAT_DBG_LEVEL"
51 #define DAT_DBG_DEST_ENV "DAT_DBG_DEST"
52
53
54 /*********************************************************************
55 * *
56 * Enumerations *
57 * *
58 *********************************************************************/
59
60 typedef int DAT_OS_DBG_DEST;
61
62 typedef enum
63 {
64 DAT_OS_DBG_DEST_STDOUT = 0x1,
65 } DAT_OS_DBG_DEST_TYPE;
66
67
68 /*********************************************************************
69 * *
70 * Global Variables *
71 * *
72 *********************************************************************/
73
74 static DAT_OS_DBG_TYPE_VAL g_dbg_type = DAT_OS_DBG_TYPE_ERROR;
75 static DAT_OS_DBG_DEST g_dbg_dest = DAT_OS_DBG_DEST_STDOUT;
76
77
78 /***********************************************************************
79 * Function: dat_os_dbg_set_level
80 ***********************************************************************/
81
82 void
83 dat_os_dbg_init ( void )
84 {
85 char *dbg_type;
86 char *dbg_dest;
87
88 dbg_type = dat_os_getenv (DAT_DBG_LEVEL_ENV);
89 if ( dbg_type != NULL )
90 {
91 g_dbg_type = dat_os_strtol(dbg_type, NULL, 0);
92 }
93
94 dbg_dest = dat_os_getenv (DAT_DBG_DEST_ENV);
95 if ( dbg_dest != NULL )
96 {
97 g_dbg_dest = dat_os_strtol(dbg_dest, NULL, 0);
98 }
99 }
100
101
102 /***********************************************************************
103 * Function: dat_os_dbg_print
104 ***********************************************************************/
105
106 void
107 dat_os_dbg_print (
108 DAT_OS_DBG_TYPE_VAL type,
109 const char * fmt,
110 ...)
111 {
112 if ( (DAT_OS_DBG_TYPE_ERROR == type) || (type & g_dbg_type) )
113 {
114 va_list args;
115
116 if ( DAT_OS_DBG_DEST_STDOUT & g_dbg_dest )
117 {
118 va_start(args, fmt);
119 vfprintf(stdout, fmt, args);
120 fflush(stdout);
121 va_end(args);
122 }
123 /* no syslog() susport in Windows */
124 }
125 }
126
127 HANDLE heap;
128
129 BOOL APIENTRY
130 DllMain(
131 IN HINSTANCE h_module,
132 IN DWORD ul_reason_for_call,
133 IN LPVOID lp_reserved )
134 {
135 extern DAT_BOOLEAN udat_check_state ( void );
136
137 UNREFERENCED_PARAMETER( lp_reserved );
138
139 switch( ul_reason_for_call )
140 {
141 case DLL_PROCESS_ATTACH:
142 heap = HeapCreate(0, 0, 0);
143 if (heap == NULL)
144 return FALSE;
145 DisableThreadLibraryCalls( h_module );
146 udat_check_state();
147 break;
148
149 case DLL_PROCESS_DETACH:
150 dat_fini();
151 HeapDestroy(heap);
152 }
153
154 return TRUE;
155 }
156
157 char *
158 dat_os_library_error(void)
159 {
160 DWORD rc;
161 LPVOID lpMsgBuf;
162
163 if (errno == 0)
164 return NULL;
165
166 // consider formatmessage()?
167 rc = GetLastError();
168
169 FormatMessage(
170 FORMAT_MESSAGE_ALLOCATE_BUFFER |
171 FORMAT_MESSAGE_FROM_SYSTEM |
172 FORMAT_MESSAGE_IGNORE_INSERTS,
173 NULL,
174 rc,
175 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
176 (LPTSTR) &lpMsgBuf,
177 0, NULL );
178
179 //LocalFree(lpMsgBuf); error condition - will exit anyway.
180
181 return (char*)lpMsgBuf;
182 }
183
184 #ifndef INLINE_LIB_LOAD
185
186 DAT_RETURN
187 dat_os_library_load ( const char *library_path,
188 DAT_OS_LIBRARY_HANDLE *library_handle_ptr )
189 {
190 DAT_OS_LIBRARY_HANDLE library_handle;
191 DAT_RETURN rc = DAT_SUCCESS;
192
193 if ( NULL != (library_handle = LoadLibrary(library_path)) )
194 {
195 if ( NULL != library_handle_ptr )
196 {
197 *library_handle_ptr = library_handle;
198 }
199 }
200 else
201 {
202 dat_os_dbg_print(DAT_OS_DBG_TYPE_ERROR,
203 "DAT: library %s load failure\n",library_path);
204 rc = DAT_INTERNAL_ERROR;
205 }
206 return rc;
207 }
208 #endif