diff options
author | Ben Hutchings <ben@decadent.org.uk> | 2012-09-06 16:32:14 -0400 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@linuxfoundation.org> | 2012-09-10 19:43:32 -0400 |
commit | 44c8b25fb3b4b67442426abdc2371e750f7a393e (patch) | |
tree | de374c871cabe327c92b4aa98f7a74e99302507a /tools/hv | |
parent | 436473bc2173499ae274d0f50111d1e355006caf (diff) |
tools/hv: Parse /etc/os-release
There is a new convention, used by systemd and supported by most
distributions, to put basic OS release information in /etc/os-release.
Added some additional error checking on strdup()
Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
Signed-off-by: K. Y. Srinivasan <kys@microsoft.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'tools/hv')
-rw-r--r-- | tools/hv/hv_kvp_daemon.c | 59 |
1 files changed, 55 insertions, 4 deletions
diff --git a/tools/hv/hv_kvp_daemon.c b/tools/hv/hv_kvp_daemon.c index 3922abc0daef..5959affd8820 100644 --- a/tools/hv/hv_kvp_daemon.c +++ b/tools/hv/hv_kvp_daemon.c | |||
@@ -454,6 +454,7 @@ void kvp_get_os_info(void) | |||
454 | 454 | ||
455 | uname(&uts_buf); | 455 | uname(&uts_buf); |
456 | os_build = uts_buf.release; | 456 | os_build = uts_buf.release; |
457 | os_name = uts_buf.sysname; | ||
457 | processor_arch = uts_buf.machine; | 458 | processor_arch = uts_buf.machine; |
458 | 459 | ||
459 | /* | 460 | /* |
@@ -465,20 +466,70 @@ void kvp_get_os_info(void) | |||
465 | if (p) | 466 | if (p) |
466 | *p = '\0'; | 467 | *p = '\0'; |
467 | 468 | ||
469 | /* | ||
470 | * Parse the /etc/os-release file if present: | ||
471 | * http://www.freedesktop.org/software/systemd/man/os-release.html | ||
472 | */ | ||
473 | file = fopen("/etc/os-release", "r"); | ||
474 | if (file != NULL) { | ||
475 | while (fgets(buf, sizeof(buf), file)) { | ||
476 | char *value, *q; | ||
477 | |||
478 | /* Ignore comments */ | ||
479 | if (buf[0] == '#') | ||
480 | continue; | ||
481 | |||
482 | /* Split into name=value */ | ||
483 | p = strchr(buf, '='); | ||
484 | if (!p) | ||
485 | continue; | ||
486 | *p++ = 0; | ||
487 | |||
488 | /* Remove quotes and newline; un-escape */ | ||
489 | value = p; | ||
490 | q = p; | ||
491 | while (*p) { | ||
492 | if (*p == '\\') { | ||
493 | ++p; | ||
494 | if (!*p) | ||
495 | break; | ||
496 | *q++ = *p++; | ||
497 | } else if (*p == '\'' || *p == '"' || | ||
498 | *p == '\n') { | ||
499 | ++p; | ||
500 | } else { | ||
501 | *q++ = *p++; | ||
502 | } | ||
503 | } | ||
504 | *q = 0; | ||
505 | |||
506 | if (!strcmp(buf, "NAME")) { | ||
507 | p = strdup(value); | ||
508 | if (!p) | ||
509 | break; | ||
510 | os_name = p; | ||
511 | } else if (!strcmp(buf, "VERSION_ID")) { | ||
512 | p = strdup(value); | ||
513 | if (!p) | ||
514 | break; | ||
515 | os_major = p; | ||
516 | } | ||
517 | } | ||
518 | fclose(file); | ||
519 | return; | ||
520 | } | ||
521 | |||
522 | /* Fallback for older RH/SUSE releases */ | ||
468 | file = fopen("/etc/SuSE-release", "r"); | 523 | file = fopen("/etc/SuSE-release", "r"); |
469 | if (file != NULL) | 524 | if (file != NULL) |
470 | goto kvp_osinfo_found; | 525 | goto kvp_osinfo_found; |
471 | file = fopen("/etc/redhat-release", "r"); | 526 | file = fopen("/etc/redhat-release", "r"); |
472 | if (file != NULL) | 527 | if (file != NULL) |
473 | goto kvp_osinfo_found; | 528 | goto kvp_osinfo_found; |
474 | /* | ||
475 | * Add code for other supported platforms. | ||
476 | */ | ||
477 | 529 | ||
478 | /* | 530 | /* |
479 | * We don't have information about the os. | 531 | * We don't have information about the os. |
480 | */ | 532 | */ |
481 | os_name = uts_buf.sysname; | ||
482 | return; | 533 | return; |
483 | 534 | ||
484 | kvp_osinfo_found: | 535 | kvp_osinfo_found: |