Project

General

Profile

Actions

Feature #13677

open

pwait -v should decode wait status

Added by Rich Lowe 9 months ago. Updated 8 months ago.

Status:
New
Priority:
Normal
Assignee:
Category:
cmd - userland programs
Start date:
Due date:
% Done:

0%

Estimated time:
Difficulty:
Bite-size
Tags:
Gerrit CR:

Description

At present, pwait -v just prints the return from wait as a hex integer, it'd be nice if it decoded it.


Files

pwait_screenshot.PNG (8.26 KB) pwait_screenshot.PNG Ryan England, 2021-04-16 02:01 AM
zomb.c (137 Bytes) zomb.c Ryan England, 2021-04-26 04:23 PM
Actions #1

Updated by Ryan England 8 months ago

Rich, I have modified the code to return and integer as opposed to hex. Is this what you meant by decode?

Test: Echo terminal PID, pwait -v PID, close terminal. See screenshot for output.

Actions #2

Updated by Ryan England 8 months ago

  • Assignee set to Ryan England
Actions #3

Updated by Joshua M. Clulow 8 months ago

I suspect Rich probably meant that in addition to printing the numeric value, we could print a string that describes what it means symbolically. For reference, there are a number of macros documented in the wait.h manual page that allow for decoding the status. e.g., you might do something like:

static void
maybe_comma(custr_t *)
{
    if (custr_len(cu) != 0) {
        if (custr_append(cu, ", ") != 0) {
            err(1, "custr_append");
        }
    }
}

static custr_t *
describe_status(int status)
{
    custr_t *cu;

    if (custr_alloc(&cu) != 0) {
        err(1, "custr_alloc");
    }

    if (WIFEXITED(status)) {
        maybe_comma(cu);
        if (custr_append_printf(cu, "exited with status %d",
            WEXITSTATUS(status)) != 0) {
            err(1, "custr_append_printf");
        }
    }

    if (WIFSIGNALED(status)) {
        maybe_comma(cu);
        if (custr_append_printf(cu, "terminated by signal %d",
            WTERMSIG(status)) != 0) {
            err(1, "custr_append_printf");
        }

        if (WCOREDUMP(status)) {
            maybe_comma(cu);
            if (custr_append(cu, "dumped core")) {
                err(1, "custr_append_printf");
            }
        }
    }

    return (cu);
}

Note that this is an incomplete sketch. You might also consider translating the signal numbers into names as well, say, for WTERMSIG and WSTOPSIG.

Actions #4

Updated by Andy Fiddaman 8 months ago

When I recently tried to get pwait to show anything other than zero, I was unsuccessful.
Have you had any success there?

Actions #5

Updated by Ryan England 8 months ago

Andy Fiddaman wrote in #note-4:

When I recently tried to get pwait to show anything other than zero, I was unsuccessful.
Have you had any success there?

Andy, I have NOT been able to get pwait to return anything other than zero either.

Actions #6

Updated by Rich Lowe 8 months ago

I had seen other values at least once, but I'm struggling to get it now. I guess maybe we should change it to "should work"?
And yes, decoded in the sense that Joshua sketched out. Sorry for being so terse.

Actions #7

Updated by Ryan England 8 months ago

Shout out to Andy Fiddaman, who created the following code in order to create a zombie process. This was needed in order to get pwait to output something other than zero. The code can be created within a file named zomb.c.

#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>

void
main(void)
{
        if (fork() == 0)
                exit(13);
        for (;;)
                sleep(60);
}
build% ./zomb &
[1] 24936
build% ptree $!
11196  zsched
  11340  /sbin/init
    11865  /usr/sbin/sshd
      9386   /usr/sbin/sshd -R
        9388   /usr/sbin/sshd -R
          9389   -zsh
            24936  ./zomb
              24937  <defunct>
build% xxd -s 230 -l4 /proc/24937/psinfo
000000e6: 0000 000d                                ....
build% pwait -v 24937
24937: terminated, wait status 0x0d00
Actions

Also available in: Atom PDF