crash-manager: fix get cmd_path 74/170974/10
authorMateusz Moscicki <mmoscicki2@partner.samsung.com>
Thu, 22 Feb 2018 09:40:41 +0000 (10:40 +0100)
committerKarol Lewandowski <k.lewandowsk@samsung.com>
Fri, 16 Mar 2018 13:59:45 +0000 (13:59 +0000)
This commit fixes issue with report not being generated for processes
started using a relative path (e.g.: ./test-app).

In above case cmdline contained only './test-app' string and based on
that it was impossible to determine the full path to a binary.
Consequently, crash-manager assumed crashed program binary did not
exist and did not generate the report.

This commit changes the logic as follows:

  cinfo->cmd_name is read from /proc/<pid>/cmdline
  cinfo->cmd_path is read from /proc/<pid>/exe

Change-Id: If1b7557b53417d2703a81644939692fee5c801ee

src/crash-manager/crash-manager.c

index 580cb3c1a0b971294b6bb4ba8eba8659ddc1fc1a..4a5a44a49e5010dc0be215dd05ffe8cc15ddfda5 100644 (file)
@@ -209,8 +209,9 @@ static int get_cmd_info(struct crash_info *cinfo)
 {
        int fd;
        int ret;
-       char *dup_cmd_path;
+       char cmdline[PATH_MAX];
        char cmdline_path[PATH_MAX];
+       char exe_link[PATH_MAX];
 
        snprintf(cmdline_path, sizeof(cmdline_path),
                        "/proc/%s/cmdline", cinfo->pid_info);
@@ -220,21 +221,32 @@ static int get_cmd_info(struct crash_info *cinfo)
                return -1;
        }
 
-       ret = read(fd, cinfo->cmd_path, sizeof(cinfo->cmd_path) - 1);
+       ret = read(fd, cmdline, sizeof(cmdline) - 1);
        if (ret <= 0) {
                _E("Failed to read %s", cmdline_path);
                goto error;
-       } else if (access(cinfo->cmd_path, F_OK) == -1) {
-               _E("Invalid path %s", cinfo->cmd_path);
-               ret = -1;
+       }
+
+       snprintf(cinfo->cmd_name, sizeof(cinfo->cmd_name), "%s",
+                basename(cmdline));
+
+       snprintf(exe_link, sizeof(exe_link),
+               "/proc/%s/exe", cinfo->pid_info);
+
+       ret = readlink(exe_link, cinfo->cmd_path,
+                               sizeof(cinfo->cmd_path) - 1);
+       if (ret <= 0) {
+               _E("Failed to read link %s", exe_link);
                goto error;
        }
+
        cinfo->cmd_path[ret] = '\0';
 
-       dup_cmd_path = strdup(cinfo->cmd_path);
-       snprintf(cinfo->cmd_name, sizeof(cinfo->cmd_name),
-                       "%s", basename(dup_cmd_path));
-       free(dup_cmd_path);
+       if (access(cinfo->cmd_path, F_OK) == -1) {
+               _E("Invalid path %s", cinfo->cmd_path);
+               ret = -1;
+               goto error;
+       }
 
 error:
        close(fd);