• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "volume/process.h"
17 #include <unistd.h>
18 #include <csignal>
19 #include <cerrno>
20 #include <sys/types.h>
21 #include <dirent.h>
22 #include "storage_service_log.h"
23 #include "storage_service_errno.h"
24 #include "utils/string_utils.h"
25 
26 using namespace std;
27 
28 namespace OHOS {
29 namespace StorageDaemon {
Process(std::string path)30 Process::Process(std::string path)
31 {
32     path_ = path;
33 }
34 
GetPids()35 std::unordered_set<pid_t> Process::GetPids()
36 {
37     return pids_;
38 }
39 
GetPath()40 std::string Process::GetPath()
41 {
42     return path_;
43 }
44 
Readlink(std::string path)45 std::string Process::Readlink(std::string path)
46 {
47     int len = 0;
48     int size = 0;
49     int growlen = 64;
50     std::string buf;
51 
52     do {
53         size += growlen;
54         buf.assign(std::string(size, '\0'));
55         len = readlink(path.c_str(), buf.data(), size);
56         if (len == -1) {
57             if (errno != ENOENT) {
58                 LOGE("readlink %{public}s failed, errno: %{public}d", path.c_str(), errno);
59             }
60             return "";
61         }
62     } while (size <= len);
63 
64     return buf;
65 }
66 
CheckSubDir(std::string subdir)67 bool Process::CheckSubDir(std::string subdir)
68 {
69     const char *p = path_.c_str();
70     const char *q = subdir.c_str();
71 
72     while (*p != '\0' && *q != '\0') {
73         if (*p != *q) {
74             return false;
75         }
76         p++;
77         q++;
78     }
79 
80     if (*p == '\0' && *q == '\0') {
81         return true;
82     }
83 
84     if (*p == '\0' && *q == '/') {
85         return true;
86     }
87 
88     return false;
89 }
90 
CheckMaps(std::string pidPath)91 bool Process::CheckMaps(std::string pidPath)
92 {
93     char *buf = nullptr;
94     size_t lineLen = 0;
95     std::string line;
96     auto path = StringPrintf("%s/maps", pidPath.c_str());
97     FILE *file = fopen(path.c_str(), "r");
98     if (file == nullptr) {
99         return false;
100     }
101 
102     while (getline(&buf, &lineLen, file) > 0) {
103         line = buf;
104         std::string::size_type pos = line.find('/');
105         if (pos != line.npos) {
106             line = line.substr(pos);
107             if (CheckSubDir(line)) {
108                 LOGI("Found map in %{public}s", pidPath.c_str());
109                 (void)fclose(file);
110                 return true;
111             }
112         }
113     }
114 
115     (void)fclose(file);
116     return false;
117 }
118 
CheckSymlink(std::string path)119 bool Process::CheckSymlink(std::string path)
120 {
121     std::string link = Readlink(path);
122     if (!link.empty() && CheckSubDir(link)) {
123         return true;
124     }
125     return false;
126 }
127 
CheckFds(std::string pidPath)128 bool Process::CheckFds(std::string pidPath)
129 {
130     struct dirent *dirEntry;
131     auto path = StringPrintf("%s/fd", pidPath.c_str());
132     DIR *dir = opendir(path.c_str());
133     if (dir == nullptr) {
134         return E_ERR;
135     }
136 
137     while ((dirEntry = readdir(dir)) != nullptr) {
138         if (dirEntry->d_type != DT_LNK) continue;
139         if (CheckSymlink(path + "/" + dirEntry->d_name)) {
140             closedir(dir);
141             return true;
142         }
143     }
144 
145     closedir(dir);
146     return false;
147 }
148 
UpdatePidByPath()149 int32_t Process::UpdatePidByPath()
150 {
151     struct dirent *dirEntry;
152     DIR *dir = opendir("/proc");
153     if (dir == nullptr) {
154         return E_ERR;
155     }
156 
157     while ((dirEntry = readdir(dir)) != nullptr) {
158         if (dirEntry->d_type != DT_DIR) continue;
159         pid_t pid = atoi(dirEntry->d_name);
160         if (pid > 0 && pid != getpid()) {
161             std::string pidPath = StringPrintf("/proc/%d", pid);
162             if (CheckMaps(pidPath)
163                 || CheckSymlink(pidPath + "/cwd")
164                 || CheckSymlink(pidPath + "/root")
165                 || CheckSymlink(pidPath + "/exe")
166                 || CheckFds(pidPath)) {
167                 pids_.insert(pid);
168             }
169         }
170     }
171 
172     closedir(dir);
173     return E_OK;
174 }
175 
KillProcess(int signal)176 void Process::KillProcess(int signal)
177 {
178     if (signal == 0) {
179         return;
180     }
181 
182     for (const auto& pid : pids_) {
183         LOGI("KILL PID %{public}d", pid);
184         kill(pid, signal);
185     }
186     pids_.clear();
187 }
188 } // StorageDaemon
189 } // OHOS