Share fork_inferior et al with gdbserver
[external/binutils.git] / gdb / gdbserver / fork-child.c
1 /* Fork a Unix child process, and set up to debug it, for GDBserver.
2    Copyright (C) 1989-2017 Free Software Foundation, Inc.
3
4    This file is part of GDB.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19 #include "server.h"
20 #include "job-control.h"
21 #include "nat/fork-inferior.h"
22
23 #ifdef SIGTTOU
24 /* A file descriptor for the controlling terminal.  */
25 static int terminal_fd;
26
27 /* TERMINAL_FD's original foreground group.  */
28 static pid_t old_foreground_pgrp;
29
30 /* Hand back terminal ownership to the original foreground group.  */
31
32 static void
33 restore_old_foreground_pgrp (void)
34 {
35   tcsetpgrp (terminal_fd, old_foreground_pgrp);
36 }
37 #endif
38
39 /* See nat/fork-inferior.h.  */
40
41 void
42 prefork_hook (const char *args)
43 {
44   if (debug_threads)
45     {
46       debug_printf ("args: %s\n", args);
47       debug_flush ();
48     }
49
50 #ifdef SIGTTOU
51   signal (SIGTTOU, SIG_DFL);
52   signal (SIGTTIN, SIG_DFL);
53 #endif
54
55   /* Clear this so the backend doesn't get confused, thinking
56      CONT_THREAD died, and it needs to resume all threads.  */
57   cont_thread = null_ptid;
58 }
59
60 /* See nat/fork-inferior.h.  */
61
62 void
63 postfork_hook (pid_t pid)
64 {
65 }
66
67 /* See nat/fork-inferior.h.  */
68
69 void
70 postfork_child_hook ()
71 {
72   /* This is set to the result of setpgrp, which if vforked, will be
73      visible to you in the parent process.  It's only used by humans
74      for debugging.  */
75   static int debug_setpgrp = 657473;
76
77   debug_setpgrp = gdb_setpgid ();
78   if (debug_setpgrp == -1)
79     perror (_("setpgrp failed in child"));
80 }
81
82 /* See nat/fork-inferior.h.  */
83
84 void
85 gdb_flush_out_err ()
86 {
87   fflush (stdout);
88   fflush (stderr);
89 }
90
91 /* See server.h.  */
92
93 void
94 post_fork_inferior (int pid, const char *program)
95 {
96 #ifdef SIGTTOU
97   signal (SIGTTOU, SIG_IGN);
98   signal (SIGTTIN, SIG_IGN);
99   terminal_fd = fileno (stderr);
100   old_foreground_pgrp = tcgetpgrp (terminal_fd);
101   tcsetpgrp (terminal_fd, pid);
102   atexit (restore_old_foreground_pgrp);
103 #endif
104
105   startup_inferior (pid, START_INFERIOR_TRAPS_EXPECTED,
106                     &last_status, &last_ptid);
107   current_thread->last_resume_kind = resume_stop;
108   current_thread->last_status = last_status;
109   signal_pid = pid;
110   target_post_create_inferior ();
111   fprintf (stderr, "Process %s created; pid = %d\n", program, pid);
112   fflush (stderr);
113 }