From fffd96ebd5148e1ac6e73250f06a132ab36443b6 Mon Sep 17 00:00:00 2001 From: Leah Neukirchen Date: Mon, 17 Sep 2018 13:08:14 +0200 Subject: extract: verify pid exists --- NEWS.md | 1 + extrace.c | 25 ++++++++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 5d67ad4..90c972a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,7 @@ ## HEAD * pwait: detect and warn for non-existing PID. +* extract: detect and error for non-existing PID in `-p`. ## 0.6 (2018-06-19) diff --git a/extrace.c b/extrace.c index 24c5d84..e28a29c 100644 --- a/extrace.c +++ b/extrace.c @@ -443,6 +443,29 @@ handle_msg(struct cn_msg *cn_hdr) } } +static pid_t +parse_pid(char *s) +{ + pid_t pid; + char *end; + + errno = 0; + pid = strtol(s, &end, 10); + if (pid <= 0 || *end || errno != 0) { + fprintf(stderr, "extrace: %s: invalid process id\n", s); + exit(1); + } + + errno = 0; + kill(pid, 0); + if (errno == ESRCH) { + fprintf(stderr, "extrace: %s: no such process\n", s); + exit(1); + } + + return pid; +} + int main(int argc, char *argv[]) { @@ -465,7 +488,7 @@ main(int argc, char *argv[]) case 'e': show_env = 1; break; case 'f': flat = 1; break; case 'l': full_path = 1; break; - case 'p': parent = atoi(optarg); break; + case 'p': parent = parse_pid(optarg); break; case 'q': show_args = 0; break; case 't': show_exit = 1; break; case 'o': -- cgit 1.4.1