From c867b9d40ecaea6452a8f71cf9bf14b7dd69bc5b Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Sat, 1 Feb 2020 11:49:47 +0300 Subject: [PATCH] Ticket #5056: avoid subshell warning for mcedit run from mc. Don't show message "GNU Midnight Commander is already running on this terminal. Subshell support will be disabled." if standalone mcedit/mcview/mcdiffview is run from mc. Show this message only in case of mc run from another mc, as was before commit 41abcbf706b97cebd8e127465469c69639da9f09 (ticket #3380). Signed-off-by: Andrew Borodin --- lib/global.c | 1 + lib/global.h | 1 + src/main.c | 104 +++++++++++++++++++++++++++++++++--------- src/subshell/common.c | 54 ---------------------- 4 files changed, 85 insertions(+), 75 deletions(-) diff --git a/lib/global.c b/lib/global.c index 0c63dc5b2..b21b5c0e9 100644 --- a/lib/global.c +++ b/lib/global.c @@ -50,6 +50,7 @@ /* *INDENT-OFF* */ mc_global_t mc_global = { .mc_run_mode = MC_RUN_FULL, + .run_from_parent_mc = FALSE, .timer = NULL, .midnight_shutdown = FALSE, diff --git a/lib/global.h b/lib/global.h index 244781df6..846c724c6 100644 --- a/lib/global.h +++ b/lib/global.h @@ -166,6 +166,7 @@ typedef enum typedef struct { mc_run_mode_t mc_run_mode; + gboolean run_from_parent_mc; /* global timer */ mc_timer_t *timer; /* Used so that widgets know if they are being destroyed or shut down */ diff --git a/src/main.c b/src/main.c index a4a33e18e..0470d31b6 100644 --- a/src/main.c +++ b/src/main.c @@ -38,8 +38,10 @@ #include #include #include +#include #include #include +#include /* getsid() */ #include "lib/global.h" @@ -210,6 +212,35 @@ init_sigchld (void) } } +/* --------------------------------------------------------------------------------------------- */ +/** + * Check MC_SID to prevent running one mc from another. + * + * @return TRUE if no parent mc in our session was found, FALSE otherwise. + */ + +static gboolean +check_sid (void) +{ + pid_t my_sid, old_sid; + const char *sid_str; + + sid_str = getenv ("MC_SID"); + if (sid_str == NULL) + return TRUE; + + old_sid = (pid_t) strtol (sid_str, NULL, 0); + if (old_sid == 0) + return TRUE; + + my_sid = getsid (0); + if (my_sid == -1) + return TRUE; + + /* The parent mc is in a different session, it's OK */ + return (old_sid != my_sid); +} + /* --------------------------------------------------------------------------------------------- */ /*** public functions ****************************************************************************/ /* --------------------------------------------------------------------------------------------- */ @@ -222,6 +253,8 @@ main (int argc, char *argv[]) char *config_migrate_msg = NULL; int exit_code = EXIT_FAILURE; + mc_global.run_from_parent_mc = !check_sid (); + mc_global.timer = mc_timer_new (); /* We had LC_CTYPE before, LC_ALL includs LC_TYPE as well */ @@ -331,6 +364,10 @@ main (int argc, char *argv[]) handle_console (CONSOLE_INIT); #ifdef ENABLE_SUBSHELL + /* Disallow subshell when invoked as standalone viewer or editor from running mc */ + if (mc_global.mc_run_mode != MC_RUN_FULL && mc_global.run_from_parent_mc) + mc_global.tty.use_subshell = FALSE; + if (mc_global.tty.use_subshell) subshell_get_console_attributes (); #endif /* ENABLE_SUBSHELL */ @@ -370,32 +407,57 @@ main (int argc, char *argv[]) #ifdef ENABLE_SUBSHELL /* Done here to ensure that the subshell doesn't */ /* inherit the file descriptors opened below, etc */ + if (mc_global.tty.use_subshell && mc_global.run_from_parent_mc) + { + int r; + + r = query_dialog (_("Warning"), + _("GNU Midnight Commander\nis already running on this terminal.\n" + "Subshell support will be disabled."), + D_ERROR, 2, _("&OK"), _("&Quit")); + if (r == 0) + { + /* parent mc was found and the user wants to continue */ + ; + } + else + { + /* parent mc was found and the user wants to quit mc */ + mc_global.midnight_shutdown = TRUE; + } + + mc_global.tty.use_subshell = FALSE; + } + if (mc_global.tty.use_subshell) init_subshell (); #endif /* ENABLE_SUBSHELL */ - /* Also done after init_subshell, to save any shell init file messages */ - if (mc_global.tty.console_flag != '\0') - handle_console (CONSOLE_SAVE); - - if (mc_global.tty.alternate_plus_minus) - application_keypad_mode (); - - /* Done after subshell initialization to allow select and paste text by mouse - w/o Shift button in subshell in the native console */ - init_mouse (); - - /* Done after tty_enter_ca_mode (tty_init) because in VTE bracketed mode is - separate for the normal and alternate screens */ - enable_bracketed_paste (); - - /* subshell_prompt is NULL here */ - mc_prompt = (geteuid () == 0) ? "# " : "$ "; - - if (config_migrated) + if (!mc_global.midnight_shutdown) { - message (D_ERROR, _("Warning"), "%s", config_migrate_msg); - g_free (config_migrate_msg); + /* Also done after init_subshell, to save any shell init file messages */ + if (mc_global.tty.console_flag != '\0') + handle_console (CONSOLE_SAVE); + + if (mc_global.tty.alternate_plus_minus) + application_keypad_mode (); + + /* Done after subshell initialization to allow select and paste text by mouse + w/o Shift button in subshell in the native console */ + init_mouse (); + + /* Done after tty_enter_ca_mode (tty_init) because in VTE bracketed mode is + separate for the normal and alternate screens */ + enable_bracketed_paste (); + + /* subshell_prompt is NULL here */ + mc_prompt = (geteuid () == 0) ? "# " : "$ "; + + if (config_migrated) + { + message (D_ERROR, _("Warning"), "%s", config_migrate_msg); + g_free (config_migrate_msg); + } } /* Program main loop */ diff --git a/src/subshell/common.c b/src/subshell/common.c index 32444cfb0..d612d0abf 100644 --- a/src/subshell/common.c +++ b/src/subshell/common.c @@ -402,47 +402,6 @@ init_subshell_child (const char *pty_name) my_exit (FORK_FAILURE); } - -/* --------------------------------------------------------------------------------------------- */ -/** - * Check MC_SID to prevent running one mc from another. - * Return: - * 0 if no parent mc in our session was found, - * 1 if parent mc was found and the user wants to continue, - * 2 if parent mc was found and the user wants to quit mc. - */ - -static int -check_sid (void) -{ - pid_t my_sid, old_sid; - const char *sid_str; - int r; - - sid_str = getenv ("MC_SID"); - if (sid_str == NULL) - return 0; - - old_sid = (pid_t) strtol (sid_str, NULL, 0); - if (old_sid == 0) - return 0; - - my_sid = getsid (0); - if (my_sid == -1) - return 0; - - /* The parent mc is in a different session, it's OK */ - if (old_sid != my_sid) - return 0; - - r = query_dialog (_("Warning"), - _("GNU Midnight Commander is already\n" - "running on this terminal.\n" - "Subshell support will be disabled."), D_ERROR, 2, _("&OK"), _("&Quit")); - - return (r != 0) ? 2 : 1; -} - /* --------------------------------------------------------------------------------------------- */ static void @@ -1012,19 +971,6 @@ init_subshell (void) /* Must be considerably longer than BUF_SMALL (128) to support fancy shell prompts */ char precmd[BUF_MEDIUM]; - switch (check_sid ()) - { - case 1: - mc_global.tty.use_subshell = FALSE; - return; - case 2: - mc_global.tty.use_subshell = FALSE; - mc_global.midnight_shutdown = TRUE; - return; - default: - break; - } - /* Take the current (hopefully pristine) tty mode and make */ /* a raw mode based on it now, before we do anything else with it */ init_raw_mode ();