From 5be6f2f60e3225bf3d214432044721fe474d55d7 Mon Sep 17 00:00:00 2001 From: Danny Al-Gaaf Date: Wed, 25 Jun 2014 00:31:48 +0200 Subject: [PATCH] osd/OSD.cc: parse lsb release data via lsb_release Use lsb_release tool to be portable since parsing /etc/lsb-release is not the same between different distributions. The old code failed e.g. for SUSE products to parse LSB information. Fixes: #8654 Signed-off-by: Danny Al-Gaaf (cherry picked from commit 0b3a3987d382ff33fdf892f189b30df22be80e59) --- src/osd/OSD.cc | 76 +++++++++++++++++++++++++++++++------------------- src/osd/OSD.h | 2 ++ 2 files changed, 50 insertions(+), 28 deletions(-) diff --git a/src/osd/OSD.cc b/src/osd/OSD.cc index 0da860bd95225..586b7d707b644 100644 --- a/src/osd/OSD.cc +++ b/src/osd/OSD.cc @@ -3762,6 +3762,53 @@ void OSD::_send_boot() monc->send_mon_message(mboot); } +bool OSD::_lsb_release_set (char *buf, const char *str, map *pm, const char *key) +{ + if (strncmp (buf, str, strlen (str)) == 0) { + char *value; + + if (buf[strlen(buf)-1] == '\n') + buf[strlen(buf)-1] = '\0'; + + value = buf + strlen (str) + 1; + (*pm)[key] = value; + + return true; + } + return false; +} + +void OSD::_lsb_release_parse (map *pm) +{ + FILE *fp = NULL; + char buf[512]; + + fp = popen("lsb_release -idrc", "r"); + if (!fp) { + int ret = -errno; + derr << "lsb_release_parse - failed to call lsb_release binary with error: " << cpp_strerror(ret) << dendl; + return; + } + + while (fgets(buf, sizeof(buf) - 1, fp) != NULL) { + if (_lsb_release_set(buf, "Distributor ID:", pm, "distro")) + continue; + if (_lsb_release_set(buf, "Description:", pm, "distro_description")) + continue; + if (_lsb_release_set(buf, "Release:", pm, "distro_version")) + continue; + if (_lsb_release_set(buf, "Codename:", pm, "distro_codename")) + continue; + + derr << "unhandled output: " << buf << dendl; + } + + if (pclose(fp)) { + int ret = -errno; + derr << "lsb_release_parse - pclose failed: " << cpp_strerror(ret) << dendl; + } +} + void OSD::_collect_metadata(map *pm) { (*pm)["ceph_version"] = pretty_version_to_str(); @@ -3831,34 +3878,7 @@ void OSD::_collect_metadata(map *pm) } // distro info - f = fopen("/etc/lsb-release", "r"); - if (f) { - char buf[100]; - while (!feof(f)) { - char *line = fgets(buf, sizeof(buf), f); - if (!line) - break; - char *eq = strchr(buf, '='); - if (!eq) - break; - *eq = '\0'; - ++eq; - while (*eq == '\"') - ++eq; - while (*eq && (eq[strlen(eq)-1] == '\n' || - eq[strlen(eq)-1] == '\"')) - eq[strlen(eq)-1] = '\0'; - if (strcmp(buf, "DISTRIB_ID") == 0) - (*pm)["distro"] = eq; - else if (strcmp(buf, "DISTRIB_RELEASE") == 0) - (*pm)["distro_version"] = eq; - else if (strcmp(buf, "DISTRIB_CODENAME") == 0) - (*pm)["distro_codename"] = eq; - else if (strcmp(buf, "DISTRIB_DESCRIPTION") == 0) - (*pm)["distro_description"] = eq; - } - fclose(f); - } + _lsb_release_parse(pm); dout(10) << __func__ << " " << *pm << dendl; } diff --git a/src/osd/OSD.h b/src/osd/OSD.h index e2a3c8e33f2ea..9d03e4dc06fc7 100644 --- a/src/osd/OSD.h +++ b/src/osd/OSD.h @@ -1472,6 +1472,8 @@ protected: void _maybe_boot(epoch_t oldest, epoch_t newest); void _send_boot(); void _collect_metadata(map *pmeta); + bool _lsb_release_set(char *buf, const char *str, map *pm, const char *key); + void _lsb_release_parse (map *pm); void start_waiting_for_healthy(); bool _is_healthy(); -- 2.39.5