Exemplo n.º 1
0
int pipe_write(const char *cmd, struct list *l, int close_out)
{
	int fds[2]; /* 0 = read, 1 = write */

	if(pipe(fds) == -1)
		return -1;

	switch(fork()){
		case -1:
			return -1;

		case 0:
		{
			if(close_out){
				freopen("/dev/null", "w", stdout);
				freopen("/dev/null", "w", stderr);
			}

			close(fds[WRITE_FD]);
			if(dup2(fds[READ_FD], STDIN_FILENO) == -1)
				exit(-1);

			exit(system(cmd));
		}

		default:
		{
			int ret;
			close(fds[READ_FD]);
			parent_write(fds[WRITE_FD], l);
			wait(&ret);
			return ret;
		}
	}
}
Exemplo n.º 2
0
struct list *pipe_readwrite(const char *cmd, struct list *l)
{
	int parent_to_child[2], child_to_parent[2];

	if(pipe(parent_to_child) == -1 || pipe(child_to_parent) == -1){
		close(parent_to_child[READ_FD]);
		close(parent_to_child[WRITE_FD]);
		close(child_to_parent[READ_FD]);
		close(child_to_parent[WRITE_FD]);
		return NULL;
	}

	switch(fork()){
		case -1:
			return NULL;

		case 0:
			close(parent_to_child[WRITE_FD]);
			close(child_to_parent[READ_FD]);

			if(
				dup2(parent_to_child[READ_FD],  STDIN_FILENO ) == -1 ||
				dup2(child_to_parent[WRITE_FD], STDOUT_FILENO) == -1 ||
				dup2(STDOUT_FILENO, STDERR_FILENO) == -1
			){
				exit(-1);
			}

			execlp("sh", "sh", "-c", cmd, NULL);
			perror("exec()");
			exit(-1);

		default:
		{
			struct list *ret;
			int saveerrno;

			close(parent_to_child[READ_FD]);
			close(child_to_parent[WRITE_FD]);

			if(parent_write(parent_to_child[WRITE_FD], l))
				ret = NULL;
			else
				ret = list_from_fd(child_to_parent[READ_FD], NULL);

			saveerrno = errno;
			wait(NULL);
			errno = saveerrno;

			return ret;
		}
	}
}
static void parent_process( int to_child, const CrashData *crash )
{
	/* 1. Write the CrashData. */
	if( !parent_write(to_child, crash, sizeof(CrashData)) )
		return;
	
	/* 2. Write info. */
	const char *p = RageLog::GetInfo();
	int size = strlen( p )+1;
	if( !parent_write(to_child, &size, sizeof(size)) )
		return;
	if( !parent_write(to_child, p, size) )
		return;

	/* 3. Write AdditionalLog. */
	p = RageLog::GetAdditionalLog();
	size = strlen( p )+1;
	if( !parent_write(to_child, &size, sizeof(size)) )
		return;
	if( !parent_write(to_child, p, size) )
		return;
	
	/* 4. Write RecentLogs. */
	int cnt = 0;
	const char *ps[1024];
	while( cnt < 1024 && (ps[cnt] = RageLog::GetRecentLog( cnt )) != NULL )
		++cnt;

	parent_write(to_child, &cnt, sizeof(cnt));
	for( int i = 0; i < cnt; ++i )
	{
		size = strlen(ps[i])+1;
		if( !parent_write(to_child, &size, sizeof(size)) )
			return;
		if( !parent_write(to_child, ps[i], size) )
			return;
	}

	/* 5. Write CHECKPOINTs. */
	static char buf[1024*32];
	Checkpoints::GetLogs( buf, sizeof(buf), "\n" );
	size = strlen( buf )+1;
	if( !parent_write(to_child, &size, sizeof(size)) )
		return;
	if( !parent_write(to_child, buf, size) )
		return;
	
	/* 6. Write the crashed thread's name. */
	p = RageThread::GetCurrentThreadName();
	size = strlen( p )+1;
	if( !parent_write(to_child, &size, sizeof(size)) )
		return;
	if( !parent_write(to_child, p, size) )
		return;
}