[systemd-devel] [PATCH] detect_virtualization: make Virtualization an out value

Lennart Poettering lennart at poettering.net
Sun Feb 23 12:39:28 PST 2014


On Sat, 22.02.14 14:36, Thomas H.P. Andersen (phomes at gmail.com) wrote:

> From: Thomas Hindoe Paaboel Andersen <phomes at gmail.com>
> 
> The return value from detect_virtualization used to be a
> Virtualization enum but in cases of error it would also be a
> negative errno. This caused a warning in clang when test-architecture
> began comparing the return value to -EPERM and -EACCES.

I'd prefer to go the other way: make this an anonymous enum, and return
"int"...

> ---
>  src/core/dbus-manager.c       |  2 +-
>  src/core/main.c               |  2 +-
>  src/detect-virt/detect-virt.c |  6 +++---
>  src/hostname/hostnamectl.c    |  3 ++-
>  src/hostname/hostnamed.c      |  2 +-
>  src/shared/condition-util.c   |  8 ++++----
>  src/shared/virt.c             | 20 ++++++++++++++------
>  src/shared/virt.h             |  2 +-
>  src/test/test-architecture.c  |  5 +++--
>  9 files changed, 30 insertions(+), 20 deletions(-)
> 
> diff --git a/src/core/dbus-manager.c b/src/core/dbus-manager.c
> index 75004cb..f5a06ba 100644
> --- a/src/core/dbus-manager.c
> +++ b/src/core/dbus-manager.c
> @@ -84,7 +84,7 @@ static int property_get_virtualization(
>          assert(bus);
>          assert(reply);
>  
> -        detect_virtualization(&id);
> +        detect_virtualization(&id, NULL);
>  
>          return sd_bus_message_append(reply, "s", id);
>  }
> diff --git a/src/core/main.c b/src/core/main.c
> index 086e283..5facd17 100644
> --- a/src/core/main.c
> +++ b/src/core/main.c
> @@ -1507,7 +1507,7 @@ int main(int argc, char *argv[]) {
>  
>                  log_info(PACKAGE_STRING " running in system mode. (" SYSTEMD_FEATURES ")");
>  
> -                detect_virtualization(&virtualization);
> +                detect_virtualization(&virtualization, NULL);
>                  if (virtualization)
>                          log_info("Detected virtualization '%s'.", virtualization);
>  
> diff --git a/src/detect-virt/detect-virt.c b/src/detect-virt/detect-virt.c
> index 2f8b0eb..698658c 100644
> --- a/src/detect-virt/detect-virt.c
> +++ b/src/detect-virt/detect-virt.c
> @@ -131,9 +131,9 @@ int main(int argc, char *argv[]) {
>          case ANY_VIRTUALIZATION: {
>                  Virtualization v;
>  
> -                v = detect_virtualization(&id);
> -                if (v < 0) {
> -                        log_error("Failed to check for virtualization: %s", strerror(-v));
> +                r = detect_virtualization(&id, &v);
> +                if (r < 0) {
> +                        log_error("Failed to check for virtualization: %s", strerror(-r));
>                          return EXIT_FAILURE;
>                  }
>  
> diff --git a/src/hostname/hostnamectl.c b/src/hostname/hostnamectl.c
> index e455249..2e1e6d7 100644
> --- a/src/hostname/hostnamectl.c
> +++ b/src/hostname/hostnamectl.c
> @@ -104,7 +104,8 @@ static void print_status_info(StatusInfo *i) {
>          if (r >= 0)
>                  printf("           Boot ID: " SD_ID128_FORMAT_STR "\n", SD_ID128_FORMAT_VAL(bid));
>  
> -        if (detect_virtualization(&id) > 0)
> +        r = detect_virtualization(&id, NULL);
> +        if (r > 0)
>                  printf("    Virtualization: %s\n", id);
>  
>          r = parse_env_file("/etc/os-release", NEWLINE,
> diff --git a/src/hostname/hostnamed.c b/src/hostname/hostnamed.c
> index e57891b..2e36d01 100644
> --- a/src/hostname/hostnamed.c
> +++ b/src/hostname/hostnamed.c
> @@ -125,7 +125,7 @@ static const char* fallback_chassis(void) {
>          unsigned t;
>          Virtualization v;
>  
> -        v = detect_virtualization(NULL);
> +        r = detect_virtualization(NULL, &v);
>  
>          if (v == VIRTUALIZATION_VM)
>                  return "vm";
> diff --git a/src/shared/condition-util.c b/src/shared/condition-util.c
> index 4aea3ca..44feabe 100644
> --- a/src/shared/condition-util.c
> +++ b/src/shared/condition-util.c
> @@ -121,7 +121,7 @@ bool condition_test_kernel_command_line(Condition *c) {
>  }
>  
>  bool condition_test_virtualization(Condition *c) {
> -        int b;
> +        int b, r;
>          Virtualization v;
>          const char *id;
>  
> @@ -129,9 +129,9 @@ bool condition_test_virtualization(Condition *c) {
>          assert(c->parameter);
>          assert(c->type == CONDITION_VIRTUALIZATION);
>  
> -        v = detect_virtualization(&id);
> -        if (v < 0) {
> -                log_warning("Failed to detect virtualization, ignoring: %s", strerror(-v));
> +        r = detect_virtualization(&id, &v);
> +        if (r < 0) {
> +                log_warning("Failed to detect virtualization, ignoring: %s", strerror(-r));
>                  return c->negate;
>          }
>  
> diff --git a/src/shared/virt.c b/src/shared/virt.c
> index c79d35d..73ce9ff 100644
> --- a/src/shared/virt.c
> +++ b/src/shared/virt.c
> @@ -278,20 +278,28 @@ finish:
>  }
>  
>  /* Returns a short identifier for the various VM/container implementations */
> -Virtualization detect_virtualization(const char **id) {
> +int detect_virtualization(const char **id, Virtualization *type) {
>          int r;
>  
>          r = detect_container(id);
>          if (r < 0)
>                  return r;
> -        if (r > 0)
> -                return VIRTUALIZATION_CONTAINER;
> +        if (r > 0) {
> +                if (type != NULL)
> +                        *type = VIRTUALIZATION_CONTAINER;
> +                return r;
> +        }
>  
>          r = detect_vm(id);
>          if (r < 0)
>                  return r;
> -        if (r > 0)
> -                return VIRTUALIZATION_VM;
> +        if (r > 0) {
> +                if (type != NULL)
> +                         *type = VIRTUALIZATION_VM;
> +                return r;
> +        }
>  
> -        return VIRTUALIZATION_NONE;
> +        if (type != NULL)
> +                *type = VIRTUALIZATION_NONE;
> +        return 0;
>  }
> diff --git a/src/shared/virt.h b/src/shared/virt.h
> index aa6ad35..6c9cb0f 100644
> --- a/src/shared/virt.h
> +++ b/src/shared/virt.h
> @@ -32,4 +32,4 @@ typedef enum Virtualization {
>          _VIRTUALIZATION_INVALID = -1
>  } Virtualization;
>  
> -Virtualization detect_virtualization(const char **id);
> +int detect_virtualization(const char **id, Virtualization *type);
> diff --git a/src/test/test-architecture.c b/src/test/test-architecture.c
> index b586c0d..bc17b15 100644
> --- a/src/test/test-architecture.c
> +++ b/src/test/test-architecture.c
> @@ -27,10 +27,11 @@
>  int main(int argc, char *argv[]) {
>          Architecture a;
>          Virtualization v;
> +        int r;
>          const char *id = NULL;
>  
> -        v = detect_virtualization(&id);
> -        if (v == -EPERM || v == -EACCES)
> +        r = detect_virtualization(&id, &v);
> +        if (r == -EPERM || r == -EACCES)
>                  return EXIT_TEST_SKIP;
>  
>          assert_se(v >= 0);


Lennart

-- 
Lennart Poettering, Red Hat


More information about the systemd-devel mailing list