app/trace: print error messages when input file is corrupted

Found while trying to process a file that wasn't completely
written yet - it would SIGBUS instead of printing an error
message.

Signed-off-by: Jim Harris <james.r.harris@intel.com>
Change-Id: I56d60519f0608900697102c06310ebb92ec4d39c

Reviewed-on: https://review.gerrithub.io/c/441988
Reviewed-by: Ben Walker <benjamin.walker@intel.com>
Reviewed-by: Shuhei Matsumoto <shuhei.matsumoto.xt@hitachi.com>
Reviewed-by: Changpeng Liu <changpeng.liu@intel.com>
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Chandler-Test-Pool: SPDK Automated Test System <sys_sgsw@intel.com>
This commit is contained in:
Jim Harris 2019-01-24 17:27:47 -07:00 committed by Changpeng Liu
parent 20476c6521
commit b4c8e45632

View File

@ -286,7 +286,7 @@ int main(int argc, char **argv)
void *history_ptr; void *history_ptr;
struct spdk_trace_history *history; struct spdk_trace_history *history;
struct spdk_trace_histories *histories; struct spdk_trace_histories *histories;
int fd, i; int fd, i, rc;
int lcore = SPDK_TRACE_MAX_LCORE; int lcore = SPDK_TRACE_MAX_LCORE;
uint64_t tsc_offset; uint64_t tsc_offset;
const char *app_name = NULL; const char *app_name = NULL;
@ -295,6 +295,7 @@ int main(int argc, char **argv)
char shm_name[64]; char shm_name[64];
int shm_id = -1, shm_pid = -1; int shm_id = -1, shm_pid = -1;
uint64_t trace_histories_size; uint64_t trace_histories_size;
struct stat _stat;
g_exe_name = argv[0]; g_exe_name = argv[0];
while ((op = getopt(argc, argv, "c:f:i:p:qs:")) != -1) { while ((op = getopt(argc, argv, "c:f:i:p:qs:")) != -1) {
@ -358,6 +359,18 @@ int main(int argc, char **argv)
exit(-1); exit(-1);
} }
rc = fstat(fd, &_stat);
if (rc < 0) {
fprintf(stderr, "Could not get size of %s.\n", file_name);
usage();
exit(-1);
}
if ((size_t)_stat.st_size < sizeof(*g_histories)) {
fprintf(stderr, "%s is not a valid trace file\n", file_name);
usage();
exit(-1);
}
/* Map the header of trace file */ /* Map the header of trace file */
history_ptr = mmap(NULL, sizeof(*g_histories), PROT_READ, MAP_SHARED, fd, 0); history_ptr = mmap(NULL, sizeof(*g_histories), PROT_READ, MAP_SHARED, fd, 0);
if (history_ptr == MAP_FAILED) { if (history_ptr == MAP_FAILED) {
@ -382,6 +395,11 @@ int main(int argc, char **argv)
/* Remap the entire trace file */ /* Remap the entire trace file */
trace_histories_size = spdk_get_trace_histories_size(g_histories); trace_histories_size = spdk_get_trace_histories_size(g_histories);
munmap(history_ptr, sizeof(*g_histories)); munmap(history_ptr, sizeof(*g_histories));
if ((size_t)_stat.st_size < trace_histories_size) {
fprintf(stderr, "%s is not a valid trace file\n", file_name);
usage();
exit(-1);
}
history_ptr = mmap(NULL, trace_histories_size, PROT_READ, MAP_SHARED, fd, 0); history_ptr = mmap(NULL, trace_histories_size, PROT_READ, MAP_SHARED, fd, 0);
if (history_ptr == MAP_FAILED) { if (history_ptr == MAP_FAILED) {
fprintf(stderr, "Could not mmap %s.\n", file_name); fprintf(stderr, "Could not mmap %s.\n", file_name);