1 /***************************************************************************
3 * Copyright (C) Lakshya Solutions Ltd. All rights reserved. *
5 ***************************************************************************/
11 #if defined(SOLARIS) || defined(LINUX) || defined(FreeBSD)
20 #include <sys/types.h>
28 #include <sys/socket.h>
33 #include <arpa/inet.h>
34 #include <netinet/in.h>
35 #include <netinet/tcp.h>
39 #include <sys/varargs.h>
42 #include <sys/atomic.h>
50 #include <sys/errno.h>
54 #include <sys/errno.h>
57 typedef void (*sighandler_t
)(int);
61 fileOpenReadOnly
= O_RDONLY
,
62 fileOpenWriteOnly
= O_WRONLY
,
63 fileOpenReadWrite
= O_RDWR
,
64 fileOpenAppend
= O_CREAT
| O_APPEND
|O_RDWR
,
65 fileOpenCreat
= O_CREAT
|O_RDWR
,
66 // If set and fileOpenExcl is set, the
67 // open will fail if the file already exists.
68 fileOpenExcl
= O_EXCL
,
69 fileOpenTrunc
= O_TRUNC
74 mapProtRead
= PROT_READ
,
75 mapProtWrite
= PROT_WRITE
,
76 mapProcExec
= PROT_EXEC
,
77 mapProcNone
= PROT_NONE
82 mapShared
= MAP_SHARED
,
83 mapPrivate
= MAP_PRIVATE
,
85 // Interpret address exactly.
86 mapNoReserve
= MAP_NORESERVE
,
87 // Don't reserver swap space.
89 //mapAlign = MAP_ALIGN,
90 //for msync system call
96 #define SQL_STMT_LEN 8192
97 #define MAX_FILE_LEN 1024
98 #define IDENTIFIER_LENGTH 64
99 #define ERROR_STRING_LENGTH 128
100 #define DEFAULT_VALUE_BUF_LENGTH 32
101 #define STATE_LENGTH 8
102 #define SYSTEMDB "SYSTEMDB"
103 #define DBAUSER "root"
104 #define DBAPASS "manager"
105 #define I_USER "i@1r4D_f$_a"
106 #define I_PASS "a_$f_D4r1@i"
107 #define DEFAULT_CONFIG_FILE "/etc/csql/csql.conf"
108 #define LOCK_BUCKET_SIZE 2048
109 #define STMT_BUCKET_SIZE 1023
110 #define MAX_CHUNKS 20
111 #define PAGE_SIZE Conf::config.getPageSize()
112 #define MAX_MUTEX_PER_THREAD 4
113 #define MAX_THREADS_PER_PROCESS 30
114 #define MAX_FILE_PATH_LEN 1024
115 #define CHUNK_NAME_LEN 64
116 #define LOG_ROLLOVER_SIZE 20*1024*1024
117 #define SIGCSQL1 SIGUSR1
119 #define BIT(x) (1 << (x))
120 #define SETBITS(x,y) ((x) |= (y))
121 #define CLEARBITS(x,y) ((x) &= (~(y)))
122 #define SETBIT(x,y) SETBITS((x), (BIT((y))))
123 #define CLEARBIT(x,y) CLEARBITS((x), (BIT((y))))
124 #define BITSET(x,y) ((x) & (BIT(y)))
127 typedef key_t shared_memory_key
;
128 typedef int shared_memory_id
;
129 #if defined(__sparcv9)
140 static void* mmap(void* addr
, size_t len
, int prot
, int flags
, int fildes
, off_t off
);
141 static int munmap(caddr_t addr
, size_t len
);
142 static int openFile(const char *name
, FileOpenMode flags
, size_t size
);
143 static int closeFile(int fd
);
144 static int lockFile(int fd
);
145 static int unlockFile(int fd
);
146 static off_t
lseek(int fildes
, off_t offset
, int whence
);
147 static int openFileForAppend(const char* fname
, int flags
);
148 static int getFileSize(const char* fname
);
149 static size_t write(int fildes
, char *buf
, size_t size
);
150 static int msync(caddr_t addr
, size_t len
, int flags
);
151 static int fsync(int fildes
);
152 inline static size_t alignLong(size_t size
)
153 { return ((size
- 1) | (sizeof(long) - 1)) + 1;}
154 inline static size_t align(size_t size
)
155 { return ((size
- 1) | (sizeof(InUse
) - 1)) + 1;}
156 static char* encrypt(const char * key
, const char *salt
);
157 static void* memset(void *src
, int c
, size_t size
);
158 static void* memcpy(void *src
, const void *dest
, size_t size
);
159 static int memcmp(const void *s1
, const void *s2
, size_t size
);
160 static int select(int nfds
, fd_set
*readfds
, fd_set
*writefds
,
161 fd_set
*exceptfds
, struct timeval
* timeout
);
163 static int usleep(int microsecs
);
164 static int sleep(int secs
);
165 static shared_memory_id
shm_create(shared_memory_key key
, size_t size
, int flag
);
166 static shared_memory_id
shm_open(shared_memory_key key
, size_t size
, int flag
);
167 static void* shm_attach(shared_memory_id id
, const void *ptr
, int flag
);
168 static int shm_detach (void*);
169 static int shmctl(int shmid
, int cmd
);
170 inline static double floor(double val
)
171 { return ::floor(val
); }
172 static sighandler_t
signal(int signum
, sighandler_t handler
);
174 static int gettimeofday(struct timeval
*tp
);
175 static struct tm
* localtime(long *secs
);
176 static pid_t
getpid()
177 { return ::getpid(); }
178 static pthread_t
getthrid()
179 { return ::pthread_self(); }
180 static char* getenv(const char *envVarName
);
181 static int setenv(const char *envVarName
, const char *value
);
183 static int kill(pid_t pid
, int sig
);
184 static bool atobool(char *value
);
185 static pid_t
createProcess(const char* cmdName
, const char *arg0
);
187 static size_t send(int fd
, const void *buf
, size_t len
, int flags
);
188 static size_t recv(int fd
, void *buf
, size_t len
, int flags
);
189 static int gethostname(char *hostname
, size_t len
);
190 static int strmatch(char *pattern
, char *str
);
191 static int msgget(key_t key
, int oflag
);
192 static int msgsnd(int msqid
, const void *ptr
, size_t len
, int flag
);
193 static ssize_t
msgrcv(int msqid
, void *ptr
, size_t len
, long type
, int flag
);
194 static int msgctl(int msqid
, int cmd
, struct msqid_ds
*buff
);
195 static int isValidIP(char ipstr
[] );
196 static bool fileExists(char *fileName
);
197 static char* strcasestr(char *s1
, const char *s2
);
198 static int getNoOfProcessors();
199 static mode_t
umask(mode_t mask
);
200 static int fdatasync(int fd
);
201 static int atexit(void (*exitHndlr
)(void));