Skip to content

Commit

Permalink
deduplicate kernel_version open-coded parser
Browse files Browse the repository at this point in the history
The code that parses kernel version from OSRELEASE/UTSRELEASE strings
and populates the global kernel table is duplicated across the codebase
for no good reason. This commit consolidates all the duplicated parsing
code into a single method to remove the unnecessary duplicated code.

Signed-off-by: Rafael Aquini <aquini@redhat.com>
  • Loading branch information
aquini committed Aug 13, 2023
1 parent 558aecc commit 55a9718
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 88 deletions.
27 changes: 3 additions & 24 deletions arm64.c
Original file line number Diff line number Diff line change
Expand Up @@ -834,35 +834,14 @@ static struct kernel_va_range_handler kernel_va_range_handlers[] = {
static unsigned long arm64_get_kernel_version(void)
{
char *string;
char buf[BUFSIZE];
char *p1, *p2;

if (THIS_KERNEL_VERSION)
return THIS_KERNEL_VERSION;

string = pc->read_vmcoreinfo("OSRELEASE");
if (string) {
strcpy(buf, string);

p1 = p2 = buf;
while (*p2 != '.')
p2++;
*p2 = NULLCHAR;
kt->kernel_version[0] = atoi(p1);

p1 = ++p2;
while (*p2 != '.')
p2++;
*p2 = NULLCHAR;
kt->kernel_version[1] = atoi(p1);

p1 = ++p2;
while ((*p2 >= '0') && (*p2 <= '9'))
p2++;
*p2 = NULLCHAR;
kt->kernel_version[2] = atoi(p1);
if ((string = pc->read_vmcoreinfo("OSRELEASE"))) {
parse_kernel_version(string);
free(string);
}
free(string);
return THIS_KERNEL_VERSION;
}

Expand Down
2 changes: 2 additions & 0 deletions defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -6032,6 +6032,8 @@ void clone_bt_info(struct bt_info *, struct bt_info *, struct task_context *);
void dump_kernel_table(int);
void dump_bt_info(struct bt_info *, char *where);
void dump_log(int);
void parse_kernel_version(char *);

#define LOG_LEVEL(v) ((v) & 0x07)
#define SHOW_LOG_LEVEL (0x1)
#define SHOW_LOG_DICT (0x2)
Expand Down
77 changes: 36 additions & 41 deletions kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,38 @@ static void check_vmcoreinfo(void);
static int is_pvops_xen(void);
static int get_linux_banner_from_vmlinux(char *, size_t);

/*
* popuplate the global kernel table (kt) with kernel version
* information parsed from UTSNAME/OSRELEASE string
*/
void
parse_kernel_version(char *str)
{
char *p1, *p2, separator;

p1 = p2 = str;
while (*p2 != '.' && *p2 != '\0')
p2++;

*p2 = '\0';
kt->kernel_version[0] = atoi(p1);
p1 = ++p2;
while (*p2 != '.' && *p2 != '-' && *p2 != '\0')
p2++;

separator = *p2;
*p2 = '\0';
kt->kernel_version[1] = atoi(p1);

if (separator == '.') {
p1 = ++p2;
while ((*p2 >= '0') && (*p2 <= '9'))
p2++;

*p2 = '\0';
kt->kernel_version[2] = atoi(p1);
}
}

/*
* Gather a few kernel basics.
Expand All @@ -112,7 +144,7 @@ void
kernel_init()
{
int i, c;
char *p1, *p2, buf[BUFSIZE];
char buf[BUFSIZE];
struct syment *sp1, *sp2;
char *rqstruct;
char *rq_timestamp_name = NULL;
Expand Down Expand Up @@ -270,28 +302,7 @@ kernel_init()
if (buf[64])
buf[64] = NULLCHAR;
if (ascii_string(kt->utsname.release)) {
char separator;

p1 = p2 = buf;
while (*p2 != '.')
p2++;
*p2 = NULLCHAR;
kt->kernel_version[0] = atoi(p1);
p1 = ++p2;
while (*p2 != '.' && *p2 != '-' && *p2 != '\0')
p2++;
separator = *p2;
*p2 = NULLCHAR;
kt->kernel_version[1] = atoi(p1);
*p2 = separator;
if (*p2 == '.') {
p1 = ++p2;
while ((*p2 >= '0') && (*p2 <= '9'))
p2++;
*p2 = NULLCHAR;
kt->kernel_version[2] = atoi(p1);
} else
kt->kernel_version[2] = 0;
parse_kernel_version(buf);

if (CRASHDEBUG(1))
fprintf(fp, "base kernel version: %d.%d.%d\n",
Expand Down Expand Up @@ -10973,8 +10984,6 @@ void
get_log_from_vmcoreinfo(char *file)
{
char *string;
char buf[BUFSIZE];
char *p1, *p2;
struct vmcoreinfo_data *vmc = &kt->vmcoreinfo;

if (!(pc->flags2 & VMCOREINFO))
Expand All @@ -10986,22 +10995,8 @@ get_log_from_vmcoreinfo(char *file)
if ((string = pc->read_vmcoreinfo("OSRELEASE"))) {
if (CRASHDEBUG(1))
fprintf(fp, "OSRELEASE: %s\n", string);
strcpy(buf, string);
p1 = p2 = buf;
while (*p2 != '.')
p2++;
*p2 = NULLCHAR;
kt->kernel_version[0] = atoi(p1);
p1 = ++p2;
while (*p2 != '.')
p2++;
*p2 = NULLCHAR;
kt->kernel_version[1] = atoi(p1);
p1 = ++p2;
while ((*p2 >= '0') && (*p2 <= '9'))
p2++;
*p2 = NULLCHAR;
kt->kernel_version[2] = atoi(p1);

parse_kernel_version(string);

if (CRASHDEBUG(1))
fprintf(fp, "base kernel version: %d.%d.%d\n",
Expand Down
25 changes: 2 additions & 23 deletions riscv64.c
Original file line number Diff line number Diff line change
Expand Up @@ -259,33 +259,12 @@ riscv64_processor_speed(void)
static unsigned long riscv64_get_kernel_version(void)
{
char *string;
char buf[BUFSIZE];
char *p1, *p2;

if (THIS_KERNEL_VERSION)
return THIS_KERNEL_VERSION;

string = pc->read_vmcoreinfo("OSRELEASE");
if (string) {
strcpy(buf, string);

p1 = p2 = buf;
while (*p2 != '.')
p2++;
*p2 = NULLCHAR;
kt->kernel_version[0] = atoi(p1);

p1 = ++p2;
while (*p2 != '.')
p2++;
*p2 = NULLCHAR;
kt->kernel_version[1] = atoi(p1);

p1 = ++p2;
while ((*p2 >= '0') && (*p2 <= '9'))
p2++;
*p2 = NULLCHAR;
kt->kernel_version[2] = atoi(p1);
if ((string = pc->read_vmcoreinfo("OSRELEASE"))) {
parse_kernel_version(string);
free(string);
}
return THIS_KERNEL_VERSION;
Expand Down

0 comments on commit 55a9718

Please sign in to comment.