Separate monitoring function plugin
[platform/core/connectivity/stc-manager.git] / src / helper / helper-cgroup.c
index af9f1f1..1d5ba2b 100755 (executable)
 
 #define MAX_PATH_LENGTH 512
 
-static int read_uint(FILE *handler, uint32_t *out)
-{
-       return fscanf(handler, "%u", out);
-}
-
-static int write_uint(FILE *handler, uint32_t number)
-{
-       _cleanup_free_ char *digit_buf = NULL;
-       int ret;
-
-       ret = asprintf(&digit_buf, "%u\n", number);
-       ret_value_errno_msg_if(ret < 0, -ENOMEM, "asprintf failed\n");
-
-       ret = fputs(digit_buf, handler);
-       ret_value_errno_msg_if(ret == EOF, errno ? -errno : -EIO,
-                              "Fail to write file");
-
-       return 0;
-}
-
 static bool cgroup_is_exists(const char *cgroup_full_path)
 {
        struct stat stat_buf;
@@ -54,7 +34,7 @@ static int cgroup_create(const char *cgroup_full_path)
 {
        if (mkdir(cgroup_full_path,
                  S_IRUSR | S_IWUSR | S_IRGRP) < 0)
-               return -errno;
+               return -errno; //LCOV_EXCL_LINE
 
        return 0;
 }
@@ -69,8 +49,8 @@ stc_error_e cgroup_write_pid_fullpath(const char *cgroup_full_path,
        int ret;
 
        if (pid <= 0) {
-               STC_LOGE("try to write empty pid to %s", cgroup_full_path);
-               return STC_ERROR_NO_DATA;
+               STC_LOGE("try to write empty pid to %s", cgroup_full_path); //LCOV_EXCL_LINE
+               return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
        }
 
        ret = cgroup_write_node_uint32(cgroup_full_path, CGROUP_FILE_NAME,
@@ -89,13 +69,14 @@ stc_error_e cgroup_write_pid(const char *cgroup_subsystem,
        return cgroup_write_pid_fullpath(buf, pid);
 }
 
+//LCOV_EXCL_START
 stc_error_e cgroup_write_pidtree(const char *cgroup_subsystem,
                                 const char *cgroup_name, const int pid)
 {
        char buf[MAX_PATH_LENGTH];
 
        /*/proc/%d/task/%d/children */
-       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];
        char pidbuf[MAX_DEC_SIZE(int)];
        stc_error_e ret;
 
@@ -130,13 +111,17 @@ stc_error_e cgroup_write_pidtree(const char *cgroup_subsystem,
        fclose(f);
        return STC_ERROR_NONE;
 }
+//LCOV_EXCL_STOP
 
 int cgroup_write_node_uint32(const char *cgroup_name,
                             const char *file_name, uint32_t value)
 {
        char buf[MAX_PATH_LENGTH];
        snprintf(buf, sizeof(buf), "%s/%s", cgroup_name, file_name);
-       STC_LOGD("cgroup_buf %s, value %d\n", buf, value);
+
+       if (STC_STAT_LOG)
+               STC_LOGD("cgroup_buf %s, value %d\n", buf, value); //LCOV_EXCL_LINE
+
        return fwrite_uint(buf, value);
 }
 
@@ -145,7 +130,10 @@ int cgroup_write_node_str(const char *cgroup_name,
 {
        char buf[MAX_PATH_LENGTH];
        snprintf(buf, sizeof(buf), "%s/%s", cgroup_name, file_name);
-       STC_LOGD("cgroup_buf %s, string %s\n", buf, string);
+
+       if (STC_STAT_LOG)
+               STC_LOGD("cgroup_buf %s, string %s\n", buf, string); //LCOV_EXCL_LINE
+
        return fwrite_str(buf, string);
 }
 
@@ -156,7 +144,10 @@ int cgroup_read_node_uint32(const char *cgroup_name,
        int ret;
        snprintf(buf, sizeof(buf), "%s/%s", cgroup_name, file_name);
        ret = fread_uint(buf, value);
-       STC_LOGD("cgroup_buf %s, value %d\n", buf, *value);
+
+       if (STC_STAT_LOG)
+               STC_LOGD("cgroup_buf %s, value %d\n", buf, *value); //LCOV_EXCL_LINE
+
        return ret;
 }
 
@@ -184,12 +175,12 @@ int cgroup_make_subdir(const char *parentdir, const char *cgroup_name,
 
                if (parentdir && !strncmp(parentdir, DEFAULT_CGROUP,
                                          sizeof(DEFAULT_CGROUP))) {
-                       ret = mount("tmpfs", DEFAULT_CGROUP, "tmpfs",
+                       ret = mount("tmpfs", DEFAULT_CGROUP, "tmpfs", //LCOV_EXCL_LINE
                                    MS_REMOUNT|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME,
                                    "mode=755");
                        if (ret < 0) {
-                               STC_LOGE("Fail to RW mount cgroup directory. Can't make %s cgroup", cgroup_name);
-                               return STC_ERROR_FAIL;
+                               STC_LOGE("Fail to RW mount cgroup directory. Can't make %s cgroup", cgroup_name); //LCOV_EXCL_LINE
+                               return STC_ERROR_FAIL; //LCOV_EXCL_LINE
                        }
                        cgroup_remount = true;
                }
@@ -200,11 +191,11 @@ int cgroup_make_subdir(const char *parentdir, const char *cgroup_name,
                                 cgroup_name, errno);
 
                if (cgroup_remount) {
-                       ret = mount("tmpfs", DEFAULT_CGROUP, "tmpfs",
+                       ret = mount("tmpfs", DEFAULT_CGROUP, "tmpfs",  //LCOV_EXCL_LINE
                                    MS_REMOUNT|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME|MS_RDONLY,
                                    "mode=755");
                        if (ret < 0)
-                               STC_LOGD("Fail to RO mount");
+                               STC_LOGD("Fail to RO mount"); //LCOV_EXCL_LINE
                }
        }
 
@@ -214,51 +205,6 @@ int cgroup_make_subdir(const char *parentdir, const char *cgroup_name,
        return STC_ERROR_NONE;
 }
 
-/* FIXME: tasks is not removed from tasks list */
-int cgroup_remove_pid(const char *cgroup_subsystem, const char *cgroup_name,
-                     const int pid)
-{
-       char cgroup_tasks_file_path[MAX_PATH_LENGTH];
-       FILE *handler = 0;
-       guint i = 0;
-       pid_t pid_for_read = 0;
-       GArray *pids = NULL;
-       guint pid_count = 0;;
-
-       snprintf(cgroup_tasks_file_path, sizeof(cgroup_tasks_file_path),
-                "%s/%s/tasks", cgroup_subsystem, cgroup_name);
-
-       handler = fopen(cgroup_tasks_file_path, "r");
-       if (!handler) {
-               STC_LOGE("Read file open failed");
-               return -1;
-       }
-
-       pids = g_array_new(FALSE, FALSE, sizeof(pid_t));
-
-       while (read_uint(handler, (uint32_t *)&pid_for_read) >= 0) {
-               if (pid_for_read != pid) {
-                       pids = g_array_append_val(pids, pid_for_read);
-                       ++pid_count;
-               }
-       }
-
-       fclose(handler);
-
-       handler = fopen(cgroup_tasks_file_path, "w");
-       if (!handler) {
-               STC_LOGE("Write file open failed");
-               return -1;
-       }
-
-       for (i = 0; i < pid_count; i++)
-               write_uint(handler, g_array_index(pids, pid_t, i));
-
-       fclose(handler);
-       g_array_free(pids, TRUE);
-       return 0;
-}
-
 int cgroup_set_release_agent(const char *cgroup_subsys,
                             const char *release_agent)
 {
@@ -276,7 +222,7 @@ int cgroup_set_release_agent(const char *cgroup_subsys,
        return cgroup_write_node_str(buf, NOTIFY_ON_RELEASE, "1");
 }
 
-void cgroup_init(void)
+API void cgroup_init(void)
 {
        /* create stc cgroup directory */
        cgroup_make_subdir(CGROUP_NETWORK, STC_CGROUP_NAME, NULL);
@@ -285,10 +231,11 @@ void cgroup_init(void)
        cgroup_make_subdir(STC_CGROUP_NETWORK, STC_BACKGROUND_CGROUP_NAME,
                           NULL);
 
-       cgroup_write_node_uint32(BACKGROUND_CGROUP_NETWORK,
-               CLASSID_FILE_NAME, STC_BACKGROUND_APP_CLASSID);
-
        /* create foreground cgroup directory */
        cgroup_make_subdir(STC_CGROUP_NETWORK, STC_FOREGROUND_CGROUP_NAME,
                           NULL);
+
+       /* create tethering cgroup directory */
+       cgroup_make_subdir(STC_CGROUP_NETWORK, STC_TETHERING_CGROUP_NAME,
+                          NULL);
 }