src/server/os.cxx

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007 by www.databasecache.com                           *
00003  *   Contact: praba_tuty@databasecache.com                                 *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015   ***************************************************************************/
00016 #include <math.h>
00017 #include <os.h>
00018 #include <Debug.h>
00019 
00020 caddr_t os::mmap(caddr_t addr, size_t len, int prot, int flags, int fildes, off_t off)
00021 {
00022     return ((caddr_t)::mmap(addr,len,prot,flags,fildes,off));
00023 }
00024 
00025 int os::munmap(caddr_t addr, size_t len)
00026 {
00027     return ::munmap(addr, len);
00028 }
00029 
00030 
00031 shared_memory_id os::shm_create(shared_memory_key key, size_t size, int flag)
00032 {
00033     return ::shmget(key, size, IPC_CREAT | IPC_EXCL | flag);
00034     //return ::shmget(key, size, IPC_CREAT | flag);
00035 }
00036 
00037 shared_memory_id os::shm_open(shared_memory_key key, size_t size, int flag)
00038 {
00039     return ::shmget(key, size, flag);
00040 }
00041 int os::shmctl(int shmid, int cmd)
00042 {
00043     return ::shmctl(shmid, cmd, NULL);
00044 }
00045 
00046 void*  os::shm_attach(shared_memory_id id, const void *ptr, int flag)
00047 {
00048     return ::shmat(id, ptr, flag);
00049 }
00050 
00051 int os::shm_detach (void* addr)
00052 {
00053     return ::shmdt((char*)addr);
00054 }
00055 
00056 double os::floor(double val)
00057 {
00058     return ::floor(val);
00059 }
00060 
00061 int os::gettimeofday(struct timeval *tp)
00062 {
00063     int retval;
00064     retval=::gettimeofday(tp, NULL);
00065     return retval;
00066 }
00067 
00068 struct tm* os::localtime(long *secs)
00069 {
00070     return ::localtime(secs);
00071 }
00072 
00073 pid_t os::getpid()
00074 {
00075     return ::getpid();
00076 }
00077 pthread_t os::getthrid()
00078 {
00079     return ::pthread_self();
00080 }
00081 
00082 
00083 
00084 
00085 int os::openFile(const char *name, FileOpenMode flags, size_t size)
00086 {
00087     int retval = -1;
00088     //mode_t mode = S_IRWXU | S_IRGRP | S_IWGRP ;
00089     mode_t mode = (mode_t)0755 ;
00090     retval=::open(name, flags, mode);
00091     if (0 == size)
00092         return retval;
00093     os::lseek(retval, size-1, SEEK_SET);
00094     char *buf = (char*)" ";
00095     os::write(retval, buf, 1);
00096     return retval;
00097 }
00098 
00099 int os::closeFile(int fd)
00100 {
00101     return ::close(fd);
00102 }
00103 
00104 off_t os::lseek(int fildes, off_t offset, int whence)
00105 {
00106     return ::lseek(fildes, offset, whence);
00107 }
00108 
00109 size_t os::write(int fildes, char *buf, size_t size)
00110 {
00111     return ::write(fildes, buf, size);
00112 }
00113 
00114 int os::msync(caddr_t addr, size_t len, int flags)
00115 {
00116     return ::msync(addr, len, flags);
00117 }
00118 int os::fsync(int fildes)
00119 {
00120     return ::fsync(fildes);
00121 }
00122 
00123 char* os::encrypt(const char *key, const char *salt)
00124 {
00125     return ::crypt(key, salt);
00126 }
00127 
00128 void* os::memset(void *src, int c, size_t size)
00129 {
00130     return::memset(src, c, size);
00131 }
00132 
00133 void* os::memcpy(void *src, const void *dest, size_t size)
00134 {
00135     return ::memcpy(src, dest, size);
00136 }
00137 
00138 int os::memcmp(const void *s1, const void *s2, size_t size)
00139 {
00140     return ::memcmp(s1, s2, size);
00141 }
00142 sighandler_t os::signal(int signum, sighandler_t handler)
00143 {
00144     return ::signal(signum, handler);
00145 }
00146 
00147 size_t os::alignLong(size_t size)
00148 {
00149     return ((size - 1) | (sizeof(long) - 1)) + 1;
00150 }
00151 
00152 size_t os::align(size_t size)
00153 {
00154     //Calls alignLong
00155     return ((size - 1) | (sizeof(long) - 1)) + 1;
00156 }
00157 
00158 int os::select(int nfds, fd_set *readfds, fd_set *writefds,
00159                  fd_set *exceptfds, struct timeval * timeout)
00160 {
00161     return ::select(nfds, readfds, writefds, exceptfds, timeout);
00162 }
00163 int os::sleep(int secs)
00164 { 
00165     return ::sleep(secs);
00166 }
00167 int os::usleep(int msecs)
00168 { 
00169     struct timeval timeout;
00170     timeout.tv_sec = 0;
00171     timeout.tv_usec = msecs;
00172     os::select(0,0,0,0, &timeout);
00173     return 0;
00174 }
00175 
00176 
00177 
00178 char* os::getenv(const char *envVarName)
00179 {
00180     char *retVal;
00181     retVal = ::getenv(envVarName);
00182     return retVal;
00183 }
00184 
00185 int os::setenv(const char *envVarName, const char *value)
00186 {
00187     return ::setenv(envVarName, value,1);
00188 }
00189 
00190 int os::kill(pid_t pid, int sig)
00191 {
00192     return ::kill(pid, sig);
00193 }
00194 bool os::atobool(char *value)
00195 {
00196     if (strlen(value) ==3 && strncasecmp(value,"YES",3)==0) return true;
00197     else if (strlen(value) ==2 && strncasecmp(value,"NO", 2)==0) return false;
00198     else if (strlen(value) ==4 && strncasecmp(value,"true",4)==0) return true;
00199     else if (strlen(value) ==5 && strncasecmp(value,"false",5)==0) return false;
00200     return false;
00201 }
00202 pid_t os::createProcess(const char* cmdName, const char *arg0, ...)
00203 {
00204     pid_t pid;
00205     pid = ::vfork();
00206     if (pid == (pid_t) -1 )
00207     {
00208         printf("Process creation failed\n");
00209         return -1;
00210     }
00211     if (pid >0)
00212     {
00213         //return for parent
00214         return pid;
00215     }
00216     va_list ap;
00217     va_start(ap,arg0);
00218 
00219     const char *argv[5];
00220 
00221     argv[0]=cmdName;
00222     argv[1]=arg0;
00223 
00224     argv[2]=NULL;
00225     int i = 2;
00226     while(argv[i++]=va_arg(ap,char *));
00227     switch(i){
00228         case 2:
00229             pid=::execl(argv[0],argv[1]);break;
00230         case 3:
00231             pid=::execl(argv[0],argv[1],argv[2]);break;
00232         case 4:
00233             pid=::execl(argv[0],argv[1],argv[2],argv[3]);break;
00234         case 5:
00235             pid=::execl(argv[0],argv[1],argv[2],argv[3],argv[4]);break;
00236         default:
00237             printf("only three options allowed\n");
00238             pid=-1;break;
00239     }
00240     if (pid < 0)
00241         printf("Exec failed\n");
00242     return pid;
00243 
00244 }
00245 pid_t os::fork()
00246 {
00247     return ::fork();
00248 }
00249 size_t os::send(int fd, const void *buf, size_t len, int flags)
00250 {
00251     return ::send(fd, buf, len, flags);
00252 }
00253 size_t os::recv(int fd, void *buf, size_t len, int flags)
00254 {
00255     return ::recv(fd, buf, len, flags);
00256 }

Generated on Mon Jun 9 22:37:15 2008 for csql by  doxygen 1.4.7