[Fix] Fetch proper app_id for creation of cgroup.
[platform/core/connectivity/stc-manager.git] / src / helper / helper-procfs.c
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 /**
19  * @file procfs.c
20  * @desc wrapper for reading profs information.
21  *
22  * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved.
23  *
24  */
25
26 #include <ctype.h>
27 #include <stdio.h>
28 #include <stdbool.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <dirent.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
36
37 #include "stc-error.h"
38 #include "stc-manager-util.h"
39 #include "helper-procfs.h"
40
41 #define USRAPPS "/usr/apps/"
42
43 static int __proc_get_data(char *path, char *buf, int len)
44 {
45         _cleanup_fclose_ FILE *fp = NULL;
46
47         fp = fopen(path, "r");
48         if (fp == NULL)
49                 return STC_ERROR_FAIL;
50
51         if (fgets(buf, len - 1, fp) == NULL)
52                 return STC_ERROR_FAIL;
53
54         return STC_ERROR_NONE;
55 }
56
57 int proc_get_cmdline(pid_t pid, char *cmdline)
58 {
59         char *filename;
60         char *token = NULL;
61         char *saveptr = NULL;
62         char path_buf[PROC_BUF_MAX] = {0, };
63         char cmdline_buf[PROC_NAME_MAX] = {0, };
64         stc_error_e ret = STC_ERROR_NONE;
65
66         snprintf(path_buf, sizeof(path_buf), "/proc/%d/cmdline", pid);
67
68         ret = __proc_get_data(path_buf, cmdline_buf, PROC_NAME_MAX);
69         if (ret != STC_ERROR_NONE)
70                 return ret;
71
72         if (g_strstr_len(cmdline_buf, strlen(USRAPPS), USRAPPS) != NULL) {
73                 /* Application */
74                 filename = cmdline_buf + strlen(USRAPPS);
75                 token = strtok_r(filename, "/", &saveptr);
76                 if (token != NULL)
77                         filename = token;
78         } else {
79                 token = strtok_r(cmdline_buf, " ", &saveptr);
80                 if (token != NULL)
81                         filename = strrchr(token, '/');
82                 else
83                         filename = strrchr(cmdline_buf, '/');
84
85                 if (filename == NULL)
86                         filename = cmdline_buf;
87                 else
88                         filename = filename + 1;
89         }
90
91         strncpy(cmdline, filename, PROC_NAME_MAX-1);
92         return STC_ERROR_NONE;
93 }
94
95 int proc_get_label(pid_t pid, char *label)
96 {
97         char path_buf[PROC_BUF_MAX] = {0, };
98         char label_buf[PROC_NAME_MAX] = {0, };
99         stc_error_e ret = STC_ERROR_NONE;
100
101         snprintf(path_buf, sizeof(path_buf), "/proc/%d/attr/current", pid);
102
103         ret = __proc_get_data(path_buf, label_buf, PROC_NAME_MAX);
104         if (ret != STC_ERROR_NONE)
105                 return ret;
106
107         strncpy(label, label_buf, PROC_NAME_MAX-1);
108         return STC_ERROR_NONE;
109 }
110
111 int proc_get_status(pid_t pid, char status[][PROC_BUF_MAX])
112 {
113         unsigned int i;
114         char path[PROC_BUF_MAX];
115         char status_buf[PROC_BUF_MAX];
116         FILE *fp;
117
118         snprintf(path, sizeof(path), "/proc/%d/status", pid);
119         fp = fopen(path, "r");
120         if (fp == NULL)
121                 return STC_ERROR_FAIL;
122
123         for (i = 0; i < PROC_STATUS_CNT; ++i) {
124                 char *token = NULL;
125                 char *saveptr = NULL;
126
127                 if (fgets(status_buf, sizeof(status_buf), fp) == NULL) {
128                         fclose(fp);
129                         return STC_ERROR_FAIL;
130                 }
131
132                 token = strtok_r(status_buf, ":", &saveptr);
133                 if (token != NULL) {
134                         token = strtok_r(NULL, "\n", &saveptr);
135                         if (token != NULL) {
136                                 while (isspace((unsigned char)*token))
137                                         token++;
138                                 strncpy(status[i], token, sizeof(status[i]));
139                         }
140                 }
141         }
142         fclose(fp);
143
144         return STC_ERROR_NONE;
145 }