xref: /onnv-gate/usr/src/lib/udapl/libdat/include/dat_osd.h (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright (c) 2002-2003, Network Appliance, Inc. All rights reserved.
24*0Sstevel@tonic-gate  */
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate /*
27*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
28*0Sstevel@tonic-gate  * Use is subject to license terms.
29*0Sstevel@tonic-gate  */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate /*
32*0Sstevel@tonic-gate  *
33*0Sstevel@tonic-gate  * HEADER: dat_osd.h
34*0Sstevel@tonic-gate  *
35*0Sstevel@tonic-gate  * PURPOSE: Operating System Dependent layer
36*0Sstevel@tonic-gate  * Description:
37*0Sstevel@tonic-gate  *	Provide OS dependent data structures & functions with
38*0Sstevel@tonic-gate  *	a canonical DAT interface. Designed to be portable
39*0Sstevel@tonic-gate  *	and hide OS specific quirks of common functions.
40*0Sstevel@tonic-gate  *
41*0Sstevel@tonic-gate  * $Id: dat_osd.h,v 1.14 2003/07/31 14:04:19 jlentini Exp $
42*0Sstevel@tonic-gate  */
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate #ifndef _DAT_OSD_H_
45*0Sstevel@tonic-gate #define	_DAT_OSD_H_
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate #include <dat/udat.h>
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate #include <assert.h>
52*0Sstevel@tonic-gate #include <ctype.h>
53*0Sstevel@tonic-gate #include <dlfcn.h>
54*0Sstevel@tonic-gate #include <errno.h>
55*0Sstevel@tonic-gate #include <pthread.h>
56*0Sstevel@tonic-gate #include <stdarg.h>
57*0Sstevel@tonic-gate #include <stdio.h>
58*0Sstevel@tonic-gate #include <stdlib.h>
59*0Sstevel@tonic-gate #include <string.h>
60*0Sstevel@tonic-gate #include <syslog.h>
61*0Sstevel@tonic-gate #include <unistd.h>
62*0Sstevel@tonic-gate #include <sys/time.h>
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate #ifdef	__cplusplus
65*0Sstevel@tonic-gate extern "C" {
66*0Sstevel@tonic-gate #endif
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate /*
69*0Sstevel@tonic-gate  *
70*0Sstevel@tonic-gate  * Debugging
71*0Sstevel@tonic-gate  *
72*0Sstevel@tonic-gate  */
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate #define	dat_os_assert(expr)	assert(expr)
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate typedef int 			DAT_OS_DBG_TYPE_VAL;
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate typedef enum
79*0Sstevel@tonic-gate {
80*0Sstevel@tonic-gate     DAT_OS_DBG_TYPE_ERROR 		= 0x1,
81*0Sstevel@tonic-gate     DAT_OS_DBG_TYPE_GENERIC 		= 0x2,
82*0Sstevel@tonic-gate     DAT_OS_DBG_TYPE_SR  		= 0x4,
83*0Sstevel@tonic-gate     DAT_OS_DBG_TYPE_DR  		= 0x8,
84*0Sstevel@tonic-gate     DAT_OS_DBG_TYPE_PROVIDER_API 	= 0x10,
85*0Sstevel@tonic-gate     DAT_OS_DBG_TYPE_CONSUMER_API 	= 0x20,
86*0Sstevel@tonic-gate     DAT_OS_DBG_TYPE_ALL 		= 0xff
87*0Sstevel@tonic-gate } DAT_OS_DBG_TYPE;
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate extern void
90*0Sstevel@tonic-gate dat_os_dbg_init(void);
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate extern void
93*0Sstevel@tonic-gate dat_os_dbg_print(
94*0Sstevel@tonic-gate 	DAT_OS_DBG_TYPE_VAL		type,
95*0Sstevel@tonic-gate 	const char			*fmt,
96*0Sstevel@tonic-gate 	...);
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate /*
100*0Sstevel@tonic-gate  *
101*0Sstevel@tonic-gate  * Utility Functions
102*0Sstevel@tonic-gate  *
103*0Sstevel@tonic-gate  */
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate #define	DAT_ERROR(Type, SubType) ((DAT_RETURN)(DAT_CLASS_ERROR | Type | \
106*0Sstevel@tonic-gate 					SubType))
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate typedef size_t 			DAT_OS_SIZE;
109*0Sstevel@tonic-gate typedef void * 			DAT_OS_LIBRARY_HANDLE;
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate extern DAT_RETURN
112*0Sstevel@tonic-gate dat_os_library_load(
113*0Sstevel@tonic-gate     const char 			*library_path,
114*0Sstevel@tonic-gate     DAT_OS_LIBRARY_HANDLE 	*library_handle_ptr);
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate extern DAT_RETURN
117*0Sstevel@tonic-gate dat_os_library_unload(
118*0Sstevel@tonic-gate     const DAT_OS_LIBRARY_HANDLE library_handle);
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate /*
121*0Sstevel@tonic-gate  * void *dat_os_library_sym(DAT_OS_LIBRARY_HANDLE library_handle, char *sym)
122*0Sstevel@tonic-gate  */
123*0Sstevel@tonic-gate #define	dat_os_library_sym(libhndl, sym)	dlsym((libhndl), (sym))
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate /* char *dat_os_getenv(const char *name) */
126*0Sstevel@tonic-gate #define	dat_os_getenv(name)	getenv((name))
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate /* long int dat_os_strtol(const char *nptr, char **endptr, int base) */
129*0Sstevel@tonic-gate #define	dat_os_strtol(nptr, endptr, base)	strtol((nptr), (endptr), (base))
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate /* DAT_OS_SIZE dat_os_strlen(const char *s) */
132*0Sstevel@tonic-gate #define	dat_os_strlen(s)	strlen((s))
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate /* int dat_os_strncmp(const char *s1, const char *s2, DAT_OS_SIZE n) */
135*0Sstevel@tonic-gate #define	dat_os_strncmp(s1, s2, n)	strncmp((s1), (s2), (n))
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate /* void * dat_os_strncpy(char *dest, const char *src, DAT_OS_SIZE len) */
138*0Sstevel@tonic-gate #define	dat_os_strncpy(dest, src, len)	strncpy((dest), (src), (len))
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate /* DAT_BOOLEAN dat_os_isblank(int c) */
141*0Sstevel@tonic-gate #define	dat_os_isblank(c)	((DAT_BOOLEAN)((' ' == (c)) || ('\t' == (c))) \
142*0Sstevel@tonic-gate 					? DAT_TRUE : DAT_FALSE)
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate /* DAT_BOOLEAN dat_os_isdigit(int c) */
146*0Sstevel@tonic-gate #define	dat_os_isdigit(c)	((DAT_BOOLEAN)(isdigit((c)) ? DAT_TRUE : \
147*0Sstevel@tonic-gate 					DAT_FALSE))
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate /* void dat_os_usleep(unsigned long usec) */
150*0Sstevel@tonic-gate #define	dat_os_usleep(usec)	usleep((usec))
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate /*
153*0Sstevel@tonic-gate  *
154*0Sstevel@tonic-gate  * Memory Functions
155*0Sstevel@tonic-gate  *
156*0Sstevel@tonic-gate  */
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate /* void *dat_os_alloc(int size) */
159*0Sstevel@tonic-gate #define	dat_os_alloc(size)	malloc((size))
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate /* void dat_os_free(void *ptr, int size) */
162*0Sstevel@tonic-gate #define	dat_os_free(ptr, size)	free((ptr))
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate /* void *dat_os_memset(void *loc, int c, DAT_OS_SIZE size) */
165*0Sstevel@tonic-gate #define	dat_os_memset(loc, c, size)	memset((loc), (c), (size))
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate /*
168*0Sstevel@tonic-gate  *
169*0Sstevel@tonic-gate  * File I/O
170*0Sstevel@tonic-gate  *
171*0Sstevel@tonic-gate  */
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate typedef FILE 			DAT_OS_FILE;
174*0Sstevel@tonic-gate typedef fpos_t			DAT_OS_FILE_POS;
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate /*
177*0Sstevel@tonic-gate  * DAT_OS_FILE *dat_os_fopen(const char	*path)
178*0Sstevel@tonic-gate  * always open files in read only mode
179*0Sstevel@tonic-gate  */
180*0Sstevel@tonic-gate #define	dat_os_fopen(path)	((DAT_OS_FILE *)fopen((path), "r"))
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate /* DAT_RETURN dat_os_fgetpos(DAT_OS_FILE *file, DAT_OS_FILE_POS *pos) */
184*0Sstevel@tonic-gate #define	dat_os_fgetpos(file, pos)	((DAT_RETURN)(			\
185*0Sstevel@tonic-gate 			(0 == fgetpos((file), (pos))) ? DAT_SUCCESS :	\
186*0Sstevel@tonic-gate 			DAT_INTERNAL_ERROR))
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate /* DAT_RETURN dat_os_fsetpos(DAT_OS_FILE *file, DAT_OS_FILE_POS *pos) */
189*0Sstevel@tonic-gate #define	dat_os_fsetpos(file, pos)	((DAT_RETURN)(			\
190*0Sstevel@tonic-gate 			(0 == fsetpos((file), (pos))) ? DAT_SUCCESS :	\
191*0Sstevel@tonic-gate 			DAT_INTERNAL_ERROR))
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate /*
194*0Sstevel@tonic-gate  * dat_os_fgetc() returns EOF on error or end of file.
195*0Sstevel@tonic-gate  * int dat_os_fgetc(DAT_OS_FILE *file)
196*0Sstevel@tonic-gate  */
197*0Sstevel@tonic-gate #define	dat_os_fgetc(file)	fgetc((file))
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate /* int dat_os_fputc(DAT_OS_FILE *file, int c) */
201*0Sstevel@tonic-gate #define	dat_os_fputc(file, c)	fputc((c), (file))
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate /* int dat_os_fungetc(DAT_OS_FILE *file) */
204*0Sstevel@tonic-gate #define	dat_os_fungetc(file)	fseek((file), -1, SEEK_CUR)
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate /*
207*0Sstevel@tonic-gate  * dat_os_fread returns the number of bytes read from the file.
208*0Sstevel@tonic-gate  * DAT_OS_SIZE dat_os_fread(DAT_OS_FILE *file, char *buf, DAT_OS_SIZE len)
209*0Sstevel@tonic-gate  */
210*0Sstevel@tonic-gate #define	dat_os_fread(file, buf, len)	fread((buf), sizeof (char),	\
211*0Sstevel@tonic-gate 						(len), (file))
212*0Sstevel@tonic-gate 
213*0Sstevel@tonic-gate /* DAT_RETURN dat_os_fclose(DAT_OS_FILE *file) */
214*0Sstevel@tonic-gate #define	dat_os_fclose(file)	((0 == fclose(file)) ? DAT_SUCCESS :	\
215*0Sstevel@tonic-gate 					DAT_INTERNAL_ERROR)
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate /*
218*0Sstevel@tonic-gate  *
219*0Sstevel@tonic-gate  * Locks
220*0Sstevel@tonic-gate  *
221*0Sstevel@tonic-gate  */
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate typedef pthread_mutex_t 	DAT_OS_LOCK;
224*0Sstevel@tonic-gate 
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate /* lock functions */
227*0Sstevel@tonic-gate /*
228*0Sstevel@tonic-gate  * DAT_RETURN dat_os_lock_init(IN DAT_OS_LOCK *m)
229*0Sstevel@tonic-gate  */
230*0Sstevel@tonic-gate #define	dat_os_lock_init(m)	((0 == pthread_mutex_init((m), NULL)) ?	\
231*0Sstevel@tonic-gate 					DAT_SUCCESS : DAT_INTERNAL_ERROR)
232*0Sstevel@tonic-gate 
233*0Sstevel@tonic-gate /* DAT_RETURN dat_os_lock(IN DAT_OS_LOCK *m) */
234*0Sstevel@tonic-gate #define	dat_os_lock(m)		((DAT_RETURN)(				\
235*0Sstevel@tonic-gate 				(0 == pthread_mutex_lock((m))) ?	\
236*0Sstevel@tonic-gate 					DAT_SUCCESS : DAT_INTERNAL_ERROR))
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate /* DAT_RETURN dat_os_unlock(IN DAT_OS_LOCK *m) */
239*0Sstevel@tonic-gate #define	dat_os_unlock(m)	((DAT_RETURN)(				\
240*0Sstevel@tonic-gate 				(0 == pthread_mutex_unlock((m))) ?	\
241*0Sstevel@tonic-gate 					DAT_SUCCESS : DAT_INTERNAL_ERROR))
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate /* DAT_RETURN dat_os_lock_destroy(IN DAT_OS_LOCK *m) */
244*0Sstevel@tonic-gate #define	dat_os_lock_destroy(m)	((DAT_RETURN)(				\
245*0Sstevel@tonic-gate 				(0 == pthread_mutex_destroy((m))) ?	\
246*0Sstevel@tonic-gate 					DAT_SUCCESS : DAT_INTERNAL_ERROR))
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate /*
249*0Sstevel@tonic-gate  * Simple macro to verify a handle is bad. Conditions:
250*0Sstevel@tonic-gate  * - pointer is NULL
251*0Sstevel@tonic-gate  * - pointer is not word aligned
252*0Sstevel@tonic-gate  */
253*0Sstevel@tonic-gate #define	DAT_BAD_HANDLE(h) (((h) == NULL) || ((unsigned long)(h) & 3))
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate #ifdef	__cplusplus
256*0Sstevel@tonic-gate }
257*0Sstevel@tonic-gate #endif
258*0Sstevel@tonic-gate 
259*0Sstevel@tonic-gate #endif	/* _DAT_OSD_H_ */
260