summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorSean Kurtz <seankurtz75@gmail.com>2026-11-24 23:49:46 -0600
committerGitHub <noreply@github.com>2026-11-24 23:49:46 -0600
commitc4bbeec5dcf57289391fe108830d1e2e1c930321 (patch)
treef538741d8f7238369b28b397f1aeef30c262b0b1
parentd5d70f632866eb5757e0117a0ddb7122dab865a1 (diff)
parentf846648e980bcf21b1dafda08637ea19c490c2a4 (diff)
downloadwhoson-c4bbeec5dcf57289391fe108830d1e2e1c930321.tar.gz
Merge pull request #12 from skurtz97/feat/session-detail
More about the user: SSH CPU time, session process count, real USERID; make list
-rw-r--r--.gitignore2
-rw-r--r--Makefile25
-rw-r--r--README.md32
-rw-r--r--whoson.c827
4 files changed, 466 insertions, 420 deletions
diff --git a/.gitignore b/.gitignore
index 4ffb1bb..c4b4758 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,5 @@
/whoson
*.o
CEEDUMP.*
+/whoson.json
+/whoson.listing
diff --git a/Makefile b/Makefile
index 41bd793..a687945 100644
--- a/Makefile
+++ b/Makefile
@@ -13,10 +13,11 @@
# 64-bit is not negotiable: the ZOAU shared library is a 64-bit DLL and both
# its sidedecks (/usr/lpp/IBM/zoautil/lib and /usr/lpp/IBM/zoau/v1r3/lib)
# import CODE64, so -m64 is required even though a 31-bit program would start
-# faster. Use the IBM compiler directly, not the ~/.local/bin/ibm-clang
-# wrapper: the wrapper injects -Wl,dll,edit=no, which makes the binder treat
-# the sidedeck as a DD and fail with IEW2763S. The library also has to be
-# reachable at run time through LIBPATH.
+# faster. The compiler is pinned by absolute path because 2.1 and 2.2 are
+# both installed and PATH has carried each; this project builds with 2.2
+# (clang 21.1). A wrapper around the compiler that adds -Wl,dll,edit=no is
+# unusable: it makes the binder treat the sidedeck as a DD and fail with
+# IEW2763S. The library also has to be reachable at run time through LIBPATH.
#
# ASCII character mode is deliberate: the text ZOAU returns and the text the
# ssh channel carries are ASCII, so literals stay ASCII and the display fields
@@ -28,7 +29,9 @@
ZOAU_HOME = /usr/lpp/IBM/zoautil
PREFIX = $(HOME)/.local
BINDIR = $(PREFIX)/bin
-CC = /usr/lpp/IBM/cnw/v2r2/openxl/bin/ibm-clang
+CCDIR = /usr/lpp/IBM/cnw/v2r2/openxl/bin
+CC = $(CCDIR)/ibm-clang
+LISTFMT = $(CCDIR)/ibm-clang-listing-formatter
CPPFLAGS = -D_XOPEN_SOURCE=600 -D_POSIX_C_SOURCE=200809L -D_ALL_SOURCE \
-D_EXT
CFLAGS = -std=c17 -O3 -m64 -fzos-le-char-mode=ascii -Wall -Wextra \
@@ -36,13 +39,21 @@ CFLAGS = -std=c17 -O3 -m64 -fzos-le-char-mode=ascii -Wall -Wextra \
ZOAU_DLL = $(ZOAU_HOME)/lib/zoautil.x
CEEOPTS = HEAPCHK(ON,1,32),RPTSTG(ON),FILETAG(AUTOCVT,AUTOTAG),POSIX(ON)
-.PHONY: all install check clean
+.PHONY: all install check list clean
all: whoson
whoson: whoson.c
$(CC) $(CPPFLAGS) $(CFLAGS) -o $@ whoson.c $(ZOAU_DLL)
+# Open XL 2.2 can write a listing of the compile, which the formatter shipped
+# with it renders as pseudo assembly with offsets, grouped by function. The
+# binary and the listing come out of the same compile, so they cannot
+# disagree; whoson.json is the intermediate the formatter reads.
+list:
+ $(CC) $(CPPFLAGS) $(CFLAGS) -mzos-listing=json -mzos-listing-file-name=whoson.json -o whoson whoson.c $(ZOAU_DLL)
+ $(LISTFMT) whoson.json > whoson.listing
+
# CEE's heap checker catches overruns; RPTSTG prints the storage report at
# exit, which is how leaks show up without a debugger.
# Installs into $(PREFIX)/bin, ~/.local/bin by default, which is on PATH
@@ -58,4 +69,4 @@ check: whoson
_CEE_RUNOPTS="$(CEEOPTS)" ./whoson
clean:
- rm -f whoson *.o
+ rm -f whoson *.o whoson.json whoson.listing
diff --git a/README.md b/README.md
index 0ddc123..972fcaf 100644
--- a/README.md
+++ b/README.md
@@ -1,13 +1,15 @@
# whoson
Who is logged on to this z/OS system, and how: TSO/E or SSH, with the job id,
-CPU time, elapsed time, service class, priority and region size.
+CPU time, elapsed time, service class, priority, region size and process
+count.
-```
+```text
$ whoson
-USER TYPE STATUS ASID PID WUID CPU ELAPSED SCL PRIO RGP
-SKURTZ SSH - 0033 67109440 NA NA 04.21.51 NA NA NA
-SKURTZ TSO/E OMVS OWT O 0058 33554625 TSU08400 000.870 00.08.00 TSO 1 N/A
+USER TYPE STATUS ASID PID WUID CPU ELAPSED SCL PRIO RGP PROCS
+SKURTZ SSH - 0030 83886457 NA 000.000 04.00.02 NA NA NA 5
+SKURTZ SSH - 0070 67108897 NA 010.100 09.15.16 NA NA NA 1
+SKURTZ TSO/E OMVS OWT O 0058 33554625 TSU08400 000.870 00.08.00 TSO 1 N/A 1
```
`TYPE` is `TSO/E` for a session listed by `DISPLAY TS,A`, `SSH` for a session
@@ -17,6 +19,13 @@ is the OMVS process id of that session, matched to the TSO/E session by
address space id: an SSH session always has one, a TSO/E session only while it
owns an OMVS process, and `NA` otherwise.
+`CPU` is seconds.milliseconds, whether it comes from the TSO/E record's `CT`
+or from `CT_SECS` on the session's OMVS process. `PROCS` counts the processes
+below the session's own: the sshd and the shell and whatever they started, or
+the POSIX work a TSO/E session has running. Each fork gets its own address
+space on z/OS, which is why the count follows the pid tree rather than the
+ASID.
+
## Requirements
- z/OS with ZOAU installed at `/usr/lpp/IBM/zoautil` (v1.3 or later: the
@@ -35,9 +44,14 @@ its sidedeck imports `CODE64`.
```sh
make # builds ./whoson with ibm-clang, -m64, ASCII character mode
make check # runs it under the CEE heap checker and storage report
+make list # builds, and writes a compiler listing to whoson.listing
make install # copies it to ~/.local/bin, creating it if needed
```
+`make list` turns on Open XL 2.2's listing (`-mzos-listing=json`) and renders
+it with `ibm-clang-listing-formatter`: the pseudo assembly with offsets,
+grouped by function, which is readable without a debugger.
+
`PREFIX` defaults to `$(HOME)/.local`; pass your own to install elsewhere:
```sh
@@ -64,11 +78,15 @@ so the program does not shell out to `opercmd`:
- `DISPLAY TS,A` lists TSO/E sessions: user, status, address space id, CPU
time (`CT`), elapsed time (`ET`), job id (`WUID`), service class, priority
- and region (`RGP`). `DISPLAY TSO,...` is not a valid command on this system,
- and `DISPLAY TS,L` omits the timing fields, so `,A` is used.
+ and region (`RGP`). `USERID=` in the same record is the account, which is
+ not always the job name in the first column. `DISPLAY TSO,...` is not a
+ valid command on this system, and `DISPLAY TS,L` omits the timing fields,
+ so `,A` is used.
- `DISPLAY OMVS,A=ALL` lists OMVS processes. An `sshd` process is a session;
the account that owns the `sshd` process whose parent is the init process is
a daemon account and is dropped, which keeps started tasks out of the list.
+ The row's `CT_SECS` and `PPID` give the session its CPU time and the rest of
+ its pid tree.
The vector's `opercmd` splits its argument string into `argv` without a shell,
so the console command is passed quoted (`"D TS,A"`) as a single argument.
diff --git a/whoson.c b/whoson.c
index 1cc8ae7..87146bc 100644
--- a/whoson.c
+++ b/whoson.c
@@ -24,8 +24,10 @@
*
* A TSO/E session reports its own CPU time (CT), elapsed time (ET), job id
* (WUID/SCL/P) and region (RGP), so those columns need no second source. An
- * SSH session reports a start time instead, and its elapsed time is computed
- * from that.
+ * SSH session has no such record: its CPU time comes from CT_SECS on its own
+ * OMVS process, and its elapsed time is computed from that process's start
+ * time. The process count follows the pid tree below the session, which says
+ * how much work is running behind the login.
*/
#include <inttypes.h>
@@ -52,237 +54,234 @@
#define SCL_LEN 8
#define PRIO_LEN 4
#define RGP_LEN 12
+#define PROCS_LEN 6
#define PID_LEN 12
#define STATUS_LEN 12
#define SESSION_LEN 12
#define CMD_LEN 256
struct proc {
- char user[NAME_LEN];
- char asid[ASID_LEN];
- char start[TIME_LEN];
- int32_t pid;
- int32_t ppid;
- char cmd[CMD_LEN];
+ char user[NAME_LEN];
+ char asid[ASID_LEN];
+ char start[TIME_LEN];
+ int32_t pid;
+ int32_t ppid;
+ char cpu[CPU_LEN];
+ char cmd[CMD_LEN];
};
struct row {
- char user[NAME_LEN];
- char session[SESSION_LEN];
- char status[STATUS_LEN];
- char asid[ASID_LEN];
- char wuid[WUID_LEN];
- char cpu[CPU_LEN];
- char elapsed[TIME_LEN];
- char scl[SCL_LEN];
- char prio[PRIO_LEN];
- char rgp[RGP_LEN];
- char pid[PID_LEN];
+ char user[NAME_LEN];
+ char session[SESSION_LEN];
+ char status[STATUS_LEN];
+ char asid[ASID_LEN];
+ char wuid[WUID_LEN];
+ char cpu[CPU_LEN];
+ char elapsed[TIME_LEN];
+ char scl[SCL_LEN];
+ char prio[PRIO_LEN];
+ char rgp[RGP_LEN];
+ char procs[PROCS_LEN];
+ char pid[PID_LEN];
};
-static const char usage_text[] = "usage: whoson\n"
- "\n"
- "List logged-on users with session type (TSO/E or SSH).\n"
- "\n"
- "Options:\n"
- " -h, --help show this help and exit\n";
+static const char usage_text[] =
+ "usage: whoson\n"
+ "\n"
+ "List logged-on users with session type (TSO/E or SSH).\n"
+ "\n"
+ "Options:\n"
+ " -h, --help show this help and exit\n";
/* Write a message in the character set its destination expects. CEE converts
* stdio output from ASCII to EBCDIC when the stream is a terminal, and sshd
* converts a pipe, socket or file the other way, so neither printf nor a raw
* write() is right on both: a terminal gets ASCII, everything else EBCDIC. */
-static void emit(int fd, char *buf, size_t len)
-{
- if (!isatty(fd))
- __a2e_l(buf, len);
- while (len > 0) {
- ssize_t n = write(fd, buf, len);
+static void emit(int fd, char *buf, size_t len) {
+ if (!isatty(fd))
+ __a2e_l(buf, len);
+ while (len > 0) {
+ ssize_t n = write(fd, buf, len);
- if (n <= 0)
- return;
- buf += n;
- len -= (size_t)n;
- }
+ if (n <= 0)
+ return;
+ buf += n;
+ len -= (size_t)n;
+ }
}
-static void emit_str(int fd, const char *text)
-{
- char buf[MSG_LEN];
- size_t len = strlen(text);
+static void emit_str(int fd, const char *text) {
+ char buf[MSG_LEN];
+ size_t len = strlen(text);
- if (len >= sizeof(buf))
- len = sizeof(buf) - 1;
- memcpy(buf, text, len);
- emit(fd, buf, len);
+ if (len >= sizeof(buf))
+ len = sizeof(buf) - 1;
+ memcpy(buf, text, len);
+ emit(fd, buf, len);
}
/* Report an error on stderr; messages are short enough for one line buffer. */
-static void error_msg(const char *fmt, ...)
-{
- char text[MSG_LEN];
- va_list ap;
+static void error_msg(const char *fmt, ...) {
+ char text[MSG_LEN];
+ va_list ap;
- va_start(ap, fmt);
- vsnprintf(text, sizeof(text), fmt, ap);
- va_end(ap);
- emit_str(STDERR_FILENO, text);
+ va_start(ap, fmt);
+ vsnprintf(text, sizeof(text), fmt, ap);
+ va_end(ap);
+ emit_str(STDERR_FILENO, text);
}
-static int usage_error(const char *msg)
-{
- error_msg("whoson: %s\n%s", msg, usage_text);
- return 2;
+static int usage_error(const char *msg) {
+ error_msg("whoson: %s\n%s", msg, usage_text);
+ return 2;
}
/* Split a display line in place; returns the token count. */
-static int tokenize(char *line, char **tok)
-{
- int n = 0;
- char *p = line;
+static int tokenize(char *line, char **tok) {
+ int n = 0;
+ char *p = line;
- while (*p != '\0' && n < MAX_TOKENS) {
- while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')
- p++;
- if (*p == '\0')
- break;
- tok[n++] = p;
- while (*p != '\0' && *p != ' ' && *p != '\t' && *p != '\n' &&
- *p != '\r')
- p++;
- if (*p != '\0')
- *p++ = '\0';
- }
- return n;
+ while (*p != '\0' && n < MAX_TOKENS) {
+ while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')
+ p++;
+ if (*p == '\0')
+ break;
+ tok[n++] = p;
+ while (*p != '\0' && *p != ' ' && *p != '\t' && *p != '\n' &&
+ *p != '\r')
+ p++;
+ if (*p != '\0')
+ *p++ = '\0';
+ }
+ return n;
}
/* "hh.mm.ss", the form opercmd prints start times in. */
-static int is_hms(const char *s)
-{
- int digits = 0;
- int dots = 0;
+static int is_hms(const char *s) {
+ int digits = 0;
+ int dots = 0;
- for (; *s != '\0'; s++) {
- if (*s >= '0' && *s <= '9')
- digits++;
- else if (*s == '.')
- dots++;
- else
- return 0;
- }
- return digits == 6 && dots == 2;
+ for (; *s != '\0'; s++) {
+ if (*s >= '0' && *s <= '9')
+ digits++;
+ else if (*s == '.')
+ dots++;
+ else
+ return 0;
+ }
+ return digits == 6 && dots == 2;
}
-static int32_t hms_to_secs(const char *s)
-{
- int32_t h = 0, m = 0, sec = 0;
+static int32_t hms_to_secs(const char *s) {
+ int32_t h = 0, m = 0, sec = 0;
- if (sscanf(s, "%" SCNd32 ".%" SCNd32 ".%" SCNd32, &h, &m, &sec) != 3)
- return -1;
- return h * 3600 + m * 60 + sec;
+ if (sscanf(s, "%" SCNd32 ".%" SCNd32 ".%" SCNd32, &h, &m, &sec) != 3)
+ return -1;
+ return h * 3600 + m * 60 + sec;
}
-static void format_elapsed(const char *start, char *out, size_t outlen)
-{
- time_t now = time(NULL);
- struct tm *tm = localtime(&now);
- int32_t then, delta;
+static void format_elapsed(const char *start, char *out, size_t outlen) {
+ time_t now = time(NULL);
+ struct tm *tm = localtime(&now);
+ int32_t then, delta;
- then = hms_to_secs(start);
- if (then < 0 || tm == NULL) {
- snprintf(out, outlen, "-");
- return;
- }
- delta = (int32_t)tm->tm_hour * 3600 + (int32_t)tm->tm_min * 60 +
- (int32_t)tm->tm_sec - then;
- if (delta < 0)
- delta += 86400;
- snprintf(out, outlen, "%02" PRId32 ".%02" PRId32 ".%02" PRId32,
- delta / 3600, delta % 3600 / 60, delta % 60);
+ then = hms_to_secs(start);
+ if (then < 0 || tm == NULL) {
+ snprintf(out, outlen, "-");
+ return;
+ }
+ delta = (int32_t)tm->tm_hour * 3600 + (int32_t)tm->tm_min * 60 +
+ (int32_t)tm->tm_sec - then;
+ if (delta < 0)
+ delta += 86400;
+ snprintf(out, outlen, "%02" PRId32 ".%02" PRId32 ".%02" PRId32,
+ delta / 3600, delta % 3600 / 60, delta % 60);
}
-static void copy_field(char *dst, size_t dstlen, const char *src)
-{
- snprintf(dst, dstlen, "%s", src);
+static void copy_field(char *dst, size_t dstlen, const char *src) {
+ snprintf(dst, dstlen, "%s", src);
+}
+
+/* CPU times arrive in two shapes: DISPLAY TS reports "000.870S", seconds and
+ * milliseconds with an S suffix, and DISPLAY OMVS reports CT_SECS as plain
+ * seconds such as "669.3" or ".4". Both are printed as seconds.milliseconds
+ * so the column reads one way. */
+static void fmt_cpu(const char *in, char *out, size_t outlen) {
+ int32_t ms;
+
+ if ((in[0] < '0' || in[0] > '9') && in[0] != '.') {
+ snprintf(out, outlen, "%s", in);
+ return;
+ }
+ ms = (int32_t)(atof(in) * 1000.0 + 0.5);
+ snprintf(out, outlen, "%03" PRId32 ".%03" PRId32, ms / 1000, ms % 1000);
}
/* Parse DISPLAY OMVS,A=ALL: one 8-field row per process, each followed by an
* indented detail line naming the command. */
-static int parse_omvs(char *text, struct proc *procs, int max)
-{
- int n = 0;
- char *line = text;
- char *tok[MAX_TOKENS];
- char raw[512];
- char work[512];
+static int parse_omvs(char *text, struct proc *procs, int max) {
+ int n = 0;
+ char *line = text;
+ char *tok[MAX_TOKENS];
+ char raw[512];
+ char work[512];
- while (line != NULL && n < max) {
- char *next = strchr(line, '\n');
- int count;
+ while (line != NULL && n < max) {
+ char *next = strchr(line, '\n');
+ int count;
- if (next != NULL)
- *next++ = '\0';
- snprintf(raw, sizeof(raw), "%s", line);
- snprintf(work, sizeof(work), "%s", line);
- count = tokenize(work, tok);
- if (count == 8 && is_hms(tok[6]) && strchr(tok[5], '-') != NULL) {
- copy_field(procs[n].user, NAME_LEN, tok[0]);
- copy_field(procs[n].asid, ASID_LEN, tok[2]);
- copy_field(procs[n].start, TIME_LEN, tok[6]);
- procs[n].pid = (int32_t)strtol(tok[3], NULL, 10);
- procs[n].ppid = (int32_t)strtol(tok[4], NULL, 10);
- procs[n].cmd[0] = '\0';
- n++;
- } else if (n > 0) {
- char *cmd = strstr(raw, "CMD=");
+ if (next != NULL)
+ *next++ = '\0';
+ snprintf(raw, sizeof(raw), "%s", line);
+ snprintf(work, sizeof(work), "%s", line);
+ count = tokenize(work, tok);
+ if (count == 8 && is_hms(tok[6]) && strchr(tok[5], '-') != NULL) {
+ copy_field(procs[n].user, NAME_LEN, tok[0]);
+ copy_field(procs[n].asid, ASID_LEN, tok[2]);
+ copy_field(procs[n].start, TIME_LEN, tok[6]);
+ procs[n].pid = (int32_t)strtol(tok[3], NULL, 10);
+ procs[n].ppid = (int32_t)strtol(tok[4], NULL, 10);
+ fmt_cpu(tok[7], procs[n].cpu, CPU_LEN);
+ procs[n].cmd[0] = '\0';
+ n++;
+ } else if (n > 0) {
+ char *cmd = strstr(raw, "CMD=");
- if (cmd != NULL)
- copy_field(procs[n - 1].cmd, CMD_LEN, cmd + 4);
- }
- line = next;
- }
- return n;
+ if (cmd != NULL)
+ copy_field(procs[n - 1].cmd, CMD_LEN, cmd + 4);
+ }
+ line = next;
+ }
+ return n;
}
/* The account that owns an sshd process whose parent is the init process owns
* the listener, and with it the pre-authentication half of every session. */
-static int is_daemon(const struct proc *procs, int n, const char *user)
-{
- for (int i = 0; i < n; i++)
- if (procs[i].ppid == 1 && strstr(procs[i].cmd, "sshd") != NULL &&
- strcmp(procs[i].user, user) == 0)
- return 1;
- return 0;
+static int is_daemon(const struct proc *procs, int n, const char *user) {
+ for (int i = 0; i < n; i++)
+ if (procs[i].ppid == 1 && strstr(procs[i].cmd, "sshd") != NULL &&
+ strcmp(procs[i].user, user) == 0)
+ return 1;
+ return 0;
}
-/* Parse DISPLAY TS,A: one record per TSO/E user, five lines long --
- *
- * SKURTZ OWT A=0058 PER=NO SMC=000 PGN=N/A DMN=N/A AFF=NONE
- * CT=000.058S ET=01.22.42
- * WUID=TSU08375
- * WKL=TSO SCL=TSO P=1
- * RGP=N/A SRVR=NO QSC=NO
- *
- * A record starts at the line that carries A=; every field after that belongs
- * to the record above it.
- */
-/* Elapsed and CPU times arrive in two shapes: "01.22.42" is hours.minutes
+/* Elapsed times arrive in two shapes too: "01.22.42" is hours.minutes
* .seconds, and "019.705S" is seconds.milliseconds with an S suffix, which is
* what a session younger than an hour reports. Normalise the latter. */
-static void fmt_clock(const char *in, char *out, size_t outlen)
-{
- size_t len = strlen(in);
+static void fmt_clock(const char *in, char *out, size_t outlen) {
+ size_t len = strlen(in);
- if (len == 0 || in[len - 1] != 'S') {
- snprintf(out, outlen, "%s", in);
- return;
- }
- {
- double secs = atof(in);
- int32_t whole = (int32_t)secs;
+ if (len == 0 || in[len - 1] != 'S') {
+ snprintf(out, outlen, "%s", in);
+ return;
+ }
+ {
+ double secs = atof(in);
+ int32_t whole = (int32_t)secs;
- snprintf(out, outlen, "%02" PRId32 ".%02" PRId32 ".%02" PRId32,
- whole / 3600, whole % 3600 / 60, whole % 60);
- }
+ snprintf(out, outlen, "%02" PRId32 ".%02" PRId32 ".%02" PRId32,
+ whole / 3600, whole % 3600 / 60, whole % 60);
+ }
}
/* Parse DISPLAY TS,A: one record per TSO/E user, five lines long --
@@ -298,145 +297,164 @@ static void fmt_clock(const char *in, char *out, size_t outlen)
* user id and it is the status. Every field after that belongs to the record
* above it.
*/
-static int parse_ts(char *text, struct row *rows, int max)
-{
- int n = 0;
- char *line = text;
- char *tok[MAX_TOKENS];
- char work[512];
-
- while (line != NULL && n < max) {
- char *next = strchr(line, '\n');
- int count;
- int aat = -1;
+static int parse_ts(char *text, struct row *rows, int max) {
+ int n = 0;
+ char *line = text;
+ char *tok[MAX_TOKENS];
+ char work[512];
- if (next != NULL)
- *next++ = '\0';
- snprintf(work, sizeof(work), "%s", line);
- count = tokenize(work, tok);
- for (int i = 2; i < count; i++)
- if (strncmp(tok[i], "A=", 2) == 0) {
- aat = i;
- break;
- }
- if (aat > 0) {
- struct row *r = &rows[n];
+ while (line != NULL && n < max) {
+ char *next = strchr(line, '\n');
+ int count;
+ int aat = -1;
- memset(r, 0, sizeof(*r));
- copy_field(r->user, NAME_LEN, tok[0]);
- copy_field(r->session, sizeof(r->session), "TSO/E");
- r->status[0] = '\0';
- for (int i = 1; i < aat; i++) {
- size_t used = strlen(r->status);
+ if (next != NULL)
+ *next++ = '\0';
+ snprintf(work, sizeof(work), "%s", line);
+ count = tokenize(work, tok);
+ for (int i = 2; i < count; i++)
+ if (strncmp(tok[i], "A=", 2) == 0) {
+ aat = i;
+ break;
+ }
+ if (aat > 0) {
+ struct row *r = &rows[n];
- snprintf(r->status + used, STATUS_LEN - used,
- "%s%s", used > 0 ? " " : "", tok[i]);
- }
- copy_field(r->asid, ASID_LEN, tok[aat] + 2);
- copy_field(r->cpu, CPU_LEN, "NA");
- copy_field(r->elapsed, TIME_LEN, "NA");
- copy_field(r->scl, SCL_LEN, "NA");
- copy_field(r->prio, PRIO_LEN, "NA");
- copy_field(r->rgp, RGP_LEN, "NA");
- n++;
- }
- if (n == 0) {
- line = next;
- continue;
- }
- {
- struct row *r = &rows[n - 1];
+ memset(r, 0, sizeof(*r));
+ copy_field(r->user, NAME_LEN, tok[0]);
+ copy_field(r->session, sizeof(r->session), "TSO/E");
+ r->status[0] = '\0';
+ for (int i = 1; i < aat; i++) {
+ size_t used = strlen(r->status);
- for (int i = 0; i < count; i++) {
- if (strncmp(tok[i], "CT=", 3) == 0) {
- copy_field(r->cpu, CPU_LEN, tok[i] + 3);
- {
- size_t len = strlen(r->cpu);
+ snprintf(r->status + used, STATUS_LEN - used, "%s%s",
+ used > 0 ? " " : "", tok[i]);
+ }
+ copy_field(r->asid, ASID_LEN, tok[aat] + 2);
+ copy_field(r->cpu, CPU_LEN, "NA");
+ copy_field(r->elapsed, TIME_LEN, "NA");
+ copy_field(r->scl, SCL_LEN, "NA");
+ copy_field(r->prio, PRIO_LEN, "NA");
+ copy_field(r->rgp, RGP_LEN, "NA");
+ n++;
+ }
+ if (n == 0) {
+ line = next;
+ continue;
+ }
+ {
+ struct row *r = &rows[n - 1];
- if (len > 1 &&
- r->cpu[len - 1] == 'S')
- r->cpu[len - 1] = '\0';
- }
- } else if (strncmp(tok[i], "ET=", 3) == 0) {
- fmt_clock(tok[i] + 3, r->elapsed,
- TIME_LEN);
- } else if (strncmp(tok[i], "WUID=", 5) == 0) {
- copy_field(r->wuid, WUID_LEN, tok[i] + 5);
- } else if (strncmp(tok[i], "SCL=", 4) == 0) {
- copy_field(r->scl, SCL_LEN, tok[i] + 4);
- } else if (strncmp(tok[i], "P=", 2) == 0) {
- copy_field(r->prio, PRIO_LEN, tok[i] + 2);
- } else if (strncmp(tok[i], "RGP=", 4) == 0) {
- copy_field(r->rgp, RGP_LEN, tok[i] + 4);
- }
- }
- }
- line = next;
- }
- return n;
+ for (int i = 0; i < count; i++) {
+ if (strncmp(tok[i], "CT=", 3) == 0) {
+ fmt_cpu(tok[i] + 3, r->cpu, CPU_LEN);
+ } else if (strncmp(tok[i], "USERID=", 7) == 0) {
+ /* The first field is the job name; this one is the
+ * account, and the two can differ. */
+ copy_field(r->user, NAME_LEN, tok[i] + 7);
+ } else if (strncmp(tok[i], "ET=", 3) == 0) {
+ fmt_clock(tok[i] + 3, r->elapsed, TIME_LEN);
+ } else if (strncmp(tok[i], "WUID=", 5) == 0) {
+ copy_field(r->wuid, WUID_LEN, tok[i] + 5);
+ } else if (strncmp(tok[i], "SCL=", 4) == 0) {
+ copy_field(r->scl, SCL_LEN, tok[i] + 4);
+ } else if (strncmp(tok[i], "P=", 2) == 0) {
+ copy_field(r->prio, PRIO_LEN, tok[i] + 2);
+ } else if (strncmp(tok[i], "RGP=", 4) == 0) {
+ copy_field(r->rgp, RGP_LEN, tok[i] + 4);
+ }
+ }
+ }
+ line = next;
+ }
+ return n;
}
/* PID of the OMVS process in this address space, or "NA" for a TSO/E
* session that has no OMVS process at all. A session that has one is
* labelled "TSO/E OMVS", so it is obvious that a TSO/E logon is behind it. */
-static void set_pid(const struct proc *procs, int nprocs, struct row *r)
-{
- for (int i = 0; i < nprocs; i++) {
- if (strcmp(procs[i].asid, r->asid) == 0) {
- snprintf(r->pid, PID_LEN, "%" PRId32, procs[i].pid);
- copy_field(r->session, sizeof(r->session),
- "TSO/E OMVS");
- return;
- }
- }
- copy_field(r->pid, PID_LEN, "NA");
+static void set_pid(const struct proc *procs, int nprocs, struct row *r) {
+ for (int i = 0; i < nprocs; i++) {
+ if (strcmp(procs[i].asid, r->asid) == 0) {
+ snprintf(r->pid, PID_LEN, "%" PRId32, procs[i].pid);
+ copy_field(r->session, sizeof(r->session), "TSO/E OMVS");
+ return;
+ }
+ }
+ copy_field(r->pid, PID_LEN, "NA");
}
static int add_ssh_rows(const struct proc *procs, int nprocs, struct row *rows,
- int nrows, int max)
-{
- for (int i = 0; i < nprocs && nrows < max; i++) {
- if (strstr(procs[i].cmd, "sshd") == NULL)
- continue;
- if (is_daemon(procs, nprocs, procs[i].user))
- continue;
- copy_field(rows[nrows].user, NAME_LEN, procs[i].user);
- copy_field(rows[nrows].session, sizeof(rows[nrows].session), "SSH");
- copy_field(rows[nrows].status, sizeof(rows[nrows].status), "-");
- copy_field(rows[nrows].asid, ASID_LEN, procs[i].asid);
- copy_field(rows[nrows].wuid, WUID_LEN, "NA");
- copy_field(rows[nrows].cpu, CPU_LEN, "NA");
- copy_field(rows[nrows].scl, SCL_LEN, "NA");
- copy_field(rows[nrows].prio, PRIO_LEN, "NA");
- copy_field(rows[nrows].rgp, RGP_LEN, "NA");
- snprintf(rows[nrows].pid, PID_LEN, "%" PRId32, procs[i].pid);
- format_elapsed(procs[i].start, rows[nrows].elapsed, TIME_LEN);
- nrows++;
- }
- return nrows;
+ int nrows, int max) {
+ for (int i = 0; i < nprocs && nrows < max; i++) {
+ if (strstr(procs[i].cmd, "sshd") == NULL)
+ continue;
+ if (is_daemon(procs, nprocs, procs[i].user))
+ continue;
+ copy_field(rows[nrows].user, NAME_LEN, procs[i].user);
+ copy_field(rows[nrows].session, sizeof(rows[nrows].session), "SSH");
+ copy_field(rows[nrows].status, sizeof(rows[nrows].status), "-");
+ copy_field(rows[nrows].asid, ASID_LEN, procs[i].asid);
+ copy_field(rows[nrows].wuid, WUID_LEN, "NA");
+ copy_field(rows[nrows].cpu, CPU_LEN, procs[i].cpu);
+ copy_field(rows[nrows].scl, SCL_LEN, "NA");
+ copy_field(rows[nrows].prio, PRIO_LEN, "NA");
+ copy_field(rows[nrows].rgp, RGP_LEN, "NA");
+ snprintf(rows[nrows].pid, PID_LEN, "%" PRId32, procs[i].pid);
+ format_elapsed(procs[i].start, rows[nrows].elapsed, TIME_LEN);
+ nrows++;
+ }
+ return nrows;
+}
+
+/* How many processes the session is running: its own OMVS process and every
+ * child below it. Each fork gets its own address space on z/OS, so the
+ * session's shell is not in the sshd's ASID and cannot be found that way. */
+static int32_t parent_of(const struct proc *procs, int nprocs, int32_t pid) {
+ for (int i = 0; i < nprocs; i++)
+ if (procs[i].pid == pid)
+ return procs[i].ppid;
+ return -1;
+}
+
+static int count_procs(const struct proc *procs, int nprocs, int32_t root) {
+ int count = 0;
+
+ if (root <= 0)
+ return 0;
+ for (int i = 0; i < nprocs; i++) {
+ int32_t pid = procs[i].pid;
+
+ for (int hops = 0; hops < nprocs && pid > 0; hops++) {
+ if (pid == root) {
+ count++;
+ break;
+ }
+ pid = parent_of(procs, nprocs, pid);
+ }
+ }
+ return count;
}
-static int compare_rows(const void *a, const void *b)
-{
- const struct row *x = a;
- const struct row *y = b;
- int cmp = strcmp(x->user, y->user);
+static int compare_rows(const void *a, const void *b) {
+ const struct row *x = a;
+ const struct row *y = b;
+ int cmp = strcmp(x->user, y->user);
- if (cmp != 0)
- return cmp;
- cmp = strcmp(x->session, y->session);
- if (cmp != 0)
- return cmp;
- return strcmp(x->asid, y->asid);
+ if (cmp != 0)
+ return cmp;
+ cmp = strcmp(x->session, y->session);
+ if (cmp != 0)
+ return cmp;
+ return strcmp(x->asid, y->asid);
}
/* Convert EBCDIC text to ASCII in place, leaving ASCII text alone: with
* _TAG_REDIR_OUT=txt in the environment opercmd answers in ASCII already.
* EBCDIC console text breaks lines with 0x15 and contains no '\n'. */
-static void to_ascii(char *buf, size_t len)
-{
- if (memchr(buf, '\n', len) == NULL)
- __e2a_l(buf, len);
+static void to_ascii(char *buf, size_t len) {
+ if (memchr(buf, '\n', len) == NULL)
+ __e2a_l(buf, len);
}
/* Run one display; returns 0 and a NUL-terminated copy of stdout, or -1.
@@ -444,128 +462,125 @@ static void to_ascii(char *buf, size_t len)
* one argv element. The display is converted to ASCII here before the parser
* sees it. */
static int run_display(zoautil_vector4_t *vector, const char *args,
- zoautil_options_t options, char **out)
-{
- zoau_response *response = NULL;
- char parms[MAX_ARGS];
- size_t len;
- int rc;
+ zoautil_options_t options, char **out) {
+ zoau_response *response = NULL;
+ char parms[MAX_ARGS];
+ size_t len;
+ int rc;
- *out = NULL;
- snprintf(parms, sizeof(parms), "\"%s\"", args);
- len = strlen(parms);
- rc = vector->opercmd(parms, len, &response, options);
- if (rc != 0 || response == NULL) {
- error_msg("whoson: %s failed, rc = %d\n", args, rc);
- if (response != NULL)
- error_msg("whoson: cmd_rc = %d, stdout_len = %d, "
- "stderr_len = %d\n", response->cmd_rc,
- response->stdout_len,
- response->stderr_len);
- if (response != NULL && response->stderr_response != NULL &&
- response->stderr_len > 0) {
- char text[MSG_LEN];
+ *out = NULL;
+ snprintf(parms, sizeof(parms), "\"%s\"", args);
+ len = strlen(parms);
+ rc = vector->opercmd(parms, len, &response, options);
+ if (rc != 0 || response == NULL) {
+ error_msg("whoson: %s failed, rc = %d\n", args, rc);
+ if (response != NULL)
+ error_msg("whoson: cmd_rc = %d, stdout_len = %d, "
+ "stderr_len = %d\n",
+ response->cmd_rc, response->stdout_len,
+ response->stderr_len);
+ if (response != NULL && response->stderr_response != NULL &&
+ response->stderr_len > 0) {
+ char text[MSG_LEN];
- to_ascii(response->stderr_response,
- (size_t)response->stderr_len);
- snprintf(text, sizeof(text), "%.*s\n",
- response->stderr_len,
- response->stderr_response);
- emit_str(STDERR_FILENO, text);
- }
- if (response != NULL)
- free_response(response);
- return -1;
- }
- if (response->stdout_response == NULL || response->stdout_len <= 0) {
- error_msg("whoson: %s returned no output\n", args);
- free_response(response);
- return -1;
- }
- *out = malloc((size_t)response->stdout_len + 1);
- if (*out == NULL) {
- error_msg("whoson: out of memory\n");
- free_response(response);
- return -1;
- }
- memcpy(*out, response->stdout_response, (size_t)response->stdout_len);
- /* This program is ASCII whatever the display came back as. */
- to_ascii(*out, (size_t)response->stdout_len);
- (*out)[response->stdout_len] = '\0';
- free_response(response);
- return 0;
+ to_ascii(response->stderr_response, (size_t)response->stderr_len);
+ snprintf(text, sizeof(text), "%.*s\n", response->stderr_len,
+ response->stderr_response);
+ emit_str(STDERR_FILENO, text);
+ }
+ if (response != NULL)
+ free_response(response);
+ return -1;
+ }
+ if (response->stdout_response == NULL || response->stdout_len <= 0) {
+ error_msg("whoson: %s returned no output\n", args);
+ free_response(response);
+ return -1;
+ }
+ *out = malloc((size_t)response->stdout_len + 1);
+ if (*out == NULL) {
+ error_msg("whoson: out of memory\n");
+ free_response(response);
+ return -1;
+ }
+ memcpy(*out, response->stdout_response, (size_t)response->stdout_len);
+ /* This program is ASCII whatever the display came back as. */
+ to_ascii(*out, (size_t)response->stdout_len);
+ (*out)[response->stdout_len] = '\0';
+ free_response(response);
+ return 0;
}
-int main(int argc, char *argv[])
-{
- static struct proc procs[MAX_PROCS];
- static struct row rows[MAX_ROWS];
- static char out[(MAX_ROWS + 1) * 128];
- size_t used;
- zoautil_options_t options = {
- ZOAUTIL_EYECATCHER,
- VERSION_CURRENT,
- FORMAT_TEXT
- };
- zoautil_vector4_t *vector;
- char *ts_text = NULL;
- char *omvs_text = NULL;
- unsigned int vrm;
- int nprocs, nrows = 0;
- int rc = 1;
+int main(int argc, char *argv[]) {
+ static struct proc procs[MAX_PROCS];
+ static struct row rows[MAX_ROWS];
+ static char out[(MAX_ROWS + 1) * 128];
+ size_t used;
+ zoautil_options_t options = {ZOAUTIL_EYECATCHER, VERSION_CURRENT,
+ FORMAT_TEXT};
+ zoautil_vector4_t *vector;
+ char *ts_text = NULL;
+ char *omvs_text = NULL;
+ unsigned int vrm;
+ int nprocs, nrows = 0;
+ int rc = 1;
- if (argc > 1) {
- if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) {
- emit_str(STDOUT_FILENO, usage_text);
- return 0;
- }
- return usage_error("unknown argument");
- }
+ if (argc > 1) {
+ if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) {
+ emit_str(STDOUT_FILENO, usage_text);
+ return 0;
+ }
+ return usage_error("unknown argument");
+ }
- vrm = version();
- if (vrm < VERSION_CURRENT) {
- error_msg("whoson: ZOAU shared library is too old: 0x%x < 0x%x\n",
- vrm, VERSION_CURRENT);
- return 1;
- }
- vector = init(vrm);
- if (vector == NULL) {
- error_msg("whoson: cannot get the ZOAU vector table\n");
- return 1;
- }
- if (run_display(vector, "D OMVS,A=ALL", options, &omvs_text) != 0)
- goto out;
- if (run_display(vector, "D TS,A", options, &ts_text) != 0)
- goto out;
+ vrm = version();
+ if (vrm < VERSION_CURRENT) {
+ error_msg("whoson: ZOAU shared library is too old: 0x%x < 0x%x\n", vrm,
+ VERSION_CURRENT);
+ return 1;
+ }
+ vector = init(vrm);
+ if (vector == NULL) {
+ error_msg("whoson: cannot get the ZOAU vector table\n");
+ return 1;
+ }
+ if (run_display(vector, "D OMVS,A=ALL", options, &omvs_text) != 0)
+ goto out;
+ if (run_display(vector, "D TS,A", options, &ts_text) != 0)
+ goto out;
- nprocs = parse_omvs(omvs_text, procs, MAX_PROCS);
- nrows = parse_ts(ts_text, rows, MAX_ROWS);
- for (int i = 0; i < nrows; i++)
- set_pid(procs, nprocs, &rows[i]);
- nrows = add_ssh_rows(procs, nprocs, rows, nrows, MAX_ROWS);
- qsort(rows, (size_t)nrows, sizeof(rows[0]), compare_rows);
+ nprocs = parse_omvs(omvs_text, procs, MAX_PROCS);
+ nrows = parse_ts(ts_text, rows, MAX_ROWS);
+ for (int i = 0; i < nrows; i++)
+ set_pid(procs, nprocs, &rows[i]);
+ nrows = add_ssh_rows(procs, nprocs, rows, nrows, MAX_ROWS);
+ for (int i = 0; i < nrows; i++)
+ snprintf(
+ rows[i].procs, PROCS_LEN, "%d",
+ count_procs(procs, nprocs, (int32_t)strtol(rows[i].pid, NULL, 10)));
+ qsort(rows, (size_t)nrows, sizeof(rows[0]), compare_rows);
- /* One line is far under 128 bytes, and the buffer holds the header plus
- * one per row, so these snprintf calls cannot truncate. */
- used = (size_t)snprintf(out, sizeof(out),
- "%-8s %-12s %-6s %-4s %-11s %-10s %-8s %-10s "
- "%-4s %-4s %-6s\n",
- "USER", "TYPE", "STATUS", "ASID", "PID", "WUID",
- "CPU", "ELAPSED", "SCL", "PRIO", "RGP");
- for (int i = 0; i < nrows; i++)
- used += (size_t)snprintf(out + used, sizeof(out) - used,
- "%-8s %-12s %-6s %-4s %-11s %-10s "
- "%-8s %-10s %-4s %-4s %-6s\n",
- rows[i].user, rows[i].session,
- rows[i].status, rows[i].asid, rows[i].pid,
- rows[i].wuid, rows[i].cpu, rows[i].elapsed,
- rows[i].scl, rows[i].prio, rows[i].rgp);
+ /* One line is far under 128 bytes, and the buffer holds the header plus
+ * one per row, so these snprintf calls cannot truncate. */
+ used = (size_t)snprintf(out, sizeof(out),
+ "%-8s %-12s %-6s %-4s %-11s %-10s %-8s %-10s "
+ "%-4s %-4s %-6s %-5s\n",
+ "USER", "TYPE", "STATUS", "ASID", "PID", "WUID",
+ "CPU", "ELAPSED", "SCL", "PRIO", "RGP", "PROCS");
+ for (int i = 0; i < nrows; i++)
+ used += (size_t)snprintf(out + used, sizeof(out) - used,
+ "%-8s %-12s %-6s %-4s %-11s %-10s "
+ "%-8s %-10s %-4s %-4s %-6s %-5s\n",
+ rows[i].user, rows[i].session, rows[i].status,
+ rows[i].asid, rows[i].pid, rows[i].wuid,
+ rows[i].cpu, rows[i].elapsed, rows[i].scl,
+ rows[i].prio, rows[i].rgp, rows[i].procs);
- emit(STDOUT_FILENO, out, used);
- rc = 0;
+ emit(STDOUT_FILENO, out, used);
+ rc = 0;
out:
- free(ts_text);
- free(omvs_text);
- term(vector);
- return rc;
+ free(ts_text);
+ free(omvs_text);
+ term(vector);
+ return rc;
}