]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
lxc-execute to report exit code of started application
authorMichel Normand <normand@fr.ibm.com>
Mon, 18 May 2009 20:27:35 +0000 (22:27 +0200)
committerDaniel Lezcano <dlezcano@fr.ibm.com>
Mon, 18 May 2009 20:27:35 +0000 (22:27 +0200)
The exit code of the application as reported by lxc-execute is:
0-126       exit code of the application itself
128+n       signal n received by the application
255         lxc error

Note that this is the same type of changes as done
for lxc-start command line.

Signed-off-by: Michel Normand <normand@fr.ibm.com>
Signed-off-by: Daniel Lezcano <dlezcano@fr.ibm.com>
src/lxc/lxc_execute.c
src/lxc/lxc_init.c

index eb7ef9cbdfc3c2e65b146541b8cca06f8b7a67e0..aea7a3c5693ad50fce2914d788ff7f5aae7db583 100644 (file)
@@ -81,7 +81,7 @@ int main(int argc, char *argv[])
        int opt;
        int nbargs = 0;
        int autodestroy = 0;
-       int ret = 1;
+       int ret = -1;
        struct lxc_conf lxc_conf;
 
        if (lxc_arguments_parse(&my_args, argc, argv))
@@ -131,17 +131,12 @@ int main(int argc, char *argv[])
                args[nbargs++] = my_args.argv[opt];
 
        ret = lxc_start(my_args.name, args);
-       if (ret) {
-               ERROR("failed to start '%s'", my_args.name);
-               goto out;
-       }
-
-       ret = 0;
 out:
        if (autodestroy) {
                if (lxc_destroy(my_args.name)) {
                        ERROR("failed to destroy '%s'", my_args.name);
-                       ret = 1;
+                       if (!ret)
+                               ret = -1;
                }
        }
 
index 5b00d147a112bc3c4be28822b367475e052149ae..964730706584a2a0452b36c477260a0c8eb2f91e 100644 (file)
@@ -33,6 +33,7 @@
 #define _GNU_SOURCE
 #include <getopt.h>
 #include "log.h"
+#include "error.h"
 
 lxc_log_define(lxc_init, lxc);
 
@@ -55,6 +56,7 @@ int main(int argc, char *argv[])
 {
        pid_t pid;
        int nbargs = 0;
+       int err = -1;
        char **aargv;
 
        while (1) {
@@ -66,17 +68,17 @@ int main(int argc, char *argv[])
                case 'o':       log_file = optarg; break;
                case 'l':       log_priority = optarg; break;
                case '?':
-                       exit(1);
+                       exit(err);
                }
                nbargs++;
        }
 
        if (lxc_log_init(log_file, log_priority, basename(argv[0]), quiet))
-               exit(1);
+               exit(err);
 
        if (!argv[optind]) {
                ERROR("missing command to launch");
-               exit(1);
+               exit(err);
        }
 
        aargv = &argv[optind];
@@ -85,36 +87,41 @@ int main(int argc, char *argv[])
        pid = fork();
        
        if (pid < 0)
-               exit(1);
+               exit(err);
 
        if (!pid) {
                
                if (mount_sysfs && mount("sysfs", "/sys", "sysfs", 0, NULL)) {
                        ERROR("failed to mount '/sys' : %s", strerror(errno));
-                       exit(1);
+                       exit(err);
                }
                
                if (mount_procfs && mount("proc", "/proc", "proc", 0, NULL)) {
                        ERROR("failed to mount '/proc' : %s", strerror(errno));
-                       exit(1);
+                       exit(err);
                }
 
                execvp(aargv[0], aargv);
                ERROR("failed to exec: '%s' : %s", aargv[0], strerror(errno));
-               exit(1);
+               exit(err);
        }
 
        for (;;) {
                int status;
-               if (wait(&status) < 0) {
+               pid_t waited_pid;
+
+               waited_pid = wait(&status);
+               if (waited_pid < 0) {
                        if (errno == ECHILD)
-                               exit(0);
+                               goto out;
                        if (errno == EINTR)
                                continue;
-                       ERROR("failed to wait child");
-                       return 1;
+                       ERROR("failed to wait child : %s", strerror(errno));
+                       goto out;
+               } else {
+                       err = lxc_error_set_and_log(waited_pid, status);
                }
        }
-
-       return 0;
+out:
+       return err;
 }