ser-pipe.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* Serial interface for a pipe to a separate program
  2. Copyright (C) 1999-2022 Free Software Foundation, Inc.
  3. Contributed by Cygnus Solutions.
  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 "defs.h"
  16. #include "serial.h"
  17. #include "ser-base.h"
  18. #include "ser-unix.h"
  19. #include "gdb_vfork.h"
  20. #include <sys/types.h>
  21. #include <sys/socket.h>
  22. #include "gdbsupport/gdb_sys_time.h"
  23. #include <fcntl.h>
  24. #include "gdbsupport/filestuff.h"
  25. #include "gdbsupport/pathstuff.h"
  26. #include <signal.h>
  27. static int pipe_open (struct serial *scb, const char *name);
  28. static void pipe_close (struct serial *scb);
  29. struct pipe_state
  30. {
  31. int pid;
  32. };
  33. /* Open up a raw pipe. */
  34. static int
  35. pipe_open (struct serial *scb, const char *name)
  36. {
  37. #if !HAVE_SOCKETPAIR
  38. return -1;
  39. #else
  40. struct pipe_state *state;
  41. /* This chunk: */
  42. /* Copyright (c) 1988, 1993
  43. * The Regents of the University of California. All rights reserved.
  44. *
  45. * This code is derived from software written by Ken Arnold and
  46. * published in UNIX Review, Vol. 6, No. 8.
  47. */
  48. int pdes[2];
  49. int err_pdes[2];
  50. int pid;
  51. if (*name == '|')
  52. {
  53. name++;
  54. name = skip_spaces (name);
  55. }
  56. if (gdb_socketpair_cloexec (AF_UNIX, SOCK_STREAM, 0, pdes) < 0)
  57. return -1;
  58. if (gdb_socketpair_cloexec (AF_UNIX, SOCK_STREAM, 0, err_pdes) < 0)
  59. {
  60. close (pdes[0]);
  61. close (pdes[1]);
  62. return -1;
  63. }
  64. /* Create the child process to run the command in. Note that the
  65. apparent call to vfork() below *might* actually be a call to
  66. fork() due to the fact that autoconf will ``#define vfork fork''
  67. on certain platforms. */
  68. pid = vfork ();
  69. /* Error. */
  70. if (pid == -1)
  71. {
  72. close (pdes[0]);
  73. close (pdes[1]);
  74. close (err_pdes[0]);
  75. close (err_pdes[1]);
  76. return -1;
  77. }
  78. if (fcntl (err_pdes[0], F_SETFL, O_NONBLOCK) == -1)
  79. {
  80. close (err_pdes[0]);
  81. close (err_pdes[1]);
  82. err_pdes[0] = err_pdes[1] = -1;
  83. }
  84. /* Child. */
  85. if (pid == 0)
  86. {
  87. /* We don't want ^c to kill the connection. */
  88. #ifdef HAVE_SETSID
  89. pid_t sid = setsid ();
  90. if (sid == -1)
  91. signal (SIGINT, SIG_IGN);
  92. #else
  93. signal (SIGINT, SIG_IGN);
  94. #endif
  95. /* Re-wire pdes[1] to stdin/stdout. */
  96. close (pdes[0]);
  97. if (pdes[1] != STDOUT_FILENO)
  98. {
  99. dup2 (pdes[1], STDOUT_FILENO);
  100. close (pdes[1]);
  101. }
  102. dup2 (STDOUT_FILENO, STDIN_FILENO);
  103. if (err_pdes[0] != -1)
  104. {
  105. close (err_pdes[0]);
  106. dup2 (err_pdes[1], STDERR_FILENO);
  107. close (err_pdes[1]);
  108. }
  109. close_most_fds ();
  110. const char *shellfile = get_shell ();
  111. execl (shellfile, shellfile, "-c", name, (char *) 0);
  112. _exit (127);
  113. }
  114. /* Parent. */
  115. close (pdes[1]);
  116. if (err_pdes[1] != -1)
  117. close (err_pdes[1]);
  118. /* :end chunk */
  119. state = XNEW (struct pipe_state);
  120. state->pid = pid;
  121. scb->fd = pdes[0];
  122. scb->error_fd = err_pdes[0];
  123. scb->state = state;
  124. /* If we don't do this, GDB simply exits when the remote side dies. */
  125. signal (SIGPIPE, SIG_IGN);
  126. return 0;
  127. #endif
  128. }
  129. static void
  130. pipe_close (struct serial *scb)
  131. {
  132. struct pipe_state *state = (struct pipe_state *) scb->state;
  133. close (scb->fd);
  134. scb->fd = -1;
  135. if (state != NULL)
  136. {
  137. int wait_result, status;
  138. /* Don't kill the task right away, give it a chance to shut down cleanly.
  139. But don't wait forever though. */
  140. #define PIPE_CLOSE_TIMEOUT 5
  141. /* Assume the program will exit after SIGTERM. Might be
  142. useful to print any remaining stderr output from
  143. scb->error_fd while waiting. */
  144. #define SIGTERM_TIMEOUT INT_MAX
  145. wait_result = -1;
  146. #ifdef HAVE_WAITPID
  147. wait_result = wait_to_die_with_timeout (state->pid, &status,
  148. PIPE_CLOSE_TIMEOUT);
  149. #endif
  150. if (wait_result == -1)
  151. {
  152. kill (state->pid, SIGTERM);
  153. #ifdef HAVE_WAITPID
  154. wait_to_die_with_timeout (state->pid, &status, SIGTERM_TIMEOUT);
  155. #endif
  156. }
  157. if (scb->error_fd != -1)
  158. close (scb->error_fd);
  159. scb->error_fd = -1;
  160. xfree (state);
  161. scb->state = NULL;
  162. }
  163. }
  164. int
  165. gdb_pipe (int pdes[2])
  166. {
  167. #if !HAVE_SOCKETPAIR
  168. errno = ENOSYS;
  169. return -1;
  170. #else
  171. if (gdb_socketpair_cloexec (AF_UNIX, SOCK_STREAM, 0, pdes) < 0)
  172. return -1;
  173. /* If we don't do this, GDB simply exits when the remote side
  174. dies. */
  175. signal (SIGPIPE, SIG_IGN);
  176. return 0;
  177. #endif
  178. }
  179. static const struct serial_ops pipe_ops =
  180. {
  181. "pipe",
  182. pipe_open,
  183. pipe_close,
  184. NULL,
  185. ser_base_readchar,
  186. ser_base_write,
  187. ser_base_flush_output,
  188. ser_base_flush_input,
  189. ser_base_send_break,
  190. ser_base_raw,
  191. ser_base_get_tty_state,
  192. ser_base_copy_tty_state,
  193. ser_base_set_tty_state,
  194. ser_base_print_tty_state,
  195. ser_base_setbaudrate,
  196. ser_base_setstopbits,
  197. ser_base_setparity,
  198. ser_base_drain_output,
  199. ser_base_async,
  200. ser_unix_read_prim,
  201. ser_unix_write_prim
  202. };
  203. void _initialize_ser_pipe ();
  204. void
  205. _initialize_ser_pipe ()
  206. {
  207. serial_add_interface (&pipe_ops);
  208. }