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
42 #define MAP_ADDR_VALUE 399998976L
44 #define BIT(x) (1 << (x))
45 #define SETBITS(x,y) ((x) |= (y))
46 #define CLEARBITS(x,y) ((x) &= (~(y)))
47 #define SETBIT(x,y) SETBITS((x), (BIT((y))))
48 #define CLEARBIT(x,y) CLEARBITS((x), (BIT((y))))
49 #define BITSET(x,y) ((x) & (BIT(y)))
51 typedef void (*sighandler_t
)(int);
53 #define LENGTH socklen_t
57 #include <sys/types.h>
66 #if defined(SOLARIS) || defined(LINUX) || defined(FreeBSD)
74 #include <sys/socket.h>
79 #include <arpa/inet.h>
80 #include <netinet/in.h>
81 #include <netinet/tcp.h>
86 #include <sys/varargs.h>
89 #include <sys/atomic.h>
97 #include <sys/errno.h>
101 #include <sys/errno.h>
108 fileOpenReadOnly
= O_RDONLY
,
109 fileOpenWriteOnly
= O_WRONLY
,
110 fileOpenReadWrite
= O_RDWR
,
111 fileOpenAppend
= O_CREAT
| O_APPEND
|O_RDWR
,
112 fileOpenCreat
= O_CREAT
|O_RDWR
,
113 // If set and fileOpenExcl is set, the
114 // open will fail if the file already exists.
115 fileOpenExcl
= O_EXCL
,
116 fileOpenTrunc
= O_TRUNC
121 mapProtRead
= PROT_READ
,
122 mapProtWrite
= PROT_WRITE
,
123 mapProcExec
= PROT_EXEC
,
124 mapProcNone
= PROT_NONE
129 mapShared
= MAP_SHARED
,
130 mapPrivate
= MAP_PRIVATE
,
131 mapFixed
= MAP_FIXED
,
132 // Interpret address exactly.
133 mapNoReserve
= MAP_NORESERVE
,
134 // Don't reserver swap space.
136 //mapAlign = MAP_ALIGN,
137 //for msync system call
142 typedef int file_desc
;
143 typedef key_t shared_memory_key
;
144 typedef int shared_memory_id
;
145 #if defined(__sparcv9)
154 #include <winsock2.h>
158 #define snprintf sprintf_s
159 #define strncasecmp _strnicmp
160 #define strcasecmp strcmpi
161 #define vsnprintf vsnprintf_s
162 #define strtok_r strtok_s
163 typedef HANDLE file_desc
;
165 typedef void* caddr_t
;
168 typedef int pthread_t
;
171 typedef int shared_memory_key
;
172 typedef int shared_memory_id
;
173 typedef size_t socklen_t
;
174 #define MAP_FAILED -1
175 #define MSG_NOSIGNAL 0
178 #define SQL_API __declspec(dllexport)
179 #define ALLREADY_HAVE_WINDOWS_TYPE
180 #define SIGCSQL1 0 //drop table signal wont work for windoes
181 #define SIGCHLD SIGTERM
186 #define LHANDLE HMODULE
188 #define DllExport __declspec( dllexport )
192 typedef signed short int SWORD
;
193 #define SQL_NOUNICODEMAP //Note: to suppress mapping to unicode APIs
196 fileOpenReadOnly
= _O_RDONLY
,
197 fileOpenWriteOnly
= _O_WRONLY
,
198 fileOpenReadWrite
= _O_RDWR
,
199 fileOpenAppend
= _O_CREAT
| _O_APPEND
|_O_RDWR
,
200 fileOpenCreat
= _O_CREAT
|_O_RDWR
,
201 // If set and fileOpenExcl is set, the
202 // open will fail if the file already exists.
203 fileOpenExcl
= _O_EXCL
,
204 fileOpenTrunc
= _O_TRUNC
208 mapProtRead
= PAGE_READONLY
,
209 mapProtWrite
= PAGE_READWRITE
,
210 mapProcExec
= PAGE_READWRITE
, //execute is not supported in win2k
211 mapProcNone
= PAGE_READWRITE
// exec and none mode is not used in code
214 //Mapping all modes to dummy for now
217 mapShared
= PAGE_READWRITE
,
218 mapPrivate
= PAGE_READWRITE
,
219 mapFixed
= PAGE_READWRITE
,
220 mapNoReserve
= PAGE_READWRITE
,
221 mapSync
= PAGE_READWRITE
,
222 mapASync
= PAGE_READWRITE
225 extern DllExport
int optind
, opterr
;
226 extern DllExport TCHAR
*optarg
;
233 static void* mmap(void* addr
, size_t len
, int prot
, int flags
, file_desc fildes
, off_t off
);
234 static int munmap(caddr_t addr
, size_t len
);
235 static int open(const char *name
, FileOpenMode flags
, size_t size
);
236 static int close(int fd
);
237 static file_desc
openFile(const char *name
, FileOpenMode flags
, size_t size
=0);
238 static int closeFile(file_desc fd
);
239 static int closeSocket(int fd
);
240 static int lockFile(int fd
);
241 static int unlockFile(int fd
);
242 static int truncate(const char* fname
);
243 static off_t
lseek(int fildes
, off_t offset
, int whence
);
244 static int openFileForAppend(const char* fname
, int flags
);
245 static int getFileSize(const char* fname
);
246 static size_t write(int fildes
, char *buf
, size_t size
);
247 static int msync(caddr_t addr
, size_t len
, int flags
);
248 static int fsync(int fildes
);
249 inline static size_t alignLong(size_t size
)
250 { return ((size
- 1) | (sizeof(long) - 1)) + 1;}
251 inline static size_t align(size_t size
)
252 { return ((size
- 1) | (sizeof(InUse
) - 1)) + 1;}
253 static char* encrypt(const char * key
, const char *salt
);
254 static void* memset(void *src
, int c
, size_t size
);
255 static void* memcpy(void *src
, const void *dest
, size_t size
);
256 static int memcmp(const void *s1
, const void *s2
, size_t size
);
257 static int select(int nfds
, fd_set
*readfds
, fd_set
*writefds
,
258 fd_set
*exceptfds
, struct timeval
* timeout
);
260 static int usleep(int microsecs
);
261 static int sleep(int secs
);
262 static shared_memory_id
shm_create(shared_memory_key key
, size_t size
, int flag
);
263 static shared_memory_id
shm_open(shared_memory_key key
, size_t size
, int flag
);
264 static void* shm_attach(shared_memory_id id
, const void *ptr
, int flag
);
265 static int shm_detach (void*);
266 static int shmctl(int shmid
, int cmd
);
267 static int shm_remove(int shmid
);
268 inline static double floor(double val
)
269 { return ::floor(val
); }
270 static sighandler_t
signal(int signum
, sighandler_t handler
);
272 static int gettimeofday(struct timeval
*tp
);
273 static struct tm
* localtime(long *secs
);
274 static pid_t
getpid();
275 static pthread_t
getthrid();
276 static char* getenv(const char *envVarName
);
277 static int setenv(const char *envVarName
, const char *value
);
279 static int kill(pid_t pid
, int sig
);
280 static bool atobool(char *value
);
281 static pid_t
createProcess(const char* cmdName
, const char *arg0
);
283 static size_t send(int fd
, const void *buf
, size_t len
, int flags
);
284 static size_t recv(int fd
, void *buf
, size_t len
, int flags
);
285 static int gethostname(char *hostname
, size_t len
);
286 static int strmatch(char *pattern
, char *str
);
287 static int msgget(key_t key
, int oflag
);
288 static int msgsnd(int msqid
, const void *ptr
, size_t len
, int flag
);
289 static int msgrcv(int msqid
, void *ptr
, size_t len
, long type
, int flag
);
290 static int msgctl(int msqid
, int cmd
, struct msqid_ds
*buff
);
291 static int isValidIP(char ipstr
[] );
292 static bool fileExists(char *fileName
);
293 static char* strcasestr(char *s1
, const char *s2
);
294 static int getNoOfProcessors();
295 static mode_t
umask(mode_t mask
);
296 static int fdatasync(file_desc fd
);
297 static int atexit(void (*exitHndlr
)(void));
298 static void* dlsym(LHANDLE hdl
, char* funcName
);
299 static char getopt(int argc
, char *argv
[], char *opt
);