FreeBSD Porting
[csql.git] / include / os.h
blob700c025f08ef22c324307ab443c0ae412e3244fd
1 /***************************************************************************
2 * *
3 * Copyright (C) Lakshya Solutions Ltd. All rights reserved. *
4 * *
5 ***************************************************************************/
7 #ifndef OS_H
8 #define OS_H
9 #include<build.h>
11 #if defined(SOLARIS) || defined(LINUX) || defined(FreeBSD)
13 #include <stdio.h>
14 #include <sys/mman.h>
15 #include <sys/msg.h>
16 #include <sys/shm.h>
17 #include <unistd.h>
18 #include <string.h>
19 #include <stdlib.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <fcntl.h>
23 #include <pthread.h>
24 #include <math.h>
25 #include <signal.h>
26 #include <ctype.h>
27 #include <limits.h>
28 #include <sys/socket.h>
29 #include <fnmatch.h>
30 #include <sys/time.h>
31 #include <stdarg.h>
32 #include <net/if.h>
33 #include <arpa/inet.h>
34 #include <netinet/in.h>
35 #include <netinet/tcp.h>
36 #include <sys/file.h>
37 #if defined(SOLARIS)
38 #include <sys/varargs.h>
39 #include <crypt.h>
40 #include <errno.h>
41 #include <sys/atomic.h>
42 #include <timecsql.h>
43 #include <ucontext.h>
44 //extern int errno;
45 #endif
46 #if defined(LINUX)
47 #include <execinfo.h>
48 #include <ifaddrs.h>
49 #include <sys/errno.h>
50 #include <crypt.h>
51 #endif
52 #if defined(FreeBSD)
53 #include <sys/errno.h>
54 #endif
56 typedef void (*sighandler_t)(int);
58 enum FileOpenMode
60 fileOpenReadOnly = O_RDONLY,
61 fileOpenWriteOnly = O_WRONLY,
62 fileOpenReadWrite = O_RDWR,
63 fileOpenAppend = O_CREAT| O_APPEND |O_RDWR,
64 fileOpenCreat = O_CREAT |O_RDWR,
65 // If set and fileOpenExcl is set, the
66 // open will fail if the file already exists.
67 fileOpenExcl = O_EXCL,
68 fileOpenTrunc = O_TRUNC
71 enum MapProtMode
73 mapProtRead = PROT_READ,
74 mapProtWrite = PROT_WRITE,
75 mapProcExec = PROT_EXEC,
76 mapProcNone = PROT_NONE
79 enum MapMode
81 mapShared = MAP_SHARED,
82 mapPrivate = MAP_PRIVATE,
83 mapFixed = MAP_FIXED,
84 // Interpret address exactly.
85 mapNoReserve = MAP_NORESERVE,
86 // Don't reserver swap space.
88 //mapAlign = MAP_ALIGN,
89 //for msync system call
90 mapSync = MS_SYNC ,
91 mapASync = MS_ASYNC
95 #define MAX_FILE_LEN 1024
96 #define IDENTIFIER_LENGTH 64
97 #define ERROR_STRING_LENGTH 128
98 #define DEFAULT_VALUE_BUF_LENGTH 32
99 #define STATE_LENGTH 8
100 #define SYSTEMDB "SYSTEMDB"
101 #define DBAUSER "root"
102 #define DBAPASS "manager"
103 #define I_USER "i@1r4D_f$_a"
104 #define I_PASS "a_$f_D4r1@i"
105 #define DEFAULT_CONFIG_FILE "/etc/csql/csql.conf"
106 #define LOCK_BUCKET_SIZE 2048
107 #define STMT_BUCKET_SIZE 1023
108 #define MAX_CHUNKS 20
109 #define PAGE_SIZE Conf::config.getPageSize()
110 #define MAX_MUTEX_PER_THREAD 3
111 #define MAX_THREADS_PER_PROCESS 30
112 #define MAX_FILE_PATH_LEN 1024
113 #define CHUNK_NAME_LEN 64
114 #define LOG_ROLLOVER_SIZE 20*1024*1024
116 #define BIT(x) (1 << (x))
117 #define SETBITS(x,y) ((x) |= (y))
118 #define CLEARBITS(x,y) ((x) &= (~(y)))
119 #define SETBIT(x,y) SETBITS((x), (BIT((y))))
120 #define CLEARBIT(x,y) CLEARBITS((x), (BIT((y))))
121 #define BITSET(x,y) ((x) & (BIT(y)))
124 typedef key_t shared_memory_key;
125 typedef int shared_memory_id;
126 #if defined(__sparcv9)
127 typedef long InUse;
128 #else
129 typedef int InUse;
130 #endif
132 #endif
134 class os
136 public:
137 static void* mmap(void* addr, size_t len, int prot, int flags, int fildes, off_t off);
138 static int munmap(caddr_t addr, size_t len);
139 static int openFile(const char *name, FileOpenMode flags, size_t size);
140 static int closeFile(int fd);
141 static int lockFile(int fd);
142 static int unlockFile(int fd);
143 static off_t lseek(int fildes, off_t offset, int whence);
144 static int openFileForAppend(const char* fname, int flags);
145 static int getFileSize(const char* fname);
146 static size_t write(int fildes, char *buf, size_t size);
147 static int msync(caddr_t addr, size_t len, int flags);
148 static int fsync(int fildes);
149 inline static size_t alignLong(size_t size)
150 { return ((size - 1) | (sizeof(long) - 1)) + 1;}
151 inline static size_t align(size_t size)
152 { return ((size - 1) | (sizeof(int) - 1)) + 1;}
153 static char* encrypt(const char * key, const char *salt);
154 static void* memset(void *src, int c, size_t size);
155 static void* memcpy(void *src, const void *dest, size_t size);
156 static int memcmp(const void *s1, const void *s2, size_t size);
157 static int select(int nfds, fd_set *readfds, fd_set *writefds,
158 fd_set *exceptfds, struct timeval * timeout);
160 static int usleep(int microsecs);
161 static int sleep(int secs);
162 static shared_memory_id shm_create(shared_memory_key key, size_t size, int flag);
163 static shared_memory_id shm_open(shared_memory_key key, size_t size, int flag);
164 static void* shm_attach(shared_memory_id id, const void *ptr, int flag);
165 static int shm_detach (void*);
166 static int shmctl(int shmid, int cmd);
167 inline static double floor(double val)
168 { return ::floor(val); }
169 static sighandler_t signal(int signum, sighandler_t handler);
171 static int gettimeofday(struct timeval *tp);
172 static struct tm* localtime(long *secs);
173 static pid_t getpid()
174 { return ::getpid(); }
175 static pthread_t getthrid()
176 { return ::pthread_self(); }
177 static char* getenv(const char *envVarName);
178 static int setenv(const char *envVarName, const char *value);
180 static int kill(pid_t pid, int sig);
181 static bool atobool(char *value);
182 static pid_t createProcess(const char* cmdName, const char *arg0);
183 static pid_t fork();
184 static size_t send(int fd, const void *buf, size_t len, int flags);
185 static size_t recv(int fd, void *buf, size_t len, int flags);
186 static int gethostname(char *hostname, size_t len);
187 static int strmatch(char *pattern, char *str);
188 static int msgget(key_t key, int oflag);
189 static int msgsnd(int msqid, const void *ptr, size_t len, int flag);
190 static ssize_t msgrcv(int msqid, void *ptr, size_t len, long type, int flag);
191 static int msgctl(int msqid, int cmd, struct msqid_ds *buff);
192 static int isValidIP(char ipstr[] );
193 static bool fileExists(char *fileName);
194 static char* strcasestr(char *s1, const char *s2);
195 static int getNoOfProcessors();
196 static mode_t umask(mode_t mask);
197 static int fdatasync(int fd);
201 #endif