| 3539 | } |
| 3540 | |
| 3541 | cbm_daemon_application_t *cbm_daemon_application_new( |
| 3542 | const cbm_daemon_application_config_t *config) { |
| 3543 | cbm_daemon_application_t *application = calloc(1, sizeof(*application)); |
| 3544 | if (!application) { |
| 3545 | return NULL; |
| 3546 | } |
| 3547 | cbm_mutex_init(&application->mutex); |
| 3548 | application->physical_job_limit = APPLICATION_DEFAULT_PHYSICAL_JOB_LIMIT; |
| 3549 | size_t aggregate_memory_budget_bytes = cbm_mem_budget(); |
| 3550 | if (config) { |
| 3551 | if ((config->ui_readiness_secret != NULL || config->ui_readiness_secret_length != 0) && |
| 3552 | (!config->ui_readiness_secret || |
| 3553 | config->ui_readiness_secret_length != sizeof(application->ui_readiness_secret))) { |
| 3554 | cbm_mutex_destroy(&application->mutex); |
| 3555 | cbm_secure_zero(application->ui_readiness_secret, |
| 3556 | sizeof(application->ui_readiness_secret)); |
| 3557 | free(application); |
| 3558 | return NULL; |
| 3559 | } |
| 3560 | application->watcher = config->watcher; |
| 3561 | application->config = config->config; |
| 3562 | application->project_locks = config->project_locks; |
| 3563 | if (config->physical_job_limit > 0) { |
| 3564 | application->physical_job_limit = config->physical_job_limit; |
| 3565 | } |
| 3566 | if (config->aggregate_memory_budget_bytes > 0) { |
| 3567 | aggregate_memory_budget_bytes = config->aggregate_memory_budget_bytes; |
| 3568 | } |
| 3569 | if (config->worker_ops) { |
| 3570 | application->worker_ops = *config->worker_ops; |
| 3571 | } |
| 3572 | if (config->update_ops) { |
| 3573 | application->update_ops = *config->update_ops; |
| 3574 | } |
| 3575 | if (config->ui_readiness_secret && |
| 3576 | config->ui_readiness_secret_length == sizeof(application->ui_readiness_secret)) { |
| 3577 | memcpy(application->ui_readiness_secret, config->ui_readiness_secret, |
| 3578 | sizeof(application->ui_readiness_secret)); |
| 3579 | application->ui_readiness_secret_set = true; |
| 3580 | } |
| 3581 | } |
| 3582 | /* The per-worker slice is decided at spawn time (see |
| 3583 | * application_worker_memory_slice_locked): the aggregate divided by the |
| 3584 | * jobs active then, so a lone worker may use the whole aggregate and |
| 3585 | * concurrent jobs split it (decision 2, #1997 #832) — the former fixed |
| 3586 | * aggregate/limit slice starved a lone job on hosts like #1864. The absurd |
| 3587 | * sub-byte-per-slot case is still made safe by reducing effective capacity |
| 3588 | * so every admitted job can receive at least one byte; normal daemon |
| 3589 | * budgets are many orders larger. */ |
| 3590 | if (aggregate_memory_budget_bytes > 0 && |
| 3591 | application->physical_job_limit > aggregate_memory_budget_bytes) { |
| 3592 | application->physical_job_limit = aggregate_memory_budget_bytes; |
| 3593 | } |
| 3594 | application->aggregate_memory_budget_bytes = aggregate_memory_budget_bytes; |
| 3595 | if (!application->worker_ops.start) { |
| 3596 | application->worker_ops = (cbm_daemon_application_worker_ops_t){ |
| 3597 | .context = NULL, |
| 3598 | .start = application_worker_start_default, |