Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from plundstr/master
New plugin bootreasonlogger
- Loading branch information
Showing
6 changed files
with
301 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,270 @@ | ||
/** | ||
@file bootreasonlogger.c | ||
Log the system shutdown and powerup reasons. | ||
<p> | ||
Copyright (C) 2013 Jolla Oy. | ||
@author Pekka Lundstrom <pekka.lundstrom@jolla.com> | ||
This file is part of Dsme. | ||
Dsme is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU Lesser General Public License | ||
version 2.1 as published by the Free Software Foundation. | ||
Dsme is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public | ||
License along with Dsme. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include "dbusproxy.h" | ||
#include "dsme_dbus.h" | ||
|
||
#include "dsme/modules.h" | ||
#include "dsme/logging.h" | ||
|
||
#include <dsme/state.h> | ||
#include <dsme/protocol.h> | ||
|
||
#include <stdio.h> | ||
#include <time.h> | ||
#include <string.h> | ||
#include <stdlib.h> | ||
|
||
#define BOOT_LOG_FILE "/var/log/systemboot.log" | ||
#define PFIX "bootlogger: " | ||
#define MAX_CMDLINE_LEN 1024 | ||
|
||
typedef enum { | ||
SD_REASON_UNKNOWN, | ||
SD_SW_REBOOT, | ||
SD_SW_SHUTDOWN, | ||
SD_DEVICE_OVERHEAT, | ||
SD_BATTERY_EMPTY, | ||
SD_USER_PWR_KEY, | ||
|
||
SD_REASON_COUNT | ||
} shutdown_reason_t; | ||
|
||
static const char* const shutdown_reason_string[SD_REASON_COUNT] = { | ||
"Reason Unknown", | ||
"SW reboot request", | ||
"SW shutdown request", | ||
"Device overheated", | ||
"Battery empty", | ||
"User Power Key" | ||
}; | ||
|
||
static shutdown_reason_t saved_shutdown_reason = SD_REASON_UNKNOWN; | ||
|
||
/* Powerup reason is either in enviroment or in kernel commandline | ||
* we will look for all these and in this order | ||
*/ | ||
static const char *possible_pwrup_strings[] = { | ||
"pwr_on_status", | ||
"bootreason", | ||
"bootup_reason", | ||
"androidboot.mode", | ||
0 | ||
}; | ||
|
||
static char pwrup_reason[80]; | ||
|
||
static const char * get_timestamp(void) | ||
{ | ||
static const char default_date[] = "00000000_000000"; | ||
static char date_time[80]; | ||
time_t raw_time; | ||
struct tm *timeinfo; | ||
const char *timestamp = default_date; | ||
|
||
if ((time(&raw_time) > 0) && | ||
((timeinfo = localtime(&raw_time)) != NULL) && | ||
(strftime(date_time, sizeof(date_time), "%Y%m%d_%H%M%S", timeinfo) > 0)) { | ||
timestamp = date_time; | ||
} else | ||
dsme_log(LOG_ERR, PFIX"failed to get timestamp"); | ||
|
||
return timestamp; | ||
} | ||
|
||
static void write_log(const char *state, const char *reason) | ||
{ | ||
FILE* f; | ||
bool success = false; | ||
|
||
f = fopen(BOOT_LOG_FILE, "a"); | ||
if (f) { | ||
if ( fprintf(f, "%s %s %s\n", get_timestamp(), state, reason) > 0) { | ||
success = true; | ||
sync(); | ||
} | ||
fclose(f); | ||
} | ||
|
||
if (! success) { | ||
dsme_log(LOG_ERR, PFIX"can't write into %s", BOOT_LOG_FILE); | ||
} | ||
} | ||
|
||
|
||
static int get_cmd_line_value(char* get_value, int max_len, const char* key) | ||
{ | ||
FILE* cmdline_file; | ||
char cmdline[MAX_CMDLINE_LEN]; | ||
int ret = -1; | ||
int keylen; | ||
char* key_and_value; | ||
char* value; | ||
|
||
cmdline_file = fopen("/proc/cmdline", "r"); | ||
if(!cmdline_file) { | ||
dsme_log(LOG_ERR, PFIX"Could not open /proc/cmdline\n"); | ||
return -1; | ||
} | ||
|
||
if (fgets(cmdline, MAX_CMDLINE_LEN, cmdline_file)) { | ||
key_and_value = strtok(cmdline, " "); | ||
keylen = strlen(key); | ||
while (key_and_value != NULL) { | ||
if(!strncmp(key_and_value, key, keylen)) { | ||
value = strtok(key_and_value, "="); | ||
value = strtok(NULL, "="); | ||
if (value) { | ||
snprintf(get_value, max_len, "%s", value); | ||
ret = strlen(get_value); | ||
} | ||
break; | ||
} | ||
key_and_value = strtok(NULL, " "); | ||
} | ||
} | ||
fclose(cmdline_file); | ||
return ret; | ||
} | ||
|
||
static char * get_powerup_reason_str(void) | ||
{ | ||
char *env; | ||
const char *search_key; | ||
int i = 0; | ||
char cmdvalue[80]; | ||
|
||
/* set default */ | ||
snprintf(pwrup_reason, sizeof(pwrup_reason), "Reason Unknown"); | ||
|
||
/* Powerup reason is either in enviroment or in kernel commandline | ||
* we will look both and use first match | ||
*/ | ||
|
||
while ((search_key = possible_pwrup_strings[i])) { | ||
if ((env = getenv(search_key))) { | ||
snprintf(pwrup_reason, sizeof(pwrup_reason),"%s=%s", search_key, env); | ||
break; | ||
} else if ((get_cmd_line_value(cmdvalue, sizeof(cmdvalue), search_key)) > 0) { | ||
snprintf(pwrup_reason, sizeof(pwrup_reason),"%s=%s", search_key, cmdvalue); | ||
break; | ||
} | ||
i++; | ||
} | ||
return pwrup_reason; | ||
} | ||
|
||
|
||
static void log_startup(void) | ||
{ | ||
write_log("Startup: ", get_powerup_reason_str()); | ||
} | ||
|
||
static void log_shutdown(void) | ||
{ | ||
write_log("Shutdown:", shutdown_reason_string[saved_shutdown_reason]); | ||
} | ||
|
||
DSME_HANDLER(DSM_MSGTYPE_SET_BATTERY_STATE, conn, battery) | ||
{ | ||
dsme_log(LOG_DEBUG, | ||
PFIX"battery %s state received", | ||
battery->empty ? "empty" : "not empty"); | ||
|
||
write_log("Received: battery ", battery->empty ? "empty" : "not empty"); | ||
if (battery->empty) | ||
saved_shutdown_reason = SD_BATTERY_EMPTY; | ||
else // Battery is no more empty. Shutdown won't happen | ||
saved_shutdown_reason = SD_REASON_UNKNOWN; | ||
} | ||
|
||
DSME_HANDLER(DSM_MSGTYPE_SET_THERMAL_STATE, conn, msg) | ||
{ | ||
dsme_log(LOG_DEBUG, | ||
PFIX"%s state received", | ||
msg->overheated ? "overheated" : "not overheated"); | ||
|
||
write_log("Received: device ", msg->overheated ? "overheated" : "not overheated"); | ||
if (msg->overheated) | ||
saved_shutdown_reason = SD_DEVICE_OVERHEAT; | ||
else // Device is no more overheated. Shutdown won't happen | ||
saved_shutdown_reason = SD_REASON_UNKNOWN; | ||
} | ||
|
||
DSME_HANDLER(DSM_MSGTYPE_REBOOT_REQ, conn, msg) | ||
{ | ||
dsme_log(LOG_DEBUG, PFIX"reboot request received"); | ||
if (saved_shutdown_reason == SD_REASON_UNKNOWN) | ||
saved_shutdown_reason = SD_SW_REBOOT; | ||
} | ||
|
||
DSME_HANDLER(DSM_MSGTYPE_SHUTDOWN_REQ, conn, msg) | ||
{ | ||
char* sender = endpoint_name(conn); | ||
|
||
dsme_log(LOG_DEBUG, PFIX"shutdown request received from %s", | ||
(sender ? sender : "(unknown)")); | ||
if (saved_shutdown_reason == SD_REASON_UNKNOWN) { | ||
if (sender && (strstr(sender, "/mce") != NULL)) | ||
saved_shutdown_reason = SD_USER_PWR_KEY; | ||
else | ||
saved_shutdown_reason = SD_SW_SHUTDOWN; | ||
} | ||
free(sender); | ||
} | ||
|
||
DSME_HANDLER(DSM_MSGTYPE_STATE_CHANGE_IND, conn, msg) | ||
{ | ||
dsme_log(LOG_DEBUG, PFIX"Received new state %d", msg->state); | ||
if (saved_shutdown_reason == SD_REASON_UNKNOWN) { | ||
if (msg->state == DSME_STATE_SHUTDOWN) | ||
saved_shutdown_reason = SD_SW_SHUTDOWN; | ||
else if (msg->state == DSME_STATE_REBOOT) | ||
saved_shutdown_reason = SD_SW_REBOOT; | ||
} | ||
} | ||
|
||
module_fn_info_t message_handlers[] = { | ||
DSME_HANDLER_BINDING(DSM_MSGTYPE_SHUTDOWN_REQ), | ||
DSME_HANDLER_BINDING(DSM_MSGTYPE_REBOOT_REQ), | ||
DSME_HANDLER_BINDING(DSM_MSGTYPE_SET_THERMAL_STATE), | ||
DSME_HANDLER_BINDING(DSM_MSGTYPE_SET_BATTERY_STATE), | ||
DSME_HANDLER_BINDING(DSM_MSGTYPE_STATE_CHANGE_IND), | ||
{0} | ||
}; | ||
|
||
|
||
void module_init(module_t* handle) | ||
{ | ||
dsme_log(LOG_DEBUG, "bootreasonlogger.so loaded"); | ||
log_startup(); | ||
saved_shutdown_reason = SD_REASON_UNKNOWN; | ||
} | ||
|
||
void module_fini(void) | ||
{ | ||
log_shutdown(); | ||
dsme_log(LOG_DEBUG, "bootreasonlogger.so unloaded"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters