diff --git a/README.md b/README.md index 912fe35..6059c1c 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,34 @@ Error messages go to stderr; stdout carries only the console output of the ps2. ### PS2LINK COMMANDS +* `health` + +A read-only health check that performs two independent EE/IOP `host:` +round trips, compatible with existing PS2Link binaries: + +```sh +ps2client -h 192.168.31.10 -t 3 health +``` + +Each round trip sends a `DUMP_REG` request for two INTC registers to the +EE command handler, then verifies that the PS2Link IOP file service +completes the `host:` open, write and close cycle. The register data is +discarded. Exit status 0 means both checks succeeded; status 69 means +either check failed. `-t` specifies each probe's timeout in seconds +(default 2, range 1–120). + +Unlike ordinary ICMP `ping`, this requires the EE command handler and +IOP host-file service to be responsive. Existing `ps2client ping` +already tests one such EE round trip; `health` explicitly tests two +fresh connections and reports them separately. It does **not** prove +that the debugged user thread is making progress, nor that every PS2Link +service is healthy. Neither check resets, changes EE state, or launches +an ELF. + +Like `ping`, `health` temporarily opens the single `host:` service +connection. Do not run it concurrently with another `ps2client` session +serving application files, since that connection can be displaced. + * `reset` Send an unacknowledged UDP reset request to ps2link. A zero exit code diff --git a/src/ps2client.c b/src/ps2client.c index 3dd4d2a..bb01fdd 100644 --- a/src/ps2client.c +++ b/src/ps2client.c @@ -21,7 +21,7 @@ // The commands: their minimum number of arguments, what they need from the // connection, and whether they wait for output afterwards. enum { CMD_RESET, CMD_RESETCHECK, CMD_EXECIOP, CMD_EXECEE, CMD_POWEROFF, CMD_SCRDUMP, CMD_NETDUMP, CMD_DUMPMEM, CMD_STARTVU, - CMD_STOPVU, CMD_DUMPREG, CMD_GSEXEC, CMD_WRITEMEM, CMD_IOPEXCEP, CMD_LISTEN, CMD_PING, CMD_CONSOLE }; + CMD_STOPVU, CMD_DUMPREG, CMD_GSEXEC, CMD_WRITEMEM, CMD_IOPEXCEP, CMD_LISTEN, CMD_PING, CMD_HEALTH, CMD_CONSOLE }; #define NEEDS_HOST 1 // ps2link reads or writes host: files, so wait until it can #define NO_WAIT 2 // exit right after sending the command @@ -43,6 +43,7 @@ { "iopexcep", CMD_IOPEXCEP, 0, NO_WAIT }, { "listen", CMD_LISTEN, 0, 0 }, { "ping", CMD_PING, 0, NEEDS_HOST }, + { "health", CMD_HEALTH, 0, NO_WAIT }, { "console", CMD_CONSOLE, 0, 0 }, }; @@ -156,6 +157,10 @@ if (argc != 0) { fprintf(stderr, "Error: resetcheck takes no arguments.\n"); return PS2CLIENT_EXIT_USAGE; } return ps2client_resetcheck(executable, hostname, timeout < 0 ? 10 : timeout); } + if (commands[command].id == CMD_HEALTH) { + if (argc != 0 || marker != NULL) { fprintf(stderr, "Error: health takes no arguments or marker.\n"); return PS2CLIENT_EXIT_USAGE; } + return ps2client_health(executable, hostname, timeout < 0 ? 2 : timeout); + } // Commands that exit right away need neither console nor request service. // Keep listen's existing host: service; console only receives UDP logs. diff --git a/src/resetcheck.c b/src/resetcheck.c index d73a942..b2d62af 100644 --- a/src/resetcheck.c +++ b/src/resetcheck.c @@ -17,10 +17,12 @@ /* Separate processes avoid changing ps2client's legacy network/thread state. */ static int run_command(const char *executable, const char *hostname, - const char *name, int quiet) + const char *name, int timeout_seconds, int quiet) { + char timeout_text[16]; + snprintf(timeout_text, sizeof(timeout_text), "%d", timeout_seconds); char *const args[] = { - (char *)executable, "-h", (char *)hostname, "-t", "1", + (char *)executable, "-h", (char *)hostname, "-t", timeout_text, (char *)name, NULL }; #ifdef _WIN32 @@ -65,13 +67,13 @@ int ps2client_resetcheck(const char *executable, const char *hostname, } fprintf(stderr, "resetcheck: checking the EE/IOP round trip before RESET...\n"); - if (run_command(executable, hostname, "ping", 1) != PS2CLIENT_EXIT_OK) { + if (run_command(executable, hostname, "ping", 1, 1) != PS2CLIENT_EXIT_OK) { fprintf(stderr, "resetcheck: PS2Link is not responding; RESET was NOT sent.\n"); return PS2CLIENT_EXIT_NOCONNECT; } fprintf(stderr, "resetcheck: sending one legacy UDP RESET request...\n"); - status = run_command(executable, hostname, "reset", 1); + status = run_command(executable, hostname, "reset", 1, 1); if (status != PS2CLIENT_EXIT_OK) { fprintf(stderr, "resetcheck: failed to send RESET (%d).\n", status); return PS2CLIENT_EXIT_NOCONNECT; @@ -82,7 +84,7 @@ int ps2client_resetcheck(const char *executable, const char *hostname, * not proof that the reset command was processed. */ deadline = time_ms() + (long long)timeout_seconds * 1000; while (time_ms() < deadline) { - status = run_command(executable, hostname, "ping", 1); + status = run_command(executable, hostname, "ping", 1, 1); if (status != PS2CLIENT_EXIT_OK) { if (!saw_outage) { saw_outage = 1; @@ -101,3 +103,33 @@ int ps2client_resetcheck(const char *executable, const char *hostname, fprintf(stderr, "resetcheck: PS2Link did not recover; reset UNCONFIRMED.\n"); return PS2CLIENT_EXIT_TIMEOUT; } + +/* Each child gets its own TCP host: session and sends a read-only EE + * DUMP_REG command. Merely answering ICMP or accepting IOP TCP does not + * satisfy either probe: the EE command thread must complete both. */ +int ps2client_health(const char *executable, const char *hostname, + int timeout_seconds) +{ + int probe; + + if (timeout_seconds < 1 || timeout_seconds > 120) { + fprintf(stderr, "health requires a -t timeout between 1 and 120 seconds.\n"); + return PS2CLIENT_EXIT_USAGE; + } + + for (probe = 1; probe <= 2; ++probe) { + long long start = time_ms(); + int status = run_command(executable, hostname, "ping", timeout_seconds, 1); + if (status != PS2CLIENT_EXIT_OK) { + fprintf(stderr, + "health: EE/IOP round trip %d/2 FAILED (status %d).\n", + probe, status); + return PS2CLIENT_EXIT_NOCONNECT; + } + printf("health: EE/IOP round trip %d/2 OK (%lld ms).\n", + probe, time_ms() - start); + } + printf("health: PS2Link EE command and IOP host: services responded twice (%s).\n", + hostname); + return PS2CLIENT_EXIT_OK; +} diff --git a/src/resetcheck.h b/src/resetcheck.h index 6f94ee9..90c2eec 100644 --- a/src/resetcheck.h +++ b/src/resetcheck.h @@ -5,4 +5,8 @@ int ps2client_resetcheck(const char *executable, const char *hostname, int timeout_seconds); +/* Read-only health check: two independent EE/IOP host: round trips. */ +int ps2client_health(const char *executable, const char *hostname, + int timeout_seconds); + #endif diff --git a/src/utility.c b/src/utility.c index 7f62eca..7f21cef 100644 --- a/src/utility.c +++ b/src/utility.c @@ -128,6 +128,7 @@ fprintf(stderr, " Available commands:\n\n"); fprintf(stderr, " reset\n"); fprintf(stderr, " resetcheck preflight EE ping, send RESET, observe loss and recovery (-t default 10)\n"); + fprintf(stderr, " health check two independent EE/IOP round trips (read-only, -t default 2 each)\n"); fprintf(stderr, " execiop [arguments]\n"); fprintf(stderr, " execee [arguments]\n"); fprintf(stderr, " poweroff\n"); @@ -143,7 +144,7 @@ fprintf(stderr, " iopexcep\n"); fprintf(stderr, " listen\n"); fprintf(stderr, " console (UDP-only logging without host: service)\n"); - fprintf(stderr, " ping\n\n"); + fprintf(stderr, " ping verify EE/IOP host: round trip (not ICMP)\n\n"); fprintf(stderr, " Exit status: 0 ok, 64 usage error, 69 ps2link not answering, 74 ps2link closed\n"); fprintf(stderr, " the connection, 75 console port in use (another ps2client running?), 124 timeout\n"); fprintf(stderr, " before the -e marker. With -e: the status from the marker line.\n\n"); diff --git a/tests/fake_ps2link.c b/tests/fake_ps2link.c index 5d8bd44..c85255d 100644 --- a/tests/fake_ps2link.c +++ b/tests/fake_ps2link.c @@ -28,10 +28,14 @@ struct fake { struct sockaddr_in client_addr; pthread_mutex_t lock; int ignore_dumpreg; + int max_dumpreg; + int dumpreg_count; int reset_outage_ms; long long unavailable_until_ms; }; -static struct fake fake = {.client = -1, .lock = PTHREAD_MUTEX_INITIALIZER}; +static struct fake fake = { + .client = -1, .lock = PTHREAD_MUTEX_INITIALIZER, .max_dumpreg = -1 +}; static long long now_ms(void) { @@ -265,6 +269,8 @@ static void command_packet(unsigned char *packet, size_t length) int fd; if (fake.unavailable_until_ms > now_ms()) return; if (fake.ignore_dumpreg > 0) { fake.ignore_dumpreg--; return; } + if (fake.max_dumpreg >= 0 && + fake.dumpreg_count++ >= fake.max_dumpreg) return; fd = fake_open((char *)packet + 10, 0x0202); if (fd >= 0) { unsigned char zeros[8] = {0}; @@ -324,11 +330,13 @@ int main(int argc, char **argv) if (!strcmp(argv[i], "--bind") && i + 1 < argc) ip = argv[++i]; else if (!strcmp(argv[i], "--ignore-dumpreg") && i + 1 < argc) fake.ignore_dumpreg = atoi(argv[++i]); + else if (!strcmp(argv[i], "--max-dumpreg") && i + 1 < argc) + fake.max_dumpreg = atoi(argv[++i]); else if (!strcmp(argv[i], "--reset-outage-ms") && i + 1 < argc) fake.reset_outage_ms = atoi(argv[++i]); else { fprintf(stderr, "Unknown option: %s\n", argv[i]); return 2; } } - if (!ip) { fprintf(stderr, "Usage: fake_ps2link --bind IPV4 [--ignore-dumpreg N] [--reset-outage-ms N]\n"); return 2; } + if (!ip) { fprintf(stderr, "Usage: fake_ps2link --bind IPV4 [--ignore-dumpreg N] [--max-dumpreg N] [--reset-outage-ms N]\n"); return 2; } fake.listener = bind_port(SOCK_STREAM, ip, 0x4711); fake.commands = bind_port(SOCK_DGRAM, ip, 0x4712); if (fake.listener < 0 || fake.commands < 0 || listen(fake.listener, 5) < 0) { diff --git a/tests/run-tests.sh b/tests/run-tests.sh index 643c3a8..d8b0f26 100755 --- a/tests/run-tests.sh +++ b/tests/run-tests.sh @@ -167,5 +167,11 @@ check "handshake retries" 0 "${H[@]}" -t 10 -e @@END@@ execee host:nostatus.elf expect "retried after 1 s" test "$MS" -ge 990 stop_fake +start_fake --max-dumpreg 1 +check "health needs two EE round trips" 69 "${H[@]}" -t 1 health +expect "health reports first successful round trip" grep -q "round trip 1/2 OK" out.txt +expect "health rejects missing second EE response" grep -q "round trip 2/2 FAILED" err.txt +stop_fake + echo "passed $PASS, failed $FAIL" [ "$FAIL" = 0 ] diff --git a/tests/smoke.c b/tests/smoke.c index 6a41ff4..457fcfa 100644 --- a/tests/smoke.c +++ b/tests/smoke.c @@ -171,6 +171,36 @@ static void test_resetcheck_fake(int outage_ms, const char *timeout, message); } +static void test_health_fake(int max_replies, int expected, + const char *message) +{ + char output[2048]; + char *const args[] = {(char *)client, "-h", "127.0.0.1", + "-t", "1", "health", NULL}; + pid_t server = fork(); + int rc; + if (server < 0) { check(0, message); return; } + if (server == 0) { + char replies[24]; + snprintf(replies, sizeof(replies), "%d", max_replies); + execl("bin/fake_ps2link", "fake_ps2link", "--bind", "127.0.0.1", + "--max-dumpreg", replies, (char *)NULL); + _exit(127); + } + pause_ms(300); + rc = run(args, NULL, output, sizeof(output)); + kill(server, SIGTERM); + waitpid(server, NULL, 0); + check(rc == expected && + (max_replies == 2 ? + strstr(output, "round trip 2/2 OK") != NULL : + max_replies == 1 ? + (strstr(output, "round trip 1/2 OK") && + strstr(output, "round trip 2/2 FAILED")) : + strstr(output, "round trip 1/2 FAILED") != NULL), + message); +} + int main(int argc, char **argv) { char text[4096]; @@ -208,5 +238,22 @@ int main(int argc, char **argv) "resetcheck refuses to claim success without a loss"); test_resetcheck_fake(15000, "2", 124, "resetcheck reports a failed recovery"); + { char output[1024]; + char *const invalid_timeout[] = {(char *)client, "-t", "0", + "health", NULL}; + char *const extra_argument[] = {(char *)client, "health", + "unexpected", NULL}; + char *const unreachable[] = {(char *)client, "-h", "127.0.0.1", + "-t", "1", "health", NULL}; + check(run(invalid_timeout, NULL, output, sizeof(output)) == 64, + "health rejects zero timeout"); + check(run(extra_argument, NULL, output, sizeof(output)) == 64, + "health rejects extra arguments"); + check(run(unreachable, NULL, output, sizeof(output)) == 69, + "health rejects missing PS2Link without side effects"); + } + test_health_fake(2, 0, "health verifies two independent EE round trips"); + test_health_fake(1, 69, "health fails if EE stops answering second probe"); + test_health_fake(0, 69, "health fails when TCP responds but EE does not"); return failures ? 1 : 0; } diff --git a/tests/smoke_windows.c b/tests/smoke_windows.c index d8fa3ce..ca245f2 100644 --- a/tests/smoke_windows.c +++ b/tests/smoke_windows.c @@ -121,6 +121,12 @@ int main(void) "resetcheck refuses to send RESET to an unreachable PS2"); check(run("-h 127.0.0.1 -t 0 resetcheck") == 64, "resetcheck rejects an invalid observation timeout"); + check(run("-h 1.1.l.1 -t 1 health") == 69, + "health rejects an unreachable PS2Link EE handler"); + check(run("-t 0 health") == 64, + "health rejects an invalid probe timeout"); + check(run("health unexpected") == 64, + "health rejects extra arguments"); WSACleanup(); return failures ? 1 : 0;