reverting back the changes. causes core dump
[csql.git] / include / os.h
blobc2c3d2943e2fe9c2fa8024f30835843f5281268d
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 //Added for gettimeofday impl for windows
12 #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
13 #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
14 #else
15 #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
16 #endif
19 #define SQL_STMT_LEN 8192
20 #define MAX_FILE_LEN 1024
21 #define IDENTIFIER_LENGTH 64
22 #define ERROR_STRING_LENGTH 128
23 #define DEFAULT_VALUE_BUF_LENGTH 32
24 #define STATE_LENGTH 8
25 #define SYSTEMDB "SYSTEMDB"
26 #define DBAUSER "root"
27 #define DBAPASS "manager"
28 #define I_USER "i@1r4D_f$_a"
29 #define I_PASS "a_$f_D4r1@i"
30 #define DEFAULT_CONFIG_FILE "/etc/csql/csql.conf"
31 #define LOCK_BUCKET_SIZE 2048
32 #define STMT_BUCKET_SIZE 1023
33 #define MAX_CHUNKS 20
34 #define PAGE_SIZE Conf::config.getPageSize()
35 #define MAX_MUTEX_PER_THREAD 5
36 #define MAX_THREADS_PER_PROCESS 30
37 #define MAX_FILE_PATH_LEN 1024
38 #define CHUNK_NAME_LEN 64
39 #define LOG_ROLLOVER_SIZE 20*1024*1024
40 #define SIGCSQL1 SIGUSR1
41 #define MIN_VARCHAR_ALLOC_SIZE 30
43 #define BIT(x) (1 << (x))
44 #define SETBITS(x,y) ((x) |= (y))
45 #define CLEARBITS(x,y) ((x) &= (~(y)))
46 #define SETBIT(x,y) SETBITS((x), (BIT((y))))
47 #define CLEARBIT(x,y) CLEARBITS((x), (BIT((y))))
48 #define BITSET(x,y) ((x) & (BIT(y)))
49 #define DllExport
50 typedef void (*sighandler_t)(int);
51 #define LHANDLE void*
52 #define LENGTH socklen_t
54 #include <stdio.h>
55 #include <fcntl.h>
56 #include <sys/types.h>
57 #include <sys/stat.h>
58 #include <math.h>
59 #include <string.h>
60 #include <stdlib.h>
61 #include <limits.h>
62 #include <signal.h>
65 #if defined(SOLARIS) || defined(LINUX) || defined(FreeBSD)
67 #include <sys/mman.h>
68 #include <sys/msg.h>
69 #include <sys/shm.h>
70 #include <unistd.h>
71 #include <pthread.h>
72 #include <ctype.h>
73 #include <sys/socket.h>
74 #include <fnmatch.h>
75 #include <sys/time.h>
76 #include <stdarg.h>
77 #include <net/if.h>
78 #include <arpa/inet.h>
79 #include <netinet/in.h>
80 #include <netinet/tcp.h>
81 #include <sys/file.h>
82 #include <sys/wait.h>
83 #include <netdb.h>
84 #if defined(SOLARIS)
85 #include <sys/varargs.h>
86 #include <crypt.h>
87 #include <errno.h>
88 #include <sys/atomic.h>
89 #include <timecsql.h>
90 #include <ucontext.h>
91 //extern int errno;
92 #endif
93 #if defined(LINUX)
94 #include <execinfo.h>
95 #include <ifaddrs.h>
96 #include <sys/errno.h>
97 #include <crypt.h>
98 #endif
99 #if defined(FreeBSD)
100 #include <sys/errno.h>
101 #endif
102 #include <dlfcn.h>
105 enum FileOpenMode
107 fileOpenReadOnly = O_RDONLY,
108 fileOpenWriteOnly = O_WRONLY,
109 fileOpenReadWrite = O_RDWR,
110 fileOpenAppend = O_CREAT| O_APPEND |O_RDWR,
111 fileOpenCreat = O_CREAT |O_RDWR,
112 // If set and fileOpenExcl is set, the
113 // open will fail if the file already exists.
114 fileOpenExcl = O_EXCL,
115 fileOpenTrunc = O_TRUNC
118 enum MapProtMode
120 mapProtRead = PROT_READ,
121 mapProtWrite = PROT_WRITE,
122 mapProcExec = PROT_EXEC,
123 mapProcNone = PROT_NONE
126 enum MapMode
128 mapShared = MAP_SHARED,
129 mapPrivate = MAP_PRIVATE,
130 mapFixed = MAP_FIXED,
131 // Interpret address exactly.
132 mapNoReserve = MAP_NORESERVE,
133 // Don't reserver swap space.
135 //mapAlign = MAP_ALIGN,
136 //for msync system call
137 mapSync = MS_SYNC ,
138 mapASync = MS_ASYNC
141 typedef int file_desc;
142 typedef key_t shared_memory_key;
143 typedef int shared_memory_id;
144 #if defined(__sparcv9)
145 typedef long InUse;
146 #else
147 typedef int InUse;
148 #endif
150 #elif defined(WINNT)
151 #include <io.h>
152 #include <time.h>
153 #include <winsock2.h>
154 #include <errno.h>
155 #include <tchar.h>
157 #define snprintf sprintf_s
158 #define strncasecmp _strnicmp
159 #define strcasecmp strcmpi
160 #define vsnprintf vsnprintf_s
161 typedef HANDLE file_desc;
162 typedef long off_t;
163 typedef void* caddr_t;
164 typedef int InUse;
165 typedef int pid_t;
166 typedef int pthread_t;
167 typedef int mode_t;
168 typedef int key_t;
169 typedef int shared_memory_key;
170 typedef int shared_memory_id;
171 typedef size_t socklen_t;
172 #define MAP_FAILED -1
173 #define MSG_NOSIGNAL 0
174 #define SHM_RND 0
175 #define IPC_RMID 0
176 #define SQL_API
177 #define ALLREADY_HAVE_WINDOWS_TYPE
178 #define SIGCSQL1 0 //drop table signal wont work for windoes
179 #define SIGCHLD SIGTERM
180 #define MS_SYNC 0
181 #define O_SYNC 0
182 #define O_DIRECT 0
183 #define LENGTH int
184 #define LHANDLE HMODULE
185 #ifndef APP_BUILD
186 #define DllExport __declspec( dllexport )
187 #endif
188 typedef signed short int SWORD;
189 #define SQL_NOUNICODEMAP //Note: to suppress mapping to unicode APIs
190 enum FileOpenMode
192 fileOpenReadOnly = _O_RDONLY,
193 fileOpenWriteOnly = _O_WRONLY,
194 fileOpenReadWrite = _O_RDWR,
195 fileOpenAppend = _O_CREAT| _O_APPEND |_O_RDWR,
196 fileOpenCreat = _O_CREAT |_O_RDWR,
197 // If set and fileOpenExcl is set, the
198 // open will fail if the file already exists.
199 fileOpenExcl = _O_EXCL,
200 fileOpenTrunc = _O_TRUNC
202 enum MapProtMode
204 mapProtRead = PAGE_READONLY,
205 mapProtWrite = PAGE_READWRITE,
206 mapProcExec = PAGE_READWRITE, //execute is not supported in win2k
207 mapProcNone = PAGE_READWRITE // exec and none mode is not used in code
210 //Mapping all modes to dummy for now
211 enum MapMode
213 mapShared = PAGE_READWRITE ,
214 mapPrivate = PAGE_READWRITE ,
215 mapFixed = PAGE_READWRITE ,
216 mapNoReserve = PAGE_READWRITE ,
217 mapSync = PAGE_READWRITE ,
218 mapASync = PAGE_READWRITE
221 extern DllExport int optind, opterr;
222 extern DllExport TCHAR *optarg;
224 #endif
226 class DllExport os
228 public:
229 static void* mmap(void* addr, size_t len, int prot, int flags, file_desc fildes, off_t off);
230 static int munmap(caddr_t addr, size_t len);
231 static int open(const char *name, FileOpenMode flags, size_t size);
232 static int close(int fd);
233 static file_desc openFile(const char *name, FileOpenMode flags, size_t size);
234 static int closeFile(file_desc fd);
235 static int closeSocket(int fd);
236 static int lockFile(int fd);
237 static int unlockFile(int fd);
238 static int truncate(const char* fname);
239 static off_t lseek(int fildes, off_t offset, int whence);
240 static int openFileForAppend(const char* fname, int flags);
241 static int getFileSize(const char* fname);
242 static size_t write(int fildes, char *buf, size_t size);
243 static int msync(caddr_t addr, size_t len, int flags);
244 static int fsync(int fildes);
245 inline static size_t alignLong(size_t size)
246 { return ((size - 1) | (sizeof(long) - 1)) + 1;}
247 inline static size_t align(size_t size)
248 { return ((size - 1) | (sizeof(InUse) - 1)) + 1;}
249 static char* encrypt(const char * key, const char *salt);
250 static void* memset(void *src, int c, size_t size);
251 static void* memcpy(void *src, const void *dest, size_t size);
252 static int memcmp(const void *s1, const void *s2, size_t size);
253 static int select(int nfds, fd_set *readfds, fd_set *writefds,
254 fd_set *exceptfds, struct timeval * timeout);
256 static int usleep(int microsecs);
257 static int sleep(int secs);
258 static shared_memory_id shm_create(shared_memory_key key, size_t size, int flag);
259 static shared_memory_id shm_open(shared_memory_key key, size_t size, int flag);
260 static void* shm_attach(shared_memory_id id, const void *ptr, int flag);
261 static int shm_detach (void*);
262 static int shmctl(int shmid, int cmd);
263 static int shm_remove(int shmid);
264 inline static double floor(double val)
265 { return ::floor(val); }
266 static sighandler_t signal(int signum, sighandler_t handler);
268 static int gettimeofday(struct timeval *tp);
269 static struct tm* localtime(long *secs);
270 static pid_t getpid();
271 static pthread_t getthrid();
272 static char* getenv(const char *envVarName);
273 static int setenv(const char *envVarName, const char *value);
275 static int kill(pid_t pid, int sig);
276 static bool atobool(char *value);
277 static pid_t createProcess(const char* cmdName, const char *arg0);
278 static pid_t fork();
279 static size_t send(int fd, const void *buf, size_t len, int flags);
280 static size_t recv(int fd, void *buf, size_t len, int flags);
281 static int gethostname(char *hostname, size_t len);
282 static int strmatch(char *pattern, char *str);
283 static int msgget(key_t key, int oflag);
284 static int msgsnd(int msqid, const void *ptr, size_t len, int flag);
285 static int msgrcv(int msqid, void *ptr, size_t len, long type, int flag);
286 static int msgctl(int msqid, int cmd, struct msqid_ds *buff);
287 static int isValidIP(char ipstr[] );
288 static bool fileExists(char *fileName);
289 static char* strcasestr(char *s1, const char *s2);
290 static int getNoOfProcessors();
291 static mode_t umask(mode_t mask);
292 static int fdatasync(file_desc fd);
293 static int atexit(void (*exitHndlr)(void));
294 static void* dlsym(LHANDLE hdl, char* funcName);
295 static char getopt(int argc, char *argv[], char *opt);
298 #endif