| 1628 | } |
| 1629 | |
| 1630 | static bool runtime_worker_handle_application(cbm_daemon_runtime_worker_t *worker, |
| 1631 | const uint8_t *payload, uint32_t length) { |
| 1632 | if (!payload || length < APPLICATION_REQUEST_PREFIX_SIZE) { |
| 1633 | return false; |
| 1634 | } |
| 1635 | cbm_daemon_runtime_application_token_t request_token = runtime_get_u64(payload); |
| 1636 | uint32_t request_length = runtime_get_u32(payload + 8); |
| 1637 | if (request_length > CBM_DAEMON_RUNTIME_APPLICATION_PAYLOAD_MAX || |
| 1638 | length != APPLICATION_REQUEST_PREFIX_SIZE + request_length || |
| 1639 | request_token == CBM_DAEMON_RUNTIME_APPLICATION_TOKEN_INVALID || |
| 1640 | request_token <= worker->last_application_request_token) { |
| 1641 | return false; |
| 1642 | } |
| 1643 | /* A structurally valid token is consumed at admission, including BUSY, |
| 1644 | * UNAVAILABLE, and local allocation/thread failures. Reusing a token after |
| 1645 | * any terminal response would make late cancellation controls ambiguous. */ |
| 1646 | worker->last_application_request_token = request_token; |
| 1647 | cbm_daemon_runtime_service_t *service = worker->service; |
| 1648 | if (!service->application.request) { |
| 1649 | return runtime_worker_send_application_response( |
| 1650 | worker, request_token, CBM_DAEMON_RUNTIME_APPLICATION_UNAVAILABLE, NULL, 0, false); |
| 1651 | } |
| 1652 | (void)runtime_worker_reap_application(worker, false); |
| 1653 | if (worker->application_thread_started) { |
| 1654 | return runtime_worker_send_application_response( |
| 1655 | worker, request_token, CBM_DAEMON_RUNTIME_APPLICATION_BUSY, NULL, 0, false); |
| 1656 | } |
| 1657 | |
| 1658 | uint8_t *request_copy = NULL; |
| 1659 | if (request_length > 0) { |
| 1660 | request_copy = malloc(request_length); |
| 1661 | if (!request_copy) { |
| 1662 | return runtime_worker_send_application_response( |
| 1663 | worker, request_token, CBM_DAEMON_RUNTIME_APPLICATION_HANDLER_ERROR, NULL, 0, |
| 1664 | false); |
| 1665 | } |
| 1666 | memcpy(request_copy, payload + APPLICATION_REQUEST_PREFIX_SIZE, request_length); |
| 1667 | } |
| 1668 | worker->application_request_token = request_token; |
| 1669 | worker->application_request = request_copy; |
| 1670 | worker->application_request_length = request_length; |
| 1671 | atomic_store_explicit(&worker->application_thread_done, false, memory_order_release); |
| 1672 | if (cbm_thread_create(&worker->application_thread, RUNTIME_WORKER_STACK_SIZE, |
| 1673 | runtime_application_worker, worker) != 0) { |
| 1674 | free(worker->application_request); |
| 1675 | worker->application_request = NULL; |
| 1676 | worker->application_request_length = 0; |
| 1677 | return runtime_worker_send_application_response( |
| 1678 | worker, request_token, CBM_DAEMON_RUNTIME_APPLICATION_HANDLER_ERROR, NULL, 0, false); |
| 1679 | } |
| 1680 | worker->application_thread_started = true; |
| 1681 | return true; |
| 1682 | } |
| 1683 | |
| 1684 | static bool runtime_worker_handle_application_cancel(cbm_daemon_runtime_worker_t *worker, |
| 1685 | const uint8_t *payload, uint32_t length) { |
no test coverage detected