| 970 | } |
| 971 | |
| 972 | int cbm_daemon_host_run(const cbm_daemon_host_config_t *config) { |
| 973 | if (!config || !config->endpoint || !config->executable_path || |
| 974 | !config->identity.semantic_version || !config->identity.build_fingerprint) { |
| 975 | return -1; |
| 976 | } |
| 977 | cbm_ui_log_init(); |
| 978 | char conflict_log[HOST_PATH_CAP]; |
| 979 | if (!host_log_open(conflict_log)) { |
| 980 | (void)fprintf(stderr, "codebase-memory: daemon log path is not private or safe\n"); |
| 981 | return -1; |
| 982 | } |
| 983 | cbm_version_cohort_manager_t *cohort_manager = cbm_version_cohort_manager_new(config->endpoint); |
| 984 | cbm_version_cohort_lease_t *cohort_lease = NULL; |
| 985 | cbm_daemon_conflict_t cohort_conflict; |
| 986 | uint64_t now_ms = cbm_now_ms(); |
| 987 | uint64_t cohort_deadline = now_ms > UINT64_MAX - HOST_INITIAL_CLIENT_TIMEOUT_MS |
| 988 | ? UINT64_MAX |
| 989 | : now_ms + HOST_INITIAL_CLIENT_TIMEOUT_MS; |
| 990 | cbm_version_cohort_status_t cohort_status = |
| 991 | cohort_manager |
| 992 | ? cbm_version_cohort_acquire(cohort_manager, &config->identity, cohort_deadline, |
| 993 | &cohort_lease, &cohort_conflict) |
| 994 | : CBM_VERSION_COHORT_IO; |
| 995 | if (cohort_status != CBM_VERSION_COHORT_OK) { |
| 996 | cbm_log_error("daemon.start_failed", "component", "cohort"); |
| 997 | char message[CBM_DAEMON_CONFLICT_MESSAGE_SIZE]; |
| 998 | bool formatted = cohort_status == CBM_VERSION_COHORT_CONFLICT && |
| 999 | cbm_daemon_conflict_format(&cohort_conflict, message, sizeof(message)); |
| 1000 | if (cohort_status == CBM_VERSION_COHORT_CONFLICT) { |
| 1001 | (void)cbm_version_cohort_log_conflict(&cohort_conflict); |
| 1002 | } |
| 1003 | (void)fprintf(stderr, "codebase-memory: %s\n", |
| 1004 | formatted ? message : "daemon exact-build admission failed"); |
| 1005 | host_cohort_close(&cohort_lease, &cohort_manager); |
| 1006 | host_log_close(); |
| 1007 | return -1; |
| 1008 | } |
| 1009 | cbm_daemon_ipc_participant_guard_t *participant_guard = NULL; |
| 1010 | if (cbm_daemon_ipc_participant_guard_try_join(config->endpoint, &participant_guard) != 1) { |
| 1011 | cbm_log_error("daemon.start_failed", "component", "participant"); |
| 1012 | (void)fprintf(stderr, "codebase-memory: daemon participant admission failed\n"); |
| 1013 | host_participant_guard_close(&participant_guard); |
| 1014 | host_cohort_close(&cohort_lease, &cohort_manager); |
| 1015 | host_log_close(); |
| 1016 | return -1; |
| 1017 | } |
| 1018 | cbm_version_cohort_daemon_claim_t *daemon_claim = NULL; |
| 1019 | uint64_t claim_deadline = host_deadline_after(HOST_DAEMON_CLAIM_TIMEOUT_MS); |
| 1020 | cbm_version_cohort_status_t claim_status = |
| 1021 | cbm_version_cohort_daemon_claim_acquire(cohort_manager, &daemon_claim); |
| 1022 | while (claim_status == CBM_VERSION_COHORT_BUSY && cbm_now_ms() < claim_deadline) { |
| 1023 | /* The previous generation is releasing; retry until the claim is free |
| 1024 | * or the handoff window elapses (a persistent BUSY = a real conflict). */ |
| 1025 | cbm_usleep(HOST_WAIT_TICK_MS * 1000); |
| 1026 | claim_status = cbm_version_cohort_daemon_claim_acquire(cohort_manager, &daemon_claim); |
| 1027 | } |
| 1028 | if (claim_status != CBM_VERSION_COHORT_OK) { |
| 1029 | cbm_log_error("daemon.start_failed", "component", "claim"); |