diff --git a/tools/acrn-crashlog/acrnprobe/include/load_conf.h b/tools/acrn-crashlog/acrnprobe/include/load_conf.h index 881bba276..664c6ccd5 100644 --- a/tools/acrn-crashlog/acrnprobe/include/load_conf.h +++ b/tools/acrn-crashlog/acrnprobe/include/load_conf.h @@ -57,6 +57,8 @@ struct log_t { size_t path_len; const char *lines; size_t lines_len; + const char *sizelimit; + size_t sizelimit_len; void (*get)(struct log_t *, void *); }; diff --git a/tools/acrn-crashlog/acrnprobe/load_conf.c b/tools/acrn-crashlog/acrnprobe/load_conf.c index 4e8c6e003..3bfa9121c 100644 --- a/tools/acrn-crashlog/acrnprobe/load_conf.c +++ b/tools/acrn-crashlog/acrnprobe/load_conf.c @@ -73,6 +73,7 @@ static void print(void) print_id_item(type, log, id); print_id_item(lines, log, id); print_id_item(path, log, id); + print_id_item(sizelimit, log, id); } for_each_info(id, info, conf) { @@ -466,6 +467,8 @@ static int parse_log(xmlNodePtr cur, struct log_t *log) res = load_cur_content(cur, log, path); else if (name_is(cur, "lines")) res = load_cur_content(cur, log, lines); + else if (name_is(cur, "sizelimit")) + res = load_cur_content(cur, log, sizelimit); if (res) return -1; diff --git a/tools/acrn-crashlog/acrnprobe/sender.c b/tools/acrn-crashlog/acrnprobe/sender.c index 7f2a63f6a..b0dffe1a3 100644 --- a/tools/acrn-crashlog/acrnprobe/sender.c +++ b/tools/acrn-crashlog/acrnprobe/sender.c @@ -123,9 +123,10 @@ static void get_log_file(const char *despath, const char *srcpath, get_log_file_complete(despath, srcpath); } -static void get_log_node(const char *despath, const char *nodepath) +static void get_log_node(const char *despath, const char *nodepath, + size_t sizelimit) { - const int res = do_copy_eof(nodepath, despath); + const int res = do_copy_limit(nodepath, despath, sizelimit); if (res < 0) { LOGE("copy (%s) failed, error (%s)\n", nodepath, @@ -156,8 +157,17 @@ static void get_log_by_type(const char *despath, const struct log_t *log, if (cfg_atoi(log->lines, log->lines_len, &lines) == -1) return; get_log_file(despath, srcpath, lines); - } else if (!strcmp("node", log->type)) - get_log_node(despath, log->path); + } else if (!strcmp("node", log->type)) { + int size; + + if (!log->sizelimit) + size = 0; + else + if (cfg_atoi(log->sizelimit, log->sizelimit_len, + &size) == -1) + return; + get_log_node(despath, log->path, (size_t)(size * 1024 * 1024)); + } else if (!strcmp("cmd", log->type)) get_log_cmd(despath, log->path); } diff --git a/tools/acrn-crashlog/common/fsutils.c b/tools/acrn-crashlog/common/fsutils.c index 397d1150e..99833259e 100644 --- a/tools/acrn-crashlog/common/fsutils.c +++ b/tools/acrn-crashlog/common/fsutils.c @@ -528,44 +528,57 @@ int file_update_int(const char *filename, unsigned int current, * * @param src Path of source file. * @param dest Path of destin file. + * @param limitsize Bytes will copy from src at most. * - * @return 0 if successful, or a negative value if not. + * @return 0 if successful, or -1 if not. */ -int do_copy_eof(const char *src, const char *des) +int do_copy_limit(const char *src, const char *des, size_t limitsize) { char buffer[CPBUFFERSIZE]; int rc = 0; - int fd1 = -1, fd2 = -1; - struct stat info; - int r_count, w_count = 0; + int fd1; + int fd2; + size_t dsize = 0; + size_t rbsize = CPBUFFERSIZE; + ssize_t r_count; + ssize_t w_count; if (src == NULL || des == NULL) - return -EINVAL; + return -1; - if (stat(src, &info) < 0) { - LOGE("can not open file: %s\n", src); - return -errno; + fd1 = open(src, O_RDONLY | O_NONBLOCK); + if (fd1 < 0) { + LOGE("failed to open file: %s, err: %s\n", src, + strerror(errno)); + return -1; } - fd1 = open(src, O_RDONLY); - if (fd1 < 0) - return -errno; - fd2 = open(des, O_WRONLY | O_CREAT | O_TRUNC, 0660); if (fd2 < 0) { - LOGE("can not open file: %s\n", des); + LOGE("failed to open file: %s, err: %s\n", des, + strerror(errno)); close(fd1); - return -errno; + return -1; } /* Start copy loop */ while (1) { + if (limitsize > 0) { + if (dsize >= limitsize) + break; + rbsize = MIN(limitsize - dsize, CPBUFFERSIZE); + } + /* Read data from src */ - r_count = read(fd1, buffer, CPBUFFERSIZE); + r_count = read(fd1, buffer, rbsize); if (r_count < 0) { - LOGE("read failed, err:%s\n", strerror(errno)); - rc = -1; - break; + if (errno == EAGAIN) { + break; + } else { + LOGE("read failed, err:%s\n", strerror(errno)); + rc = -1; + break; + } } if (r_count == 0) @@ -579,11 +592,13 @@ int do_copy_eof(const char *src, const char *des) break; } if (r_count != w_count) { - LOGE("write failed, r_count:%d w_count:%d\n", + LOGE("write failed, r_count:%zd w_count:%zd\n", r_count, w_count); rc = -1; break; } + + dsize += w_count; } if (fd1 >= 0) diff --git a/tools/acrn-crashlog/common/include/fsutils.h b/tools/acrn-crashlog/common/include/fsutils.h index 8fde72058..b5ae6a387 100644 --- a/tools/acrn-crashlog/common/include/fsutils.h +++ b/tools/acrn-crashlog/common/include/fsutils.h @@ -92,7 +92,7 @@ void file_reset_init(const char *filename); int file_read_int(const char *filename, unsigned int *pcurrent); int file_update_int(const char *filename, unsigned int current, unsigned int max); -int do_copy_eof(const char *src, const char *des); +int do_copy_limit(const char *src, const char *des, size_t limitsize); int space_available(const char *path, int quota); int count_lines_in_file(const char *filename); int read_full_binary_file(const char *path, unsigned long *size, diff --git a/tools/acrn-crashlog/data/acrnprobe.xml b/tools/acrn-crashlog/data/acrnprobe.xml index 9e02bef58..0939cf94b 100644 --- a/tools/acrn-crashlog/data/acrnprobe.xml +++ b/tools/acrn-crashlog/data/acrnprobe.xml @@ -72,8 +72,9 @@ kmsg - cmd - dmesg + node + /dev/kmsg + 2 cmdline