|
1 /* Copyright 2025 Mike Becker. All rights reserved. |
|
2 * |
|
3 * Redistribution and use in source and binary forms, with or without |
|
4 * modification, are permitted provided that the following conditions are met: |
|
5 * |
|
6 * 1. Redistributions of source code must retain the above copyright |
|
7 * notice, this list of conditions and the following disclaimer. |
|
8 * |
|
9 * 2. Redistributions in binary form must reproduce the above copyright |
|
10 * notice, this list of conditions and the following disclaimer in the |
|
11 * documentation and/or other materials provided with the distribution. |
|
12 * |
|
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|
16 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
|
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
|
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
|
20 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
|
21 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
22 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
23 */ |
|
24 |
|
25 #include "process.h" |
|
26 |
|
27 #include <cstdio> |
|
28 #include <cstdlib> |
|
29 #include <fcntl.h> |
|
30 #include <unistd.h> |
|
31 #include <sys/wait.h> |
|
32 |
|
33 using namespace fm; |
|
34 |
|
35 process::process(std::string path): m_path(std::move(path)) { |
|
36 } |
|
37 |
|
38 bool process::exec(std::vector<std::string> args) { |
|
39 // fd-pair for the pipe |
|
40 int pipefd[2]; |
|
41 |
|
42 if (pipe(pipefd)) { |
|
43 perror("pipe"); |
|
44 return false; |
|
45 } |
|
46 |
|
47 pid_t pid = fork(); |
|
48 if (pid == 0) { |
|
49 // connect the pipe and close the end we don't use |
|
50 if (dup2(pipefd[1], STDOUT_FILENO) == -1) { |
|
51 perror("pipe"); |
|
52 exit(EXIT_FAILURE); |
|
53 } |
|
54 close(pipefd[0]); |
|
55 |
|
56 // create the execv argument list |
|
57 char *argv[args.size() + 2] = {}; |
|
58 auto slash = m_path.find_last_of('/'); |
|
59 if (slash == std::string::npos) { |
|
60 argv[0] = m_path.data(); |
|
61 } else { |
|
62 argv[0] = m_path.data() + slash + 1; |
|
63 } |
|
64 unsigned i = 0; |
|
65 for (auto&& arg : args) { |
|
66 argv[++i] = arg.data(); |
|
67 } |
|
68 argv[args.size() + 1] = nullptr; |
|
69 |
|
70 // execute the child program |
|
71 if (execv(m_path.c_str(), argv)) { |
|
72 perror("execl"); |
|
73 exit(EXIT_FAILURE); |
|
74 } |
|
75 return true; // unreachable, but the compiler doesn't know that |
|
76 } else if (pid > 0) { |
|
77 // close the end of the pipe we don't use |
|
78 close(pipefd[1]); |
|
79 |
|
80 // read all the output |
|
81 char buf[64]; |
|
82 ssize_t r; |
|
83 while ((r = read(pipefd[0], buf, sizeof(buf))) > 0) { |
|
84 m_output.append(buf, r); |
|
85 } |
|
86 if (r < 0) { |
|
87 perror("read"); |
|
88 } |
|
89 close(pipefd[0]); |
|
90 |
|
91 // wait for the process to completely finish |
|
92 int status = -1; |
|
93 waitpid(pid, &status, 0); |
|
94 |
|
95 return status == 0; |
|
96 } else { |
|
97 perror("fork"); |
|
98 return false; |
|
99 } |
|
100 } |
|
101 |
|
102 const std::string &process::output() const { |
|
103 return m_output; |
|
104 } |