Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Ben Hutchings and committed by
Greg Kroah-Hartman
44c8b25f 436473bc

+55 -4
+55 -4
tools/hv/hv_kvp_daemon.c
··· 454 454 455 455 uname(&uts_buf); 456 456 os_build = uts_buf.release; 457 + os_name = uts_buf.sysname; 457 458 processor_arch = uts_buf.machine; 458 459 459 460 /* ··· 466 465 if (p) 467 466 *p = '\0'; 468 467 468 + /* 469 + * Parse the /etc/os-release file if present: 470 + * http://www.freedesktop.org/software/systemd/man/os-release.html 471 + */ 472 + file = fopen("/etc/os-release", "r"); 473 + if (file != NULL) { 474 + while (fgets(buf, sizeof(buf), file)) { 475 + char *value, *q; 476 + 477 + /* Ignore comments */ 478 + if (buf[0] == '#') 479 + continue; 480 + 481 + /* Split into name=value */ 482 + p = strchr(buf, '='); 483 + if (!p) 484 + continue; 485 + *p++ = 0; 486 + 487 + /* Remove quotes and newline; un-escape */ 488 + value = p; 489 + q = p; 490 + while (*p) { 491 + if (*p == '\\') { 492 + ++p; 493 + if (!*p) 494 + break; 495 + *q++ = *p++; 496 + } else if (*p == '\'' || *p == '"' || 497 + *p == '\n') { 498 + ++p; 499 + } else { 500 + *q++ = *p++; 501 + } 502 + } 503 + *q = 0; 504 + 505 + if (!strcmp(buf, "NAME")) { 506 + p = strdup(value); 507 + if (!p) 508 + break; 509 + os_name = p; 510 + } else if (!strcmp(buf, "VERSION_ID")) { 511 + p = strdup(value); 512 + if (!p) 513 + break; 514 + os_major = p; 515 + } 516 + } 517 + fclose(file); 518 + return; 519 + } 520 + 521 + /* Fallback for older RH/SUSE releases */ 469 522 file = fopen("/etc/SuSE-release", "r"); 470 523 if (file != NULL) 471 524 goto kvp_osinfo_found; 472 525 file = fopen("/etc/redhat-release", "r"); 473 526 if (file != NULL) 474 527 goto kvp_osinfo_found; 475 - /* 476 - * Add code for other supported platforms. 477 - */ 478 528 479 529 /* 480 530 * We don't have information about the os. 481 531 */ 482 - os_name = uts_buf.sysname; 483 532 return; 484 533 485 534 kvp_osinfo_found: