1 /*************************************************************************
3 * Copyright (C) Lakshya Solutions Ltd. All rights reserved. *
5 ***************************************************************************/
11 //Added for gettimeofday impl for windows
12 #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
13 #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
15 #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
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
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)))
50 typedef void (*sighandler_t
)(int);
55 #include <sys/types.h>
64 #if defined(SOLARIS) || defined(LINUX) || defined(FreeBSD)
73 #include <sys/socket.h>
78 #include <arpa/inet.h>
79 #include <netinet/in.h>
80 #include <netinet/tcp.h>
85 #include <sys/varargs.h>
88 #include <sys/atomic.h>
96 #include <sys/errno.h>
100 #include <sys/errno.h>
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
119 mapProtRead
= PROT_READ
,
120 mapProtWrite
= PROT_WRITE
,
121 mapProcExec
= PROT_EXEC
,
122 mapProcNone
= PROT_NONE
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
140 typedef int file_desc
;
141 typedef key_t shared_memory_key
;
142 typedef int shared_memory_id
;
143 #if defined(__sparcv9)
152 #include <winsock2.h>
154 #define snprintf sprintf_s
155 #define strncasecmp _strnicmp
156 #define strcasecmp strcmpi
157 #define vsnprintf vsnprintf_s
158 typedef HANDLE file_desc
;
160 typedef void* caddr_t
;
163 typedef int pthread_t
;
166 typedef int shared_memory_key
;
167 typedef int shared_memory_id
;
168 #define MAP_FAILED -1
171 #define ALLREADY_HAVE_WINDOWS_TYPE
173 #define LHANDLE HMODULE
174 #define DllExport __declspec( dllexport )
178 fileOpenReadOnly
= _O_RDONLY
,
179 fileOpenWriteOnly
= _O_WRONLY
,
180 fileOpenReadWrite
= _O_RDWR
,
181 fileOpenAppend
= _O_CREAT
| _O_APPEND
|_O_RDWR
,
182 fileOpenCreat
= _O_CREAT
|_O_RDWR
,
183 // If set and fileOpenExcl is set, the
184 // open will fail if the file already exists.
185 fileOpenExcl
= _O_EXCL
,
186 fileOpenTrunc
= _O_TRUNC
190 mapProtRead
= PAGE_READONLY
,
191 mapProtWrite
= PAGE_READWRITE
,
192 mapProcExec
= PAGE_READWRITE
, //execute is not supported in win2k
193 mapProcNone
= PAGE_READWRITE
// exec and none mode is not used in code
196 //Mapping all modes to dummy for now
199 mapShared
= PAGE_READWRITE
,
200 mapPrivate
= PAGE_READWRITE
,
201 mapFixed
= PAGE_READWRITE
,
202 mapNoReserve
= PAGE_READWRITE
,
203 mapSync
= PAGE_READWRITE
,
204 mapASync
= PAGE_READWRITE
212 static void* mmap(void* addr
, size_t len
, int prot
, int flags
, file_desc fildes
, off_t off
);
213 static int munmap(caddr_t addr
, size_t len
);
214 static int open(const char *name
, FileOpenMode flags
, size_t size
);
215 static int close(int fd
);
216 static file_desc
openFile(const char *name
, FileOpenMode flags
, size_t size
);
217 static int closeFile(file_desc fd
);
218 static int lockFile(int fd
);
219 static int unlockFile(int fd
);
220 static int truncate(const char* fname
);
221 static off_t
lseek(int fildes
, off_t offset
, int whence
);
222 static int openFileForAppend(const char* fname
, int flags
);
223 static int getFileSize(const char* fname
);
224 static size_t write(int fildes
, char *buf
, size_t size
);
225 static int msync(caddr_t addr
, size_t len
, int flags
);
226 static int fsync(int fildes
);
227 inline static size_t alignLong(size_t size
)
228 { return ((size
- 1) | (sizeof(long) - 1)) + 1;}
229 inline static size_t align(size_t size
)
230 { return ((size
- 1) | (sizeof(InUse
) - 1)) + 1;}
231 static char* encrypt(const char * key
, const char *salt
);
232 static void* memset(void *src
, int c
, size_t size
);
233 static void* memcpy(void *src
, const void *dest
, size_t size
);
234 static int memcmp(const void *s1
, const void *s2
, size_t size
);
235 static int select(int nfds
, fd_set
*readfds
, fd_set
*writefds
,
236 fd_set
*exceptfds
, struct timeval
* timeout
);
238 static int usleep(int microsecs
);
239 static int sleep(int secs
);
240 static shared_memory_id
shm_create(shared_memory_key key
, size_t size
, int flag
);
241 static shared_memory_id
shm_open(shared_memory_key key
, size_t size
, int flag
);
242 static void* shm_attach(shared_memory_id id
, const void *ptr
, int flag
);
243 static int shm_detach (void*);
244 static int shmctl(int shmid
, int cmd
);
245 static int shm_remove(int shmid
);
246 inline static double floor(double val
)
247 { return ::floor(val
); }
248 static sighandler_t
signal(int signum
, sighandler_t handler
);
250 static int gettimeofday(struct timeval
*tp
);
251 static struct tm
* localtime(long *secs
);
252 static pid_t
getpid();
253 static pthread_t
getthrid();
254 static char* getenv(const char *envVarName
);
255 static int setenv(const char *envVarName
, const char *value
);
257 static int kill(pid_t pid
, int sig
);
258 static bool atobool(char *value
);
259 static pid_t
createProcess(const char* cmdName
, const char *arg0
);
261 static size_t send(int fd
, const void *buf
, size_t len
, int flags
);
262 static size_t recv(int fd
, void *buf
, size_t len
, int flags
);
263 static int gethostname(char *hostname
, size_t len
);
264 static int strmatch(char *pattern
, char *str
);
265 static int msgget(key_t key
, int oflag
);
266 static int msgsnd(int msqid
, const void *ptr
, size_t len
, int flag
);
267 static int msgrcv(int msqid
, void *ptr
, size_t len
, long type
, int flag
);
268 static int msgctl(int msqid
, int cmd
, struct msqid_ds
*buff
);
269 static int isValidIP(char ipstr
[] );
270 static bool fileExists(char *fileName
);
271 static char* strcasestr(char *s1
, const char *s2
);
272 static int getNoOfProcessors();
273 static mode_t
umask(mode_t mask
);
274 static int fdatasync(file_desc fd
);
275 static int atexit(void (*exitHndlr
)(void));
276 static void* dlsym(LHANDLE hdl
, char* funcName
);