Merge "Return errors to caller" into tizen_5.5
[platform/core/connectivity/stc-manager.git] / src / helper / helper-net-cls.c
index 6be717a..350dfb6 100755 (executable)
@@ -36,7 +36,7 @@ static uint32_t __produce_classid(check_classid_used_cb check_classid_cb)
        uint32_t classid = STC_RESERVED_CLASSID_MAX;
        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;
 
@@ -51,7 +51,7 @@ static uint32_t __produce_classid(check_classid_used_cb check_classid_cb)
 
        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;
 }
@@ -72,20 +72,22 @@ static int __place_classid_to_cgroup(const char *cgroup, const char *subdir,
        return cgroup_write_node_uint32(buf, CLASSID_FILE_NAME, result_classid);
 }
 
-static uint32_t __get_classid_from_cgroup(const char *cgroup,
-                                         const char *subdir)
+static stc_error_e __get_classid_from_cgroup(const char *cgroup,
+                                         const char *subdir, uint32_t *classid)
 {
        char buf[MAX_PATH_LENGTH];
-       uint32_t classid = STC_UNKNOWN_CLASSID;
        snprintf(buf, sizeof(buf), "%s/%s", cgroup, subdir);
 
-       int ret = cgroup_read_node_uint32(buf, CLASSID_FILE_NAME, &classid);
-       if (ret < 0)
-               STC_LOGE("Can't read classid from cgroup %s", buf);
-       return classid;
+       int ret = cgroup_read_node_uint32(buf, CLASSID_FILE_NAME, classid);
+       if (ret < 0) {
+               *classid = STC_UNKNOWN_CLASSID;
+               return STC_ERROR_NO_DATA;
+       }
+
+       return STC_ERROR_NONE;
 }
 
-stc_error_e init_current_classid(void)
+API stc_error_e init_current_classid(void)
 {
        int ret = 0;
        struct stat stat_buf;
@@ -94,27 +96,27 @@ stc_error_e init_current_classid(void)
                uint32_t classid = STC_RESERVED_CLASSID_MAX;
                ret = fwrite_uint(CUR_CLASSID_PATH, classid);
                if (ret < 0) {
-                       STC_LOGE("Can not init current classid");
-                       return STC_ERROR_FAIL;
+                       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)
+API uint32_t get_classid_by_app_id(const char *app_id, int create)
 {
-       int ret = 0;
        bool exists;
+       int ret = STC_ERROR_NONE;
        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_BACKGROUND_APP_ID))
+       if (!strcmp(app_id, STC_TOTAL_BACKGROUND))
                return STC_BACKGROUND_APP_CLASSID;
 
        if (!strcmp(app_id, STC_TOTAL_DATACALL))
@@ -132,15 +134,23 @@ uint32_t get_classid_by_app_id(const char *app_id, int create)
        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,
-                                                   app_id);
+               ret = __get_classid_from_cgroup(path_to_net_cgroup_dir, //LCOV_EXCL_LINE
+                                                       app_id, &classid);
+
+       if (ret != STC_ERROR_NONE)
+               return STC_UNKNOWN_CLASSID;
 
        if (classid != STC_UNKNOWN_CLASSID)
                return classid;
@@ -151,46 +161,43 @@ uint32_t get_classid_by_app_id(const char *app_id, int create)
                goto handle_error;
 
        if (exists)
-               classid = __get_classid_from_cgroup(path_to_net_cgroup_dir,
-                                                   app_id);
+               ret = __get_classid_from_cgroup(path_to_net_cgroup_dir,
+                                                   app_id, &classid);
        else
                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;
+       if (STC_DEBUG_LOG)
+               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)
+API 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))
-               path_to_net_cgroup_dir = STC_CGROUP_NETWORK;
-       else if (strstr(app_id, STC_BACKGROUND_APP_SUFFIX))
+       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;
+               path_to_net_cgroup_dir = FOREGROUND_CGROUP_NETWORK; //LCOV_EXCL_LINE
 
-       if (access(child_buf, F_OK)) {
-               if (STC_DEBUG_LOG)
-                       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
 }