Ejemplo n.º 1
0
R_API R2Pipe *r2p_open(const char *cmd) {
	R2Pipe *r2p = R_NEW0 (R2Pipe);
	r2p->magic = R2P_MAGIC;
#if __WINDOWS__
	w32_createPipe (r2p, cmd);
	r2p->child = (int)(r2p->pipe);
#else
	pipe (r2p->input);
	pipe (r2p->output);
	r2p->child = fork ();
	if (r2p->child == -1) {
		r2p_close (r2p);
		return NULL;
	}
	env ("R2PIPE_IN", r2p->input[0]);
	env ("R2PIPE_OUT", r2p->output[1]);

	if (r2p->child) {
		eprintf ("Child is %d\n", r2p->child);
	} else {
		int rc;
		rc = r_sandbox_system (cmd, 1);
		eprintf ("Child was %d with %d\n", r2p->child, rc);
		r2p_close (r2p);
		exit (0);
		return NULL;
	}
#endif
	return r2p;
}
Ejemplo n.º 2
0
R_API R2Pipe *r2p_open(const char *cmd) {
	R2Pipe *r2p = R_NEW0 (R2Pipe);
	r2p->magic = R2P_MAGIC;
	if (cmd == NULL) {
		r2p->child = -1;
#if __UNIX__
		 {
			char *out = r_sys_getenv ("R2PIPE_IN");
			char *in = r_sys_getenv ("R2PIPE_OUT");
			int done = R_FALSE;
			if (in && out) {
				int i_in = atoi (in);
				int i_out = atoi (out);
				if (i_in>=0 && i_out>=0) {
					r2p->input[0] = r2p->input[1] = i_in;
					r2p->output[0] = r2p->output[1] = i_out;
					done = R_TRUE;
				}
			}
			if (!done) {
				eprintf ("Cannot find R2PIPE_IN or R2PIPE_OUT environment\n");
				R_FREE (r2p);
			}
			free (in);
			free (out);
		 }
		return r2p;
#else
		eprintf ("r2p_open(NULL) not supported on windows\n");
		return NULL;
#endif
	}
#if __WINDOWS__
	w32_createPipe (r2p, cmd);
	r2p->child = (int)(r2p->pipe);
#else
	pipe (r2p->input);
	pipe (r2p->output);
	r2p->child = fork ();
	if (r2p->child == -1) {
		r2p_close (r2p);
		return NULL;
	}
	env ("R2PIPE_IN", r2p->input[0]);
	env ("R2PIPE_OUT", r2p->output[1]);

	if (r2p->child) {
		eprintf ("Child is %d\n", r2p->child);
	} else {
		int rc;
		if (cmd && *cmd) {
			rc = r_sandbox_system (cmd, 1);
		} else rc = 0;
		r2p_close (r2p);
		exit (0);
		return NULL;
	}
#endif
	return r2p;
}
Ejemplo n.º 3
0
R_API R2Pipe *r2p_open(const char *cmd) {
	R2Pipe *r2p = R_NEW0 (R2Pipe);
	if (!r2p) return NULL;
	r2p->magic = R2P_MAGIC;
	if (!cmd) {
		r2p->child = -1;
		return r2p_open_spawn (r2p, cmd);
	}
#if __WINDOWS__ && !defined(__CYGWIN__)
	w32_createPipe (r2p, cmd);
	r2p->child = (int)(r2p->pipe);
#else
	pipe (r2p->input);
	pipe (r2p->output);
#if LIBC_HAVE_FORK
	r2p->child = fork ();
#else
	r2p->child = -1;
#endif
	if (r2p->child == -1) {
		r2p_close (r2p);
		return NULL;
	}
	env ("R2PIPE_IN", r2p->input[0]);
	env ("R2PIPE_OUT", r2p->output[1]);

	if (r2p->child) {
		char ch;
		eprintf ("[+] r2pipe child is %d\n", r2p->child);
#if 0
		if (read (r2p->output[0], &ch, 1) != 1) {
			eprintf ("Failed to read 1 byte\n");
			r2p_close (r2p);
			return NULL;
		}
		if (ch == 0x00) {
			eprintf ("[+] r2pipe-io link stablished\n");
		}
#endif
	} else {
		int rc = 0;
		if (cmd && *cmd) {
			close (0);
			close (1);
			dup2 (r2p->input[0], 0);
			dup2 (r2p->output[1], 1);
			rc = r_sandbox_system (cmd, 0);
		}
		r2p_close (r2p);
		exit (rc);
		return NULL;
	}
#endif
	return r2p;
}
Ejemplo n.º 4
0
R_API R2Pipe *r2p_open(const char *cmd) {
    R2Pipe *r2p = R_NEW0 (R2Pipe);
    r2p->magic = R2P_MAGIC;
    if (!cmd) {
        r2p->child = -1;
        return r2p_open_spawn (r2p, cmd);
    }
#if __WINDOWS__ && !defined(__CYGWIN__)
    w32_createPipe (r2p, cmd);
    r2p->child = (int)(r2p->pipe);
#else
    pipe (r2p->input);
    pipe (r2p->output);
    r2p->child = fork ();
    if (r2p->child == -1) {
        r2p_close (r2p);
        return NULL;
    }
    env ("R2PIPE_IN", r2p->input[0]);
    env ("R2PIPE_OUT", r2p->output[1]);

    if (r2p->child) {
        eprintf ("Child is %d\n", r2p->child);
        char ch;
        read (r2p->output[0], &ch, 1);
    } else {
        int rc = 0;
        if (cmd && *cmd) {
            close (0);
            close (1);
            dup2 (r2p->input[0], 0);
            dup2 (r2p->output[1], 1);
            rc = r_sandbox_system (cmd, 0);
        }
        r2p_close (r2p);
        exit (rc);
        return NULL;
    }
#endif
    return r2p;
}
Ejemplo n.º 5
0
R_API R2Pipe *r2pipe_open(const char *cmd) {
	R2Pipe *r2pipe = r2pipe_new ();
	if (!r2pipe) {
		return NULL;
	}
	if (!cmd) {
		r2pipe->child = -1;
		return r2pipe_open_spawn (r2pipe);
	}
#if __WINDOWS__ && !defined(__CYGWIN__)
	w32_createPipe (r2pipe, cmd);
	r2pipe->child = (int)(r2pipe->pipe);
#else
	int r = pipe (r2pipe->input);
	if (r != 0) {
		eprintf ("pipe failed on input\n");
		r2pipe_close (r2pipe);
		return NULL;
	}
	r = pipe (r2pipe->output);
	if (r != 0) {
		eprintf ("pipe failed on output\n");
		r2pipe_close (r2pipe);
		return NULL;
	}
#if LIBC_HAVE_FORK
	r2pipe->child = fork ();
#else
	r2pipe->child = -1;
#endif
	if (r2pipe->child == -1) {
		r2pipe_close (r2pipe);
		return NULL;
	}
	env ("R2PIPE_IN", r2pipe->input[0]);
	env ("R2PIPE_OUT", r2pipe->output[1]);

	if (r2pipe->child) {
		char ch = 1;
		// eprintf ("[+] r2pipeipe child is %d\n", r2pipe->child);
		if (read (r2pipe->output[0], &ch, 1) != 1) {
			eprintf ("Failed to read 1 byte\n");
			r2pipe_close (r2pipe);
			return NULL;
		}
		if (ch) {
			eprintf ("[+] r2pipeipe-io link failed. Expected two null bytes.\n");
			r2pipe_close (r2pipe);
			return NULL;
		}
		// Close parent's end of pipes
		close (r2pipe->input[0]);
		close (r2pipe->output[1]);
		r2pipe->input[0] = -1;
		r2pipe->output[1] = -1;
	} else {
		int rc = 0;
		if (cmd && *cmd) {
			close (0);
			close (1);
			dup2 (r2pipe->input[0], 0);
			dup2 (r2pipe->output[1], 1);
			close (r2pipe->input[1]);
			close (r2pipe->output[0]);
			r2pipe->input[1] = -1;
			r2pipe->output[0] = -1;
			rc = r_sandbox_system (cmd, 0);
		}
		r2pipe_close (r2pipe);
		exit (rc);
		return NULL;
	}
#endif
	return r2pipe;
}