From 3b2670bd26b21a6e93617e6838145372546dd903 Mon Sep 17 00:00:00 2001 From: Chuck Zmudzinski Date: Tue, 31 Oct 2023 10:44:16 -0400 Subject: [PATCH] Add option to run vncsession without forking and detaching Option is -D, which is what sshd uses for the same option. Also add description of the new option to the vncsession man page. Tested on Void Linux using the new option, also tested on Fedora without the new option. Resolves #1649 --- unix/vncserver/vncsession.c | 41 +++++++++++++++++++++++++------- unix/vncserver/vncsession.man.in | 12 ++++++++++ 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/unix/vncserver/vncsession.c b/unix/vncserver/vncsession.c index c722922183..1c6c3310f2 100644 --- a/unix/vncserver/vncsession.c +++ b/unix/vncserver/vncsession.c @@ -503,6 +503,14 @@ run_script(const char *username, const char *display, char **envp) _exit(EX_OSERR); } +static void +usage(void) +{ + fprintf(stderr, "Syntax:\n"); + fprintf(stderr, " vncsession [-D] \n"); + _exit(EX_USAGE); +} + int main(int argc, char **argv) { @@ -511,14 +519,26 @@ main(int argc, char **argv) const char *username, *display; - if ((argc != 3) || (argv[2][0] != ':')) { - fprintf(stderr, "Syntax:\n"); - fprintf(stderr, " %s \n", argv[0]); - return EX_USAGE; + int opt, forking = 1; + if (argc == 4) { + if (argv[3][0] != ':') { + usage(); + } + while ((opt = getopt(argc, argv, "D")) != -1) { + switch (opt) { + case 'D': + forking = 0; + break; + default: + usage(); + } + } + } else if ((argc != 3) || (argv[2][0] != ':')) { + usage(); } - username = argv[1]; - display = argv[2]; + username = argv[argc - 2]; + display = argv[argc - 1]; if (geteuid() != 0) { fprintf(stderr, "This program needs to be run as root!\n"); @@ -534,8 +554,10 @@ main(int argc, char **argv) return EX_OSERR; } - if (begin_daemon() == -1) - return EX_OSERR; + if (forking) { + if (begin_daemon() == -1) + return EX_OSERR; + } openlog("vncsession", LOG_PID, LOG_AUTH); @@ -586,7 +608,8 @@ main(int argc, char **argv) fclose(f); } - finish_daemon(); + if (forking) + finish_daemon(); while (1) { int status; diff --git a/unix/vncserver/vncsession.man.in b/unix/vncserver/vncsession.man.in index 4efad41bc5..c2a0e26116 100644 --- a/unix/vncserver/vncsession.man.in +++ b/unix/vncserver/vncsession.man.in @@ -3,6 +3,7 @@ vncsession \- start a VNC server .SH SYNOPSIS .B vncsession +.RI [-D] .RI < username > .RI <: display# > .SH DESCRIPTION @@ -16,6 +17,17 @@ appropriate options and starts a window manager on the VNC desktop. is rarely called directly and is normally started by the system service manager. +.SH -D OPTION +.B vncsession +by default forks and detaches so it normally is a systemd Type=forking service. +If the -D option is used, it does not fork and detach. This option is provided +for use with other types of systemd services such as Type=simple and Type=exec +and for use with non-systemd system service managers that are not compatible +with forking services. This option is not intended for debugging in a login +shell from a terminal or for running +.B vncsession +from a terminal as an ordinary user. + .SH FILES Several VNC-related files are found in the directory $HOME/.vnc: .TP