From 7ff541040edc3b3f8a25bb69e04ecb57cba12954 Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Sat, 24 Jan 2026 16:29:23 +0100 Subject: GTK: fixed --detach and support stdout/stderr redirections * We fork after command line arguments have been parsed, which is after gtk_get_option_group() has been called. This means that GTK was already initialized and it wasn't safe to continue after forking. * As a workaround, we now re-exec with the original argv array, so GTK can be properly reinitialized. Since we did not remove `--detach` from argv (and that would be nontrivial), it would fork again endlessly, so we use an environment variable $__SCITECO_DETACHED to guard against recursive forks. * Also, do not close stdin/stdout/stderr if has been redirected to a file, so you can now e.g. call `gsciteco -d >some-file`. * This was broken since v2.5.0. --- src/interface-gtk/interface.c | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) (limited to 'src/interface-gtk') diff --git a/src/interface-gtk/interface.c b/src/interface-gtk/interface.c index ace3a2f..08ccf5d 100644 --- a/src/interface-gtk/interface.c +++ b/src/interface-gtk/interface.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include @@ -132,10 +133,10 @@ static struct { } teco_interface; void -teco_interface_init(void) +teco_interface_init(gint argc, gchar **argv) { #ifdef G_OS_UNIX - if (teco_interface.detach) { + if (teco_interface.detach && !g_getenv("__SCITECO_DETACHED")) { /* * NOTE: There is also daemon() on BSD/Linux, * but the following should be more portable. @@ -148,9 +149,29 @@ teco_interface_init(void) setsid(); - g_freopen("/dev/null", "r", stdin); - g_freopen("/dev/null", "a+", stdout); - g_freopen("/dev/null", "a+", stderr); + if (isatty(0)) { + G_GNUC_UNUSED FILE *stdin_new = g_freopen("/dev/null", "r", stdin); + g_assert(stdin_new != NULL); + } + if (isatty(1)) { + G_GNUC_UNUSED FILE *stdout_new = g_freopen("/dev/null", "a+", stdout); + g_assert(stdout_new != NULL); + } + if (isatty(2)) { + G_GNUC_UNUSED FILE *stderr_new = g_freopen("/dev/null", "a+", stderr); + g_assert(stderr_new != NULL); + } + + /* + * gtk_get_option_group() already initialized GTK and even though the + * display is not yet opened, it's unsafe to continue. + * Instead, we re-exec already in the child process. + * We cannot easily remove --detach from argv, but still guard against + * recursive forks by using an environment variable. + */ + g_setenv("__SCITECO_DETACHED", "1", TRUE); + execv(argv[0], argv); + g_assert_not_reached(); } #endif -- cgit v1.2.3