| 371 | } |
| 372 | |
| 373 | static bool cli_activation_log_event(cli_activation_production_context_t *context, |
| 374 | const char *phase, const char *detail) { |
| 375 | if (!context || !context->activation_log || !phase) { |
| 376 | return false; |
| 377 | } |
| 378 | yyjson_mut_doc *document = yyjson_mut_doc_new(NULL); |
| 379 | yyjson_mut_val *root = document ? yyjson_mut_obj(document) : NULL; |
| 380 | if (!document || !root) { |
| 381 | yyjson_mut_doc_free(document); |
| 382 | return false; |
| 383 | } |
| 384 | yyjson_mut_doc_set_root(document, root); |
| 385 | bool encoded = |
| 386 | yyjson_mut_obj_add_str(document, root, "event", "cbm.activation") && |
| 387 | yyjson_mut_obj_add_strcpy(document, root, "phase", phase) && |
| 388 | yyjson_mut_obj_add_strcpy(document, root, "action", |
| 389 | cli_activation_action_text(context->action)) && |
| 390 | yyjson_mut_obj_add_int(document, root, "requester_pid", |
| 391 | (int64_t)cli_activation_process_id()) && |
| 392 | yyjson_mut_obj_add_int(document, root, "timestamp_unix_s", (int64_t)time(NULL)) && |
| 393 | yyjson_mut_obj_add_str(document, root, "source_version", CBM_VERSION) && |
| 394 | yyjson_mut_obj_add_strcpy( |
| 395 | document, root, "source_build", |
| 396 | context->identity.build_fingerprint ? context->identity.build_fingerprint : "") && |
| 397 | yyjson_mut_obj_add_int(document, root, "daemon_active_clients", |
| 398 | (int64_t)context->daemon_result.active_clients) && |
| 399 | yyjson_mut_obj_add_int(document, root, "daemon_active_connections", |
| 400 | (int64_t)context->daemon_result.active_connections) && |
| 401 | yyjson_mut_obj_add_bool(document, root, "restart_required", true); |
| 402 | if (encoded && context->target_version && context->target_version[0]) { |
| 403 | encoded = |
| 404 | yyjson_mut_obj_add_strcpy(document, root, "target_version", context->target_version); |
| 405 | } |
| 406 | if (encoded && context->target_build && context->target_build[0]) { |
| 407 | encoded = yyjson_mut_obj_add_strcpy(document, root, "target_build", context->target_build); |
| 408 | } |
| 409 | if (encoded && detail && detail[0]) { |
| 410 | encoded = yyjson_mut_obj_add_strcpy(document, root, "detail", detail); |
| 411 | } |
| 412 | size_t json_size = 0; |
| 413 | char *json = encoded ? yyjson_mut_write(document, 0, &json_size) : NULL; |
| 414 | bool written = |
| 415 | json && json_size > 0 && fwrite(json, 1, json_size, context->activation_log) == json_size && |
| 416 | fputc('\n', context->activation_log) != EOF && fflush(context->activation_log) == 0; |
| 417 | if (written) { |
| 418 | int descriptor = cbm_fileno(context->activation_log); |
| 419 | #ifdef _WIN32 |
| 420 | written = descriptor >= 0 && _commit(descriptor) == 0; |
| 421 | #else |
| 422 | written = descriptor >= 0 && fsync(descriptor) == 0; |
| 423 | #endif |
| 424 | } |
| 425 | free(json); |
| 426 | yyjson_mut_doc_free(document); |
| 427 | return written; |
| 428 | } |
| 429 | |
| 430 | static int cli_activation_startup_lock_acquire(cli_activation_production_context_t *context) { |
no test coverage detected