/** * Copyright 2009-2010 Bart Trojanowski * Licensed under GPLv2, or later, at your choosing. * * bidirectional popen() call * * @param rwepipe - int array of size three * @param exe - program to run * @param argv - argument list * @return pid or -1 on error * * The caller passes in an array of three integers (rwepipe), on successful * execution it can then write to element 0 (stdin of exe), and read from * element 1 (stdout) and 2 (stderr). */ /* Modified by Stephen Paul Weber to take a path and pass it to sh -c */ #include #include #include #include "popenRWE.h" int popenRWE(int *rwepipe, const char *path) { int in[2]; int out[2]; int err[2]; int pid; int rc; const char *argv[4] = {"sh", "-c", NULL, NULL}; argv[2] = path; rc = pipe(in); if (rc<0) goto error_in; rc = pipe(out); if (rc<0) goto error_out; rc = pipe(err); if (rc<0) goto error_err; pid = fork(); if (pid > 0) { /* parent */ close(in[0]); close(out[1]); close(err[1]); rwepipe[0] = in[1]; rwepipe[1] = out[0]; rwepipe[2] = err[0]; return pid; } else if (pid == 0) { /* child */ close(in[1]); close(out[0]); close(err[0]); close(0); if(dup(in[0]) < 0) exit(2); close(1); if(dup(out[1]) < 0) exit(2); close(2); if(dup(err[1]) < 0) exit(2); execvp(argv[0], (char**)argv); exit(1); } else goto error_fork; return pid; error_fork: close(err[0]); close(err[1]); error_err: close(out[0]); close(out[1]); error_out: close(in[0]); close(in[1]); error_in: return -1; } int pcloseRWE(int pid, int *rwepipe) { int rc, status; close(rwepipe[0]); close(rwepipe[1]); close(rwepipe[2]); do { rc = waitpid(pid, &status, 0); } while (rc != 0 && ! WIFEXITED(status)); return status; }