#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_LOGI("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;
}
+ }
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;
}
+stc_error_e init_current_classid(void)
+{
+ 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
+ }
+ }
+
+ return STC_ERROR_NONE;
+}
+
uint32_t get_classid_by_app_id(const char *app_id, int create)
{
int ret = 0;
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_BACKGROUND_APP_ID))
if (!strcmp(app_id, STC_TOTAL_BLUETOOTH))
return STC_TOTAL_BLUETOOTH_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
/* 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)
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 *app_id)
{
- char child_buf[21 + MAX_DEC_SIZE(int) + MAX_DEC_SIZE(int)];
+ char child_buf[21 + MAX_DEC_SIZE(int) + MAX_DEC_SIZE(int) + 1];
const char *path_to_net_cgroup_dir = NULL;
snprintf(child_buf, sizeof(child_buf), PROC_TASK_CHILDREN, pid, pid);
if (app_id == NULL) {
- STC_LOGE("package name must be not empty");
- return STC_ERROR_INVALID_PARAMETER;
+ STC_LOGE("package name must be not empty"); //LCOV_EXCL_LINE
+ return STC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
}
if (!strcmp(app_id, STC_BACKGROUND_APP_ID))
else if (strstr(app_id, STC_BACKGROUND_APP_SUFFIX))
path_to_net_cgroup_dir = BACKGROUND_CGROUP_NETWORK;
else
- path_to_net_cgroup_dir = FOREGROUND_CGROUP_NETWORK;
+ path_to_net_cgroup_dir = FOREGROUND_CGROUP_NETWORK; //LCOV_EXCL_LINE
- if (access(child_buf, F_OK)) {
- STC_LOGD("%s of %s is not existed", child_buf, app_id);
+ if (access(child_buf, F_OK))
return cgroup_write_pid(path_to_net_cgroup_dir, app_id, pid);
- }
- return cgroup_write_pidtree(path_to_net_cgroup_dir, app_id, pid);
+ return cgroup_write_pidtree(path_to_net_cgroup_dir, app_id, pid); //LCOV_EXCL_LINE
}