dump_systemstate: Continue dumping after failure of a subcommand 99/191499/5
authorMateusz Moscicki <m.moscicki2@partner.samsung.com>
Wed, 17 Oct 2018 11:23:29 +0000 (13:23 +0200)
committerMateusz Moscicki <m.moscicki2@partner.samsung.com>
Wed, 24 Oct 2018 13:32:24 +0000 (15:32 +0200)
Change-Id: I7c0481984fbb9c04b26f913fb25208b11f9d6869

src/dump_systemstate/dump_systemstate.c

index 776ec2b..fb2ee75 100644 (file)
@@ -84,7 +84,13 @@ static int get_disk_used_percent(const char *path)
 
 int main(int argc, char *argv[])
 {
-       int c, ret, i, is_root, dpercent;
+#define INFORM_IF_ERROR(ret) \
+       if (ret < 0) { \
+               exit_code = ret; \
+               fprintf_fd(out_fd, "\nCommand failed with error code: %d", ret); \
+       }
+
+       int c, ret, i, is_root, dpercent, exit_code = 0;
        const char *arg_file = NULL;
        int out_fd = -1;
        bool arg_dlog = false;
@@ -130,7 +136,7 @@ int main(int argc, char *argv[])
                out_fd = open(arg_file, O_WRONLY | O_TRUNC | O_CREAT, FILE_PERM);
                if (out_fd < 0) {
                        perror("couldn't open output file");
-                       ret = out_fd;
+                       exit_code = out_fd;
                        goto exit;
                }
        }
@@ -144,66 +150,56 @@ int main(int argc, char *argv[])
                fprintf_fd(out_fd, "\n%s(%s)\n",
                                                dump_item[i].title, dump_item[i].path);
                ret = dump_file_write_fd((char *)dump_item[i].path, out_fd);
-               if (ret < 0)
-                       goto exit_close;
+               INFORM_IF_ERROR(ret)
        }
        fprintf_fd(out_fd, "\n");
 
        fprintf_fd(out_fd, "\n==== System disk space usage (/bin/df -h)\n");
        char *df_args[] = {"/bin/df", "-h", NULL};
        ret = run_command_write_fd_timeout(df_args[0], df_args, NULL, out_fd, NULL, 0, TIMEOUT_DEFAULT);
-       if (ret < 0)
-               goto exit_close;
+       INFORM_IF_ERROR(ret)
 
        dpercent = get_disk_used_percent("/opt");
        if (90 < dpercent) {
                fprintf_fd(out_fd, "\n==== System disk space usage detail - %d%% (/bin/du -ah /opt)\n", dpercent);
                char *du_args[] = {"/bin/du", "-ah", "/opt", "--exclude=/opt/usr", NULL};
                ret = run_command_write_fd_timeout(du_args[0], du_args, NULL, out_fd, NULL, 0, TIMEOUT_DEFAULT);
-               if (ret < 0)
-                       goto exit_close;
+               INFORM_IF_ERROR(ret)
        }
        fprintf_fd(out_fd, "\n==== System timezone (ls -al /opt/etc/localtime)\n");
        char *ls_args[] = {"/bin/ls", "-al", "/opt/etc/localtime", NULL};
        ret = run_command_write_fd_timeout(ls_args[0], ls_args, NULL, out_fd, NULL, 0, TIMEOUT_DEFAULT);
-       if (ret < 0)
-               goto exit_close;
+       INFORM_IF_ERROR(ret)
 
        fprintf_fd(out_fd, "\n==== System summary (/usr/bin/top -bcH -n 1)\n");
        char *top_args[] = {"/bin/top", "-bcH", "-n", "1", NULL};
        char *top_env[] = {"COLUMNS=200", NULL};
        ret = run_command_write_fd_timeout(top_args[0], top_args, top_env, out_fd, NULL, 0, TIMEOUT_DEFAULT);
