job-control.cc 2.4 KB

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