| 2639 | } |
| 2640 | |
| 2641 | cbm_daemon_runtime_service_t *cbm_daemon_runtime_service_start( |
| 2642 | const cbm_daemon_runtime_service_config_t *config) { |
| 2643 | if (!config || !config->endpoint) { |
| 2644 | return NULL; |
| 2645 | } |
| 2646 | cbm_daemon_ipc_startup_lock_t *startup_lock = NULL; |
| 2647 | if (cbm_daemon_ipc_startup_lock_try_acquire(config->endpoint, &startup_lock) != 1) { |
| 2648 | return NULL; |
| 2649 | } |
| 2650 | int generation = |
| 2651 | cbm_daemon_ipc_generation_probe_under_startup_lock(config->endpoint, startup_lock); |
| 2652 | if (generation != 0 || !cbm_daemon_ipc_startup_lock_prepare_handoff(startup_lock)) { |
| 2653 | runtime_startup_lock_release_complete(&startup_lock, config->shutdown_timeout_ms); |
| 2654 | return NULL; |
| 2655 | } |
| 2656 | cbm_daemon_ipc_participant_guard_t *participant_guard = NULL; |
| 2657 | if (cbm_daemon_ipc_participant_guard_try_join(config->endpoint, &participant_guard) != 1) { |
| 2658 | runtime_startup_lock_release_complete(&startup_lock, config->shutdown_timeout_ms); |
| 2659 | return NULL; |
| 2660 | } |
| 2661 | cbm_daemon_ipc_lifetime_reservation_t *reservation = NULL; |
| 2662 | if (cbm_daemon_ipc_lifetime_reservation_try_acquire(config->endpoint, &reservation) != 1) { |
| 2663 | if (!runtime_participant_guard_release_complete(&participant_guard, |
| 2664 | config->shutdown_timeout_ms)) { |
| 2665 | runtime_cleanup_fail_stop("participant_guard_cleanup"); |
| 2666 | } |
| 2667 | runtime_startup_lock_release_complete(&startup_lock, config->shutdown_timeout_ms); |
| 2668 | return NULL; |
| 2669 | } |
| 2670 | cbm_daemon_runtime_service_t *service = |
| 2671 | cbm_daemon_runtime_service_start_reserved(config, &reservation); |
| 2672 | cbm_daemon_ipc_lifetime_reservation_release(reservation); |
| 2673 | if (!service) { |
| 2674 | if (!runtime_participant_guard_release_complete(&participant_guard, |
| 2675 | config->shutdown_timeout_ms)) { |
| 2676 | runtime_cleanup_fail_stop("participant_guard_cleanup"); |
| 2677 | } |
| 2678 | runtime_startup_lock_release_complete(&startup_lock, config->shutdown_timeout_ms); |
| 2679 | return NULL; |
| 2680 | } |
| 2681 | service->owned_participant_guard = participant_guard; |
| 2682 | runtime_startup_lock_release_complete(&startup_lock, config->shutdown_timeout_ms); |
| 2683 | return service; |
| 2684 | } |
| 2685 | |
| 2686 | cbm_daemon_runtime_service_state_t cbm_daemon_runtime_service_state( |
| 2687 | cbm_daemon_runtime_service_t *service) { |