From 313a64359d186ac3acc0216d9cf9fd22c7de9aa9 Mon Sep 17 00:00:00 2001 From: Navdeep Parhar Date: Sat, 7 Oct 2017 01:20:30 +0000 Subject: [PATCH] cxgbetool(8): Do not create a large file devoid of useful content when the dumpstate ioctl fails. Make the file world-readable while here. MFC after: 2 weeks Sponsored by: Chelsio Communications --- usr.sbin/cxgbetool/cxgbetool.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/usr.sbin/cxgbetool/cxgbetool.c b/usr.sbin/cxgbetool/cxgbetool.c index b11001f10c0c..f80e59fe6667 100644 --- a/usr.sbin/cxgbetool/cxgbetool.c +++ b/usr.sbin/cxgbetool/cxgbetool.c @@ -1896,13 +1896,6 @@ dumpstate(int argc, const char *argv[]) return (EINVAL); } - fd = open(fname, O_CREAT | O_TRUNC | O_EXCL | O_WRONLY, - S_IRUSR | S_IRGRP); - if (fd < 0) { - warn("open(%s)", fname); - return (errno); - } - dump.wr_flash = 0; memset(&dump.bitmap, 0xff, sizeof(dump.bitmap)); dump.len = 8 * 1024 * 1024; @@ -1913,9 +1906,20 @@ dumpstate(int argc, const char *argv[]) } rc = doit(CHELSIO_T4_CUDBG_DUMP, &dump); + if (rc != 0) + goto done; + + fd = open(fname, O_CREAT | O_TRUNC | O_EXCL | O_WRONLY, + S_IRUSR | S_IRGRP | S_IROTH); + if (fd < 0) { + warn("open(%s)", fname); + rc = errno; + goto done; + } write(fd, dump.data, dump.len); - free(dump.data); close(fd); +done: + free(dump.data); return (rc); }