xref: /aosp_15_r20/system/netd/server/IptablesRestoreController.cpp (revision 8542734a0dd1db395a4d42aae09c37f3c3c3e7a1)
1*8542734aSAndroid Build Coastguard Worker /*
2*8542734aSAndroid Build Coastguard Worker  * Copyright (C) 2017 The Android Open Source Project
3*8542734aSAndroid Build Coastguard Worker  *
4*8542734aSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*8542734aSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*8542734aSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*8542734aSAndroid Build Coastguard Worker  *
8*8542734aSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*8542734aSAndroid Build Coastguard Worker  *
10*8542734aSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*8542734aSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*8542734aSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8542734aSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*8542734aSAndroid Build Coastguard Worker  * limitations under the License.
15*8542734aSAndroid Build Coastguard Worker  */
16*8542734aSAndroid Build Coastguard Worker 
17*8542734aSAndroid Build Coastguard Worker #define LOG_TAG "IptablesRestoreController"
18*8542734aSAndroid Build Coastguard Worker #include "IptablesRestoreController.h"
19*8542734aSAndroid Build Coastguard Worker 
20*8542734aSAndroid Build Coastguard Worker #include <poll.h>
21*8542734aSAndroid Build Coastguard Worker #include <signal.h>
22*8542734aSAndroid Build Coastguard Worker #include <sys/wait.h>
23*8542734aSAndroid Build Coastguard Worker #include <unistd.h>
24*8542734aSAndroid Build Coastguard Worker 
25*8542734aSAndroid Build Coastguard Worker #include <android-base/file.h>
26*8542734aSAndroid Build Coastguard Worker #include <android-base/logging.h>
27*8542734aSAndroid Build Coastguard Worker #include <android-base/properties.h>
28*8542734aSAndroid Build Coastguard Worker #include <netdutils/Syscalls.h>
29*8542734aSAndroid Build Coastguard Worker 
30*8542734aSAndroid Build Coastguard Worker #include "Controllers.h"
31*8542734aSAndroid Build Coastguard Worker #include "NetdConstants.h"
32*8542734aSAndroid Build Coastguard Worker 
33*8542734aSAndroid Build Coastguard Worker using android::netdutils::StatusOr;
34*8542734aSAndroid Build Coastguard Worker using android::netdutils::sSyscalls;
35*8542734aSAndroid Build Coastguard Worker 
36*8542734aSAndroid Build Coastguard Worker constexpr char IPTABLES_RESTORE_PATH[] = "/system/bin/iptables-restore";
37*8542734aSAndroid Build Coastguard Worker constexpr char IP6TABLES_RESTORE_PATH[] = "/system/bin/ip6tables-restore";
38*8542734aSAndroid Build Coastguard Worker 
39*8542734aSAndroid Build Coastguard Worker constexpr char PING[] = "#PING\n";
40*8542734aSAndroid Build Coastguard Worker 
41*8542734aSAndroid Build Coastguard Worker constexpr size_t PING_SIZE = sizeof(PING) - 1;
42*8542734aSAndroid Build Coastguard Worker 
43*8542734aSAndroid Build Coastguard Worker // Not compile-time constants because they are changed by the unit tests.
44*8542734aSAndroid Build Coastguard Worker int IptablesRestoreController::MAX_RETRIES = 50;
45*8542734aSAndroid Build Coastguard Worker int IptablesRestoreController::POLL_TIMEOUT_MS = 100 * android::base::HwTimeoutMultiplier();
46*8542734aSAndroid Build Coastguard Worker 
47*8542734aSAndroid Build Coastguard Worker class IptablesProcess {
48*8542734aSAndroid Build Coastguard Worker public:
IptablesProcess(const IptablesRestoreController::IptablesProcessType type,pid_t pid,int stdIn,int stdOut,int stdErr)49*8542734aSAndroid Build Coastguard Worker     IptablesProcess(const IptablesRestoreController::IptablesProcessType type,
50*8542734aSAndroid Build Coastguard Worker             pid_t pid, int stdIn, int stdOut, int stdErr) :
51*8542734aSAndroid Build Coastguard Worker         type(type),
52*8542734aSAndroid Build Coastguard Worker         pid(pid),
53*8542734aSAndroid Build Coastguard Worker         stdIn(stdIn),
54*8542734aSAndroid Build Coastguard Worker         processTerminated(false) {
55*8542734aSAndroid Build Coastguard Worker 
56*8542734aSAndroid Build Coastguard Worker         pollFds[STDOUT_IDX] = { .fd = stdOut, .events = POLLIN };
57*8542734aSAndroid Build Coastguard Worker         pollFds[STDERR_IDX] = { .fd = stdErr, .events = POLLIN };
58*8542734aSAndroid Build Coastguard Worker     }
59*8542734aSAndroid Build Coastguard Worker 
~IptablesProcess()60*8542734aSAndroid Build Coastguard Worker     ~IptablesProcess() {
61*8542734aSAndroid Build Coastguard Worker         close(stdIn);
62*8542734aSAndroid Build Coastguard Worker         close(pollFds[STDOUT_IDX].fd);
63*8542734aSAndroid Build Coastguard Worker         close(pollFds[STDERR_IDX].fd);
64*8542734aSAndroid Build Coastguard Worker     }
65*8542734aSAndroid Build Coastguard Worker 
outputReady()66*8542734aSAndroid Build Coastguard Worker     bool outputReady() {
67*8542734aSAndroid Build Coastguard Worker         struct pollfd pollfd = { .fd = stdIn, .events = POLLOUT };
68*8542734aSAndroid Build Coastguard Worker         int ret = poll(&pollfd, 1, 0);
69*8542734aSAndroid Build Coastguard Worker         if (ret == -1) {
70*8542734aSAndroid Build Coastguard Worker             ALOGE("outputReady poll failed: %s", strerror(errno));
71*8542734aSAndroid Build Coastguard Worker             return false;
72*8542734aSAndroid Build Coastguard Worker         }
73*8542734aSAndroid Build Coastguard Worker         return (ret == 1) && !(pollfd.revents & POLLERR);
74*8542734aSAndroid Build Coastguard Worker     }
75*8542734aSAndroid Build Coastguard Worker 
stop()76*8542734aSAndroid Build Coastguard Worker     void stop() {
77*8542734aSAndroid Build Coastguard Worker         if (processTerminated) return;
78*8542734aSAndroid Build Coastguard Worker 
79*8542734aSAndroid Build Coastguard Worker         // This can be called by drainAndWaitForAck (after a POLLHUP) or by sendCommand (if the
80*8542734aSAndroid Build Coastguard Worker         // process was killed by something else on the system). In both cases, it's safe to send the
81*8542734aSAndroid Build Coastguard Worker         // PID a SIGTERM, because the PID continues to exist until its parent (i.e., us) calls
82*8542734aSAndroid Build Coastguard Worker         // waitpid on it, so there's no risk that the PID is reused.
83*8542734aSAndroid Build Coastguard Worker         ::stopProcess(pid, (type == IptablesRestoreController::IPTABLES_PROCESS) ?
84*8542734aSAndroid Build Coastguard Worker                 "iptables-restore" : "ip6tables-restore");
85*8542734aSAndroid Build Coastguard Worker 
86*8542734aSAndroid Build Coastguard Worker         processTerminated = true;
87*8542734aSAndroid Build Coastguard Worker     }
88*8542734aSAndroid Build Coastguard Worker 
89*8542734aSAndroid Build Coastguard Worker     const IptablesRestoreController::IptablesProcessType type;
90*8542734aSAndroid Build Coastguard Worker     const pid_t pid;  // NOLINT(misc-non-private-member-variables-in-classes)
91*8542734aSAndroid Build Coastguard Worker     const int stdIn;  // NOLINT(misc-non-private-member-variables-in-classes)
92*8542734aSAndroid Build Coastguard Worker 
93*8542734aSAndroid Build Coastguard Worker     struct pollfd pollFds[2];
94*8542734aSAndroid Build Coastguard Worker     std::string errBuf;
95*8542734aSAndroid Build Coastguard Worker 
96*8542734aSAndroid Build Coastguard Worker     std::atomic_bool processTerminated;
97*8542734aSAndroid Build Coastguard Worker 
98*8542734aSAndroid Build Coastguard Worker     static constexpr size_t STDOUT_IDX = 0;
99*8542734aSAndroid Build Coastguard Worker     static constexpr size_t STDERR_IDX = 1;
100*8542734aSAndroid Build Coastguard Worker };
101*8542734aSAndroid Build Coastguard Worker 
IptablesRestoreController()102*8542734aSAndroid Build Coastguard Worker IptablesRestoreController::IptablesRestoreController() {
103*8542734aSAndroid Build Coastguard Worker     Init();
104*8542734aSAndroid Build Coastguard Worker }
105*8542734aSAndroid Build Coastguard Worker 
~IptablesRestoreController()106*8542734aSAndroid Build Coastguard Worker IptablesRestoreController::~IptablesRestoreController() {
107*8542734aSAndroid Build Coastguard Worker }
108*8542734aSAndroid Build Coastguard Worker 
Init()109*8542734aSAndroid Build Coastguard Worker void IptablesRestoreController::Init() {
110*8542734aSAndroid Build Coastguard Worker     // We cannot fork these in parallel or a child process could inherit the pipe fds intended for
111*8542734aSAndroid Build Coastguard Worker     // use by the other child process. see https://android-review.googlesource.com/469559 for what
112*8542734aSAndroid Build Coastguard Worker     // breaks. This does not cause a latency hit, because the parent only has to wait for
113*8542734aSAndroid Build Coastguard Worker     // forkAndExec, which is sub-millisecond, and the child processes then call exec() in parallel.
114*8542734aSAndroid Build Coastguard Worker     mIpRestore.reset(forkAndExec(IPTABLES_PROCESS));
115*8542734aSAndroid Build Coastguard Worker     mIp6Restore.reset(forkAndExec(IP6TABLES_PROCESS));
116*8542734aSAndroid Build Coastguard Worker }
117*8542734aSAndroid Build Coastguard Worker 
118*8542734aSAndroid Build Coastguard Worker /* static */
forkAndExec(const IptablesProcessType type)119*8542734aSAndroid Build Coastguard Worker IptablesProcess* IptablesRestoreController::forkAndExec(const IptablesProcessType type) {
120*8542734aSAndroid Build Coastguard Worker     const char* const cmd = (type == IPTABLES_PROCESS) ?
121*8542734aSAndroid Build Coastguard Worker         IPTABLES_RESTORE_PATH : IP6TABLES_RESTORE_PATH;
122*8542734aSAndroid Build Coastguard Worker 
123*8542734aSAndroid Build Coastguard Worker     // Create the pipes we'll use for communication with the child
124*8542734aSAndroid Build Coastguard Worker     // process. One each for the child's in, out and err files.
125*8542734aSAndroid Build Coastguard Worker     int stdin_pipe[2];
126*8542734aSAndroid Build Coastguard Worker     int stdout_pipe[2];
127*8542734aSAndroid Build Coastguard Worker     int stderr_pipe[2];
128*8542734aSAndroid Build Coastguard Worker 
129*8542734aSAndroid Build Coastguard Worker     // Assumes stdin, stdout, stderr are already in use.
130*8542734aSAndroid Build Coastguard Worker     if (pipe2(stdin_pipe,  O_CLOEXEC) == -1 ||
131*8542734aSAndroid Build Coastguard Worker         pipe2(stdout_pipe, O_NONBLOCK | O_CLOEXEC) == -1 ||
132*8542734aSAndroid Build Coastguard Worker         pipe2(stderr_pipe, O_NONBLOCK | O_CLOEXEC) == -1) {
133*8542734aSAndroid Build Coastguard Worker 
134*8542734aSAndroid Build Coastguard Worker         ALOGE("pipe2() failed: %s", strerror(errno));
135*8542734aSAndroid Build Coastguard Worker         return nullptr;
136*8542734aSAndroid Build Coastguard Worker     }
137*8542734aSAndroid Build Coastguard Worker 
138*8542734aSAndroid Build Coastguard Worker     const auto& sys = sSyscalls.get();
139*8542734aSAndroid Build Coastguard Worker     StatusOr<pid_t> child_pid = sys.fork();
140*8542734aSAndroid Build Coastguard Worker     if (!isOk(child_pid)) {
141*8542734aSAndroid Build Coastguard Worker         ALOGE("fork() failed: %s", strerror(child_pid.status().code()));
142*8542734aSAndroid Build Coastguard Worker         return nullptr;
143*8542734aSAndroid Build Coastguard Worker     }
144*8542734aSAndroid Build Coastguard Worker 
145*8542734aSAndroid Build Coastguard Worker     if (child_pid.value() == 0) {
146*8542734aSAndroid Build Coastguard Worker         // The child process. Reads from stdin, writes to stderr and stdout.
147*8542734aSAndroid Build Coastguard Worker 
148*8542734aSAndroid Build Coastguard Worker         // stdin_pipe[0] : The read end of the stdin pipe.
149*8542734aSAndroid Build Coastguard Worker         // stdout_pipe[1] : The write end of the stdout pipe.
150*8542734aSAndroid Build Coastguard Worker         // stderr_pipe[1] : The write end of the stderr pipe.
151*8542734aSAndroid Build Coastguard Worker         // Note: dup2 does not set O_CLOEXEC. std*_pipe[*] is closed by execl.
152*8542734aSAndroid Build Coastguard Worker         if (dup2(stdin_pipe[0], 0) == -1 ||
153*8542734aSAndroid Build Coastguard Worker             dup2(stdout_pipe[1], 1) == -1 ||
154*8542734aSAndroid Build Coastguard Worker             dup2(stderr_pipe[1], 2) == -1) {
155*8542734aSAndroid Build Coastguard Worker             ALOGE("dup2() failed: %s", strerror(errno));
156*8542734aSAndroid Build Coastguard Worker             abort();
157*8542734aSAndroid Build Coastguard Worker         }
158*8542734aSAndroid Build Coastguard Worker 
159*8542734aSAndroid Build Coastguard Worker         if (execl(cmd,
160*8542734aSAndroid Build Coastguard Worker                   cmd,
161*8542734aSAndroid Build Coastguard Worker                   "--noflush",  // Don't flush the whole table.
162*8542734aSAndroid Build Coastguard Worker                   "-w",         // Wait instead of failing if the lock is held.
163*8542734aSAndroid Build Coastguard Worker                   "-v",         // Verbose mode, to make sure our ping is echoed
164*8542734aSAndroid Build Coastguard Worker                                 // back to us.
165*8542734aSAndroid Build Coastguard Worker                   nullptr) == -1) {
166*8542734aSAndroid Build Coastguard Worker             ALOGE("execl(%s, ...) failed: %s", cmd, strerror(errno));
167*8542734aSAndroid Build Coastguard Worker             abort();
168*8542734aSAndroid Build Coastguard Worker         }
169*8542734aSAndroid Build Coastguard Worker 
170*8542734aSAndroid Build Coastguard Worker         // This statement is unreachable. We abort() upon error, and execl
171*8542734aSAndroid Build Coastguard Worker         // if everything goes well.
172*8542734aSAndroid Build Coastguard Worker         return nullptr;
173*8542734aSAndroid Build Coastguard Worker     }
174*8542734aSAndroid Build Coastguard Worker 
175*8542734aSAndroid Build Coastguard Worker     // The parent process.
176*8542734aSAndroid Build Coastguard Worker 
177*8542734aSAndroid Build Coastguard Worker     if (close(stdin_pipe[0]) == -1 ||
178*8542734aSAndroid Build Coastguard Worker         close(stdout_pipe[1]) == -1 ||
179*8542734aSAndroid Build Coastguard Worker         close(stderr_pipe[1]) == -1) {
180*8542734aSAndroid Build Coastguard Worker         ALOGW("close() failed: %s", strerror(errno));
181*8542734aSAndroid Build Coastguard Worker     }
182*8542734aSAndroid Build Coastguard Worker 
183*8542734aSAndroid Build Coastguard Worker     // stdin_pipe[1] : The write end of the stdin pipe.
184*8542734aSAndroid Build Coastguard Worker     // stdout_pipe[0] : The read end of the stdout pipe.
185*8542734aSAndroid Build Coastguard Worker     // stderr_pipe[0] : The read end of the stderr pipe.
186*8542734aSAndroid Build Coastguard Worker     return new IptablesProcess(type,
187*8542734aSAndroid Build Coastguard Worker             child_pid.value(), stdin_pipe[1], stdout_pipe[0], stderr_pipe[0]);
188*8542734aSAndroid Build Coastguard Worker }
189*8542734aSAndroid Build Coastguard Worker 
190*8542734aSAndroid Build Coastguard Worker // TODO: Return -errno on failure instead of -1.
191*8542734aSAndroid Build Coastguard Worker // TODO: Maybe we should keep a rotating buffer of the last N commands
192*8542734aSAndroid Build Coastguard Worker // so that they can be dumped on dumpsys.
sendCommand(const IptablesProcessType type,const std::string & command,std::string * output)193*8542734aSAndroid Build Coastguard Worker int IptablesRestoreController::sendCommand(const IptablesProcessType type,
194*8542734aSAndroid Build Coastguard Worker                                            const std::string& command,
195*8542734aSAndroid Build Coastguard Worker                                            std::string *output) {
196*8542734aSAndroid Build Coastguard Worker    std::unique_ptr<IptablesProcess> *process =
197*8542734aSAndroid Build Coastguard Worker            (type == IPTABLES_PROCESS) ? &mIpRestore : &mIp6Restore;
198*8542734aSAndroid Build Coastguard Worker 
199*8542734aSAndroid Build Coastguard Worker 
200*8542734aSAndroid Build Coastguard Worker     // We might need to fork a new process if we haven't forked one yet, or
201*8542734aSAndroid Build Coastguard Worker     // if the forked process terminated.
202*8542734aSAndroid Build Coastguard Worker     //
203*8542734aSAndroid Build Coastguard Worker     // NOTE: For a given command, this is the last point at which we try to
204*8542734aSAndroid Build Coastguard Worker     // recover from a child death. If the child dies at some later point during
205*8542734aSAndroid Build Coastguard Worker     // the execution of this method, we will receive an EPIPE and return an
206*8542734aSAndroid Build Coastguard Worker     // error. The command will then need to be retried at a higher level.
207*8542734aSAndroid Build Coastguard Worker     IptablesProcess *existingProcess = process->get();
208*8542734aSAndroid Build Coastguard Worker     if (existingProcess != nullptr && !existingProcess->outputReady()) {
209*8542734aSAndroid Build Coastguard Worker         existingProcess->stop();
210*8542734aSAndroid Build Coastguard Worker         existingProcess = nullptr;
211*8542734aSAndroid Build Coastguard Worker     }
212*8542734aSAndroid Build Coastguard Worker 
213*8542734aSAndroid Build Coastguard Worker     if (existingProcess == nullptr) {
214*8542734aSAndroid Build Coastguard Worker         // Fork a new iptables[6]-restore process.
215*8542734aSAndroid Build Coastguard Worker         IptablesProcess *newProcess = IptablesRestoreController::forkAndExec(type);
216*8542734aSAndroid Build Coastguard Worker         if (newProcess == nullptr) {
217*8542734aSAndroid Build Coastguard Worker             LOG(ERROR) << "Unable to fork ip[6]tables-restore, type: " << type;
218*8542734aSAndroid Build Coastguard Worker             return -1;
219*8542734aSAndroid Build Coastguard Worker         }
220*8542734aSAndroid Build Coastguard Worker 
221*8542734aSAndroid Build Coastguard Worker         process->reset(newProcess);
222*8542734aSAndroid Build Coastguard Worker     }
223*8542734aSAndroid Build Coastguard Worker 
224*8542734aSAndroid Build Coastguard Worker     if (!android::base::WriteFully((*process)->stdIn, command.data(), command.length())) {
225*8542734aSAndroid Build Coastguard Worker         ALOGE("Unable to send command: %s", strerror(errno));
226*8542734aSAndroid Build Coastguard Worker         return -1;
227*8542734aSAndroid Build Coastguard Worker     }
228*8542734aSAndroid Build Coastguard Worker 
229*8542734aSAndroid Build Coastguard Worker     if (!android::base::WriteFully((*process)->stdIn, PING, PING_SIZE)) {
230*8542734aSAndroid Build Coastguard Worker         ALOGE("Unable to send ping command: %s", strerror(errno));
231*8542734aSAndroid Build Coastguard Worker         return -1;
232*8542734aSAndroid Build Coastguard Worker     }
233*8542734aSAndroid Build Coastguard Worker 
234*8542734aSAndroid Build Coastguard Worker     if (!drainAndWaitForAck(*process, command, output)) {
235*8542734aSAndroid Build Coastguard Worker         // drainAndWaitForAck has already logged an error.
236*8542734aSAndroid Build Coastguard Worker         return -1;
237*8542734aSAndroid Build Coastguard Worker     }
238*8542734aSAndroid Build Coastguard Worker 
239*8542734aSAndroid Build Coastguard Worker     return 0;
240*8542734aSAndroid Build Coastguard Worker }
241*8542734aSAndroid Build Coastguard Worker 
maybeLogStderr(const std::unique_ptr<IptablesProcess> & process,const std::string & command)242*8542734aSAndroid Build Coastguard Worker void IptablesRestoreController::maybeLogStderr(const std::unique_ptr<IptablesProcess> &process,
243*8542734aSAndroid Build Coastguard Worker                                                const std::string& command) {
244*8542734aSAndroid Build Coastguard Worker     if (process->errBuf.empty()) {
245*8542734aSAndroid Build Coastguard Worker         return;
246*8542734aSAndroid Build Coastguard Worker     }
247*8542734aSAndroid Build Coastguard Worker 
248*8542734aSAndroid Build Coastguard Worker     ALOGE("iptables error:");
249*8542734aSAndroid Build Coastguard Worker     ALOGE("------- COMMAND -------");
250*8542734aSAndroid Build Coastguard Worker     ALOGE("%s", command.c_str());
251*8542734aSAndroid Build Coastguard Worker     ALOGE("-------  ERROR -------");
252*8542734aSAndroid Build Coastguard Worker     ALOGE("%s", process->errBuf.c_str());
253*8542734aSAndroid Build Coastguard Worker     ALOGE("----------------------");
254*8542734aSAndroid Build Coastguard Worker     process->errBuf.clear();
255*8542734aSAndroid Build Coastguard Worker }
256*8542734aSAndroid Build Coastguard Worker 
257*8542734aSAndroid Build Coastguard Worker /* static */
drainAndWaitForAck(const std::unique_ptr<IptablesProcess> & process,const std::string & command,std::string * output)258*8542734aSAndroid Build Coastguard Worker bool IptablesRestoreController::drainAndWaitForAck(const std::unique_ptr<IptablesProcess> &process,
259*8542734aSAndroid Build Coastguard Worker                                                    const std::string& command,
260*8542734aSAndroid Build Coastguard Worker                                                    std::string *output) {
261*8542734aSAndroid Build Coastguard Worker     bool receivedAck = false;
262*8542734aSAndroid Build Coastguard Worker     int timeout = 0;
263*8542734aSAndroid Build Coastguard Worker     while (!receivedAck && (timeout++ < MAX_RETRIES)) {
264*8542734aSAndroid Build Coastguard Worker         int numEvents = TEMP_FAILURE_RETRY(
265*8542734aSAndroid Build Coastguard Worker             poll(process->pollFds, ARRAY_SIZE(process->pollFds), POLL_TIMEOUT_MS));
266*8542734aSAndroid Build Coastguard Worker         if (numEvents == -1) {
267*8542734aSAndroid Build Coastguard Worker             ALOGE("Poll failed: %s", strerror(errno));
268*8542734aSAndroid Build Coastguard Worker             return false;
269*8542734aSAndroid Build Coastguard Worker         }
270*8542734aSAndroid Build Coastguard Worker 
271*8542734aSAndroid Build Coastguard Worker         // We've timed out, which means something has gone wrong - we know that stdout should have
272*8542734aSAndroid Build Coastguard Worker         // become available to read with the ACK message, or that stderr should have been available
273*8542734aSAndroid Build Coastguard Worker         // to read with an error message.
274*8542734aSAndroid Build Coastguard Worker         if (numEvents == 0) {
275*8542734aSAndroid Build Coastguard Worker             continue;
276*8542734aSAndroid Build Coastguard Worker         }
277*8542734aSAndroid Build Coastguard Worker 
278*8542734aSAndroid Build Coastguard Worker         char buffer[PIPE_BUF];
279*8542734aSAndroid Build Coastguard Worker         for (size_t i = 0; i < ARRAY_SIZE(process->pollFds); ++i) {
280*8542734aSAndroid Build Coastguard Worker             const struct pollfd &pollfd = process->pollFds[i];
281*8542734aSAndroid Build Coastguard Worker             if (pollfd.revents & POLLIN) {
282*8542734aSAndroid Build Coastguard Worker                 ssize_t size;
283*8542734aSAndroid Build Coastguard Worker                 do {
284*8542734aSAndroid Build Coastguard Worker                     size = TEMP_FAILURE_RETRY(read(pollfd.fd, buffer, sizeof(buffer)));
285*8542734aSAndroid Build Coastguard Worker 
286*8542734aSAndroid Build Coastguard Worker                     if (size == -1) {
287*8542734aSAndroid Build Coastguard Worker                         if (errno != EAGAIN) {
288*8542734aSAndroid Build Coastguard Worker                             ALOGE("Unable to read from descriptor: %s", strerror(errno));
289*8542734aSAndroid Build Coastguard Worker                         }
290*8542734aSAndroid Build Coastguard Worker                         break;
291*8542734aSAndroid Build Coastguard Worker                     }
292*8542734aSAndroid Build Coastguard Worker 
293*8542734aSAndroid Build Coastguard Worker                     if (i == IptablesProcess::STDOUT_IDX) {
294*8542734aSAndroid Build Coastguard Worker                         // i == STDOUT_IDX: accumulate stdout into *output, and look
295*8542734aSAndroid Build Coastguard Worker                         // for the ping response.
296*8542734aSAndroid Build Coastguard Worker                         output->append(buffer, size);
297*8542734aSAndroid Build Coastguard Worker                         size_t pos = output->find(PING);
298*8542734aSAndroid Build Coastguard Worker                         if (pos != std::string::npos) {
299*8542734aSAndroid Build Coastguard Worker                             if (output->size() > pos + PING_SIZE) {
300*8542734aSAndroid Build Coastguard Worker                                 size_t extra = output->size() - (pos + PING_SIZE);
301*8542734aSAndroid Build Coastguard Worker                                 ALOGW("%zd extra characters after iptables response: '%s...'",
302*8542734aSAndroid Build Coastguard Worker                                       extra, output->substr(pos + PING_SIZE, 128).c_str());
303*8542734aSAndroid Build Coastguard Worker                             }
304*8542734aSAndroid Build Coastguard Worker                             output->resize(pos);
305*8542734aSAndroid Build Coastguard Worker                             receivedAck = true;
306*8542734aSAndroid Build Coastguard Worker                         }
307*8542734aSAndroid Build Coastguard Worker                     } else {
308*8542734aSAndroid Build Coastguard Worker                         // i == STDERR_IDX: accumulate stderr into errBuf.
309*8542734aSAndroid Build Coastguard Worker                         process->errBuf.append(buffer, size);
310*8542734aSAndroid Build Coastguard Worker                     }
311*8542734aSAndroid Build Coastguard Worker                 } while (size > 0);
312*8542734aSAndroid Build Coastguard Worker             }
313*8542734aSAndroid Build Coastguard Worker             if (pollfd.revents & POLLHUP) {
314*8542734aSAndroid Build Coastguard Worker                 // The pipe was closed. This likely means the subprocess is exiting, since
315*8542734aSAndroid Build Coastguard Worker                 // iptables-restore only closes stdin on error.
316*8542734aSAndroid Build Coastguard Worker                 process->stop();
317*8542734aSAndroid Build Coastguard Worker                 break;
318*8542734aSAndroid Build Coastguard Worker             }
319*8542734aSAndroid Build Coastguard Worker         }
320*8542734aSAndroid Build Coastguard Worker     }
321*8542734aSAndroid Build Coastguard Worker 
322*8542734aSAndroid Build Coastguard Worker     if (!receivedAck && !process->processTerminated) {
323*8542734aSAndroid Build Coastguard Worker         ALOGE("Timed out waiting for response from iptables process %d", process->pid);
324*8542734aSAndroid Build Coastguard Worker         // Kill the process so that if it eventually recovers, we don't misinterpret the ping
325*8542734aSAndroid Build Coastguard Worker         // response (or any output) of the command we just sent as coming from future commands.
326*8542734aSAndroid Build Coastguard Worker         process->stop();
327*8542734aSAndroid Build Coastguard Worker     }
328*8542734aSAndroid Build Coastguard Worker 
329*8542734aSAndroid Build Coastguard Worker     maybeLogStderr(process, command);
330*8542734aSAndroid Build Coastguard Worker 
331*8542734aSAndroid Build Coastguard Worker     return receivedAck;
332*8542734aSAndroid Build Coastguard Worker }
333*8542734aSAndroid Build Coastguard Worker 
execute(const IptablesTarget target,const std::string & command,std::string * output)334*8542734aSAndroid Build Coastguard Worker int IptablesRestoreController::execute(const IptablesTarget target, const std::string& command,
335*8542734aSAndroid Build Coastguard Worker                                        std::string *output) {
336*8542734aSAndroid Build Coastguard Worker     std::lock_guard lock(mLock);
337*8542734aSAndroid Build Coastguard Worker 
338*8542734aSAndroid Build Coastguard Worker     std::string buffer;
339*8542734aSAndroid Build Coastguard Worker     if (output == nullptr) {
340*8542734aSAndroid Build Coastguard Worker         output = &buffer;
341*8542734aSAndroid Build Coastguard Worker     } else {
342*8542734aSAndroid Build Coastguard Worker         output->clear();
343*8542734aSAndroid Build Coastguard Worker     }
344*8542734aSAndroid Build Coastguard Worker 
345*8542734aSAndroid Build Coastguard Worker     int res = 0;
346*8542734aSAndroid Build Coastguard Worker     if (target == V4 || target == V4V6) {
347*8542734aSAndroid Build Coastguard Worker         res |= sendCommand(IPTABLES_PROCESS, command, output);
348*8542734aSAndroid Build Coastguard Worker     }
349*8542734aSAndroid Build Coastguard Worker     if (target == V6 || target == V4V6) {
350*8542734aSAndroid Build Coastguard Worker         res |= sendCommand(IP6TABLES_PROCESS, command, output);
351*8542734aSAndroid Build Coastguard Worker     }
352*8542734aSAndroid Build Coastguard Worker     return res;
353*8542734aSAndroid Build Coastguard Worker }
354*8542734aSAndroid Build Coastguard Worker 
getIpRestorePid(const IptablesProcessType type)355*8542734aSAndroid Build Coastguard Worker int IptablesRestoreController::getIpRestorePid(const IptablesProcessType type) {
356*8542734aSAndroid Build Coastguard Worker     return type == IPTABLES_PROCESS ? mIpRestore->pid : mIp6Restore->pid;
357*8542734aSAndroid Build Coastguard Worker }
358