tools: acrn-crashlog: remove unsafe strlen in common

Remove strlen in common apis, and change their caller if necessary.

Tracked-On: #1254
Signed-off-by: Liu, Xinwu <xinwu.liu@intel.com>
Reviewed-by: Yonghua Huang <yonghua.huang@intel.com>
Acked-by: Chen Gang <gang.c.chen@intel.com>
This commit is contained in:
Liu, Xinwu
2018-09-27 14:00:34 +08:00
committed by Xie, Nanlin
parent f25bc50e68
commit 40dbdcde4f
14 changed files with 378 additions and 461 deletions

View File

@@ -32,38 +32,6 @@ static const char *android_img = "/data/android/android.img";
static const char *android_histpath = "logs/history_event";
char *loop_dev;
static char *get_line(const char *str, size_t str_size,
const char *area, size_t area_size,
const char *search_from, size_t *len)
{
char *p;
char *match;
char *tail;
ssize_t search_size = area + area_size - search_from;
if (search_size < 0 || (size_t)search_size < str_size)
return NULL;
match = memmem(search_from, search_size, str, str_size);
if (!match)
return NULL;
tail = memchr(match + str_size, '\n',
area + area_size - match - str_size);
if (!tail)
return NULL;
for (p = match; p >= area; p--) {
if (*p == '\n') {
*len = tail - p - 1;
return (char *)(p + 1);
}
}
*len = tail - area;
return (char *)area;
}
/* Find the next event that needs to be synced.
* There is a history_event file in UOS side, it records UOS's events in
* real-time. Generally, the cursor point to the first unsynchronized line.
@@ -117,8 +85,10 @@ static char *next_vm_event(const char *cursor, const char *data,
* 'type'.
*/
if (!new || memcmp(new, type + 1, tlen - 1) ||
*(new + tlen - 1) != ' ')
*(new + tlen - 1) != ' ') {
free(type);
continue;
}
} else {
new = get_line(type, (size_t)tlen, data, dlen,
cursor, &len);
@@ -136,10 +106,11 @@ static char *next_vm_event(const char *cursor, const char *data,
return line_to_sync;
}
#define VMRECORD_HEAD_LINES 6
#define VMRECORD_HEAD_LINES 7
#define VMRECORD_TAG_LEN 9
#define VMRECORD_TAG_WAITING_SYNC " <=="
#define VMRECORD_TAG_NOT_FOUND "NOT_FOUND"
#define VMRECORD_TAG_MISS_LOG "MISS_LOGS"
#define VMRECORD_TAG_SUCCESS " "
static int generate_log_vmrecord(const char *path)
{
@@ -148,6 +119,7 @@ static int generate_log_vmrecord(const char *path)
" * This file records VM id synced or about to be synched,\n"
" * the tag \"<==\" indicates event waiting to sync.\n"
" * the tag \"NOT_FOUND\" indicates event not found in UOS.\n"
" * the tag \"MISS_LOGS\" indicates event miss logs in UOS.\n"
" */\n\n";
LOGD("Generate (%s)\n", path);
@@ -204,7 +176,12 @@ static int refresh_key_synced_stage1(const struct sender_t *sender,
return -1;
}
append_file(log_vmrecordid, log_new);
if (append_file(log_vmrecordid, log_new,
strnlen(log_new, 64)) < 0) {
LOGE("failed to add new record (%s) to (%s)\n",
log_new, log_vmrecordid);
return -1;
}
return 0;
}
@@ -215,7 +192,8 @@ static int refresh_key_synced_stage1(const struct sender_t *sender,
enum stage2_refresh_type_t {
SUCCESS,
NOT_FOUND
NOT_FOUND,
MISS_LOG
};
static int refresh_key_synced_stage2(char *line, size_t len,
@@ -228,6 +206,8 @@ static int refresh_key_synced_stage2(char *line, size_t len,
memcpy(tag, VMRECORD_TAG_SUCCESS, VMRECORD_TAG_LEN);
else if (type == NOT_FOUND)
memcpy(tag, VMRECORD_TAG_NOT_FOUND, VMRECORD_TAG_LEN);
else if (type == MISS_LOG)
memcpy(tag, VMRECORD_TAG_MISS_LOG, VMRECORD_TAG_LEN);
else
return -1;
@@ -390,7 +370,7 @@ static void sync_lines_stage2(const struct sender_t *sender,
}
for (record = next_record(recos, recos->begin, &recolen); record;
record = next_record(recos, record, &recolen)) {
record = next_record(recos, record + recolen, &recolen)) {
const char * const record_fmt =
VM_NAME_FMT ANDROID_KEY_FMT IGN_RESTS;
char *hist_line;
@@ -398,6 +378,7 @@ static void sync_lines_stage2(const struct sender_t *sender,
char vm_name[32];
char vmkey[ANDROID_WORD_LEN];
struct vm_t *vm;
int res;
/* VMNAME xxxxxxxxxxxxxxxxxxxx <== */
if (str_split_ere(record, recolen,
@@ -421,8 +402,11 @@ static void sync_lines_stage2(const struct sender_t *sender,
continue;
}
if (fn(hist_line, len + 1, vm) == VMEVT_HANDLED)
res = fn(hist_line, len + 1, vm);
if (res == VMEVT_HANDLED)
refresh_key_synced_stage2(record, recolen, SUCCESS);
else if (res == VMEVT_MISSLOG)
refresh_key_synced_stage2(record, recolen, MISS_LOG);
}
out:
@@ -454,8 +438,9 @@ static void get_last_line_synced(const struct sender_t *sender)
continue;
snprintf(vm_name, sizeof(vm_name), "%s ", vm->name);
ret = file_read_key_value_r(sender->log_vmrecordid, vm_name,
sizeof(vmkey), vmkey);
ret = file_read_key_value_r(vmkey, sizeof(vmkey),
sender->log_vmrecordid,
vm_name, strnlen(vm_name, 32));
if (ret == -ENOENT) {
LOGD("no (%s), will generate\n",
sender->log_vmrecordid);
@@ -536,8 +521,8 @@ static char *setup_loop_dev(void)
/* This function searches all android vms' new events and call the fn for
* each event.
*
* Note that: fn should return 0 to indicate event has been handled,
* or fn will be called in a time loop until it returns 0.
* Note that: fn should return VMEVT_HANDLED to indicate event has been handled.
* fn will be called in a time loop if it returns VMEVT_DEFER.
*/
void refresh_vm_history(const struct sender_t *sender,
int (*fn)(const char*, size_t, const struct vm_t *))

View File

@@ -122,7 +122,6 @@ void hist_raise_event(const char *event, const char *type, const char *log,
char eventtime[LONG_TIME_SIZE];
struct sender_t *crashlog;
int maxlines;
int ret;
struct history_entry entry = {
.event = event,
.type = type,
@@ -143,16 +142,13 @@ void hist_raise_event(const char *event, const char *type, const char *log,
backup_history();
}
ret = get_current_time_long(eventtime);
if (ret <= 0)
if (get_current_time_long(eventtime) <= 0)
return;
entry.eventtime = eventtime;
entry_to_history_line(&entry, line);
ret = append_file(history_file, line);
if (ret < 0) {
LOGE("append (%s) failed, error (%s)\n", history_file,
strerror(errno));
if (append_file(history_file, line, strnlen(line, MAXLINESIZE)) <= 0) {
LOGE("failed to append (%s) to (%s)\n", line, history_file);
return;
}
}
@@ -235,7 +231,8 @@ static int get_time_from_firstline(char *buffer, size_t size)
const char *prefix = "#V1.0 CURRENTUPTIME ";
int len;
len = file_read_key_value(history_file, prefix, MAXLINESIZE, lasttime);
len = file_read_key_value(lasttime, MAXLINESIZE, history_file, prefix,
strlen(prefix));
if (len <= 0) {
LOGW("failed to read value from %s, error %s\n",
history_file, strerror(-len));
@@ -289,11 +286,12 @@ int prepare_history(void)
strerror(errno));
return ret;
}
ret = append_file(history_file, HISTORY_BLANK_LINE2);
ret = append_file(history_file, HISTORY_BLANK_LINE2,
sizeof(HISTORY_BLANK_LINE2) - 1);
if (ret < 0) {
LOGE("Write (%s, %s) failed, error (%s)\n",
history_file, HISTORY_BLANK_LINE2,
strerror(errno));
strerror(-ret));
return ret;
}
current_lines = count_lines_in_file(history_file);

View File

@@ -11,7 +11,9 @@ extern char *loop_dev;
#define VMEVT_HANDLED 0
#define VMEVT_DEFER -1
#define VMEVT_MISSLOG -2
#define ANDROID_LOGS_DIR "/logs/"
#define IGN_SPACES "%*[[[:space:]]*]"
#define IGN_RESTS "%*[[.]*]"
#define IGN_ONEWORD "%*[[^[:space:]]*]" IGN_SPACES

View File

@@ -71,8 +71,9 @@ static int get_buildversion(struct sender_t *sender)
char *logbuildid;
char *currentbuild = gbuildversion;
ret = file_read_key_value(OS_VERSION, OS_VERSION_KEY,
sizeof(gbuildversion), gbuildversion);
ret = file_read_key_value(gbuildversion, sizeof(gbuildversion),
OS_VERSION, OS_VERSION_KEY,
strlen(OS_VERSION_KEY));
if (ret <= 0) {
LOGE("failed to get version from %s, error (%s)\n",
OS_VERSION, strerror(-ret));

View File

@@ -216,13 +216,14 @@ static void telemd_get_log(struct log_t *log, void *data)
int res;
int i;
struct dirent **filelist;
struct ac_filter_data acfd = {log->name, log->name_len};
if (d->srcdir == NULL)
goto send_nologs;
/* search file which use log->name as substring */
count = ac_scandir(d->srcdir, &filelist, filter_filename_substr,
log->name, NULL);
&acfd, NULL);
if (count < 0) {
LOGE("search (%s) in dir (%s) failed\n", log->name, d->srcdir);
return;
@@ -544,8 +545,7 @@ static void telemd_send_reboot(void)
}
static int telemd_new_vmevent(const char *line_to_sync,
size_t len __attribute__((unused)),
const struct vm_t *vm)
size_t len, const struct vm_t *vm)
{
char event[ANDROID_WORD_LEN];
char longtime[ANDROID_WORD_LEN];
@@ -589,21 +589,26 @@ static int telemd_new_vmevent(const char *line_to_sync,
severity = INFO_SEVERITY;
/* if line contains log, fill vmlogpath */
log = strstr(rest, "/logs/");
log = strstr(rest, ANDROID_LOGS_DIR);
if (log) {
struct sender_t *crashlog;
struct sender_t *crashlog = get_sender_by_name("crashlog");
const char *logf;
size_t logflen;
int res;
crashlog = get_sender_by_name("crashlog");
if (!crashlog)
return VMEVT_HANDLED;
res = find_file(crashlog->outdir, log + strlen("/logs/"),
2, &vmlogpath, 1);
if (res < 0) {
LOGE("find (%s) in (%s) failed\n",
log + strlen("/logs/"), crashlog->outdir);
logf = log + sizeof(ANDROID_LOGS_DIR) - 1;
logflen = &rest[0] + strnlen(rest, PATH_MAX) - logf;
res = find_file(crashlog->outdir, logf, logflen, 1,
&vmlogpath, 1);
if (res == -1) {
LOGE("failed to find (%s) in (%s)\n",
logf, crashlog->outdir);
return VMEVT_DEFER;
} else if (res == 0)
return VMEVT_DEFER;
}
}
res = asprintf(&class, "%s/%s/%s", vm->name, event, type);
@@ -898,8 +903,7 @@ static void crashlog_send_reboot(void)
}
static int crashlog_new_vmevent(const char *line_to_sync,
size_t len __attribute__((unused)),
const struct vm_t *vm)
size_t len, const struct vm_t *vm)
{
struct sender_t *crashlog;
char event[ANDROID_WORD_LEN];
@@ -980,12 +984,18 @@ static int crashlog_new_vmevent(const char *line_to_sync,
ret = VMEVT_DEFER;
} else {
LOGW("(%s) is missing\n", vmlogpath);
ret = VMEVT_HANDLED; /* missing logdir */
ret = VMEVT_MISSLOG; /* missing logdir */
}
res = remove(dir);
if (res == -1 && errno != ENOENT)
if (remove_r(dir) == -1)
LOGE("failed to remove %s (%d)\n", dir, -errno);
goto free_dir;
}
if (cnt == 1) {
LOGW("(%s) is empty, will sync it in the next loop\n",
vmlogpath);
ret = VMEVT_DEFER;
if (remove_r(dir) == -1)
LOGE("failed to remove %s (%d)\n", dir, -errno);
goto free_dir;
}
}