ZZZ Digital Lab

Back to WaitLens

target/waitlens_probe.c

/* Original, bounded QNX 8.0 IPC workload. Compile with an owner-licensed SDP.
 * API references are listed in docs/sources.md. */
#include <sys/neutrino.h>
#include <sys/utsname.h>
#include <sys/wait.h>
#include <errno.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

#if !defined(__QNX__) || __QNX__ != 800
#error "This workload requires the QNX 8.0 toolchain and headers"
#endif

static void fail(const char *operation) {
    perror(operation);
    exit(EXIT_FAILURE);
}

int main(void) {
    struct utsname platform;
    const pid_t server = getpid();
    int chid, status;
    pid_t client;
    setvbuf(stdout, NULL, _IONBF, 0);
    if (uname(&platform) == -1) fail("uname");
    printf("WaitLens probe: OS=%s release=%s compiler_qnx=%d\n",
           platform.sysname, platform.release, __QNX__);
    /* A default terminating SIGALRM bounds the parent and child independently. */
    alarm(35);
    chid = ChannelCreate(0);
    if (chid == -1) fail("ChannelCreate");
    client = fork();
    if (client == -1) fail("fork");
    if (client == 0) {
        int coid, result;
        const char request[] = "waitlens";
        char reply[8] = {0};
        uint64_t timeout_ns = UINT64_C(25000000000);
        alarm(30);
        coid = ConnectAttach(0, server, chid, _NTO_SIDE_CHANNEL, 0);
        if (coid == -1) fail("ConnectAttach");
        printf("client=%ld destination=%ld phase=SEND_then_REPLY\n", (long)getpid(), (long)server);
        /* TimerTimeout must immediately precede the matching blocking call. */
        if (TimerTimeout(CLOCK_MONOTONIC, _NTO_TIMEOUT_SEND | _NTO_TIMEOUT_REPLY,
                         NULL, &timeout_ns, NULL) == -1) fail("TimerTimeout send");
        result = MsgSend(coid, request, sizeof(request), reply, sizeof(reply));
        if (result == -1) fail("MsgSend");
        if (reply[0] != 'O' || reply[1] != 'K') {
            fprintf(stderr, "unexpected reply\n");
            return EXIT_FAILURE;
        }
        printf("client=%ld phase=completed reply=OK\n", (long)getpid());
        if (ConnectDetach(coid) == -1) fail("ConnectDetach");
        return EXIT_SUCCESS;
    }
    printf("server=%ld client=%ld phase=delay_before_receive seconds=6\n", (long)server, (long)client);
    sleep(6);
    {
        union { struct _pulse pulse; char request[32]; } message;
        int rcvid;
        uint64_t timeout_ns = UINT64_C(10000000000);
        if (TimerTimeout(CLOCK_MONOTONIC, _NTO_TIMEOUT_RECEIVE, NULL,
                         &timeout_ns, NULL) == -1) fail("TimerTimeout receive");
        rcvid = MsgReceive(chid, &message, sizeof(message), NULL);
        if (rcvid <= 0) fail("MsgReceive");
        printf("server=%ld phase=delay_before_reply seconds=6\n", (long)server);
        sleep(6);
        if (MsgReply(rcvid, 0, "OK", 3) == -1) fail("MsgReply");
    }
    if (waitpid(client, &status, 0) == -1) fail("waitpid");
    if (ChannelDestroy(chid) == -1) fail("ChannelDestroy");
    alarm(0);
    if (!WIFEXITED(status) || WEXITSTATUS(status) != EXIT_SUCCESS) return EXIT_FAILURE;
    puts("WaitLens probe: completed");
    return EXIT_SUCCESS;
}