10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*1914Scasper * Common Development and Distribution License (the "License"). 6*1914Scasper * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 220Sstevel@tonic-gate * Copyright (c) 2002-2003, Network Appliance, Inc. All rights reserved. 230Sstevel@tonic-gate */ 240Sstevel@tonic-gate 250Sstevel@tonic-gate /* 26*1914Scasper * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 270Sstevel@tonic-gate * Use is subject to license terms. 280Sstevel@tonic-gate */ 290Sstevel@tonic-gate 300Sstevel@tonic-gate /* 310Sstevel@tonic-gate * 320Sstevel@tonic-gate * HEADER: dat_osd.h 330Sstevel@tonic-gate * 340Sstevel@tonic-gate * PURPOSE: Operating System Dependent layer 350Sstevel@tonic-gate * Description: 360Sstevel@tonic-gate * Provide OS dependent data structures & functions with 370Sstevel@tonic-gate * a canonical DAT interface. Designed to be portable 380Sstevel@tonic-gate * and hide OS specific quirks of common functions. 390Sstevel@tonic-gate * 400Sstevel@tonic-gate * $Id: dat_osd.h,v 1.14 2003/07/31 14:04:19 jlentini Exp $ 410Sstevel@tonic-gate */ 420Sstevel@tonic-gate 430Sstevel@tonic-gate #ifndef _DAT_OSD_H_ 440Sstevel@tonic-gate #define _DAT_OSD_H_ 450Sstevel@tonic-gate 460Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 470Sstevel@tonic-gate 480Sstevel@tonic-gate #include <dat/udat.h> 490Sstevel@tonic-gate 500Sstevel@tonic-gate #include <assert.h> 510Sstevel@tonic-gate #include <ctype.h> 520Sstevel@tonic-gate #include <dlfcn.h> 530Sstevel@tonic-gate #include <errno.h> 540Sstevel@tonic-gate #include <pthread.h> 550Sstevel@tonic-gate #include <stdarg.h> 560Sstevel@tonic-gate #include <stdio.h> 570Sstevel@tonic-gate #include <stdlib.h> 580Sstevel@tonic-gate #include <string.h> 590Sstevel@tonic-gate #include <syslog.h> 600Sstevel@tonic-gate #include <unistd.h> 610Sstevel@tonic-gate #include <sys/time.h> 620Sstevel@tonic-gate 630Sstevel@tonic-gate #ifdef __cplusplus 640Sstevel@tonic-gate extern "C" { 650Sstevel@tonic-gate #endif 660Sstevel@tonic-gate 670Sstevel@tonic-gate /* 680Sstevel@tonic-gate * 690Sstevel@tonic-gate * Debugging 700Sstevel@tonic-gate * 710Sstevel@tonic-gate */ 720Sstevel@tonic-gate 730Sstevel@tonic-gate #define dat_os_assert(expr) assert(expr) 740Sstevel@tonic-gate 750Sstevel@tonic-gate typedef int DAT_OS_DBG_TYPE_VAL; 760Sstevel@tonic-gate 770Sstevel@tonic-gate typedef enum 780Sstevel@tonic-gate { 790Sstevel@tonic-gate DAT_OS_DBG_TYPE_ERROR = 0x1, 800Sstevel@tonic-gate DAT_OS_DBG_TYPE_GENERIC = 0x2, 810Sstevel@tonic-gate DAT_OS_DBG_TYPE_SR = 0x4, 820Sstevel@tonic-gate DAT_OS_DBG_TYPE_DR = 0x8, 830Sstevel@tonic-gate DAT_OS_DBG_TYPE_PROVIDER_API = 0x10, 840Sstevel@tonic-gate DAT_OS_DBG_TYPE_CONSUMER_API = 0x20, 850Sstevel@tonic-gate DAT_OS_DBG_TYPE_ALL = 0xff 860Sstevel@tonic-gate } DAT_OS_DBG_TYPE; 870Sstevel@tonic-gate 880Sstevel@tonic-gate extern void 890Sstevel@tonic-gate dat_os_dbg_init(void); 900Sstevel@tonic-gate 910Sstevel@tonic-gate extern void 920Sstevel@tonic-gate dat_os_dbg_print( 930Sstevel@tonic-gate DAT_OS_DBG_TYPE_VAL type, 940Sstevel@tonic-gate const char *fmt, 950Sstevel@tonic-gate ...); 960Sstevel@tonic-gate 970Sstevel@tonic-gate 980Sstevel@tonic-gate /* 990Sstevel@tonic-gate * 1000Sstevel@tonic-gate * Utility Functions 1010Sstevel@tonic-gate * 1020Sstevel@tonic-gate */ 1030Sstevel@tonic-gate 1040Sstevel@tonic-gate #define DAT_ERROR(Type, SubType) ((DAT_RETURN)(DAT_CLASS_ERROR | Type | \ 1050Sstevel@tonic-gate SubType)) 1060Sstevel@tonic-gate 1070Sstevel@tonic-gate typedef size_t DAT_OS_SIZE; 1080Sstevel@tonic-gate typedef void * DAT_OS_LIBRARY_HANDLE; 1090Sstevel@tonic-gate 1100Sstevel@tonic-gate extern DAT_RETURN 1110Sstevel@tonic-gate dat_os_library_load( 1120Sstevel@tonic-gate const char *library_path, 1130Sstevel@tonic-gate DAT_OS_LIBRARY_HANDLE *library_handle_ptr); 1140Sstevel@tonic-gate 1150Sstevel@tonic-gate extern DAT_RETURN 1160Sstevel@tonic-gate dat_os_library_unload( 1170Sstevel@tonic-gate const DAT_OS_LIBRARY_HANDLE library_handle); 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate /* 1200Sstevel@tonic-gate * void *dat_os_library_sym(DAT_OS_LIBRARY_HANDLE library_handle, char *sym) 1210Sstevel@tonic-gate */ 1220Sstevel@tonic-gate #define dat_os_library_sym(libhndl, sym) dlsym((libhndl), (sym)) 1230Sstevel@tonic-gate 1240Sstevel@tonic-gate /* char *dat_os_getenv(const char *name) */ 1250Sstevel@tonic-gate #define dat_os_getenv(name) getenv((name)) 1260Sstevel@tonic-gate 1270Sstevel@tonic-gate /* long int dat_os_strtol(const char *nptr, char **endptr, int base) */ 1280Sstevel@tonic-gate #define dat_os_strtol(nptr, endptr, base) strtol((nptr), (endptr), (base)) 1290Sstevel@tonic-gate 1300Sstevel@tonic-gate /* DAT_OS_SIZE dat_os_strlen(const char *s) */ 1310Sstevel@tonic-gate #define dat_os_strlen(s) strlen((s)) 1320Sstevel@tonic-gate 1330Sstevel@tonic-gate /* int dat_os_strncmp(const char *s1, const char *s2, DAT_OS_SIZE n) */ 1340Sstevel@tonic-gate #define dat_os_strncmp(s1, s2, n) strncmp((s1), (s2), (n)) 1350Sstevel@tonic-gate 1360Sstevel@tonic-gate /* void * dat_os_strncpy(char *dest, const char *src, DAT_OS_SIZE len) */ 1370Sstevel@tonic-gate #define dat_os_strncpy(dest, src, len) strncpy((dest), (src), (len)) 1380Sstevel@tonic-gate 1390Sstevel@tonic-gate /* DAT_BOOLEAN dat_os_isblank(int c) */ 1400Sstevel@tonic-gate #define dat_os_isblank(c) ((DAT_BOOLEAN)((' ' == (c)) || ('\t' == (c))) \ 1410Sstevel@tonic-gate ? DAT_TRUE : DAT_FALSE) 1420Sstevel@tonic-gate 1430Sstevel@tonic-gate 1440Sstevel@tonic-gate /* DAT_BOOLEAN dat_os_isdigit(int c) */ 1450Sstevel@tonic-gate #define dat_os_isdigit(c) ((DAT_BOOLEAN)(isdigit((c)) ? DAT_TRUE : \ 1460Sstevel@tonic-gate DAT_FALSE)) 1470Sstevel@tonic-gate 1480Sstevel@tonic-gate /* void dat_os_usleep(unsigned long usec) */ 1490Sstevel@tonic-gate #define dat_os_usleep(usec) usleep((usec)) 1500Sstevel@tonic-gate 1510Sstevel@tonic-gate /* 1520Sstevel@tonic-gate * 1530Sstevel@tonic-gate * Memory Functions 1540Sstevel@tonic-gate * 1550Sstevel@tonic-gate */ 1560Sstevel@tonic-gate 1570Sstevel@tonic-gate /* void *dat_os_alloc(int size) */ 1580Sstevel@tonic-gate #define dat_os_alloc(size) malloc((size)) 1590Sstevel@tonic-gate 1600Sstevel@tonic-gate /* void dat_os_free(void *ptr, int size) */ 1610Sstevel@tonic-gate #define dat_os_free(ptr, size) free((ptr)) 1620Sstevel@tonic-gate 1630Sstevel@tonic-gate /* void *dat_os_memset(void *loc, int c, DAT_OS_SIZE size) */ 1640Sstevel@tonic-gate #define dat_os_memset(loc, c, size) memset((loc), (c), (size)) 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate /* 1670Sstevel@tonic-gate * 1680Sstevel@tonic-gate * File I/O 1690Sstevel@tonic-gate * 1700Sstevel@tonic-gate */ 1710Sstevel@tonic-gate 1720Sstevel@tonic-gate typedef FILE DAT_OS_FILE; 1730Sstevel@tonic-gate typedef fpos_t DAT_OS_FILE_POS; 1740Sstevel@tonic-gate 1750Sstevel@tonic-gate /* 1760Sstevel@tonic-gate * DAT_OS_FILE *dat_os_fopen(const char *path) 1770Sstevel@tonic-gate * always open files in read only mode 1780Sstevel@tonic-gate */ 179*1914Scasper #define dat_os_fopen(path) ((DAT_OS_FILE *)fopen((path), "rF")) 1800Sstevel@tonic-gate 1810Sstevel@tonic-gate 1820Sstevel@tonic-gate /* DAT_RETURN dat_os_fgetpos(DAT_OS_FILE *file, DAT_OS_FILE_POS *pos) */ 1830Sstevel@tonic-gate #define dat_os_fgetpos(file, pos) ((DAT_RETURN)( \ 1840Sstevel@tonic-gate (0 == fgetpos((file), (pos))) ? DAT_SUCCESS : \ 1850Sstevel@tonic-gate DAT_INTERNAL_ERROR)) 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate /* DAT_RETURN dat_os_fsetpos(DAT_OS_FILE *file, DAT_OS_FILE_POS *pos) */ 1880Sstevel@tonic-gate #define dat_os_fsetpos(file, pos) ((DAT_RETURN)( \ 1890Sstevel@tonic-gate (0 == fsetpos((file), (pos))) ? DAT_SUCCESS : \ 1900Sstevel@tonic-gate DAT_INTERNAL_ERROR)) 1910Sstevel@tonic-gate 1920Sstevel@tonic-gate /* 1930Sstevel@tonic-gate * dat_os_fgetc() returns EOF on error or end of file. 1940Sstevel@tonic-gate * int dat_os_fgetc(DAT_OS_FILE *file) 1950Sstevel@tonic-gate */ 1960Sstevel@tonic-gate #define dat_os_fgetc(file) fgetc((file)) 1970Sstevel@tonic-gate 1980Sstevel@tonic-gate 1990Sstevel@tonic-gate /* int dat_os_fputc(DAT_OS_FILE *file, int c) */ 2000Sstevel@tonic-gate #define dat_os_fputc(file, c) fputc((c), (file)) 2010Sstevel@tonic-gate 2020Sstevel@tonic-gate /* int dat_os_fungetc(DAT_OS_FILE *file) */ 2030Sstevel@tonic-gate #define dat_os_fungetc(file) fseek((file), -1, SEEK_CUR) 2040Sstevel@tonic-gate 2050Sstevel@tonic-gate /* 2060Sstevel@tonic-gate * dat_os_fread returns the number of bytes read from the file. 2070Sstevel@tonic-gate * DAT_OS_SIZE dat_os_fread(DAT_OS_FILE *file, char *buf, DAT_OS_SIZE len) 2080Sstevel@tonic-gate */ 2090Sstevel@tonic-gate #define dat_os_fread(file, buf, len) fread((buf), sizeof (char), \ 2100Sstevel@tonic-gate (len), (file)) 2110Sstevel@tonic-gate 2120Sstevel@tonic-gate /* DAT_RETURN dat_os_fclose(DAT_OS_FILE *file) */ 2130Sstevel@tonic-gate #define dat_os_fclose(file) ((0 == fclose(file)) ? DAT_SUCCESS : \ 2140Sstevel@tonic-gate DAT_INTERNAL_ERROR) 2150Sstevel@tonic-gate 2160Sstevel@tonic-gate /* 2170Sstevel@tonic-gate * 2180Sstevel@tonic-gate * Locks 2190Sstevel@tonic-gate * 2200Sstevel@tonic-gate */ 2210Sstevel@tonic-gate 2220Sstevel@tonic-gate typedef pthread_mutex_t DAT_OS_LOCK; 2230Sstevel@tonic-gate 2240Sstevel@tonic-gate 2250Sstevel@tonic-gate /* lock functions */ 2260Sstevel@tonic-gate /* 2270Sstevel@tonic-gate * DAT_RETURN dat_os_lock_init(IN DAT_OS_LOCK *m) 2280Sstevel@tonic-gate */ 2290Sstevel@tonic-gate #define dat_os_lock_init(m) ((0 == pthread_mutex_init((m), NULL)) ? \ 2300Sstevel@tonic-gate DAT_SUCCESS : DAT_INTERNAL_ERROR) 2310Sstevel@tonic-gate 2320Sstevel@tonic-gate /* DAT_RETURN dat_os_lock(IN DAT_OS_LOCK *m) */ 2330Sstevel@tonic-gate #define dat_os_lock(m) ((DAT_RETURN)( \ 2340Sstevel@tonic-gate (0 == pthread_mutex_lock((m))) ? \ 2350Sstevel@tonic-gate DAT_SUCCESS : DAT_INTERNAL_ERROR)) 2360Sstevel@tonic-gate 2370Sstevel@tonic-gate /* DAT_RETURN dat_os_unlock(IN DAT_OS_LOCK *m) */ 2380Sstevel@tonic-gate #define dat_os_unlock(m) ((DAT_RETURN)( \ 2390Sstevel@tonic-gate (0 == pthread_mutex_unlock((m))) ? \ 2400Sstevel@tonic-gate DAT_SUCCESS : DAT_INTERNAL_ERROR)) 2410Sstevel@tonic-gate 2420Sstevel@tonic-gate /* DAT_RETURN dat_os_lock_destroy(IN DAT_OS_LOCK *m) */ 2430Sstevel@tonic-gate #define dat_os_lock_destroy(m) ((DAT_RETURN)( \ 2440Sstevel@tonic-gate (0 == pthread_mutex_destroy((m))) ? \ 2450Sstevel@tonic-gate DAT_SUCCESS : DAT_INTERNAL_ERROR)) 2460Sstevel@tonic-gate 2470Sstevel@tonic-gate /* 2480Sstevel@tonic-gate * Simple macro to verify a handle is bad. Conditions: 2490Sstevel@tonic-gate * - pointer is NULL 2500Sstevel@tonic-gate * - pointer is not word aligned 2510Sstevel@tonic-gate */ 2520Sstevel@tonic-gate #define DAT_BAD_HANDLE(h) (((h) == NULL) || ((unsigned long)(h) & 3)) 2530Sstevel@tonic-gate 2540Sstevel@tonic-gate #ifdef __cplusplus 2550Sstevel@tonic-gate } 2560Sstevel@tonic-gate #endif 2570Sstevel@tonic-gate 2580Sstevel@tonic-gate #endif /* _DAT_OSD_H_ */ 259