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 static int __proc_get_data(char *path, char *buf, int len)
45 _cleanup_fclose_ FILE *fp = NULL;
47 fp = fopen(path, "r");
49 return STC_ERROR_FAIL;
51 if (fgets(buf, len - 1, fp) == NULL)
52 return STC_ERROR_FAIL;
54 return STC_ERROR_NONE;
57 int proc_get_cmdline(pid_t pid, char *cmdline)
62 char path_buf[PROC_BUF_MAX] = {0, };
63 char cmdline_buf[PROC_NAME_MAX] = {0, };
64 stc_error_e ret = STC_ERROR_NONE;
66 snprintf(path_buf, sizeof(path_buf), "/proc/%d/cmdline", pid);
68 ret = __proc_get_data(path_buf, cmdline_buf, PROC_NAME_MAX);
69 if (ret != STC_ERROR_NONE)
72 if (g_strstr_len(cmdline_buf, strlen(USRAPPS), USRAPPS) != NULL) {
74 filename = cmdline_buf + strlen(USRAPPS);
75 token = strtok_r(filename, "/", &saveptr);
79 token = strtok_r(cmdline_buf, " ", &saveptr);
81 filename = strrchr(token, '/');
83 filename = strrchr(cmdline_buf, '/');
86 filename = cmdline_buf;
88 filename = filename + 1;
91 strncpy(cmdline, filename, PROC_NAME_MAX-1);
92 return STC_ERROR_NONE;
95 int proc_get_label(pid_t pid, char *label)
97 char path_buf[PROC_BUF_MAX] = {0, };
98 char label_buf[PROC_NAME_MAX] = {0, };
99 stc_error_e ret = STC_ERROR_NONE;
101 snprintf(path_buf, sizeof(path_buf), "/proc/%d/attr/current", pid);
103 ret = __proc_get_data(path_buf, label_buf, PROC_NAME_MAX);
104 if (ret != STC_ERROR_NONE)
107 strncpy(label, label_buf, PROC_NAME_MAX-1);
108 return STC_ERROR_NONE;
111 int proc_get_status(pid_t pid, char status[][PROC_BUF_MAX])
114 char path[PROC_BUF_MAX];
115 char status_buf[PROC_BUF_MAX];
118 snprintf(path, sizeof(path), "/proc/%d/status", pid);
119 fp = fopen(path, "r");
121 return STC_ERROR_FAIL;
123 for (i = 0; i < PROC_STATUS_CNT; ++i) {
125 char *saveptr = NULL;
127 if (fgets(status_buf, sizeof(status_buf), fp) == NULL) {
129 return STC_ERROR_FAIL;
132 token = strtok_r(status_buf, ":", &saveptr);
134 token = strtok_r(NULL, "\n", &saveptr);
136 while (isspace((unsigned char)*token))
138 strncpy(status[i], token, sizeof(status[i]));
144 return STC_ERROR_NONE;