Re: [hackers] setsid: add optional -f to force fork()

From: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
Date: Tue, 14 Jul 2020 14:11:08 +0200

On Tue, Jul 14, 2020 at 12:15:20PM +0200, Mattias Andrée wrote:
> Hi,
>
> Is there any reason you would want to force it?
>
>

Yes, when getpgrp() != getpid().

I use this in my plumb script for my news program to setsid -f and open a link
my browser. Using the setsid -f option the browser is not closed when I close
my news program (due to it being in the same process group).

The -f option exists already in the util-linux(-ng) implementation:
https://www.man7.org/linux/man-pages/man1/setsid.1.html
https://git.kernel.org/pub/scm/utils/util-linux/util-linux.git/tree/sys-utils/setsid.c#n96

> Regards,
> Mattias Andrée
>
>
> On Tue, 14 Jul 2020 10:15:43 +0200
> Hiltjo Posthuma <hiltjo_AT_codemadness.org> wrote:
>
> > Hi,
> >
> > The below patch adds an -f flag to force fork(2)ing and creating a new process.
> >
> >
> > From a75ef384c11b64732dd6a3adc9249ba6beb8a67e Mon Sep 17 00:00:00 2001
> > From: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
> > Date: Tue, 14 Jul 2020 10:11:43 +0200
> > Subject: [PATCH] setsid: add optional -f to force fork()
> >
> > ---
> > setsid.1 | 3 ++-
> > setsid.c | 9 +++++++--
> > 2 files changed, 9 insertions(+), 3 deletions(-)
> >
> > diff --git a/setsid.1 b/setsid.1
> > index d43bcfc..4df6439 100644
> > --- a/setsid.1
> > +++ b/setsid.1
> > _AT_@ -1,4 +1,4 @@
> > -.Dd 2015-10-08
> > +.Dd 2020-07-14
> > .Dt SETSID 1
> > .Os sbase
> > .Sh NAME
> > _AT_@ -6,6 +6,7 @@
> > .Nd run a command in a new session
> > .Sh SYNOPSIS
> > .Nm
> > +.Op Fl f
> > .Ar cmd
> > .Op Ar arg ...
> > .Sh DESCRIPTION
> > diff --git a/setsid.c b/setsid.c
> > index 28d3442..3355b40 100644
> > --- a/setsid.c
> > +++ b/setsid.c
> > _AT_@ -4,10 +4,12 @@
> >
> > #include "util.h"
> >
> > +static int fflag = 0;
> > +
> > static void
> > usage(void)
> > {
> > - eprintf("usage: %s cmd [arg ...]\n", argv0);
> > + eprintf("usage: %s cmd [-f] [arg ...]\n", argv0);
> > }
> >
> > int
> > _AT_@ -16,6 +18,9 @@ main(int argc, char *argv[])
> > int savederrno;
> >
> > ARGBEGIN {
> > + case 'f':
> > + fflag = 1;
> > + break;
> > default:
> > usage();
> > } ARGEND
> > _AT_@ -23,7 +28,7 @@ main(int argc, char *argv[])
> > if (!argc)
> > usage();
> >
> > - if (getpgrp() == getpid()) {
> > + if (fflag || getpgrp() == getpid()) {
> > switch (fork()) {
> > case -1:
> > eprintf("fork:");
>
>

-- 
Kind regards,
Hiltjo
Received on Tue Jul 14 2020 - 14:11:08 CEST

This archive was generated by hypermail 2.3.0 : Tue Jul 14 2020 - 14:12:33 CEST