1 /*
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 
26 class IptablesProcess;
27 
28 class IptablesRestoreController {
29 public:
30     // Not for general use. Use gCtls->iptablesRestoreCtrl
31     // to get an instance of this class.
32     IptablesRestoreController();
33 
34     // Execute |commands| on the given |target|.
35     int execute(const IptablesTarget target, const std::string& commands);
36 
37     // 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 
40     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 
52 protected:
53     friend class IptablesRestoreControllerTest;
54     pid_t getIpRestorePid(const IptablesProcessType type);
55 
56     // 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 
65 private:
66     static IptablesProcess* forkAndExec(const IptablesProcessType type);
67 
68     int sendCommand(const IptablesProcessType type, const std::string& command,
69                     std::string *output);
70 
71     static bool drainAndWaitForAck(const std::unique_ptr<IptablesProcess> &process,
72                                    const std::string& command,
73                                    std::string *output);
74 
75     static void maybeLogStderr(const std::unique_ptr<IptablesProcess> &process,
76                                const std::string& command);
77 
78     // Guards calls to execute().
79     std::mutex mLock;
80 
81     std::unique_ptr<IptablesProcess> mIpRestore;
82     std::unique_ptr<IptablesProcess> mIp6Restore;
83 };
84 
85 #endif  // NETD_SERVER_IPTABLES_RESTORE_CONTROLLER_H
86