示例#1
0
文件: misc.c 项目: plean/PSU
void	reset_fd(void)
{
  get_stdin(0);
  get_stdout(1);
  get_stderr(2);
  if (get_pipes(0))
    while (get_pipes(-1));
}
示例#2
0
void	print_env(t_node_cmd *node, t_env *env)
{
	int		out_fd;

	out_fd = get_stdout(node);
	while (env)
	{
		ft_putstr_fd(env->key, out_fd);
		ft_putchar_fd('=', out_fd);
		ft_putendl_fd(env->val, out_fd);
		env = env->next;
	}
}
示例#3
0
void create_processes(char* buffer, char commands[][CMD_SIZE], char* envp[])
{
	int size = parse_cmds(buffer, commands);
	
	int pip[2], total = 0, pipe_in = 0;
	switch(fork())
	{
		case -1:
			shell_error("fork()");
		case 0:
			while(total != size)
			{
				if(pipe(pip) == -1)
					shell_error("pipe()");
				//printf("pip : %d, %d\n", pip[0], pip[1]);
				char* temp[CMD_SIZE];
				char* args[CMD_SIZE];
				int used = separate_commands(total, commands, size, temp);
				get_arguments(temp, used, args); //NULL TERMINATED

				//print_args(temp, used);
				//print_args2(args);
				int in = get_stdin(temp, used, pipe_in); //get instream and outstream.
				int out = get_stdout(temp, used, pip);
				//printf("%d : %d\n", in, out);
				prepare_to_execute(args, envp, in, out);
				pipe_in = has_pipe(temp, used, pip);	
				//printf("[%d]\n", pipe_in);
				total += used;
			}
			exit(1);

		default:
			if(!has_bg(commands, size))
				if (wait(0) == -1)
					shell_error("wait()");
	}
}	
示例#4
0
__EXPORT void px4_log_raw(int level, const char *fmt, ...)
{
	FILE *out = stdout;
	bool use_color = true;

#ifdef __PX4_POSIX
	out = get_stdout(&use_color);
#endif

#ifndef PX4_LOG_COLORIZED_OUTPUT
	use_color = false;
#endif

	if (level >= _PX4_LOG_LEVEL_INFO) {
		if (use_color) { fputs(__px4_log_level_color[level], out); }

		va_list argptr;
		va_start(argptr, fmt);
		vfprintf(out, fmt, argptr);
		va_end(argptr);

		if (use_color) { fputs(PX4_ANSI_COLOR_RESET, out); }
	}
}
示例#5
0
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;
}
示例#6
0
文件: ChapelIO.c 项目: hildeth/chapel
/* 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;
}
示例#7
0
__EXPORT void px4_log_modulename(int level, const char *moduleName, const char *fmt, ...)
{
	FILE *out = stdout;
	bool use_color = true;

#ifdef __PX4_POSIX
	out = get_stdout(&use_color);
#endif

#ifndef PX4_LOG_COLORIZED_OUTPUT
	use_color = false;
#endif

	if (level >= _PX4_LOG_LEVEL_INFO) {
		if (use_color) { fputs(__px4_log_level_color[level], out); }

		fprintf(out, __px4__log_level_fmt __px4__log_level_arg(level));

		if (use_color) { fputs(PX4_ANSI_COLOR_GRAY, out); }

		fprintf(out, __px4__log_modulename_pfmt, moduleName);

		if (use_color) { fputs(__px4_log_level_color[level], out); }

		va_list argptr;
		va_start(argptr, fmt);
		vfprintf(out, fmt, argptr);
		va_end(argptr);

		if (use_color) { fputs(PX4_ANSI_COLOR_RESET, out); }

		fputc('\n', out);
	}

	/* publish an orb log message */
	if (level >= _PX4_LOG_LEVEL_WARN && orb_log_message_pub) { //only publish important messages

		struct log_message_s log_message;
		const unsigned max_length_pub = sizeof(log_message.text);
		log_message.timestamp = hrt_absolute_time();

		const uint8_t log_level_table[] = {
			7, /* _PX4_LOG_LEVEL_DEBUG */
			6, /* _PX4_LOG_LEVEL_INFO */
			4, /* _PX4_LOG_LEVEL_WARN */
			3, /* _PX4_LOG_LEVEL_ERROR */
			0  /* _PX4_LOG_LEVEL_PANIC */
		};
		log_message.severity = log_level_table[level];

		unsigned pos = 0;

		va_list argptr;

		pos += snprintf((char *)log_message.text + pos, max_length_pub - pos, __px4__log_modulename_pfmt, moduleName);
		va_start(argptr, fmt);
		pos += vsnprintf((char *)log_message.text + pos, max_length_pub - pos, fmt, argptr);
		va_end(argptr);
		log_message.text[max_length_pub - 1] = 0; //ensure 0-termination

#if !defined(PARAM_NO_ORB)
		orb_publish(ORB_ID(log_message), orb_log_message_pub, &log_message);
#endif /* !PARAM_NO_ORB */
	}
}