From dce818b390a857a11f7dd634684500675cf79833 Mon Sep 17 00:00:00 2001 From: Kay Sievers Date: Thu, 12 Apr 2012 17:15:18 +0200 Subject: move all tools to subdirs --- src/detect-virt/detect-virt.c | 171 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 src/detect-virt/detect-virt.c (limited to 'src/detect-virt/detect-virt.c') diff --git a/src/detect-virt/detect-virt.c b/src/detect-virt/detect-virt.c new file mode 100644 index 0000000000..a83fb3ced5 --- /dev/null +++ b/src/detect-virt/detect-virt.c @@ -0,0 +1,171 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +/*** + This file is part of systemd. + + Copyright 2010 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see . +***/ + +#include +#include +#include +#include +#include + +#include "util.h" +#include "virt.h" +#include "build.h" + +static bool arg_quiet = false; +static enum { + ANY_VIRTUALIZATION, + ONLY_VM, + ONLY_CONTAINER +} arg_mode = ANY_VIRTUALIZATION; + +static int help(void) { + + printf("%s [OPTIONS...]\n\n" + "Detect execution in a virtualized environment.\n\n" + " -h --help Show this help\n" + " --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" + " -q --quiet Don't output anything, just set return value\n", + program_invocation_short_name); + + return 0; +} + +static int parse_argv(int argc, char *argv[]) { + + enum { + ARG_VERSION = 0x100 + }; + + static const struct option options[] = { + { "help", no_argument, NULL, 'h' }, + { "version", no_argument, NULL, ARG_VERSION }, + { "container", no_argument, NULL, 'c' }, + { "vm", optional_argument, NULL, 'v' }, + { "quiet", required_argument, NULL, 'q' }, + { NULL, 0, NULL, 0 } + }; + + int c; + + assert(argc >= 0); + assert(argv); + + while ((c = getopt_long(argc, argv, "hqcv", options, NULL)) >= 0) { + + switch (c) { + + case 'h': + help(); + return 0; + + case ARG_VERSION: + puts(PACKAGE_STRING); + puts(DISTRIBUTION); + puts(SYSTEMD_FEATURES); + return 0; + + case 'q': + arg_quiet = true; + break; + + case 'c': + arg_mode = ONLY_CONTAINER; + break; + + case 'v': + arg_mode = ONLY_VM; + break; + + case '?': + return -EINVAL; + + default: + log_error("Unknown option code %c", c); + return -EINVAL; + } + } + + if (optind < argc) { + help(); + return -EINVAL; + } + + return 1; +} + +int main(int argc, char *argv[]) { + const char *id = NULL; + int retval, r; + + /* This is mostly intended to be used for scripts which want + * to detect whether we are being run in a virtualized + * environment or not */ + + log_parse_environment(); + log_open(); + + r = parse_argv(argc, argv); + if (r <= 0) + return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; + + switch (arg_mode) { + + case ANY_VIRTUALIZATION: { + Virtualization v; + + v = detect_virtualization(&id); + if (v < 0) { + log_error("Failed to check for virtualization: %s", strerror(-v)); + return EXIT_FAILURE; + } + + retval = v != VIRTUALIZATION_NONE ? EXIT_SUCCESS : EXIT_FAILURE; + break; + } + + case ONLY_CONTAINER: + r = detect_container(&id); + if (r < 0) { + log_error("Failed to check for container: %s", strerror(-r)); + return EXIT_FAILURE; + } + + retval = r > 0 ? EXIT_SUCCESS : EXIT_FAILURE; + break; + + case ONLY_VM: + r = detect_vm(&id); + if (r < 0) { + log_error("Failed to check for vm: %s", strerror(-r)); + return EXIT_FAILURE; + } + + retval = r > 0 ? EXIT_SUCCESS : EXIT_FAILURE; + break; + } + + if (!arg_quiet) + puts(id ? id : "none"); + + return retval; +} -- cgit v1.2.3-54-g00ecf From 018ef268b1667ba0dbfc15804ab33deed6092147 Mon Sep 17 00:00:00 2001 From: Kay Sievers Date: Tue, 17 Apr 2012 18:42:09 +0200 Subject: silence a bunch of gcc warnings --- docs/gudev/Makefile.am | 2 +- src/detect-virt/detect-virt.c | 3 ++- src/shared/cgroup-util.c | 2 +- src/udev/udev-builtin.c | 2 +- 4 files changed, 5 insertions(+), 4 deletions(-) (limited to 'src/detect-virt/detect-virt.c') diff --git a/docs/gudev/Makefile.am b/docs/gudev/Makefile.am index f2e953247a..5c82167471 100644 --- a/docs/gudev/Makefile.am +++ b/docs/gudev/Makefile.am @@ -21,7 +21,7 @@ DOC_MAIN_SGML_FILE=$(DOC_MODULE)-docs.xml # gtk-doc will search all .c & .h files beneath here for inline comments # documenting the functions and macros. # e.g. DOC_SOURCE_DIR=../../../gtk -DOC_SOURCE_DIR=$(top_srcdir)/src/gudev +DOC_SOURCE_DIR=$(top_srcdir)/src/gudev $(top_builddir)/src/gudev # Extra options to pass to gtkdoc-scangobj. Not normally needed. SCANGOBJ_OPTIONS= diff --git a/src/detect-virt/detect-virt.c b/src/detect-virt/detect-virt.c index a83fb3ced5..8b8fc45ea0 100644 --- a/src/detect-virt/detect-virt.c +++ b/src/detect-virt/detect-virt.c @@ -115,7 +115,8 @@ static int parse_argv(int argc, char *argv[]) { int main(int argc, char *argv[]) { const char *id = NULL; - int retval, r; + int r; + int retval = EXIT_SUCCESS; /* This is mostly intended to be used for scripts which want * to detect whether we are being run in a virtualized diff --git a/src/shared/cgroup-util.c b/src/shared/cgroup-util.c index 3cb41c7c5f..d34c142729 100644 --- a/src/shared/cgroup-util.c +++ b/src/shared/cgroup-util.c @@ -514,7 +514,7 @@ static const char *normalize_controller(const char *controller) { } static int join_path(const char *controller, const char *path, const char *suffix, char **fs) { - char *t; + char *t = NULL; if (!(controller || path)) return -EINVAL; diff --git a/src/udev/udev-builtin.c b/src/udev/udev-builtin.c index b8cdc708bc..6509f5881d 100644 --- a/src/udev/udev-builtin.c +++ b/src/udev/udev-builtin.c @@ -42,7 +42,7 @@ static const struct udev_builtin *builtins[] = { int udev_builtin_init(struct udev *udev) { unsigned int i; - int err; + int err = 0; for (i = 0; i < ELEMENTSOF(builtins); i++) { if (builtins[i]->init) { -- cgit v1.2.3-54-g00ecf From 8c32f0143b15e4c18142cce6703f66e26db69899 Mon Sep 17 00:00:00 2001 From: Kay Sievers Date: Mon, 4 Jun 2012 12:39:22 +0200 Subject: systemd-detect-virt: fix "option '--quiet' requires an argument" https://bugs.freedesktop.org/show_bug.cgi?id=50671 --- src/detect-virt/detect-virt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/detect-virt/detect-virt.c') diff --git a/src/detect-virt/detect-virt.c b/src/detect-virt/detect-virt.c index 8b8fc45ea0..bbb9dfc19a 100644 --- a/src/detect-virt/detect-virt.c +++ b/src/detect-virt/detect-virt.c @@ -61,7 +61,7 @@ static int parse_argv(int argc, char *argv[]) { { "version", no_argument, NULL, ARG_VERSION }, { "container", no_argument, NULL, 'c' }, { "vm", optional_argument, NULL, 'v' }, - { "quiet", required_argument, NULL, 'q' }, + { "quiet", no_argument, NULL, 'q' }, { NULL, 0, NULL, 0 } }; -- cgit v1.2.3-54-g00ecf