code reorg
[csql.git] / src / server / os.cxx
blob8ad7333ce793d0bb56c145fb97d65c025b34d3e1
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 <math.h>
17 #include <os.h>
18 #include <Debug.h>
20 caddr_t os::mmap(caddr_t addr, size_t len, int prot, int flags, int fildes, off_t off)
22 return ((caddr_t)::mmap(addr,len,prot,flags,fildes,off));
25 int os::munmap(caddr_t addr, size_t len)
27 return ::munmap(addr, len);
31 shared_memory_id os::shm_create(shared_memory_key key, size_t size, int flag)
33 return ::shmget(key, size, IPC_CREAT | IPC_EXCL | flag);
34 //return ::shmget(key, size, IPC_CREAT | flag);
37 shared_memory_id os::shm_open(shared_memory_key key, size_t size, int flag)
39 return ::shmget(key, size, flag);
41 int os::shmctl(int shmid, int cmd)
43 return ::shmctl(shmid, cmd, NULL);
46 void* os::shm_attach(shared_memory_id id, const void *ptr, int flag)
48 return ::shmat(id, ptr, flag);
51 int os::shm_detach (void* addr)
53 return ::shmdt((char*)addr);
56 double os::floor(double val)
58 return ::floor(val);
61 int os::gettimeofday(struct timeval *tp)
63 int retval;
64 retval=::gettimeofday(tp, NULL);
65 return retval;
68 struct tm* os::localtime(long *secs)
70 return ::localtime(secs);
73 pid_t os::getpid()
75 return ::getpid();
77 pthread_t os::getthrid()
79 return ::pthread_self();
85 int os::openFile(const char *name, FileOpenMode flags, size_t size)
87 int retval = -1;
88 //mode_t mode = S_IRWXU | S_IRGRP | S_IWGRP ;
89 mode_t mode = (mode_t)0755 ;
90 retval=::open(name, flags, mode);
91 if (0 == size)
92 return retval;
93 os::lseek(retval, size-1, SEEK_SET);
94 char *buf = (char*)" ";
95 os::write(retval, buf, 1);
96 return retval;
99 int os::closeFile(int fd)
101 return ::close(fd);
104 off_t os::lseek(int fildes, off_t offset, int whence)
106 return ::lseek(fildes, offset, whence);
109 size_t os::write(int fildes, char *buf, size_t size)
111 return ::write(fildes, buf, size);
114 int os::msync(caddr_t addr, size_t len, int flags)
116 return ::msync(addr, len, flags);
118 int os::fsync(int fildes)
120 return ::fsync(fildes);
123 char* os::encrypt(const char *key, const char *salt)
125 return ::crypt(key, salt);
128 void* os::memset(void *src, int c, size_t size)
130 return::memset(src, c, size);
133 void* os::memcpy(void *src, const void *dest, size_t size)
135 return ::memcpy(src, dest, size);
138 int os::memcmp(const void *s1, const void *s2, size_t size)
140 return ::memcmp(s1, s2, size);
142 sighandler_t os::signal(int signum, sighandler_t handler)
144 return ::signal(signum, handler);
147 size_t os::alignLong(size_t size)
149 return ((size - 1) | (sizeof(long) - 1)) + 1;
152 size_t os::align(size_t size)
154 //Calls alignLong
155 return ((size - 1) | (sizeof(long) - 1)) + 1;
158 int os::select(int nfds, fd_set *readfds, fd_set *writefds,
159 fd_set *exceptfds, struct timeval * timeout)
161 return ::select(nfds, readfds, writefds, exceptfds, timeout);
163 int os::sleep(int secs)
165 return ::sleep(secs);
167 int os::usleep(int msecs)
169 struct timeval timeout;
170 timeout.tv_sec = 0;
171 timeout.tv_usec = msecs;
172 os::select(0,0,0,0, &timeout);
173 return 0;
178 char* os::getenv(const char *envVarName)
180 char *retVal;
181 retVal = ::getenv(envVarName);
182 return retVal;
185 int os::setenv(const char *envVarName, const char *value)
187 return ::setenv(envVarName, value,1);
190 int os::kill(pid_t pid, int sig)
192 return ::kill(pid, sig);
194 bool os::atobool(char *value)
196 if (strlen(value) ==3 && strncasecmp(value,"YES",3)==0) return true;
197 else if (strlen(value) ==2 && strncasecmp(value,"NO", 2)==0) return false;
198 else if (strlen(value) ==4 && strncasecmp(value,"true",4)==0) return true;
199 else if (strlen(value) ==5 && strncasecmp(value,"false",5)==0) return false;
200 return false;
202 pid_t os::createProcess(const char* cmdName, const char *arg0, ...)
204 pid_t pid;
205 pid = ::vfork();
206 if (pid == (pid_t) -1 )
208 printf("Process creation failed\n");
209 return -1;
211 if (pid >0)
213 //return for parent
214 return pid;
216 va_list ap;
217 va_start(ap,arg0);
219 const char *argv[5];
221 argv[0]=cmdName;
222 argv[1]=arg0;
224 argv[2]=NULL;
225 int i = 2;
226 while(argv[i++]=va_arg(ap,char *));
227 switch(i){
228 case 2:
229 pid=::execl(argv[0],argv[1]);break;
230 case 3:
231 pid=::execl(argv[0],argv[1],argv[2]);break;
232 case 4:
233 pid=::execl(argv[0],argv[1],argv[2],argv[3]);break;
234 case 5:
235 pid=::execl(argv[0],argv[1],argv[2],argv[3],argv[4]);break;
236 default:
237 printf("only three options allowed\n");
238 pid=-1;break;
240 if (pid < 0)
241 printf("Exec failed\n");
242 return pid;
245 pid_t os::fork()
247 return ::fork();
249 size_t os::send(int fd, const void *buf, size_t len, int flags)
251 return ::send(fd, buf, len, flags);
253 size_t os::recv(int fd, void *buf, size_t len, int flags)
255 return ::recv(fd, buf, len, flags);