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);
52 #define LENGTH socklen_t
56 #include <sys/types.h>
65 #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>
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
120 mapProtRead
= PROT_READ
,
121 mapProtWrite
= PROT_WRITE
,
122 mapProcExec
= PROT_EXEC
,
123 mapProcNone
= PROT_NONE
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
141 typedef int file_desc
;
142 typedef key_t shared_memory_key
;
143 typedef int shared_memory_id
;
144 #if defined(__sparcv9)
153 #include <winsock2.h>
157 #define snprintf sprintf_s
158 #define strncasecmp _strnicmp
159 #define strcasecmp strcmpi
160 #define vsnprintf vsnprintf_s
161 #define strtok_r strtok_s
162 typedef HANDLE file_desc
;
164 typedef void* caddr_t
;
167 typedef int pthread_t
;
170 typedef int shared_memory_key
;
171 typedef int shared_memory_id
;
172 typedef size_t socklen_t
;
173 #define MAP_FAILED -1
174 #define MSG_NOSIGNAL 0
177 #define SQL_API __declspec(dllexport)
178 #define ALLREADY_HAVE_WINDOWS_TYPE
179 #define SIGCSQL1 0 //drop table signal wont work for windoes
180 #define SIGCHLD SIGTERM
185 #define LHANDLE HMODULE
187 #define DllExport __declspec( dllexport )
191 typedef signed short int SWORD
;
192 #define SQL_NOUNICODEMAP //Note: to suppress mapping to unicode APIs
195 fileOpenReadOnly
= _O_RDONLY
,
196 fileOpenWriteOnly
= _O_WRONLY
,
197 fileOpenReadWrite
= _O_RDWR
,
198 fileOpenAppend
= _O_CREAT
| _O_APPEND
|_O_RDWR
,
199 fileOpenCreat
= _O_CREAT
|_O_RDWR
,
200 // If set and fileOpenExcl is set, the
201 // open will fail if the file already exists.
202 fileOpenExcl
= _O_EXCL
,
203 fileOpenTrunc
= _O_TRUNC
207 mapProtRead
= PAGE_READONLY
,
208 mapProtWrite
= PAGE_READWRITE
,
209 mapProcExec
= PAGE_READWRITE
, //execute is not supported in win2k
210 mapProcNone
= PAGE_READWRITE
// exec and none mode is not used in code
213 //Mapping all modes to dummy for now
216 mapShared
= PAGE_READWRITE
,
217 mapPrivate
= PAGE_READWRITE
,
218 mapFixed
= PAGE_READWRITE
,
219 mapNoReserve
= PAGE_READWRITE
,
220 mapSync
= PAGE_READWRITE
,
221 mapASync
= PAGE_READWRITE
224 extern DllExport
int optind
, opterr
;
225 extern DllExport TCHAR
*optarg
;
232 static void* mmap(void* addr
, size_t len
, int prot
, int flags
, file_desc fildes
, off_t off
);
233 static int munmap(caddr_t addr
, size_t len
);
234 static int open(const char *name
, FileOpenMode flags
, size_t size
);
235 static int close(int fd
);
236 static file_desc
openFile(const char *name
, FileOpenMode flags
, size_t size
=0);
237 static int closeFile(file_desc fd
);
238 static int closeSocket(int fd
);
239 static int lockFile(int fd
);
240 static int unlockFile(int fd
);
241 static int truncate(const char* fname
);
242 static off_t
lseek(int fildes
, off_t offset
, int whence
);
243 static int openFileForAppend(const char* fname
, int flags
);
244 static int getFileSize(const char* fname
);
245 static size_t write(int fildes
, char *buf
, size_t size
);
246 static int msync(caddr_t addr
, size_t len
, int flags
);
247 static int fsync(int fildes
);
248 inline static size_t alignLong(size_t size
)
249 { return ((size
- 1) | (sizeof(long) - 1)) + 1;}
250 inline static size_t align(size_t size
)
251 { return ((size
- 1) | (sizeof(InUse
) - 1)) + 1;}
252 static char* encrypt(const char * key
, const char *salt
);
253 static void* memset(void *src
, int c
, size_t size
);
254 static void* memcpy(void *src
, const void *dest
, size_t size
);
255 static int memcmp(const void *s1
, const void *s2
, size_t size
);
256 static int select(int nfds
, fd_set
*readfds
, fd_set
*writefds
,
257 fd_set
*exceptfds
, struct timeval
* timeout
);
259 static int usleep(int microsecs
);
260 static int sleep(int secs
);
261 static shared_memory_id
shm_create(shared_memory_key key
, size_t size
, int flag
);
262 static shared_memory_id
shm_open(shared_memory_key key
, size_t size
, int flag
);
263 static void* shm_attach(shared_memory_id id
, const void *ptr
, int flag
);
264 static int shm_detach (void*);
265 static int shmctl(int shmid
, int cmd
);
266 static int shm_remove(int shmid
);
267 inline static double floor(double val
)
268 { return ::floor(val
); }
269 static sighandler_t
signal(int signum
, sighandler_t handler
);
271 static int gettimeofday(struct timeval
*tp
);
272 static struct tm
* localtime(long *secs
);
273 static pid_t
getpid();
274 static pthread_t
getthrid();
275 static char* getenv(const char *envVarName
);
276 static int setenv(const char *envVarName
, const char *value
);
278 static int kill(pid_t pid
, int sig
);
279 static bool atobool(char *value
);
280 static pid_t
createProcess(const char* cmdName
, const char *arg0
);
282 static size_t send(int fd
, const void *buf
, size_t len
, int flags
);
283 static size_t recv(int fd
, void *buf
, size_t len
, int flags
);
284 static int gethostname(char *hostname
, size_t len
);
285 static int strmatch(char *pattern
, char *str
);
286 static int msgget(key_t key
, int oflag
);
287 static int msgsnd(int msqid
, const void *ptr
, size_t len
, int flag
);
288 static int msgrcv(int msqid
, void *ptr
, size_t len
, long type
, int flag
);
289 static int msgctl(int msqid
, int cmd
, struct msqid_ds
*buff
);
290 static int isValidIP(char ipstr
[] );
291 static bool fileExists(char *fileName
);
292 static char* strcasestr(char *s1
, const char *s2
);
293 static int getNoOfProcessors();
294 static mode_t
umask(mode_t mask
);
295 static int fdatasync(file_desc fd
);
296 static int atexit(void (*exitHndlr
)(void));
297 static void* dlsym(LHANDLE hdl
, char* funcName
);
298 static char getopt(int argc
, char *argv
[], char *opt
);