From 490caf5d4ac56b2421e2e3d05666f9a7198f6edb Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Fri, 16 Mar 2012 01:46:50 +0100 Subject: [PATCH] cgroup_add_task(), cgroup_set_mem_limit*(): More careful error detection --- cgroup.c | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/cgroup.c b/cgroup.c index 882fa6a..ab09b65 100644 --- a/cgroup.c +++ b/cgroup.c @@ -66,8 +66,15 @@ cgroup_add_task(const char *chier, const char *cgroup, pid_t pid) cgroup_perror(tasksfile); return -1; } - fprintf(tasks, "%d\n", pid); - fclose(tasks); + if (fprintf(tasks, "%d\n", pid) < 0) { + cgroup_perror(tasksfile); + fclose(tasks); + return -1; + } + if (fclose(tasks) < 0) { + cgroup_perror(tasksfile); + return -1; + } return 1; } @@ -144,8 +151,15 @@ cgroup_set_mem_limit_do(const char *chier, const char *cgroup, size_t nlimit, ch cgroup_perror(limitfile); return -1; } - fprintf(limit, "%zu\n", nlimit); - fclose(limit); + if (fprintf(limit, "%zu\n", nlimit) < 0) { + cgroup_perror(limitfile); + fclose(limit); + return -1; + } + if (fclose(limit) < 0) { + cgroup_perror(limitfile); + return -1; + } return 1; } @@ -156,8 +170,10 @@ cgroup_set_mem_limit(const char *chier, const char *cgroup, size_t nlimit) * since normal <= memsw must hold. */ int ret = cgroup_set_mem_limit_do(chier, cgroup, nlimit, ""); #if SWAP_LIMIT - if (ret >= 0) - ret |= cgroup_set_mem_limit_do(chier, cgroup, nlimit, MEMSW); + if (ret >= 0) { + int ret2 = cgroup_set_mem_limit_do(chier, cgroup, nlimit, MEMSW); + ret = ret2 < 0 ? ret2 : (ret || ret2); + } #endif return ret; } -- 2.11.4.GIT