#include "helper-file.h"
#include "helper-net-cls.h"
-#define CUR_CLASSID_PATH "/tmp/cur_classid"
+#define CUR_CLASSID_PATH "/var/lib/stc/cur_classid"
#define CLASSID_FILE_NAME "net_cls.classid"
typedef GArray task_classid_array;
static uint32_t __produce_classid(check_classid_used_cb check_classid_cb)
{
uint32_t classid = STC_RESERVED_CLASSID_MAX;
- int classid_test_count = 0;
int ret = fread_uint(CUR_CLASSID_PATH, &classid);
if (ret < 0)
- STC_LOGE("Can not read current classid");
+ STC_LOGI("Can not read current classid"); //LCOV_EXCL_LINE
+
classid += 1;
- if (check_classid_cb)
+
+ if (check_classid_cb) {
+ int classid_test_count = 0;
for (classid_test_count = 0; classid_test_count < INT32_MAX;
++classid) {
if (!check_classid_cb(classid))
break;
}
-
- if (classid_test_count == INT32_MAX) {
- STC_LOGE("there is no available classid due to kernel limitation");
- return STC_UNKNOWN_CLASSID;
}
ret = fwrite_uint(CUR_CLASSID_PATH, ++classid);
if (ret < 0)
- STC_LOGE("Can not write classid");
+ STC_LOGE("Can not write classid"); //LCOV_EXCL_LINE
return classid;
}
int ret = cgroup_read_node_uint32(buf, CLASSID_FILE_NAME, &classid);
if (ret < 0)
- STC_LOGE("Can't read classid from cgroup %s", buf);
+ STC_LOGE("Can't read classid from cgroup %s", buf); //LCOV_EXCL_LINE
return classid;
}
-/* TODO: background tasks update from list */
-static void __add_background_apps(GSList *background_pid_list)
+stc_error_e init_current_classid(void)
{
- if (background_pid_list == NULL)
- return;
-
- /* remove old list */
- /* add new list */
-}
-
-static stc_error_e __make_net_cls_cgroup(const char *pkg_name, uint32_t classid,
- check_classid_used_cb cb)
-{
- stc_error_e ret = STC_ERROR_NONE;
- bool exists = false;
-
- if (pkg_name == NULL) {
- STC_LOGE("package name must be not empty");
- return STC_ERROR_INVALID_PARAMETER;
+ int ret = 0;
+ struct stat stat_buf;
+
+ if (stat(STC_CGROUP_NETWORK, &stat_buf) != 0) {
+ uint32_t classid = STC_RESERVED_CLASSID_MAX;
+ ret = fwrite_uint(CUR_CLASSID_PATH, classid);
+ if (ret < 0) {
+ STC_LOGE("Can not init current classid"); //LCOV_EXCL_LINE
+ return STC_ERROR_FAIL; //LCOV_EXCL_LINE
+ }
}
- ret = cgroup_make_subdir(PATH_TO_NET_CGROUP_DIR, (char *)pkg_name,
- &exists);
- ret_value_if(ret < 0, STC_ERROR_FAIL);
-
- if (!exists) {
- ret = __place_classid_to_cgroup(PATH_TO_NET_CGROUP_DIR,
- pkg_name,
- classid ? &classid : NULL, cb);
- ret_value_if(ret < 0, STC_ERROR_FAIL);
- }
- return ret;
+ return STC_ERROR_NONE;
}
uint32_t get_classid_by_app_id(const char *app_id, int create)
int ret = 0;
bool exists;
uint32_t classid = STC_UNKNOWN_CLASSID;
+ const char *path_to_net_cgroup_dir = NULL;
if (app_id == NULL) {
- STC_LOGE("app_id must be not empty");
- return STC_UNKNOWN_CLASSID;
+ STC_LOGE("app_id must be not empty"); //LCOV_EXCL_LINE
+ return STC_UNKNOWN_CLASSID; //LCOV_EXCL_LINE
}
- if (!strcmp(app_id, STC_ALL_APP))
- return STC_ALL_APP_CLASSID;
+ if (!strcmp(app_id, STC_BACKGROUND_APP_ID))
+ return STC_BACKGROUND_APP_CLASSID;
- if (!strcmp(app_id, TETHERING_APP_NAME))
- return STC_TETHERING_APP_CLASSID;
+ if (!strcmp(app_id, STC_TOTAL_DATACALL))
+ return STC_TOTAL_DATACALL_CLASSID;
- if (!strcmp(app_id, STC_BACKGROUND_APP_NAME))
- return STC_BACKGROUND_APP_CLASSID;
+ if (!strcmp(app_id, STC_TOTAL_WIFI))
+ return STC_TOTAL_WIFI_CLASSID;
+
+ if (!strcmp(app_id, STC_TOTAL_BLUETOOTH))
+ return STC_TOTAL_BLUETOOTH_CLASSID;
- if (!strcmp(app_id, NETWORK_RESTRICTION_APP_NAME))
- return STC_NETWORK_RESTRICTION_APP_CLASSID;
+ if (!strcmp(app_id, STC_TOTAL_IPV4))
+ return STC_TOTAL_IPV4_CLASSID;
+
+ if (!strcmp(app_id, STC_TOTAL_IPV6))
+ return STC_TOTAL_IPV6_CLASSID;
+
+ if (!strcmp(app_id, STC_TOTAL_TETHERING))
+ return STC_TETHERING_APP_CLASSID;
+
+ if (strstr(app_id, STC_BACKGROUND_APP_SUFFIX))
+ path_to_net_cgroup_dir = BACKGROUND_CGROUP_NETWORK;
+ else if (strstr(app_id, STC_TETHERING_APP_SUFFIX))
+ path_to_net_cgroup_dir = TETHERING_CGROUP_NETWORK;
+ else
+ path_to_net_cgroup_dir = FOREGROUND_CGROUP_NETWORK;
/* just read */
if (!create)
- classid = __get_classid_from_cgroup(PATH_TO_NET_CGROUP_DIR,
+ classid = __get_classid_from_cgroup(path_to_net_cgroup_dir, //LCOV_EXCL_LINE
app_id);
if (classid != STC_UNKNOWN_CLASSID)
return classid;
- ret = cgroup_make_subdir(PATH_TO_NET_CGROUP_DIR, (char *)app_id,
+ ret = cgroup_make_subdir(path_to_net_cgroup_dir, (char *)app_id,
&exists);
if (ret)
goto handle_error;
if (exists)
- classid = __get_classid_from_cgroup(PATH_TO_NET_CGROUP_DIR,
+ classid = __get_classid_from_cgroup(path_to_net_cgroup_dir,
app_id);
else
- ret = __place_classid_to_cgroup(PATH_TO_NET_CGROUP_DIR,
+ ret = __place_classid_to_cgroup(path_to_net_cgroup_dir,
(char *)app_id, &classid, NULL);
if (ret)
- goto handle_error;
+ goto handle_error; //LCOV_EXCL_LINE
return classid;
handle_error:
- STC_LOGE("error_code: [%d]", ret);
- return STC_UNKNOWN_CLASSID;
+ STC_LOGE("error_code: [%d]", ret); //LCOV_EXCL_LINE
+ return STC_UNKNOWN_CLASSID; //LCOV_EXCL_LINE
}
-stc_error_e place_pids_to_net_cgroup(const int pid, const char *pkg_name)
+stc_error_e place_pids_to_net_cgroup(const int pid, const char *app_id)
{
- char child_buf[21 + MAX_DEC_SIZE(int) + MAX_DEC_SIZE(int)];
- snprintf(child_buf, sizeof(child_buf), PROC_TASK_CHILDREN, pid, pid);
+ char child_buf[21 + MAX_DEC_SIZE(int) + MAX_DEC_SIZE(int) + 1];
+ const char *path_to_net_cgroup_dir = NULL;
- if (pkg_name == NULL) {
- STC_LOGE("package name must be not empty");
- return STC_ERROR_INVALID_PARAMETER;
- }
+ snprintf(child_buf, sizeof(child_buf), PROC_TASK_CHILDREN, pid, pid);
- if (access(child_buf, F_OK)) {
- STC_LOGD("%s of %s is not existed", child_buf, pkg_name);
- return cgroup_write_pid(PATH_TO_NET_CGROUP_DIR, pkg_name, pid);
+ if (app_id == NULL) {
+ STC_LOGE("package name must be not empty"); //LCOV_EXCL_LINE
+ return STC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
}
- return cgroup_write_pidtree(PATH_TO_NET_CGROUP_DIR, pkg_name, pid);
-}
-
-void create_net_background_cgroup(GSList *background_pid_list)
-{
- stc_error_e ret = __make_net_cls_cgroup(STC_BACKGROUND_APP_NAME,
- STC_BACKGROUND_APP_CLASSID,
- NULL);
- if (ret == STC_ERROR_NONE)
- __add_background_apps(background_pid_list);
+ if (!strcmp(app_id, STC_BACKGROUND_APP_ID))
+ path_to_net_cgroup_dir = STC_CGROUP_NETWORK;
+ else if (strstr(app_id, STC_BACKGROUND_APP_SUFFIX))
+ path_to_net_cgroup_dir = BACKGROUND_CGROUP_NETWORK;
+ else if (strstr(app_id, STC_TETHERING_APP_SUFFIX))
+ path_to_net_cgroup_dir = TETHERING_CGROUP_NETWORK;
else
- STC_LOGD("Could not support quota for background applications");
-}
+ path_to_net_cgroup_dir = FOREGROUND_CGROUP_NETWORK; //LCOV_EXCL_LINE
-void add_pid_to_background_cgroup(pid_t pid)
-{
- place_pids_to_net_cgroup(pid, STC_BACKGROUND_APP_NAME);
-}
+ if (access(child_buf, F_OK))
+ return cgroup_write_pid(path_to_net_cgroup_dir, app_id, pid);
-void remove_pid_from_background_cgroup(pid_t pid)
-{
- cgroup_remove_pid(PATH_TO_NET_CGROUP_DIR, STC_BACKGROUND_APP_NAME, pid);
+ return cgroup_write_pidtree(path_to_net_cgroup_dir, app_id, pid); //LCOV_EXCL_LINE
}