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);
84 return STC_ERROR_NONE;
88 pid_t find_pid_from_cmdline(char *cmdline)
90 pid_t pid = -1, foundpid = -1;
93 struct dirent *dentry;
94 char appname[PROC_NAME_MAX];
96 dp = opendir("/proc");
98 STC_LOGE("BACKGRD MANAGE : fail to open /proc");
99 return STC_ERROR_FAIL;
102 while ((dentry = readdir(dp)) != NULL) {
103 if (!isdigit(dentry->d_name[0]))
106 pid = atoi(dentry->d_name);
109 ret = proc_get_cmdline(pid, appname);
110 if (ret == STC_ERROR_NONE) {
111 if (!strncmp(cmdline, appname, strlen(appname)+1)) {
121 API void proc_foreach_pid(proc_pid_cb cb, void *user_data)
126 struct dirent *dentry;
128 dp = opendir("/proc");
130 STC_LOGE("failed to open /proc");
134 while ((dentry = readdir(dp)) != NULL) {
135 if (!isdigit(dentry->d_name[0]))
138 pid = atoi(dentry->d_name);
142 ret = cb(pid, user_data);
150 int proc_get_label(pid_t pid, char *label)
152 char buf[PROC_BUF_MAX];
155 snprintf(buf, sizeof(buf), "/proc/%d/attr/current", pid);
156 fp = fopen(buf, "r");
158 return STC_ERROR_FAIL;
160 if (fgets(label, PROC_NAME_MAX-1, fp) == NULL) {
162 return STC_ERROR_FAIL;
165 return STC_ERROR_NONE;
168 int proc_get_exepath(pid_t pid, char *buf, int len)
170 char path[PROC_BUF_MAX];
173 snprintf(path, sizeof(path), "/proc/%d/exe", pid);
174 ret = readlink(path, buf, len-1);
179 return STC_ERROR_NONE;
182 static int proc_get_data(char *path, char *buf, int len)
184 _cleanup_close_ int fd = -1;
187 fd = open(path, O_RDONLY);
189 return STC_ERROR_FAIL;
191 ret = read(fd, buf, len-1);
194 return STC_ERROR_FAIL;
197 return STC_ERROR_NONE;
200 int proc_get_raw_cmdline(pid_t pid, char *buf, int len)
202 char path[PROC_BUF_MAX];
203 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
204 return proc_get_data(path, buf, len);
208 API int proc_get_status(pid_t pid, char status[][PROC_BUF_MAX])
211 unsigned int index = 0;
212 char path[PROC_BUF_MAX];
213 char status_buf[PROC_BUF_MAX];
214 bool updated[PROC_STATUS_CNT] = {FALSE, };
217 snprintf(path, sizeof(path), "/proc/%d/status", pid);
218 fp = fopen(path, "r");
220 return STC_ERROR_FAIL;
222 for (i = 0; i < PROC_STATUS_CNT; ++i) {
224 char *saveptr = NULL;
226 if (fgets(status_buf, sizeof(status_buf), fp) == NULL) {
227 fclose(fp); //LCOV_EXCL_LINE
228 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
231 if (!updated[PROC_STATUS_NAME] && strstr(status_buf,
232 PROC_STATUS_NAME_STR))
233 index = PROC_STATUS_NAME;
234 else if (!updated[PROC_STATUS_STATE] && strstr(status_buf,
235 PROC_STATUS_STATE_STR))
236 index = PROC_STATUS_STATE;
237 else if (!updated[PROC_STATUS_TGID] && strstr(status_buf,
238 PROC_STATUS_TGID_STR))
239 index = PROC_STATUS_TGID;
240 else if (!updated[PROC_STATUS_NGID] && strstr(status_buf,
241 PROC_STATUS_NGID_STR))
242 index = PROC_STATUS_NGID;
243 else if (!updated[PROC_STATUS_PID] && strstr(status_buf,
244 PROC_STATUS_PID_STR))
245 index = PROC_STATUS_PID;
246 else if (!updated[PROC_STATUS_PPID] && strstr(status_buf,
247 PROC_STATUS_PPID_STR))
248 index = PROC_STATUS_PPID;
249 else if (!updated[PROC_STATUS_TRACERPID] && strstr(status_buf,
250 PROC_STATUS_TRACERPID_STR))
251 index = PROC_STATUS_TRACERPID;
255 token = strtok_r(status_buf, ":", &saveptr);
257 token = strtok_r(NULL, "\n", &saveptr);
259 while (isspace((unsigned char)*token))
261 strncpy(status[index], token,
262 sizeof(status[index]));
263 updated[index] = TRUE;
269 return STC_ERROR_NONE;