jehanne/qa/kern/fork_chain.c

131 lines
2.6 KiB
C
Raw Normal View History

/*
* This file is part of Jehanne.
*
* Copyright (C) 2017 Giacomo Tesio <giacomo@tesio.it>
*
* This is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* Jehanne is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Jehanne. If not, see <http://www.gnu.org/licenses/>.
*/
#include <u.h>
#include <lib9.h>
int r1;
int r2;
#define CHILD_READY(pid) ((long)rendezvous(&r1, (void*)(~(pid))))
2017-06-05 19:25:08 +02:00
//#define C2P_READY(pid) ((long)rendezvous(&r2, (void*)(~(pid))))
2017-06-05 19:25:08 +02:00
int *__target_pid;
static void
forwarding_note_handler(void *ureg, char *note)
{
2017-06-05 19:25:08 +02:00
postnote(PNPROC, *__target_pid, note);
noted(NDFLT);
}
static void
2017-06-05 19:25:08 +02:00
donothing(void)
{
2017-06-05 19:25:08 +02:00
int i = 0;
2017-06-06 19:53:37 +02:00
while(++i < 100)
2017-06-05 19:25:08 +02:00
sleep(3000);
exits(nil);
}
static int
2017-06-05 19:25:08 +02:00
crazy_fork(void)
{
int father = getpid();
int p2c;
long c2p = -1, child = -1;
switch(p2c = rfork(RFPROC|RFMEM)){
case -1:
return -1;
case 0:
switch(c2p = rfork(RFPROC|RFMEM)){
case -1:
exits("rfork (c2p)");
case 0:
switch(child = fork()){
case -1:
exits("rfork (child)");
case 0:
return 0;
default:
while(CHILD_READY(child) == -1)
;
2017-06-05 19:25:08 +02:00
*__target_pid = father;
notify(forwarding_note_handler);
donothing();
}
default:
while((child = CHILD_READY(-3)) == -1)
;
child = ~child;
2017-06-05 19:25:08 +02:00
*__target_pid = child;
notify(forwarding_note_handler);
2017-06-05 19:25:08 +02:00
donothing();
}
default:
break;
}
return p2c;
}
int
2017-06-05 19:25:08 +02:00
pass_on_die(void *v, char *s)
{
2017-06-05 19:25:08 +02:00
if(strncmp(s, "die", 4) == 0){
print("PASS\n");
exits("PASS");
}
return 0;
}
void
main(void)
{
int c, target_pid = 0;
2017-06-05 19:25:08 +02:00
__target_pid = &target_pid;
2017-06-05 19:25:08 +02:00
if (!atnotify(pass_on_die, 1)){
fprint(2, "%r\n");
exits("atnotify fails");
}
2017-06-05 19:25:08 +02:00
c = crazy_fork();
switch(c){
case -1:
print("FAIL: fork\n");
exits("FAIL");
case 0:
2017-06-05 19:25:08 +02:00
print("child is %d; child's parent is %d\n", getpid(), getppid());
*__target_pid = getppid();
notify(forwarding_note_handler);
/* wait to be killed */
2017-06-05 19:25:08 +02:00
donothing();
break;
default:
2017-06-05 19:25:08 +02:00
print("father is %d; forked child is %d\n", getpid(), c);
sleep(1000); /* give children time to notify() */
print("starting note chain\n");
postnote(PNPROC, c, "die");
2017-06-05 19:25:08 +02:00
/* wait to be killed by the chain */
sleep(30000);
}
print("FAIL\n");
exits("FAIL");
}