fixing build warning messages
[csql.git] / include / os.h
blob3a343a9441af31116e9e671254a64abc2fd50b50
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 #if defined(SOLARIS) || defined(LINUX)
13 #include <stdio.h>
14 #include <sys/mman.h>
15 #include <sys/msg.h>
16 #include <sys/shm.h>
17 #include <crypt.h>
18 #include <unistd.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <pthread.h>
25 #include <math.h>
26 #include <signal.h>
27 #include <ctype.h>
28 #include <limits.h>
29 #include <sys/socket.h>
30 #include <fnmatch.h>
31 #include <sys/time.h>
32 #include <stdarg.h>
33 #include <net/if.h>
34 #include <arpa/inet.h>
35 #include <netinet/in.h>
36 #include <netinet/tcp.h>
37 #include <sys/file.h>
38 #if defined(SOLARIS)
39 #include <sys/varargs.h>
40 #include <errno.h>
41 #include <sys/atomic.h>
42 #include <timecsql.h>
43 #include <ucontext.h>
44 //extern int errno;
45 #endif
46 #if defined(LINUX)
47 #include <execinfo.h>
48 #include <ifaddrs.h>
49 #include <sys/errno.h>
50 #endif
51 typedef void (*sighandler_t)(int);
53 enum FileOpenMode
55 fileOpenReadOnly = O_RDONLY,
56 fileOpenWriteOnly = O_WRONLY,
57 fileOpenReadWrite = O_RDWR,
58 fileOpenAppend = O_CREAT| O_APPEND |O_RDWR,
59 fileOpenCreat = O_CREAT |O_RDWR,
60 // If set and fileOpenExcl is set, the
61 // open will fail if the file already exists.
62 fileOpenExcl = O_EXCL,
63 fileOpenTrunc = O_TRUNC
66 enum MapProtMode
68 mapProtRead = PROT_READ,
69 mapProtWrite = PROT_WRITE,
70 mapProcExec = PROT_EXEC,
71 mapProcNone = PROT_NONE
74 enum MapMode
76 mapShared = MAP_SHARED,
77 mapPrivate = MAP_PRIVATE,
78 mapFixed = MAP_FIXED,
79 // Interpret address exactly.
80 mapNoReserve = MAP_NORESERVE,
81 // Don't reserver swap space.
83 //mapAlign = MAP_ALIGN,
84 //for msync system call
85 mapSync = MS_SYNC ,
86 mapASync = MS_ASYNC
90 #define MAX_FILE_LEN 1024
91 #define IDENTIFIER_LENGTH 64
92 #define ERROR_STRING_LENGTH 128
93 #define DEFAULT_VALUE_BUF_LENGTH 32
94 #define STATE_LENGTH 8
95 #define SYSTEMDB "SYSTEMDB"
96 #define DBAUSER "root"
97 #define DBAPASS "manager"
98 #define I_USER "i@1r4D_f$_a"
99 #define I_PASS "a_$f_D4r1@i"
100 #define DEFAULT_CONFIG_FILE "/etc/csql/csql.conf"
101 #define LOCK_BUCKET_SIZE 2048
102 #define STMT_BUCKET_SIZE 1023
103 #define MAX_CHUNKS 20
104 #define PAGE_SIZE Conf::config.getPageSize()
105 #define MAX_MUTEX_PER_THREAD 3
106 #define MAX_THREADS_PER_PROCESS 30
107 #define MAX_FILE_PATH_LEN 1024
108 #define CHUNK_NAME_LEN 64
109 #define LOG_ROLLOVER_SIZE 20*1024*1024
111 #define BIT(x) (1 << (x))
112 #define SETBITS(x,y) ((x) |= (y))
113 #define CLEARBITS(x,y) ((x) &= (~(y)))
114 #define SETBIT(x,y) SETBITS((x), (BIT((y))))
115 #define CLEARBIT(x,y) CLEARBITS((x), (BIT((y))))
116 #define BITSET(x,y) ((x) & (BIT(y)))
119 typedef key_t shared_memory_key;
120 typedef int shared_memory_id;
121 #if defined(__sparcv9)
122 typedef long InUse;
123 #else
124 typedef int InUse;
125 #endif
127 #endif
129 class os
131 public:
132 static void* mmap(void* addr, size_t len, int prot, int flags, int fildes, off_t off);
133 static int munmap(caddr_t addr, size_t len);
134 static int openFile(const char *name, FileOpenMode flags, size_t size);
135 static int closeFile(int fd);
136 static int lockFile(int fd);
137 static int unlockFile(int fd);
138 static off_t lseek(int fildes, off_t offset, int whence);
139 static int openFileForAppend(const char* fname, int flags);
140 static int getFileSize(const char* fname);
141 static size_t write(int fildes, char *buf, size_t size);
142 static int msync(caddr_t addr, size_t len, int flags);
143 static int fsync(int fildes);
144 inline static size_t alignLong(size_t size)
145 { return ((size - 1) | (sizeof(long) - 1)) + 1;}
146 inline static size_t align(size_t size)
147 { return ((size - 1) | (sizeof(int) - 1)) + 1;}
148 static char* encrypt(const char * key, const char *salt);
149 static void* memset(void *src, int c, size_t size);
150 static void* memcpy(void *src, const void *dest, size_t size);
151 static int memcmp(const void *s1, const void *s2, size_t size);
152 static int select(int nfds, fd_set *readfds, fd_set *writefds,
153 fd_set *exceptfds, struct timeval * timeout);
155 static int usleep(int microsecs);
156 static int sleep(int secs);
157 static shared_memory_id shm_create(shared_memory_key key, size_t size, int flag);
158 static shared_memory_id shm_open(shared_memory_key key, size_t size, int flag);
159 static void* shm_attach(shared_memory_id id, const void *ptr, int flag);
160 static int shm_detach (void*);
161 static int shmctl(int shmid, int cmd);
162 inline static double floor(double val)
163 { return ::floor(val); }
164 static sighandler_t signal(int signum, sighandler_t handler);
166 static int gettimeofday(struct timeval *tp);
167 static struct tm* localtime(long *secs);
168 static pid_t getpid()
169 { return ::getpid(); }
170 static pthread_t getthrid()
171 { return ::pthread_self(); }
172 static char* getenv(const char *envVarName);
173 static int setenv(const char *envVarName, const char *value);
175 static int kill(pid_t pid, int sig);
176 static bool atobool(char *value);
177 static pid_t createProcess(const char* cmdName, const char *arg0);
178 static pid_t fork();
179 static size_t send(int fd, const void *buf, size_t len, int flags);
180 static size_t recv(int fd, void *buf, size_t len, int flags);
181 static int gethostname(char *hostname, size_t len);
182 static int strmatch(char *pattern, char *str);
183 static int msgget(key_t key, int oflag);
184 static int msgsnd(int msqid, const void *ptr, size_t len, int flag);
185 static ssize_t msgrcv(int msqid, void *ptr, size_t len, long type, int flag);
186 static int msgctl(int msqid, int cmd, struct msqid_ds *buff);
187 static int isValidIP(char ipstr[] );
188 static bool fileExists(char *fileName);
189 static char* strcasestr(char *s1, const char *s2);
190 static int getNoOfProcessors();
193 #endif