summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKarol Lewandowski <k.lewandowsk@samsung.com>2019-07-19 15:22:54 +0200
committerKarol Lewandowski <k.lewandowsk@samsung.com>2019-07-24 11:43:12 +0200
commite0121a8b42ed0d6f83b91aa2e967ac00524a2851 (patch)
treeb065f13519ab3ca6893509af1ffa3163dace6eff
parentdb04cb109be580223004b8f22470428be5dae4ea (diff)
downloadcrash-worker-e0121a8b42ed0d6f83b91aa2e967ac00524a2851.tar.gz
crash-worker-e0121a8b42ed0d6f83b91aa2e967ac00524a2851.tar.bz2
crash-worker-e0121a8b42ed0d6f83b91aa2e967ac00524a2851.zip
shared: Generalize function to read any /proc/pid entry
Change-Id: Ide42d323a2ce6f3901d7a5c26baa9ff5153a512f
-rw-r--r--src/crash-manager/crash-manager.c23
-rw-r--r--src/shared/util.c60
-rw-r--r--src/shared/util.h4
3 files changed, 61 insertions, 26 deletions
diff --git a/src/crash-manager/crash-manager.c b/src/crash-manager/crash-manager.c
index 59b0537..3cbc777 100644
--- a/src/crash-manager/crash-manager.c
+++ b/src/crash-manager/crash-manager.c
@@ -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;
}
diff --git a/src/shared/util.c b/src/shared/util.c
index f3685d4..84fa25e 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -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)
diff --git a/src/shared/util.h b/src/shared/util.h
index 561c8bb..0f81ca7 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -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[]);