diff options
| author | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-28 14:39:47 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-28 14:39:47 -0600 |
| commit | 18e2f56fb315186276990d75ce6629a1b0fd9daf (patch) | |
| tree | 584be76eed2f8d9ee466c2a58ae9522f5ab29c1d | |
| parent | 939f3ec1bd064a0789ac0fab53fc59af5b49d8d1 (diff) | |
| parent | 8f41c5a331d07580c9faf0adec41b314a97cba93 (diff) | |
| download | whoson-18e2f56fb315186276990d75ce6629a1b0fd9daf.tar.gz | |
Merge pull request #22 from skurtz97/fix/code-review-939f3ec
Fix the code review findings for 939f3ec
| -rw-r--r-- | CONTRIBUTING.md | 4 | ||||
| -rw-r--r-- | Makefile | 5 | ||||
| -rw-r--r-- | README.md | 8 | ||||
| -rw-r--r-- | buildenv | 6 | ||||
| -rw-r--r-- | whoson.c | 104 |
5 files changed, 65 insertions, 62 deletions
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 1ddabd7..4984110 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -36,7 +36,9 @@ syntax (`?=` is out), and let `make -n` stay free of side effects. - Warnings are bugs. The tree builds with `-Wall -Wextra -Wpedantic -Wshadow -Wconversion`; a warning from any of them, or from `clang-tidy` with - `.clang-tidy`, gets fixed rather than silenced. + `.clang-tidy`, gets fixed rather than silenced. `.clang-tidy` needs the z/OS + headers (`<sys/ps.h>`), so only a z/OS build environment can run it; the + Ubuntu CI job enforces clang-format, not clang-tidy. - `clang-format -i whoson.c` before committing; `.clang-format` is the authority, and CI checks the result against clang-format 23.1.5. - Comments say why, not what, and the file header says what the control-block @@ -36,7 +36,10 @@ BINDIR = $(PREFIX)/bin # reports it through --version. VERSION = 1.1.0 CCDIR = /usr/lpp/IBM/cnw/v2r2/openxl/bin -CC = $(CCDIR)/ibm-clang +CCNAME = ibm-clang +CC = $(CCDIR)/$(CCNAME) +# The listing formatter ships beside the compiler; the zopen buildenv overrides +# CCDIR/CCNAME, so the binary and the listing always use one toolchain. LISTFMT = $(CCDIR)/ibm-clang-listing-formatter CPPFLAGS = -D_XOPEN_SOURCE=600 -D_POSIX_C_SOURCE=200809L -D_ALL_SOURCE \ -D_EXT -D_OPEN_SYS -D_OPEN_SYS_FILE_EXT \ @@ -30,10 +30,10 @@ Three types of session are listed, and nothing else: machinery, different logon procedure. Any other procedure z/OSMF is configured with can be added to `WHOSON_ZOSMF_PROCS`. - `SSH` — a process whose parent is an `sshd` (or `sshd-session`) and which is - not itself an `sshd`. sshd forks that worker per connection; the listener - has no `sshd` parent. A connection that has not authenticated yet is - excluded by the same rule, and a port-forward-only connection has no such - child, so neither is listed. + not itself an `sshd` and not the sftp subsystem (`sftp-server`). sshd forks + that worker per connection; the listener has no `sshd` parent. A connection + that has not authenticated yet is excluded by the same rule, and a + port-forward-only connection has no such child, so neither is listed. Started tasks, OMVS daemons and the processes inside a session are not sessions and are not listed: `TCPIP`, `CEA`, `IZUSVR` and the shells behind an @@ -28,11 +28,11 @@ export ZOPEN_CONFIGURE="skip" # would replace them. The port only passes the compiler zopen detected. # These strings are eval'd at build time, when PATH and CC are set. export ZOPEN_MAKE_MINIMAL="1" -export ZOPEN_MAKE_OPTS='CC=${CC:-clang}' +export ZOPEN_MAKE_OPTS='CCDIR="$(dirname "${CC:?zopen did not set CC}")" CCNAME="$(basename "${CC:?zopen did not set CC}")"' export ZOPEN_CHECK="make" -export ZOPEN_CHECK_OPTS='check CC=${CC:-clang}' +export ZOPEN_CHECK_OPTS='check CCDIR="$(dirname "${CC:?zopen did not set CC}")" CCNAME="$(basename "${CC:?zopen did not set CC}")"' export ZOPEN_CHECK_MINIMAL="1" -export ZOPEN_INSTALL_OPTS='install CC=${CC:-clang} PREFIX=$ZOPEN_INSTALL_DIR' +export ZOPEN_INSTALL_OPTS='install CCDIR="$(dirname "${CC:?zopen did not set CC}")" CCNAME="$(basename "${CC:?zopen did not set CC}")" PREFIX=$ZOPEN_INSTALL_DIR' export ZOPEN_INSTALL_MINIMAL="1" # `make check` runs the binary once: its only job is to prove the program @@ -27,7 +27,9 @@ * this file may be cached across the walk, because the blocks change under * it. Every read of a control block is guarded: an address space can end * between the ASVT read and the ASCB read, and a stale pointer must skip one - * row, not abend the program. + * row, not abend the program. The handler assumes z/OS raises SIGSEGV + * synchronously for an unmapped common-storage read; ISO C leaves that + * implementation-defined, but the platform does it. * * Character conversion follows the zopen conventions: source and output are * ISO8859-1 (CCSID 819) and the descriptor's tag says what the destination @@ -141,7 +143,6 @@ struct space { /* One output row. TSO rows are built from a space; SSH rows from a process. */ struct row { unsigned asid; - bool tso; uint64_t us; pid_t pid; const char *type; @@ -187,9 +188,13 @@ static const char usage_text[] = static sigjmp_buf fault_env; static volatile sig_atomic_t fault_armed; +/* Only fcntl(), which is safe in a signal handler. */ +static void restore_cvt(void); + static void on_fault(int sig) { if (fault_armed) siglongjmp(fault_env, 1); + restore_cvt(); _exit(128 + sig); } @@ -228,17 +233,23 @@ static uint64_t read_u64(uint32_t addr) { return *(const volatile uint64_t *)(uintptr_t)addr; } -static void write_all(int fd, const char *buf, size_t len) { +/* Set by a failed write, so main can exit nonzero on truncated output. */ +static bool output_failed; + +static int write_all(int fd, const char *buf, size_t len) { while (len > 0) { ssize_t n = write(fd, buf, len); if (n < 0 && errno == EINTR) continue; - if (n <= 0) - return; + if (n <= 0) { + output_failed = true; + return -1; + } buf += n; len -= (size_t)n; } + return 0; } /* Tag fd with ccsid as text. zopen's setccsid(): an untagged file created by @@ -287,6 +298,8 @@ static void save_cvt(int fd) { q.cvtcmd = QUERYCVT; if (fcntl(fd, F_CONTROL_CVT, &q) != 0) return; + if (nsaved >= sizeof(saved_cvt) / sizeof(saved_cvt[0])) + return; /* main saves stdout and stderr only */ saved_cvt[nsaved].fd = fd; saved_cvt[nsaved].v = q; nsaved++; @@ -398,42 +411,18 @@ static void read_name(uint32_t ptr, char *dst, size_t dstlen) { dst[n] = '\0'; } -static void read_tagged_name(uint32_t addr, char *dst, size_t dstlen) { - unsigned char raw[8]; - size_t i; - size_t n = 0; - - if (dstlen == 0) - return; - dst[0] = '\0'; - for (i = 0; i < sizeof(raw); i++) - raw[i] = read_u8(addr + (uint32_t)i); - __e2a_l((char *)raw, sizeof(raw)); - for (i = 0; i < sizeof(raw) && n + 1 < dstlen; i++) { - if (raw[i] == ' ' || raw[i] == '\0') - break; - if (raw[i] < 0x20 || raw[i] > 0x7E) - break; - dst[n++] = (char)raw[i]; - } - dst[n] = '\0'; -} - /* Read one ASCB. Returns 0 and fills *out on success; -1 skips it. */ static int scan_one(unsigned idx, uint32_t entry, uint32_t entries, struct space *out) { - struct space tmp; + struct space tmp = {0}; uint32_t ascb = entry & 0x7FFFFFFFu; uint16_t asid; uint32_t cscb; uint64_t tod; bool tso; - char key[NAME_LEN]; - char cls[JOB_LEN]; + char key[NAME_LEN] = {0}; + char cls[JOB_LEN] = {0}; - memset(&tmp, 0, sizeof(tmp)); - memset(key, 0, sizeof(key)); - memset(cls, 0, sizeof(cls)); fault_armed = 1; if (sigsetjmp(fault_env, 1) != 0) { fault_armed = 0; @@ -456,10 +445,10 @@ static int scan_one(unsigned idx, uint32_t entry, uint32_t entries, tod += read_u64(assb + (uint32_t)offsetof(struct assb, assbasst)); } if (cscb != 0) { - read_tagged_name(cscb + (uint32_t)offsetof(struct cscb, chkey), key, - sizeof(key)); - read_tagged_name(cscb + (uint32_t)offsetof(struct cscb, chcls), cls, - sizeof(cls)); + read_name(cscb + (uint32_t)offsetof(struct cscb, chkey), key, + sizeof(key)); + read_name(cscb + (uint32_t)offsetof(struct cscb, chcls), cls, + sizeof(cls)); if (read_u8(cscb + (uint32_t)offsetof(struct cscb, chtrkid)) == 0x01) tso = true; } @@ -519,8 +508,8 @@ fail: return -1; } -static int ascb_scan(uint32_t entries, unsigned maxu, struct space *snap, - unsigned *found) { +static void ascb_scan(uint32_t entries, unsigned maxu, struct space *snap, + unsigned *found) { unsigned i; *found = 0; @@ -532,7 +521,6 @@ static int ascb_scan(uint32_t entries, unsigned maxu, struct space *snap, if (scan_one(i, entry, entries, &snap[i]) == 0) (*found)++; } - return 0; } static FILE *ps_open(void) { @@ -634,8 +622,14 @@ static int add_pid_asid(struct pid_asid **map, size_t *n, size_t *cap, return 0; } +/* ps prints one header line ("PID ASID" on current releases); anything else + * that does not parse is an error, not a line to drop. */ static bool is_header(const char *s) { - return !isdigit((unsigned char)s[0]); + while (isspace((unsigned char)*s)) + s++; + return strncmp(s, "PID", 3) == 0 && + (s[3] == '\0' || isspace((unsigned char)s[3])) && + strstr(s, "ASID") != NULL; } static int ps_scan(struct pid_asid **map, size_t *count) { @@ -670,10 +664,12 @@ static int ps_scan(struct pid_asid **map, size_t *count) { } if (mode == 0) text_to_ascii(buf, len); + /* The buffer is ASCII by now: mode 1 and 2 were read as ASCII, and mode 0 + * was converted above, which also turns EBCDIC NL into '\n'. */ for (i = 0; i <= len; i++) { char c = i < len ? buf[i] : '\n'; - if (c != '\n' && c != 0x15 && c != 0x25) + if (c != '\n' && c != '\r') continue; buf[i < len ? i : len] = '\0'; if (buf[start] != '\0') { @@ -736,10 +732,8 @@ static int cmp_proc(const void *a, const void *b) { static const struct proc *find_proc(const struct proc *procs, size_t n, pid_t pid) { - struct proc key; + struct proc key = {.pid = pid}; - memset(&key, 0, sizeof(key)); - key.pid = pid; return bsearch(&key, procs, n, sizeof(*procs), cmp_proc); } @@ -750,6 +744,13 @@ static bool is_sshd(const char *path) { return strcmp(base, "sshd") == 0 || strcmp(base, "sshd-session") == 0; } +/* The sftp subsystem also runs as a child of sshd-session, but it is a file + * transfer, not a login. */ +static bool is_sftp_server(const char *name) { + return strcmp(name, "sftp-server") == 0 || + strcmp(name, "internal-sftp") == 0; +} + static int append_proc(struct proc **procs, size_t *n, size_t *cap, const W_PSPROC *ps, const char *path) { const char *base; @@ -993,7 +994,6 @@ static int collect(struct row *rows, size_t maxrows, const struct space *snap, if (!snap[i].tso) continue; rows[n].asid = i + 1; - rows[n].tso = true; rows[n].us = snap[i].us; rows[n].pid = 0; rows[n].type = logon_is_zosmf(snap[i].logon) ? "z/OSMF" : "TSO/E"; @@ -1012,7 +1012,8 @@ static int collect(struct row *rows, size_t maxrows, const struct space *snap, const char *job = "-"; parent = find_proc(procs, nprocs, procs[k].ppid); - if (parent == NULL || !is_sshd(parent->name) || is_sshd(procs[k].name)) + if (parent == NULL || !is_sshd(parent->name) || + is_sshd(procs[k].name) || is_sftp_server(procs[k].name)) continue; pa = find_asid(map, nmap, procs[k].pid); if (pa != NULL && pa->asid >= 1 && pa->asid <= maxu) { @@ -1022,7 +1023,6 @@ static int collect(struct row *rows, size_t maxrows, const struct space *snap, job = snap[asid - 1].job; } rows[n].asid = asid; - rows[n].tso = false; rows[n].us = us; rows[n].pid = procs[k].pid; rows[n].type = "SSH"; @@ -1046,7 +1046,6 @@ int main(int argc, char **argv) { uint32_t entries; int nrows; int arg; - size_t space_bytes; save_cvt(STDOUT_FILENO); save_cvt(STDERR_FILENO); @@ -1054,11 +1053,11 @@ int main(int argc, char **argv) { for (arg = 1; arg < argc; arg++) { if (strcmp(argv[arg], "-h") == 0 || strcmp(argv[arg], "--help") == 0) { emit_str(STDOUT_FILENO, usage_text); - return 0; + return output_failed ? 1 : 0; } else if (strcmp(argv[arg], "-V") == 0 || strcmp(argv[arg], "--version") == 0) { emit_str(STDOUT_FILENO, "whoson " WHOSON_VERSION "\n"); - return 0; + return output_failed ? 1 : 0; } else { usage_error("whoson: unknown argument: %s", argv[arg]); return 1; @@ -1070,8 +1069,7 @@ int main(int argc, char **argv) { error_msg("whoson: cannot read the ASVT\n"); return 1; } - space_bytes = (sizeof(struct space) + 15u) & ~(size_t)15u; - snap = calloc(1, (size_t)maxu * space_bytes); + snap = calloc((size_t)maxu, sizeof(*snap)); if (snap == NULL) { unguard(); error_msg("whoson: out of memory for %u address spaces\n", maxu); @@ -1113,5 +1111,5 @@ int main(int argc, char **argv) { free(procs); free(map); free(snap); - return 0; + return output_failed ? 1 : 0; } |
