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 int proc_get_cmdline(pid_t pid, char *cmdline)
43 char buf[PROC_BUF_MAX];
44 char cmdline_buf[PROC_NAME_MAX];
48 snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
51 return STC_ERROR_FAIL;
53 if (fgets(cmdline_buf, PROC_NAME_MAX-1, fp) == NULL) {
55 return STC_ERROR_FAIL;
59 filename = strrchr(cmdline_buf, '/');
61 filename = cmdline_buf;
63 filename = filename + 1;
65 strncpy(cmdline, filename, PROC_NAME_MAX-1);
67 return STC_ERROR_NONE;
70 pid_t find_pid_from_cmdline(char *cmdline)
72 pid_t pid = -1, foundpid = -1;
75 struct dirent *dentry;
76 char appname[PROC_NAME_MAX];
78 dp = opendir("/proc");
80 STC_LOGE("BACKGRD MANAGE : fail to open /proc");
81 return STC_ERROR_FAIL;
84 while ((dentry = readdir(dp)) != NULL) {
85 if (!isdigit(dentry->d_name[0]))
88 pid = atoi(dentry->d_name);
91 ret = proc_get_cmdline(pid, appname);
92 if (ret == STC_ERROR_NONE) {
93 if (!strncmp(cmdline, appname, strlen(appname)+1)) {
103 int proc_get_label(pid_t pid, char *label)
105 char buf[PROC_BUF_MAX];
108 snprintf(buf, sizeof(buf), "/proc/%d/attr/current", pid);
109 fp = fopen(buf, "r");
111 return STC_ERROR_FAIL;
113 if (fgets(label, PROC_NAME_MAX-1, fp) == NULL) {
115 return STC_ERROR_FAIL;
118 return STC_ERROR_NONE;
121 int proc_get_exepath(pid_t pid, char *buf, int len)
123 char path[PROC_BUF_MAX];
126 snprintf(path, sizeof(path), "/proc/%d/exe", pid);
127 ret = readlink(path, buf, len-1);
132 return STC_ERROR_NONE;
135 static int proc_get_data(char *path, char *buf, int len)
137 _cleanup_close_ int fd = -1;
140 fd = open(path, O_RDONLY);
142 return STC_ERROR_FAIL;
144 ret = read(fd, buf, len-1);
147 return STC_ERROR_FAIL;
150 return STC_ERROR_NONE;
153 int proc_get_raw_cmdline(pid_t pid, char *buf, int len)
155 char path[PROC_BUF_MAX];
156 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
157 return proc_get_data(path, buf, len);
160 int proc_get_status(pid_t pid, char status[][PROC_BUF_MAX])
163 char path[PROC_BUF_MAX];
164 char status_buf[PROC_BUF_MAX];
167 snprintf(path, sizeof(path), "/proc/%d/status", pid);
168 fp = fopen(path, "r");
170 return STC_ERROR_FAIL;
172 for (i = 0; i < PROC_STATUS_CNT; ++i) {
174 char *saveptr = NULL;
176 if (fgets(status_buf, sizeof(status_buf), fp) == NULL) {
178 return STC_ERROR_FAIL;
181 token = strtok_r(status_buf, ":", &saveptr);
183 token = strtok_r(NULL, "\n", &saveptr);
185 while (isspace((unsigned char)*token))
187 strncpy(status[i], token, sizeof(status[i]));
193 return STC_ERROR_NONE;