Changeset 507 for trunk/zoo-project/zoo-kernel
- Timestamp:
- Oct 8, 2014, 11:38:12 AM (10 years ago)
- Location:
- trunk/zoo-project/zoo-kernel
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/zoo-project/zoo-kernel/service.h
r490 r507 40 40 #define zOpen _open 41 41 #define zWrite _write 42 #define zSleep Sleep 42 43 #else 43 44 #define zStrdup strdup … … 45 46 #define zOpen open 46 47 #define zWrite write 48 #define zSleep sleep 47 49 #endif 48 50 … … 68 70 #define false -1 69 71 #endif 70 #else71 //#include <stdbool.h>72 72 #endif 73 73 -
trunk/zoo-project/zoo-kernel/service_internal.c
r505 r507 83 83 } 84 84 85 char* _getStatus(maps* conf,int pid){ 86 char lid[1024]; 87 sprintf(lid,"%d",pid); 88 setMapInMaps(conf,"lenv","lid",lid); 89 semid lockid=getShmLockId(conf,1); 90 if( 91 #ifdef WIN32 92 lockid==NULL 93 #else 94 lockid<0 95 #endif 96 ){ 97 char tmp[3]; 98 sprintf(tmp,"%d",ZOO_LOCK_CREATE_FAILED); 99 return tmp; 100 } 101 if(lockShm(lockid)<0){ 102 fprintf(stderr,"%s %d\n",__FILE__,__LINE__); 103 fflush(stderr); 104 char tmp[3]; 105 sprintf(tmp,"%d",ZOO_LOCK_ACQUIRE_FAILED); 106 return tmp; 107 } 108 char *tmp=getStatus(pid); 109 unlockShm(lockid); 110 if(tmp==NULL || strncmp(tmp,"-1",2)==0){ 111 removeShmLock(conf,1); 112 } 113 return tmp; 114 } 115 85 116 #ifdef WIN32 86 117 … … 92 123 93 124 #define SHMEMSIZE 4096 125 126 char* getKeyValue(maps* conf){ 127 map *tmpMap=getMapFromMaps(conf,"lenv","lid"); 128 if(tmpMap==NULL) 129 tmpMap=getMapFromMaps(conf,"lenv","sid"); 130 char* key="-1"; 131 if(tmpMap!=NULL){ 132 key=(char*)malloc((strlen(tmpMap->value)+9)*sizeof(char)); 133 sprintf(key,"zoo_sem_%s",tmpMap->value); 134 } 135 return key; 136 } 137 138 139 semid getShmLockId(maps* conf, int nsems){ 140 semid sem_id; 141 char* key=getKeyValue(conf); 142 143 sem_id = CreateSemaphore( NULL, nsems, nsems+1, key); 144 if(sem_id==NULL){ 145 if(strncmp(key,"-1",2)!=0) 146 free(key); 147 #ifdef DEBUG 148 fprintf(stderr,"Semaphore failed to create ! %s\n",GetLastError()); 149 #endif 150 return NULL; 151 } 152 #ifdef DEBUG 153 fprintf(stderr,"%s Accessed !\n",key); 154 #endif 155 if(strncmp(key,"-1",2)!=0) 156 free(key); 157 return sem_id; 158 } 159 160 int removeShmLock(maps* conf, int nsems){ 161 semid sem_id=getShmLockId(conf,1); 162 if (CloseHandle(sem_id) == 0) { 163 fprintf(stderr,"Unable to remove semaphore %s",GetLastError()); 164 return -1; 165 } 166 #ifdef DEBUG 167 fprintf(stderr,"%d Removed !\n",sem_id); 168 #endif 169 return 0; 170 } 171 172 int lockShm(semid id){ 173 DWORD dwWaitResult=WaitForSingleObject(id,INFINITE); 174 switch (dwWaitResult){ 175 case WAIT_OBJECT_0: 176 return 0; 177 break; 178 case WAIT_TIMEOUT: 179 return -1; 180 break; 181 default: 182 return -2; 183 break; 184 } 185 return 0; 186 } 187 188 int unlockShm(semid id){ 189 if(!ReleaseSemaphore(id,1,NULL)){ 190 return -1; 191 } 192 return 0; 193 } 94 194 95 195 static LPVOID lpvMemG = NULL; // pointer to shared memory … … 103 203 map *tmpMap1; 104 204 map *tmpMap=getMapFromMaps(conf,"lenv","sid"); 205 semid lockid=getShmLockId(conf,1); 206 if(lockid==NULL){ 207 #ifdef DEBUG 208 fprintf(stderr,"Unable to create semaphore on line %d!! \n",__LINE__); 209 #endif 210 return ZOO_LOCK_CREATE_FAILED; 211 } 212 if(lockShm(lockid)<0){ 213 #ifdef DEBUG 214 fprintf(stderr,"Unable to create semaphore on line %d!! \n",__LINE__); 215 #endif 216 return ZOO_LOCK_ACQUIRE_FAILED; 217 } 218 105 219 if(hMapObjectG==NULL) 106 220 hMapObjectG = CreateFileMapping( … … 112 226 TEXT(tmpMap->value)); // name of map object 113 227 if (hMapObjectG == NULL){ 114 fprintf(stderr,"Unable to create share memory segment %s !! \n",tmpMap->value); 228 #ifdef DEBUG 229 fprintf(stderr,"Unable to create shared memory segment %d !! \n",GetLastError()); 230 #endif 115 231 return -2; 116 232 } … … 124 240 0); // default: map entire file 125 241 if (lpvMemG == NULL){ 242 #ifdef DEBUG 126 243 fprintf(stderr,"Unable to create or access the shared memory segment %s !! \n",tmpMap->value); 244 #endif 127 245 return -1; 128 246 } … … 139 257 *lpszTmp++ = '\0'; 140 258 free(final_string); 259 unlockShm(lockid); 141 260 return 0; 142 261 } … … 149 268 HANDLE hMapObject = NULL; 150 269 BOOL fIgnore,fInit; 151 char tmp[10 0];152 sprintf(tmp,"% i",pid);270 char tmp[1024]; 271 sprintf(tmp,"%d",pid); 153 272 if(hMapObject==NULL) 154 273 hMapObject = CreateFileMapping( … … 159 278 4096, // size: low 32-bits 160 279 TEXT(tmp)); // name of map object 161 if (hMapObject == NULL) 162 return FALSE; 280 if (hMapObject == NULL){ 281 #ifdef DEBUG 282 fprintf(stderr,"ERROR on line %d\n",__LINE__); 283 #endif 284 return "-1"; 285 } 163 286 if((GetLastError() != ERROR_ALREADY_EXISTS)){ 287 #ifdef DEBUG 288 fprintf(stderr,"ERROR on line %d\n",__LINE__); 289 fprintf(stderr,"READING STRING S %s\n",GetLastError()); 290 #endif 164 291 fIgnore = UnmapViewOfFile(lpvMem); 165 292 fIgnore = CloseHandle(hMapObject); … … 174 301 0, // low offset: beginning 175 302 0); // default: map entire file 176 if (lpvMem == NULL) 303 if (lpvMem == NULL){ 304 #ifdef DEBUG 305 fprintf(stderr,"READING STRING S %d\n",__LINE__); 306 fprintf(stderr,"READING STRING S %s\n",GetLastError()); 307 #endif 177 308 return "-1"; 309 } 178 310 lpszTmp = (LPWSTR) lpvMem; 179 311 while (*lpszTmp){ … … 183 315 i++; 184 316 } 185 #ifdef DEBUG186 fprintf(stderr,"READING STRING S %s\n",lpszBuf);187 #endif188 317 return (char*)lpszBuf; 189 318 } … … 194 323 fIgnore = CloseHandle(hMapObjectG); 195 324 } 325 196 326 #else 327 328 #define MAX_RETRIES 10 329 330 int getKeyValue(maps* conf){ 331 map *tmpMap=getMapFromMaps(conf,"lenv","lid"); 332 if(tmpMap==NULL) 333 tmpMap=getMapFromMaps(conf,"lenv","sid"); 334 int key=-1; 335 if(tmpMap!=NULL) 336 key=atoi(tmpMap->value); 337 return key; 338 } 339 340 int getShmLockId(maps* conf, int nsems){ 341 int i; 342 union semun arg; 343 struct semid_ds buf; 344 struct sembuf sb; 345 semid sem_id; 346 int key=getKeyValue(conf); 347 348 sem_id = semget(key, nsems, IPC_CREAT | IPC_EXCL | 0666); 349 350 if (sem_id >= 0) { /* we got it first */ 351 sb.sem_op = 1; 352 sb.sem_flg = 0; 353 arg.val=1; 354 for(sb.sem_num = 0; sb.sem_num < nsems; sb.sem_num++) { 355 /* do a semop() to "free" the semaphores. */ 356 /* this sets the sem_otime field, as needed below. */ 357 if (semop(sem_id, &sb, 1) == -1) { 358 int e = errno; 359 semctl(sem_id, 0, IPC_RMID); /* clean up */ 360 errno = e; 361 return -1; /* error, check errno */ 362 } 363 } 364 } else if (errno == EEXIST) { /* someone else got it first */ 365 int ready = 0; 366 367 sem_id = semget(key, nsems, 0); /* get the id */ 368 if (sem_id < 0) return sem_id; /* error, check errno */ 369 370 /* wait for other process to initialize the semaphore: */ 371 arg.buf = &buf; 372 for(i = 0; i < MAX_RETRIES && !ready; i++) { 373 semctl(sem_id, nsems-1, IPC_STAT, arg); 374 if (arg.buf->sem_otime != 0) { 375 #ifdef DEBUG 376 fprintf(stderr,"Semaphore acquired ...\n"); 377 #endif 378 ready = 1; 379 } else { 380 #ifdef DEBUG 381 fprintf(stderr,"Retry to access the semaphore later ...\n"); 382 #endif 383 sleep(1); 384 } 385 } 386 errno = NULL; 387 if (!ready) { 388 #ifdef DEBUG 389 fprintf(stderr,"Unable to access the semaphore ...\n"); 390 #endif 391 errno = ETIME; 392 return -1; 393 } 394 } else { 395 return sem_id; /* error, check errno */ 396 } 397 #ifdef DEBUG 398 fprintf(stderr,"%d Created !\n",sem_id); 399 #endif 400 return sem_id; 401 } 402 403 int removeShmLock(maps* conf, int nsems){ 404 union semun arg; 405 int sem_id=getShmLockId(conf,nsems); 406 if (semctl(sem_id, 0, IPC_RMID, arg) == -1) { 407 perror("semctl"); 408 return -1; 409 } 410 return 0; 411 } 412 413 int lockShm(int id){ 414 struct sembuf sb; 415 int i; 416 sb.sem_num = 0; 417 sb.sem_op = -1; /* set to allocate resource */ 418 sb.sem_flg = SEM_UNDO; 419 if (semop(id, &sb, 1) == -1){ 420 perror("semop"); 421 return -1; 422 } 423 return 0; 424 } 425 426 int unlockShm(int id){ 427 struct sembuf sb; 428 sb.sem_num = 0; 429 sb.sem_op = 1; /* free resource */ 430 sb.sem_flg = SEM_UNDO; 431 if (semop(id, &sb, 1) == -1) { 432 perror("semop"); 433 return -1; 434 } 435 return 0; 436 } 197 437 198 438 void unhandleStatus(maps *conf){ … … 223 463 int _updateStatus(maps *conf){ 224 464 int shmid; 225 key_t key;226 465 char *shm,*s,*s1; 227 466 map *tmpMap=NULL; 228 tmpMap=getMapFromMaps(conf,"lenv","sid"); 229 if(tmpMap!=NULL){ 230 key=atoi(tmpMap->value); 467 key_t key=getKeyValue(conf); 468 if(key!=-1){ 469 semid lockid=getShmLockId(conf,1); 470 if(lockid<0) 471 return ZOO_LOCK_CREATE_FAILED; 472 if(lockShm(lockid)<0){ 473 return ZOO_LOCK_ACQUIRE_FAILED; 474 } 231 475 if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) { 232 476 #ifdef DEBUG 233 477 fprintf(stderr,"shmget failed to create new Shared memory segment\n"); 234 478 #endif 479 unlockShm(lockid); 235 480 return -2; 236 481 }else{ … … 239 484 fprintf(stderr,"shmat failed to update value\n"); 240 485 #endif 486 unlockShm(lockid); 241 487 return -1; 242 488 } … … 255 501 *s1=NULL; 256 502 shmdt((void *)shm); 503 unlockShm(lockid); 257 504 } 258 505 } … … 1112 1359 if(nci0==0){ 1113 1360 nc7 = xmlNewNode(ns_ows, BAD_CAST "MinimumValue"); 1114 int nci=1; 1115 for(nci=1;nci<strlen(pToken);nci++){ 1116 tmpStr[nci-1]=pToken[nci]; 1117 } 1118 }else{ 1361 strncpy( tmpStr, pToken+1, strlen(pToken)-1 ); 1362 tmpStr[strlen(pToken)-1] = '\0'; 1363 }else{ 1119 1364 nc7 = xmlNewNode(ns_ows, BAD_CAST "MaximumValue"); 1120 int nci=0; 1121 for(nci=0;nci<strlen(pToken)-1;nci++){ 1122 tmpStr[nci]=pToken[nci]; 1123 } 1365 const char* bkt; 1366 if ( ( bkt = strchr(pToken, '[') ) != NULL || ( bkt = strchr(pToken, ']') ) != NULL ){ 1367 strncpy( tmpStr, pToken, bkt - pToken ); 1368 tmpStr[bkt - pToken] = '\0'; 1369 } 1124 1370 } 1125 1371 xmlAddChild(nc7,xmlNewText(BAD_CAST tmpStr)); … … 1179 1425 xmlAddChild(nc6,nc8); 1180 1426 _tmp0=e->supported; 1181 if(getMap(_tmp0->content,"range")!=NULL || 1182 getMap(_tmp0->content,"rangeMin")!=NULL || 1183 getMap(_tmp0->content,"rangeMax")!=NULL || 1184 getMap(_tmp0->content,"rangeClosure")!=NULL ){ 1427 if(_tmp0!=NULL && 1428 (getMap(_tmp0->content,"range")!=NULL || 1429 getMap(_tmp0->content,"rangeMin")!=NULL || 1430 getMap(_tmp0->content,"rangeMax")!=NULL || 1431 getMap(_tmp0->content,"rangeClosure")!=NULL )){ 1185 1432 tmp1=_tmp0->content; 1186 1433 goto doRange; … … 1650 1897 xmlDocSetRootElement(doc, nr); 1651 1898 1652 if(hasStoredExecuteResponse==true){ 1653 /* We need to write the ExecuteResponse Document somewhere */ 1654 FILE* output=fopen(stored_path,"w"); 1655 if(output==NULL){ 1656 /* If the file cannot be created return an ExceptionReport */ 1657 char tmpMsg[1024]; 1658 sprintf(tmpMsg,_("Unable to create the file : \"%s\" for storing the ExecuteResponse."),stored_path); 1659 map * errormap = createMap("text",tmpMsg); 1660 addToMap(errormap,"code", "InternalError"); 1661 printExceptionReportResponse(m,errormap); 1662 freeMap(&errormap); 1663 free(errormap); 1664 xmlFreeDoc(doc); 1665 xmlCleanupParser(); 1666 zooXmlCleanupNs(); 1899 if(hasStoredExecuteResponse==true && status!=SERVICE_STARTED){ 1900 semid lid=getShmLockId(m,1); 1901 if(lid<0) 1667 1902 return; 1668 } 1669 xmlChar *xmlbuff; 1670 int buffersize; 1671 xmlDocDumpFormatMemoryEnc(doc, &xmlbuff, &buffersize, "UTF-8", 1); 1672 fwrite(xmlbuff,1,xmlStrlen(xmlbuff)*sizeof(char),output); 1673 xmlFree(xmlbuff); 1674 fclose(output); 1903 else{ 1904 #ifdef DEBUG 1905 fprintf(stderr,"LOCK %s %d !\n",__FILE__,__LINE__); 1906 #endif 1907 lockShm(lid); 1908 /* We need to write the ExecuteResponse Document somewhere */ 1909 FILE* output=fopen(stored_path,"w"); 1910 if(output==NULL){ 1911 /* If the file cannot be created return an ExceptionReport */ 1912 char tmpMsg[1024]; 1913 sprintf(tmpMsg,_("Unable to create the file : \"%s\" for storing the ExecuteResponse."),stored_path); 1914 map * errormap = createMap("text",tmpMsg); 1915 addToMap(errormap,"code", "InternalError"); 1916 printExceptionReportResponse(m,errormap); 1917 freeMap(&errormap); 1918 free(errormap); 1919 xmlFreeDoc(doc); 1920 xmlCleanupParser(); 1921 zooXmlCleanupNs(); 1922 unlockShm(lid); 1923 return; 1924 } 1925 xmlChar *xmlbuff; 1926 int buffersize; 1927 xmlDocDumpFormatMemoryEnc(doc, &xmlbuff, &buffersize, "UTF-8", 1); 1928 fwrite(xmlbuff,1,xmlStrlen(xmlbuff)*sizeof(char),output); 1929 xmlFree(xmlbuff); 1930 fclose(output); 1931 #ifdef DEBUG 1932 fprintf(stderr,"UNLOCK %s %d !\n",__FILE__,__LINE__); 1933 #endif 1934 unlockShm(lid); 1935 map* test1=getMap(request,"status"); 1936 if(test1==NULL || strcasecmp(test1->value,"true")!=0){ 1937 removeShmLock(m,1); 1938 } 1939 } 1675 1940 } 1676 1941 printDocument(m,doc,pid); -
trunk/zoo-project/zoo-kernel/service_internal.h
r501 r507 36 36 #define _ss(String) dgettext ("zoo-services",String) 37 37 38 #define ZOO_LOCK_CREATE_FAILED -4 39 #define ZOO_LOCK_ACQUIRE_FAILED -5 40 #define ZOO_LOCK_RELEASE_FAILED -6 41 38 42 #include <sys/stat.h> 39 43 #include <sys/types.h> … … 42 46 #include <sys/ipc.h> 43 47 #include <sys/shm.h> 48 #include <sys/sem.h> 44 49 #else 45 50 #include <direct.h> … … 99 104 void unhandleStatus(maps*); 100 105 int _updateStatus(maps*); 106 char* _getStatus(maps*,int); 101 107 char* getStatus(int); 108 int removeShmLock(maps*, int); 109 #ifndef WIN32 110 #define semid int 111 #else 112 #define semid HANDLE 113 #endif 114 semid getShmLockId(maps*,int); 115 int lockShm(semid); 116 int unlockShm(semid); 102 117 103 118 #ifdef USE_JS -
trunk/zoo-project/zoo-kernel/ulinet.h
r492 r507 27 27 28 28 #include "fcgi_stdio.h" 29 #include "service.h"30 29 #include <stdlib.h> 31 30 #include <fcntl.h> … … 44 43 #include "jsapi.h" 45 44 #endif 45 #ifndef bool 46 #define bool int 47 #endif 48 #ifndef true 49 #define true 1 50 #define false -1 51 #endif 46 52 47 #define MAX_REQ 10053 #define MAX_REQ 50 48 54 49 55 #ifdef _ULINET -
trunk/zoo-project/zoo-kernel/zoo_service_loader.c
r504 r507 2629 2629 * process answer to http client. 2630 2630 */ 2631 #ifndef WIN32 2632 zSleep(1); 2633 #endif 2631 2634 r_inputs=getMapFromMaps(m,"main","tmpPath"); 2632 2635 map* r_inputs1=getMap(s1->content,"ServiceProvider"); … … 2641 2644 #endif 2642 2645 freopen(flog, "w+", stderr); 2646 semid lid=getShmLockId(m,1); 2647 fflush(stderr); 2648 if(lid<0){ 2649 fprintf(stderr,"ERROR %s %d\n",__FILE__,__LINE__); 2650 fflush(stderr); 2651 return -1; 2652 } 2653 else{ 2654 if(lockShm(lid)<0){ 2655 fprintf(stderr,"ERROR %s %d\n",__FILE__,__LINE__); 2656 fflush(stderr); 2657 return -1; 2658 } 2659 fflush(stderr); 2660 } 2643 2661 f0=freopen(fbkp , "w+", stdout); 2662 rewind(stdout); 2644 2663 #ifndef WIN32 2645 2664 fclose(stdin); … … 2654 2673 printProcessResponse(m,request_inputs,cpid,s1,r_inputs1->value,SERVICE_STARTED, 2655 2674 request_input_real_format,request_output_real_format); 2656 #ifndef WIN322657 2675 fflush(stdout); 2658 rewind(stdout); 2659 #else 2660 #endif 2676 unlockShm(lid); 2677 fflush(stderr); 2661 2678 fbkp1=(char*)malloc((strlen(r_inputs->value)+strlen(r_inputs1->value)+1024)*sizeof(char)); 2662 2679 sprintf(fbkp1,"%s/%s_final_%d.xml",r_inputs->value,r_inputs1->value,cpid); … … 2697 2714 fclose(stdout); 2698 2715 fclose(stderr); 2699 unhandleStatus(m);2700 2716 /** 2701 2717 * Dump back the final file fbkp1 to fbkp … … 2704 2720 fclose(f1); 2705 2721 FILE* f2=fopen(fbkp1,"rb"); 2722 semid lid=getShmLockId(m,1); 2723 if(lid<0) 2724 return -1; 2725 lockShm(lid); 2706 2726 FILE* f3=fopen(fbkp,"wb+"); 2707 2727 free(fbkp); … … 2714 2734 fclose(f2); 2715 2735 fclose(f3); 2736 unlockShm(lid); 2716 2737 unlink(fbkp1); 2717 2738 free(fbkp1); 2718 2739 free(tmps1); 2740 unhandleStatus(m); 2719 2741 } 2720 2742
Note: See TracChangeset
for help on using the changeset viewer.