void check_pipe(t_env *env) { close_pipe(&env->fd.pipe, env->fd.pipefd); close_pipe(&env->fd.pipe2, env->fd.pipefd2); close_pipe(&env->fd.pipe, env->fd.pipefd); close_pipe(&env->fd.pipe2, env->fd.pipefd2); }
void make_loop(int n, int* last_pipe, char* grammar) { /* last_pipe[0] = input of the recently created pipe * * last_pipe[1] = output of the first pipe */ pid_t pid; if (n == 1) { prepare_input(last_pipe); prepare_output(last_pipe); close_pipe(last_pipe); return; } int next_pipe[2]; create_pipe(next_pipe); switch (pid = fork()) { case -1: syserr("Error in fork()\n"); case 0: prepare_input(last_pipe); close_pipe(last_pipe); prepare_output(next_pipe); close_pipe(next_pipe); execl("./worker", "./worker", grammar, NULL); syserr("Error in execl()\n"); default: last_pipe[0] = next_pipe[0]; make_loop(n - 1, last_pipe, grammar); return; } }
static int spawn_worker(char *cmd) { /* params struct for both workers */ struct worker_params params; /* children's pids */ pid_t c1, c2; int status, ret = 0; trim(cmd); params.cmd = cmd; if(open_pipe(params.pipe) == -1) { (void) fprintf(stderr, "%s: Could not create pipe\n", pgname); return -1; } /* We flush all our standard fd's so we'll have them empty in the workers */ fflush(stdin); fflush(stdout); fflush(stderr); /* Fork execute worker */ if((c1 = fork_function(execute, ¶ms)) == -1) { (void) fprintf(stderr, "%s: Could not spawn execute worker\n", pgname); close_pipe(params.pipe, channel_all); return -1; } /* Fork format worker */ if((c2 = fork_function(format, ¶ms)) == -1) { (void) fprintf(stderr, "%s: Could not spawn format worker\n", pgname); /* Wait for child 1 */ if(wait_for_child(c1) == -1) { (void) fprintf(stderr, "%s: Error waiting for execute worker to finish\n", pgname); } close_pipe(params.pipe, channel_all); return -1; } /* We need to close the pipe in parent, so that the format worker will quit working when execute's output has finished */ close_pipe(params.pipe, channel_all); if((status = wait_for_child(c1)) != 0) { (void) fprintf(stderr, "%s: Execute worker returned %d\n", pgname, status); /* not neccessarily an error. If there was a typo in cmd don't quit the whole programm */ // ret = -1; } if((status = wait_for_child(c2)) != 0) { (void) fprintf(stderr, "%s: Format worker returned %d\n", pgname, status); // ret = -1; } return ret; }
static void prepare_process_launcher_pipes_for_manager (gint *command_pipe, gint *reply_pipe, GIOChannel **read_channel, GIOChannel **write_channel) { close_pipe(command_pipe, MILTER_UTILS_READ_PIPE); close_pipe(reply_pipe, MILTER_UTILS_WRITE_PIPE); *write_channel = create_write_io_channel(command_pipe[MILTER_UTILS_WRITE_PIPE]); *read_channel = create_read_io_channel(reply_pipe[MILTER_UTILS_READ_PIPE]); }
static FILE *plugin_open(const char *path, const char *mode){ int sfd; int ret; struct addrinfo hints,*rp,*result; char *url,*port,*filename; if(open_pipe()) return NULL; if(parse_url(path,&url,&port,&filename)){ return NULL; } memset(&hints,0,sizeof(struct addrinfo)); hints.ai_family=AF_INET; hints.ai_socktype=SOCK_STREAM; hints.ai_protocol=0; if((ret=getaddrinfo(url,port,&hints,&result))){ fprintf(stderr,"error (%s) - getaddrinfo: %s\n",path,gai_strerror(ret)); close_pipe(); free(port); return NULL; } free(url); free(port); for(rp=result;rp;rp=rp->ai_next){ if((sfd=socket(rp->ai_family,rp->ai_socktype,rp->ai_protocol))==-1) continue; if(connect(sfd,rp->ai_addr,rp->ai_addrlen)!=-1){ h.sfd=sfd; h.ffd=fdopen(sfd,mode); break; } close(sfd); } if(!rp){ fprintf(stderr,"Cannot connect to: %s\n",path); close_pipe(); return NULL; } freeaddrinfo(result); h.print_meta=1; if(stream_hello(filename)){ plugin_close(NULL); return NULL; } free(filename); return h.rfd; }
//turns process into zombie. if it has children, all its children go to p1 int kexit(int exitValue) { int i; PROC *p; for (i = 0; i < NFD; i++) { if(running->fd[i] != 0) close_pipe(i); } //send children (dead or alive) to P1's orphanage for (i = 1; i < NPROC; i++) { p = &proc[i]; if(p->status != FREE && p->ppid == running->pid) { p->ppid = 1; p->parent = &proc[1]; } } //restore name string strcpy(running->name, pname[running->pid]); //record exitValue and become a ZOMBIE running->exitCode = exitValue; running->status = ZOMBIE; //wakeup parent and P1 kwakeup(running->parent); kwakeup(&proc[1]); tswitch(); }
static bool create_namedpipe(impl_type& impl,const String& name_) { if(impl.serv) close_pipe(impl); String name=make_pipename(name_); int fd; struct sockaddr_un un; if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { return false; } impl.serv.reset(fd); unlink(name.c_str()); socklen_t nlen=unix_sockaddr(un,name); if (bind(fd, (struct sockaddr *)&un, nlen) < 0) { return false; } if (listen(fd, 10) < 0) { return false; } impl.name=name_; return true; }
void readfr_pipe(QSP_ARG_DECL Pipe *pp,const char* varname) { char buf[LLEN]; if( (pp->p_flgs & READ_PIPE) == 0 ){ sprintf(ERROR_STRING,"Can't read from write pipe %s",pp->p_name); WARN(ERROR_STRING); return; } if( fgets(buf,LLEN,pp->p_fp) == NULL ){ if( verbose ){ sprintf(ERROR_STRING, "read failed on pipe \"%s\"",pp->p_name); advise(ERROR_STRING); } close_pipe(QSP_ARG pp); ASSIGN_VAR(varname,"pipe_read_error"); } else { /* remove trailing newline */ if( buf[strlen(buf)-1] == '\n' ) buf[strlen(buf)-1] = 0; ASSIGN_VAR(varname,buf); } }
void creat_pipe(QSP_ARG_DECL const char *name, const char* command, const char* rw) { Pipe *pp; int flg; if( *rw == 'r' ) flg=READ_PIPE; else if( *rw == 'w' ) flg=WRITE_PIPE; else { sprintf(ERROR_STRING,"create_pipe: bad r/w string \"%s\"",rw); WARN(ERROR_STRING); return; } pp = new_pipe(QSP_ARG name); if( pp == NO_PIPE ) return; pp->p_cmd = savestr(command); pp->p_flgs = flg; pp->p_fp = popen(command,rw); if( pp->p_fp == NULL ){ sprintf(ERROR_STRING, "unable to execute command \"%s\"",command); WARN(ERROR_STRING); close_pipe(QSP_ARG pp); } }
void signal_handler(int signal_type) { int i; if (signal_type == SIGPIPE) return; if (signal_type == SIGSEGV) { printf("*** WARNING *** - Got segmentation fault (SIGSEGV)\n"); printf("\nPlease mail a bug report to [email protected] or\n" "[email protected] containing the situation\n" "in which this segfault occurred, gdb-output (+backtrace)\n" "and other possibly noteworthy information\n"); } else if (signal_type == SIGTERM) { printf("*** WARNING *** - Got termination signal (SIGTERM)\n"); } else if (signal_type == SIGINT) { printf("*** WARNING *** - Got interrupt signal (SIGINT)\n"); } printf("Trying to shut down lav-applications (if running): "); for (i=0;i<NUM;i++) close_pipe(i); printf("succeeded\n"); signal(signal_type, SIG_DFL); }
void uv_pipe_endgame(uv_pipe_t* handle) { uv_err_t err; int status; if (handle->flags & UV_HANDLE_SHUTTING && !(handle->flags & UV_HANDLE_SHUT) && handle->write_reqs_pending == 0) { close_pipe(handle, &status, &err); if (handle->shutdown_req->cb) { if (status == -1) { LOOP->last_error = err; } handle->shutdown_req->cb(handle->shutdown_req, status); } handle->reqs_pending--; } if (handle->flags & UV_HANDLE_CLOSING && handle->reqs_pending == 0) { assert(!(handle->flags & UV_HANDLE_CLOSED)); handle->flags |= UV_HANDLE_CLOSED; if (handle->close_cb) { handle->close_cb((uv_handle_t*)handle); } uv_unref(); } }
static void run_child(size_t cpu) { struct child * self = &children[cpu]; self->pid = getpid(); self->sigusr1 = 0; self->sigusr2 = 0; self->sigterm = 0; inner_child = self; if (atexit(close_pipe)){ close_pipe(); exit(EXIT_FAILURE); } umask(0); /* Change directory to allow directory to be removed */ if (chdir("/") < 0) { perror("Unable to chdir to \"/\""); exit(EXIT_FAILURE); } setup_signals(); set_affinity(cpu); create_context(self); write_pmu(self); load_context(self); notify_parent(self, cpu); /* Redirect standard files to /dev/null */ freopen( "/dev/null", "r", stdin); freopen( "/dev/null", "w", stdout); freopen( "/dev/null", "w", stderr); for (;;) { sigset_t sigmask; sigfillset(&sigmask); sigdelset(&sigmask, SIGUSR1); sigdelset(&sigmask, SIGUSR2); sigdelset(&sigmask, SIGTERM); if (self->sigusr1) { perfmon_start_child(self->ctx_fd); self->sigusr1 = 0; } if (self->sigusr2) { perfmon_stop_child(self->ctx_fd); self->sigusr2 = 0; } sigsuspend(&sigmask); } }
static int lclose(lua_State *L) { HANDLE pipe = lua_touserdata(L, 1); if (pipe == NULL) return luaL_error(L, "invalid pipe"); close_pipe(pipe); return 0; }
void stop_scene_detection_process(GtkWidget *widget, gpointer data) { close_pipe(LAV2YUV_S); scene_detection_button_label = NULL; scene_detection_status_label = NULL; scene_detection_bar = NULL; gtk_widget_destroy(scene_detection_window); }
static COMMAND_FUNC( do_closepipe ) { Pipe *pp; pp = pick_pipe(""); if( pp == NULL ) return; close_pipe(QSP_ARG pp); }
int chain(char *path, char *args[], FILE *filters, unsigned int elements) { Pipe pipes[2]; Pipe *in, *out, *tmp; unsigned int child; in = &pipes[0]; out = &pipes[1]; for (child = 0; child < NPROCS; child++) { if (child < NPROCS-1) { if (mk_pipe(out) != 0) { perror("begin"); return -1; } } switch(fork()) { case -1: return -1; case 0: if (child > 0) { dup2(in->reader, STDIN_FILENO); close(in->reader); close(in->writer); } if (child < NPROCS-1) { dup2(out->writer, STDOUT_FILENO); close(out->writer); close(out->reader); } if (execv(path, args) == -1) { perror("begin"); exit(1); }; default: if (child > 0) close_pipe(in); } /* patch up arguments */ if (child == 0 || child >= elements) { free(path); path = get_name(filters); if (path == NULL) { fprintf(stderr, "begin: Unexpected end of filter list\n"); return -1; } args[0] = path; } tmp = in; in = out; out = tmp; } return 0; }
static void connection_free(Connection *c) { assert(c); if (c->context) set_remove(c->context->connections, c); sd_event_source_unref(c->server_event_source); sd_event_source_unref(c->client_event_source); if (c->server_fd >= 0) close_nointr_nofail(c->server_fd); if (c->client_fd >= 0) close_nointr_nofail(c->client_fd); close_pipe(c->server_to_client_buffer); close_pipe(c->client_to_server_buffer); free(c); }
/* creates an array containing all the PIDs of the child processes */ void spawn_sorts(int **in_pipe, int **out_pipe) { //Spawn all the sort processes process_array = malloc(sizeof(pid_t) * num_sorts); pid_t pid; int i; for (i = 0; i < num_sorts; i++){ create_pipe(in_pipe[i]); create_pipe(out_pipe[i]); switch(pid = fork()){ case -1: //Oops case puke_and_exit("Forking error\n"); case 0: //Child case close(STDIN_FILENO); close(STDOUT_FILENO); if (in_pipe[i][0] != STDIN_FILENO) { //Defensive check if (dup2(in_pipe[i][0], STDIN_FILENO) == -1) puke_and_exit("dup2 0"); } /* Bind stdout to out_pipe*/ close_pipe(out_pipe[i][0]); //close read end of output pipe if (out_pipe[i][1] != STDOUT_FILENO) { //Defensive check if (dup2(out_pipe[i][1], STDOUT_FILENO) == -1) puke_and_exit("dup2 1"); } /* Pipes from previously-spawned children are still open in this child Close them and close the duplicate pipes just created by dup2 */ close_pipes_array(in_pipe, i+1); close_pipes_array(out_pipe, i+1); execlp("sort", "sort", (char *)NULL); default: //Parent case process_array[i] = pid; close_pipe(in_pipe[i][0]); close_pipe(out_pipe[i][1]); break; } } }
static int run_shell_cmd(menu_t* menu, char* cmd) { #ifndef __MINGW32__ int in[2],out[2],err[2]; mp_msg(MSGT_GLOBAL,MSGL_INFO,MSGTR_LIBMENU_ConsoleRun,cmd); if(mpriv->child) { mp_msg(MSGT_GLOBAL,MSGL_ERR,MSGTR_LIBMENU_AChildIsAlreadyRunning); return 0; } pipe(in); pipe(out); pipe(err); mpriv->child = fork(); if(mpriv->child < 0) { mp_msg(MSGT_GLOBAL,MSGL_ERR,MSGTR_LIBMENU_ForkFailed); close_pipe(in); close_pipe(out); close_pipe(err); return 0; } if(!mpriv->child) { // Chlid process int err_fd = dup(2); FILE* errf = fdopen(err_fd,"w"); // Bind the std fd to our pipes dup2(in[0],0); dup2(out[1],1); dup2(err[1],2); execl("/bin/sh","sh","-c",cmd,(void*)NULL); fprintf(errf,"exec failed : %s\n",strerror(errno)); exit(1); } // MPlayer mpriv->child_fd[0] = in[1]; mpriv->child_fd[1] = out[0]; mpriv->child_fd[2] = err[0]; mpriv->prompt = mpriv->child_prompt; //add_line(mpriv,"Child process started"); #endif return 1; }
main(int argc, char *argv[]) { int pid, cmd, i; char name[64]; ucolor = 0; clearScreen(UMODE); printf("enter main() : argc = %d\n", argc); for (i=0; i<argc; i++) printf("argv[%d] = %s\n", i, argv[i]); while(1){ pid = getpid(); color = ucolor ? ucolor:0x000B + (pid % 5);// avoid black on black baground cmd?resetCursor(UMODE):1; printf("----------------------------------------------\n"); printf("I am proc %din U mode: segment=%x\n", pid, (pid+1)*0x1000); show_menu(); printf("Command ? "); getCurPos(UMODE); printf(" \n"); setCurPos(ux_col,uy_row,UMODE); gets(name); clearScreenRegion(K_X_PRINT_OFFSET,80,U_Y_PRINT_OFFSET-1,24,UMODE); if (name[0]==0) continue; cmd = find_cmd(name); getCurPos(UMODE); setCurPos(K_X_PRINT_OFFSET,U_Y_PRINT_OFFSET,UMODE); switch(cmd){ case 0 : do_getpid(); break; case 1 : ps(); break; case 2 : chname(); break; case 3 : kmode(); break; case 4 : kswitch(); break; case 5 : wait(); break; case 6 : exit(); break; case 7 : fork(); break; case 8 : exec(); break; case 9 : ucolor = chcolor(); break; case 10: pipe(); break; case 11: pfd(); break; case 12: read_pipe(); break; case 13: write_pipe(); break; case 14: close_pipe(); break; default: invalid(name); break; } setCurPos(ux_col,uy_row,UMODE); } }
void sendto_pipe(QSP_ARG_DECL Pipe *pp,const char* text) { if( (pp->p_flgs & WRITE_PIPE) == 0 ){ sprintf(ERROR_STRING,"Can't write to read pipe %s",pp->p_name); WARN(ERROR_STRING); return; } if( fprintf(pp->p_fp,"%s\n",text) == EOF ){ sprintf(ERROR_STRING, "write failed on pipe \"%s\"",pp->p_name); WARN(ERROR_STRING); close_pipe(QSP_ARG pp); } else if( fflush(pp->p_fp) == EOF ){ sprintf(ERROR_STRING, "fflush failed on pipe \"%s\"",pp->p_name); WARN(ERROR_STRING); close_pipe(QSP_ARG pp); } #ifdef DEBUG else if( debug ) advise("pipe flushed"); #endif /* DEBUG */ }
void textclient_reshape (text_data *d, int pix_w, int pix_h, int char_w, int char_h, int max_lines) { # if defined(HAVE_FORKPTY) && defined(TIOCSWINSZ) d->pix_w = pix_w; d->pix_h = pix_h; d->char_w = char_w; d->char_h = char_h; d->max_lines = max_lines; # ifdef DEBUG fprintf (stderr, "%s: textclient: reshape: %dx%d, %dx%d\n", progname, pix_w, pix_h, char_w, char_h); # endif if (d->pid && d->pipe) { /* Tell the sub-process that the screen size has changed. */ struct winsize ws; ws.ws_col = char_w; ws.ws_row = char_h; ws.ws_xpixel = pix_w; ws.ws_ypixel = pix_h; ioctl (fileno (d->pipe), TIOCSWINSZ, &ws); kill (d->pid, SIGWINCH); # ifdef DEBUG fprintf (stderr, "%s: textclient: SIGWINCH\n", progname); # endif } # endif /* HAVE_FORKPTY && TIOCSWINSZ */ /* If we're running xscreensaver-text, then kill and restart it any time the window is resized so that it gets an updated --cols arg right away. But if we're running something else, leave it alone. */ if (!strcmp (d->program, "xscreensaver-text")) { # ifdef DEBUG fprintf (stderr, "%s: textclient: reshape relaunch\n", progname); # endif close_pipe (d); d->input_available_p = False; start_timer (d); } }
void destroy( GtkWidget *widget, gpointer data ) { int i; if (verbose) g_print ("Quitting...\n"); /* this closes all apps if opened */ for (i=0;i<NUM;i++) { close_pipe(i); } gtk_main_quit(); }
/** * Helper function to close all ends of the specified pipes. */ void close_all_pipes(const int *fd_descriptor_env_sort, const int *fd_descriptor_sort_pager, const int *fd_descriptor_grep_sort) { close_pipe(fd_descriptor_env_sort[PIPE_WRITE]); close_pipe(fd_descriptor_env_sort[PIPE_READ]); close_pipe(fd_descriptor_sort_pager[PIPE_WRITE]); close_pipe(fd_descriptor_sort_pager[PIPE_READ]); close_pipe(fd_descriptor_grep_sort[PIPE_WRITE]); close_pipe(fd_descriptor_grep_sort[PIPE_READ]); }
void textclient_close (text_data *d) { # ifdef DEBUG fprintf (stderr, "%s: textclient: free\n", progname); # endif close_pipe (d); if (d->program) free (d->program); if (d->pipe_timer) XtRemoveTimeOut (d->pipe_timer); d->pipe_timer = 0; memset (d, 0, sizeof (*d)); free (d); }
static int del_stream(stream_player_t *sp, int s) { tcvp_player_t *sh = sp->shared; int ss = sp->smap[s]; struct sp_stream *str = sp->streams + s; tc2_print("STREAM", TC2_PRINT_DEBUG, "deleting stream %i\n", s); if(ss == sh->vs) sh->vs = -1; else if(ss == sh->as) sh->as = -1; pthread_mutex_lock(&sp->lock); close_pipe(str->pipe); str->pipe = NULL; str->end = NULL; if(str->packets){ tclist_destroy(str->packets, tcfree); str->packets = NULL; } sp->ms->used_streams[s] = 0; sp->nbuf &= ~(1ULL << s); if(sp->fail == sp->ms->n_streams){ tcvp_event_send(sh->sq, TCVP_STATE, TCVP_STATE_ERROR); } if(str->sp){ if(!--sp->pstreams){ pthread_mutex_lock(&sh->lock); if(!--sh->nstreams) tcvp_event_send(sh->sq, TCVP_STATE, TCVP_STATE_END); pthread_mutex_unlock(&sh->lock); sp->state = STOP; } } pthread_cond_broadcast(&sp->cond); pthread_mutex_unlock(&sp->lock); return 0; }
int kcinth() { u16 segment, offset; int a,b,c,d, r; segment = running->uss; offset = running->usp; a = get_word(segment, offset + 2*PA); b = get_word(segment, offset + 2*PB); c = get_word(segment, offset + 2*PC); d = get_word(segment, offset + 2*PD); /*printf("interupthandler a = %d\n", a); printf("b = %d\n", b); printf("string: %s\n", c); printf("n = %d\n", d);*/ switch(a){ case 0 : r = running->pid; break; case 1 : r = do_ps(); break; case 2 : r = kchname(b); break; case 3 : r = kmode(); break; case 4 : r = tswitch(); break; case 5 : r = do_wait(b); break; case 6 : r = do_exit(b); break; case 7 : r = fork(); break; case 8 : r = exec(b); break; /****** these are YOUR pipe functions ************/ case 30 : r = kpipe(b); break; case 31 : r = read_pipe(b,c,d); break; case 32 : r = write_pipe(b,c,d); break; case 33 : r = close_pipe(b); break; case 34 : r = pfd(); break; /**************** end of pipe functions ***********/ case 90: r = getc(); break; case 91: color=running->pid+11; r = putc(b); break; case 99: do_exit(b); break; default: printf("invalid syscall # : %d\n", a); } //printf("interupthandler r = %d\n", r); //getc(); put_word(r, segment, offset + 2*AX); }
static unsigned int execute(fork_func_param_t param) { /* Cast argument */ struct worker_params *params = (struct worker_params *) param; /* We dont need the read end of the pipe */ close_pipe(params->pipe, channel_read); /* Redirect stdout to write end */ if(redirect(params->pipe, stdout, channel_write) == -1) { return 1; } /* We use sh here, to circumvent parsing the command string */ (void) execlp("/bin/sh", "sh", "-c", params->cmd, (char *) NULL); // Should not be reached; return 1; }
int kcinth() { u16 segment, offset; int a,b,c,d, r; segment = running->uss; offset = running->usp; a = get_word(segment, offset + 2*PA); b = get_word(segment, offset + 2*PB); c = get_word(segment, offset + 2*PC); d = get_word(segment, offset + 2*PD); switch(a){ case 0 : r = running->pid; break; case 1 : r = do_ps(); break; case 2 : r = chname(b); break; case 3 : r = kmode(); break; case 4 : r = tswitch(); break; case 5 : r = do_wait(b); break; case 6 : r = do_exit(b); break; case 7 : r = fork(); break; case 8 : r = exec(b); break; case 9 : r = vfork(); break; case 30 : r = kpipe(b); break; case 31 : r = read_pipe(b,c,d); break; case 32 : r = write_pipe(b,c,d); break; case 33 : r = close_pipe(b); break; case 34 : r = pfd(); break; case 90: r = getc(); break; case 91: color=running->pid+11; r = putc(b); break; case 99: do_exit(b); break; default: printf("invalid syscall # : %d\n", a); } put_word(r, segment, offset + 2*AX); }
main() { char name[64]; int pid, cmd; while(1){ pid = getpid(); color = 0x000B + (pid % 6); printf("----------------------------------------------\n"); printf("I am proc %d in U mode: running segment=%x\n",getpid(), getcs()); show_menu(); printf("Command ? "); gets(name); if (name[0]==0) continue; cmd = find_cmd(name); printf("find_cmd = %d\n", cmd); /*char *cmd[]={"getpid", "ps", "chname", "kmode", "switch", "wait", "exit", "fork", "exec", "pipe", "pfd", "read", "write", "close", 0};*/ switch(cmd){ case 0 : getpid(); break; case 1 : ps(); break; case 2 : chname(); break; case 3 : kmode(); break; case 4 : kswitch(); break; case 5 : wait(); break; case 6 : exit(); break; case 7 : fork(); break; case 8 : exec(); break; case 9 : pipe(); break; case 10: pfd(); break; case 11 : read_pipe(); break; case 12 : write_pipe();break; case 13: close_pipe(); break; default: invalid(name); break; } } }