void reset_fd(void) { get_stdin(0); get_stdout(1); get_stderr(2); if (get_pipes(0)) while (get_pipes(-1)); }
tz_return_t TZEDispatchImpProtobuf(const tze_pb_proto_t protos[], const tze_pb_imp_t imps[], uint32_t num_svcs, uint32_t uiCommand, struct tzi_encode_buffer_t *psInBuf, struct tzi_encode_buffer_t *psOutBuf, tz_return_t *puiRv) { ProtobufCMessage *req=NULL, *res=NULL; tz_return_t tzerr=0; if (protos[uiCommand].req_descriptor) { tzerr = TZIDecodeProtobuf(psInBuf, protos[uiCommand].req_descriptor, NULL, &req); if(tzerr) { goto out; } } tzerr = TZEDispatchImpProtobufMsgs(protos, imps, num_svcs, uiCommand, req, &res, puiRv); { /* XXX Temp. this should be an independent layer */ size_t len; void *buf; buf = get_stderr(&len); TZIEncodeArray(psOutBuf, buf, len); free(buf); } if(tzerr || *puiRv) { assert(!res); goto free_unpacked_req; } if (protos[uiCommand].res_descriptor) { tzerr = TZIEncodeProtobuf(psOutBuf, res); if(tzerr) { goto free_res; } } free_res: if(imps[uiCommand].release_res) { imps[uiCommand].release_res(res); } free(res); free_unpacked_req: if(req) { protobuf_c_message_free_unpacked(req, NULL); } out: return tzerr; }
void tee_out(int c) //@ requires tee_out_io(?t1, c, ?t2) &*& token(t1); //@ ensures token(t2); { //@ open tee_out_io(_, _, _); //@ split(); putchar(c); putc(c, get_stderr()); //@ join(); }
static void append_stderr(tzi_encode_buffer_t *psOutBuf) { size_t len; void *buf; buf = get_stderr(&len); if(NULL != buf) { /* If len is too long, too bad. */ TZIEncodeArray(psOutBuf, buf, len); } free(buf); }
void print_env_usage(t_node_cmd *node) { int err_fd; err_fd = get_stderr(node); ft_putstr_fd("usage : env [--help] [-i] [-P path] [-u VAR]", err_fd); ft_putstr_fd(" [name=value ...] [command]\n", err_fd); ft_putendl_fd("--help: Print this message", err_fd); ft_putendl_fd("If no command is provided, the environnement is printed", err_fd); ft_putendl_fd("-i: Empty the environnement", err_fd); ft_putendl_fd("-P: Change the path for the command to launch", err_fd); ft_putendl_fd("-u: Unset the variable VAR", err_fd); ft_putendl_fd("name=value: Set the variable NAME to VALUE", err_fd); }
bool WhmShellProcess::run(WhmShellContext *sc) { WhmShellCommand *c = command; bool result = true; Token_t token = NULL; #ifndef HTTP_PROXY if (runAsUser) { //创建运行身份令牌 if (sc->vh==NULL) { sc->last_error = 128; return false; } token = sc->vh->createToken(result); if(!result){ sc->last_error = 129; return false; } } #endif //the commands stdout pipe PIPE_T big_stdout_pipe[2]; if (!KPipeStream::create(big_stdout_pipe)) { if (token) { KVirtualHost::closeToken(token); } return false; } //the commands stdin pipe PIPE_T big_stdin_pipe[2]; bool big_stdin_pipe_created = false; //init the std file PIPE_T hstdin = get_stdin(sc); if (hstdin==INVALIDE_PIPE && sc->in_buffer.getLen()>0) { //如果没有输入文件并且输入有数据。就要创建输入管道 if (!KPipeStream::create(big_stdin_pipe)) { //关闭打开的big_stdout_pipe ClosePipe(big_stdout_pipe[0]); ClosePipe(big_stdout_pipe[1]); if (token) { KVirtualHost::closeToken(token); } return false; } big_stdin_pipe_created = true; hstdin = big_stdin_pipe[READ_PIPE]; } PIPE_T hstdout = get_stdout(sc); PIPE_T hstderr = get_stderr(sc); PIPE_T in,out,err; err = hstderr; if (err==INVALIDE_PIPE) { err = big_stdout_pipe[WRITE_PIPE]; } PIPE_T pp[2]; for(int i=0;i<2;i++){ pp[i] = INVALIDE_PIPE; } while (c) { char **arg = c->makeArgs(sc); KCmdEnv *env = c->makeEnv(sc); pid_t pid; kfinit(pid); //set in if (c==command) { in = hstdin; } else { assert(pp[READ_PIPE]!=INVALIDE_PIPE); in = pp[READ_PIPE]; pp[READ_PIPE] = INVALIDE_PIPE; assert(pp[WRITE_PIPE]!=INVALIDE_PIPE); //close the out pipe ClosePipe(pp[WRITE_PIPE]); pp[WRITE_PIPE] = INVALIDE_PIPE; } //set out if (c->next) { //create a new pipe if (!KPipeStream::create(pp)) { if (c!=command) { ClosePipe(in); } break; } out = pp[WRITE_PIPE]; } else { //if the command is last. //then set the out to big_pipe out = hstdout; if (out==INVALIDE_PIPE) { out = big_stdout_pipe[WRITE_PIPE]; } } char *curdir2 = NULL; if (curdir) { curdir2 = sc->parseString(curdir); } result = createProcess(token,arg,env,curdir2,in,out,err,pid); if (curdir2) { free(curdir2); } if (c!=command) { //close the in pipe ClosePipe(in); } //free args for (int i=0;;i++) { if(arg[i]==NULL){ break; } free(arg[i]); } delete[] arg; if (env) { delete env; } sc->setLastPid(pid); if (!result) { sc->last_error = 127; break; } c = c->next; } //关闭输入,输出父进程无用的管道端 if (kflike(hstdin)) { ClosePipe(hstdin); } ClosePipe(big_stdout_pipe[WRITE_PIPE]); //处理输入 if (big_stdin_pipe_created) { if (result) { //创建成功才写入数据 buff *buf = sc->in_buffer.getHead(); while (buf && buf->data) { if (write_pipe(big_stdin_pipe[WRITE_PIPE],buf->data,buf->used)!=buf->used) { break; } buf = buf->next; } } //清理输入数据和管道资源 sc->in_buffer.destroy(); ClosePipe(big_stdin_pipe[WRITE_PIPE]); } //处理输出 if (result && (hstdout==INVALIDE_PIPE || hstderr==INVALIDE_PIPE)) { for (;;) { char buf[512]; int len = read_pipe(big_stdout_pipe[READ_PIPE],buf,512); if (len<=0) { break; } sc->lock.Lock(); if (sc->out_buffer.getLen() > 1048576) { //the out msg is too large.drop it. sc->lock.Unlock(); fwrite(buf,1,len,stdout); } else { sc->out_buffer.write_all(buf,len); sc->lock.Unlock(); } //fwrite(buf,1,len,stdout); } } if (kflike(hstdout)) { ClosePipe(hstdout); } if (kflike(hstderr)) { ClosePipe(hstderr); } ClosePipe(big_stdout_pipe[READ_PIPE]); if (token) { KVirtualHost::closeToken(token); } #ifdef _WIN32 if (kflike(sc->last_pid)) { WaitForSingleObject(sc->last_pid,INFINITE); } #endif return result; }
/* ChapelIO.chpl:1 */ void __init_ChapelIO(int32_t _ln, _string _fn) { chpl_bool T1; _cfile T2; file T5 = NULL; file T3 = NULL; _syncvar_uint64_t T4 = NULL; _cfile T6; file T9 = NULL; file T7 = NULL; _syncvar_uint64_t T8 = NULL; _cfile T10; file T13 = NULL; file T11 = NULL; _syncvar_uint64_t T12 = NULL; T1 = (!__run_ChapelIO_firsttime3); if (T1) { goto _end___init_ChapelIO; } __run_ChapelIO_firsttime3 = false; T2 = get_stdin(); T3 = (file)chpl_alloc(sizeof(_file), "instance of class _unknown", _ln, _fn); ((object)T3)->_cid = _e_file; T3->filename = ""; T3->mode = FileAccessMode_read; T3->path = ""; T3->_fp = 0; T3->filename = "stdin"; T3->mode = FileAccessMode_read; T3->path = "/dev"; T3->_fp = T2; T4 = _construct__syncvar(UINT64(0), _ln, _fn); T3->_lock = T4; T5 = _construct_file("stdin", FileAccessMode_read, "/dev", T2, T4, T3, _ln, _fn); _stdin_16853 = T5; T6 = get_stdout(); T7 = (file)chpl_alloc(sizeof(_file), "instance of class _unknown", _ln, _fn); ((object)T7)->_cid = _e_file; T7->filename = ""; T7->mode = FileAccessMode_read; T7->path = ""; T7->_fp = 0; T7->filename = "stdout"; T7->mode = FileAccessMode_write; T7->path = "/dev"; T7->_fp = T6; T8 = _construct__syncvar(UINT64(0), _ln, _fn); T7->_lock = T8; T9 = _construct_file("stdout", FileAccessMode_write, "/dev", T6, T8, T7, _ln, _fn); _stdout_16871 = T9; T10 = get_stderr(); T11 = (file)chpl_alloc(sizeof(_file), "instance of class _unknown", _ln, _fn); ((object)T11)->_cid = _e_file; T11->filename = ""; T11->mode = FileAccessMode_read; T11->path = ""; T11->_fp = 0; T11->filename = "stderr"; T11->mode = FileAccessMode_write; T11->path = "/dev"; T11->_fp = T10; T12 = _construct__syncvar(UINT64(0), _ln, _fn); T11->_lock = T12; T13 = _construct_file("stderr", FileAccessMode_write, "/dev", T10, T12, T11, _ln, _fn); _stderr_16889 = T13; _end___init_ChapelIO: ; return; }