diff options
Diffstat (limited to 'src/detect-virt/detect-virt.c')
-rw-r--r-- | src/detect-virt/detect-virt.c | 42 |
1 files changed, 26 insertions, 16 deletions
diff --git a/src/detect-virt/detect-virt.c b/src/detect-virt/detect-virt.c index 97ae569ca5..0a256c29be 100644 --- a/src/detect-virt/detect-virt.c +++ b/src/detect-virt/detect-virt.c @@ -19,20 +19,20 @@ along with systemd; If not, see <http://www.gnu.org/licenses/>. ***/ -#include <stdlib.h> -#include <stdbool.h> #include <errno.h> #include <getopt.h> +#include <stdbool.h> +#include <stdlib.h> #include "util.h" #include "virt.h" -#include "build.h" static bool arg_quiet = false; static enum { ANY_VIRTUALIZATION, ONLY_VM, - ONLY_CONTAINER + ONLY_CONTAINER, + ONLY_CHROOT, } arg_mode = ANY_VIRTUALIZATION; static void help(void) { @@ -42,6 +42,7 @@ static void help(void) { " --version Show package version\n" " -c --container Only detect whether we are run in a container\n" " -v --vm Only detect whether we are run in a VM\n" + " -r --chroot Detect whether we are run in a chroot() environment\n" " -q --quiet Don't output anything, just set return value\n" , program_invocation_short_name); } @@ -56,7 +57,8 @@ static int parse_argv(int argc, char *argv[]) { { "help", no_argument, NULL, 'h' }, { "version", no_argument, NULL, ARG_VERSION }, { "container", no_argument, NULL, 'c' }, - { "vm", optional_argument, NULL, 'v' }, + { "vm", no_argument, NULL, 'v' }, + { "chroot", no_argument, NULL, 'r' }, { "quiet", no_argument, NULL, 'q' }, {} }; @@ -66,7 +68,7 @@ static int parse_argv(int argc, char *argv[]) { assert(argc >= 0); assert(argv); - while ((c = getopt_long(argc, argv, "hqcv", options, NULL)) >= 0) + while ((c = getopt_long(argc, argv, "hqcvr", options, NULL)) >= 0) switch (c) { @@ -75,9 +77,7 @@ static int parse_argv(int argc, char *argv[]) { return 0; case ARG_VERSION: - puts(PACKAGE_STRING); - puts(SYSTEMD_FEATURES); - return 0; + return version(); case 'q': arg_quiet = true; @@ -91,6 +91,10 @@ static int parse_argv(int argc, char *argv[]) { arg_mode = ONLY_VM; break; + case 'r': + arg_mode = ONLY_CHROOT; + break; + case '?': return -EINVAL; @@ -99,8 +103,7 @@ static int parse_argv(int argc, char *argv[]) { } if (optind < argc) { - log_error("%s takes no arguments.", - program_invocation_short_name); + log_error("%s takes no arguments.", program_invocation_short_name); return -EINVAL; } @@ -108,7 +111,7 @@ static int parse_argv(int argc, char *argv[]) { } int main(int argc, char *argv[]) { - int retval = EXIT_SUCCESS, r; + int r; /* This is mostly intended to be used for scripts which want * to detect whether we are being run in a virtualized @@ -126,7 +129,7 @@ int main(int argc, char *argv[]) { case ONLY_VM: r = detect_vm(); if (r < 0) { - log_error_errno(r, "Failed to check for vm: %m"); + log_error_errno(r, "Failed to check for VM: %m"); return EXIT_FAILURE; } @@ -141,6 +144,15 @@ int main(int argc, char *argv[]) { break; + case ONLY_CHROOT: + r = running_in_chroot(); + if (r < 0) { + log_error_errno(r, "Failed to check for chroot() environment: %m"); + return EXIT_FAILURE; + } + + return r ? EXIT_SUCCESS : EXIT_FAILURE; + case ANY_VIRTUALIZATION: default: r = detect_virtualization(); @@ -155,7 +167,5 @@ int main(int argc, char *argv[]) { if (!arg_quiet) puts(virtualization_to_string(r)); - retval = r != VIRTUALIZATION_NONE ? EXIT_SUCCESS : EXIT_FAILURE; - - return retval; + return r != VIRTUALIZATION_NONE ? EXIT_SUCCESS : EXIT_FAILURE; } |