18dffb485Schristos /* Job control and terminal related functions, for GDB and gdbserver 28dffb485Schristos when running under Unix. 38dffb485Schristos 4*5ba1f45fSchristos Copyright (C) 1986-2024 Free Software Foundation, Inc. 58dffb485Schristos 68dffb485Schristos This file is part of GDB. 78dffb485Schristos 88dffb485Schristos This program is free software; you can redistribute it and/or modify 98dffb485Schristos it under the terms of the GNU General Public License as published by 108dffb485Schristos the Free Software Foundation; either version 3 of the License, or 118dffb485Schristos (at your option) any later version. 128dffb485Schristos 138dffb485Schristos This program is distributed in the hope that it will be useful, 148dffb485Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of 158dffb485Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 168dffb485Schristos GNU General Public License for more details. 178dffb485Schristos 188dffb485Schristos You should have received a copy of the GNU General Public License 198dffb485Schristos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 208dffb485Schristos 218dffb485Schristos #include "job-control.h" 228dffb485Schristos #ifdef HAVE_TERMIOS_H 238dffb485Schristos #include <termios.h> 248dffb485Schristos #endif 258dffb485Schristos #include <unistd.h> 268dffb485Schristos 278dffb485Schristos /* Nonzero if we have job control. */ 288dffb485Schristos int job_control; 298dffb485Schristos 308dffb485Schristos /* Set the process group ID of the inferior. 318dffb485Schristos 328dffb485Schristos Just using job_control only does part of it because setpgid or 338dffb485Schristos setpgrp might not exist on a system without job control. 348dffb485Schristos 358dffb485Schristos For a more clean implementation, in libiberty, put a setpgid which merely 368dffb485Schristos calls setpgrp and a setpgrp which does nothing (any system with job control 378dffb485Schristos will have one or the other). */ 388dffb485Schristos 398dffb485Schristos int 408dffb485Schristos gdb_setpgid () 418dffb485Schristos { 428dffb485Schristos int retval = 0; 438dffb485Schristos 448dffb485Schristos if (job_control) 458dffb485Schristos { 468dffb485Schristos #ifdef HAVE_SETPGID 478dffb485Schristos /* The call setpgid (0, 0) is supposed to work and mean the same 488dffb485Schristos thing as this, but on Ultrix 4.2A it fails with EPERM (and 498dffb485Schristos setpgid (getpid (), getpid ()) succeeds). */ 508dffb485Schristos retval = setpgid (getpid (), getpid ()); 518dffb485Schristos #else 528dffb485Schristos #ifdef HAVE_SETPGRP 538dffb485Schristos #ifdef SETPGRP_VOID 548dffb485Schristos retval = setpgrp (); 558dffb485Schristos #else 568dffb485Schristos retval = setpgrp (getpid (), getpid ()); 578dffb485Schristos #endif 588dffb485Schristos #endif /* HAVE_SETPGRP */ 598dffb485Schristos #endif /* HAVE_SETPGID */ 608dffb485Schristos } 618dffb485Schristos 628dffb485Schristos return retval; 638dffb485Schristos } 648dffb485Schristos 658dffb485Schristos /* See gdbsupport/common-terminal.h. */ 668dffb485Schristos 678dffb485Schristos void 688dffb485Schristos have_job_control () 698dffb485Schristos { 708dffb485Schristos /* OK, figure out whether we have job control. If termios is not 718dffb485Schristos available, leave job_control 0. */ 728dffb485Schristos #if defined (HAVE_TERMIOS_H) 738dffb485Schristos /* Do all systems with termios have the POSIX way of identifying job 748dffb485Schristos control? I hope so. */ 758dffb485Schristos #ifdef _POSIX_JOB_CONTROL 768dffb485Schristos job_control = 1; 778dffb485Schristos #else 788dffb485Schristos #ifdef _SC_JOB_CONTROL 798dffb485Schristos job_control = sysconf (_SC_JOB_CONTROL); 808dffb485Schristos #else 818dffb485Schristos job_control = 0; /* Have to assume the worst. */ 828dffb485Schristos #endif /* _SC_JOB_CONTROL */ 838dffb485Schristos #endif /* _POSIX_JOB_CONTROL */ 848dffb485Schristos #endif /* HAVE_TERMIOS_H */ 858dffb485Schristos } 86