Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion src/ps2client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 },
};

Expand Down Expand Up @@ -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.
Expand Down
42 changes: 37 additions & 5 deletions src/resetcheck.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}
4 changes: 4 additions & 0 deletions src/resetcheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion src/utility.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <filename> [arguments]\n");
fprintf(stderr, " execee <filename> [arguments]\n");
fprintf(stderr, " poweroff\n");
Expand All @@ -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");
Expand Down
12 changes: 10 additions & 2 deletions tests/fake_ps2link.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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};
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 6 additions & 0 deletions tests/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
47 changes: 47 additions & 0 deletions tests/smoke.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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;
}
6 changes: 6 additions & 0 deletions tests/smoke_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading