1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "executer_utils.h"
17 
18 #include "edm_log.h"
19 #include "netsys_controller.h"
20 
21 namespace OHOS {
22 namespace EDM {
23 namespace IPTABLES {
24 
25 std::shared_ptr<ExecuterUtils> ExecuterUtils::instance_ = nullptr;
26 std::mutex ExecuterUtils::mutexLock_;
GetInstance()27 std::shared_ptr<ExecuterUtils> ExecuterUtils::GetInstance()
28 {
29     if (instance_ == nullptr) {
30         std::lock_guard<std::mutex> lock(mutexLock_);
31         if (instance_ == nullptr) {
32             std::shared_ptr<ExecuterUtils> temp = std::make_shared<ExecuterUtils>();
33             instance_ = temp;
34         }
35     }
36     return instance_;
37 }
38 
Execute(const std::string & rule,std::string & result)39 ErrCode ExecuterUtils::Execute(const std::string& rule, std::string &result)
40 {
41     EDMLOGD("ExecuterUtils Execute:%{public}s", rule.c_str());
42     ErrCode ret = NetManagerStandard::NetsysController::GetInstance().SetIptablesCommandForRes(rule, result);
43     if (ret != ERR_OK) {
44         if (!result.empty()) {
45             EDMLOGE("ExecuterUtils Execute fail:%{public}d, %{public}s", ret, result.c_str());
46         } else {
47             EDMLOGE("ExecuterUtils Execute fail:%{public}d", ret);
48         }
49         return ret;
50     }
51     return ERR_OK;
52 }
53 } // namespace IPTABLES
54 } // namespace EDM
55 } // namespace OHOS