X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=src%2Fhelper%2Fhelper-cgroup.c;h=1d5ba2b7e6f4ec3fab2d1d7e60d7621bf75cf545;hb=refs%2Fheads%2Ftizen_5.5;hp=fe05ea14db3fdb6533f0d4ca89ac81785b7dc883;hpb=006d9d618412b49068c6aa4c2fc3833e32edc30a;p=platform%2Fcore%2Fconnectivity%2Fstc-manager.git diff --git a/src/helper/helper-cgroup.c b/src/helper/helper-cgroup.c index fe05ea1..1d5ba2b 100755 --- a/src/helper/helper-cgroup.c +++ b/src/helper/helper-cgroup.c @@ -20,27 +20,9 @@ #define NOTIFY_ON_RELEASE "notify_on_release" #define CGROUP_FILE_NAME "cgroup.procs" -#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"); +#define CLASSID_FILE_NAME "net_cls.classid" - ret = fputs(digit_buf, handler); - ret_value_errno_msg_if(ret == EOF, errno ? -errno : -EIO, - "Fail to write file"); - - return 0; -} +#define MAX_PATH_LENGTH 512 static bool cgroup_is_exists(const char *cgroup_full_path) { @@ -52,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; } @@ -67,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, @@ -87,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; @@ -128,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); } @@ -143,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); } @@ -154,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; } @@ -182,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; } @@ -198,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 } } @@ -212,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) { @@ -273,3 +221,21 @@ int cgroup_set_release_agent(const char *cgroup_subsys, return cgroup_write_node_str(buf, NOTIFY_ON_RELEASE, "1"); } + +API void cgroup_init(void) +{ + /* create stc cgroup directory */ + cgroup_make_subdir(CGROUP_NETWORK, STC_CGROUP_NAME, NULL); + + /* create background cgroup directory */ + cgroup_make_subdir(STC_CGROUP_NETWORK, STC_BACKGROUND_CGROUP_NAME, + NULL); + + /* 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); +}