| 3098 | } |
| 3099 | |
| 3100 | cbm_daemon_runtime_client_t *cbm_daemon_runtime_client_connect( |
| 3101 | const cbm_daemon_ipc_endpoint_t *endpoint, const cbm_daemon_build_identity_t *identity, |
| 3102 | uint32_t timeout_ms, cbm_daemon_runtime_connect_result_t *result_out) { |
| 3103 | if (result_out) { |
| 3104 | memset(result_out, 0, sizeof(*result_out)); |
| 3105 | } |
| 3106 | uint8_t request[CBM_DAEMON_RENDEZVOUS_REQUEST_SIZE]; |
| 3107 | if (!endpoint || !identity || !result_out || timeout_ms == CBM_DAEMON_IPC_WAIT_FOREVER || |
| 3108 | !cbm_daemon_runtime_hello_request_encode(request, identity)) { |
| 3109 | return NULL; |
| 3110 | } |
| 3111 | cbm_daemon_ipc_connection_t *connection = cbm_daemon_ipc_connect(endpoint, timeout_ms); |
| 3112 | if (!connection) { |
| 3113 | return NULL; |
| 3114 | } |
| 3115 | RUNTIME_HELLO_POINT(CLIENT_CONNECTED); |
| 3116 | bool hello_sent = |
| 3117 | cbm_daemon_ipc_send_frame(connection, CBM_DAEMON_FRAME_REQUEST, CBM_DAEMON_RUNTIME_OP_HELLO, |
| 3118 | request, (uint32_t)sizeof(request)); |
| 3119 | RUNTIME_HELLO_POINT(CLIENT_SENT); |
| 3120 | cbm_daemon_frame_t frame = {0}; |
| 3121 | uint8_t *payload = NULL; |
| 3122 | /* A daemon at capacity or stopping answers from its accept loop without |
| 3123 | * reading this HELLO and closes at once. When that close lands first, the |
| 3124 | * send fails (EPIPE) with the whole answer already queued here: read it, |
| 3125 | * or the caller gets a bare transport error and bootstrap classifies by |
| 3126 | * lock state instead of by the daemon's own "capacity" / "stopping". */ |
| 3127 | int received = |
| 3128 | hello_sent |
| 3129 | ? cbm_daemon_ipc_receive_frame_bounded( |
| 3130 | connection, timeout_ms, CBM_DAEMON_RENDEZVOUS_RESPONSE_SIZE, &frame, &payload) |
| 3131 | : cbm_daemon_ipc_receive_frame_after_failed_send( |
| 3132 | connection, timeout_ms, CBM_DAEMON_RENDEZVOUS_RESPONSE_SIZE, &frame, &payload); |
| 3133 | bool valid = received == 1 && frame.type == CBM_DAEMON_FRAME_RESPONSE && |
| 3134 | frame.flags == CBM_DAEMON_RUNTIME_OP_HELLO && |
| 3135 | frame.length == CBM_DAEMON_RENDEZVOUS_RESPONSE_SIZE && |
| 3136 | runtime_hello_response_decode(payload, result_out); |
| 3137 | free(payload); |
| 3138 | if (!hello_sent && (!valid || result_out->status != CBM_DAEMON_RUNTIME_CONNECT_REJECTED)) { |
| 3139 | /* Only a rejection can answer a HELLO the daemon never received; |
| 3140 | * anything else stays the plain send failure it always was. */ |
| 3141 | memset(result_out, 0, sizeof(*result_out)); |
| 3142 | cbm_daemon_ipc_connection_close(connection); |
| 3143 | return NULL; |
| 3144 | } |
| 3145 | if (received != 1) { |
| 3146 | /* The kernel completed the pipe/socket connect, so a server process |
| 3147 | * exists, yet the HELLO went unanswered. Name that holder: a dead |
| 3148 | * runtime behind a live endpoint is otherwise indistinguishable from |
| 3149 | * absence, and the 2026-08-29 zombie hid behind exactly that gap. */ |
| 3150 | result_out->muted_endpoint_holder_pid = cbm_daemon_ipc_connection_peer_pid(connection); |
| 3151 | } |
| 3152 | if (!valid || result_out->status != CBM_DAEMON_RUNTIME_CONNECT_ACCEPTED) { |
| 3153 | cbm_daemon_ipc_connection_close(connection); |
| 3154 | return NULL; |
| 3155 | } |
| 3156 | |
| 3157 | cbm_daemon_runtime_client_t *client = calloc(1, sizeof(*client)); |