2 * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 * @desc wrapper for reading profs information.
22 * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved.
33 #include <sys/types.h>
37 #include "stc-error.h"
38 #include "stc-manager-util.h"
39 #include "helper-procfs.h"
41 #define USRAPPS "/usr/apps/"
43 API int proc_get_cmdline(pid_t pid, char *cmdline)
45 char buf[PROC_BUF_MAX];
46 char cmdline_buf[PROC_NAME_MAX];
52 snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
55 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
57 if (fgets(cmdline_buf, PROC_NAME_MAX-1, fp) == NULL) {
58 fclose(fp); //LCOV_EXCL_LINE
59 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
63 if (g_strstr_len(cmdline_buf, strlen(USRAPPS), USRAPPS) != NULL) {
65 filename = cmdline_buf + strlen(USRAPPS);
66 token = strtok_r(filename, "/", &saveptr);
70 token = strtok_r(cmdline_buf, " ", &saveptr);
72 filename = strrchr(token, '/');
74 filename = strrchr(cmdline_buf, '/');
77 filename = cmdline_buf;
79 filename = filename + 1;
82 strncpy(cmdline, filename, PROC_NAME_MAX - 1);
83 cmdline[PROC_NAME_MAX - 1] = '\0';
85 return STC_ERROR_NONE;
89 pid_t find_pid_from_cmdline(char *cmdline)
91 pid_t pid = -1, foundpid = -1;
94 struct dirent *dentry;
95 char appname[PROC_NAME_MAX];
97 dp = opendir("/proc");
99 STC_LOGE("BACKGRD MANAGE : fail to open /proc");
100 return STC_ERROR_FAIL;
103 while ((dentry = readdir(dp)) != NULL) {
104 if (!isdigit(dentry->d_name[0]))
107 pid = atoi(dentry->d_name);
110 ret = proc_get_cmdline(pid, appname);
111 if (ret == STC_ERROR_NONE) {
112 if (!strncmp(cmdline, appname, strlen(appname)+1)) {
122 API void proc_foreach_pid(proc_pid_cb cb, void *user_data)
127 struct dirent *dentry;
129 dp = opendir("/proc");
131 STC_LOGE("failed to open /proc");
135 while ((dentry = readdir(dp)) != NULL) {
136 if (!isdigit(dentry->d_name[0]))
139 pid = atoi(dentry->d_name);
143 ret = cb(pid, user_data);
151 int proc_get_label(pid_t pid, char *label)
153 char buf[PROC_BUF_MAX];
156 snprintf(buf, sizeof(buf), "/proc/%d/attr/current", pid);
157 fp = fopen(buf, "r");
159 return STC_ERROR_FAIL;
161 if (fgets(label, PROC_NAME_MAX-1, fp) == NULL) {
163 return STC_ERROR_FAIL;
166 return STC_ERROR_NONE;
169 int proc_get_exepath(pid_t pid, char *buf, int len)
171 char path[PROC_BUF_MAX];
174 snprintf(path, sizeof(path), "/proc/%d/exe", pid);
175 ret = readlink(path, buf, len-1);
180 return STC_ERROR_NONE;
183 static int proc_get_data(char *path, char *buf, int len)
185 _cleanup_close_ int fd = -1;
188 fd = open(path, O_RDONLY);
190 return STC_ERROR_FAIL;
192 ret = read(fd, buf, len-1);
195 return STC_ERROR_FAIL;
198 return STC_ERROR_NONE;
201 int proc_get_raw_cmdline(pid_t pid, char *buf, int len)
203 char path[PROC_BUF_MAX];
204 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
205 return proc_get_data(path, buf, len);
209 API int proc_get_status(pid_t pid, char status[][PROC_BUF_MAX])
212 unsigned int index = 0;
213 char path[PROC_BUF_MAX];
214 char status_buf[PROC_BUF_MAX];
215 bool updated[PROC_STATUS_CNT] = {FALSE, };
218 snprintf(path, sizeof(path), "/proc/%d/status", pid);
219 fp = fopen(path, "r");
221 return STC_ERROR_FAIL;
223 for (i = 0; i < PROC_STATUS_CNT; ++i) {
225 char *saveptr = NULL;
227 if (fgets(status_buf, sizeof(status_buf), fp) == NULL) {
228 fclose(fp); //LCOV_EXCL_LINE
229 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
232 if (!updated[PROC_STATUS_NAME] && strstr(status_buf,
233 PROC_STATUS_NAME_STR))
234 index = PROC_STATUS_NAME;
235 else if (!updated[PROC_STATUS_STATE] && strstr(status_buf,
236 PROC_STATUS_STATE_STR))
237 index = PROC_STATUS_STATE;
238 else if (!updated[PROC_STATUS_TGID] && strstr(status_buf,
239 PROC_STATUS_TGID_STR))
240 index = PROC_STATUS_TGID;
241 else if (!updated[PROC_STATUS_NGID] && strstr(status_buf,
242 PROC_STATUS_NGID_STR))
243 index = PROC_STATUS_NGID;
244 else if (!updated[PROC_STATUS_PID] && strstr(status_buf,
245 PROC_STATUS_PID_STR))
246 index = PROC_STATUS_PID;
247 else if (!updated[PROC_STATUS_PPID] && strstr(status_buf,
248 PROC_STATUS_PPID_STR))
249 index = PROC_STATUS_PPID;
250 else if (!updated[PROC_STATUS_TRACERPID] && strstr(status_buf,
251 PROC_STATUS_TRACERPID_STR))
252 index = PROC_STATUS_TRACERPID;
256 token = strtok_r(status_buf, ":", &saveptr);
258 token = strtok_r(NULL, "\n", &saveptr);
260 while (isspace((unsigned char)*token))
262 g_strlcpy(status[index], token,
263 sizeof(status[index]));
264 updated[index] = TRUE;
270 return STC_ERROR_NONE;