📄 util.c
字号:
/*write the file*/ while(!feof(in)) { ch=getc(in); if(!feof(in)) { if( ch=='\n' ) fprintf(out,"\r\n"); else fprintf(out,"%c",ch); } } /*close the files*/ fclose(in); fclose(out); /*no problems*/ return 0;}/******************************************************************** void ArchiveReady Wait for the archive device or file to be ready for tar to operate on it. arguments: timeout: Timeout in seconds. Timeout granularity is 10 seconds, so smaller timeout values than 10 seconds would timout without waiting return: 0 on success When the device is no longer busy, or on timeout, we return -1 and let the callee handle the timeout********************************************************************/int ArchiveReady(int timeout){ /* try to sync the filesystem */ Sync(); /*device ready*/ return 0;}/******************************************************************** void GenerateStartMark() Generate unique start mark name. arguments: none return: none********************************************************************/void GenerateStartMark(){ FILE *file; time_t timep; struct tm *currtime; int fd; char buf[3]="aa"; unsigned ibuf=0; /*create the unique name for the start mark in the archive*/ if((fd=open("/dev/random",O_RDONLY))!=-1) { read(fd,buf,2); close(fd); } ibuf=buf[0]<<8; ibuf+=buf[1]; timep=time(NULL); currtime=localtime(&timep); sprintf(conf__start_mark,"%02d%02d%04d%02d%02d%04X.chk" ,currtime->tm_mon+1 ,currtime->tm_mday ,currtime->tm_year+1900 ,currtime->tm_hour ,currtime->tm_min ,ibuf); /*generate the file*/ file=fopen(conf__start_mark,"w"); fclose(file);}/******************************************************************** void Execute() Execute an application or script via the execvp() function. arguments: fmt; printf-like command line string. ... ; args. for the 'fmt' string. return: pid of exec()'ed process on success -1 on error********************************************************************/int Execute(char *fmt, ... ){ char **argv=NULL; int pid; va_list ap; char *commandline; unsigned commandline_size=1024; int len; char *p; unsigned parts=1; unsigned pos=0; char *tmp_buf; char *element; FILE *redir; unsigned local_string=0; /* allocate initial commandline buffer */ debug("Allocating initial commandline buffer. size: %u\n",commandline_size); if((commandline=(char*) malloc(commandline_size))==NULL) { debug("That failed...\n"); log("Out of memory\n"); return -1; } /* write the message */ for(;;) { /* try to write the string */ errno=0; va_start(ap,fmt); len=vsnprintf(commandline,commandline_size,fmt,ap); va_end(ap); /* check if the commandline is complete */ if((unsigned) len>commandline_size) { debug("vsnprintf() returned len=%d, commandline_size=%u, Failed..\n",len,commandline_size); commandline_size+=1024; /* try to reallocate the buffer */ debug("Trying to realloc() the commandline buffer\n"); if((p=(char*) realloc(commandline,commandline_size))==NULL) { debug("That failed...\n"); log("Out of memory\n"); free(commandline); return -1; } debug("Ok. commandline_size is now %u\n",commandline_size); commandline=p; } else { debug("vsnprintf() returned len=%d, commandline_size=%u, Ok\n",len,commandline_size); break; } } /* Strip command line for trailing blankspaces */ while(commandline[strlen(commandline)-1]==' ') { commandline[strlen(commandline)-1]='\0'; if(strlen(commandline)==0) { debug("Commandline becomes empty after stripping trailing blankspaces\n"); return -1; } } /* paranoid check */ if(strlen(commandline)==0) { debug("Call of 'Execute()' with zero-length commandline.. Stupid!!\n"); return -1; } /* how many separate parts ?? */ debug("commandline: '%s'\n",commandline); p=commandline; while(*p!='\0') { if(*p=='\"' && local_string==0) { local_string=1; } else if(*p=='\"' && local_string==1) { local_string=0; } if(*p==' ' && local_string==0) parts++; p++; } debug("commandline has %u substrings.\n",parts); /* allocate the arguments array */ debug("Allocating argv array with %u elements\n",parts+1); if((argv=(char**) malloc((parts+1)*sizeof(char**)))==NULL) { debug("Oups.. that failed!\n"); log("Out of memory\n"); return -1; } /* allocate a temporary string for extracting the elements */ debug("Allocating temporary buffer for elements extraction\n"); if((tmp_buf=(char*) malloc(strlen(commandline)+1))==NULL) { debug("That failed....\n"); log("Out of memory\n"); free(argv); return -1; } /* now insert the strings into the argv array */ local_string=0; p=commandline; strcpy(tmp_buf,commandline); element=tmp_buf; do { /* terminate the string, if not the last element */ p=element; while(*p!='\0') { if(*p=='\"' && local_string==0) local_string=1; else if(*p=='\"' && local_string==1) local_string=0; if(*p==' ' && local_string==0) { *p='\0'; break; } p++; } /* allocate space for an element */ debug("Allocating space for element %u\n",pos); if((argv[pos]=(char*) malloc(strlen(element)+1))==NULL) { /* oups.. */ debug("That failed..\n"); log("Out of memory\n"); /* release previous elements, and the argv array */ while(pos!=0) { pos--; free(argv[pos]); } free(argv); free(tmp_buf); /* too bad!! */ return -1; } /* then fill in the element sub-string */ debug("element: '%s'\n",element); if(element[0]=='\"') strcpy(argv[pos],&element[1]); else strcpy(argv[pos],element); if(argv[pos][strlen(argv[pos])-1]=='\"') argv[pos][strlen(argv[pos])-1]='\0'; /* next element */ pos++; strcpy(tmp_buf,commandline); element=p; element++; } while(pos<parts); free(tmp_buf); /* last element must be NULL */ argv[pos]=NULL; /*no more use for the commandline */ free(commandline); /* done.. Fixup embedded substrings and output some debug garbage */ for(pos=0;pos<parts;pos++) { p=argv[pos]; while((p=strchr(p,'\''))!=NULL) *p='\"'; debug("argv[%u]='%s'\n",pos,argv[pos]); } /*execute and wait for the process to finish*/ debug("Trying to fork process..\n"); pid=fork(); debug("pid=%d\n",pid); if(pid==-1) { /*release the arguments array*/ debug("That failed...\n"); for(pos=0;pos<parts;pos++) free(argv[pos]); free(argv); return -1; } if(!pid) { /* flush buffers */ fflush(stdout); fflush(stdin); fflush(stderr); /*redirect stdout and stderr*/ debug("child: redirecting stdout and stderr\n"); freopen("sitback.tmperr","w",stderr); freopen("sitback.tmpout","w",stdout); /* connect stdin to sitback.tmpin */ debug("child: redirecting stdin\n"); if((redir=fopen("sitback.tmpin","r+"))!=NULL) { close(0); dup(fileno(redir)); } /*the child calls exec. exit(127) is only executed if execp fails*/ debug("child: calling execvp()\n"); errno=0; execvp(argv[0],argv); debug("child: unexpected return from execvp()\n"); debug("error: %s\n",strerror(errno)); exit(127); /*huh.. error*/ } /*release the arguments array*/ debug("parent: releasing my copy of the argv array\n"); for(pos=0;pos<parts;pos++) free(argv[pos]); free(argv); /*return pid of the tar-process*/ debug("parent: returning pid %d\n",pid); return pid;}/******************************************************************** void Kill() Kill a process and its children.. arguments: pid; pid of the process to kill return: none********************************************************************/void Kill(int pid){ char command[60]; FILE *file; char line[1024]; int proc_pid; int child_pid[256]; unsigned child_cnt=0; char ch; char *p; unsigned count; /* First get a list of running processes */ sprintf(command,"%s xfa &> sitback.ps",conf__ps); if(system(command)!=0) { UI__Warning("Unable to kill pid %d, system() failed\n",pid); return; } /* read through the file, searching for the pid we want to kill */ if((file=fopen("sitback.ps","r"))==NULL) { UI__Warning("Unable to kill pid %d, fopen() failed\n"); return; } while(!feof(file)) { /* get a line */ fgets(line,1024,file); if(line[strlen(line)-1]!='\n') { do ch=fgetc(file); while(ch!='\n' && ch!='\0'); } else line[strlen(line)-1]='\0'; /* read the pid */ sscanf(line,"%d",&proc_pid); if(proc_pid==pid) { break; } } /* now read on as long as the line contains the '\_' indicator */ while(!feof(file)) { /* get a line */ fgets(line,1024,file); if(line[strlen(line)-1]!='\n') { do ch=fgetc(file); while(ch!='\n' && ch!='\0'); } else line[strlen(line)-1]='\0'; /* check if it is a child process */ if((p=strstr(line,"\\_"))!=NULL && strchr(line,'|')!=NULL) { /* read the pid */ sscanf(line,"%d",&child_pid[child_cnt++]); if(child_cnt>=256) break; } else break; } /* done */ fclose(file); /* now try to kill those ##!)())(/)#(/ processes */ while(1) { /* kill processes */ kill(proc_pid,9); for(count=0;count<child_cnt;count++) kill(child_pid[count],9); /* wait() */ waitpid(proc_pid,NULL,WNOHANG); for(count=0;count<child_cnt;count++) waitpid(child_pid[count],NULL,WNOHANG); /* still some of the processes alive ?? */ if(kill(proc_pid,0)) { for(count=0;count<child_cnt;count++) { if(!kill(child_pid[count],0)) { break; } } if(count>=child_cnt) break; } } unlink("sitback.ps");}/******************************************************************** int RewindDevice() Rewind the device.. Will detect if the device needs to be rewound or not..
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -