removing repl references
[csql.git] / include / os.h
blobf4ffc9c81786de07e7effdc3bd9bd0e10823d1e2
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*
53 #include <stdio.h>
54 #include <fcntl.h>
55 #include <sys/types.h>
56 #include <sys/stat.h>
57 #include <math.h>
58 #include <string.h>
59 #include <stdlib.h>
60 #include <limits.h>
61 #include <signal.h>
64 #if defined(SOLARIS) || defined(LINUX) || defined(FreeBSD)
66 #include <sys/mman.h>
67 #include <sys/msg.h>
68 #include <sys/shm.h>
69 #include <unistd.h>
70 #include <pthread.h>
71 #include <ctype.h>
72 #include <sys/socket.h>
73 #include <fnmatch.h>
74 #include <sys/time.h>
75 #include <stdarg.h>
76 #include <net/if.h>
77 #include <arpa/inet.h>
78 #include <netinet/in.h>
79 #include <netinet/tcp.h>
80 #include <sys/file.h>
81 #include <sys/wait.h>
82 #include <netdb.h>
83 #if defined(SOLARIS)
84 #include <sys/varargs.h>
85 #include <crypt.h>
86 #include <errno.h>
87 #include <sys/atomic.h>
88 #include <timecsql.h>
89 #include <ucontext.h>
90 //extern int errno;
91 #endif
92 #if defined(LINUX)
93 #include <execinfo.h>
94 #include <ifaddrs.h>
95 #include <sys/errno.h>
96 #include <crypt.h>
97 #endif
98 #if defined(FreeBSD)
99 #include <sys/errno.h>
100 #endif
101 #include <dlfcn.h>
104 enum FileOpenMode
106 fileOpenReadOnly = O_RDONLY,
107 fileOpenWriteOnly = O_WRONLY,
108 fileOpenReadWrite = O_RDWR,
109 fileOpenAppend = O_CREAT| O_APPEND |O_RDWR,
110 fileOpenCreat = O_CREAT |O_RDWR,
111 // If set and fileOpenExcl is set, the
112 // open will fail if the file already exists.
113 fileOpenExcl = O_EXCL,
114 fileOpenTrunc = O_TRUNC
117 enum MapProtMode
119 mapProtRead = PROT_READ,
120 mapProtWrite = PROT_WRITE,
121 mapProcExec = PROT_EXEC,
122 mapProcNone = PROT_NONE
125 enum MapMode
127 mapShared = MAP_SHARED,
128 mapPrivate = MAP_PRIVATE,
129 mapFixed = MAP_FIXED,
130 // Interpret address exactly.
131 mapNoReserve = MAP_NORESERVE,
132 // Don't reserver swap space.
134 //mapAlign = MAP_ALIGN,
135 //for msync system call
136 mapSync = MS_SYNC ,
137 mapASync = MS_ASYNC
140 typedef int file_desc;
141 typedef key_t shared_memory_key;
142 typedef int shared_memory_id;
143 #if defined(__sparcv9)
144 typedef long InUse;
145 #else
146 typedef int InUse;
147 #endif
149 #elif defined(WINNT)
150 #include <io.h>
151 #include <time.h>
152 #include <winsock2.h>
153 #include <errno.h>
154 #include <tchar.h>
156 #define snprintf sprintf_s
157 #define strncasecmp _strnicmp
158 #define strcasecmp strcmpi
159 #define vsnprintf vsnprintf_s
160 typedef HANDLE file_desc;
161 typedef long off_t;
162 typedef void* caddr_t;
163 typedef int InUse;
164 typedef int pid_t;
165 typedef int pthread_t;
166 typedef int mode_t;
167 typedef int key_t;
168 typedef int shared_memory_key;
169 typedef int shared_memory_id;
170 typedef size_t socklen_t;
171 #define MAP_FAILED -1
172 #define MSG_NOSIGNAL 0
173 #define SHM_RND 0
174 #define IPC_RMID 0
175 #define SQL_API
176 #define ALLREADY_HAVE_WINDOWS_TYPE
177 #define SIGCSQL1 0 //drop table signal wont work for windoes
178 #define SIGCHLD SIGTERM
179 #define MS_SYNC 0
180 #define O_SYNC 0
181 #define O_DIRECT 0
182 #define LHANDLE HMODULE
183 #ifndef APP_BUILD
184 #define DllExport __declspec( dllexport )
185 #endif
186 typedef signed short int SWORD;
187 #define SQL_NOUNICODEMAP //Note: to suppress mapping to unicode APIs
188 enum FileOpenMode
190 fileOpenReadOnly = _O_RDONLY,
191 fileOpenWriteOnly = _O_WRONLY,
192 fileOpenReadWrite = _O_RDWR,
193 fileOpenAppend = _O_CREAT| _O_APPEND |_O_RDWR,
194 fileOpenCreat = _O_CREAT |_O_RDWR,
195 // If set and fileOpenExcl is set, the
196 // open will fail if the file already exists.
197 fileOpenExcl = _O_EXCL,
198 fileOpenTrunc = _O_TRUNC
200 enum MapProtMode
202 mapProtRead = PAGE_READONLY,
203 mapProtWrite = PAGE_READWRITE,
204 mapProcExec = PAGE_READWRITE, //execute is not supported in win2k
205 mapProcNone = PAGE_READWRITE // exec and none mode is not used in code
208 //Mapping all modes to dummy for now
209 enum MapMode
211 mapShared = PAGE_READWRITE ,
212 mapPrivate = PAGE_READWRITE ,
213 mapFixed = PAGE_READWRITE ,
214 mapNoReserve = PAGE_READWRITE ,
215 mapSync = PAGE_READWRITE ,
216 mapASync = PAGE_READWRITE
219 extern DllExport int optind, opterr;
220 extern DllExport TCHAR *optarg;
222 #endif
224 class DllExport os
226 public:
227 static void* mmap(void* addr, size_t len, int prot, int flags, file_desc fildes, off_t off);
228 static int munmap(caddr_t addr, size_t len);
229 static int open(const char *name, FileOpenMode flags, size_t size);
230 static int close(int fd);
231 static file_desc openFile(const char *name, FileOpenMode flags, size_t size);
232 static int closeFile(file_desc fd);
233 static int lockFile(int fd);
234 static int unlockFile(int fd);
235 static int truncate(const char* fname);
236 static off_t lseek(int fildes, off_t offset, int whence);
237 static int openFileForAppend(const char* fname, int flags);
238 static int getFileSize(const char* fname);
239 static size_t write(int fildes, char *buf, size_t size);
240 static int msync(caddr_t addr, size_t len, int flags);
241 static int fsync(int fildes);
242 inline static size_t alignLong(size_t size)
243 { return ((size - 1) | (sizeof(long) - 1)) + 1;}
244 inline static size_t align(size_t size)
245 { return ((size - 1) | (sizeof(InUse) - 1)) + 1;}
246 static char* encrypt(const char * key, const char *salt);
247 static void* memset(void *src, int c, size_t size);
248 static void* memcpy(void *src, const void *dest, size_t size);
249 static int memcmp(const void *s1, const void *s2, size_t size);
250 static int select(int nfds, fd_set *readfds, fd_set *writefds,
251 fd_set *exceptfds, struct timeval * timeout);
253 static int usleep(int microsecs);
254 static int sleep(int secs);
255 static shared_memory_id shm_create(shared_memory_key key, size_t size, int flag);
256 static shared_memory_id shm_open(shared_memory_key key, size_t size, int flag);
257 static void* shm_attach(shared_memory_id id, const void *ptr, int flag);
258 static int shm_detach (void*);
259 static int shmctl(int shmid, int cmd);
260 static int shm_remove(int shmid);
261 inline static double floor(double val)
262 { return ::floor(val); }
263 static sighandler_t signal(int signum, sighandler_t handler);
265 static int gettimeofday(struct timeval *tp);
266 static struct tm* localtime(long *secs);
267 static pid_t getpid();
268 static pthread_t getthrid();
269 static char* getenv(const char *envVarName);
270 static int setenv(const char *envVarName, const char *value);
272 static int kill(pid_t pid, int sig);
273 static bool atobool(char *value);
274 static pid_t createProcess(const char* cmdName, const char *arg0);
275 static pid_t fork();
276 static size_t send(int fd, const void *buf, size_t len, int flags);
277 static size_t recv(int fd, void *buf, size_t len, int flags);
278 static int gethostname(char *hostname, size_t len);
279 static int strmatch(char *pattern, char *str);
280 static int msgget(key_t key, int oflag);
281 static int msgsnd(int msqid, const void *ptr, size_t len, int flag);
282 static int msgrcv(int msqid, void *ptr, size_t len, long type, int flag);
283 static int msgctl(int msqid, int cmd, struct msqid_ds *buff);
284 static int isValidIP(char ipstr[] );
285 static bool fileExists(char *fileName);
286 static char* strcasestr(char *s1, const char *s2);
287 static int getNoOfProcessors();
288 static mode_t umask(mode_t mask);
289 static int fdatasync(file_desc fd);
290 static int atexit(void (*exitHndlr)(void));
291 static void* dlsym(LHANDLE hdl, char* funcName);
292 static char getopt(int argc, char *argv[], char *opt);
295 #endif