1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
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. *
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. *
15 ***************************************************************************/
20 #if defined(solaris) || defined(LINUX)
25 #include <sys/errno.h>
30 #include <sys/types.h>
37 #include <sys/socket.h>
40 #include <sys/varargs.h>
46 typedef void (*sighandler_t
)(int);
50 fileOpenReadOnly
= O_RDONLY
,
51 fileOpenWriteOnly
= O_WRONLY
,
52 fileOpenReadWrite
= O_RDWR
,
53 fileOpenAppend
= O_APPEND
|O_RDWR
,
54 fileOpenCreat
= O_CREAT
|O_RDWR
,
55 // If set and fileOpenExcl is set, the
56 // open will fail if the file already exists.
57 fileOpenExcl
= O_EXCL
,
58 fileOpenTrunc
= O_TRUNC
63 mapProtRead
= PROT_READ
,
64 mapProtWrite
= PROT_WRITE
,
65 mapProcExec
= PROT_EXEC
,
66 mapProcNone
= PROT_NONE
71 mapShared
= MAP_SHARED
,
72 mapPrivate
= MAP_PRIVATE
,
74 // Interpret address exactly.
75 mapNoReserve
= MAP_NORESERVE
,
76 // Don't reserver swap space.
78 //mapAlign = MAP_ALIGN,
79 //for msync system call
85 #define MAX_FILE_LEN 1024
86 #define IDENTIFIER_LENGTH 64
87 #define ERROR_STRING_LENGTH 128
88 #define DEFAULT_VALUE_BUF_LENGTH 32
89 #define SYSTEMDB "SYSTEMDB"
90 #define DBAUSER "root"
91 #define DBAPASS "manager"
92 #define LOCK_BUCKET_SIZE 2048
94 #define PAGE_SIZE Conf::config.getPageSize()
95 #define MAX_MUTEX_PER_THREAD 3
96 #define MAX_THREADS_PER_PROCESS 30
97 #define MAX_FILE_PATH_LEN 1024
98 #define CHUNK_NAME_LEN 64
100 #define BIT(x) (1 << (x))
101 #define SETBITS(x,y) ((x) |= (y))
102 #define CLEARBITS(x,y) ((x) &= (~(y)))
103 #define SETBIT(x,y) SETBITS((x), (BIT((y))))
104 #define CLEARBIT(x,y) CLEARBITS((x), (BIT((y))))
105 #define BITSET(x,y) ((x) & (BIT(y)))
108 typedef key_t shared_memory_key
;
109 typedef int shared_memory_id
;
117 static caddr_t
mmap(caddr_t addr
, size_t len
, int prot
, int flags
, int fildes
, off_t off
);
118 static int munmap(caddr_t addr
, size_t len
);
119 static int openFile(const char *name
, FileOpenMode flags
, size_t size
);
120 static int closeFile(int fd
);
121 static off_t
lseek(int fildes
, off_t offset
, int whence
);
122 static size_t write(int fildes
, char *buf
, size_t size
);
123 static int msync(caddr_t addr
, size_t len
, int flags
);
124 static int fsync(int fildes
);
125 inline static size_t alignLong(size_t size
)
126 { return ((size
- 1) | (sizeof(long) - 1)) + 1;}
127 inline static size_t align(size_t size
)
128 { return ((size
- 1) | (sizeof(long) - 1)) + 1;}
129 static char* encrypt(const char * key
, const char *salt
);
130 static void* memset(void *src
, int c
, size_t size
);
131 static void* memcpy(void *src
, const void *dest
, size_t size
);
132 static int memcmp(const void *s1
, const void *s2
, size_t size
);
133 static int select(int nfds
, fd_set
*readfds
, fd_set
*writefds
,
134 fd_set
*exceptfds
, struct timeval
* timeout
);
136 static int usleep(int microsecs
);
137 static int sleep(int secs
);
138 static shared_memory_id
shm_create(shared_memory_key key
, size_t size
, int flag
);
139 static shared_memory_id
shm_open(shared_memory_key key
, size_t size
, int flag
);
140 static void* shm_attach(shared_memory_id id
, const void *ptr
, int flag
);
141 static int shm_detach (void*);
142 static int shmctl(int shmid
, int cmd
);
143 inline static double floor(double val
)
144 { return ::floor(val
); }
145 static sighandler_t
signal(int signum
, sighandler_t handler
);
147 static int gettimeofday(struct timeval
*tp
);
148 static struct tm
* localtime(long *secs
);
149 static pid_t
getpid()
150 { return ::getpid(); }
151 static pthread_t
getthrid()
152 { return ::pthread_self(); }
153 static char* getenv(const char *envVarName
);
154 static int setenv(const char *envVarName
, const char *value
);
156 static int kill(pid_t pid
, int sig
);
157 static bool atobool(char *value
);
158 static pid_t
createProcess(const char* cmdName
, const char *arg0
, ...);
160 static size_t send(int fd
, const void *buf
, size_t len
, int flags
);
161 static size_t recv(int fd
, void *buf
, size_t len
, int flags
);
162 static int gethostname(char *hostname
, size_t len
);
163 static int strmatch(char *pattern
, char *str
);