coverity appeasement
[minix.git] / commands / gcore / gcore.c
blobd07d9c0b26570fc86927a4bc92d025227943fee0
1 /* gcore - create core file of running process */
3 #include <sys/types.h>
4 #include <sys/ptrace.h>
5 #include <sys/wait.h>
6 #include <signal.h>
7 #include <errno.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <stdlib.h>
12 int main(int argc, char *argv[])
14 pid_t pid;
15 int status;
17 if(argc != 2) {
18 printf("usage: %s <pid>\n", argv[0]);
19 return 1;
22 pid = atoi(argv[1]);
24 if (ptrace(T_ATTACH, pid, 0, 0) != 0) {
25 perror("ptrace(T_ATTACH)");
26 return 1;
29 if (waitpid(pid, &status, 0) != pid) {
30 perror("waitpid");
31 return 1;
34 while (WIFSTOPPED(status) && WSTOPSIG(status) != SIGSTOP) {
35 /* whatever happens here is fine */
36 ptrace(T_RESUME, pid, 0, WSTOPSIG(status));
38 if (waitpid(pid, &status, 0) != pid) {
39 perror("waitpid");
40 return 1;
44 if (!WIFSTOPPED(status)) {
45 fprintf(stderr, "process died while attaching\n");
46 return 1;
49 if (ptrace(T_DUMPCORE, pid, 0, 0)) {
50 fprintf(stderr, "warning, dumpcore failed (%s)\n", strerror(errno));
53 if (ptrace(T_DETACH, pid, 0, 0)) {
54 fprintf(stderr, "warning, detaching failed (%s)\n", strerror(errno));
57 return 0;