]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
setsid: new option --fork
authorKarel Zak <kzak@redhat.com>
Wed, 8 Nov 2017 10:38:26 +0000 (11:38 +0100)
committerKarel Zak <kzak@redhat.com>
Wed, 8 Nov 2017 10:38:26 +0000 (11:38 +0100)
Let's make semantic more predictable.

Addresses: https://github.com/karelzak/util-linux/issues/518
Signed-off-by: Karel Zak <kzak@redhat.com>
sys-utils/setsid.1
sys-utils/setsid.c

index 61b13ce1db7ec603e77874d5e97f1d8397cad7d4..64f0555716450eae41ba66a56011e562aa6d0ef7 100644 (file)
@@ -12,13 +12,17 @@ setsid \- run a program in a new session
 .B setsid
 runs a program in a new session. The command calls
 .BR fork (2)
-if already a process group leader. Otherwise, it executes a program in the
-current process.
+if already a process group leader.  Otherwise, it executes a program in the
+current process.  This default behavior is possible to override by
+the \fB\-\-fork\fR option.
 .SH OPTIONS
 .TP
 .BR \-c , " \-\-ctty"
 Set the controlling terminal to the current one.
 .TP
+.BR \-f , " \-\-fork"
+Always create a new process.
+.TP
 .BR \-w , " \-\-wait"
 Wait for the execution of the program to end, and return the exit value of
 this program as the return value of
index 0b139523220088668064b387ee6494455f8778e3..2991da058304805757bd84413e4f7e1076337609 100644 (file)
@@ -38,6 +38,7 @@ static void __attribute__((__noreturn__)) usage(void)
 
        fputs(USAGE_OPTIONS, out);
        fputs(_(" -c, --ctty     set the controlling terminal to the current one\n"), out);
+       fputs(_(" -f, --fork     always fork\n"), out);
        fputs(_(" -w, --wait     wait program to exit, and use the same return\n"), out);
 
        printf(USAGE_HELP_OPTIONS(16));
@@ -48,13 +49,14 @@ static void __attribute__((__noreturn__)) usage(void)
 
 int main(int argc, char **argv)
 {
-       int ch;
+       int ch, forcefork = 0;
        int ctty = 0;
        pid_t pid;
        int status = 0;
 
        static const struct option longopts[] = {
                {"ctty", no_argument, NULL, 'c'},
+               {"fork", no_argument, NULL, 'f'},
                {"wait", no_argument, NULL, 'w'},
                {"version", no_argument, NULL, 'V'},
                {"help", no_argument, NULL, 'h'},
@@ -66,7 +68,7 @@ int main(int argc, char **argv)
        textdomain(PACKAGE);
        atexit(close_stdout);
 
-       while ((ch = getopt_long(argc, argv, "+Vhcw", longopts, NULL)) != -1)
+       while ((ch = getopt_long(argc, argv, "+Vhcfw", longopts, NULL)) != -1)
                switch (ch) {
                case 'V':
                        printf(UTIL_LINUX_VERSION);
@@ -74,6 +76,9 @@ int main(int argc, char **argv)
                case 'c':
                        ctty=1;
                        break;
+               case 'f':
+                       forcefork = 1;
+                       break;
                case 'w':
                        status = 1;
                        break;
@@ -88,7 +93,7 @@ int main(int argc, char **argv)
                errtryhelp(EXIT_FAILURE);
        }
 
-       if (getpgrp() == getpid()) {
+       if (forcefork || getpgrp() == getpid()) {
                pid = fork();
                switch (pid) {
                case -1: