1 /*
2 * Copyright (c) 2024 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 <errno.h>
17 #include <inttypes.h>
18 #include <limits.h>
19 #include <fcntl.h>
20 #include <signal.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27
28 #include "appspawn_adapter.h"
29 #include "appspawn_server.h"
30 #include "appspawn_service.h"
31 #include "securec.h"
32
MakeDirRecursive(const char * path,mode_t mode)33 static int MakeDirRecursive(const char *path, mode_t mode)
34 {
35 if (path == NULL || *path == '\0') {
36 return -1;
37 }
38
39 char buffer[PATH_MAX] = {0};
40 const char slash = '/';
41 const char *p = path;
42 char *curPos = strchr(path, slash);
43 while (curPos != NULL) {
44 int len = curPos - p;
45 p = curPos + 1;
46 if (len == 0) {
47 curPos = strchr(p, slash);
48 continue;
49 }
50 if (len < 0) {
51 break;
52 }
53 if (memcpy_s(buffer, PATH_MAX, path, p - path - 1) != 0) {
54 return -1;
55 }
56 int ret = mkdir(buffer, mode);
57 if (ret == -1 && errno != EEXIST) {
58 return errno;
59 }
60 curPos = strchr(p, slash);
61 }
62 if (mkdir(path, mode) == -1 && errno != EEXIST) {
63 return errno;
64 }
65 return 0;
66 }
67
GetCgroupPath(const AppSpawnAppInfo * appInfo,char * buffer,uint32_t buffLen)68 APPSPAWN_STATIC int GetCgroupPath(const AppSpawnAppInfo *appInfo, char *buffer, uint32_t buffLen)
69 {
70 const int userId = appInfo->uid / 200000; // 200000 uid base
71 #ifdef APPSPAWN_TEST
72 int ret = snprintf_s(buffer, buffLen, buffLen - 1, SOCKET_DIR "/dev/pids/testpids/%d/%s/%d/",
73 userId, appInfo->name, appInfo->pid);
74 #else
75 int ret = snprintf_s(buffer, buffLen, buffLen - 1, "/dev/pids/%d/%s/app_%d/", userId, appInfo->name, appInfo->pid);
76 #endif
77 APPSPAWN_CHECK(ret > 0, return ret, "Failed to snprintf_s errno: %{public}d", errno);
78 APPSPAWN_LOGV("Cgroup path %{public}s ", buffer);
79 return 0;
80 }
81
WriteToFile(const char * path,int truncated,pid_t pids[],uint32_t count)82 static int WriteToFile(const char *path, int truncated, pid_t pids[], uint32_t count)
83 {
84 char pidName[32] = {0}; // 32 max len
85 int fd = open(path, O_RDWR | (truncated ? O_TRUNC : O_APPEND));
86 APPSPAWN_CHECK(fd >= 0, return -1,
87 "Failed to open file errno: %{public}d path: %{public}s", errno, path);
88 int ret = 0;
89 for (uint32_t i = 0; i < count; i++) {
90 APPSPAWN_LOGV(" WriteToFile pid %{public}d ", pids[i]);
91 if (pids[i] == 0) {
92 continue;
93 }
94 ret = snprintf_s(pidName, sizeof(pidName), sizeof(pidName) - 1, "%d\n", pids[i]);
95 APPSPAWN_CHECK(ret > 0, break, "Failed to snprintf_s errno: %{public}d", errno);
96 ret = write(fd, pidName, strlen(pidName));
97 APPSPAWN_CHECK(ret > 0, break,
98 "Failed to write file errno: %{public}d path: %{public}s %{public}s %{public}d", errno, path, pidName, ret);
99 ret = 0;
100 }
101 close(fd);
102 return ret;
103 }
104
KillProcessesByCGroup(const char * path,const AppSpawnContentExt * content,const AppSpawnAppInfo * appInfo)105 static void KillProcessesByCGroup(const char *path, const AppSpawnContentExt *content, const AppSpawnAppInfo *appInfo)
106 {
107 FILE *file = fopen(path, "r");
108 APPSPAWN_CHECK(file != NULL, return, "Open file fail %{public}s errno: %{public}d", path, errno);
109 pid_t pid = 0;
110 while (fscanf_s(file, "%d\n", &pid) == 1 && pid > 0) {
111 APPSPAWN_LOGV(" KillProcessesByCGroup pid %{public}d ", pid);
112 if (pid == appInfo->pid) {
113 continue;
114 }
115 AppSpawnAppInfo *tmp = GetAppInfo(pid);
116 if (tmp != NULL) {
117 APPSPAWN_LOGI("Got app %{public}s in same group for pid %{public}d.", tmp->name, pid);
118 continue;
119 }
120 APPSPAWN_LOGI("Kill app pid %{public}d now ...", pid);
121 kill(pid, SIGKILL);
122 }
123 (void)fclose(file);
124 }
125
ProcessAppDied(const AppSpawnContentExt * content,const AppSpawnAppInfo * appInfo)126 int ProcessAppDied(const AppSpawnContentExt *content, const AppSpawnAppInfo *appInfo)
127 {
128 APPSPAWN_CHECK_ONLY_EXPER(content != NULL, return -1);
129 APPSPAWN_CHECK_ONLY_EXPER(appInfo != NULL, return -1);
130 char path[PATH_MAX] = {};
131 APPSPAWN_LOGV("ProcessAppDied %{public}d %{public}d to cgroup ", appInfo->pid, appInfo->uid);
132 int ret = GetCgroupPath(appInfo, path, sizeof(path));
133 APPSPAWN_CHECK(ret == 0, return -1, "Failed to get real path errno: %d", errno);
134 ret = strcat_s(path, sizeof(path), "cgroup.procs");
135 APPSPAWN_CHECK(ret == 0, return ret, "Failed to strcat_s errno: %{public}d", errno);
136 KillProcessesByCGroup(path, content, appInfo);
137 return ret;
138 }
139
ProcessAppAdd(const AppSpawnContentExt * content,const AppSpawnAppInfo * appInfo)140 int ProcessAppAdd(const AppSpawnContentExt *content, const AppSpawnAppInfo *appInfo)
141 {
142 APPSPAWN_CHECK_ONLY_EXPER(content != NULL, return -1);
143 APPSPAWN_CHECK_ONLY_EXPER(appInfo != NULL, return -1);
144 if (content->content.isNweb) {
145 return 0;
146 }
147 char path[PATH_MAX] = {};
148 APPSPAWN_LOGV("ProcessAppAdd %{public}d %{public}d to cgroup ", appInfo->pid, appInfo->uid);
149 int ret = GetCgroupPath(appInfo, path, sizeof(path));
150 APPSPAWN_CHECK(ret == 0, return -1, "Failed to get real path errno: %d", errno);
151 (void)MakeDirRecursive(path, 0755); // 0755 default mode
152 ret = strcat_s(path, sizeof(path), "cgroup.procs");
153 APPSPAWN_CHECK(ret == 0, return ret, "Failed to strcat_s errno: %{public}d", errno);
154 ret = WriteToFile(path, 0, (pid_t *)&appInfo->pid, 1);
155 APPSPAWN_CHECK(ret == 0, return ret, "write pid to cgroup.procs fail %{public}s", path);
156 APPSPAWN_LOGV("Add app %{public}d to cgroup %{public}s success", appInfo->pid, path);
157 return 0;
158 }
159