Separate monitoring function plugin
[platform/core/connectivity/stc-manager.git] / src / helper / helper-net-cls.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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 #include <dirent.h>
18 #include <glib.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <unistd.h>
22
23 #include "helper-cgroup.h"
24 #include "counter.h"
25 #include "stc-db.h"
26 #include "helper-file.h"
27 #include "helper-net-cls.h"
28
29 #define CUR_CLASSID_PATH   "/var/lib/stc/cur_classid"
30 #define CLASSID_FILE_NAME "net_cls.classid"
31
32 typedef GArray task_classid_array;
33
34 static uint32_t __produce_classid(check_classid_used_cb check_classid_cb)
35 {
36         uint32_t classid = STC_RESERVED_CLASSID_MAX;
37         int ret = fread_uint(CUR_CLASSID_PATH, &classid);
38         if (ret < 0)
39                 STC_LOGI("Can not read current classid"); //LCOV_EXCL_LINE
40
41         classid += 1;
42
43         if (check_classid_cb) {
44                 int classid_test_count = 0;
45                 for (classid_test_count = 0; classid_test_count < INT32_MAX;
46                      ++classid) {
47                         if (!check_classid_cb(classid))
48                                 break;
49                 }
50         }
51
52         ret = fwrite_uint(CUR_CLASSID_PATH, ++classid);
53         if (ret < 0)
54                 STC_LOGE("Can not write classid"); //LCOV_EXCL_LINE
55
56         return classid;
57 }
58
59 static int __place_classid_to_cgroup(const char *cgroup, const char *subdir,
60                                      uint32_t *classid,
61                                      check_classid_used_cb cb)
62 {
63         char buf[MAX_PATH_LENGTH];
64         uint32_t result_classid = (classid && *classid) ? *classid :
65                 __produce_classid(cb);
66
67         /* set classid as out argument */
68         if (classid && !*classid)
69                 *classid = result_classid;
70
71         snprintf(buf, sizeof(buf), "%s/%s", cgroup, subdir);
72         return cgroup_write_node_uint32(buf, CLASSID_FILE_NAME, result_classid);
73 }
74
75 static stc_error_e __get_classid_from_cgroup(const char *cgroup,
76                                           const char *subdir, uint32_t *classid)
77 {
78         char buf[MAX_PATH_LENGTH];
79         snprintf(buf, sizeof(buf), "%s/%s", cgroup, subdir);
80
81         int ret = cgroup_read_node_uint32(buf, CLASSID_FILE_NAME, classid);
82         if (ret < 0) {
83                 *classid = STC_UNKNOWN_CLASSID;
84                 return STC_ERROR_NO_DATA;
85         }
86
87         return STC_ERROR_NONE;
88 }
89
90 API stc_error_e init_current_classid(void)
91 {
92         int ret = 0;
93         struct stat stat_buf;
94
95         if (stat(STC_CGROUP_NETWORK, &stat_buf) != 0) {
96                 uint32_t classid = STC_RESERVED_CLASSID_MAX;
97                 ret = fwrite_uint(CUR_CLASSID_PATH, classid);
98                 if (ret < 0) {
99                         STC_LOGE("Can not init current classid"); //LCOV_EXCL_LINE
100                         return STC_ERROR_FAIL; //LCOV_EXCL_LINE
101                 }
102         }
103
104         return STC_ERROR_NONE;
105 }
106
107 API uint32_t get_classid_by_app_id(const char *app_id, int create)
108 {
109         bool exists;
110         int ret = STC_ERROR_NONE;
111         uint32_t classid = STC_UNKNOWN_CLASSID;
112         const char *path_to_net_cgroup_dir = NULL;
113
114         if (app_id == NULL) {
115                 STC_LOGE("app_id must be not empty"); //LCOV_EXCL_LINE
116                 return STC_UNKNOWN_CLASSID; //LCOV_EXCL_LINE
117         }
118
119         if (!strcmp(app_id, STC_TOTAL_BACKGROUND))
120                 return STC_BACKGROUND_APP_CLASSID;
121
122         if (!strcmp(app_id, STC_TOTAL_DATACALL))
123                 return STC_TOTAL_DATACALL_CLASSID;
124
125         if (!strcmp(app_id, STC_TOTAL_WIFI))
126                 return STC_TOTAL_WIFI_CLASSID;
127
128         if (!strcmp(app_id, STC_TOTAL_BLUETOOTH))
129                 return STC_TOTAL_BLUETOOTH_CLASSID;
130
131         if (!strcmp(app_id, STC_TOTAL_IPV4))
132                 return STC_TOTAL_IPV4_CLASSID;
133
134         if (!strcmp(app_id, STC_TOTAL_IPV6))
135                 return STC_TOTAL_IPV6_CLASSID;
136
137         if (!strcmp(app_id, STC_TOTAL_TETHERING))
138                 return STC_TETHERING_APP_CLASSID;
139
140         if (strstr(app_id, STC_BACKGROUND_APP_SUFFIX))
141                 path_to_net_cgroup_dir = BACKGROUND_CGROUP_NETWORK;
142         else if (strstr(app_id, STC_TETHERING_APP_SUFFIX))
143                 path_to_net_cgroup_dir = TETHERING_CGROUP_NETWORK;
144         else
145                 path_to_net_cgroup_dir = FOREGROUND_CGROUP_NETWORK;
146
147         /* just read */
148         if (!create)
149                 ret = __get_classid_from_cgroup(path_to_net_cgroup_dir, //LCOV_EXCL_LINE
150                                                         app_id, &classid);
151
152         if (ret != STC_ERROR_NONE)
153                 return STC_UNKNOWN_CLASSID;
154
155         if (classid != STC_UNKNOWN_CLASSID)
156                 return classid;
157
158         ret = cgroup_make_subdir(path_to_net_cgroup_dir, (char *)app_id,
159                                  &exists);
160         if (ret)
161                 goto handle_error;
162
163         if (exists)
164                 ret = __get_classid_from_cgroup(path_to_net_cgroup_dir,
165                                                     app_id, &classid);
166         else
167                 ret = __place_classid_to_cgroup(path_to_net_cgroup_dir,
168                                                 (char *)app_id, &classid, NULL);
169         if (ret)
170                 goto handle_error; //LCOV_EXCL_LINE
171
172         return classid;
173
174 handle_error:
175         if (STC_DEBUG_LOG)
176                 STC_LOGE("error_code: [%d]", ret); //LCOV_EXCL_LINE
177         return STC_UNKNOWN_CLASSID; //LCOV_EXCL_LINE
178 }
179
180 API stc_error_e place_pids_to_net_cgroup(const int pid, const char *app_id)
181 {
182         char child_buf[21 + MAX_DEC_SIZE(int) + MAX_DEC_SIZE(int) + 1];
183         const char *path_to_net_cgroup_dir = NULL;
184
185         snprintf(child_buf, sizeof(child_buf), PROC_TASK_CHILDREN, pid, pid);
186
187         if (app_id == NULL) {
188                 STC_LOGE("package name must be not empty"); //LCOV_EXCL_LINE
189                 return STC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
190         }
191
192         if (strstr(app_id, STC_BACKGROUND_APP_SUFFIX))
193                 path_to_net_cgroup_dir = BACKGROUND_CGROUP_NETWORK;
194         else if (strstr(app_id, STC_TETHERING_APP_SUFFIX))
195                 path_to_net_cgroup_dir = TETHERING_CGROUP_NETWORK;
196         else
197                 path_to_net_cgroup_dir = FOREGROUND_CGROUP_NETWORK; //LCOV_EXCL_LINE
198
199         if (access(child_buf, F_OK))
200                 return cgroup_write_pid(path_to_net_cgroup_dir, app_id, pid);
201
202         return cgroup_write_pidtree(path_to_net_cgroup_dir, app_id, pid); //LCOV_EXCL_LINE
203 }