void pipe_int_read(int *c) { int len; #ifdef DEBUGPIPE int code; len = read(fpip_in,&code,sizeof(code)); //fprintf(stderr, "Read pipe debug int from pipe %d\n", code); if (len!=sizeof(code)) switch (len) { case 0: pipe_error("PIPE_INT_READ_EOF"); case -1: pipe_error("PIPE_INT_READ_EAGAIN"); default: pipe_error("PIPE_INT_READ"); } if (code!=INT_CODE) fprintf(stderr,"BUG ALERT ON INT PIPE %i\n",code); #endif len = read(fpip_in,c, sizeof(int)); //fprintf(stderr, "Read pipe int from pipe %d\n", *c); if (len!=sizeof(int)) pipe_error("PIPE_INT_READ"); }
void gtk_pipe_open(void) { int res; res=pipe(pipeAppli); if (res!=0) pipe_error("PIPE_APPLI CREATION"); res=pipe(pipeGtk); if (res!=0) pipe_error("PIPE_GTK CREATION"); if ((pid=fork())==0) { /*child*/ close(pipeGtk[1]); close(pipeAppli[0]); fpip_in=pipeGtk[0]; fpip_out= pipeAppli[1]; Launch_Gtk_Process(fpip_in); /* Won't come back from here */ fprintf(stderr,"WARNING: come back from Gtk+\n"); exit(0); } close(pipeGtk[0]); close(pipeAppli[1]); fpip_in= pipeAppli[0]; fpip_out= pipeGtk[1]; }
void pipe_open() { int res; res=pipe(pipeAppli); if (res!=0) pipe_error("PIPE_APPLI CREATION"); res=pipe(pipeMotif); if (res!=0) pipe_error("PIPE_MOTIF CREATION"); if ((pid=fork())==0) /*child*/ { close(pipeMotif[1]); close(pipeAppli[0]); fpip_in=pipeMotif[0]; fpip_out= pipeAppli[1]; Launch_Motif_Process(fpip_in); /* Won't come back from here */ fprintf(stderr,"WARNING: come back from MOTIF\n"); exit(0); } close(pipeMotif[0]); close(pipeAppli[1]); fpip_in= pipeAppli[0]; fpip_out= pipeMotif[1]; }
pipe_string_read(char *str) { int len, slen; #ifdef DEBUGPIPE int code; len = read(fpip_in,&code,sizeof(code)); //fprintf(stderr, "Reading pipe debug int %d\n", code); if (len!=sizeof(code)) pipe_error("PIPE_STRING_READ"); if (code!=STRING_CODE) fprintf(stderr,"BUG ALERT ON STRING PIPE %i\n",code); #endif len = read(fpip_in,&slen,sizeof(slen)); //fprintf(stderr, "Reading pipe string length %d\n", slen); if (len!=sizeof(slen)) pipe_error("PIPE_STRING_READ"); //fprintf(stderr, "Read num bytes %d\n", len); len = read(fpip_in,str,slen); if (len!=slen) pipe_error("PIPE_STRING_READ on string part"); str[slen]='\0'; /* Append a terminal 0 */ //fprintf(stderr, "Reading pipe str from pipe '%s'\n", str); }
void gtk_pipe_open(void) { int res; GError *error = NULL; res=pipe(pipeAppli); if (res!=0) pipe_error("PIPE_APPLI CREATION"); res=pipe(pipeGtk); if (res!=0) pipe_error("PIPE_GTK CREATION"); if ((pid=fork())==0) { /*child*/ close(pipeGtk[1]); close(pipeAppli[0]); fpip_in=pipeGtk[0]; fpip_out= pipeAppli[1]; #if GTK_MAJOR_VERSION == 2 Launch_Gtk_Process(fpip_in); #else channel_in = g_io_channel_unix_new(fpip_in); channel_out = g_io_channel_unix_new(fpip_out); /* set to raw I/O, unbuffered */ g_io_channel_set_encoding(channel_in, NULL, &error); g_io_channel_set_encoding(channel_out, NULL, &error); g_io_channel_set_buffered(channel_in, FALSE); g_io_channel_set_buffered(channel_out, FALSE); Launch_Gtk_Process(channel_in); #endif /* Won't come back from here */ fprintf(stderr,"WARNING: come back from Gtk+\n"); exit(0); } close(pipeGtk[0]); close(pipeAppli[1]); fpip_in= pipeAppli[0]; fpip_out= pipeGtk[1]; #if GTK_MAJOR_VERSION == 3 /* channel_in = g_io_channel_unix_new(fpip_in); */ //channel_out = g_io_channel_unix_new(fpip_out); //g_io_channel_set_encoding(channel_in, NULL, &error); //g_io_channel_set_encoding(channel_out, NULL, &error); #endif }
void gtk_pipe_int_read(int *c) { #if GTK_MAJOR_VERSION == 2 int len; #ifdef DEBUGPIPE int code; len = read(fpip_in,&code,sizeof(code)); //fprintf(stderr, "Read debug int %d\n", code); if (len!=sizeof(code)) pipe_error("PIPE_INT_READ"); if (code!=INT_CODE) fprintf(stderr,"BUG ALERT ON INT PIPE %i\n",code); #endif len = read(fpip_in,c, sizeof(int)); //fprintf(stderr, "Read int %d\n", *c); if (len!=sizeof(int)) pipe_error("PIPE_INT_READ"); #else // GTK 3 gsize len; GError *error = NULL; GIOStatus status; #ifdef DEBUGPIPE int code; status = g_io_channel_read_chars(channel_in, (gchar *)&code, sizeof(code), &len, &error); if ((len!=sizeof(code)) || (status != G_IO_STATUS_NORMAL)) channel_error("CHANNEL_INT_READ", status, error); //fprintf(stderr, "Read gtk debug int %d\n", code); if (code!=INT_CODE) fprintf(stderr,"BUG ALERT ON INT CHANNEL %i\n",code); #endif status = g_io_channel_read_chars(channel_in, (gchar *) c, sizeof(int), &len, &error); if ((len!=sizeof(int)) || (status != G_IO_STATUS_NORMAL)) channel_error("CHANNEL_INT_READ", status, error); //fprintf(stderr, "Read gtk int %d\n", *c); // if (len!=sizeof(int)) pipe_error("CHANNEL_INT_READ"); #endif }
void pipe_int_write(int c) { int len; int code=INT_CODE; #ifdef DEBUGPIPE len = write(fpip_out,&code,sizeof(code)); if (len!=sizeof(code)) pipe_error("PIPE_INT_WRITE"); #endif len = write(fpip_out,&c,sizeof(c)); if (len!=sizeof(int)) pipe_error("PIPE_INT_WRITE"); }
//--ENTRY POINT-- int main (int argc, char **argv) { if (argc!=2) { //Argument validation. fprintf(stderr, "Invalid argument number. Terminating...\n"); exit(EXIT_FAILURE); } mainrt=getpid(); node_type *root=get_tree_from_file(argv[1]); //Read tree: we suppose non-empty return tree. pid_t _fork; int k[2]; if (pipe(k)) { //Pipe error. pipe_error(mainrt); exit(EXIT_FAILURE); } if ((_fork=fork())==-1) { //Fork error. fprintf(stderr, "Fork error at main. ERRNO: (%d).\n", errno); exit(EXIT_FAILURE); } else if (_fork==0) { //Child: ccret(close(k[0])); fork_proc(root, k[1]); } else { //Main: ccret(close(k[1])); int rwn=0, rwrv=0, result; while (rwn<sizeof(int)) { //Read result. if ((rwrv=read(k[0], &result+rwn, sizeof(int)-rwn))==-1) { read_err(getpid()); exit(EXIT_FAILURE); } rwn+=rwrv; } ccret(close(k[0])); printf("RESULT: %d\n", result); //Print result. } return 0; }
void pipe_int_read(int *c) { int len; #ifdef DEBUGPIPE int code; len = read(fpip_in,&code,sizeof(code)); if (len!=sizeof(code)) pipe_error("PIPE_INT_READ"); if (code!=INT_CODE) fprintf(stderr,"BUG ALERT ON INT PIPE %i\n",code); #endif len = read(fpip_in,c, sizeof(int)); if (len!=sizeof(int)) pipe_error("PIPE_INT_READ"); }
void pipe_string_write(char *str) { int len, slen; #ifdef DEBUGPIPE int code=STRING_CODE; len = write(fpip_out,&code,sizeof(code)); if (len!=sizeof(code)) pipe_error("PIPE_STRING_WRITE"); #endif slen=strlen(str); len = write(fpip_out,&slen,sizeof(slen)); if (len!=sizeof(slen)) pipe_error("PIPE_STRING_WRITE"); len = write(fpip_out,str,slen); if (len!=slen) pipe_error("PIPE_STRING_WRITE on string part"); }
void fork_proc (node_type *cur, int pfd) { pid_t par=getpid(); size_t i=0; pid_t ch[2]; int p[2][2]; int result=0, res[2]={0, 0}; int rwn=0, rwrv=0; if (!cur->nr_children) { //If no children: atoi arg. result=atoi(cur->name); } else if (cur->nr_children==2) { //Child with 2 children: Open pipes, fork children, and wait to read. for (i=0; i<cur->nr_children; i++) { //For the 2 children, open pipes, and fork giving them the write end. if (pipe(p[i])) { //Open pipe. pipe_error(par); shit_n_exit;//exit(EXIT_FAILURE); } ch[i]=fork(); //Fork. if (ch[i]<0) { fork_error(par); shit_n_exit;//exit(EXIT_FAILURE); } else if (ch[i]==0) { ccret(close(p[i][0])); fork_proc(&cur->children[i], p[i][1]); } else { ccret(close(p[i][1])); } } for (i=0; i<cur->nr_children; i++) { //Read from the 2 child streams (and bury them in the process). rwn=rwrv=0; while (rwn<sizeof(int)) { if ((rwrv=read(p[i][0], &res[i]+rwn, sizeof(int)-rwn))==-1) { read_err(par); shit_n_exit;//exit(EXIT_FAILURE); } rwn+=rwrv; } ccret(close(p[i][0])); wait(NULL); } _Bool err=0; result = (cur->name[0]=='+') ? res[0]+res[1] : ((cur->name[0]=='*') ? res[0]*res[1] : ((cur->name[0]=='-') ? res[0]-res[1] : (err=1))); if (err && op_err(par, cur->name[0]) && shit_n_exit) ; } else { //Child with illegal number of children: fprintf(stderr, "Illegal operand number ( %u ) in process: %ld.\n", cur->nr_children, (long)par); shit_n_exit;//exit(EXIT_FAILURE); } rwn=sizeof(int); printf("Writing data (%d) to pipe from process %ld.\n", result, (long)par); while (rwn!=0) { if ((rwrv=write(pfd, &result+sizeof(int)-rwn, (sizeof(int)<<1)-rwn))==-1) { write_err(par); shit_n_exit;//exit(EXIT_FAILURE); } rwn-=rwrv; } ccret(close(pfd)); exit(EXIT_SUCCESS); }
void pipe_int_write(int c) { int len; int code=INT_CODE; #ifdef DEBUGPIPE //fprintf(stderr, "Writing pipe debug int %d\n", code); len = write(fpip_out,&code,sizeof(code)); if (len!=sizeof(code)) pipe_error("PIPE_INT_WRITE"); #endif //fprintf(stderr, "Writing pipe int %d\n", c); len = write(fpip_out,&c,sizeof(c)); if (len!=sizeof(int)) pipe_error("PIPE_INT_WRITE"); }
void pipe_string_read(char *str) { int len, slen; #ifdef DEBUGPIPE int code; len = read(fpip_in,&code,sizeof(code)); if (len!=sizeof(code)) pipe_error("PIPE_STRING_READ"); if (code!=STRING_CODE) fprintf(stderr,"BUG ALERT ON STRING PIPE %i\n",code); #endif len = read(fpip_in,&slen,sizeof(slen)); if (len!=sizeof(slen)) pipe_error("PIPE_STRING_READ"); len = read(fpip_in,str,slen); if (len!=slen) pipe_error("PIPE_STRING_READ on string part"); str[slen]='\0'; /* Append a terminal 0 */ }
void IPCommandSenderBase::DoCall() { WriteCommandId(GetCommandId()); SendRequest(); int ret = ReadResult(); if(ret != 0) { throw pipe_error("Fail to process the request in the server."); } }
void gtk_pipe_int_write(int c) { #if GTK_MAJOR_VERSION == 2 int len; int code=INT_CODE; #ifdef DEBUGPIPE //fprintf(stderr, "Writing debug int %d\n", code); len = write(fpip_out,&code,sizeof(code)); if (len!=sizeof(code)) pipe_error("PIPE_INT_WRITE"); #endif //fprintf(stderr, "Writing int %d\n", c); len = write(fpip_out,&c,sizeof(c)); if (len!=sizeof(int)) pipe_error("PIPE_INT_WRITE"); #else // GTK 3 gsize len; int code=INT_CODE; GError *error = NULL; GIOStatus status; #ifdef DEBUGPIPE //fprintf(stderr, "Writing gtk debug int %d\n", code); status = g_io_channel_write_chars(channel_out, (gchar *)&code, sizeof(code), &len, &error); if ((len!=sizeof(code)) || (status != G_IO_STATUS_NORMAL)) channel_error("CHANNEL_INT_WRITE", status, error); g_io_channel_flush(channel_out, &error); #endif //fprintf(stderr, "Writing gtk int %d\n", c); g_io_channel_write_chars(channel_out, (gchar *)&c, sizeof(c), &len, &error); if (len!=sizeof(int)) pipe_error("CHANNEL_INT_WRITE"); g_io_channel_flush(channel_out, &error); #endif }
void gtk_pipe_string_write(char *str) { #if GTK_MAJOR_VERSION == 2 int len, slen; #ifdef DEBUGPIPE int code=STRING_CODE; //fprintf(stderr, "Writing debug int %d\n", code); len = write(fpip_out,&code,sizeof(code)); if (len!=sizeof(code)) pipe_error("PIPE_STRING_WRITE"); #endif slen=strlen(str); //fprintf(stderr, "Writing int %d\n", slen); len = write(fpip_out,&slen,sizeof(slen)); if (len!=sizeof(slen)) pipe_error("PIPE_STRING_WRITE"); //fprintf(stderr, "Writing str %s\n", str); len = write(fpip_out,str,slen); if (len!=slen) pipe_error("PIPE_STRING_WRITE on string part"); #else // GTK 3 gsize len; int slen; GError *error = NULL; #ifdef DEBUGPIPE int code=STRING_CODE; //fprintf(stderr, "Writing gtk debug int %d\n", code); g_io_channel_write_chars(channel_out,(gchar *)&code, sizeof(code), &len, &error); if (len!=sizeof(code)) pipe_error("CHANNEL_STRING_WRITE"); g_io_channel_flush(channel_out, &error); #endif slen=strlen(str); //fprintf(stderr, "Writing gtk strlen int %d of size %ld\n", slen, sizeof(slen)); g_io_channel_write_chars(channel_out, (gchar *)&slen, sizeof(slen), &len, &error); if (len != sizeof(slen)) pipe_error("CHANNEL_STRING_WRITE"); g_io_channel_flush(channel_out, &error); //fprintf(stderr, "Writing gtk str %s\n", str); g_io_channel_write_chars(channel_out, str, slen, &len, &error); if (len!=slen) pipe_error("CHANNEL_STRING_WRITE on string part"); g_io_channel_flush(channel_out, &error); #endif }
void pipe_string_write(char *str) { int len, slen; #ifdef DEBUGPIPE int code=STRING_CODE; //fprintf(stderr, "Writing pipe debug int %d\n", code); len = write(fpip_out,&code,sizeof(code)); if (len!=sizeof(code)) pipe_error("PIPE_STRING_WRITE"); #endif slen=strlen(str); //fprintf(stderr, "Writing pipe strlen %d\n", slen); len = write(fpip_out,&slen,sizeof(slen)); if (len!=sizeof(slen)) pipe_error("PIPE_STRING_WRITE"); //fprintf(stderr, "Writing pipe str %s\n", str); len = write(fpip_out,str,slen); if (len!=slen) pipe_error("PIPE_STRING_WRITE on string part"); }
void gtk_pipe_string_read(char *str) { #if GTK_MAJOR_VERSION == 2 int len, slen; #ifdef DEBUGPIPE int code; len = read(fpip_in,&code,sizeof(code)); //fprintf(stderr, "Reading debug int %d\n", code); if (len!=sizeof(code)) pipe_error("PIPE_STRING_READ"); if (code!=STRING_CODE) fprintf(stderr,"BUG ALERT ON STRING PIPE %i\n",code); #endif len = read(fpip_in,&slen,sizeof(slen)); //fprintf(stderr, "Reading int %d\n", slen); if (len!=sizeof(slen)) pipe_error("PIPE_STRING_READ"); //fprintf(stderr, "Reading int %d\n", len); len = read(fpip_in,str,slen); if (len!=slen) pipe_error("PIPE_STRING_READ on string part"); str[slen]='\0'; /* Append a terminal 0 */ //fprintf(stderr, "Reading str %s\n", str); #else // GTK 3 gsize len; int slen; GError *error = NULL; #ifdef DEBUGPIPE int code; g_io_channel_read_chars(channel_in, (gchar *)&code, sizeof(code), &len, &error); //fprintf(stderr, "Reading gtk debug string int %d size %ld\n", code, sizeof(code)); if (len!=sizeof(code)) pipe_error("CHANNEL_STRING_READ"); if (code!=STRING_CODE) fprintf(stderr,"BUG ALERT ON STRING CHANNEL %i\n",code); #endif g_io_channel_read_chars(channel_in, (gchar *)&slen, sizeof(slen), &len, &error); //fprintf(stderr, "Reading gtk strlen int %d\n", (int)slen); if (len!=sizeof(slen)) pipe_error("CHANNEL_STRING_READ"); g_io_channel_read_chars(channel_in, str, slen, &len, &error); if (len!=slen) pipe_error("CHANNEL_STRING_READ on string part"); str[slen]='\0'; /* Append a terminal 0 */ //fprintf(stderr, "Reading gtk str %s\n", str); #endif }
void pipes_loop(t_pipe pi, t_env *e, long tot) { int p[2]; pid_t pid; char *cat[3]; if (*(pi.table + tot + pi.redir) && **(pi.table + tot + pi.redir) == '|') { if (pipe(p) == -1) return (pipe_error("pipe() failed.", -1, e)); if ((pid = fork()) < 0) return (pipe_error("fork() failed.", -1, e)); if (pid == 0) { if (pi.pipe) { dup2(pi.pipe, STDIN_FILENO); close(pi.pipe); pi.pipe = 0; } close(p[0]); dup2(p[1], STDOUT_FILENO); close(p[1]); pipe_exec(*(pi.cmd + tot), e, 0); } else { close(p[1]); waitpid(pid, &e->ret, 0); if (e->ret) return (pipe_error(NULL, p[0], e)); tot += pi.redir + 1; pi.redir = 0; if (*(pi.table + tot + pi.redir)) { pi.pipe = p[0]; pipes_loop(pi, e, tot); close(p[0]); } else { dup2(p[0], STDIN_FILENO); close(p[0]); pipe_exec(*(pi.cmd + tot + pi.redir), e, 1); dup2(1, STDIN_FILENO); } tot += pi.redir + 1; pi.redir = 0; } } else if (*(pi.table + tot + pi.redir) && **(pi.table + tot + pi.redir) == '<') { if (pipe(p) == -1) return (pipe_error("pipe() failed.", -1, e)); if ((pid = fork()) < 0) return (pipe_error("fork() failed.", -1, e)); if (pid == 0) { if (pi.pipe) { dup2(pi.pipe, STDIN_FILENO); close(pi.pipe); pi.pipe = 0; } close(p[0]); if (**(pi.cmd + tot + pi.redir)) dup2(p[1], STDOUT_FILENO); close(p[1]); int i = 0; while (*(pi.table + tot + i) && **(pi.table + tot + i) == '<') i++; char *more[i + 1]; int j = 0; more[j] = "cat"; while (*(pi.table + tot + j) && **(pi.table + tot + j) == '<') { more[j + 1] = **(pi.cmd + tot + j + 1); j++; } more[j + 1] = NULL; pipe_exec(more, e, 0); } else { close(p[1]); waitpid(pid, &e->ret, 0); if (e->ret) return (pipe_error(NULL, p[0], e)); while (*(pi.table + tot + pi.redir) && **(pi.table + tot + pi.redir) == '<') pi.redir++; if (*(pi.table + tot + pi.redir)) { pi.pipe = p[0]; pipes_loop(pi, e, tot); close(p[0]); } else { dup2(p[0], STDIN_FILENO); close(p[0]); if (**(pi.cmd + tot)) pipe_exec(*(pi.cmd + tot), e, 1); dup2(1, STDIN_FILENO); } } } else if (*(pi.table + tot + pi.redir) && **(pi.table + tot + pi.redir) == '>') { if ((pid = fork()) < 0) return (pipe_error("fork() failed.", -1, e)); if (pid == 0) { if (pi.pipe) { dup2(pi.pipe, STDIN_FILENO); close(pi.pipe); pi.pipe = 0; } pi.redir++; dup2(*(pi.fds + tot + pi.redir), STDOUT_FILENO); close(*(pi.fds + tot + pi.redir)); if (tot + pi.redir - 2 >= 0 && **(pi.table + tot + pi.redir - 2) == '>') { cat[0] = "cat"; cat[1] = **(pi.cmd + tot + pi.redir - 1); pipe_exec(cat, e, 0); } else if (**(pi.cmd + tot)) pipe_exec(*(pi.cmd + tot), e, 0); else { cat[0] = "cat"; pipe_exec(cat, e, 0); } } else { waitpid(pid, &e->ret, 0); if (e->ret) return (pipe_error(NULL, p[0], e)); pi.redir++; if (*(pi.table + tot + pi.redir)) { pi.pipe = *(pi.fds + tot + pi.redir); pipes_loop(pi, e, tot); } } } }
void channel_error(char *str, GIOStatus status, GError *error) { if (error != NULL) fprintf(stderr, "GIO channel error: %s\n", error->message); status_error(status); pipe_error(str); }