Enterprise to opensource. 40 files including Makefil.am and .in from storage, sqllog...
[csql.git] / include / os.h
blob0c5cb9d676b46f16c4c3ad1ccf813506bfd102b8
1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 ***************************************************************************/
16 #ifndef OS_H
17 #define OS_H
18 #include<build.h>
20 #if defined(solaris) || defined(LINUX)
22 #include <stdio.h>
23 #include <sys/mman.h>
24 #include <sys/shm.h>
25 #include <sys/errno.h>
26 #include <crypt.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <fcntl.h>
33 #include <pthread.h>
34 #include <math.h>
35 #include <limits.h>
36 #include <signal.h>
37 #include <ctype.h>
38 #include <sys/socket.h>
39 #include <fnmatch.h>
40 #include <execinfo.h>
41 #if defined(solaris)
42 #include <sys/varargs.h>
43 #endif
44 #if defined(LINUX)
45 #include <sys/time.h>
46 #include <stdarg.h>
47 #endif
48 typedef void (*sighandler_t)(int);
50 enum FileOpenMode
52 fileOpenReadOnly = O_RDONLY,
53 fileOpenWriteOnly = O_WRONLY,
54 fileOpenReadWrite = O_RDWR,
55 fileOpenAppend = O_APPEND |O_RDWR,
56 fileOpenCreat = O_CREAT |O_RDWR,
57 // If set and fileOpenExcl is set, the
58 // open will fail if the file already exists.
59 fileOpenExcl = O_EXCL,
60 fileOpenTrunc = O_TRUNC
63 enum MapProtMode
65 mapProtRead = PROT_READ,
66 mapProtWrite = PROT_WRITE,
67 mapProcExec = PROT_EXEC,
68 mapProcNone = PROT_NONE
71 enum MapMode
73 mapShared = MAP_SHARED,
74 mapPrivate = MAP_PRIVATE,
75 mapFixed = MAP_FIXED,
76 // Interpret address exactly.
77 mapNoReserve = MAP_NORESERVE,
78 // Don't reserver swap space.
80 //mapAlign = MAP_ALIGN,
81 //for msync system call
82 mapSync = MS_SYNC ,
83 mapASync = MS_ASYNC
87 #define MAX_FILE_LEN 1024
88 #define IDENTIFIER_LENGTH 64
89 #define ERROR_STRING_LENGTH 128
90 #define DEFAULT_VALUE_BUF_LENGTH 32
91 #define SYSTEMDB "SYSTEMDB"
92 #define DBAUSER "root"
93 #define DBAPASS "manager"
94 #define LOCK_BUCKET_SIZE 2048
95 #define STMT_BUCKET_SIZE 1023
96 #define MAX_CHUNKS 20
97 #define PAGE_SIZE Conf::config.getPageSize()
98 #define MAX_MUTEX_PER_THREAD 3
99 #define MAX_THREADS_PER_PROCESS 30
100 #define MAX_FILE_PATH_LEN 1024
101 #define CHUNK_NAME_LEN 64
103 #define BIT(x) (1 << (x))
104 #define SETBITS(x,y) ((x) |= (y))
105 #define CLEARBITS(x,y) ((x) &= (~(y)))
106 #define SETBIT(x,y) SETBITS((x), (BIT((y))))
107 #define CLEARBIT(x,y) CLEARBITS((x), (BIT((y))))
108 #define BITSET(x,y) ((x) & (BIT(y)))
111 typedef key_t shared_memory_key;
112 typedef int shared_memory_id;
114 #endif
117 class os
119 public:
120 static caddr_t mmap(caddr_t addr, size_t len, int prot, int flags, int fildes, off_t off);
121 static int munmap(caddr_t addr, size_t len);
122 static int openFile(const char *name, FileOpenMode flags, size_t size);
123 static int closeFile(int fd);
124 static off_t lseek(int fildes, off_t offset, int whence);
125 static size_t write(int fildes, char *buf, size_t size);
126 static int msync(caddr_t addr, size_t len, int flags);
127 static int fsync(int fildes);
128 inline static size_t alignLong(size_t size)
129 { return ((size - 1) | (sizeof(long) - 1)) + 1;}
130 inline static size_t align(size_t size)
131 { return ((size - 1) | (sizeof(long) - 1)) + 1;}
132 static char* encrypt(const char * key, const char *salt);
133 static void* memset(void *src, int c, size_t size);
134 static void* memcpy(void *src, const void *dest, size_t size);
135 static int memcmp(const void *s1, const void *s2, size_t size);
136 static int select(int nfds, fd_set *readfds, fd_set *writefds,
137 fd_set *exceptfds, struct timeval * timeout);
139 static int usleep(int microsecs);
140 static int sleep(int secs);
141 static shared_memory_id shm_create(shared_memory_key key, size_t size, int flag);
142 static shared_memory_id shm_open(shared_memory_key key, size_t size, int flag);
143 static void* shm_attach(shared_memory_id id, const void *ptr, int flag);
144 static int shm_detach (void*);
145 static int shmctl(int shmid, int cmd);
146 inline static double floor(double val)
147 { return ::floor(val); }
148 static sighandler_t signal(int signum, sighandler_t handler);
150 static int gettimeofday(struct timeval *tp);
151 static struct tm* localtime(long *secs);
152 static pid_t getpid()
153 { return ::getpid(); }
154 static pthread_t getthrid()
155 { return ::pthread_self(); }
156 static char* getenv(const char *envVarName);
157 static int setenv(const char *envVarName, const char *value);
159 static int kill(pid_t pid, int sig);
160 static bool atobool(char *value);
161 static pid_t createProcess(const char* cmdName, const char *arg0, ...);
162 static pid_t fork();
163 static size_t send(int fd, const void *buf, size_t len, int flags);
164 static size_t recv(int fd, void *buf, size_t len, int flags);
165 static int gethostname(char *hostname, size_t len);
166 static int strmatch(char *pattern, char *str);
169 #endif