Skip to content

Commit 0683b16

Browse files
lauxinwjren1
authored andcommitted
tools:acrn-crashlog: Get reboot reason in acrnprobe
Get system reboot reason from kernel commandline. Signed-off-by: Liu, Xinwu <xinwu.liu@intel.com> Acked-by: Chen Gang <gang.c.chen@intel.com>
1 parent 2d03706 commit 0683b16

File tree

3 files changed

+67
-48
lines changed

3 files changed

+67
-48
lines changed

tools/acrn-crashlog/acrnprobe/include/startupreason.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@
1919
* limitations under the License.
2020
*/
2121

22-
extern void read_startupreason(char *startupreason);
22+
#define REBOOT_REASON_SIZE 32
23+
24+
extern void read_startupreason(char *startupreason, const size_t limit);

tools/acrn-crashlog/acrnprobe/sender.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ static void telemd_send_reboot(void)
601601
{
602602
struct sender_t *telemd;
603603
char *class;
604-
char reason[MAXLINESIZE];
604+
char reason[REBOOT_REASON_SIZE];
605605
int ret;
606606

607607
telemd = get_sender_by_name("telemd");
@@ -627,7 +627,7 @@ static void telemd_send_reboot(void)
627627
free(content);
628628
}
629629

630-
read_startupreason(reason);
630+
read_startupreason(reason, sizeof(reason));
631631
ret = asprintf(&class, "clearlinux/reboot/%s", reason);
632632
if (ret < 0) {
633633
LOGE("compute string failed, out of memory\n");
@@ -952,7 +952,7 @@ static void crashlog_send_uptime(void)
952952

953953
static void crashlog_send_reboot(void)
954954
{
955-
char reason[MAXLINESIZE];
955+
char reason[REBOOT_REASON_SIZE];
956956
char *key;
957957
struct sender_t *crashlog;
958958

@@ -972,7 +972,7 @@ static void crashlog_send_reboot(void)
972972
free(key);
973973
}
974974

975-
read_startupreason(reason);
975+
read_startupreason(reason, sizeof(reason));
976976
key = generate_event_id("REBOOT", reason);
977977
if (key == NULL) {
978978
LOGE("generate event id failed, error (%s)\n",

tools/acrn-crashlog/acrnprobe/startupreason.c

Lines changed: 60 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -28,68 +28,85 @@
2828
#include "startupreason.h"
2929
#include "log_sys.h"
3030

31-
#define MAX_KERNEL_COMMAND_LINE_SIZE 4096
3231
#define CURRENT_KERNEL_CMDLINE "/proc/cmdline"
3332

34-
static int get_cmdline_bootreason(char *bootreason)
33+
static int get_cmdline_bootreason(char *bootreason, const size_t limit)
3534
{
36-
int res, len = MAX_KERNEL_COMMAND_LINE_SIZE;
37-
char *p, *p1, *p2;
38-
char *cmdline;
39-
const char key[] = "bootreason=";
40-
41-
cmdline = malloc(len);
42-
if (!cmdline) {
43-
LOGE("failed to allocate memory to read %s\n",
44-
CURRENT_KERNEL_CMDLINE);
45-
return -1;
46-
}
47-
res = file_read_string(CURRENT_KERNEL_CMDLINE, cmdline, len);
48-
if (res <= 0) {
35+
int res;
36+
unsigned long size;
37+
char *start, *p1, *p2, *end;
38+
void *cmdline;
39+
const char key[] = "ABL.reset=";
40+
41+
res = read_file(CURRENT_KERNEL_CMDLINE, &size, &cmdline);
42+
if (res < 0) {
4943
LOGE("failed to read file %s - %s\n",
5044
CURRENT_KERNEL_CMDLINE, strerror(errno));
51-
free(cmdline);
5245
return -1;
5346
}
5447

55-
p = strstr(cmdline, key);
56-
if (!p) {
48+
start = strstr(cmdline, key);
49+
if (!start) {
50+
LOGW("can't find reboot reason with key (%s) in cmdline\n",
51+
key);
5752
free(cmdline);
5853
return 0;
5954
}
60-
p += strlen(key);
61-
p1 = strstr(p, " ");
62-
p2 = strstr(p, "\n");
63-
if (p2 && !p1)
64-
*p2 = '\0';
65-
else if (p2 && p2 < p1)
66-
*p2 = '\0';
67-
else if (p1)
68-
*p1 = '\0';
69-
70-
strncpy(bootreason, p, strlen(p) + 1);
55+
56+
/* if the string contains ' ' or '\n', break it by '\0' */
57+
start += strlen(key);
58+
p1 = strchr(start, ' ');
59+
p2 = strchr(start, '\n');
60+
if (p2 && p1)
61+
end = MIN(p1, p2);
62+
else
63+
end = MAX(p1, p2);
64+
65+
if (end)
66+
*end = 0;
67+
68+
const size_t len = MIN(strlen(start), limit - 1);
69+
70+
if (len > 0)
71+
memcpy(bootreason, start, len + 1);
72+
7173
free(cmdline);
72-
return strlen(bootreason);
74+
return len;
7375
}
7476

75-
static void get_default_bootreason(char *bootreason)
77+
static int get_default_bootreason(char *bootreason, const size_t limit)
7678
{
77-
int ret;
78-
unsigned int i;
79-
char bootreason_prop[MAX_KERNEL_COMMAND_LINE_SIZE];
79+
int len;
80+
int i;
8081

81-
ret = get_cmdline_bootreason(bootreason_prop);
82-
if (ret <= 0)
83-
return;
82+
len = get_cmdline_bootreason(bootreason, limit);
83+
if (len <= 0)
84+
return len;
8485

85-
for (i = 0; i < strlen(bootreason_prop); i++)
86-
bootreason[i] = toupper(bootreason_prop[i]);
87-
bootreason[i] = '\0';
86+
for (i = 0; i < len; i++)
87+
bootreason[i] = toupper(bootreason[i]);
88+
89+
return len;
8890

8991
}
9092

91-
void read_startupreason(char *startupreason)
93+
void read_startupreason(char *startupreason, const size_t limit)
9294
{
93-
strcpy(startupreason, "UNKNOWN");
94-
get_default_bootreason(startupreason);
95+
int res;
96+
static char reboot_reason_cache[REBOOT_REASON_SIZE];
97+
98+
if (!reboot_reason_cache[0]) {
99+
/* fill cache */
100+
res = get_default_bootreason(reboot_reason_cache,
101+
sizeof(reboot_reason_cache));
102+
if (res <= 0)
103+
strncpy(reboot_reason_cache, "UNKNOWN",
104+
sizeof(reboot_reason_cache));
105+
}
106+
107+
const size_t len = MIN(strlen(reboot_reason_cache), limit - 1);
108+
109+
memcpy(startupreason, reboot_reason_cache, len);
110+
*(startupreason + len) = 0;
111+
return;
95112
}

0 commit comments

Comments
 (0)