Debian Codegraph @

src/core/popenRWE.c

raw symbols ◀ ▶
Source reconstructed at patch state 1 of 3 — not the installed (fully patched) source.
3 Debian patches touch this file
1 /**
2 * Copyright 2009-2010 Bart Trojanowski <[email protected]>
3 * Licensed under GPLv2, or later, at your choosing.
4 *
5 * bidirectional popen() call
6 *
7 * @param rwepipe - int array of size three
8 * @param exe - program to run
9 * @param argv - argument list
10 * @return pid or -1 on error
11 *
12 * The caller passes in an array of three integers (rwepipe), on successful
13 * execution it can then write to element 0 (stdin of exe), and read from
14 * element 1 (stdout) and 2 (stderr).
15 */
16
17 /* Modified by Stephen Paul Weber to take a path and pass it to sh -c */
18
19 #include <unistd.h>
20 #include <stdlib.h>
21 #include <sys/wait.h>
22
23 #include "popenRWE.h"
24
25 int popenRWE(int *rwepipe, const char *path) {
26 int in[2];
27 int out[2];
28 int err[2];
29 int pid;
30 int rc;
31 const char *argv[4] = {"sh", "-c", NULL, NULL};
32 argv[2] = path;
33
34 rc = pipe(in);
35 if (rc<0)
36 goto error_in;
37
38 rc = pipe(out);
39 if (rc<0)
40 goto error_out;
41
42 rc = pipe(err);
43 if (rc<0)
44 goto error_err;
45
46 pid = fork();
47 if (pid > 0) { /* parent */
48 close(in[0]);
49 close(out[1]);
50 close(err[1]);
51 rwepipe[0] = in[1];
52 rwepipe[1] = out[0];
53 rwepipe[2] = err[0];
54 return pid;
55 } else if (pid == 0) { /* child */
56 close(in[1]);
57 close(out[0]);
58 close(err[0]);
59 close(0);
60 dup(in[0]);
61 close(1);
62 dup(out[1]);
63 close(2);
64 dup(err[1]);
65
66 execvp(argv[0], (char**)argv);
67 exit(1);
68 } else
69 goto error_fork;
70
71 return pid;
72
73 error_fork:
74 close(err[0]);
75 close(err[1]);
76 error_err:
77 close(out[0]);
78 close(out[1]);
79 error_out:
80 close(in[0]);
81 close(in[1]);
82 error_in:
83 return -1;
84 }
85
86 int pcloseRWE(int pid, int *rwepipe) {
87 int rc, status;
88 close(rwepipe[0]);
89 close(rwepipe[1]);
90 close(rwepipe[2]);
91 rc = waitpid(pid, &status, 0);
92 return status;
93 }