]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
cgroups: improve error handling and logging in cgroup_attach_leaf()
authorChristian Brauner <christian.brauner@ubuntu.com>
Thu, 4 Feb 2021 14:02:14 +0000 (15:02 +0100)
committerChristian Brauner <christian.brauner@ubuntu.com>
Thu, 4 Feb 2021 14:59:53 +0000 (15:59 +0100)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/cgroups/cgfsng.c

index 3db1b60ace3d316af5abb4982c92c010b744e3cf..f4eccbd7620239b4c3bbba11995cdc662b89ae0e 100644 (file)
@@ -2233,23 +2233,26 @@ static int cgroup_attach_leaf(const struct lxc_conf *conf, int unified_fd, pid_t
        int idx = 1;
        int ret;
        char pidstr[INTTYPE_TO_STRLEN(int64_t) + 1];
-       size_t pidstr_len;
+       ssize_t pidstr_len;
 
        /* Create leaf cgroup. */
        ret = mkdirat(unified_fd, ".lxc", 0755);
        if (ret < 0 && errno != EEXIST)
-               return log_error_errno(-1, errno, "Failed to create leaf cgroup \".lxc\"");
+               return log_error_errno(-errno, errno, "Failed to create leaf cgroup \".lxc\"");
+
+       pidstr_len = snprintf(pidstr, sizeof(pidstr), INT64_FMT, (int64_t)pid);
+       if (pidstr_len < 0 || (size_t)pidstr_len >= sizeof(pidstr))
+               return ret_errno(EIO);
 
-       pidstr_len = sprintf(pidstr, INT64_FMT, (int64_t)pid);
        ret = lxc_writeat(unified_fd, ".lxc/cgroup.procs", pidstr, pidstr_len);
        if (ret < 0)
                ret = lxc_writeat(unified_fd, "cgroup.procs", pidstr, pidstr_len);
        if (ret == 0)
-               return 0;
+               return log_trace(0, "Moved process %s into cgroup %d(.lxc)", pidstr, unified_fd);
 
        /* this is a non-leaf node */
        if (errno != EBUSY)
-               return log_error_errno(-1, errno, "Failed to attach to unified cgroup");
+               return log_error_errno(-errno, errno, "Failed to attach to unified cgroup");
 
        do {
                bool rm = false;
@@ -2281,7 +2284,7 @@ static int cgroup_attach_leaf(const struct lxc_conf *conf, int unified_fd, pid_t
 
                ret = lxc_writeat(unified_fd, attach_cgroup, pidstr, pidstr_len);
                if (ret == 0)
-                       return 0;
+                       return log_trace(0, "Moved process %s into cgroup %d(%s)", pidstr, unified_fd, attach_cgroup);
 
                if (rm && unlinkat(unified_fd, attach_cgroup, AT_REMOVEDIR))
                        SYSERROR("Failed to remove cgroup \"%d(%s)\"", unified_fd, attach_cgroup);