]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
lxc-init: use cleanup macros
authorChristian Brauner <christian.brauner@ubuntu.com>
Sat, 9 Feb 2019 10:57:07 +0000 (11:57 +0100)
committerChristian Brauner <christian.brauner@ubuntu.com>
Sun, 17 Feb 2019 13:13:44 +0000 (14:13 +0100)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/Makefile.am
src/lxc/cmd/lxc_init.c

index ef19df9e082d72f16ccbed4099aa483eecb1edf5..35b805393f0d58551dc41c23ef4c8bc7a7207fe5 100644 (file)
@@ -363,6 +363,7 @@ init_lxc_SOURCES = cmd/lxc_init.c \
                   compiler.h \
                   error.h \
                   initutils.c initutils.h \
+                  memory_utils.h \
                   parse.c parse.h \
                   raw_syscalls.c raw_syscalls.h \
                   string_utils.c string_utils.h
@@ -411,6 +412,7 @@ init_lxc_static_SOURCES = cmd/lxc_init.c \
                          file_utils.c file_utils.h \
                          log.c log.h \
                          macro.h \
+                         memory_utils.h \
                          namespace.c namespace.h \
                          string_utils.c string_utils.h
 
index d9ba40b8d88af1e592d3052d944006752c773059..aca14d64b8955efc269fd3078b1b71c88f953bce 100644 (file)
@@ -47,6 +47,7 @@
 #include "config.h"
 #include "error.h"
 #include "initutils.h"
+#include "memory_utils.h"
 #include "parse.h"
 #include "raw_syscalls.h"
 #include "string_utils.h"
@@ -100,10 +101,10 @@ static struct arguments my_args = {
 
 static void prevent_forking(void)
 {
-       FILE *f;
-       size_t len = 0;
-       char *line = NULL;
+       __do_free char *line = NULL;
+       __do_fclose FILE *f = NULL;
        char path[PATH_MAX];
+       size_t len = 0;
 
        f = fopen("/proc/self/cgroup", "r");
        if (!f)
@@ -138,14 +139,14 @@ static void prevent_forking(void)
                if (ret < 0 || (size_t)ret >= sizeof(path)) {
                        if (my_args.quiet)
                                fprintf(stderr, "Failed to create string\n");
-                       goto on_error;
+                       return;
                }
 
                fd = open(path, O_WRONLY);
                if (fd < 0) {
                        if (my_args.quiet)
                                fprintf(stderr, "Failed to open \"%s\"\n", path);
-                       goto on_error;
+                       return;
                }
 
                ret = write(fd, "1", 1);
@@ -153,17 +154,13 @@ static void prevent_forking(void)
                        fprintf(stderr, "Failed to write to \"%s\"\n", path);
 
                close(fd);
-               break;
+               return;
        }
-
-on_error:
-       free(line);
-       fclose(f);
 }
 
 static void kill_children(pid_t pid)
 {
-       FILE *f;
+       __do_fclose FILE *f = NULL;
        char path[PATH_MAX];
        int ret;
 
@@ -187,15 +184,12 @@ static void kill_children(pid_t pid)
                if (fscanf(f, "%d ", &find_pid) != 1) {
                        if (my_args.quiet)
                                fprintf(stderr, "Failed to retrieve pid\n");
-                       fclose(f);
                        return;
                }
 
                (void)kill_children(find_pid);
                (void)kill(find_pid, SIGKILL);
        }
-
-       fclose(f);
 }
 
 static void remove_self(void)