bool parse( rt::cstring& in ) { in.trim_left(); if( first_char( in ) != '[' ) return false; in.trim_left( 1 ); if( !first.parse( in ) ) return false; in.trim_left(); if( first_char( in ) != ',' ) return false; in.trim_left( 1 ); if( !second.parse( in ) ) return false; in.trim_left(); if( first_char( in ) != ']' ) return false; in.trim_left( 1 ); return true; }
void followup(player *p,char *str) { char *oldstack,*body,*indent,*scan,*first; note *article,*old; oldstack=stack; old=find_news_article(atoi(str)); if (!old) { sprintf(stack,"No such news article '%s'\n",str); stack=end_string(stack); tell_player(p,oldstack); stack=oldstack; return; } article=create_note(); if (strstr(old->header,"Re: ")==old->header) strcpy(article->header,old->header); else { sprintf(stack,"Re: %s",old->header); strncpy(article->header,stack,MAX_TITLE-2); } article->flags |= NEWS_ARTICLE; first=first_char(p); scan=strstr(first,"followup"); if (scan && (scan!=first_char(p)) && (*(scan-1)=='a')) article->flags |= ANONYMOUS; strcpy(article->name,p->name); indent=stack; get_string(stack,old->text.where); stack=end_string(stack); body=stack; if (old->flags&ANONYMOUS) sprintf(stack,"From annonymous article written on %s ...\n", convert_time(old->date)); else sprintf(stack,"On %s, %s wrote ...\n", convert_time(old->date),old->name); stack=strchr(stack,0); while(*indent) { *stack++='>'; *stack++=' '; while(*indent && *indent!='\n') *stack++=*indent++; *stack++='\n'; indent++; } *stack++='\n'; *stack++=0; tell_player(p,"Please trim article as much as is possible ....\n"); start_edit(p,MAX_ARTICLE_SIZE,end_post,quit_post,body); if (p->edit_info) p->edit_info->misc=(void *)article; else FREE(article); stack=oldstack; }
static char *next_line(char *buf, int max, char *str) { char *c = buf; int sanity = 0; int first=1; /* Skip current line, and any following comments or blank lines. * Return pointer to the next line. Also copy * the line to a null-terminated C string because sscanf * may perform really poorly on very long strings. */ if (str) *str = 0; while (c && (first || (*c == '%'))){ /* go to end of line */ while (*c && (*c != '\n') && (sanity++ < max)) c++; if ((sanity >= max) || (*c == 0)){ return NULL; } first = 0; c++; /* skip blank lines, and/or initial white space */ c = first_char(c, max); } if (str && c){ make_string(c, str); } return c; }
bool parse( rt::cstring& in ) { in.trim_left(); if( first_char( in ) != '(' ) return false; in.trim_left( 1 ); rt::cstring::size_type pos = in.find( ")" ); if( pos == rt::cstring::npos ) return false; rt::cstring ss( in.begin(), pos ); pos = ss.find( "," ); if( pos == rt::cstring::npos ) return false; rt::cstring f( ss.begin(), pos ); rt::cstring s( ss.begin()+pos+1, ss.end() ); f.trim(); s.trim(); try { first = boost::lexical_cast<int>( f ); second = boost::lexical_cast<int>( s ); } catch( boost::bad_lexical_cast const& ) { return false; } in.trim_left( ss.end()+1 ); return true; }
int process_tex_file (const char *fname) { FILE *fp; fp = fopen(fname, "r"); if (fp == NULL) { fprintf(stderr, "couldn't open %s\n", fname); return 1; } while (fgets(line, sizeof line, fp)) { if (strstr(line, START)) { printf("\n\n# %s", line); while (fgets(line, sizeof line, fp)) { if (strstr(line, END)) { printf("# %s", line); break; } else if (first_char(line) == '\\') { printf("# %s", line); } else { fputs(line, stdout); } } } } fclose(fp); return 0; }
void send_letter(player *p,char *str) { note *mail; char *subject,*scan,*first; int length; if (posted_count(p)>=p->max_mail) { tell_player(p,"Sorry, you have reached your mail limit.\n"); return; } subject=next_space(str); if (!*subject) { tell_player(p,"Format: post <character(s)> <subject>\n"); return; } *subject++=0; mail=create_note(); first=first_char(p); scan=strstr(first,"post"); if (scan && (scan!=first_char(p)) && (*(scan-1)=='a')) mail->flags |= ANONYMOUS; strcpy(mail->name,p->name); strncpy(mail->header,subject,MAX_TITLE-2); tell_player(p,"Enter main text of the letter...\n"); *stack=0; start_edit(p,MAX_ARTICLE_SIZE,end_mail,quit_mail,stack); if (p->edit_info) { p->edit_info->misc=(void *)mail; length=strlen(str)+1; mail->text.where=(char *)MALLOC(length); memcpy(mail->text.where,str,length); } else FREE(mail); }
void post_news(player *p,char *str) { note *article; char *scan,*first; if (!*str) { tell_player(p,"Format: post <header>\n"); return; } article=create_note(); strncpy(article->header,str,MAX_TITLE-2); article->flags |= NEWS_ARTICLE; first=first_char(p); scan=strstr(first,"post"); if (scan && (scan!=first_char(p)) && (*(scan-1)=='a')) article->flags |= ANONYMOUS; strcpy(article->name,p->name); tell_player(p,"Now enter the main body text for the article.\n"); *stack=0; start_edit(p,MAX_ARTICLE_SIZE,end_post,quit_post,stack); if (p->edit_info) p->edit_info->misc=(void *)article; else FREE(article); }
bool parse( rt::cstring& in ) { in.trim_left(); if( first_char( in ) != '[' ) return false; in.trim_left( 1 ); if( !first.parse( in ) ) return false; in.trim_left(); if( first_char( in ) != ',' ) return false; in.trim_left( 1 ); rt::cstring::size_type pos = in.find( "]" ); if( pos == rt::cstring::npos ) return false; rt::cstring ss( in.begin(), pos ); ss.trim(); try { second = boost::lexical_cast<int>( ss ); } catch( boost::bad_lexical_cast const& ) { return false; } in.trim_left( pos+1 ); return true; }
int main(int argc, char **argv) { int shmfd, shm_seg_size = sizeof(struct msg); struct msg *shm_msg; char buff[MAX_MSG_LENGTH]; sem_t *semfd; // semaphore file descriptor // open named semaphore semfd = sem_open(SEM_PATH, O_RDWR, S_IRWXU | S_IRWXG, 0); if(semfd == SEM_FAILED) error("sem_open()"); // open shared memory shmfd = shm_open(SHM_PATH, O_RDWR, S_IRWXU | S_IRWXG); if(shmfd < 0) error("shm_open()"); // map shared memory to address space shm_msg = (struct msg *) mmap(NULL, shm_seg_size, PROT_READ | PROT_WRITE, MAP_SHARED, shmfd, 0); if(shm_msg == NULL) error("mmap()"); pid_t pid; pid = getpid(); user *usr = new_user(pid, "",""); // this user strncpy(usr->channel, "all", 3);; pid_t fid = fork(); // input handling if(fid > 0) { // register client sem_wait(semfd); shm_msg->read = '_'; shm_msg->pid = pid; shm_msg->type = TYPE_CLIENT_MSG; strncpy(shm_msg->cmd, "reg", MAX_CMD_LENGTH); sem_post(semfd); // get input line by line while(getLine("", buff, sizeof(buff)) == IN_OK) { while(shm_msg->read && shm_msg->read == '_') sleep(1); if(!shm_msg->read || shm_msg->read == '!') { sem_wait(semfd); shm_msg->pid = pid; shm_msg->type = TYPE_CLIENT_MSG; shm_msg->read = '_'; if(first_char(buff) == '/') { cmd(buff, shm_msg->cmd); remove_cmd(buff); } else { snprintf(shm_msg->cmd, MAX_CMD_LENGTH, "msg"); } snprintf(shm_msg->content, MAX_MSG_LENGTH, "%s", buff); } } // reponse handling } else { while(1) { fflush(stdout); fflush(stdin); if(shm_msg->read == '*') { if(strncmp("resp", shm_msg->cmd, 4) == 0 && shm_msg->pid == pid && shm_msg->type == TYPE_SERVER_MSG) { printf("\n"); printf("response: %s\n", shm_msg->content); shm_msg->read = '!'; char CMD[MAX_MSG_LENGTH]; cmd(shm_msg->content, CMD); remove_cmd(shm_msg->content); if(strncmp(CMD, "join", 4) == 0) { strncpy(usr->channel, shm_msg->content, MAX_CHAN_LEN); } else if(strncmp(CMD, "name", 4) == 0) { strncpy(usr->name, shm_msg->content, MAX_NAME_LEN); } sem_post(semfd); } else if(strncmp("pm", shm_msg->cmd, 2) == 0 && shm_msg->pid == pid && shm_msg->type == TYPE_CLIENT_MSG) { printf("[PM -> %d] %s: %s\n", shm_msg->pid, shm_msg->from, shm_msg->content); shm_msg->read = '!'; } else if(strncmp("msg", shm_msg->cmd, 3) == 0) { if((strncmp(shm_msg->channel, usr->channel, MAX_CHAN_LEN) == 0) && shm_msg->pid != pid && shm_msg->read != '!') { printf("[#%s] <%s> ", shm_msg->channel, shm_msg->from); printf("%s\n", shm_msg->content); shm_msg->read = '!'; } } } } } return EXIT_SUCCESS; }
void reply_article(player *p,char *str) { note *mail,*old; char *indent,*body,*oldstack,*scan,*first; int length; oldstack=stack; if (!*str) { tell_player(p,"Format: reply <no>\n"); return; } if (posted_count(p)>=p->max_mail) { tell_player(p,"Sorry, you have reached your mail limit.\n"); return; } if (!p->saved) { tell_player(p,"Eeek, no saved bits.\n"); return; } old=find_news_article(atoi(str)); if (!old) { sprintf(stack,"Can't find letter '%s'\n",str); stack=end_string(stack); tell_player(p,oldstack); stack=oldstack; return; } mail=create_note(); first=first_char(p); scan=strstr(first,"reply"); if (scan && (scan!=first_char(p)) && (*(scan-1)=='a')) mail->flags |= ANONYMOUS; if (old->flags&ANONYMOUS) mail->flags |= SUPRESS_NAME; strcpy(mail->name,p->name); if (strstr(old->header,"Re: ")==old->header) strcpy(mail->header,old->header); else { sprintf(stack,"Re: %s",old->header); strncpy(mail->header,stack,MAX_TITLE-2); } indent=stack; get_string(stack,old->text.where); stack=end_string(stack); body=stack; sprintf(stack,"In your article '%s' you wrote ...\n",old->header); stack=strchr(stack,0); while(*indent) { *stack++='>'; *stack++=' '; while(*indent && *indent!='\n') *stack++=*indent++; *stack++='\n'; indent++; } *stack++='\n'; *stack++=0; tell_player(p,"Please trim letter as much as possible...\n"); start_edit(p,MAX_ARTICLE_SIZE,end_mail,quit_mail,body); if (p->edit_info) { p->edit_info->misc=(void *)mail; length=strlen(old->name)+1; mail->text.where=(char *)MALLOC(length); memcpy(mail->text.where,old->name,length); } else FREE(mail); stack=oldstack; }
// Copied from mtcp/mtcp_restart.c. // Let's keep this code close to MTCP code to avoid maintenance problems. // MTCP code in: mtcp/mtcp_restart.c:open_ckpt_to_read() // A previous version tried to replace this with popen, causing a regression: // (no call to pclose, and possibility of using a wrong fd). // Returns fd; sets ext_decomp_pid, if checkpoint was compressed. static int open_ckpt_to_read(const char *filename) { int fd; int fds[2]; char fc; const char *decomp_path; const char **decomp_args; const char *gzip_path = "gzip"; static const char * gzip_args[] = { "gzip", "-d", "-", NULL }; #ifdef HBICT_DELTACOMP const char *hbict_path = "hbict"; static const char *hbict_args[] = { "hbict", "-r", NULL }; #endif pid_t cpid; fc = first_char(filename); fd = open(filename, O_RDONLY); JASSERT(fd>=0)(filename).Text("Failed to open file."); if(fc == DMTCP_MAGIC_FIRST) /* no compression */ return fd; #ifdef HBICT_DELTACOMP else if (fc == GZIP_FIRST || fc == HBICT_FIRST){ /* External compression */ #else else if(fc == GZIP_FIRST){ /* gzip */ #endif if( fc == GZIP_FIRST ){ decomp_path = gzip_path; decomp_args = gzip_args; } #ifdef HBICT_DELTACOMP else{ decomp_path = hbict_path; decomp_args = hbict_args; } #endif JASSERT(pipe(fds) != -1)(filename).Text("Cannot create pipe to execute gunzip to decompress checkpoint file!"); cpid = _real_fork(); JASSERT(cpid != -1).Text("ERROR: Cannot fork to execute gunzip to decompress checkpoint file!"); if(cpid > 0) /* parent process */ { JTRACE ( "created child process to uncompress checkpoint file")(cpid); ext_decomp_pid = cpid; close(fd); close(fds[1]); return fds[0]; } else /* child process */ { JTRACE ( "child process, will exec into external de-compressor"); fd = dup(dup(dup(fd))); fds[1] = dup(fds[1]); close(fds[0]); JASSERT(fd != -1); JASSERT(dup2(fd, STDIN_FILENO) == STDIN_FILENO); close(fd); JASSERT(dup2(fds[1], STDOUT_FILENO) == STDOUT_FILENO); close(fds[1]); _real_execvp(decomp_path, (char **)decomp_args); JASSERT(decomp_path!=NULL)(decomp_path).Text("Failed to launch gzip."); /* should not get here */ JASSERT(false)("ERROR: Decompression failed! No restoration will be performed! Cancelling now!"); abort(); } } else /* invalid magic number */ JASSERT(false).Text("ERROR: Invalid magic number in this checkpoint file!"); // NOT_REACHED return -1; } // See comments above for open_ckpt_to_read() int dmtcp::CkptSerializer::openDmtcpCheckpointFile(const dmtcp::string& path){ // Function also sets dmtcp::ext_decomp_pid::ConnectionToFds int fd = open_ckpt_to_read( path.c_str() ); // The rest of this function is for compatibility with original definition. JASSERT(fd>=0)(path).Text("Failed to open file."); char buf[512]; const int len = strlen(DMTCP_FILE_HEADER); JASSERT(_real_read(fd, buf, len)==len)(path).Text("_real_read() failed"); if(strncmp(buf, DMTCP_FILE_HEADER, len)==0){ JTRACE("opened checkpoint file [uncompressed]")(path); }else{ close_ckpt_to_read(fd); fd = open_ckpt_to_read( path.c_str() ); /* Re-open from beginning */ } return fd; }
static int process_mtxp_file(PARIO_INFO_PTR pio_info, char *filebuf, int fsize, int nprocs, int myrank, int *nGlobalEdges, int *nGlobalVtxs, int *vtxWDim, int *edgeWDim, int *nMyPins, int **myPinI, int **myPinJ, int *nMyVtx, int **myVtxNum, float **myVtxWgts, int *nMyEdgeWgts, int **myEdgeNum, float **myEdgeWgts) { int nedges, nvtxs, npins, vdim, edim, numew, nFileProcs; int countMyVtxs, countMyEdges, countMyPins; int proc, mine, nextpin, rc; int eid, vid, i, j; int mineid = -1, minvid = 0, maxeid = -1, maxvid = -1, numeids, numvids; int nexte, nextv, nDistProcs; int myminPin, mymaxPin, myminVtx, mymaxVtx, myshare, share; float pinVal; int *myi, *myj, *myvno, *myeno; char *line, *token, *linestr, *pinBuf, *vwgtBuf, *ewgtBuf; float *myvwgt, *myewgt; char cmesg[256]; *nGlobalEdges = *nGlobalVtxs = *nMyPins = 0; *nMyVtx = *nMyEdgeWgts = *vtxWDim = *edgeWDim = 0; *myPinI = *myPinJ = *myVtxNum = *myEdgeNum = NULL; *myVtxWgts = *myEdgeWgts = NULL; linestr = (char *)malloc(MATRIX_MARKET_MAX_LINE+1); if (!linestr){ sprintf(cmesg, "memory allocation\n"); Gen_Error(0, cmesg); return 0; } line = first_char(filebuf, fsize); if (line && (*line == '%')){ line = next_line(filebuf, fsize, linestr); /* skip comments & blanks */ } if (!line){ sprintf(cmesg, "Truncated file\n"); Gen_Error(0, cmesg); return 0; } rc = sscanf(linestr, "%d %d %d %d %d %d %d", &nedges, &nvtxs, &npins, &nFileProcs, &vdim, &numew, &edim); if (rc != 7){ sprintf(cmesg, "%s\nFirst line should have 7 values in it\n",linestr); Gen_Error(0, cmesg); return 0; } myminPin = mymaxPin = -1; myminVtx = mymaxVtx = -1; if ((pio_info->init_dist_procs > 0) && (pio_info->init_dist_procs < nprocs)){ nDistProcs = pio_info->init_dist_procs; } else{ nDistProcs = nprocs; } if (pio_info->init_dist_type == INITIAL_LINEAR){ /* vertex distribution */ if (myrank < nDistProcs){ share = nvtxs / nDistProcs; i = nvtxs - (nDistProcs * share); myshare = ((myrank < i) ? share+1 : share); myminVtx = myrank * myshare; if (myrank >= i) myminVtx += i; mymaxVtx = myminVtx + myshare - 1; } } if (pio_info->init_dist_pins == INITIAL_LINEAR){ /* pin distribution */ share = npins / nprocs; i = npins - (nprocs * share); myshare = ((myrank < i) ? share+1 : share); myminPin = myrank * myshare; if (myrank >= i) myminPin += i; mymaxPin = myminPin + myshare - 1; } myvno = myeno = myi = myj = NULL; myewgt = myvwgt = NULL; pinBuf = vwgtBuf = ewgtBuf = NULL; /* Read through the pins, vertex weights and edge weights. * Accumulate all vertex and edge IDs, and map these to * consecutive global numbers beginning with zero. * * Also count my pins, my vertices, and the number of edges * for which I provide weights. */ countMyPins = 0; countMyVtxs = 0; countMyEdges = 0; nexte = nextv = 0; for (i=0; i<npins; i++){ /* PINS */ line = next_line(line, fsize, linestr); if (!line){ if (myrank == 0) printf("File is truncated in pins\n"); goto failure; } if (!i) pinBuf = line; rc = sscanf(linestr, "%d %d %f %d", &eid, &vid, &pinVal, &proc); if ((rc != 4) || (eid < 1) || (eid > nedges) || (vid < 1) || (vid > nvtxs) || (proc < 0) || (proc >= nFileProcs)){ sprintf(cmesg,"%s\nlooking for \"edge vertex pin process\"\n",linestr); Gen_Error(0, cmesg); goto failure; } eid -= 1; vid -= 1; mine = my_pin(eid, vid, proc, i, npins, myminPin, mymaxPin, myrank, nprocs, pio_info); if (i){ /* should get rid of this, IDs are always 1 ... N */ if (eid < mineid) mineid = eid; if (vid < minvid) minvid = vid; if (eid > maxeid) maxeid = eid; if (vid > maxvid) maxvid = vid; } else{ mineid = maxeid = eid; minvid = maxvid = vid; } if (mine){ countMyPins++; } } if (npins) numeids = maxeid - mineid + 1; else numeids = nedges; for (i=0; i<nvtxs; i++){ /* VERTICES and possibly WEIGHTS */ line = next_line(line, fsize, linestr); if (!line){ sprintf(cmesg,"File is truncated at vertex weights\n"); Gen_Error(0, cmesg); goto failure; } if (!i) vwgtBuf = line; rc = sscanf(linestr, "%d", &vid); token = get_token(linestr, vdim + 1, strlen(linestr)); if (token) proc = atoi(token); if ((rc != 1) || !token || (vid < 1) || (vid > nvtxs) || (proc < 0) || (proc >= nFileProcs)){ sprintf(cmesg, "%s\nlooking for \"vertex {optional weights} process\"\n",linestr); Gen_Error(0, cmesg); goto failure; } vid -= 1; if (i) { if (vid < minvid) minvid = vid; if (vid > maxvid) maxvid = vid; } else minvid = maxvid = vid; mine = my_vtx(proc, i, myminVtx, mymaxVtx, myrank, nDistProcs, pio_info); if (mine){ countMyVtxs++; } } numvids = maxvid - minvid + 1; if (numew > 0){ /* HYPEREDGE WEIGHTS */ for (i=0; i<numew; i++){ line = next_line(line, fsize, linestr); if (!line){ /* error */ sprintf(cmesg,"File is truncated at edge weights\n"); Gen_Error(0, cmesg); goto failure; } if (!i) ewgtBuf = line; rc = sscanf(linestr, "%d", &eid); token = get_token(linestr, edim + 1, strlen(linestr)); if (token) proc = atoi(token); if ((rc != 1) || !token || (eid < 1) || (eid > nedges) || (proc < 0) || (proc >= nFileProcs)){ sprintf(cmesg, "%s\nlooking for \"edge {optional weights} process\"\n",linestr); Gen_Error(0, cmesg); goto failure; } proc = atoi(token); eid -= 1; if (eid < mineid) mineid = eid; if (eid > maxeid) maxeid = eid; if (rc < 0){ sprintf(cmesg,"File has more than %d edge IDs\n",nedges); Gen_Error(0, cmesg); goto failure; } if ((proc % nprocs) == myrank){ countMyEdges++; } } } if (numew) numeids = maxeid - mineid + 1; else numeids = nedges; rc = 1; if (numeids != nedges){ sprintf(cmesg,"found range of %d edges (not %d expected) in file\n", numeids,nedges); rc = 0; } else if (numvids != nvtxs){ sprintf(cmesg,"found range of %d vertices (not %d expected) in file\n", numvids,nvtxs); rc = 0; } if (!rc){ Gen_Error(0, cmesg); goto failure; } /* Start over at beginning of file and save my pins, and weights */ if (countMyPins > 0){ myi = (int *)malloc(sizeof(int) * countMyPins); myj = (int *)malloc(sizeof(int) * countMyPins); if (!myi || !myj){ sprintf(cmesg,"memory allocation\n"); Gen_Error(0, cmesg); goto failure; } nextpin = 0; make_string(pinBuf, linestr); line = pinBuf; for (i=0; i<npins; i++){ sscanf(linestr, "%d %d %f %d", &eid, &vid, &pinVal, &proc); eid -= 1; vid -= 1; mine = my_pin(eid, vid, proc, i, npins, myminPin, mymaxPin, myrank, nprocs, pio_info); if (mine){ myi[nextpin] = eid - mineid; myj[nextpin] = vid - minvid; nextpin++; } line = next_line(line, fsize, linestr); } } if (countMyVtxs){ myvno = (int *)malloc(sizeof(int) * countMyVtxs); if (vdim > 0){ myvwgt = (float *)malloc(sizeof(float) * countMyVtxs * vdim); } if (!myvno || (vdim && !myvwgt)){ sprintf(cmesg,"memory allocation\n"); Gen_Error(0, cmesg); goto failure; } nextv = 0; make_string(vwgtBuf, linestr); line = vwgtBuf; for (i=0; i<nvtxs; i++){ sscanf(linestr, "%d", &vid); token = get_token(linestr, vdim + 1, strlen(linestr)); proc = atoi(token); vid -= 1; mine = my_vtx(proc, i, myminVtx, mymaxVtx, myrank, nDistProcs, pio_info); if (mine){ myvno[nextv] = vid - minvid; for (j=0; j<vdim; j++){ token = get_token(linestr, 1 + j, strlen(linestr)); if (!token){ sprintf(cmesg,"%s\nCan't find %d vertex weights\n",linestr,vdim); Gen_Error(0, cmesg); goto failure; } myvwgt[nextv*vdim + j] = (float)atof(token); } nextv++; } line = next_line(line, fsize, linestr); } } if (countMyEdges > 0){ myeno = (int *)malloc(sizeof(int) * countMyEdges); myewgt = (float *)malloc(sizeof(float) * countMyEdges * edim); if (!myeno || !myewgt){ sprintf(cmesg,"memory allocation\n"); Gen_Error(0, cmesg); goto failure; } nexte = 0; make_string(ewgtBuf, linestr); line = ewgtBuf; for (i=0; i<numew; i++){ sscanf(linestr, "%d", &eid); token = get_token(linestr, edim + 1, strlen(linestr)); proc = atoi(token); eid -= 1; if ((proc % nprocs) == myrank){ myeno[nexte] = eid - mineid; for (j=0; j<edim; j++){ token = get_token(linestr, 1 + j, strlen(linestr)); if (!token){ sprintf(cmesg,"%s\nCan't find %d edge weights\n",linestr,edim); Gen_Error(0, cmesg); goto failure; } myewgt[nexte*edim + j] = (float)atof(token); } nexte++; } line = next_line(line, fsize, linestr); } } rc = 1; /* success */ goto done; failure: if (myvno) free(myvno); if (myvwgt) free(myvwgt); if (myeno) free(myeno); if (myewgt) free(myewgt); if (myi) free(myi); if (myj) free(myj); nedges = nvtxs = vdim = edim = 0; countMyPins = countMyVtxs = countMyEdges = 0; rc = 0; done: free(linestr); *nGlobalEdges = nedges; *nGlobalVtxs = nvtxs; *vtxWDim = vdim; *edgeWDim = edim; *nMyPins = countMyPins; *myPinI = myi; *myPinJ = myj; *nMyVtx = countMyVtxs; *myVtxNum = myvno; *myVtxWgts = myvwgt; *nMyEdgeWgts = countMyEdges; *myEdgeNum = myeno; *myEdgeWgts = myewgt; return rc; }
// Copied from mtcp/mtcp_restart.c. // Let's keep this code close to MTCP code to avoid maintenance problems. // MTCP code in: mtcp/mtcp_restart.c:open_ckpt_to_read() // A previous version tried to replace this with popen, causing a regression: // (no call to pclose, and possibility of using a wrong fd). // Returns fd; sets ext_decomp_pid, if checkpoint was compressed. static int open_ckpt_to_read(const char *filename) { int fd; int fds[2]; char fc; const char *decomp_path; const char **decomp_args; const char *gzip_path = "gzip"; static const char * gzip_args[] = { "gzip", "-d", "-", NULL }; #ifdef HBICT_DELTACOMP const char *hbict_path = "hbict"; static const char *hbict_args[] = { "hbict", "-r", NULL }; #endif pid_t cpid; fc = first_char(filename); fd = open(filename, O_RDONLY); JASSERT(fd>=0)(filename).Text("Failed to open file."); if (fc == DMTCP_MAGIC_FIRST) { /* no compression */ return fd; } else if (fc == GZIP_FIRST #ifdef HBICT_DELTACOMP || fc == HBICT_FIRST #endif ) { if (fc == GZIP_FIRST) { decomp_path = gzip_path; decomp_args = gzip_args; } #ifdef HBICT_DELTACOMP else { decomp_path = hbict_path; decomp_args = hbict_args; } #endif JASSERT(pipe(fds) != -1) (filename) .Text("Cannot create pipe to execute gunzip to decompress ckpt file!"); cpid = _real_fork(); JASSERT(cpid != -1) .Text("ERROR: Cannot fork to execute gunzip to decompress ckpt file!"); if (cpid > 0) { /* parent process */ JTRACE("created child process to uncompress checkpoint file") (cpid); ext_decomp_pid = cpid; close(fd); close(fds[1]); return fds[0]; } else { /* child process */ JTRACE ( "child process, will exec into external de-compressor"); fd = dup(dup(dup(fd))); fds[1] = dup(fds[1]); close(fds[0]); JASSERT(fd != -1); JASSERT(dup2(fd, STDIN_FILENO) == STDIN_FILENO); close(fd); JASSERT(dup2(fds[1], STDOUT_FILENO) == STDOUT_FILENO); close(fds[1]); _real_execvp(decomp_path, (char **)decomp_args); JASSERT(decomp_path!=NULL) (decomp_path) .Text("Failed to launch gzip."); /* should not get here */ JASSERT(false) .Text("Decompression failed! No restoration will be performed!"); } } else { /* invalid magic number */ JASSERT(false) .Text("ERROR: Invalid magic number in this checkpoint file!"); } return -1; }
/** * Sends the HTTP Response along with the web * content to the socket file descriptor. * * @param fileToSend: location of file to display to client * @param sock: Socket file descriptor * @param home: * @param content: * @param response: Holds information regarding HTTP request */ void SendDataBin(char *fileToSend, int sock, char *home, char *content, HTTP_Response *response) { char *fullPathToFile; //[256]; char Header[1024]; char buffer[256]; char *endChar; int size; bzero(Header, sizeof(Header)); /* * Build the full path to the file */ bool hasSlash; if ((last_char(content) == "/") || (first_char(fileToSend) == '/')) hasSlash = TRUE; else hasSlash = FALSE; size = strlen(home) + strlen(content) + strlen(fileToSend); if (hasSlash) { size += 2; fullPathToFile = malloc(size); sprintf(fullPathToFile, "%s/%s%s", home, content, fileToSend); } else { size += 3; fullPathToFile = malloc(size); sprintf(fullPathToFile, "%s/%s/%s", home, content, fileToSend); } //int file_open = open(fullPathToFile, O_RDONLY); int fileType = TypeOfFile(fullPathToFile); printf("Filetype: %d\n", fileType); if ((fileType == FILE_NOT_FOUND) || (fileType == ERROR_FILE)) { //Requested page not available. bzero(fullPathToFile, strlen(fullPathToFile)); size = strlen(home) + strlen(content); if (last_char(content) == "/") { size += 16; fullPathToFile = realloc(fullPathToFile, size); sprintf(fullPathToFile, "%s/%snot_found.html", home, content); } else { size += 17; fullPathToFile = realloc(fullPathToFile, size); sprintf(fullPathToFile, "%s/%s/not_found.html", home, content); } } else if (fileType == DIRECTORY) { //Append index.html size += 10; fullPathToFile = realloc(fullPathToFile, size); strcat(fullPathToFile, "index.html"); } printf("File to open: %s\n", fullPathToFile); int file_open; FILE *file_fd = NULL; if (fileType == EXECUTABLE_FILE) { if ((file_fd = popen(fullPathToFile, "r")) == NULL) { perror("popen"); exit(-1); } file_open = fileno(file_fd); //Convert from FILE to file descriptor integer } else { file_open = open(fullPathToFile, O_RDONLY); } if (file_open < 0) { perror("Open file error"); exit(-1); } long unsigned filesize = lseek(file_open, (off_t)0, SEEK_END); lseek(file_open, (off_t)0, SEEK_SET); //Set back to starting position setupHeader(Header, response, filesize); printf("\nHeader:\n%s\n\n", Header); /* * Send the header, open the requested file. * Send requested file, close file. */ printf("file: %s\n", fullPathToFile); write(sock, Header, strlen(Header)); // If request was for HEAD, do not send the body information if (strncmp(response -> HTTP_Type, "HEAD", 4) == 0) { free (fullPathToFile); fullPathToFile = NULL; return; } bzero(buffer, sizeof(buffer)); while (read(file_open, buffer, sizeof(buffer)) > 0) { write(sock, buffer, sizeof(buffer)); bzero(buffer, sizeof(buffer)); } if (fileType == EXECUTABLE_FILE) pclose(file_fd); else close(file_open); // Deallocate allocated pointers free (fullPathToFile); fullPathToFile = NULL; }
/** * This function will open the checkpoint file stored at the given filename. * It will check the magic number and take the appropriate action. If the * magic number is unknown, we will abort. The fd returned points to the * beginning of the uncompressed data. * NOTE: related code in ../dmtcp/src/connectionmanager.cpp:open_ckpt_to_read() * * @param filename the name of the checkpoint file * @return the fd to use */ static int open_ckpt_to_read(char *filename, char *envp[]) { int fd; int fds[2]; char fc; char *gzip_cmd = "gzip"; static char *gzip_args[] = { "gzip", "-d", "-", NULL }; #ifdef HBICT_DELTACOMP char *hbict_cmd = "hbict"; static char *hbict_args[] = { "hbict", "-r", NULL }; #endif char decomp_path[PATH_MAX]; static char **decomp_args; pid_t cpid; fc = first_char(filename); fd = mtcp_sys_open(filename, O_RDONLY, 0); if(fd < 0) { MTCP_PRINTF("ERROR: Cannot open checkpoint file %s\n", filename); mtcp_abort(); } if (fc == MAGIC_FIRST || fc == 'D') /* no compression ('D' from DMTCP) */ return fd; else if (fc == GZIP_FIRST #ifdef HBICT_DELTACOMP || fc == HBICT_FIRST #endif ) { /* Set prog_path */ if( fc == GZIP_FIRST ){ decomp_args = gzip_args; if( mtcp_find_executable(gzip_cmd, getenv("PATH"), decomp_path) == NULL ) { MTCP_PRINTF("ERROR: Cannot find gunzip to decompress ckpt file!\n"); mtcp_abort(); } } #ifdef HBICT_DELTACOMP if( fc == HBICT_FIRST ){ decomp_args = hbict_args; if( mtcp_find_executable(hbict_cmd, getenv("PATH"), decomp_path) == NULL ) { MTCP_PRINTF("ERROR: Cannot find hbict to decompress ckpt file!\n"); mtcp_abort(); } } #endif if (mtcp_sys_pipe(fds) == -1) { MTCP_PRINTF("ERROR: Cannot create pipe to execute gunzip to decompress" " checkpoint file!\n"); mtcp_abort(); } cpid = mtcp_sys_fork(); if(cpid == -1) { MTCP_PRINTF("ERROR: Cannot fork to execute gunzip to decompress" " checkpoint file!\n"); mtcp_abort(); } else if(cpid > 0) /* parent process */ { decomp_child_pid = cpid; mtcp_sys_close(fd); mtcp_sys_close(fds[1]); return fds[0]; } else /* child process */ { fd = mtcp_sys_dup(mtcp_sys_dup(mtcp_sys_dup(fd))); if (fd == -1) { MTCP_PRINTF("ERROR: dup() failed! No restoration will be performed!" " Cancel now!\n"); mtcp_abort(); } fds[1] = mtcp_sys_dup(fds[1]); mtcp_sys_close(fds[0]); if (mtcp_sys_dup2(fd, STDIN_FILENO) != STDIN_FILENO) { MTCP_PRINTF("ERROR: dup2() failed! No restoration will be performed!" " Cancel now!\n"); mtcp_abort(); } mtcp_sys_close(fd); mtcp_sys_dup2(fds[1], STDOUT_FILENO); mtcp_sys_close(fds[1]); mtcp_sys_execve(decomp_path, decomp_args, envp); /* should not get here */ MTCP_PRINTF("ERROR: Decompression failed! No restoration will be" " performed! Cancel now!\n"); mtcp_abort(); } } else /* invalid magic number */ { MTCP_PRINTF("ERROR: Invalid magic number in this checkpoint file!\n"); mtcp_abort(); } }