]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
libcgroup: check for return code of fprintf()
authorDhaval Giani <dhaval@linux.vnet.ibm.com>
Wed, 13 Aug 2008 13:33:59 +0000 (13:33 +0000)
committerDhaval Giani <dhaval@linux.vnet.ibm.com>
Wed, 13 Aug 2008 13:33:59 +0000 (13:33 +0000)
From: Vivek Goyal <vgoyal@redhat.com>

o Adding a task to a particular cgroup currently does fprintf() and never
  check the return code. Modify it to check for the return code in case
  fprintf() and fflush() did not succeed.

o I ran into the issues when i can't add migration thread to
  /container/admingroup/tasks and it will fail silently.

Signed-off-by: Vivek Goyal <vgoyal@redhat.com>
Signed-off-by: Dhaval Giani <dhaval@linux.vnet.ibm.com>
git-svn-id: https://libcg.svn.sourceforge.net/svnroot/libcg/trunk@133 4f4bb910-9a46-0410-90c8-c897d4f1cd53

api.c

diff --git a/api.c b/api.c
index 39703acbfc957bcd6f36c85f0eeb81a395031196..41e0c6dcf5ade6704b8c03b8e8171406d92d22ed 100644 (file)
--- a/api.c
+++ b/api.c
@@ -309,7 +309,7 @@ int cgroup_attach_task_pid(struct cgroup *cgroup, pid_t tid)
 {
        char path[FILENAME_MAX];
        FILE *tasks;
-       int i;
+       int i, ret = 0;
 
        if (!cgroup_initialized) {
                dbg ("libcgroup is not initialized\n");
@@ -335,7 +335,21 @@ int cgroup_attach_task_pid(struct cgroup *cgroup, pid_t tid)
                                        return ECGROUPNOTALLOWED;
                                }
                        }
-                       fprintf(tasks, "%d", tid);
+                       ret = fprintf(tasks, "%d", tid);
+                       if (ret < 0) {
+                               dbg("Error writing tid %d to %s:%s\n",
+                                               tid, path, strerror(errno));
+                               fclose(tasks);
+                               return ECGOTHER;
+                       }
+
+                       ret = fflush(tasks);
+                       if (ret) {
+                               dbg("Error writing tid  %d to %s:%s\n",
+                                               tid, path, strerror(errno));
+                               fclose(tasks);
+                               return ECGOTHER;
+                       }
                        fclose(tasks);
                }
                pthread_rwlock_unlock(&cg_mount_table_lock);
@@ -366,12 +380,24 @@ int cgroup_attach_task_pid(struct cgroup *cgroup, pid_t tid)
                                        return ECGROUPNOTALLOWED;
                                }
                        }
-                       fprintf(tasks, "%d", tid);
+                       ret = fprintf(tasks, "%d", tid);
+                       if (ret < 0) {
+                               dbg("Error writing tid %d to %s:%s\n",
+                                               tid, path, strerror(errno));
+                               fclose(tasks);
+                               return ECGOTHER;
+                       }
+                       ret = fflush(tasks);
+                       if (ret) {
+                               dbg("Error writing tid  %d to %s:%s\n",
+                                               tid, path, strerror(errno));
+                               fclose(tasks);
+                               return ECGOTHER;
+                       }
                        fclose(tasks);
                }
        }
        return 0;
-
 }
 
 /** cgroup_attach_task is used to attach the current thread to a cgroup.