mirror of
https://github.com/projectacrn/acrn-hypervisor.git
synced 2025-06-24 14:33:38 +00:00
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>
This commit is contained in:
parent
2d03706dd5
commit
0683b16573
@ -19,4 +19,6 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
extern void read_startupreason(char *startupreason);
|
#define REBOOT_REASON_SIZE 32
|
||||||
|
|
||||||
|
extern void read_startupreason(char *startupreason, const size_t limit);
|
||||||
|
@ -601,7 +601,7 @@ static void telemd_send_reboot(void)
|
|||||||
{
|
{
|
||||||
struct sender_t *telemd;
|
struct sender_t *telemd;
|
||||||
char *class;
|
char *class;
|
||||||
char reason[MAXLINESIZE];
|
char reason[REBOOT_REASON_SIZE];
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
telemd = get_sender_by_name("telemd");
|
telemd = get_sender_by_name("telemd");
|
||||||
@ -627,7 +627,7 @@ static void telemd_send_reboot(void)
|
|||||||
free(content);
|
free(content);
|
||||||
}
|
}
|
||||||
|
|
||||||
read_startupreason(reason);
|
read_startupreason(reason, sizeof(reason));
|
||||||
ret = asprintf(&class, "clearlinux/reboot/%s", reason);
|
ret = asprintf(&class, "clearlinux/reboot/%s", reason);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LOGE("compute string failed, out of memory\n");
|
LOGE("compute string failed, out of memory\n");
|
||||||
@ -952,7 +952,7 @@ static void crashlog_send_uptime(void)
|
|||||||
|
|
||||||
static void crashlog_send_reboot(void)
|
static void crashlog_send_reboot(void)
|
||||||
{
|
{
|
||||||
char reason[MAXLINESIZE];
|
char reason[REBOOT_REASON_SIZE];
|
||||||
char *key;
|
char *key;
|
||||||
struct sender_t *crashlog;
|
struct sender_t *crashlog;
|
||||||
|
|
||||||
@ -972,7 +972,7 @@ static void crashlog_send_reboot(void)
|
|||||||
free(key);
|
free(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
read_startupreason(reason);
|
read_startupreason(reason, sizeof(reason));
|
||||||
key = generate_event_id("REBOOT", reason);
|
key = generate_event_id("REBOOT", reason);
|
||||||
if (key == NULL) {
|
if (key == NULL) {
|
||||||
LOGE("generate event id failed, error (%s)\n",
|
LOGE("generate event id failed, error (%s)\n",
|
||||||
|
@ -28,68 +28,85 @@
|
|||||||
#include "startupreason.h"
|
#include "startupreason.h"
|
||||||
#include "log_sys.h"
|
#include "log_sys.h"
|
||||||
|
|
||||||
#define MAX_KERNEL_COMMAND_LINE_SIZE 4096
|
|
||||||
#define CURRENT_KERNEL_CMDLINE "/proc/cmdline"
|
#define CURRENT_KERNEL_CMDLINE "/proc/cmdline"
|
||||||
|
|
||||||
static int get_cmdline_bootreason(char *bootreason)
|
static int get_cmdline_bootreason(char *bootreason, const size_t limit)
|
||||||
{
|
{
|
||||||
int res, len = MAX_KERNEL_COMMAND_LINE_SIZE;
|
int res;
|
||||||
char *p, *p1, *p2;
|
unsigned long size;
|
||||||
char *cmdline;
|
char *start, *p1, *p2, *end;
|
||||||
const char key[] = "bootreason=";
|
void *cmdline;
|
||||||
|
const char key[] = "ABL.reset=";
|
||||||
|
|
||||||
cmdline = malloc(len);
|
res = read_file(CURRENT_KERNEL_CMDLINE, &size, &cmdline);
|
||||||
if (!cmdline) {
|
if (res < 0) {
|
||||||
LOGE("failed to allocate memory to read %s\n",
|
|
||||||
CURRENT_KERNEL_CMDLINE);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
res = file_read_string(CURRENT_KERNEL_CMDLINE, cmdline, len);
|
|
||||||
if (res <= 0) {
|
|
||||||
LOGE("failed to read file %s - %s\n",
|
LOGE("failed to read file %s - %s\n",
|
||||||
CURRENT_KERNEL_CMDLINE, strerror(errno));
|
CURRENT_KERNEL_CMDLINE, strerror(errno));
|
||||||
free(cmdline);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
p = strstr(cmdline, key);
|
start = strstr(cmdline, key);
|
||||||
if (!p) {
|
if (!start) {
|
||||||
|
LOGW("can't find reboot reason with key (%s) in cmdline\n",
|
||||||
|
key);
|
||||||
free(cmdline);
|
free(cmdline);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
p += strlen(key);
|
|
||||||
p1 = strstr(p, " ");
|
|
||||||
p2 = strstr(p, "\n");
|
|
||||||
if (p2 && !p1)
|
|
||||||
*p2 = '\0';
|
|
||||||
else if (p2 && p2 < p1)
|
|
||||||
*p2 = '\0';
|
|
||||||
else if (p1)
|
|
||||||
*p1 = '\0';
|
|
||||||
|
|
||||||
strncpy(bootreason, p, strlen(p) + 1);
|
/* if the string contains ' ' or '\n', break it by '\0' */
|
||||||
|
start += strlen(key);
|
||||||
|
p1 = strchr(start, ' ');
|
||||||
|
p2 = strchr(start, '\n');
|
||||||
|
if (p2 && p1)
|
||||||
|
end = MIN(p1, p2);
|
||||||
|
else
|
||||||
|
end = MAX(p1, p2);
|
||||||
|
|
||||||
|
if (end)
|
||||||
|
*end = 0;
|
||||||
|
|
||||||
|
const size_t len = MIN(strlen(start), limit - 1);
|
||||||
|
|
||||||
|
if (len > 0)
|
||||||
|
memcpy(bootreason, start, len + 1);
|
||||||
|
|
||||||
free(cmdline);
|
free(cmdline);
|
||||||
return strlen(bootreason);
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void get_default_bootreason(char *bootreason)
|
static int get_default_bootreason(char *bootreason, const size_t limit)
|
||||||
{
|
{
|
||||||
int ret;
|
int len;
|
||||||
unsigned int i;
|
int i;
|
||||||
char bootreason_prop[MAX_KERNEL_COMMAND_LINE_SIZE];
|
|
||||||
|
|
||||||
ret = get_cmdline_bootreason(bootreason_prop);
|
len = get_cmdline_bootreason(bootreason, limit);
|
||||||
if (ret <= 0)
|
if (len <= 0)
|
||||||
return;
|
return len;
|
||||||
|
|
||||||
for (i = 0; i < strlen(bootreason_prop); i++)
|
for (i = 0; i < len; i++)
|
||||||
bootreason[i] = toupper(bootreason_prop[i]);
|
bootreason[i] = toupper(bootreason[i]);
|
||||||
bootreason[i] = '\0';
|
|
||||||
|
return len;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void read_startupreason(char *startupreason)
|
void read_startupreason(char *startupreason, const size_t limit)
|
||||||
{
|
{
|
||||||
strcpy(startupreason, "UNKNOWN");
|
int res;
|
||||||
get_default_bootreason(startupreason);
|
static char reboot_reason_cache[REBOOT_REASON_SIZE];
|
||||||
|
|
||||||
|
if (!reboot_reason_cache[0]) {
|
||||||
|
/* fill cache */
|
||||||
|
res = get_default_bootreason(reboot_reason_cache,
|
||||||
|
sizeof(reboot_reason_cache));
|
||||||
|
if (res <= 0)
|
||||||
|
strncpy(reboot_reason_cache, "UNKNOWN",
|
||||||
|
sizeof(reboot_reason_cache));
|
||||||
|
}
|
||||||
|
|
||||||
|
const size_t len = MIN(strlen(reboot_reason_cache), limit - 1);
|
||||||
|
|
||||||
|
memcpy(startupreason, reboot_reason_cache, len);
|
||||||
|
*(startupreason + len) = 0;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user