Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
extract: verify pid exists
  • Loading branch information
leahneukirchen committed Sep 17, 2018
1 parent 0dcdc24 commit fffd96e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions 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)

Expand Down
25 changes: 24 additions & 1 deletion extrace.c
Expand Up @@ -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[])
{
Expand All @@ -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':
Expand Down

0 comments on commit fffd96e

Please sign in to comment.