From 20cafc9b23448aaab4fd8f07a5a1defd0cd7038a Mon Sep 17 00:00:00 2001 From: Jakub Filak Date: Tue, 1 Sep 2015 13:05:48 +0200 Subject: [PATCH] os: add support for OS Variant VARIANT= and VARIANT_ID= fields are optional. http://www.freedesktop.org/software/systemd/man/os-release.html Related to abrt/abrt#995 Resolves: #1342469 Signed-off-by: Jakub Filak Conflicts: include/operating_system.h lib/operating_system.c --- include/operating_system.h | 1 + lib/operating_system.c | 16 ++++++++++++++++ tests/operating_system.at | 43 +++++++++++++++++++++++++++++++++++++++---- 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/include/operating_system.h b/include/operating_system.h index f26e322..84fcce0 100644 --- a/include/operating_system.h +++ b/include/operating_system.h @@ -37,6 +37,7 @@ struct sr_operating_system char *cpe; /* Uptime in seconds. */ uint64_t uptime; + char *variant; }; struct sr_operating_system * diff --git a/lib/operating_system.c b/lib/operating_system.c index 64f7439..9bebe5b 100644 --- a/lib/operating_system.c +++ b/lib/operating_system.c @@ -43,6 +43,7 @@ sr_operating_system_init(struct sr_operating_system *operating_system) operating_system->version = NULL; operating_system->architecture = NULL; operating_system->cpe = NULL; + operating_system->variant = NULL; operating_system->uptime = 0; } @@ -56,6 +57,7 @@ sr_operating_system_free(struct sr_operating_system *operating_system) free(operating_system->version); free(operating_system->architecture); free(operating_system->cpe); + free(operating_system->variant); free(operating_system); } @@ -92,6 +94,13 @@ sr_operating_system_to_json(struct sr_operating_system *operating_system) sr_strbuf_append_str(strbuf, "\n"); } + if (operating_system->variant) + { + sr_strbuf_append_str(strbuf, ", \"variant\": "); + sr_json_append_escaped(strbuf, operating_system->variant); + sr_strbuf_append_str(strbuf, "\n"); + } + if (operating_system->uptime > 0) { sr_strbuf_append_strf(strbuf, @@ -122,6 +131,9 @@ sr_operating_system_from_json(struct sr_json_value *root, char **error_message) JSON_READ_STRING(root, "architecture", &result->architecture) && JSON_READ_UINT64(root, "uptime", &result->uptime); + /* variant is optional - failure is not fatal */ + JSON_READ_STRING(root, "variant", &result->variant); + if (!success) { sr_operating_system_free(result); @@ -194,6 +206,10 @@ os_release_callback(char *key, char *value, void *data) { operating_system->cpe = value; } + else if (0 == strcmp(key, "VARIANT_ID")) + { + operating_system->variant = value; + } else { free(value); diff --git a/tests/operating_system.at b/tests/operating_system.at index 0db2b9d..044d4bd 100644 --- a/tests/operating_system.at +++ b/tests/operating_system.at @@ -46,12 +46,14 @@ AT_TESTFUN([sr_operating_system_parse_etc_os_release], [[ #include "operating_system.h" #include "utils.h" +#include #include #include void check(const char *etc_os_release, const char *expected_name, - const char *expected_version) + const char *expected_version, + const char *expected_variant) { struct sr_operating_system *os = sr_operating_system_new(); bool success = sr_operating_system_parse_etc_os_release( @@ -60,6 +62,20 @@ void check(const char *etc_os_release, assert(success); assert(0 == strcmp(os->name, expected_name)); assert(0 == strcmp(os->version, expected_version)); + + if ( ( (expected_variant != NULL && os->variant == NULL) + || (expected_variant == NULL && os->variant != NULL) + ) + || ( (expected_variant != NULL && os->variant != NULL) + && 0 != strcmp(os->version, expected_version) + ) + ) + { + fprintf(stderr, "Expected: '%s'\n", expected_variant); + fprintf(stderr, "Got : '%s'\n", os->variant); + abort(); + } + sr_operating_system_free(os); } @@ -102,9 +118,28 @@ main(void) "REDHAT_SUPPORT_PRODUCT=\"Red Hat Enterprise Linux\"\n" "REDHAT_SUPPORT_PRODUCT_VERSION=7.0\n"; - check(f19, "fedora", "19"); - check(raw, "fedora", "rawhide"); - check(el7, "rhel", "7.0"); + char *f23 = +"NAME=Fedora\n" +"VERSION=\"23 (Workstation Edition)\"\n" +"ID=fedora\n" +"VERSION_ID=23\n" +"PRETTY_NAME=\"Fedora 23 (Workstation Edition)\"\n" +"ANSI_COLOR=\"0;34\"\n" +"CPE_NAME=\"cpe:/o:fedoraproject:fedora:23\"\n" +"HOME_URL=\"https://fedoraproject.org/\"\n" +"BUG_REPORT_URL=\"https://bugzilla.redhat.com/\"\n" +"REDHAT_BUGZILLA_PRODUCT=\"Fedora\"\n" +"REDHAT_BUGZILLA_PRODUCT_VERSION=23\n" +"REDHAT_SUPPORT_PRODUCT=\"Fedora\"\n" +"REDHAT_SUPPORT_PRODUCT_VERSION=23\n" +"PRIVACY_POLICY_URL=https://fedoraproject.org/wiki/Legal:PrivacyPolicy\n" +"VARIANT=\"Workstation Edition\"\n" +"VARIANT_ID=workstation\n"; + + check(f19, "fedora", "19", NULL); + check(raw, "fedora", "rawhide", NULL); + check(el7, "rhel", "7.0", NULL); + check(f23, "fedora", "23", "workstation"); return 0; } -- 1.8.3.1