blob: 4279facee3063d21bac3e60f2932918bbb4178a6 [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
37 enum IptablesProcessType {
38 IPTABLES_PROCESS,
39 IP6TABLES_PROCESS,
40 INVALID_PROCESS = -1,
41 };
42
43 // Called by the SIGCHLD signal handler when it detects that one
44 // of the forked iptables[6]-restore process has died.
45 IptablesProcessType notifyChildTermination(pid_t pid);
46
47 virtual ~IptablesRestoreController();
48
Lorenzo Colitti173da322017-02-05 01:56:40 +090049protected:
50 friend class IptablesRestoreControllerTest;
51 pid_t getIpRestorePid(const IptablesProcessType type);
52
Narayan Kamatha5ace892017-01-06 15:10:02 +000053private:
54 static IptablesProcess* forkAndExec(const IptablesProcessType type);
55
56 int sendCommand(const IptablesProcessType type, const std::string& command);
57
58 static std::string fixCommandString(const std::string& command);
59
60 bool drainAndWaitForAck(const std::unique_ptr<IptablesProcess> &process);
61
62 static void maybeLogStderr(const std::unique_ptr<IptablesProcess> &process,
63 const char* buf, const ssize_t numBytes);
64
Narayan Kamatha5ace892017-01-06 15:10:02 +000065 // Guards calls to execute().
66 std::mutex mLock;
Lorenzo Colitti173da322017-02-05 01:56:40 +090067
68 std::unique_ptr<IptablesProcess> mIpRestore;
69 std::unique_ptr<IptablesProcess> mIp6Restore;
Narayan Kamatha5ace892017-01-06 15:10:02 +000070};
71
72#endif // NETD_SERVER_IPTABLES_RESTORE_CONTROLLER_H