-       if (ret < 0)
-               goto exit_close;
+       INFORM_IF_ERROR(ret)
 
        fprintf_fd(out_fd, "\n==== Current processes (/bin/ps auxfw)\n");
        char *ps_args[] = {"/bin/ps", "auxfw", NULL};
        ret = run_command_write_fd_timeout(ps_args[0], ps_args, NULL, out_fd, NULL, 0, TIMEOUT_DEFAULT);
-       if (ret < 0)
-               goto exit_close;
+       INFORM_IF_ERROR(ret)
 
        fprintf_fd(out_fd, "\n==== System memory statistics (/usr/bin/memps -v)\n");
        char *memps_args[] = {"/bin/memps", "-v", NULL};
        ret = run_command_write_fd_timeout(memps_args[0], memps_args, NULL, out_fd, NULL, 0, TIMEOUT_DEFAULT);
-       if (ret < 0)
-               goto exit_close;
+       INFORM_IF_ERROR(ret)
 
        if (is_root) {
                fprintf_fd(out_fd, "\n==== System configuration (/usr/bin/vconftool get memory, db, file)\n");
                char *get_mem_args[] = {"/bin/vconftool", "get", "memory/", "-r", NULL};
                ret = run_command_write_fd_timeout(get_mem_args[0], get_mem_args, NULL, out_fd, NULL, 0, TIMEOUT_DEFAULT);
-               if (ret < 0)
-                       goto exit_close;
+               INFORM_IF_ERROR(ret)
 
                char *get_db_args[] = {"/bin/vconftool", "get", "db/", "-r", NULL};
                ret = run_command_write_fd_timeout(get_db_args[0], get_db_args, NULL, out_fd, NULL, 0, TIMEOUT_DEFAULT);
-               if (ret < 0)
-                       goto exit_close;
+               INFORM_IF_ERROR(ret)
 
                char *get_file_args[] = {"/bin/vconftool", "get", "file/", "-r", NULL};
                ret = run_command_write_fd_timeout(get_file_args[0], get_file_args, NULL, out_fd, NULL, 0, TIMEOUT_DEFAULT);
-               if (ret < 0)
-                       goto exit_close;
+               INFORM_IF_ERROR(ret)
        }
 
        if (arg_dmesg && is_root) {
@@ -211,29 +207,26 @@ int main(int argc, char *argv[])
                char *dmesg_args[] = {"/bin/dmesg", "-T", NULL};
                char *dmesg_env[] = {"TZ=UTC", NULL};
                ret = run_command_write_fd_timeout(dmesg_args[0], dmesg_args, dmesg_env, out_fd, NULL, 0, TIMEOUT_DEFAULT);
-               if (ret < 0)
-                       goto exit_close;
+               INFORM_IF_ERROR(ret)
        }
 
        if (arg_dlog) {
                fprintf_fd(out_fd, "\n==== Log messages\n");
                char *dlogutil_args[] = {"/bin/dlogutil", "-d", "-v", "threadtime", "-u", "16384", NULL};
                ret = run_command_write_fd_timeout(dlogutil_args[0], dlogutil_args, NULL, out_fd, NULL, 0, TIMEOUT_DEFAULT);
-               if (ret < 0)
-                       goto exit_close;
+               INFORM_IF_ERROR(ret)
        }
 
        if (arg_journal) {
                fprintf_fd(out_fd, "\n==== Journal messages\n");
                char *journalctl_args[] = {"/bin/journalctl", "-b", "-n", "1024", NULL};
                ret = run_command_write_fd_timeout(journalctl_args[0], journalctl_args, NULL, out_fd, NULL, 0, TIMEOUT_DEFAULT);
-               if (ret < 0)
-                       goto exit_close;
+               INFORM_IF_ERROR(ret)
        }
 
-exit_close:
        if (arg_file)
                close(out_fd);
 exit:
-       return ret;
+       return exit_code;
+#undef INFORM_IF_ERROR
 }