]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
cgpath: add logging
authorChristian Brauner <christian.brauner@ubuntu.com>
Tue, 2 Feb 2021 21:55:52 +0000 (22:55 +0100)
committerChristian Brauner <christian.brauner@ubuntu.com>
Tue, 2 Feb 2021 21:56:10 +0000 (22:56 +0100)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/tests/cgpath.c

index 5e3eb56c336940083071edab523e80bd6b582df6..899f826644d8ee7bac6b9104d98f88eafd587a9d 100644 (file)
 #include <sys/stat.h>
 
 #include "cgroup.h"
-#include "lxc.h"
 #include "commands.h"
+#include "lxc.h"
+#include "lxctest.h"
+#include "utils.h"
 
 #ifndef HAVE_STRLCPY
 #include "include/strlcpy.h"
  */
 static int test_running_container(const char *lxcpath, const char *name)
 {
+       __do_close int fd_log = -EBADF;
        int ret = -1;
        struct lxc_container *c = NULL;
+       struct lxc_log log = {};
+       char template[sizeof(P_tmpdir"/attach_XXXXXX")];
        char *cgrelpath;
        char  relpath[PATH_MAX+1];
        char  value[NAME_MAX], value_save[NAME_MAX];
        struct cgroup_ops *cgroup_ops;
 
+       (void)strlcpy(template, P_tmpdir"/attach_XXXXXX", sizeof(template));
+
+       fd_log = lxc_make_tmpfile(template, false);
+       if (fd_log < 0) {
+               lxc_error("Failed to create temporary log file for container %s\n", name);
+               exit(EXIT_FAILURE);
+       }
+       log.name = name;
+       log.file = template;
+       log.level = "TRACE";
+       log.prefix = "cgpath";
+       log.quiet = false;
+       log.lxcpath = NULL;
+       if (lxc_log_init(&log))
+               goto err1;
+
        sprintf(relpath, DEFAULT_PAYLOAD_CGROUP_PREFIX "%s", name);
 
        if ((c = lxc_container_new(name, lxcpath)) == NULL) {
@@ -123,6 +144,17 @@ err3:
 err2:
        lxc_container_put(c);
 err1:
+
+       if (ret != 0) {
+               char buf[4096];
+               ssize_t buflen;
+               while ((buflen = read(fd_log, buf, 1024)) > 0) {
+                       buflen = write(STDERR_FILENO, buf, buflen);
+                       if (buflen <= 0)
+                               break;
+               }
+       }
+       (void)unlink(template);
        return ret;
 }