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
41abcbf706 (ticket #3380).

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Andrew Borodin 2020-02-01 11:49:47 +03:00
parent 647c431b27
commit c867b9d40e
4 changed files with 85 additions and 75 deletions

View File

@ -50,6 +50,7 @@
/* *INDENT-OFF* */ /* *INDENT-OFF* */
mc_global_t mc_global = { mc_global_t mc_global = {
.mc_run_mode = MC_RUN_FULL, .mc_run_mode = MC_RUN_FULL,
.run_from_parent_mc = FALSE,
.timer = NULL, .timer = NULL,
.midnight_shutdown = FALSE, .midnight_shutdown = FALSE,

View File

@ -166,6 +166,7 @@ typedef enum
typedef struct typedef struct
{ {
mc_run_mode_t mc_run_mode; mc_run_mode_t mc_run_mode;
gboolean run_from_parent_mc;
/* global timer */ /* global timer */
mc_timer_t *timer; mc_timer_t *timer;
/* Used so that widgets know if they are being destroyed or shut down */ /* Used so that widgets know if they are being destroyed or shut down */

View File

@ -38,8 +38,10 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/types.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <signal.h> #include <signal.h>
#include <unistd.h> /* getsid() */
#include "lib/global.h" #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 ****************************************************************************/ /*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
@ -222,6 +253,8 @@ main (int argc, char *argv[])
char *config_migrate_msg = NULL; char *config_migrate_msg = NULL;
int exit_code = EXIT_FAILURE; int exit_code = EXIT_FAILURE;
mc_global.run_from_parent_mc = !check_sid ();
mc_global.timer = mc_timer_new (); mc_global.timer = mc_timer_new ();
/* We had LC_CTYPE before, LC_ALL includs LC_TYPE as well */ /* 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); handle_console (CONSOLE_INIT);
#ifdef ENABLE_SUBSHELL #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) if (mc_global.tty.use_subshell)
subshell_get_console_attributes (); subshell_get_console_attributes ();
#endif /* ENABLE_SUBSHELL */ #endif /* ENABLE_SUBSHELL */
@ -370,32 +407,57 @@ main (int argc, char *argv[])
#ifdef ENABLE_SUBSHELL #ifdef ENABLE_SUBSHELL
/* Done here to ensure that the subshell doesn't */ /* Done here to ensure that the subshell doesn't */
/* inherit the file descriptors opened below, etc */ /* 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) if (mc_global.tty.use_subshell)
init_subshell (); init_subshell ();
#endif /* ENABLE_SUBSHELL */ #endif /* ENABLE_SUBSHELL */
/* Also done after init_subshell, to save any shell init file messages */ if (!mc_global.midnight_shutdown)
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); /* Also done after init_subshell, to save any shell init file messages */
g_free (config_migrate_msg); 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 */ /* Program main loop */

View File

@ -402,47 +402,6 @@ init_subshell_child (const char *pty_name)
my_exit (FORK_FAILURE); 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 static void
@ -1012,19 +971,6 @@ init_subshell (void)
/* Must be considerably longer than BUF_SMALL (128) to support fancy shell prompts */ /* Must be considerably longer than BUF_SMALL (128) to support fancy shell prompts */
char precmd[BUF_MEDIUM]; 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 */ /* Take the current (hopefully pristine) tty mode and make */
/* a raw mode based on it now, before we do anything else with it */ /* a raw mode based on it now, before we do anything else with it */
init_raw_mode (); init_raw_mode ();