| 1 | /* Job control and terminal related functions, for GDB and gdbserver |
| 2 | when running under Unix. |
| 3 | |
| 4 | Copyright (C) 1986-2020 Free Software Foundation, Inc. |
| 5 | |
| 6 | This file is part of GDB. |
| 7 | |
| 8 | This program is free software; you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation; either version 3 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 20 | |
| 21 | #include "common-defs.h" |
| 22 | #include "job-control.h" |
| 23 | #ifdef HAVE_TERMIOS_H |
| 24 | #include <termios.h> |
| 25 | #endif |
| 26 | #include <unistd.h> |
| 27 | |
| 28 | /* Nonzero if we have job control. */ |
| 29 | int job_control; |
| 30 | |
| 31 | /* Set the process group ID of the inferior. |
| 32 | |
| 33 | Just using job_control only does part of it because setpgid or |
| 34 | setpgrp might not exist on a system without job control. |
| 35 | |
| 36 | For a more clean implementation, in libiberty, put a setpgid which merely |
| 37 | calls setpgrp and a setpgrp which does nothing (any system with job control |
| 38 | will have one or the other). */ |
| 39 | |
| 40 | int |
| 41 | gdb_setpgid () |
| 42 | { |
| 43 | int retval = 0; |
| 44 | |
| 45 | if (job_control) |
| 46 | { |
| 47 | #ifdef HAVE_SETPGID |
| 48 | /* The call setpgid (0, 0) is supposed to work and mean the same |
| 49 | thing as this, but on Ultrix 4.2A it fails with EPERM (and |
| 50 | setpgid (getpid (), getpid ()) succeeds). */ |
| 51 | retval = setpgid (getpid (), getpid ()); |
| 52 | #else |
| 53 | #ifdef HAVE_SETPGRP |
| 54 | #ifdef SETPGRP_VOID |
| 55 | retval = setpgrp (); |
| 56 | #else |
| 57 | retval = setpgrp (getpid (), getpid ()); |
| 58 | #endif |
| 59 | #endif /* HAVE_SETPGRP */ |
| 60 | #endif /* HAVE_SETPGID */ |
| 61 | } |
| 62 | |
| 63 | return retval; |
| 64 | } |
| 65 | |
| 66 | /* See gdbsupport/common-terminal.h. */ |
| 67 | |
| 68 | void |
| 69 | have_job_control () |
| 70 | { |
| 71 | /* OK, figure out whether we have job control. If termios is not |
| 72 | available, leave job_control 0. */ |
| 73 | #if defined (HAVE_TERMIOS_H) |
| 74 | /* Do all systems with termios have the POSIX way of identifying job |
| 75 | control? I hope so. */ |
| 76 | #ifdef _POSIX_JOB_CONTROL |
| 77 | job_control = 1; |
| 78 | #else |
| 79 | #ifdef _SC_JOB_CONTROL |
| 80 | job_control = sysconf (_SC_JOB_CONTROL); |
| 81 | #else |
| 82 | job_control = 0; /* Have to assume the worst. */ |
| 83 | #endif /* _SC_JOB_CONTROL */ |
| 84 | #endif /* _POSIX_JOB_CONTROL */ |
| 85 | #endif /* HAVE_TERMIOS_H */ |
| 86 | } |