From a7202a1fb911f72c309380b42c0ff995c05ba94c Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Mon, 29 Dec 2025 12:01:38 +0100 Subject: GTK: implemented --detach|-d option for detaching from controlling terminal This is useful to launch from a terminal without "blocking" this terminal. There are tools like nohup and daemonize (BSD) to do the same, but having it builtin is shorter to write. --- src/interface-gtk/interface.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/interface-gtk/interface.c b/src/interface-gtk/interface.c index a86e3fd..32370d9 100644 --- a/src/interface-gtk/interface.c +++ b/src/interface-gtk/interface.c @@ -19,6 +19,7 @@ #include "config.h" #endif +#include #include #include @@ -27,6 +28,7 @@ #include #ifdef G_OS_UNIX +#include #include #endif @@ -105,7 +107,7 @@ static struct { /* current document's name or empty string for "(Unnamed)" buffer */ teco_string_t info_current; - gboolean no_csd; + gboolean no_csd, detach; gint xembed_id; GtkWidget *info_bar_widget; @@ -132,6 +134,26 @@ static struct { void teco_interface_init(void) { +#ifdef G_OS_UNIX + if (teco_interface.detach) { + /* + * NOTE: There is also daemon() on BSD/Linux, + * but the following should be more portable. + */ + pid_t pid = fork(); + g_assert(pid >= 0); + if (pid != 0) + /* parent process */ + exit(EXIT_SUCCESS); + + setsid(); + + g_freopen("/dev/null", "r", stdin); + g_freopen("/dev/null", "a+", stdout); + g_freopen("/dev/null", "a+", stderr); + } +#endif + /* * gtk_init() is not necessary when using gtk_get_option_group(), * but this will open the default display. @@ -340,6 +362,11 @@ teco_interface_get_options(void) {"xembed", 0, G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_INT, &teco_interface.xembed_id, "Embed into an existing X11 Window.", "ID"}, +#endif +#ifdef G_OS_UNIX + {"detach", 'd', G_OPTION_FLAG_IN_MAIN, + G_OPTION_ARG_NONE, &teco_interface.detach, + "Detach from controlling terminal (daemonize).", NULL}, #endif {NULL} }; -- cgit v1.2.3