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; }
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; }
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; }
static int __close(RIODesc *fd) { if (!fd || !fd->data) { return -1; } r2p_close (fd->data); fd->data = NULL; return 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; }
int main() { //R2Pipe *r2 = r2p_open ("/bin/ls"); R2Pipe *r2 = r2p_open ("r2 -q0 /bin/ls"); if (r2) { r2cmd (r2, "?e Hello World"); r2cmd (r2, "x"); r2cmd (r2, "?e Hello World"); r2cmd (r2, "pd 20"); r2p_close (r2); return 0; } return 1; }
R_API void r2p_free (R2Pipe *r2p) { r2p->magic = 0; r2p_close (r2p); }