shared: Generalize function to read any /proc/pid entry 89/210489/3
authorKarol Lewandowski <k.lewandowsk@samsung.com>
Fri, 19 Jul 2019 13:22:54 +0000 (15:22 +0200)
committerKarol Lewandowski <k.lewandowsk@samsung.com>
Wed, 24 Jul 2019 09:43:12 +0000 (11:43 +0200)
Change-Id: Ide42d323a2ce6f3901d7a5c26baa9ff5153a512f

src/crash-manager/crash-manager.c
src/shared/util.c
src/shared/util.h

index 59b0537..3cbc777 100644 (file)
@@ -251,12 +251,26 @@ static int make_dump_dir(void)
        return 0;
 }
 
-static int get_cmd_info(struct crash_info *cinfo)
+static bool get_cmd_info(struct crash_info *cinfo)
 {
-       cinfo->cmd_line = get_cmd_line(cinfo->pid_info);
+       assert(cinfo);
+
+       cinfo->cmd_line = malloc(PATH_MAX);
+       if (!cinfo->cmd_line)
+               return false;
+
+       if (!read_proc_file(cinfo->pid_info, "cmdline", cinfo->cmd_line, PATH_MAX, NULL))
+               goto err;
+
        cinfo->cmd_path = get_exe_path(cinfo->pid_info);
+       if (!cinfo->cmd_path)
+               goto err;
 
-       return (cinfo->cmd_line != NULL && cinfo->cmd_path != NULL);
+       return true;
+
+err:
+       free(cinfo->cmd_line);
+       return false;
 }
 
 static int set_prstatus(struct crash_info *cinfo)
@@ -461,8 +475,7 @@ static int set_crash_info(struct crash_info *cinfo, int argc, char *argv[])
                }
        }
 
-       ret = get_cmd_info(cinfo);
-       if (ret <= 0) {
+       if (!get_cmd_info(cinfo)) {
                _E("Failed to get command info");
                return -1;
        }
index f3685d4..84fa25e 100644 (file)
@@ -14,6 +14,8 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+
+#include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdint.h>
@@ -467,36 +469,54 @@ int find_crash_tid(int pid)
        return -1;
 }
 
-char* get_cmd_line(pid_t pid)
+bool filter_drop_trailing_whitespace(char *buf, int size, int datalen)
 {
-       char cmdline_path[PATH_MAX];
+       assert(buf);
+       assert(datalen <= size);
 
-       snprintf(cmdline_path, sizeof(cmdline_path),
-                       "/proc/%d/cmdline", pid);
+       bool filtered = false;
 
-       int fd = open(cmdline_path, O_RDONLY);
-       if (fd < 0) {
-               _E("Failed to open %s: %m\n", cmdline_path);
-               return NULL;
+       for (int i = datalen; i >= 0 && isspace(buf[i]); --i) {
+               buf[i] = '\0';
+               filtered = true;
        }
 
-       char buffer[PATH_MAX];
-       ssize_t ret = read(fd, buffer, sizeof(buffer) - 1);
-       close(fd);
+       return filtered;
+}
 
-       if (ret <= 0) {
-               _E("Failed to read %s: %m\n", cmdline_path);
-               return NULL;
+bool read_proc_file(pid_t pid, const char * const file, char *outbuf, size_t bufsize, charp0filter filter)
+{
+       assert(file);
+       assert(outbuf);
+       assert(bufsize > 0);
+
+       char path[PATH_MAX];
+
+       snprintf(path, sizeof(path), "/proc/%d/%s", pid, file);
+
+       int fd = open(path, O_RDONLY);
+       if (fd < 0) {
+               _E("Failed to open %s: %m\n", path);
+               return false;
        }
-       buffer[ret] = '\0';
 
-       char *result;
-       if (asprintf(&result, "%s", buffer) == -1) {
-               _E("asprintf() error: %m\n");
-               return NULL;
+       ssize_t ret = read(fd, outbuf, bufsize - 1);
+       if (ret < 0) {
+               _E("Failed to read %s: %m\n", path);
+               goto err_close;
        }
+       outbuf[ret] = '\0';
 
-       return result;
+       close(fd);
+
+       if (ret > 0 && filter)
+               (void)filter(outbuf, bufsize, ret - 1);
+
+       return true;
+
+err_close:
+       close(fd);
+       return false;
 }
 
 char* get_exe_path(pid_t pid)
index 561c8bb..0f81ca7 100644 (file)
@@ -60,7 +60,9 @@ int find_crash_tid(int pid);
 
 char* get_exe_path(pid_t pid);
 
-char* get_cmd_line(pid_t pid);
+typedef bool (*charp0filter)(char *buf, int size, int datalen);
+bool filter_drop_trailing_whitespace(char *buf, int size, int datalen);
+bool read_proc_file(pid_t pid, const char * const file, char *outbuf, size_t bufsize, charp0filter filter);
 
 char* concatenate(char *const vec[]);