From 861deaf09d70ff8065addd579722b0ff8524152b Mon Sep 17 00:00:00 2001 From: Pavel Roskin Date: Wed, 5 Mar 2003 20:18:11 +0000 Subject: [PATCH] * subshell.c (init_subshell_child): Set environment variable MC_SID to the session number of the subshell. (check_sid): Check that we are not running mc already in the same session. * main.c (main): Don't run do_nc() if shutdown was requested. --- src/ChangeLog | 8 ++++++ src/main.c | 3 ++- src/subshell.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 1 deletion(-) diff --git a/src/ChangeLog b/src/ChangeLog index be13a727d..1a2e9ae84 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,11 @@ +2003-03-05 Pavel Roskin + + * subshell.c (init_subshell_child): Set environment variable + MC_SID to the session number of the subshell. + (check_sid): Check that we are not running mc already in the + same session. + * main.c (main): Don't run do_nc() if shutdown was requested. + 2003-03-05 Andrew V. Samoilov * cons.handler.c (set_attr) [__FreeBSD__]: Eliminate cmd array. diff --git a/src/main.c b/src/main.c index cd1d7234a..14617467c 100644 --- a/src/main.c +++ b/src/main.c @@ -2517,7 +2517,8 @@ main (int argc, char *argv[]) prompt = (geteuid () == 0) ? "# " : "$ "; /* Program main loop */ - do_nc (); + if (!midnight_shutdown) + do_nc (); /* Save the tree store */ tree_store_save (); diff --git a/src/subshell.c b/src/subshell.c index 498aa411b..5e7396e9c 100644 --- a/src/subshell.c +++ b/src/subshell.c @@ -189,6 +189,9 @@ static void init_subshell_child (const char *pty_name) { int pty_slave; char *init_file = NULL; +#ifdef HAVE_GETSID + pid_t mc_sid; +#endif /* HAVE_GETSID */ setsid (); /* Get a fresh terminal session */ @@ -244,6 +247,16 @@ static void init_subshell_child (const char *pty_name) /* and the user's startup file may do a `cd' command anyway */ chdir (home_dir); /* FIXME? What about when we re-run the subshell? */ +#ifdef HAVE_GETSID + /* Set MC_SID to prevent running one mc from another */ + mc_sid = getsid (0); + if (mc_sid != -1) { + char sid_str[BUF_SMALL]; + g_snprintf (sid_str, sizeof (sid_str), "MC_SID=%ld", (long) mc_sid); + putenv (sid_str); + } +#endif /* HAVE_GETSID */ + switch (subshell_type) { case BASH: @@ -318,6 +331,52 @@ static void init_subshell_child (const char *pty_name) /* }}} */ } + +#ifdef HAVE_GETSID +/* + * 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 () +{ + pid_t my_sid, old_sid; + char *sid_str; + int r; + + sid_str = getenv ("MC_SID"); + if (!sid_str) + return 0; + + old_sid = (pid_t) strtol (sid_str, NULL, 0); + if (!old_sid) + 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")); + if (r != 0) { + return 2; + } + + return 1; +} +#endif /* HAVE_GETSID */ + + /* {{{ init_subshell */ /* @@ -337,6 +396,15 @@ void init_subshell (void) static char pty_name[BUF_SMALL]; int pty_slave = -1; + switch (check_sid ()) { + case 1: + use_subshell = FALSE; + return; + case 2: + use_subshell = FALSE; + midnight_shutdown = 1; + return; + } #ifdef SYNC_PTY_SIDES /* Used to wait for a SIGUSR1 signal from the subprocess */