]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.base/fork-running-state.c
Update copyright year range in all GDB files.
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.base / fork-running-state.c
CommitLineData
a2077e25
PA
1/* This testcase is part of GDB, the GNU debugger.
2
b811d2c2 3 Copyright 2015-2020 Free Software Foundation, Inc.
a2077e25
PA
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>
14897d65 22#include <errno.h>
a2077e25
PA
23
24int save_parent;
25
14897d65
PA
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. */
29volatile int exit_if_relative_exits = 0;
30
a2077e25
PA
31/* The fork child. Just runs forever. */
32
33static int
34fork_child (void)
35{
a08ac84b
TV
36 /* Don't run forever. */
37 alarm (180);
38
a2077e25 39 while (1)
14897d65
PA
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 }
a2077e25
PA
54
55 return 0;
56}
57
f50d8a2e 58/* The fork parent. Just runs forever. */
a2077e25
PA
59
60static int
61fork_parent (void)
62{
a08ac84b
TV
63 /* Don't run forever. */
64 alarm (180);
65
f50d8a2e 66 while (1)
14897d65
PA
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 }
a2077e25
PA
84
85 return 0;
86}
87
88int
89main (void)
90{
91 pid_t pid;
92
93 save_parent = getpid ();
94
a2077e25
PA
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}