changed sharedmem permissions and file creation umask to 660
[csql.git] / src / storage / os.cxx
blob0de96e0824063ffaae1383954303bc26719f458f
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 #include <os.h>
17 #include <Debug.h>
19 void* os::mmap(void* addr, size_t len, int prot, int flags, int fildes, off_t off)
21 #ifdef SOLARIS
22 return ((void*)::mmap((char*)addr,len,prot,flags,fildes,off));
23 #else
24 return ((void*)::mmap(addr,len,prot,flags,fildes,off));
25 #endif
28 int os::munmap(caddr_t addr, size_t len)
30 return ::munmap(addr, len);
34 shared_memory_id os::shm_create(shared_memory_key key, size_t size, int flag)
36 return ::shmget(key, size, IPC_CREAT | IPC_EXCL | flag);
37 //return ::shmget(key, size, IPC_CREAT | flag);
40 shared_memory_id os::shm_open(shared_memory_key key, size_t size, int flag)
42 return ::shmget(key, size, flag);
44 int os::shmctl(int shmid, int cmd)
46 return ::shmctl(shmid, cmd, NULL);
49 void* os::shm_attach(shared_memory_id id, const void *ptr, int flag)
51 return ::shmat(id, ptr, flag);
54 int os::shm_detach (void* addr)
56 return ::shmdt((char*)addr);
59 int os::gettimeofday(struct timeval *tp)
61 int retval;
62 retval=::gettimeofday(tp, NULL);
63 return retval;
66 struct tm* os::localtime(long *secs)
68 #ifdef SOLARIS
69 return (struct tm*) ::localtime(secs);
70 #else
71 return ::localtime(secs);
72 #endif
75 int os::openFile(const char *name, FileOpenMode flags, size_t size)
77 int retval = -1;
78 //mode_t mode = S_IRWXU | S_IRGRP | S_IWGRP ;
79 mode_t oldMode = umask(0000);
80 mode_t mode = (mode_t)0777 ;
81 retval=::open(name, flags, mode);
82 umask(oldMode);
83 if (0 == size)
84 return retval;
85 os::lseek(retval, size-1, SEEK_SET);
86 char *buf = (char*)" ";
87 os::write(retval, buf, 1);
88 return retval;
90 int os::openFileForAppend(const char *name, int flag)
92 mode_t mode = (mode_t)0644 ;
93 int flags = flag | O_WRONLY|O_APPEND;
94 return ::open(name, flags, mode);
97 int os::closeFile(int fd)
99 return ::close(fd);
101 int os::lockFile(int fd)
103 #ifdef SOLARIS
104 //TODO
105 return 0;
106 #else
107 return flock(fd, LOCK_EX);
108 #endif
110 int os::unlockFile(int fd)
112 #ifdef SOLARIS
113 //TODO
114 return 0;
115 #else
116 return ::flock(fd, LOCK_UN);
117 #endif
120 off_t os::lseek(int fildes, off_t offset, int whence)
122 return ::lseek(fildes, offset, whence);
125 size_t os::write(int fildes, char *buf, size_t size)
127 return ::write(fildes, buf, size);
130 int os::msync(caddr_t addr, size_t len, int flags)
132 return ::msync(addr, len, flags);
134 int os::fsync(int fildes)
136 return ::fsync(fildes);
139 char* os::encrypt(const char *key, const char *salt)
141 return ::crypt(key, salt);
144 void* os::memset(void *src, int c, size_t size)
146 return::memset(src, c, size);
149 void* os::memcpy(void *src, const void *dest, size_t size)
151 return ::memcpy(src, dest, size);
154 int os::memcmp(const void *s1, const void *s2, size_t size)
156 return ::memcmp(s1, s2, size);
158 sighandler_t os::signal(int signum, sighandler_t handler)
160 return ::signal(signum, handler);
163 int os::select(int nfds, fd_set *readfds, fd_set *writefds,
164 fd_set *exceptfds, struct timeval * timeout)
166 return ::select(nfds, readfds, writefds, exceptfds, timeout);
168 int os::sleep(int secs)
170 return ::sleep(secs);
172 int os::usleep(int msecs)
174 struct timeval timeout;
175 timeout.tv_sec = 0;
176 timeout.tv_usec = msecs;
177 os::select(0,0,0,0, &timeout);
178 return 0;
180 int os::getFileSize(const char *fileName)
182 struct stat buf;
183 stat(fileName, &buf);
184 return buf.st_size;
187 char* os::getenv(const char *envVarName)
189 char *retVal;
190 retVal = ::getenv(envVarName);
191 return retVal;
194 int os::setenv(const char *envVarName, const char *value)
196 #ifdef SOLARIS
197 char str[IDENTIFIER_LENGTH*3];
198 sprintf(str, "%s=%s", envVarName, value);
199 return putenv(str);
200 #else
201 return ::setenv(envVarName, value,1);
202 #endif
205 int os::kill(pid_t pid, int sig)
207 return ::kill(pid, sig);
209 bool os::atobool(char *value)
211 if (strlen(value) ==3 && strncasecmp(value,"YES",3)==0) return true;
212 else if (strlen(value) ==2 && strncasecmp(value,"NO", 2)==0) return false;
213 else if (strlen(value) ==4 && strncasecmp(value,"true",4)==0) return true;
214 else if (strlen(value) ==5 && strncasecmp(value,"false",5)==0) return false;
215 return false;
217 pid_t os::createProcess(const char* cmdName, const char* execName)
219 pid_t pid;
220 pid = ::fork();
221 if (pid == (pid_t) -1 )
223 printf("Process creation failed\n");
224 return -1;
226 if (pid >0)
228 //return for parent
229 return pid;
231 pid=::execl(cmdName,execName, NULL);
232 if (pid < 0)
233 printf("Exec failed\n");
234 return pid;
236 pid_t os::fork()
238 return ::fork();
240 size_t os::send(int fd, const void *buf, size_t len, int flags)
242 size_t totalLen = len;
243 size_t nbytes = ::send(fd, buf, len, flags);
244 while (nbytes != -1 && nbytes != len) {
245 len = len - nbytes;
246 nbytes = ::send(fd, ((char *)buf)+nbytes, len, flags);
248 if (nbytes == -1) return -1;
249 else return totalLen;
251 size_t os::recv(int fd, void *buf, size_t len, int flags)
253 size_t totalLen = len;
254 size_t nbytes = ::recv(fd, buf, len, flags);
255 if (!nbytes) return 0;
256 while (nbytes != -1 && nbytes != len) {
257 len = len - nbytes;
258 nbytes = ::recv(fd, ((char *)buf)+nbytes, len, flags);
260 if (nbytes == -1) return -1;
261 else return totalLen;
263 int os::gethostname(char *hostname, size_t len)
265 return ::gethostname(hostname, len);
267 int os::strmatch(char *pattern, char *str)
269 return ::fnmatch(pattern, str, 0);
272 int os::msgget(key_t key, int oflag)
274 return ::msgget(key, oflag | IPC_CREAT);
277 int os::msgsnd(int msqid, const void *ptr, size_t len, int flag)
279 return ::msgsnd(msqid, ptr, len, flag);
282 ssize_t os::msgrcv(int msqid, void *ptr, size_t len, long type, int flag)
284 return ::msgrcv(msqid, ptr, len, type, flag);
287 int os::msgctl(int msqid, int cmd, struct msqid_ds *buff)
289 return ::msgctl(msqid, cmd, buff);
291 bool os::fileExists(char *fileName)
293 int ret = access(fileName, R_OK);
294 if (0 == ret) return true; else return false;
296 int os::isValidIP(char ipstr[] )
298 #if defined(LINUX)
299 struct ifaddrs * ifAddrStruct=NULL;
300 void * tmpAddrPtr=NULL;
301 char addressBuffer[100];
302 int i=0;
303 getifaddrs(&ifAddrStruct);
305 while (ifAddrStruct!=NULL)
307 if (ifAddrStruct->ifa_addr->sa_family==AF_INET && strcmp(ifAddrStruct->ifa_name, "lo0")!=0)
309 tmpAddrPtr=&((struct sockaddr_in *)ifAddrStruct->ifa_addr)->sin_addr;
310 inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, 100);
311 if(strcmp(ipstr,addressBuffer) == 0)
313 // printf("\nThe ip is matched %s and %s",ipstr,addressBuffer);
314 return 1;
316 // printf("IP Address %s\n", inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, 100));
318 ifAddrStruct=ifAddrStruct->ifa_next;
320 #endif
321 return 0;
323 char* os::strcasestr(char *str1, const char *str2)
325 #if defined(LINUX)
326 return ::strcasestr(str1, str2);
327 #else
328 return ::strstr(str1, str2);
330 int size=strlen(str1);
331 char *tmpStr=new char[size];
332 char *tmpStrPtr=NULL;
333 strcpy(tmpStr, str1);
334 tmpStrPtr=tmpStr;
335 while(*tmpStrPtr != '\0')
337 *tmpStrPtr=::toupper(*tmpStrPtr);
338 tmpStrPtr++;
340 char *subStrPtr = ::strstr(tmpStr, str2);
341 delete []tmpStr;
342 return subStrPtr;
344 #endif
346 int os::getNoOfProcessors()
348 return ::sysconf(_SC_NPROCESSORS_ONLN);
350 mode_t os::umask(mode_t mask)
352 return::umask(mask);