Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

BOFH excuse #48: bad ether in the cables


comp / comp.unix.programmer / Re: Why does getppid() still return old parent pid after setsid()?

SubjectAuthor
* Why does getppid() still return old parent pid after setsid()?Muttley
+* Re: Why does getppid() still return old parent pid after setsid()?Lew Pitcher
|`- Re: Why does getppid() still return old parent pid after setsid()?Lawrence D'Oliveiro
+- Re: Why does getppid() still return old parent pid after setsid()?Scott Lurndal
+* Re: Why does getppid() still return old parent pid after setsid()?Kenny McCormack
|+* Re: Why does getppid() still return old parent pid after setsid()?Lew Pitcher
||`- Re: Why does getppid() still return old parent pid after setsid()?Lew Pitcher
|+* Re: Why does getppid() still return old parent pid after setsid()?Muttley
||+* Re: Why does getppid() still return old parent pid after setsid()?Lew Pitcher
|||`- Re: Why does getppid() still return old parent pid after setsid()?Scott Lurndal
||`* Re: Why does getppid() still return old parent pid after setsid()?Lawrence D'Oliveiro
|| `* Re: Why does getppid() still return old parent pid after setsid()?Kenny McCormack
||  `* Re: Why does getppid() still return old parent pid after setsid()?Richard Harnden
||   `* Re: Why does getppid() still return old parent pid after setsid()?Lew Pitcher
||    `- Re: Why does getppid() still return old parent pid after setsid()?Richard Harnden
|`* Re: Why does getppid() still return old parent pid after setsid()?Lew Pitcher
| `- Re: Why does getppid() still return old parent pid after setsid()?Kenny McCormack
+- Re: Why does getppid() still return old parent pid after setsid()?Rainer Weikusat
+* Re: Why does getppid() still return old parent pid after setsid()?Geoff Clare
|`- Re: Why does getppid() still return old parent pid after setsid()?Muttley
`* Re: Why does getppid() still return old parent pid after setsid()?Kaz Kylheku
 `- Re: Why does getppid() still return old parent pid after setsid()?Muttley

1
Subject: Why does getppid() still return old parent pid after setsid()?
From: Muttley@DastartdlyHQ.org
Newsgroups: comp.unix.programmer
Organization: A noiseless patient Spider
Date: Fri, 8 Nov 2024 14:51 UTC
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Muttley@DastartdlyHQ.org
Newsgroups: comp.unix.programmer
Subject: Why does getppid() still return old parent pid after setsid()?
Date: Fri, 8 Nov 2024 14:51:13 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 27
Message-ID: <vgl8h1$385vs$1@dont-email.me>
Injection-Date: Fri, 08 Nov 2024 15:51:15 +0100 (CET)
Injection-Info: dont-email.me; posting-host="8833238c505f801787634014ea3badaf";
logging-data="3414012"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19fHy8jfld0My10q11t5op6"
Cancel-Lock: sha1:M3oXk94EMqj8gQj2fxxVvQqEJRk=
View all headers

I've tried this code on both MacOS and Linux yet the child process getppid()
still returns its original parent process instead of "1" for init which is
what I'd expect. Isn't setsid() supposed to completely detach the child or
have I misunderstood?

int main()
{ printf("Parent pid %d\n",getpid());
switch(fork())
{
case -1:
perror("fork");
return 0;
case 0:
// child
break;
default:
// Parent
sleep(1);
return 0;
}

setsid();
printf("Child parent = %d\n",getppid());
return 0;
}

Subject: Re: Why does getppid() still return old parent pid after setsid()?
From: Lew Pitcher
Newsgroups: comp.unix.programmer
Organization: A noiseless patient Spider
Date: Fri, 8 Nov 2024 15:30 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: lew.pitcher@digitalfreehold.ca (Lew Pitcher)
Newsgroups: comp.unix.programmer
Subject: Re: Why does getppid() still return old parent pid after setsid()?
Date: Fri, 8 Nov 2024 15:30:21 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 82
Message-ID: <vglaqd$37mab$1@dont-email.me>
References: <vgl8h1$385vs$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 08 Nov 2024 16:30:21 +0100 (CET)
Injection-Info: dont-email.me; posting-host="3da3d88374dd57688ec2b2f6f936ebf3";
logging-data="3397963"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Gvh/CBFUXGUTUqR7D/AnINkS/cr4cOC4="
User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508
git://git.gnome.org/pan2)
Cancel-Lock: sha1:fYFAMp9ngzy8CuRXmfvviGz4F6Y=
View all headers

On Fri, 08 Nov 2024 14:51:13 +0000, Muttley wrote:

> I've tried this code on both MacOS and Linux yet the child process getppid()
> still returns its original parent process instead of "1" for init which is
> what I'd expect. Isn't setsid() supposed to completely detach the child or
> have I misunderstood?

I cant claim to be an expert on this, but my understanding of the situation
is this:

Each process maintains a /number/ of relationships with other processes,
/one/ of which is ppid (parent PID). setsid() only affects the "process group"
and "session" relationships, and not the "parent" relationship.

On my system, credentials(7) says:
Sessions and process groups are abstractions devised to support shell
job control. A process group (sometimes called a "job") is a collec-
tion of processes that share the same process group ID; the shell cre-
ates a new process group for the process(es) used to execute single
command or pipeline (e.g., the two processes created to execute the
command "ls | wc" are placed in the same process group). A process's
group membership can be set using setpgid(2). The process whose
process ID is the same as its process group ID is the process group
leader for that group.

A session is a collection of processes that share the same session ID.
All of the members of a process group also have the same session ID
(i.e., all of the members of a process group always belong to the same
session, so that sessions and process groups form a strict two-level
hierarchy of processes.) A new session is created when a process calls
setsid(2), which creates a new session whose session ID is the same as
the PID of the process that called setsid(2). The creator of the ses-
sion is called the session leader.

All of the processes in a session share a controlling terminal. The
controlling terminal is established when the session leader first opens
a terminal (unless the O_NOCTTY flag is specified when calling
open(2)). A terminal may be the controlling terminal of at most one
session.

At most one of the jobs in a session may be the foreground job; other
jobs in the session are background jobs. Only the foreground job may
read from the terminal; when a process in the background attempts to
read from the terminal, its process group is sent a SIGTTIN signal,
which suspends the job. If the TOSTOP flag has been set for the termi-
nal (see termios(3)), then only the foreground job may write to the
terminal; writes from background job cause a SIGTTOU signal to be gen-
erated, which suspends the job. When terminal keys that generate a
signal (such as the interrupt key, normally control-C) are pressed, the
signal is sent to the processes in the foreground job.

So, while setsid(2) "creates a session and sets the process group ID" for a
process, it does not change the process' parent PID.

> int main()
> {
> printf("Parent pid %d\n",getpid());
> switch(fork())
> {
> case -1:
> perror("fork");
> return 0;
> case 0:
> // child
> break;
> default:
> // Parent
> sleep(1);
> return 0;
> }
>
> setsid();
> printf("Child parent = %d\n",getppid());
> return 0;
> }

HTH

--
Lew Pitcher
"In Skills We Trust"

Subject: Re: Why does getppid() still return old parent pid after setsid()?
From: Scott Lurndal
Newsgroups: comp.unix.programmer
Organization: UsenetServer - www.usenetserver.com
Date: Fri, 8 Nov 2024 17:26 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!feeder2.eternal-september.org!border-1.nntp.ord.giganews.com!border-2.nntp.ord.giganews.com!nntp.giganews.com!news-out.netnews.com!s1-1.netnews.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx11.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: scott@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: Why does getppid() still return old parent pid after setsid()?
Newsgroups: comp.unix.programmer
References: <vgl8h1$385vs$1@dont-email.me>
Lines: 68
Message-ID: <lRrXO.382322$kxD8.24075@fx11.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Fri, 08 Nov 2024 17:26:41 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Fri, 08 Nov 2024 17:26:41 GMT
X-Received-Bytes: 2615
X-Original-Bytes: 2564
View all headers

Muttley@DastartdlyHQ.org writes:
>I've tried this code on both MacOS and Linux yet the child process getppid()
>still returns its original parent process instead of "1" for init which is
>what I'd expect. Isn't setsid() supposed to completely detach the child or
>have I misunderstood?

You only want the child to issue setsid.

>
>int main()
>{
> printf("Parent pid %d\n",getpid());
> switch(fork())
> {
> case -1:
> perror("fork");
> return 0;
> case 0:

setsid();

> // child
> break;
> default:
> // Parent
> sleep(1);
> return 0;
> }
>
> printf("Child parent = %d\n",getppid());
> return 0;
>}
>

pid = fork();
if (pid == (pid_t)-1) {
pfmt(stderr, MM_ERROR, failed, "fork", Strerror(errno));
goto dead;
}
if (pid == 0) {
sigchild();
#ifdef SIGTSTP
sigset(SIGTSTP, SIG_IGN);
sigset(SIGTTIN, SIG_IGN);
sigset(SIGTTOU, SIG_IGN);
#endif
signal(SIGHUP,SIG_IGN);
signal(SIGINT,SIG_IGN);
signal(SIGQUIT,SIG_IGN);
#ifdef preSVr4 /* block all tty generated signals from affecting our mailer */
setpgrp();
#else
setsid();
#endif
s = fileno(mtf);
for (i = 3; i < 32; i++)
if (i != s)
close(i);
close(0);
dup(s);
close(s);
if ((deliver = value("sendmail")) == NOSTR)
deliver = MAIL;
execvp(safeexpand(deliver), namelist);
pfmt(stderr, MM_ERROR, errmsg, deliver, Strerror(errno));
fflush(stderr);
_exit(1);
}

Subject: Re: Why does getppid() still return old parent pid after setsid()?
From: Lawrence D'Oliv
Newsgroups: comp.unix.programmer
Organization: A noiseless patient Spider
Date: Sat, 9 Nov 2024 00:07 UTC
References: 1 2
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ldo@nz.invalid (Lawrence D'Oliveiro)
Newsgroups: comp.unix.programmer
Subject: Re: Why does getppid() still return old parent pid after setsid()?
Date: Sat, 9 Nov 2024 00:07:27 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 13
Message-ID: <vgm93v$3dkuq$4@dont-email.me>
References: <vgl8h1$385vs$1@dont-email.me> <vglaqd$37mab$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 09 Nov 2024 01:07:27 +0100 (CET)
Injection-Info: dont-email.me; posting-host="2144ffc59d8cce2fe73ad947016f3118";
logging-data="3593178"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+P6X14fE9cvxy9ECb0UU4k"
User-Agent: Pan/0.160 (Toresk; )
Cancel-Lock: sha1:IIXGThANKDe/442L/sY6xhV5kCs=
View all headers

On Fri, 8 Nov 2024 15:30:21 -0000 (UTC), Lew Pitcher wrote:

> So, while setsid(2) "creates a session and sets the process group ID"
> for a process, it does not change the process' parent PID.

It is possible for the parent process to terminate before the child.
The child is then said to be “orphaned”. From this point, normally
getppid() will return 1, the PID of the init process.

However, on Linux, it is possible to specify an alternative “child
subreaper” process, via prctl()
<https://manpages.debian.org/bookworm/manpages-dev/prctl.2.en.html>.
getppid() will then return the ID of this process.

Subject: Re: Why does getppid() still return old parent pid after setsid()?
From: Kenny McCormack
Newsgroups: comp.unix.programmer
Organization: The official candy of the new Millennium
Date: Sat, 9 Nov 2024 09:25 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!feeder2.eternal-september.org!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gazelle@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.unix.programmer
Subject: Re: Why does getppid() still return old parent pid after setsid()?
Date: Sat, 9 Nov 2024 09:25:22 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <vgn9q2$8mls$1@news.xmission.com>
References: <vgl8h1$385vs$1@dont-email.me>
Injection-Date: Sat, 9 Nov 2024 09:25:22 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="285372"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
View all headers

In article <vgl8h1$385vs$1@dont-email.me>, <Muttley@DastartdlyHQ.org> wrote:
>I've tried this code on both MacOS and Linux yet the child process getppid()
>still returns its original parent process instead of "1" for init which is
>what I'd expect. Isn't setsid() supposed to completely detach the child or
>have I misunderstood?

I suppose the obvious question at this point is: Why do you care? What's
the underlying issue?

I'm not saying that to be snarky (seriously, I'm not!). I am genuinely
curious as to why you are curious. If this is a solely a question of
academic curiosity, then the answer is "because that's the way it is". As
far as I know, in classic Unix, the only way for a process to get
"re-parented" is to have the parent die. Of course, as others have noted,
under Linux, there is also the re-parenting "prctl".

The usual method for ensuring that a process *does* have getppid() == 1 is
the classic "double fork". The parent forks(), the child then forks() and
exits. The grandchild then does the work (either directly or via exec()).

BTW, note that "total isolation" (in your words, "completely detach") is
actually somewhat difficult to achieve and care must be taken to do it
right. Among other things, if you have any terminals open (the usual
stdin/stdout/stderr), then you still have a connection to those terminals
and may receive signals therefrom (e.g., if a user hits ^C on that terminal
or if the terminal is terminated). "sshfs", in particular, is vulnerable
to this problem; unless you take special care, sshfs will terminate if the
shell (terminal window) from which it was launched is exited.

In fact, it is somewhat surprising how many common ordinary day-to-day
programs there are that still do things like this. And by "do things like
this", I mean fail to daemonize correctly.

--
I shot a man on Fifth Aveneue, just to see him die.

Subject: Re: Why does getppid() still return old parent pid after setsid()?
From: Lew Pitcher
Newsgroups: comp.unix.programmer
Organization: A noiseless patient Spider
Date: Sat, 9 Nov 2024 15:49 UTC
References: 1 2
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: lew.pitcher@digitalfreehold.ca (Lew Pitcher)
Newsgroups: comp.unix.programmer
Subject: Re: Why does getppid() still return old parent pid after setsid()?
Date: Sat, 9 Nov 2024 15:49:33 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 24
Message-ID: <vgo0ad$3r9kp$1@dont-email.me>
References: <vgl8h1$385vs$1@dont-email.me>
<vgn9q2$8mls$1@news.xmission.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 09 Nov 2024 16:49:34 +0100 (CET)
Injection-Info: dont-email.me; posting-host="27a486817a06fb74ac8e3d125aa4d1f7";
logging-data="4040345"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19izdmt4guD+XWGvrOhbIrU9NY3UOSf8iI="
User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508
git://git.gnome.org/pan2)
Cancel-Lock: sha1:ZwGKExDvFKSA5TcQ+1yZkMN/8Ag=
View all headers

On Sat, 09 Nov 2024 09:25:22 +0000, Kenny McCormack wrote:

> In article <vgl8h1$385vs$1@dont-email.me>, <Muttley@DastartdlyHQ.org> wrote:
>>I've tried this code on both MacOS and Linux yet the child process getppid()
>>still returns its original parent process instead of "1" for init which is
>>what I'd expect. Isn't setsid() supposed to completely detach the child or
>>have I misunderstood?
[snip]
> The usual method for ensuring that a process *does* have getppid() == 1 is
> the classic "double fork". The parent forks(), the child then forks() and
> exits. The grandchild then does the work (either directly or via exec()).

Some systems (notably BSD) provide a daemon(3) function that does this. GNU
glibc provides this function by default (feature test macro _DEFAULT_SOURCE)
since glibc 2.21. W. Richard Stevens discusses the details in chapter 13
of "Advanced Programming in the Unix Environment" (aka the "APUE" book).

[snip]

HTH
--
Lew Pitcher
"In Skills We Trust"

Subject: Re: Why does getppid() still return old parent pid after setsid()?
From: Lew Pitcher
Newsgroups: comp.unix.programmer
Organization: A noiseless patient Spider
Date: Sat, 9 Nov 2024 19:01 UTC
References: 1 2 3
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: lew.pitcher@digitalfreehold.ca (Lew Pitcher)
Newsgroups: comp.unix.programmer
Subject: Re: Why does getppid() still return old parent pid after setsid()?
Date: Sat, 9 Nov 2024 19:01:02 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 39
Message-ID: <vgobhd$3r9kp$2@dont-email.me>
References: <vgl8h1$385vs$1@dont-email.me>
<vgn9q2$8mls$1@news.xmission.com> <vgo0ad$3r9kp$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 09 Nov 2024 20:01:02 +0100 (CET)
Injection-Info: dont-email.me; posting-host="27a486817a06fb74ac8e3d125aa4d1f7";
logging-data="4040345"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1977zr3cxTfaUKwEmusl9+WsYAVICqVNxU="
User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508
git://git.gnome.org/pan2)
Cancel-Lock: sha1:WvtcF+cURrgzxC6FVKx4AB8KRU4=
View all headers

On Sat, 09 Nov 2024 15:49:33 +0000, Lew Pitcher wrote:

> On Sat, 09 Nov 2024 09:25:22 +0000, Kenny McCormack wrote:
>
>> In article <vgl8h1$385vs$1@dont-email.me>, <Muttley@DastartdlyHQ.org> wrote:
>>>I've tried this code on both MacOS and Linux yet the child process getppid()
>>>still returns its original parent process instead of "1" for init which is
>>>what I'd expect. Isn't setsid() supposed to completely detach the child or
>>>have I misunderstood?
> [snip]
>> The usual method for ensuring that a process *does* have getppid() == 1 is
>> the classic "double fork". The parent forks(), the child then forks() and
>> exits. The grandchild then does the work (either directly or via exec()).
>
> Some systems (notably BSD) provide a daemon(3) function that does this.

I have to correct myself here. According to the BUGS section of the
glibc daemon(3) manpage:
The GNU C library implementation of this function was taken from BSD,
and does not employ the double-fork technique (i.e., fork(2), set-
sid(2), fork(2)) that is necessary to ensure that the resulting daemon
process is not a session leader.

> GNU
> glibc provides this function by default (feature test macro _DEFAULT_SOURCE)
> since glibc 2.21. W. Richard Stevens discusses the details in chapter 13
> of "Advanced Programming in the Unix Environment" (aka the "APUE" book).
>
>
> [snip]
>
> HTH

--
Lew Pitcher
"In Skills We Trust"

Subject: Re: Why does getppid() still return old parent pid after setsid()?
From: Muttley@DastartdlyHQ.org
Newsgroups: comp.unix.programmer
Organization: A noiseless patient Spider
Date: Mon, 11 Nov 2024 10:02 UTC
References: 1 2
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Muttley@DastartdlyHQ.org
Newsgroups: comp.unix.programmer
Subject: Re: Why does getppid() still return old parent pid after setsid()?
Date: Mon, 11 Nov 2024 10:02:45 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 23
Message-ID: <vgsko5$u7pg$1@dont-email.me>
References: <vgl8h1$385vs$1@dont-email.me> <vgn9q2$8mls$1@news.xmission.com>
Injection-Date: Mon, 11 Nov 2024 11:02:46 +0100 (CET)
Injection-Info: dont-email.me; posting-host="dce9ef009493e7bff49a3fdd7efd443b";
logging-data="991024"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/tFhw5w+BpAnq/ZRTFIHXm"
Cancel-Lock: sha1:agQqOd2gYNJHQPKyaurYzXY9W1Y=
View all headers

On Sat, 9 Nov 2024 09:25:22 -0000 (UTC)
gazelle@shell.xmission.com (Kenny McCormack) boring babbled:
>In article <vgl8h1$385vs$1@dont-email.me>, <Muttley@DastartdlyHQ.org> wrote:
>>I've tried this code on both MacOS and Linux yet the child process getppid()
>>still returns its original parent process instead of "1" for init which is
>>what I'd expect. Isn't setsid() supposed to completely detach the child or
>>have I misunderstood?
>
>I suppose the obvious question at this point is: Why do you care? What's
>the underlying issue?

Just curiousity mainly. I did setsid() and ignored SIGCLD so I wouldn't get
zombie processes but noticed that getppid() didn't return 1. I'd forgotten
about double fork to be fair - I need to re-read Stevens at some point! - but
can't help thinking they should be a single shot OS call to completely
seperate the child from parent. Yes, daemon() will do it as a single function
call but I imagine that just does a double fork underneath anyway.

>under Linux, there is also the re-parenting "prctl".

I didn't know that, thanks.

Subject: Re: Why does getppid() still return old parent pid after setsid()?
From: Lew Pitcher
Newsgroups: comp.unix.programmer
Organization: A noiseless patient Spider
Date: Mon, 11 Nov 2024 15:20 UTC
References: 1 2 3
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: lew.pitcher@digitalfreehold.ca (Lew Pitcher)
Newsgroups: comp.unix.programmer
Subject: Re: Why does getppid() still return old parent pid after setsid()?
Date: Mon, 11 Nov 2024 15:20:25 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 36
Message-ID: <vgt7bp$11fog$1@dont-email.me>
References: <vgl8h1$385vs$1@dont-email.me>
<vgn9q2$8mls$1@news.xmission.com> <vgsko5$u7pg$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 11 Nov 2024 16:20:25 +0100 (CET)
Injection-Info: dont-email.me; posting-host="090d5a8025b43e616f11aef0313ce297";
logging-data="1097488"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18uNSLgKPyx4KlSa5P4mIdXykGwumwWtko="
User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508
git://git.gnome.org/pan2)
Cancel-Lock: sha1:P2t5to52yxQLitBNlxCGKyZczG8=
View all headers

On Mon, 11 Nov 2024 10:02:45 +0000, Muttley wrote:

> On Sat, 9 Nov 2024 09:25:22 -0000 (UTC)
> gazelle@shell.xmission.com (Kenny McCormack) boring babbled:
>>In article <vgl8h1$385vs$1@dont-email.me>, <Muttley@DastartdlyHQ.org> wrote:
>>>I've tried this code on both MacOS and Linux yet the child process getppid()
>>>still returns its original parent process instead of "1" for init which is
>>>what I'd expect. Isn't setsid() supposed to completely detach the child or
>>>have I misunderstood?
>>
>>I suppose the obvious question at this point is: Why do you care? What's
>>the underlying issue?
>
> Just curiousity mainly. I did setsid() and ignored SIGCLD so I wouldn't get
> zombie processes but noticed that getppid() didn't return 1. I'd forgotten
> about double fork to be fair - I need to re-read Stevens at some point! - but
> can't help thinking they should be a single shot OS call to completely
> seperate the child from parent. Yes, daemon() will do it as a single function
> call but I imagine that just does a double fork underneath anyway.

According to daemon(3) from glibc, the glibc version (common on Linux systems)
does not perform a double fork(2). Specifically,
"The GNU C library implementation of this function was taken from BSD,
and does not employ the double-fork technique (i.e., fork(2), set-
sid(2), fork(2)) that is necessary to ensure that the resulting daemon
process is not a session leader. Instead, the resulting daemon
session leader. On systems that follow System V semantics (e.g.,
Linux), this means that if the daemon opens a terminal that is not
already a controlling terminal for another session, then that terminal
will inadvertently become the controlling terminal for the daemon."

[snip]

--
Lew Pitcher
"In Skills We Trust"

Subject: Re: Why does getppid() still return old parent pid after setsid()?
From: Rainer Weikusat
Newsgroups: comp.unix.programmer
Date: Mon, 11 Nov 2024 15:53 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!feeder2.eternal-september.org!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: rweikusat@talktalk.net (Rainer Weikusat)
Newsgroups: comp.unix.programmer
Subject: Re: Why does getppid() still return old parent pid after setsid()?
Date: Mon, 11 Nov 2024 15:53:44 +0000
Lines: 11
Message-ID: <87frnxzrzr.fsf@doppelsaurus.mobileactivedefense.com>
References: <vgl8h1$385vs$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: individual.net e5zcsGrEt1bDbaNh6+ICSAVyvIscUzszmytfeqeO07mvW2x/4=
Cancel-Lock: sha1:zGkUsLb1pKqO+1IciHvmwso6XGA= sha1:Jjtw3yOUh8mRd36HkMcpzGIJuaE= sha256:qctn/lHOWyuU/SFrBvRlgBeLDKN9SoLd+XaaEcKwDFo=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
View all headers

Muttley@DastartdlyHQ.org writes:
> I've tried this code on both MacOS and Linux yet the child process getppid()
> still returns its original parent process instead of "1" for init which is
> what I'd expect. Isn't setsid() supposed to completely detach the child or
> have I misunderstood?

A session is a set of processes associated with a particular controlling
terminal, namely, that of the session leader (or no controlling terminal
if the session leader doesn't have one). setsid creates an unassociated
new session lead by the process which called it. It has no effect on
parent/ child relationships of processes.

Subject: Re: Why does getppid() still return old parent pid after setsid()?
From: Lew Pitcher
Newsgroups: comp.unix.programmer
Organization: A noiseless patient Spider
Date: Mon, 11 Nov 2024 17:08 UTC
References: 1 2
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: lew.pitcher@digitalfreehold.ca (Lew Pitcher)
Newsgroups: comp.unix.programmer
Subject: Re: Why does getppid() still return old parent pid after setsid()?
Date: Mon, 11 Nov 2024 17:08:20 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 23
Message-ID: <vgtdm4$11fog$2@dont-email.me>
References: <vgl8h1$385vs$1@dont-email.me>
<vgn9q2$8mls$1@news.xmission.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 11 Nov 2024 18:08:21 +0100 (CET)
Injection-Info: dont-email.me; posting-host="090d5a8025b43e616f11aef0313ce297";
logging-data="1097488"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+qLfQNWo8kgvybUolaBdaWIpBwGgTbRW4="
User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508
git://git.gnome.org/pan2)
Cancel-Lock: sha1:rRtlk2JjvHiI3ClnHrDDFHmLApk=
View all headers

On Sat, 09 Nov 2024 09:25:22 +0000, Kenny McCormack wrote:
[snip]
> As
> far as I know, in classic Unix, the only way for a process to get
> "re-parented" is to have the parent die. Of course, as others have noted,
> under Linux, there is also the re-parenting "prctl".

Kenny, could you elaborate on the use of prctl() to "reparent" a process?
I've not seen examples of how to do this, and the prctl(2) manpage doesn't
explicitly indicate the options necessary.

I don't see any option that controls or modifies a process PPID. The only
prctl(2) options that I can see relating to "reparenting" are the
PR_SET_CHILD_SUBREAPER and PR_GET_CHILD_SUBREAPER options that
seem to govern who gets SIGCHLD on the child's death. Is this what you
mean by "reparenting"?

[snip]

--
Lew Pitcher
"In Skills We Trust"

Subject: Re: Why does getppid() still return old parent pid after setsid()?
From: Scott Lurndal
Newsgroups: comp.unix.programmer
Organization: UsenetServer - www.usenetserver.com
Date: Mon, 11 Nov 2024 17:30 UTC
References: 1 2 3 4
Path: eternal-september.org!news.eternal-september.org!feeder2.eternal-september.org!border-2.nntp.ord.giganews.com!border-3.nntp.ord.giganews.com!nntp.giganews.com!news-out.netnews.com!s1-1.netnews.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx39.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: scott@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: Why does getppid() still return old parent pid after setsid()?
Newsgroups: comp.unix.programmer
References: <vgl8h1$385vs$1@dont-email.me> <vgn9q2$8mls$1@news.xmission.com> <vgsko5$u7pg$1@dont-email.me> <vgt7bp$11fog$1@dont-email.me>
Lines: 33
Message-ID: <bbrYO.227733$2nv5.98313@fx39.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Mon, 11 Nov 2024 17:30:47 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Mon, 11 Nov 2024 17:30:47 GMT
X-Received-Bytes: 2649
X-Original-Bytes: 2598
View all headers

Lew Pitcher <lew.pitcher@digitalfreehold.ca> writes:
>On Mon, 11 Nov 2024 10:02:45 +0000, Muttley wrote:
>
>> On Sat, 9 Nov 2024 09:25:22 -0000 (UTC)
>> gazelle@shell.xmission.com (Kenny McCormack) boring babbled:
>>>In article <vgl8h1$385vs$1@dont-email.me>, <Muttley@DastartdlyHQ.org> wrote:
>>>>I've tried this code on both MacOS and Linux yet the child process getppid()
>>>>still returns its original parent process instead of "1" for init which is
>>>>what I'd expect. Isn't setsid() supposed to completely detach the child or
>>>>have I misunderstood?
>>>
>>>I suppose the obvious question at this point is: Why do you care? What's
>>>the underlying issue?
>>
>> Just curiousity mainly. I did setsid() and ignored SIGCLD so I wouldn't get
>> zombie processes but noticed that getppid() didn't return 1. I'd forgotten
>> about double fork to be fair - I need to re-read Stevens at some point! - but
>> can't help thinking they should be a single shot OS call to completely
>> seperate the child from parent. Yes, daemon() will do it as a single function
>> call but I imagine that just does a double fork underneath anyway.
>
>According to daemon(3) from glibc, the glibc version (common on Linux systems)
>does not perform a double fork(2). Specifically,
> "The GNU C library implementation of this function was taken from BSD,
> and does not employ the double-fork technique (i.e., fork(2), set-
> sid(2), fork(2)) that is necessary to ensure that the resulting daemon
> process is not a session leader. Instead, the resulting daemon
> session leader. On systems that follow System V semantics (e.g.,
> Linux), this means that if the daemon opens a terminal that is not
> already a controlling terminal for another session, then that terminal
> will inadvertently become the controlling terminal for the daemon."

That's why POSIX has the 'O_NOCTTY' flag for open.

Subject: Re: Why does getppid() still return old parent pid after setsid()?
From: Kenny McCormack
Newsgroups: comp.unix.programmer
Organization: The official candy of the new Millennium
Date: Mon, 11 Nov 2024 20:15 UTC
References: 1 2 3
Path: eternal-september.org!news.eternal-september.org!feeder2.eternal-september.org!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gazelle@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.unix.programmer
Subject: Re: Why does getppid() still return old parent pid after setsid()?
Date: Mon, 11 Nov 2024 20:15:05 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <vgtok9$br6j$1@news.xmission.com>
References: <vgl8h1$385vs$1@dont-email.me> <vgn9q2$8mls$1@news.xmission.com> <vgtdm4$11fog$2@dont-email.me>
Injection-Date: Mon, 11 Nov 2024 20:15:05 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="388307"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
View all headers

In article <vgtdm4$11fog$2@dont-email.me>,
Lew Pitcher <lew.pitcher@digitalfreehold.ca> wrote:
....
>Kenny, could you elaborate on the use of prctl() to "reparent" a process?
>I've not seen examples of how to do this, and the prctl(2) manpage doesn't
>explicitly indicate the options necessary.
>
>I don't see any option that controls or modifies a process PPID. The only
>prctl(2) options that I can see relating to "reparenting" are the
>PR_SET_CHILD_SUBREAPER and PR_GET_CHILD_SUBREAPER options that
>seem to govern who gets SIGCHLD on the child's death. Is this what you
>mean by "reparenting"?

Yes, that's it. The man page text even includes the word 'reparented'.

Note; I've never actually used this call, but I can imagine situations
where it could be used. The man page seems pretty clear as to what it does.

--
The difference between communism and capitalism?
In capitalism, man exploits man. In communism, it's the other way around.

- Daniel Bell, The End of Ideology (1960) -

Subject: Re: Why does getppid() still return old parent pid after setsid()?
From: Lawrence D'Oliv
Newsgroups: comp.unix.programmer
Organization: A noiseless patient Spider
Date: Mon, 11 Nov 2024 20:57 UTC
References: 1 2 3
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ldo@nz.invalid (Lawrence D'Oliveiro)
Newsgroups: comp.unix.programmer
Subject: Re: Why does getppid() still return old parent pid after setsid()?
Date: Mon, 11 Nov 2024 20:57:23 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 6
Message-ID: <vgtr3i$15srv$2@dont-email.me>
References: <vgl8h1$385vs$1@dont-email.me> <vgn9q2$8mls$1@news.xmission.com>
<vgsko5$u7pg$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 11 Nov 2024 21:57:23 +0100 (CET)
Injection-Info: dont-email.me; posting-host="13b7eac62ff6457352a541d5a235d1e2";
logging-data="1241983"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+n399nm56/dGasTzKxva25"
User-Agent: Pan/0.160 (Toresk; )
Cancel-Lock: sha1:v/Oih/wGC5hAqulhPvrd+fUHBYw=
View all headers

On Mon, 11 Nov 2024 10:02:45 -0000 (UTC), Muttley wrote:

> can't help thinking they should be a single shot OS call to completely
> seperate the child from parent.

I guess it’s not that important.

Subject: Re: Why does getppid() still return old parent pid after setsid()?
From: Kenny McCormack
Newsgroups: comp.unix.programmer
Organization: The official candy of the new Millennium
Date: Tue, 12 Nov 2024 12:15 UTC
References: 1 2 3 4
Path: eternal-september.org!news.eternal-september.org!feeder2.eternal-september.org!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gazelle@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.unix.programmer
Subject: Re: Why does getppid() still return old parent pid after setsid()?
Date: Tue, 12 Nov 2024 12:15:29 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <vgvgt1$cnri$1@news.xmission.com>
References: <vgl8h1$385vs$1@dont-email.me> <vgn9q2$8mls$1@news.xmission.com> <vgsko5$u7pg$1@dont-email.me> <vgtr3i$15srv$2@dont-email.me>
Injection-Date: Tue, 12 Nov 2024 12:15:29 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="417650"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
View all headers

In article <vgtr3i$15srv$2@dont-email.me>,
Lawrence D'Oliveiro <ldo@nz.invalid> wrote:
>On Mon, 11 Nov 2024 10:02:45 -0000 (UTC), Muttley wrote:
>
>> can't help thinking they should be a single shot OS call to completely
>> seperate the child from parent.
>
>I guess its not that important.

It *is* "that important". And daemon(3) fills the role.

However, it may be true that it doesn't fully fill it - and maybe that it
should also do the "double fork".

--
The whole aim of practical politics is to keep the populace alarmed (and hence clamorous
to be led to safety) by menacing it with an endless series of hobgoblins, all of them imaginary.

H. L. Mencken

Subject: Re: Why does getppid() still return old parent pid after setsid()?
From: Geoff Clare
Newsgroups: comp.unix.programmer
Date: Tue, 12 Nov 2024 13:39 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!feeder2.eternal-september.org!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: geoff@clare.See-My-Signature.invalid (Geoff Clare)
Newsgroups: comp.unix.programmer
Subject: Re: Why does getppid() still return old parent pid after setsid()?
Date: Tue, 12 Nov 2024 13:39:25 +0000
Lines: 18
Message-ID: <dd2c0l-saq.ln1@ID-313840.user.individual.net>
References: <vgl8h1$385vs$1@dont-email.me>
Reply-To: netnews@gclare.org.uk
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: individual.net Jg8af2KCZjODd4PI5GMHogDWufGy6ORKHUm0Zbk/GhkXNQZUHv
X-Orig-Path: ID-313840.user.individual.net!not-for-mail
Cancel-Lock: sha1:U1ZjWnnENUEOUso2zt0IoH3veVI= sha256:YLSYychjQJmhrFUv/gTMC/KD4CoB0b0DjqgUPfYhyuA=
User-Agent: Pan/0.154 (Izium; 517acf4)
View all headers

Muttley wrote:

> I've tried this code on both MacOS and Linux yet the child process getppid()
> still returns its original parent process instead of "1" for init which is
> what I'd expect. Isn't setsid() supposed to completely detach the child or
> have I misunderstood?

As others have pointed out, you have misunderstood, but I don't
think anyone has quoted a relevant bit of POSIX in the definition
of "Parent Process ID" (XBD 3.253 in POSIX.1-2024):

The parent process ID of a process is the process ID of its
creator, for the lifetime of the creator. After the creator's
lifetime has ended, the parent process ID is the process ID of an
implementation-defined system process.

--
Geoff Clare <netnews@gclare.org.uk>

Subject: Re: Why does getppid() still return old parent pid after setsid()?
From: Muttley@DastartdlyHQ.org
Newsgroups: comp.unix.programmer
Organization: A noiseless patient Spider
Date: Tue, 12 Nov 2024 15:13 UTC
References: 1 2
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Muttley@DastartdlyHQ.org
Newsgroups: comp.unix.programmer
Subject: Re: Why does getppid() still return old parent pid after setsid()?
Date: Tue, 12 Nov 2024 15:13:27 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 25
Message-ID: <vgvran$1la87$1@dont-email.me>
References: <vgl8h1$385vs$1@dont-email.me> <dd2c0l-saq.ln1@ID-313840.user.individual.net>
Injection-Date: Tue, 12 Nov 2024 16:13:27 +0100 (CET)
Injection-Info: dont-email.me; posting-host="ddfc3d397de469850af03a6d8c1dcede";
logging-data="1747207"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX194U0VzFXQu1qpFeuwy+AZE"
Cancel-Lock: sha1:c0hBj4c0I4zpM8Zx3KsFW2d+8xc=
View all headers

On Tue, 12 Nov 2024 13:39:25 +0000
Geoff Clare <geoff@clare.See-My-Signature.invalid> boring babbled:
>Muttley wrote:
>
>> I've tried this code on both MacOS and Linux yet the child process getppid()
>> still returns its original parent process instead of "1" for init which is
>> what I'd expect. Isn't setsid() supposed to completely detach the child or
>> have I misunderstood?
>
>As others have pointed out, you have misunderstood, but I don't
>think anyone has quoted a relevant bit of POSIX in the definition
>of "Parent Process ID" (XBD 3.253 in POSIX.1-2024):
>
> The parent process ID of a process is the process ID of its
> creator, for the lifetime of the creator. After the creator's
> lifetime has ended, the parent process ID is the process ID of an
> implementation-defined system process.

Fair enough. So reparenting can't happen so long as the parent exists no matter
what the child does.

I could envisage scenarios where the child process being able to reparent
itself - given correct permissions - could be useful. Wonder if any OS's
implement that?

Subject: Re: Why does getppid() still return old parent pid after setsid()?
From: Kaz Kylheku
Newsgroups: comp.unix.programmer
Organization: A noiseless patient Spider
Date: Tue, 12 Nov 2024 21:24 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 643-408-1753@kylheku.com (Kaz Kylheku)
Newsgroups: comp.unix.programmer
Subject: Re: Why does getppid() still return old parent pid after setsid()?
Date: Tue, 12 Nov 2024 21:24:52 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 31
Message-ID: <20241112131933.158@kylheku.com>
References: <vgl8h1$385vs$1@dont-email.me>
Injection-Date: Tue, 12 Nov 2024 22:24:53 +0100 (CET)
Injection-Info: dont-email.me; posting-host="ae3b77ee73e9050342ac300cdc53a8dc";
logging-data="1888656"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18fH91me+NNnAQbLIbN0NdYtz7362IDPWU="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:bFRz3iPbLlr/yqa2ORzVxmrO2VM=
View all headers

On 2024-11-08, Muttley@DastartdlyHQ.org <Muttley@DastartdlyHQ.org> wrote:
> I've tried this code on both MacOS and Linux yet the child process getppid()
> still returns its original parent process instead of "1" for init which is
> what I'd expect. Isn't setsid() supposed to completely detach the child or
> have I misunderstood?

No; it's because you put a 1 second delay into the parent to keep it
alive, whereas the child calls getppid() without any such timed
scheduling delay.

Because 1 second is an eternity, you've practically guaranteed that the
child will get the original parent pid. The only thing that would make
it air tight would be if the parent waited for the child's termination
instead of sleeping.

setsid has to do with the TTY session, and manipulates the group ID.
If you read descriptions of it carefully, you will see that it has
nothing to do with detaching from a parent process.

To detach from a parent process, do a double fork to create a
grandchild. Have the child quit. Then the grandchild will be orphaned
and reparented to some process (typically PID 1). The timing of
that is "racy"; it has to do with when the intermediate child
terminates, until which time, the grandchild has that process as its
parent PID.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Subject: Re: Why does getppid() still return old parent pid after setsid()?
From: Muttley@DastartdlyHQ.org
Newsgroups: comp.unix.programmer
Organization: A noiseless patient Spider
Date: Wed, 13 Nov 2024 08:12 UTC
References: 1 2
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Muttley@DastartdlyHQ.org
Newsgroups: comp.unix.programmer
Subject: Re: Why does getppid() still return old parent pid after setsid()?
Date: Wed, 13 Nov 2024 08:12:13 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 15
Message-ID: <vh1n0t$241cn$1@dont-email.me>
References: <vgl8h1$385vs$1@dont-email.me> <20241112131933.158@kylheku.com>
Injection-Date: Wed, 13 Nov 2024 09:12:13 +0100 (CET)
Injection-Info: dont-email.me; posting-host="719f81d1d81f332a402faaf93844d429";
logging-data="2229655"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1916W3csfkUO9W2zoM0KgeT"
Cancel-Lock: sha1:BvEqNYXjJpLO3jQz1z5hXB11kr0=
View all headers

On Tue, 12 Nov 2024 21:24:52 -0000 (UTC)
Kaz Kylheku <643-408-1753@kylheku.com> boring babbled:
>On 2024-11-08, Muttley@DastartdlyHQ.org <Muttley@DastartdlyHQ.org> wrote:
>> I've tried this code on both MacOS and Linux yet the child process getppid()
>> still returns its original parent process instead of "1" for init which is
>> what I'd expect. Isn't setsid() supposed to completely detach the child or
>> have I misunderstood?
>
>No; it's because you put a 1 second delay into the parent to keep it
>alive, whereas the child calls getppid() without any such timed
>scheduling delay.

True, I fixed it soon after, should have been the child that executed it.
But it doesn't make any difference but now I know why.

Subject: Re: Why does getppid() still return old parent pid after setsid()?
From: Richard Harnden
Newsgroups: comp.unix.programmer
Organization: A noiseless patient Spider
Date: Wed, 13 Nov 2024 13:38 UTC
References: 1 2 3 4 5
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: richard.nospam@gmail.invalid (Richard Harnden)
Newsgroups: comp.unix.programmer
Subject: Re: Why does getppid() still return old parent pid after setsid()?
Date: Wed, 13 Nov 2024 13:38:58 +0000
Organization: A noiseless patient Spider
Lines: 20
Message-ID: <vh2a5i$27jod$1@dont-email.me>
References: <vgl8h1$385vs$1@dont-email.me> <vgn9q2$8mls$1@news.xmission.com>
<vgsko5$u7pg$1@dont-email.me> <vgtr3i$15srv$2@dont-email.me>
<vgvgt1$cnri$1@news.xmission.com>
Reply-To: nospam.harnden@invalid.com
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 13 Nov 2024 14:38:58 +0100 (CET)
Injection-Info: dont-email.me; posting-host="87c6b61cde6c0498c083f38248d2565a";
logging-data="2346765"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19uZor2PHw3NP8toabLWDUGv5YfKQzDvns="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:nrDJyzM3q4TDVHwXv37J+YKSvY0=
In-Reply-To: <vgvgt1$cnri$1@news.xmission.com>
Content-Language: en-GB
View all headers

On 12/11/2024 12:15, Kenny McCormack wrote:
> In article <vgtr3i$15srv$2@dont-email.me>,
> Lawrence D'Oliveiro <ldo@nz.invalid> wrote:
>> On Mon, 11 Nov 2024 10:02:45 -0000 (UTC), Muttley wrote:
>>
>>> can't help thinking they should be a single shot OS call to completely
>>> seperate the child from parent.
>>
>> I guess its not that important.
>
> It *is* "that important". And daemon(3) fills the role.
>
> However, it may be true that it doesn't fully fill it - and maybe that it
> should also do the "double fork".
>

Should it be: parent fork, child setsid, child fork, grandchild exec
or: parent fork, child fork, grandchild setsid, grandchild exec

?

Subject: Re: Why does getppid() still return old parent pid after setsid()?
From: Lew Pitcher
Newsgroups: comp.unix.programmer
Organization: A noiseless patient Spider
Date: Wed, 13 Nov 2024 15:00 UTC
References: 1 2 3 4 5 6
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: lew.pitcher@digitalfreehold.ca (Lew Pitcher)
Newsgroups: comp.unix.programmer
Subject: Re: Why does getppid() still return old parent pid after setsid()?
Date: Wed, 13 Nov 2024 15:00:53 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 69
Message-ID: <vh2ev5$28cgs$1@dont-email.me>
References: <vgl8h1$385vs$1@dont-email.me>
<vgn9q2$8mls$1@news.xmission.com> <vgsko5$u7pg$1@dont-email.me>
<vgtr3i$15srv$2@dont-email.me> <vgvgt1$cnri$1@news.xmission.com>
<vh2a5i$27jod$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 13 Nov 2024 16:00:56 +0100 (CET)
Injection-Info: dont-email.me; posting-host="3a00e57054ce14f4847fa22ab39c4859";
logging-data="2372124"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+0JlEEZv1nD8Q6PnsUBZBKmZ4hA4mYCDA="
User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508
git://git.gnome.org/pan2)
Cancel-Lock: sha1:JvSdO8Ryhmc868SI4nIKF8IAczQ=
View all headers

On Wed, 13 Nov 2024 13:38:58 +0000, Richard Harnden wrote:

> On 12/11/2024 12:15, Kenny McCormack wrote:
>> In article <vgtr3i$15srv$2@dont-email.me>,
>> Lawrence D'Oliveiro <ldo@nz.invalid> wrote:
>>> On Mon, 11 Nov 2024 10:02:45 -0000 (UTC), Muttley wrote:
>>>
>>>> can't help thinking they should be a single shot OS call to completely
>>>> seperate the child from parent.
>>>
>>> I guess its not that important.
>>
>> It *is* "that important". And daemon(3) fills the role.
>>
>> However, it may be true that it doesn't fully fill it - and maybe that it
>> should also do the "double fork".
>>
>
> Should it be: parent fork, child setsid, child fork, grandchild exec
> or: parent fork, child fork, grandchild setsid, grandchild exec

With "parent fork, child fork, grandchild setsid, grandchild exec", while
the grandchild's PPID is (or should be, sans race conditions) == 1 (init),
it's SID (session ID) and PGID (process group ID) are now equal to its
PID, putting the grandchild in a different session and process group
than the original parent, and making the grandchild a session and group leader

However, with "parent fork, child setsid, child fork, grandchild exec",
while the grandchild's PPID is also (or should also be) == 1 (with the
same caveats regarding race conditions), it's SID (session ID) and PGID
(process group ID) are equal to its (real) parent. This also puts the
grandchild in a different session and process group than the original parent,
BUT, makes the grandchild a member of that session and group, instead
of its leader.

The difference is that the "parent fork, child fork, grandchild setsid,
grandchild exec" sequence can still acquire a controlling terminal if it
opens a terminal device WITHOUT the O_NOCTTY option. HOWEVER, the "parent
fork, child setsid, child fork, grandchild exec" scenario CANNOT acquire
a controlling terminal, no matter what options it opens a terminal device
with.

The "double fork" of the "double fork scenario" permits the child process
to become a session and group leader (something that the parent process
may not be able to do (if that parent process is already a process
group leader, see setsid(2)). Because that child process is a session
and group leader, it cannot "inherit" a controlling terminal; but it
can open(2) a controlling terminal if it wishes.

But, in the "double fork scenario", the child process fork(2)s the grandchild
process immediately after setsid(2), then exits. This leaves the grandchild
a) with a PPID of 1 (courtesy of the child process termination),
b) a session ID which is not equal to its PID,
c) a process group id which is not equal to its PID
(so it is neither a session leader nor a process group leader),
d) without a controlling terminal (courtesy of the setsid(2)), and
e) without the ability to obtain a controlling terminal (because it is
neither a session leader nor a process group leader).

The grandchild can access files and devices, so it /has/ the ability
to interact with the outside world, but on its own terms. The only
way (highly simplified) the outside world can interact with the grandchild,
though, is through root-generated signals, which the grandchild can handle
with appropriate signal-handling logic.

--
Lew Pitcher
"In Skills We Trust"

Subject: Re: Why does getppid() still return old parent pid after setsid()?
From: Richard Harnden
Newsgroups: comp.unix.programmer
Organization: A noiseless patient Spider
Date: Wed, 13 Nov 2024 17:23 UTC
References: 1 2 3 4 5 6 7
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: richard.nospam@gmail.invalid (Richard Harnden)
Newsgroups: comp.unix.programmer
Subject: Re: Why does getppid() still return old parent pid after setsid()?
Date: Wed, 13 Nov 2024 17:23:26 +0000
Organization: A noiseless patient Spider
Lines: 70
Message-ID: <vh2nae$2aai0$1@dont-email.me>
References: <vgl8h1$385vs$1@dont-email.me> <vgn9q2$8mls$1@news.xmission.com>
<vgsko5$u7pg$1@dont-email.me> <vgtr3i$15srv$2@dont-email.me>
<vgvgt1$cnri$1@news.xmission.com> <vh2a5i$27jod$1@dont-email.me>
<vh2ev5$28cgs$1@dont-email.me>
Reply-To: nospam.harnden@invalid.com
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 13 Nov 2024 18:23:27 +0100 (CET)
Injection-Info: dont-email.me; posting-host="87c6b61cde6c0498c083f38248d2565a";
logging-data="2435648"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19asl2ki6CvIJrpkOKD1Tj7IENmS33Dmag="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:XDrutsS/sKCLup2iOTQ7lKYCaXc=
Content-Language: en-GB
In-Reply-To: <vh2ev5$28cgs$1@dont-email.me>
View all headers

On 13/11/2024 15:00, Lew Pitcher wrote:
> On Wed, 13 Nov 2024 13:38:58 +0000, Richard Harnden wrote:
>
>> On 12/11/2024 12:15, Kenny McCormack wrote:
>>> In article <vgtr3i$15srv$2@dont-email.me>,
>>> Lawrence D'Oliveiro <ldo@nz.invalid> wrote:
>>>> On Mon, 11 Nov 2024 10:02:45 -0000 (UTC), Muttley wrote:
>>>>
>>>>> can't help thinking they should be a single shot OS call to completely
>>>>> seperate the child from parent.
>>>>
>>>> I guess its not that important.
>>>
>>> It *is* "that important". And daemon(3) fills the role.
>>>
>>> However, it may be true that it doesn't fully fill it - and maybe that it
>>> should also do the "double fork".
>>>
>>
>> Should it be: parent fork, child setsid, child fork, grandchild exec
>> or: parent fork, child fork, grandchild setsid, grandchild exec
>
> With "parent fork, child fork, grandchild setsid, grandchild exec", while
> the grandchild's PPID is (or should be, sans race conditions) == 1 (init),
> it's SID (session ID) and PGID (process group ID) are now equal to its
> PID, putting the grandchild in a different session and process group
> than the original parent, and making the grandchild a session and group leader
>
> However, with "parent fork, child setsid, child fork, grandchild exec",
> while the grandchild's PPID is also (or should also be) == 1 (with the
> same caveats regarding race conditions), it's SID (session ID) and PGID
> (process group ID) are equal to its (real) parent. This also puts the
> grandchild in a different session and process group than the original parent,
> BUT, makes the grandchild a member of that session and group, instead
> of its leader.
>
> The difference is that the "parent fork, child fork, grandchild setsid,
> grandchild exec" sequence can still acquire a controlling terminal if it
> opens a terminal device WITHOUT the O_NOCTTY option. HOWEVER, the "parent
> fork, child setsid, child fork, grandchild exec" scenario CANNOT acquire
> a controlling terminal, no matter what options it opens a terminal device
> with.
>
> The "double fork" of the "double fork scenario" permits the child process
> to become a session and group leader (something that the parent process
> may not be able to do (if that parent process is already a process
> group leader, see setsid(2)). Because that child process is a session
> and group leader, it cannot "inherit" a controlling terminal; but it
> can open(2) a controlling terminal if it wishes.
>
> But, in the "double fork scenario", the child process fork(2)s the grandchild
> process immediately after setsid(2), then exits. This leaves the grandchild
> a) with a PPID of 1 (courtesy of the child process termination),
> b) a session ID which is not equal to its PID,
> c) a process group id which is not equal to its PID
> (so it is neither a session leader nor a process group leader),
> d) without a controlling terminal (courtesy of the setsid(2)), and
> e) without the ability to obtain a controlling terminal (because it is
> neither a session leader nor a process group leader).
>
> The grandchild can access files and devices, so it /has/ the ability
> to interact with the outside world, but on its own terms. The only
> way (highly simplified) the outside world can interact with the grandchild,
> though, is through root-generated signals, which the grandchild can handle
> with appropriate signal-handling logic.
>
>

Thanks very much.

1

rocksolid light 0.9.8
clearnet tor