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