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 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;
57 if (fgets(cmdline_buf, PROC_NAME_MAX-1, fp) == NULL) {
59 return STC_ERROR_FAIL;
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;
87 pid_t find_pid_from_cmdline(char *cmdline)
89 pid_t pid = -1, foundpid = -1;
92 struct dirent *dentry;
93 char appname[PROC_NAME_MAX];
95 dp = opendir("/proc");
97 STC_LOGE("BACKGRD MANAGE : fail to open /proc");
98 return STC_ERROR_FAIL;
101 while ((dentry = readdir(dp)) != NULL) {
102 if (!isdigit(dentry->d_name[0]))
105 pid = atoi(dentry->d_name);
108 ret = proc_get_cmdline(pid, appname);
109 if (ret == STC_ERROR_NONE) {
110 if (!strncmp(cmdline, appname, strlen(appname)+1)) {
120 int proc_get_label(pid_t pid, char *label)
122 char buf[PROC_BUF_MAX];
125 snprintf(buf, sizeof(buf), "/proc/%d/attr/current", pid);
126 fp = fopen(buf, "r");
128 return STC_ERROR_FAIL;
130 if (fgets(label, PROC_NAME_MAX-1, fp) == NULL) {
132 return STC_ERROR_FAIL;
135 return STC_ERROR_NONE;
138 int proc_get_exepath(pid_t pid, char *buf, int len)
140 char path[PROC_BUF_MAX];
143 snprintf(path, sizeof(path), "/proc/%d/exe", pid);
144 ret = readlink(path, buf, len-1);
149 return STC_ERROR_NONE;
152 static int proc_get_data(char *path, char *buf, int len)
154 _cleanup_close_ int fd = -1;
157 fd = open(path, O_RDONLY);
159 return STC_ERROR_FAIL;
161 ret = read(fd, buf, len-1);
164 return STC_ERROR_FAIL;
167 return STC_ERROR_NONE;
170 int proc_get_raw_cmdline(pid_t pid, char *buf, int len)
172 char path[PROC_BUF_MAX];
173 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
174 return proc_get_data(path, buf, len);
177 int proc_get_status(pid_t pid, char status[][PROC_BUF_MAX])
180 char path[PROC_BUF_MAX];
181 char status_buf[PROC_BUF_MAX];
184 snprintf(path, sizeof(path), "/proc/%d/status", pid);
185 fp = fopen(path, "r");
187 return STC_ERROR_FAIL;
189 for (i = 0; i < PROC_STATUS_CNT; ++i) {
191 char *saveptr = NULL;
193 if (fgets(status_buf, sizeof(status_buf), fp) == NULL) {
195 return STC_ERROR_FAIL;
198 token = strtok_r(status_buf, ":", &saveptr);
200 token = strtok_r(NULL, "\n", &saveptr);
202 while (isspace((unsigned char)*token))
204 strncpy(status[i], token, sizeof(status[i]));
210 return STC_ERROR_NONE;