blob: f7ae292d8f18968adb147590040fd64051b37532 [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
Narayan Kamatha5ace892017-01-06 15:10:02 +000056private:
57 static IptablesProcess* forkAndExec(const IptablesProcessType type);
58
Lorenzo Colitticd283772017-01-31 19:00:49 +090059 int sendCommand(const IptablesProcessType type, const std::string& command,
60 std::string *output);
Narayan Kamatha5ace892017-01-06 15:10:02 +000061
62 static std::string fixCommandString(const std::string& command);
63
Lorenzo Colitticd283772017-01-31 19:00:49 +090064 bool drainAndWaitForAck(const std::unique_ptr<IptablesProcess> &process, std::string *output);
Narayan Kamatha5ace892017-01-06 15:10:02 +000065
66 static void maybeLogStderr(const std::unique_ptr<IptablesProcess> &process,
67 const char* buf, const ssize_t numBytes);
68
Narayan Kamatha5ace892017-01-06 15:10:02 +000069 // Guards calls to execute().
70 std::mutex mLock;
Lorenzo Colitti173da322017-02-05 01:56:40 +090071
72 std::unique_ptr<IptablesProcess> mIpRestore;
73 std::unique_ptr<IptablesProcess> mIp6Restore;
Narayan Kamatha5ace892017-01-06 15:10:02 +000074};
75
76#endif // NETD_SERVER_IPTABLES_RESTORE_CONTROLLER_H