|
Server 0.0.1
HTTP/REST server implementation
|
00001 00023 #define _POSIX_SOURCE 00024 #define _POSIX_C_SOURCE 200112L 00025 #define _GNU_SOURCE 00026 00027 #include <sys/types.h> 00028 #include <sys/stat.h> 00029 #include <sys/mman.h> 00030 #include <stdarg.h> 00031 #include <stdlib.h> 00032 #include <stdio.h> 00033 #include <unistd.h> 00034 #include <fcntl.h> 00035 00036 #include "class.h" 00037 #include "utils/memory.h" 00038 00039 #include "cbuf.h" 00040 00041 00042 static void cbufDtor(void*); 00043 00044 static 00045 int 00046 cbufCtor(void * _this, va_list * params) 00047 { 00048 Cbuf this = _this; 00049 char state = -1; 00050 char * shm_name = va_arg(*params, char*); 00051 long psize = sysconf(_SC_PAGESIZE); 00052 size_t size; 00053 int shm; 00054 char * data; 00055 00056 this->shm_name = malloc(strlen(shm_name) + 7 + 2); 00057 sprintf(this->shm_name, "/%06d_%s", getpid(), shm_name); 00058 00063 size = va_arg(*params, size_t); 00064 size = (0 >= size)? 1 : (0 != size%psize)? (size/psize)+1 : size/psize; 00065 this->bsize = psize * size; 00066 00067 while (-1 == state) { 00068 shm = shm_open(this->shm_name, O_RDWR|O_CREAT|O_EXCL, S_IRWXU); 00069 if (-1 == shm) { 00070 break; 00071 } 00072 00073 if (-1 == ftruncate(shm, this->bsize)) { 00074 break; 00075 } 00076 00077 this->data = mmap (0, this->bsize << 1, 00078 PROT_NONE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); 00079 if (this->data == MAP_FAILED) { 00080 this->data = NULL; 00081 break; 00082 } 00083 00084 data = mmap (this->data, this->bsize, 00085 PROT_READ|PROT_WRITE, MAP_FIXED|MAP_SHARED, shm, 0); 00086 if (data != this->data) { 00087 break; 00088 } 00089 00090 data = mmap (this->data + this->bsize, this->bsize, 00091 PROT_READ|PROT_WRITE, MAP_FIXED|MAP_SHARED, shm, 0); 00092 if (data != this->data + this->bsize) { 00093 break; 00094 } 00095 00096 state = 0; 00097 } 00098 00099 if (-1 != shm) { 00100 shm_unlink(this->shm_name); 00101 close(shm); 00102 } 00103 00104 return state; 00105 } 00106 00107 static 00108 void 00109 cbufDtor(void * _this) 00110 { 00111 Cbuf this = _this; 00112 00113 FREE(this->shm_name); 00114 00115 if (NULL != this->data && MAP_FAILED != this->data) { 00116 munmap(this->data, this->bsize << 1); 00117 } 00118 00119 this->data = NULL; 00120 } 00121 00122 INIT_IFACE(Class, cbufCtor, cbufDtor, NULL); 00123 CREATE_CLASS(Cbuf, NULL, IFACE(Class)); 00124 00125 // vim: set ts=4 sw=4: