|
2 | 2 | // Windows includes go here
|
3 | 3 | #else
|
4 | 4 | #include <cstring>
|
5 |
| -#include <iostream> |
6 |
| -#include <unistd.h> |
7 | 5 | #include <errno.h>
|
8 | 6 | #include <fcntl.h> // library for fcntl function
|
| 7 | +#include <iostream> |
| 8 | +#include <unistd.h> |
9 | 9 | #endif
|
10 | 10 |
|
11 | 11 | #include "invariant.h"
|
12 | 12 | #include "piped_process.h"
|
13 | 13 |
|
14 |
| - |
15 | 14 | piped_processt::piped_processt(const std::string &command)
|
16 | 15 | {
|
17 |
| - #ifdef _WIN32 |
18 |
| - // This should use the new error state from PR #6131 once that is done |
19 |
| - INVARIANT(false, "New SMT2 backend WIP: Windows piped_process constructor."); |
20 |
| - #else |
21 |
| - |
22 |
| - if (pipe(pipe_input) == -1) { |
23 |
| - throw std::runtime_error("Input pipe creation failed"); |
24 |
| - } |
25 |
| - |
26 |
| - if (pipe(pipe_output) == -1) { |
27 |
| - throw std::runtime_error("Output pipe creation failed"); |
28 |
| - } |
29 |
| - |
30 |
| - // Default state |
31 |
| - process_state = process_statet::NOT_CREATED; |
32 |
| - |
33 |
| - if (fcntl(pipe_output[0], F_SETFL, O_NONBLOCK) < 0) { |
34 |
| - throw std::runtime_error("Setting pipe non-blocking failed"); |
35 |
| - } |
| 16 | +#ifdef _WIN32 |
| 17 | + // This should use the new error state from PR #6131 once that is done |
| 18 | + INVARIANT(false, "New SMT2 backend WIP: Windows piped_process constructor."); |
| 19 | +#else |
36 | 20 |
|
37 |
| - // Create a new process for the child that will execute the |
38 |
| - // command and receive information via pipes. |
39 |
| - pid_t pid = fork(); |
40 |
| - if (pid == 0) { |
41 |
| - // child process here |
42 |
| - |
43 |
| - // Close pipes that will be used by the parent so we do |
44 |
| - // not have our own copies and conflicts. |
45 |
| - close(pipe_input[1]); |
46 |
| - close(pipe_output[0]); |
47 |
| - |
48 |
| - // Duplicate pipes so we have the ones we need. |
49 |
| - dup2(pipe_input[0], STDIN_FILENO); |
50 |
| - dup2(pipe_output[1], STDOUT_FILENO); |
51 |
| - dup2(pipe_output[1], STDERR_FILENO); |
52 |
| - |
53 |
| - // Create the arguments to execvp from the construction string |
54 |
| - char **args = split_command_args(command); |
55 |
| - // Execute the command |
56 |
| - execvp(args[0], args); |
57 |
| - // Only reachable if execvp failed |
58 |
| - throw std::runtime_error("Launching \"" + command + |
59 |
| - "\" failed with error: " + strerror(errno)); |
60 |
| - } else { |
61 |
| - // parent process here |
62 |
| - // Close pipes to be used by the child process |
63 |
| - close(pipe_input[0]); |
64 |
| - close(pipe_output[1]); |
65 |
| - |
66 |
| - // Get stream for sending to the child process |
67 |
| - command_stream = fdopen(pipe_input[1], "w"); |
68 |
| - process_state = process_statet::CREATED; |
69 |
| - } |
70 |
| - #endif |
| 21 | + if(pipe(pipe_input) == -1) |
| 22 | + { |
| 23 | + throw std::runtime_error("Input pipe creation failed"); |
| 24 | + } |
| 25 | + |
| 26 | + if(pipe(pipe_output) == -1) |
| 27 | + { |
| 28 | + throw std::runtime_error("Output pipe creation failed"); |
| 29 | + } |
| 30 | + |
| 31 | + // Default state |
| 32 | + process_state = process_statet::NOT_CREATED; |
| 33 | + |
| 34 | + if(fcntl(pipe_output[0], F_SETFL, O_NONBLOCK) < 0) |
| 35 | + { |
| 36 | + throw std::runtime_error("Setting pipe non-blocking failed"); |
| 37 | + } |
| 38 | + |
| 39 | + // Create a new process for the child that will execute the |
| 40 | + // command and receive information via pipes. |
| 41 | + pid_t pid = fork(); |
| 42 | + if(pid == 0) |
| 43 | + { |
| 44 | + // child process here |
| 45 | + |
| 46 | + // Close pipes that will be used by the parent so we do |
| 47 | + // not have our own copies and conflicts. |
| 48 | + close(pipe_input[1]); |
| 49 | + close(pipe_output[0]); |
| 50 | + |
| 51 | + // Duplicate pipes so we have the ones we need. |
| 52 | + dup2(pipe_input[0], STDIN_FILENO); |
| 53 | + dup2(pipe_output[1], STDOUT_FILENO); |
| 54 | + dup2(pipe_output[1], STDERR_FILENO); |
| 55 | + |
| 56 | + // Create the arguments to execvp from the construction string |
| 57 | + char **args = split_command_args(command); |
| 58 | + // Execute the command |
| 59 | + execvp(args[0], args); |
| 60 | + // Only reachable if execvp failed |
| 61 | + throw std::runtime_error( |
| 62 | + "Launching \"" + command + "\" failed with error: " + strerror(errno)); |
| 63 | + } |
| 64 | + else |
| 65 | + { |
| 66 | + // parent process here |
| 67 | + // Close pipes to be used by the child process |
| 68 | + close(pipe_input[0]); |
| 69 | + close(pipe_output[1]); |
| 70 | + |
| 71 | + // Get stream for sending to the child process |
| 72 | + command_stream = fdopen(pipe_input[1], "w"); |
| 73 | + process_state = process_statet::CREATED; |
| 74 | + } |
| 75 | +#endif |
71 | 76 | }
|
72 | 77 |
|
73 | 78 | bool piped_processt::send(const std::string &message)
|
74 | 79 | {
|
75 |
| - #ifdef _WIN32 |
76 |
| - // This should use the new error state from PR #6131 once that is done |
77 |
| - INVARIANT(false, "New SMT2 backend WIP: Windows piped_processt::send."); |
78 |
| - #else |
79 |
| - |
80 |
| - if(process_state != process_statet::CREATED) |
81 |
| - { |
82 |
| - return false; |
83 |
| - } |
84 |
| - |
85 |
| - // send message to solver process |
86 |
| - int send_status = fputs(message.c_str(), command_stream); |
87 |
| - fflush(command_stream); |
88 |
| - |
89 |
| - if(send_status == EOF) |
90 |
| - { |
91 |
| - // Some kind of error occured, maybe we should update the |
92 |
| - // solver status here? |
93 |
| - return false; |
94 |
| - } |
| 80 | +#ifdef _WIN32 |
| 81 | + // This should use the new error state from PR #6131 once that is done |
| 82 | + INVARIANT(false, "New SMT2 backend WIP: Windows piped_processt::send."); |
| 83 | +#else |
| 84 | + |
| 85 | + if(process_state != process_statet::CREATED) |
| 86 | + { |
| 87 | + return false; |
| 88 | + } |
| 89 | + |
| 90 | + // send message to solver process |
| 91 | + int send_status = fputs(message.c_str(), command_stream); |
| 92 | + fflush(command_stream); |
95 | 93 |
|
96 |
| - return true; |
97 |
| - #endif |
| 94 | + if(send_status == EOF) |
| 95 | + { |
| 96 | + // Some kind of error occured, maybe we should update the |
| 97 | + // solver status here? |
| 98 | + return false; |
| 99 | + } |
| 100 | + |
| 101 | + return true; |
| 102 | +#endif |
98 | 103 | }
|
99 | 104 |
|
100 | 105 | std::string piped_processt::receive()
|
101 | 106 | {
|
102 |
| - #ifdef _WIN32 |
103 |
| - // This should use the new error state from PR #6131 once that is done |
104 |
| - INVARIANT(false, "New SMT2 backend WIP: Windows piped_processt::receive."); |
105 |
| - #else |
106 |
| - |
107 |
| - if(process_state != process_statet::CREATED) |
108 |
| - return NULL; |
109 |
| - |
110 |
| - std::string response = std::string(""); |
111 |
| - int nbytes; |
112 |
| - char buff[BUFSIZE]; |
113 |
| - |
114 |
| - while (true) |
115 |
| - { |
116 |
| - nbytes = read(pipe_output[0], buff, BUFSIZE); |
117 |
| - switch (nbytes) { |
118 |
| - case -1: |
119 |
| - // Nothing more to read in the pipe |
120 |
| - return response; |
121 |
| - case 0: |
122 |
| - // Pipe is closed. |
123 |
| - process_state = process_statet::STOPPED; |
124 |
| - if (response == std::string("")) { |
125 |
| - return NULL; |
126 |
| - } |
127 |
| - return response; |
128 |
| - default: |
129 |
| - // Read some bytes, append them to the response and continue |
130 |
| - response.append(buff, nbytes); |
131 |
| - } |
132 |
| - } |
133 |
| - |
134 |
| - UNREACHABLE; |
135 |
| - #endif |
136 |
| -} |
| 107 | +#ifdef _WIN32 |
| 108 | + // This should use the new error state from PR #6131 once that is done |
| 109 | + INVARIANT(false, "New SMT2 backend WIP: Windows piped_processt::receive."); |
| 110 | +#else |
137 | 111 |
|
| 112 | + if(process_state != process_statet::CREATED) |
| 113 | + return NULL; |
138 | 114 |
|
139 |
| -char ** piped_processt::split_command_args(const std::string &command) |
140 |
| -{ |
141 |
| - char ** res = NULL; |
142 |
| - int n_spaces = 0; |
143 |
| - char *p = strtok(strdup(command.c_str()), " "); |
| 115 | + std::string response = std::string(""); |
| 116 | + int nbytes; |
| 117 | + char buff[BUFSIZE]; |
144 | 118 |
|
145 |
| - while(p) |
| 119 | + while(true) |
| 120 | + { |
| 121 | + nbytes = read(pipe_output[0], buff, BUFSIZE); |
| 122 | + switch(nbytes) |
146 | 123 | {
|
147 |
| - res = (char **)realloc(res, sizeof (char*) * ++n_spaces); |
148 |
| - if (res == NULL) |
149 |
| - exit (-1); /* memory allocation failed */ |
150 |
| - res[n_spaces-1] = p; |
151 |
| - p = strtok (NULL, " "); |
| 124 | + case -1: |
| 125 | + // Nothing more to read in the pipe |
| 126 | + return response; |
| 127 | + case 0: |
| 128 | + // Pipe is closed. |
| 129 | + process_state = process_statet::STOPPED; |
| 130 | + if(response == std::string("")) |
| 131 | + { |
| 132 | + return NULL; |
| 133 | + } |
| 134 | + return response; |
| 135 | + default: |
| 136 | + // Read some bytes, append them to the response and continue |
| 137 | + response.append(buff, nbytes); |
152 | 138 | }
|
| 139 | + } |
153 | 140 |
|
154 |
| - res = (char **)realloc (res, sizeof (char*) * (n_spaces+1)); |
155 |
| - res[n_spaces] = 0; |
156 |
| - return res; |
| 141 | + UNREACHABLE; |
| 142 | +#endif |
| 143 | +} |
| 144 | + |
| 145 | +char **piped_processt::split_command_args(const std::string &command) |
| 146 | +{ |
| 147 | + char **res = NULL; |
| 148 | + int n_spaces = 0; |
| 149 | + char *p = strtok(strdup(command.c_str()), " "); |
| 150 | + |
| 151 | + while(p) |
| 152 | + { |
| 153 | + res = (char **)realloc(res, sizeof(char *) * ++n_spaces); |
| 154 | + if(res == NULL) |
| 155 | + exit(-1); /* memory allocation failed */ |
| 156 | + res[n_spaces - 1] = p; |
| 157 | + p = strtok(NULL, " "); |
| 158 | + } |
| 159 | + |
| 160 | + res = (char **)realloc(res, sizeof(char *) * (n_spaces + 1)); |
| 161 | + res[n_spaces] = 0; |
| 162 | + return res; |
157 | 163 | }
|
0 commit comments