blob: 4f58461d25568650c1ab2407ee57991f75ee3e03 [file] [log] [blame]
Narayan Kamatha5ace892017-01-06 15:10:02 +00001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef NETD_SERVER_IPTABLES_RESTORE_CONTROLLER_H
18#define NETD_SERVER_IPTABLES_RESTORE_CONTROLLER_H
19
20#include <memory>
21#include <mutex>
22#include <sys/types.h>
23
24#include "NetdConstants.h"
25
26class IptablesProcess;
27
28class IptablesRestoreController {
29public:
30 // Not for general use. Use gCtls->iptablesRestoreCtrl
31 // to get an instance of this class.
32 IptablesRestoreController();
33
Narayan Kamatha5ace892017-01-06 15:10:02 +000034 // Execute |commands| on the given |target|.
35 int execute(const IptablesTarget target, const std::string& commands);
36
Lorenzo Colitticd283772017-01-31 19:00:49 +090037 // Execute |commands| on the given |target|, and populate |output| with stdout.
38 int execute(const IptablesTarget target, const std::string& commands, std::string *output);
39
Narayan Kamatha5ace892017-01-06 15:10:02 +000040 enum IptablesProcessType {
41 IPTABLES_PROCESS,
42 IP6TABLES_PROCESS,
43 INVALID_PROCESS = -1,
44 };
45
46 // Called by the SIGCHLD signal handler when it detects that one
47 // of the forked iptables[6]-restore process has died.
48 IptablesProcessType notifyChildTermination(pid_t pid);
49
50 virtual ~IptablesRestoreController();
51
Lorenzo Colitti173da322017-02-05 01:56:40 +090052protected:
53 friend class IptablesRestoreControllerTest;
54 pid_t getIpRestorePid(const IptablesProcessType type);
55
Lorenzo Colittia701afb2017-02-28 01:47:11 +090056 // The maximum number of times we poll(2) for a response on our set of polled
57 // fds. Chosen so that the overall timeout is 5s. The timeout is so high because
58 // our version of iptables still polls every second in xtables_lock.
59 static int MAX_RETRIES;
60
61 // The timeout (in millis) for each call to poll. The maximum wait is
62 // |POLL_TIMEOUT_MS * MAX_RETRIES|. Chosen so that the overall timeout is 1s.
63 static int POLL_TIMEOUT_MS;
64
Narayan Kamatha5ace892017-01-06 15:10:02 +000065private:
66 static IptablesProcess* forkAndExec(const IptablesProcessType type);
67
Lorenzo Colitticd283772017-01-31 19:00:49 +090068 int sendCommand(const IptablesProcessType type, const std::string& command,
69 std::string *output);
Narayan Kamatha5ace892017-01-06 15:10:02 +000070
Lorenzo Colitti25091422017-02-03 16:05:28 +090071 static bool drainAndWaitForAck(const std::unique_ptr<IptablesProcess> &process,
72 const std::string& command,
73 std::string *output);
Narayan Kamatha5ace892017-01-06 15:10:02 +000074
75 static void maybeLogStderr(const std::unique_ptr<IptablesProcess> &process,
Lorenzo Colitti25091422017-02-03 16:05:28 +090076 const std::string& command);
Narayan Kamatha5ace892017-01-06 15:10:02 +000077
Narayan Kamatha5ace892017-01-06 15:10:02 +000078 // Guards calls to execute().
79 std::mutex mLock;
Lorenzo Colitti173da322017-02-05 01:56:40 +090080
81 std::unique_ptr<IptablesProcess> mIpRestore;
82 std::unique_ptr<IptablesProcess> mIp6Restore;
Narayan Kamatha5ace892017-01-06 15:10:02 +000083};
84
85#endif // NETD_SERVER_IPTABLES_RESTORE_CONTROLLER_H