blob: 8f2dc30403753f3425909b6d7f0c16980170a3af [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
34 // Called precisely once in netd's lifetime, when the singleton
35 // Controllers object is created.
36 static void installSignalHandler(IptablesRestoreController *singleton);
37
38 // Execute |commands| on the given |target|.
39 int execute(const IptablesTarget target, const std::string& commands);
40
41 enum IptablesProcessType {
42 IPTABLES_PROCESS,
43 IP6TABLES_PROCESS,
44 INVALID_PROCESS = -1,
45 };
46
47 // Called by the SIGCHLD signal handler when it detects that one
48 // of the forked iptables[6]-restore process has died.
49 IptablesProcessType notifyChildTermination(pid_t pid);
50
51 virtual ~IptablesRestoreController();
52
53private:
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
65 std::unique_ptr<IptablesProcess> mIpRestore;
66 std::unique_ptr<IptablesProcess> mIp6Restore;
67
68 // Guards calls to execute().
69 std::mutex mLock;
70};
71
72#endif // NETD_SERVER_IPTABLES_RESTORE_CONTROLLER_H