Changeset 652 for trunk/zoo-project/zoo-kernel/service_internal.c
- Timestamp:
- Jun 11, 2015, 3:07:11 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/zoo-project/zoo-kernel/service_internal.c
r651 r652 39 39 40 40 #define ERROR_MSG_MAX_LENGTH 1024 41 42 /** 43 * Get the ongoing status of a running service 41 #ifndef RELY_ON_DB 42 #include <dirent.h> 43 44 /** 45 * Read the sid file attached of a service if any 44 46 * 45 47 * @param conf the maps containing the setting of the main.cfg file 46 48 * @param pid the service identifier (usid key from the [lenv] section) 47 * @return the reported status char* (MESSAGE|POURCENTAGE) 48 */ 49 char* _getStatus(maps* conf,int pid){ 50 char lid[1024]; 51 sprintf(lid,"%d",pid); 52 setMapInMaps(conf,"lenv","lid",lid); 53 semid lockid=getShmLockId(conf,1); 49 * @return the reported status char* (temporary/final result) 50 */ 51 char* getStatusId(maps* conf,char* pid){ 52 map* r_inputs = getMapFromMaps (conf, "main", "tmpPath"); 53 char* fbkpid = 54 (char *) 55 malloc ((strlen (r_inputs->value) + strlen (pid) + 7) * sizeof (char)); 56 sprintf (fbkpid, "%s/%s.sid", r_inputs->value, pid); 57 FILE* f0 = fopen (fbkpid, "r"); 58 if(f0!=NULL){ 59 fseek (f0, 0, SEEK_END); 60 long flen = ftell (f0); 61 fseek (f0, 0, SEEK_SET); 62 char *tmps1 = (char *) malloc ((flen + 1) * sizeof (char)); 63 fread(tmps1,flen,1,f0); 64 tmps1[flen]=0; 65 fclose(f0); 66 return tmps1; 67 }else 68 return NULL; 69 } 70 71 /** 72 * Acquire the global lock 73 * 74 * @param conf the maps containing the setting of the main.cfg file 75 * @return a semid 76 */ 77 semid acquireLock(maps* conf){ 78 semid lockid; 79 int itn=0; 80 toRetry1: 81 lockid=getShmLockId(conf,1); 54 82 if( 55 83 #ifdef WIN32 … … 59 87 #endif 60 88 ){ 61 char* tmp = (char*) malloc(3*sizeof(char)); 62 sprintf(tmp,"%d",ZOO_LOCK_CREATE_FAILED); 63 return tmp; 89 #ifdef WIN32 90 return NULL 91 #else 92 return -1; 93 #endif 64 94 } 65 95 if(lockShm(lockid)<0){ 66 fprintf(stderr,"%s %d\n",__FILE__,__LINE__); 67 fflush(stderr); 68 char* tmp = (char*) malloc(3*sizeof(char)); 69 sprintf(tmp,"%d",ZOO_LOCK_ACQUIRE_FAILED); 70 return tmp; 71 } 72 char *tmp=getStatus(pid); 73 unlockShm(lockid); 74 if(tmp==NULL || strncmp(tmp,"-1",2)==0){ 96 if(itn<ZOO_LOCK_MAX_RETRY){ 97 itn++; 98 goto toRetry1; 99 }else 100 #ifdef WIN32 101 return NULL 102 #else 103 return -1; 104 #endif 105 }else 106 return lockid; 107 } 108 109 /** 110 * Read the cache file of a running service 111 * 112 * @param conf the maps containing the setting of the main.cfg file 113 * @param pid the service identifier (usid key from the [lenv] section) 114 * @return the reported status char* (temporary/final result) 115 */ 116 char* _getStatusFile(maps* conf,char* pid){ 117 map* tmpTmap = getMapFromMaps (conf, "main", "tmpPath"); 118 119 struct dirent *dp; 120 DIR *dirp = opendir(tmpTmap->value); 121 char fileName[1024]; 122 int hasFile=-1; 123 if(dirp!=NULL){ 124 char tmp[128]; 125 sprintf(tmp,"_%s.xml",pid); 126 while ((dp = readdir(dirp)) != NULL){ 127 #ifdef DEBUG 128 fprintf(stderr,"File : %s searched : %s\n",dp->d_name,tmp); 129 #endif 130 if(strstr(dp->d_name,"final_")==0 && strstr(dp->d_name,tmp)!=0){ 131 sprintf(fileName,"%s/%s",tmpTmap->value,dp->d_name); 132 hasFile=1; 133 break; 134 } 135 } 136 } 137 if(hasFile>0){ 138 semid lockid; 139 char* stat=getStatusId(conf,pid); 140 if(stat!=NULL){ 141 setMapInMaps(conf,"lenv","lid",stat); 142 lockid=acquireLock(conf); 143 if(lockid<0) 144 return NULL; 145 } 146 147 FILE* f0 = fopen (fileName, "r"); 148 if(f0!=NULL){ 149 fseek (f0, 0, SEEK_END); 150 long flen = ftell (f0); 151 fseek (f0, 0, SEEK_SET); 152 char *tmps1 = (char *) malloc ((flen + 1) * sizeof (char)); 153 fread(tmps1,flen,1,f0); 154 tmps1[flen]=0; 155 fclose(f0); 156 if(stat!=NULL){ 157 unlockShm(lockid); 158 free(stat); 159 } 160 161 return tmps1; 162 } 163 else{ 164 if(stat!=NULL){ 165 unlockShm(lockid); 166 free(stat); 167 } 168 return NULL; 169 } 170 } 171 else 172 return NULL; 173 } 174 175 /** 176 * Get the ongoing status of a running service 177 * 178 * @param conf the maps containing the setting of the main.cfg file 179 * @param pid the service identifier (usid key from the [lenv] section) 180 * @return the reported status char* (MESSAGE|POURCENTAGE) 181 */ 182 char* _getStatus(maps* conf,char* lid){ 183 map* r_inputs = getMapFromMaps (conf, "main", "tmpPath"); 184 char* fbkpid = 185 (char *) 186 malloc ((strlen (r_inputs->value) + strlen (lid) + 9) * sizeof (char)); 187 sprintf (fbkpid, "%s/%s.status", r_inputs->value, lid); 188 FILE* f0 = fopen (fbkpid, "r"); 189 if(f0!=NULL){ 190 char* stat=getStatusId(conf,lid); 191 if(stat!=NULL){ 192 setMapInMaps(conf,"lenv","lid",stat); 193 semid lockid=acquireLock(conf); 194 if(lockid<0) 195 return NULL; 196 } 197 fseek (f0, 0, SEEK_END); 198 long flen = ftell (f0); 199 if(flen>0){ 200 fseek (f0, 0, SEEK_SET); 201 char *tmps1 = (char *) malloc ((flen + 1) * sizeof (char)); 202 fread(tmps1,flen,1,f0); 203 tmps1[flen]=0; 204 fclose(f0); 205 free(fbkpid); 206 if(stat!=NULL){ 207 removeShmLock(conf,1); 208 free(stat); 209 } 210 return tmps1; 211 } 212 fclose(f0); 213 free(fbkpid); 214 if(stat!=NULL){ 215 removeShmLock(conf,1); 216 free(stat); 217 } 218 return NULL; 219 }else{ 220 free(fbkpid); 221 char* stat=getStatusId(conf,lid); 222 setMapInMaps(conf,"lenv","lid",stat); 75 223 removeShmLock(conf,1); 76 } 77 return tmp; 78 } 224 return NULL; 225 } 226 } 227 228 /** 229 * Stop handling status repport. 230 * 231 * @param conf the map containing the setting of the main.cfg file 232 */ 233 void unhandleStatus(maps *conf){ 234 map* r_inputs = getMapFromMaps (conf, "main", "tmpPath"); 235 map* usid = getMapFromMaps (conf, "lenv", "usid"); 236 char* fbkpid = 237 (char *) malloc ((strlen (r_inputs->value) + strlen (usid->value) + 9) 238 * sizeof (char)); 239 sprintf (fbkpid, "%s/%s.status", r_inputs->value, usid->value); 240 unlink(fbkpid); 241 free(fbkpid); 242 } 243 244 /** 245 * Update the current status of the running service. 246 * 247 * @see acquireLock, lockShm 248 * @param conf the map containing the setting of the main.cfg file 249 * @return 0 on success, -2 if shmget failed, -1 if shmat failed 250 */ 251 int _updateStatus(maps *conf){ 252 map* r_inputs = getMapFromMaps (conf, "main", "tmpPath"); 253 map* sid = getMapFromMaps (conf, "lenv", "usid"); 254 char* fbkpid = 255 (char *) 256 malloc ((strlen (r_inputs->value) + strlen (sid->value) + 9) * sizeof (char)); 257 sprintf (fbkpid, "%s/%s.status", r_inputs->value, sid->value); 258 map* status=getMapFromMaps(conf,"lenv","status"); 259 map* msg=getMapFromMaps(conf,"lenv","message"); 260 if(status!=NULL && msg!=NULL && 261 status->value!=NULL && msg->value!=NULL && 262 strlen(status->value)>0 && strlen(msg->value)>1){ 263 semid lockid; 264 char* stat=getStatusId(conf,sid->value); 265 if(stat!=NULL){ 266 lockid=acquireLock(conf); 267 if(lockid<0) 268 return ZOO_LOCK_ACQUIRE_FAILED; 269 } 270 FILE* fstatus=fopen(fbkpid,"w"); 271 if(fstatus!=NULL){ 272 fprintf(fstatus,"%s|%s\n",status->value,msg->value); 273 fflush(fstatus); 274 fclose(fstatus); 275 } 276 if(stat!=NULL){ 277 unlockShm(lockid); 278 free(stat); 279 } 280 } 281 return 0; 282 } 283 284 #endif 79 285 80 286 #ifdef WIN32 … … 96 302 map *tmpMap=getMapFromMaps(conf,"lenv","lid"); 97 303 if(tmpMap==NULL) 98 tmpMap=getMapFromMaps(conf,"lenv"," usid");304 tmpMap=getMapFromMaps(conf,"lenv","osid"); 99 305 100 306 if(tmpMap!=NULL){ … … 130 336 semid sem_id=getShmLockId(conf,1); 131 337 if (CloseHandle(sem_id) == 0) { 338 #ifdef DEBUG 132 339 fprintf(stderr,"Unable to remove semaphore: %s\n", getLastErrorMessage()); 340 #endif 133 341 return -1; 134 342 } … … 165 373 static HANDLE hMapObjectG = NULL; // handle to file mapping 166 374 167 int _updateStatus(maps *conf){168 LPWSTR lpszTmp;169 BOOL fInit;170 char *final_string=NULL;171 char *s=NULL;172 map *tmpMap1;173 map *tmpMap=getMapFromMaps(conf,"lenv","usid");174 semid lockid=getShmLockId(conf,1);175 if(lockid==NULL){176 #ifdef DEBUG177 fprintf(stderr,"Unable to create semaphore on line %d!! \n",__LINE__);178 #endif179 return ZOO_LOCK_CREATE_FAILED;180 }181 if(lockShm(lockid)<0){182 #ifdef DEBUG183 fprintf(stderr,"Unable to create semaphore on line %d!! \n",__LINE__);184 #endif185 return ZOO_LOCK_ACQUIRE_FAILED;186 }187 188 if(hMapObjectG==NULL)189 hMapObjectG = CreateFileMapping(190 INVALID_HANDLE_VALUE, // use paging file191 NULL, // default security attributes192 PAGE_READWRITE, // read/write access193 0, // size: high 32-bits194 SHMEMSIZE, // size: low 32-bits195 TEXT(tmpMap->value)); // name of map object196 if (hMapObjectG == NULL){197 #ifdef DEBUG198 fprintf(stderr,"Unable to create shared memory segment: %s\n", getLastErrorMessage());199 #endif200 return -2;201 }202 fInit = (GetLastError() != ERROR_ALREADY_EXISTS);203 if(lpvMemG==NULL)204 lpvMemG = MapViewOfFile(205 hMapObjectG, // object to map view of206 FILE_MAP_WRITE, // read/write access207 0, // high offset: map from208 0, // low offset: beginning209 0); // default: map entire file210 if (lpvMemG == NULL){211 #ifdef DEBUG212 fprintf(stderr,"Unable to create or access the shared memory segment %s !! \n",tmpMap->value);213 #endif214 return -1;215 }216 memset(lpvMemG, '\0', SHMEMSIZE);217 tmpMap=getMapFromMaps(conf,"lenv","status");218 tmpMap1=NULL;219 tmpMap1=getMapFromMaps(conf,"lenv","message");220 lpszTmp = (LPWSTR) lpvMemG;221 final_string=(char*)malloc((strlen(tmpMap1->value)+strlen(tmpMap->value)+2)*sizeof(char));222 sprintf(final_string,"%s|%s",tmpMap->value,tmpMap1->value);223 for(s=final_string;*s!='\0';*s++){224 *lpszTmp++ = *s;225 }226 *lpszTmp++ = '\0';227 free(final_string);228 unlockShm(lockid);229 return 0;230 }231 375 232 376 char* getStatus(int pid){ … … 287 431 } 288 432 289 void unhandleStatus(maps *conf){290 BOOL fIgnore;291 fIgnore = UnmapViewOfFile(lpvMemG);292 fIgnore = CloseHandle(hMapObjectG);293 }294 295 433 #else 296 434 /** … … 312 450 313 451 /** 314 * Set in the pre-allocated key the zoo_sem_[ SID] string315 * where [ SID] is the lid (if any) or usid value from the [lenv] section.452 * Set in the pre-allocated key the zoo_sem_[OSID] string 453 * where [OSID] is the lid (if any) or osid value from the [lenv] section. 316 454 * 317 455 * @param conf the map containing the setting of the main.cfg file … … 322 460 map *tmpMap=getMapFromMaps(conf,"lenv","lid"); 323 461 if(tmpMap==NULL) 324 tmpMap=getMapFromMaps(conf,"lenv"," usid");462 tmpMap=getMapFromMaps(conf,"lenv","osid"); 325 463 int key=-1; 326 464 if(tmpMap!=NULL) … … 380 518 fprintf(stderr,"Retry to access the semaphore later ...\n"); 381 519 #endif 382 sleep(1);520 zSleep(1); 383 521 } 384 522 } … … 411 549 int sem_id=getShmLockId(conf,nsems); 412 550 if (semctl(sem_id, 0, IPC_RMID, arg) == -1) { 413 perror("semctl"); 551 #ifdef DEBUG 552 perror("semctl remove"); 553 #endif 414 554 return -1; 415 555 } 556 #ifdef DEBUG 557 fprintf(stderr,"Semaphore removed!\n"); 558 #endif 416 559 return 0; 417 560 } … … 429 572 sb.sem_flg = SEM_UNDO; 430 573 if (semop(id, &sb, 1) == -1){ 431 perror("semop"); 574 #ifdef DEBUG 575 perror("semop lock"); 576 #endif 432 577 return -1; 433 578 } … … 447 592 sb.sem_flg = SEM_UNDO; 448 593 if (semop(id, &sb, 1) == -1) { 449 perror("semop"); 594 #ifdef DEBUG 595 perror("semop unlock"); 596 #endif 450 597 return -1; 451 598 } … … 454 601 455 602 /** 456 * Stop handling status repport. 457 * 458 * @param conf the map containing the setting of the main.cfg file 459 */ 460 void unhandleStatus(maps *conf){ 461 int shmid; 462 key_t key; 463 void *shm; 464 struct shmid_ds shmids; 465 map *tmpMap=getMapFromMaps(conf,"lenv","usid"); 466 if(tmpMap!=NULL){ 467 key=atoi(tmpMap->value); 468 if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) { 469 #ifdef DEBUG 470 fprintf(stderr,"shmget failed to update value\n"); 471 #endif 472 }else{ 473 if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) { 474 #ifdef DEBUG 475 fprintf(stderr,"shmat failed to update value\n"); 476 #endif 477 }else{ 478 shmdt(shm); 479 shmctl(shmid,IPC_RMID,&shmids); 480 } 481 } 482 } 483 } 484 485 /** 486 * Update the current of the running service. 487 * 488 * @see getKeyValue, getShmLockId, lockShm 489 * @param conf the map containing the setting of the main.cfg file 490 * @return 0 on success, -2 if shmget failed, -1 if shmat failed 491 */ 492 int _updateStatus(maps *conf){ 493 int shmid; 494 char *shm,*s,*s1; 495 map *tmpMap=NULL; 496 key_t key=getKeyValue(conf); 497 if(key!=-1){ 498 semid lockid=getShmLockId(conf,1); 499 if(lockid<0) 500 return ZOO_LOCK_CREATE_FAILED; 501 if(lockShm(lockid)<0){ 502 return ZOO_LOCK_ACQUIRE_FAILED; 503 } 504 if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) { 505 #ifdef DEBUG 506 fprintf(stderr,"shmget failed to create new Shared memory segment\n"); 507 #endif 508 unlockShm(lockid); 509 return -2; 510 }else{ 511 if ((shm = (char*) shmat(shmid, NULL, 0)) == (char *) -1) { 512 #ifdef DEBUG 513 fprintf(stderr,"shmat failed to update value\n"); 514 #endif 515 unlockShm(lockid); 516 return -1; 517 } 518 else{ 519 tmpMap=getMapFromMaps(conf,"lenv","status"); 520 s1=shm; 521 for(s=tmpMap->value;*s!=NULL && *s!=0;s++){ 522 *s1++=*s; 523 } 524 *s1++='|'; 525 tmpMap=getMapFromMaps(conf,"lenv","message"); 526 if(tmpMap!=NULL) 527 for(s=tmpMap->value;*s!=NULL && *s!=0;s++){ 528 *s1++=*s; 529 } 530 *s1=NULL; 531 shmdt((void *)shm); 532 unlockShm(lockid); 533 } 534 } 535 } 536 return 0; 537 } 538 539 /** 540 * Update the current of the running service. 603 * Get the current status of the running service. 541 604 * 542 605 * @see getKeyValue, getShmLockId, lockShm
Note: See TracChangeset
for help on using the changeset viewer.