+/* Controls if async mode is permitted. */
+static int linux_async_permitted = 0;
+
+/* The set command writes to this variable. If the inferior is
+ executing, linux_nat_async_permitted is *not* updated. */
+static int linux_async_permitted_1 = 0;
+
+static void
+set_maintenance_linux_async_permitted (char *args, int from_tty,
+ struct cmd_list_element *c)
+{
+ if (target_has_execution)
+ {
+ linux_async_permitted_1 = linux_async_permitted;
+ error (_("Cannot change this setting while the inferior is running."));
+ }
+
+ linux_async_permitted = linux_async_permitted_1;
+ linux_nat_set_async_mode (linux_async_permitted);
+}
+
+static void
+show_maintenance_linux_async_permitted (struct ui_file *file, int from_tty,
+ struct cmd_list_element *c, const char *value)
+{
+ fprintf_filtered (file, _("\
+Controlling the GNU/Linux inferior in asynchronous mode is %s.\n"),
+ value);
+}
+
+/* target_is_async_p implementation. */
+
+static int
+linux_nat_is_async_p (void)
+{
+ /* NOTE: palves 2008-03-21: We're only async when the user requests
+ it explicitly with the "maintenance set linux-async" command.
+ Someday, linux will always be async. */
+ if (!linux_async_permitted)
+ return 0;
+
+ return 1;
+}
+
+/* target_can_async_p implementation. */
+
+static int
+linux_nat_can_async_p (void)
+{
+ /* NOTE: palves 2008-03-21: We're only async when the user requests
+ it explicitly with the "maintenance set linux-async" command.
+ Someday, linux will always be async. */
+ if (!linux_async_permitted)
+ return 0;
+
+ /* See target.h/target_async_mask. */
+ return linux_nat_async_mask_value;
+}
+
+/* target_async_mask implementation. */
+
+static int
+linux_nat_async_mask (int mask)
+{
+ int current_state;
+ current_state = linux_nat_async_mask_value;
+
+ if (current_state != mask)
+ {
+ if (mask == 0)
+ {
+ linux_nat_async (NULL, 0);
+ linux_nat_async_mask_value = mask;
+ /* We're in sync mode. Make sure SIGCHLD isn't handled by
+ async_sigchld_handler when we come out of sigsuspend in
+ linux_nat_wait. */
+ sigaction (SIGCHLD, &sync_sigchld_action, NULL);
+ }
+ else
+ {
+ /* Restore the async handler. */
+ sigaction (SIGCHLD, &async_sigchld_action, NULL);
+ linux_nat_async_mask_value = mask;
+ linux_nat_async (inferior_event_handler, 0);
+ }
+ }
+
+ return current_state;
+}
+
+/* Pop an event from the event pipe. */
+
+static int
+linux_nat_event_pipe_pop (int* ptr_status, int* ptr_options)
+{
+ struct waitpid_result event = {0};
+ int ret;
+
+ do
+ {
+ ret = read (linux_nat_event_pipe[0], &event, sizeof (event));
+ }
+ while (ret == -1 && errno == EINTR);
+
+ gdb_assert (ret == sizeof (event));
+
+ *ptr_status = event.status;
+ *ptr_options = event.options;
+
+ linux_nat_num_queued_events--;
+
+ return event.pid;
+}
+
+/* Push an event into the event pipe. */
+
+static void
+linux_nat_event_pipe_push (int pid, int status, int options)
+{
+ int ret;
+ struct waitpid_result event = {0};
+ event.pid = pid;
+ event.status = status;
+ event.options = options;
+
+ do
+ {
+ ret = write (linux_nat_event_pipe[1], &event, sizeof (event));
+ gdb_assert ((ret == -1 && errno == EINTR) || ret == sizeof (event));
+ } while (ret == -1 && errno == EINTR);
+
+ linux_nat_num_queued_events++;
+}
+
+static void
+get_pending_events (void)
+{
+ int status, options, pid;
+
+ if (!linux_nat_async_enabled || !linux_nat_async_events_enabled)
+ internal_error (__FILE__, __LINE__,
+ "get_pending_events called with async masked");
+
+ while (1)
+ {
+ status = 0;
+ options = __WCLONE | WNOHANG;
+
+ do
+ {
+ pid = waitpid (-1, &status, options);
+ }
+ while (pid == -1 && errno == EINTR);
+
+ if (pid <= 0)
+ {
+ options = WNOHANG;
+ do
+ {
+ pid = waitpid (-1, &status, options);
+ }
+ while (pid == -1 && errno == EINTR);
+ }
+
+ if (pid <= 0)
+ /* No more children reporting events. */
+ break;
+
+ if (debug_linux_nat_async)
+ fprintf_unfiltered (gdb_stdlog, "\
+get_pending_events: pid(%d), status(%x), options (%x)\n",
+ pid, status, options);
+
+ linux_nat_event_pipe_push (pid, status, options);
+ }
+
+ if (debug_linux_nat_async)
+ fprintf_unfiltered (gdb_stdlog, "\
+get_pending_events: linux_nat_num_queued_events(%d)\n",
+ linux_nat_num_queued_events);
+}
+
+/* SIGCHLD handler for async mode. */
+
+static void
+async_sigchld_handler (int signo)
+{
+ if (debug_linux_nat_async)
+ fprintf_unfiltered (gdb_stdlog, "async_sigchld_handler\n");
+
+ get_pending_events ();
+}
+
+/* Enable or disable async SIGCHLD handling. */
+
+static int
+linux_nat_async_events (int enable)
+{
+ int current_state = linux_nat_async_events_enabled;
+
+ if (debug_linux_nat_async)
+ fprintf_unfiltered (gdb_stdlog,
+ "LNAE: enable(%d): linux_nat_async_events_enabled(%d), "
+ "linux_nat_num_queued_events(%d)\n",
+ enable, linux_nat_async_events_enabled,
+ linux_nat_num_queued_events);
+
+ if (current_state != enable)
+ {
+ sigset_t mask;
+ sigemptyset (&mask);
+ sigaddset (&mask, SIGCHLD);
+ if (enable)
+ {
+ /* Unblock target events. */
+ linux_nat_async_events_enabled = 1;
+
+ local_event_queue_to_pipe ();
+ /* While in masked async, we may have not collected all the
+ pending events. Get them out now. */
+ get_pending_events ();
+ sigprocmask (SIG_UNBLOCK, &mask, NULL);
+ }
+ else
+ {
+ /* Block target events. */
+ sigprocmask (SIG_BLOCK, &mask, NULL);
+ linux_nat_async_events_enabled = 0;
+ /* Get events out of queue, and make them available to
+ queued_waitpid / my_waitpid. */
+ pipe_to_local_event_queue ();
+ }
+ }
+
+ return current_state;
+}
+
+static int async_terminal_is_ours = 1;
+
+/* target_terminal_inferior implementation. */
+
+static void
+linux_nat_terminal_inferior (void)
+{
+ if (!target_is_async_p ())
+ {
+ /* Async mode is disabled. */
+ terminal_inferior ();
+ return;
+ }
+
+ /* GDB should never give the terminal to the inferior, if the
+ inferior is running in the background (run&, continue&, etc.).
+ This check can be removed when the common code is fixed. */
+ if (!sync_execution)
+ return;
+
+ terminal_inferior ();
+
+ if (!async_terminal_is_ours)
+ return;
+
+ delete_file_handler (input_fd);
+ async_terminal_is_ours = 0;
+ set_sigint_trap ();
+}
+
+/* target_terminal_ours implementation. */
+
+void
+linux_nat_terminal_ours (void)
+{
+ if (!target_is_async_p ())
+ {
+ /* Async mode is disabled. */
+ terminal_ours ();
+ return;
+ }
+
+ /* GDB should never give the terminal to the inferior if the
+ inferior is running in the background (run&, continue&, etc.),
+ but claiming it sure should. */
+ terminal_ours ();
+
+ if (!sync_execution)
+ return;
+
+ if (async_terminal_is_ours)
+ return;
+
+ clear_sigint_trap ();
+ add_file_handler (input_fd, stdin_event_handler, 0);
+ async_terminal_is_ours = 1;
+}
+
+static void (*async_client_callback) (enum inferior_event_type event_type,
+ void *context);
+static void *async_client_context;
+
+static void
+linux_nat_async_file_handler (int error, gdb_client_data client_data)
+{
+ async_client_callback (INF_REG_EVENT, async_client_context);
+}
+
+/* target_async implementation. */
+
+static void
+linux_nat_async (void (*callback) (enum inferior_event_type event_type,
+ void *context), void *context)
+{
+ if (linux_nat_async_mask_value == 0 || !linux_nat_async_enabled)
+ internal_error (__FILE__, __LINE__,
+ "Calling target_async when async is masked");
+
+ if (callback != NULL)
+ {
+ async_client_callback = callback;
+ async_client_context = context;
+ add_file_handler (linux_nat_event_pipe[0],
+ linux_nat_async_file_handler, NULL);
+
+ linux_nat_async_events (1);
+ }
+ else
+ {
+ async_client_callback = callback;
+ async_client_context = context;
+
+ linux_nat_async_events (0);
+ delete_file_handler (linux_nat_event_pipe[0]);
+ }
+ return;
+}
+
+/* Enable/Disable async mode. */
+
+static void
+linux_nat_set_async_mode (int on)
+{
+ if (linux_nat_async_enabled != on)
+ {
+ if (on)
+ {
+ gdb_assert (waitpid_queue == NULL);
+ sigaction (SIGCHLD, &async_sigchld_action, NULL);
+
+ if (pipe (linux_nat_event_pipe) == -1)
+ internal_error (__FILE__, __LINE__,
+ "creating event pipe failed.");
+
+ fcntl (linux_nat_event_pipe[0], F_SETFL, O_NONBLOCK);
+ fcntl (linux_nat_event_pipe[1], F_SETFL, O_NONBLOCK);
+ }
+ else
+ {
+ sigaction (SIGCHLD, &sync_sigchld_action, NULL);
+
+ drain_queued_events (-1);
+
+ linux_nat_num_queued_events = 0;
+ close (linux_nat_event_pipe[0]);
+ close (linux_nat_event_pipe[1]);
+ linux_nat_event_pipe[0] = linux_nat_event_pipe[1] = -1;
+
+ }
+ }
+ linux_nat_async_enabled = on;
+}
+