From 08d6b0f043f30d85033aacce63fbd9985f3e5325 Mon Sep 17 00:00:00 2001 From: cinap_lenrek Date: Sun, 1 Dec 2019 17:57:14 +0100 Subject: [PATCH] kernel: improve diagnostics by reversing the roles of Proc.parentpid and Proc.parent for better system diagnostics, we *ALWAYS* want to record the parent pid of a user process, regardless of if the child will post a wait record on exit or not. for that, we reverse the roles of Proc.parent and Proc.parentpid so Proc.parentpid will always be set on rfork() and the Proc.parent pointer will point to the parent's Proc structure or is set to nil when no wait record should be posted on exit (RFNOWAIT flag). this means that we can get the pid of the original parent process from /proc, regardless of the the child having rforked with the RFNOWAIT flag. this improves the output of pstree(1) somewhat if the parent is still alive. note that theres no guarantee that the parent pid is still valid. the conditions are unchanged: a user process that will post wait record has: up->kp == 0 && up->parent != nil && up->parent->pid == up->parentpid the boot process is: up->kp == 0 && up->parent == nil && up->parentpid == 0 and kproc's have: up->kp != 0 && up->parent == nil && up->parentpid == 0 --- sys/src/9/port/proc.c | 13 ++++++++++--- sys/src/9/port/sysproc.c | 4 ++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/sys/src/9/port/proc.c b/sys/src/9/port/proc.c index adc085db2..f67dc9f3a 100644 --- a/sys/src/9/port/proc.c +++ b/sys/src/9/port/proc.c @@ -1130,7 +1130,14 @@ pexit(char *exitstr, int freemem) * if not a kernel process and have a parent, * do some housekeeping. */ - if(up->kp == 0 && up->parentpid != 0) { + if(up->kp) + goto Nowait; + + p = up->parent; + if(p != nil){ + if(p->pid != up->parentpid || p->state == Broken) + goto Nowait; + wq = smalloc(sizeof(Waitq)); wq->w.pid = up->pid; utime = up->time[TUser] + up->time[TCUser]; @@ -1143,7 +1150,6 @@ pexit(char *exitstr, int freemem) else wq->w.msg[0] = '\0'; - p = up->parent; lock(&p->exl); /* * Check that parent is still alive. @@ -1171,12 +1177,13 @@ pexit(char *exitstr, int freemem) if(wq != nil) free(wq); } - else if(up->kp == 0 && up->parent == nil){ + else if(up->parentpid == 0){ if(exitstr == nil) exitstr = "unknown"; panic("boot process died: %s", exitstr); } +Nowait: if(!freemem) addbroken(up); diff --git a/sys/src/9/port/sysproc.c b/sys/src/9/port/sysproc.c index 7625b946c..e5aa537d9 100644 --- a/sys/src/9/port/sysproc.c +++ b/sys/src/9/port/sysproc.c @@ -181,9 +181,9 @@ sysrfork(va_list list) */ forkchild(p, up->dbgreg); - p->parent = up; + p->parentpid = up->pid; if((flag&RFNOWAIT) == 0){ - p->parentpid = up->pid; + p->parent = up; lock(&up->exl); up->nchild++; unlock(&up->exl);