1 /* This testcase is part of GDB, the GNU debugger. 2 3 Copyright 2015-2019 Free Software Foundation, Inc. 4 5 This program is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation; either version 3 of the License, or 8 (at your option) any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 17 18 #include <unistd.h> 19 #include <stdio.h> 20 #include <sys/types.h> 21 #include <sys/wait.h> 22 #include <errno.h> 23 24 int save_parent; 25 26 /* Variable set by GDB. If true, then a fork child (or parent) exits 27 if its parent (or child) exits. Otherwise the process waits 28 forever until either GDB or the alarm kills it. */ 29 volatile int exit_if_relative_exits = 0; 30 31 /* The fork child. Just runs forever. */ 32 33 static int 34 fork_child (void) 35 { 36 /* Don't run forever. */ 37 alarm (180); 38 39 while (1) 40 { 41 if (exit_if_relative_exits) 42 { 43 sleep (1); 44 45 /* Exit if GDB kills the parent. */ 46 if (getppid () != save_parent) 47 break; 48 if (kill (getppid (), 0) != 0) 49 break; 50 } 51 else 52 pause (); 53 } 54 55 return 0; 56 } 57 58 /* The fork parent. Just runs forever. */ 59 60 static int 61 fork_parent (void) 62 { 63 /* Don't run forever. */ 64 alarm (180); 65 66 while (1) 67 { 68 if (exit_if_relative_exits) 69 { 70 int res = wait (NULL); 71 if (res == -1 && errno == EINTR) 72 continue; 73 else if (res == -1) 74 { 75 perror ("wait"); 76 return 1; 77 } 78 else 79 return 0; 80 } 81 else 82 pause (); 83 } 84 85 return 0; 86 } 87 88 int 89 main (void) 90 { 91 pid_t pid; 92 93 save_parent = getpid (); 94 95 /* The parent and child should basically run forever without 96 tripping on any debug event. We want to check that GDB updates 97 the parent and child running states correctly right after the 98 fork. */ 99 pid = fork (); 100 if (pid > 0) 101 return fork_parent (); 102 else if (pid == 0) 103 return fork_child (); 104 else 105 { 106 perror ("fork"); 107 return 1; 108 } 109 } 110