blob: 6850d0de2fade5469a1304fbe715e1652c6186ca [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
Joel Scherpelz08b84cd2017-05-22 13:11:54 +090028class IptablesRestoreInterface {
29 public:
30 virtual ~IptablesRestoreInterface() = default;
31
32 // Execute |commands| on the given |target|, and populate |output| with stdout.
33 virtual int execute(const IptablesTarget target, const std::string& commands,
34 std::string* output) = 0;
35};
36
37class IptablesRestoreController final : public IptablesRestoreInterface {
38 public:
Narayan Kamatha5ace892017-01-06 15:10:02 +000039 // Not for general use. Use gCtls->iptablesRestoreCtrl
40 // to get an instance of this class.
41 IptablesRestoreController();
42
Joel Scherpelz08b84cd2017-05-22 13:11:54 +090043 ~IptablesRestoreController() override;
Narayan Kamatha5ace892017-01-06 15:10:02 +000044
Joel Scherpelz08b84cd2017-05-22 13:11:54 +090045 int execute(const IptablesTarget target, const std::string& commands,
46 std::string* output) override;
Lorenzo Colitticd283772017-01-31 19:00:49 +090047
Narayan Kamatha5ace892017-01-06 15:10:02 +000048 enum IptablesProcessType {
49 IPTABLES_PROCESS,
50 IP6TABLES_PROCESS,
51 INVALID_PROCESS = -1,
52 };
53
54 // Called by the SIGCHLD signal handler when it detects that one
55 // of the forked iptables[6]-restore process has died.
56 IptablesProcessType notifyChildTermination(pid_t pid);
57
Lorenzo Colitti173da322017-02-05 01:56:40 +090058protected:
59 friend class IptablesRestoreControllerTest;
60 pid_t getIpRestorePid(const IptablesProcessType type);
61
Lorenzo Colittia701afb2017-02-28 01:47:11 +090062 // The maximum number of times we poll(2) for a response on our set of polled
63 // fds. Chosen so that the overall timeout is 5s. The timeout is so high because
64 // our version of iptables still polls every second in xtables_lock.
65 static int MAX_RETRIES;
66
67 // The timeout (in millis) for each call to poll. The maximum wait is
68 // |POLL_TIMEOUT_MS * MAX_RETRIES|. Chosen so that the overall timeout is 1s.
69 static int POLL_TIMEOUT_MS;
70
Narayan Kamatha5ace892017-01-06 15:10:02 +000071private:
72 static IptablesProcess* forkAndExec(const IptablesProcessType type);
73
Lorenzo Colitticd283772017-01-31 19:00:49 +090074 int sendCommand(const IptablesProcessType type, const std::string& command,
75 std::string *output);
Narayan Kamatha5ace892017-01-06 15:10:02 +000076
Lorenzo Colitti25091422017-02-03 16:05:28 +090077 static bool drainAndWaitForAck(const std::unique_ptr<IptablesProcess> &process,
78 const std::string& command,
79 std::string *output);
Narayan Kamatha5ace892017-01-06 15:10:02 +000080
81 static void maybeLogStderr(const std::unique_ptr<IptablesProcess> &process,
Lorenzo Colitti25091422017-02-03 16:05:28 +090082 const std::string& command);
Narayan Kamatha5ace892017-01-06 15:10:02 +000083
Narayan Kamatha5ace892017-01-06 15:10:02 +000084 // Guards calls to execute().
85 std::mutex mLock;
Lorenzo Colitti173da322017-02-05 01:56:40 +090086
87 std::unique_ptr<IptablesProcess> mIpRestore;
88 std::unique_ptr<IptablesProcess> mIp6Restore;
Narayan Kamatha5ace892017-01-06 15:10:02 +000089};
90
91#endif // NETD_SERVER_IPTABLES_RESTORE_CONTROLLER_H