xref: /titanic_50/usr/src/lib/udapl/libdat/include/dat_osd.h (revision 004388ebfdfe2ed7dfd2d153a876dfcc22d2c006)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*004388ebScasper  * Common Development and Distribution License (the "License").
6*004388ebScasper  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
227c478bd9Sstevel@tonic-gate  * Copyright (c) 2002-2003, Network Appliance, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*
26*004388ebScasper  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
277c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  *
327c478bd9Sstevel@tonic-gate  * HEADER: dat_osd.h
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * PURPOSE: Operating System Dependent layer
357c478bd9Sstevel@tonic-gate  * Description:
367c478bd9Sstevel@tonic-gate  *	Provide OS dependent data structures & functions with
377c478bd9Sstevel@tonic-gate  *	a canonical DAT interface. Designed to be portable
387c478bd9Sstevel@tonic-gate  *	and hide OS specific quirks of common functions.
397c478bd9Sstevel@tonic-gate  *
407c478bd9Sstevel@tonic-gate  * $Id: dat_osd.h,v 1.14 2003/07/31 14:04:19 jlentini Exp $
417c478bd9Sstevel@tonic-gate  */
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #ifndef _DAT_OSD_H_
447c478bd9Sstevel@tonic-gate #define	_DAT_OSD_H_
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #include <dat/udat.h>
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #include <assert.h>
517c478bd9Sstevel@tonic-gate #include <ctype.h>
527c478bd9Sstevel@tonic-gate #include <dlfcn.h>
537c478bd9Sstevel@tonic-gate #include <errno.h>
547c478bd9Sstevel@tonic-gate #include <pthread.h>
557c478bd9Sstevel@tonic-gate #include <stdarg.h>
567c478bd9Sstevel@tonic-gate #include <stdio.h>
577c478bd9Sstevel@tonic-gate #include <stdlib.h>
587c478bd9Sstevel@tonic-gate #include <string.h>
597c478bd9Sstevel@tonic-gate #include <syslog.h>
607c478bd9Sstevel@tonic-gate #include <unistd.h>
617c478bd9Sstevel@tonic-gate #include <sys/time.h>
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
647c478bd9Sstevel@tonic-gate extern "C" {
657c478bd9Sstevel@tonic-gate #endif
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate  *
697c478bd9Sstevel@tonic-gate  * Debugging
707c478bd9Sstevel@tonic-gate  *
717c478bd9Sstevel@tonic-gate  */
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate #define	dat_os_assert(expr)	assert(expr)
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate typedef int 			DAT_OS_DBG_TYPE_VAL;
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate typedef enum
787c478bd9Sstevel@tonic-gate {
797c478bd9Sstevel@tonic-gate     DAT_OS_DBG_TYPE_ERROR 		= 0x1,
807c478bd9Sstevel@tonic-gate     DAT_OS_DBG_TYPE_GENERIC 		= 0x2,
817c478bd9Sstevel@tonic-gate     DAT_OS_DBG_TYPE_SR  		= 0x4,
827c478bd9Sstevel@tonic-gate     DAT_OS_DBG_TYPE_DR  		= 0x8,
837c478bd9Sstevel@tonic-gate     DAT_OS_DBG_TYPE_PROVIDER_API 	= 0x10,
847c478bd9Sstevel@tonic-gate     DAT_OS_DBG_TYPE_CONSUMER_API 	= 0x20,
857c478bd9Sstevel@tonic-gate     DAT_OS_DBG_TYPE_ALL 		= 0xff
867c478bd9Sstevel@tonic-gate } DAT_OS_DBG_TYPE;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate extern void
897c478bd9Sstevel@tonic-gate dat_os_dbg_init(void);
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate extern void
927c478bd9Sstevel@tonic-gate dat_os_dbg_print(
937c478bd9Sstevel@tonic-gate 	DAT_OS_DBG_TYPE_VAL		type,
947c478bd9Sstevel@tonic-gate 	const char			*fmt,
957c478bd9Sstevel@tonic-gate 	...);
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate  *
1007c478bd9Sstevel@tonic-gate  * Utility Functions
1017c478bd9Sstevel@tonic-gate  *
1027c478bd9Sstevel@tonic-gate  */
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate #define	DAT_ERROR(Type, SubType) ((DAT_RETURN)(DAT_CLASS_ERROR | Type | \
1057c478bd9Sstevel@tonic-gate 					SubType))
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate typedef size_t 			DAT_OS_SIZE;
1087c478bd9Sstevel@tonic-gate typedef void * 			DAT_OS_LIBRARY_HANDLE;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate extern DAT_RETURN
1117c478bd9Sstevel@tonic-gate dat_os_library_load(
1127c478bd9Sstevel@tonic-gate     const char 			*library_path,
1137c478bd9Sstevel@tonic-gate     DAT_OS_LIBRARY_HANDLE 	*library_handle_ptr);
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate extern DAT_RETURN
1167c478bd9Sstevel@tonic-gate dat_os_library_unload(
1177c478bd9Sstevel@tonic-gate     const DAT_OS_LIBRARY_HANDLE library_handle);
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate /*
1207c478bd9Sstevel@tonic-gate  * void *dat_os_library_sym(DAT_OS_LIBRARY_HANDLE library_handle, char *sym)
1217c478bd9Sstevel@tonic-gate  */
1227c478bd9Sstevel@tonic-gate #define	dat_os_library_sym(libhndl, sym)	dlsym((libhndl), (sym))
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate /* char *dat_os_getenv(const char *name) */
1257c478bd9Sstevel@tonic-gate #define	dat_os_getenv(name)	getenv((name))
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate /* long int dat_os_strtol(const char *nptr, char **endptr, int base) */
1287c478bd9Sstevel@tonic-gate #define	dat_os_strtol(nptr, endptr, base)	strtol((nptr), (endptr), (base))
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate /* DAT_OS_SIZE dat_os_strlen(const char *s) */
1317c478bd9Sstevel@tonic-gate #define	dat_os_strlen(s)	strlen((s))
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate /* int dat_os_strncmp(const char *s1, const char *s2, DAT_OS_SIZE n) */
1347c478bd9Sstevel@tonic-gate #define	dat_os_strncmp(s1, s2, n)	strncmp((s1), (s2), (n))
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate /* void * dat_os_strncpy(char *dest, const char *src, DAT_OS_SIZE len) */
1377c478bd9Sstevel@tonic-gate #define	dat_os_strncpy(dest, src, len)	strncpy((dest), (src), (len))
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate /* DAT_BOOLEAN dat_os_isblank(int c) */
1407c478bd9Sstevel@tonic-gate #define	dat_os_isblank(c)	((DAT_BOOLEAN)((' ' == (c)) || ('\t' == (c))) \
1417c478bd9Sstevel@tonic-gate 					? DAT_TRUE : DAT_FALSE)
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate /* DAT_BOOLEAN dat_os_isdigit(int c) */
1457c478bd9Sstevel@tonic-gate #define	dat_os_isdigit(c)	((DAT_BOOLEAN)(isdigit((c)) ? DAT_TRUE : \
1467c478bd9Sstevel@tonic-gate 					DAT_FALSE))
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate /* void dat_os_usleep(unsigned long usec) */
1497c478bd9Sstevel@tonic-gate #define	dat_os_usleep(usec)	usleep((usec))
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate /*
1527c478bd9Sstevel@tonic-gate  *
1537c478bd9Sstevel@tonic-gate  * Memory Functions
1547c478bd9Sstevel@tonic-gate  *
1557c478bd9Sstevel@tonic-gate  */
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate /* void *dat_os_alloc(int size) */
1587c478bd9Sstevel@tonic-gate #define	dat_os_alloc(size)	malloc((size))
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate /* void dat_os_free(void *ptr, int size) */
1617c478bd9Sstevel@tonic-gate #define	dat_os_free(ptr, size)	free((ptr))
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate /* void *dat_os_memset(void *loc, int c, DAT_OS_SIZE size) */
1647c478bd9Sstevel@tonic-gate #define	dat_os_memset(loc, c, size)	memset((loc), (c), (size))
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate /*
1677c478bd9Sstevel@tonic-gate  *
1687c478bd9Sstevel@tonic-gate  * File I/O
1697c478bd9Sstevel@tonic-gate  *
1707c478bd9Sstevel@tonic-gate  */
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate typedef FILE 			DAT_OS_FILE;
1737c478bd9Sstevel@tonic-gate typedef fpos_t			DAT_OS_FILE_POS;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate /*
1767c478bd9Sstevel@tonic-gate  * DAT_OS_FILE *dat_os_fopen(const char	*path)
1777c478bd9Sstevel@tonic-gate  * always open files in read only mode
1787c478bd9Sstevel@tonic-gate  */
179*004388ebScasper #define	dat_os_fopen(path)	((DAT_OS_FILE *)fopen((path), "rF"))
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate /* DAT_RETURN dat_os_fgetpos(DAT_OS_FILE *file, DAT_OS_FILE_POS *pos) */
1837c478bd9Sstevel@tonic-gate #define	dat_os_fgetpos(file, pos)	((DAT_RETURN)(			\
1847c478bd9Sstevel@tonic-gate 			(0 == fgetpos((file), (pos))) ? DAT_SUCCESS :	\
1857c478bd9Sstevel@tonic-gate 			DAT_INTERNAL_ERROR))
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate /* DAT_RETURN dat_os_fsetpos(DAT_OS_FILE *file, DAT_OS_FILE_POS *pos) */
1887c478bd9Sstevel@tonic-gate #define	dat_os_fsetpos(file, pos)	((DAT_RETURN)(			\
1897c478bd9Sstevel@tonic-gate 			(0 == fsetpos((file), (pos))) ? DAT_SUCCESS :	\
1907c478bd9Sstevel@tonic-gate 			DAT_INTERNAL_ERROR))
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate /*
1937c478bd9Sstevel@tonic-gate  * dat_os_fgetc() returns EOF on error or end of file.
1947c478bd9Sstevel@tonic-gate  * int dat_os_fgetc(DAT_OS_FILE *file)
1957c478bd9Sstevel@tonic-gate  */
1967c478bd9Sstevel@tonic-gate #define	dat_os_fgetc(file)	fgetc((file))
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate /* int dat_os_fputc(DAT_OS_FILE *file, int c) */
2007c478bd9Sstevel@tonic-gate #define	dat_os_fputc(file, c)	fputc((c), (file))
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate /* int dat_os_fungetc(DAT_OS_FILE *file) */
2037c478bd9Sstevel@tonic-gate #define	dat_os_fungetc(file)	fseek((file), -1, SEEK_CUR)
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate /*
2067c478bd9Sstevel@tonic-gate  * dat_os_fread returns the number of bytes read from the file.
2077c478bd9Sstevel@tonic-gate  * DAT_OS_SIZE dat_os_fread(DAT_OS_FILE *file, char *buf, DAT_OS_SIZE len)
2087c478bd9Sstevel@tonic-gate  */
2097c478bd9Sstevel@tonic-gate #define	dat_os_fread(file, buf, len)	fread((buf), sizeof (char),	\
2107c478bd9Sstevel@tonic-gate 						(len), (file))
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate /* DAT_RETURN dat_os_fclose(DAT_OS_FILE *file) */
2137c478bd9Sstevel@tonic-gate #define	dat_os_fclose(file)	((0 == fclose(file)) ? DAT_SUCCESS :	\
2147c478bd9Sstevel@tonic-gate 					DAT_INTERNAL_ERROR)
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate /*
2177c478bd9Sstevel@tonic-gate  *
2187c478bd9Sstevel@tonic-gate  * Locks
2197c478bd9Sstevel@tonic-gate  *
2207c478bd9Sstevel@tonic-gate  */
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate typedef pthread_mutex_t 	DAT_OS_LOCK;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate /* lock functions */
2267c478bd9Sstevel@tonic-gate /*
2277c478bd9Sstevel@tonic-gate  * DAT_RETURN dat_os_lock_init(IN DAT_OS_LOCK *m)
2287c478bd9Sstevel@tonic-gate  */
2297c478bd9Sstevel@tonic-gate #define	dat_os_lock_init(m)	((0 == pthread_mutex_init((m), NULL)) ?	\
2307c478bd9Sstevel@tonic-gate 					DAT_SUCCESS : DAT_INTERNAL_ERROR)
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate /* DAT_RETURN dat_os_lock(IN DAT_OS_LOCK *m) */
2337c478bd9Sstevel@tonic-gate #define	dat_os_lock(m)		((DAT_RETURN)(				\
2347c478bd9Sstevel@tonic-gate 				(0 == pthread_mutex_lock((m))) ?	\
2357c478bd9Sstevel@tonic-gate 					DAT_SUCCESS : DAT_INTERNAL_ERROR))
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate /* DAT_RETURN dat_os_unlock(IN DAT_OS_LOCK *m) */
2387c478bd9Sstevel@tonic-gate #define	dat_os_unlock(m)	((DAT_RETURN)(				\
2397c478bd9Sstevel@tonic-gate 				(0 == pthread_mutex_unlock((m))) ?	\
2407c478bd9Sstevel@tonic-gate 					DAT_SUCCESS : DAT_INTERNAL_ERROR))
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate /* DAT_RETURN dat_os_lock_destroy(IN DAT_OS_LOCK *m) */
2437c478bd9Sstevel@tonic-gate #define	dat_os_lock_destroy(m)	((DAT_RETURN)(				\
2447c478bd9Sstevel@tonic-gate 				(0 == pthread_mutex_destroy((m))) ?	\
2457c478bd9Sstevel@tonic-gate 					DAT_SUCCESS : DAT_INTERNAL_ERROR))
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate /*
2487c478bd9Sstevel@tonic-gate  * Simple macro to verify a handle is bad. Conditions:
2497c478bd9Sstevel@tonic-gate  * - pointer is NULL
2507c478bd9Sstevel@tonic-gate  * - pointer is not word aligned
2517c478bd9Sstevel@tonic-gate  */
2527c478bd9Sstevel@tonic-gate #define	DAT_BAD_HANDLE(h) (((h) == NULL) || ((unsigned long)(h) & 3))
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
2557c478bd9Sstevel@tonic-gate }
2567c478bd9Sstevel@tonic-gate #endif
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate #endif	/* _DAT_OSD_H_ */
259