Signed-off-by: Mattias Andrée <[email protected]>
---
 Makefile |  2 ++
 setsid.1 | 23 +++++++++++++++++++++++
 setsid.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 74 insertions(+)
 create mode 100644 setsid.1
 create mode 100644 setsid.c

diff --git a/Makefile b/Makefile
index 59616a4..9de7455 100644
--- a/Makefile
+++ b/Makefile
@@ -73,6 +73,7 @@ BIN = \
        readahead         \
        respawn           \
        rmmod             \
+       setsid            \
        stat              \
        su                \
        swaplabel         \
@@ -107,6 +108,7 @@ MAN1 = \
        pidof.1             \
        ps.1                \
        respawn.1           \
+       setsid.1            \
        stat.1              \
        su.1                \
        truncate.1          \
diff --git a/setsid.1 b/setsid.1
new file mode 100644
index 0000000..b343a60
--- /dev/null
+++ b/setsid.1
@@ -0,0 +1,23 @@
+.Dd 2016-03-29
+.Dt SETSID 1
+.Os sbase
+.Sh NAME
+.Nm setsid
+.Nd run a command in a new session
+.Sh SYNOPSIS
+.Nm
+.Op Fl -c
+.Ar cmd
+.Op Ar arg ...
+.Sh DESCRIPTION
+.Nm
+runs
+.Ar cmd
+in a new session.
+.Sh OPTIONS
+.Bl -tag -width Ds
+.It Fl c
+Set the controlling terminal to stdin.
+.El
+.Sh SEE ALSO
+.Xr setsid 2
diff --git a/setsid.c b/setsid.c
new file mode 100644
index 0000000..c76b453
--- /dev/null
+++ b/setsid.c
@@ -0,0 +1,49 @@
+/* See LICENSE file for copyright and license details. */
+#include <sys/ioctl.h>
+
+#include <errno.h>
+#include <unistd.h>
+
+#include "util.h"
+
+static void
+usage(void)
+{
+       eprintf("usage: %s cmd [-c] [arg ...]\n", argv0);
+}
+
+int
+main(int argc, char *argv[])
+{
+       int savederrno;
+       int cflag = 0;
+
+       ARGBEGIN {
+       case 'c':
+               cflag = 1;
+       default:
+               usage();
+       } ARGEND
+
+       if (getpgrp() == getpid()) {
+               switch (fork()) {
+               case -1:
+                       eprintf("fork:");
+               case 0:
+                       break;
+               default:
+                       return 0;
+               }
+       }
+       if (setsid() < 0)
+               eprintf("setsid:");
+
+       if (cflag && ioctl(STDIN_FILENO, TIOCSCTTY, 1))
+               eprintf("TIOCSCTTY:");
+
+       execvp(argv[0], argv);
+       savederrno = errno;
+       weprintf("execvp %s:", argv[0]);
+
+       _exit(126 + (savederrno == ENOENT));
+}
-- 
2.7.4


Reply via email to