| 1854 | } |
| 1855 | |
| 1856 | static void runtime_worker_handle_stop(cbm_daemon_runtime_worker_t *worker, const uint8_t *payload, |
| 1857 | uint32_t length) { |
| 1858 | cbm_daemon_runtime_service_t *service = worker->service; |
| 1859 | char requested_build[CBM_DAEMON_BUILD_FINGERPRINT_SIZE] = {0}; |
| 1860 | bool peer_verified = |
| 1861 | runtime_control_request_decode(payload, length, requested_build) && |
| 1862 | runtime_activation_peer_matches_claim(service, worker->peer_process_id, requested_build); |
| 1863 | uint8_t response[CBM_DAEMON_STOP_RESPONSE_SIZE]; |
| 1864 | memset(response, 0, sizeof(response)); |
| 1865 | bool accepted = false; |
| 1866 | if (peer_verified) { |
| 1867 | uint8_t count = 0U; |
| 1868 | uint32_t pids[CBM_DAEMON_CONTROL_CLIENT_CAP] = {0}; |
| 1869 | uint64_t deadline = runtime_deadline_after(service->shutdown_timeout_ms); |
| 1870 | cbm_mutex_lock(&service->mutex); |
| 1871 | response[0] = 1U; |
| 1872 | uint16_t committed = service->committed_clients > UINT16_MAX |
| 1873 | ? UINT16_MAX |
| 1874 | : (uint16_t)service->committed_clients; |
| 1875 | runtime_control_collect_clients_locked(service, worker, &count, pids); |
| 1876 | if (service->committed_clients > 0) { |
| 1877 | /* Refuse-if-busy: the caller reports these pids to the user as |
| 1878 | * the processes that must exit before a graceful stop. */ |
| 1879 | response[1] = 0x02U; |
| 1880 | } else if (service->state == CBM_DAEMON_RUNTIME_SERVICE_RUNNING) { |
| 1881 | /* Explicit stop is the sanctioned exit for PERMANENT generations |
| 1882 | * as well — begin_stopping here overrides the permanence gate. |
| 1883 | * Mirror the activation-shutdown ACK protocol: the stopping |
| 1884 | * teardown must not consume this requester's response slot before |
| 1885 | * the acceptance byte leaves. */ |
| 1886 | runtime_service_begin_stopping_locked(service, deadline, false, |
| 1887 | "daemon_stop_requested"); |
| 1888 | /* Same exit criterion as an activation drain: a permanent |
| 1889 | * coordinator never reports client-terminal, so resource |
| 1890 | * emptiness must be allowed to complete this stop promptly. */ |
| 1891 | service->activation_shutdown_requested = true; |
| 1892 | service->activation_response_inflight = true; |
| 1893 | worker->final_response_inflight = true; |
| 1894 | response[1] = 0x01U; |
| 1895 | accepted = true; |
| 1896 | } |
| 1897 | response[2] = (uint8_t)(committed >> 8); |
| 1898 | response[3] = (uint8_t)(committed & 0xFFU); |
| 1899 | response[4] = count; |
| 1900 | for (size_t index = 0; index < CBM_DAEMON_CONTROL_CLIENT_CAP; index++) { |
| 1901 | size_t offset = 8U + index * 4U; |
| 1902 | response[offset] = (uint8_t)(pids[index] >> 24); |
| 1903 | response[offset + 1U] = (uint8_t)(pids[index] >> 16); |
| 1904 | response[offset + 2U] = (uint8_t)(pids[index] >> 8); |
| 1905 | response[offset + 3U] = (uint8_t)(pids[index] & 0xFFU); |
| 1906 | } |
| 1907 | cbm_mutex_unlock(&service->mutex); |
| 1908 | } |
| 1909 | if (accepted) { |
| 1910 | char requester_pid[32]; |
| 1911 | (void)snprintf(requester_pid, sizeof(requester_pid), "%llu", |
| 1912 | (unsigned long long)worker->peer_process_id); |
| 1913 | cbm_log_info("daemon.stop_requested", "requester_pid", requester_pid, "requester_build", |
no test coverage detected