Return exit code of the program invoked by run_command_write_fd_timeout() 17/183917/1
authorMateusz Moscicki <m.moscicki2@partner.samsung.com>
Mon, 2 Jul 2018 07:02:31 +0000 (09:02 +0200)
committerMateusz Moscicki <m.moscicki2@partner.samsung.com>
Thu, 12 Jul 2018 08:33:44 +0000 (10:33 +0200)
Change-Id: Ib7f046aed8ca352463d6ba7559f947e487ad85b8

src/crash-manager/so-info.c
src/shared/util.c

index b766e24..0ef0bc7 100644 (file)
@@ -229,7 +229,7 @@ int get_rpm_info(char *filename, char *rpm_info)
        char *args[] = {"rpm", "-qf", filename, "--qf", "%{NAME};%{VERSION};%{RELEASE};%{ARCH}", NULL};
 
        int readed;
-       if ((readed = run_command_write_fd_timeout("rpm", args, NULL, 0, rpm_info, RPM_INFO_MAXLEN, 60)) == -1) {
+       if ((readed = run_command_write_fd_timeout("rpm", args, NULL, 0, rpm_info, RPM_INFO_MAXLEN, 60)) < 0) {
                _E("Failed to get information about rpm installed packages");
                return -1;
        } else {
index 8075f12..084a57c 100644 (file)
@@ -313,7 +313,7 @@ static int run_command(char *path, char *args[], char *env[], int fd[])
        return -1;
 }
 
-static void wait_for_child(pid_t pid, int timeout)
+static int wait_for_child(pid_t pid, int *exit_code, int timeout)
 {
        for (int i = 0; i < 10*timeout; i++) {
                int status;
@@ -321,18 +321,18 @@ static void wait_for_child(pid_t pid, int timeout)
                if (p == pid) {
                        if (WIFSIGNALED(status)) {
                                _I("Killed by signal %d\n", WTERMSIG(status));
-                               break;
+                               return -1;
                        } else if (WIFEXITED(status)) {
-                               if (WEXITSTATUS(status) > 0)
-                                       _I("Exit code %d\n", WEXITSTATUS(status));
-                               break;
+                               *exit_code = WEXITSTATUS(status);
+                               return 0;
                        }
                } else if (p == -1) {
                        _E("waitpid error: %m");
-                       break;
+                       return -1;
                }
                usleep(100000);
        }
+       return -1;
 }
 
 static int read_into_buff(int fd, char *buff, int size, int timeout_us, int *eof)
@@ -479,9 +479,11 @@ int run_command_write_fd_timeout(char *path, char *args[], char *env[], int dfd,
                }
 
                // let's wait a second for a child
-               wait_for_child(pid, 1);
+               int exit_code = -1;
+               if (wait_for_child(pid, &exit_code, 1) == 0 && exit_code != 0)
+                       _I("\"%s\" exit code: %d\n", path, exit_code);
 
-               return eof == 1 ? count : -1;
+               return (eof == 1 && exit_code == 0) ? count : -abs(exit_code);
        } else {
                _E("fork() error: %m");
                return -1;