Run one literal argv vector in a contained process tree. active_git is * published under projects_lock before supervision starts, so stop/unwatch can * request cancellation without racing destruction of the handle. */
| 381 | * published under projects_lock before supervision starts, so stop/unwatch can |
| 382 | * request cancellation without racing destruction of the handle. */ |
| 383 | static watcher_git_status_t watcher_git_run(cbm_watcher_t *w, project_state_t *state, |
| 384 | const char *const *argv, size_t output_limit, |
| 385 | watcher_git_output_t *output) { |
| 386 | if (!w || !state || !argv || !argv[0]) { |
| 387 | return WATCHER_GIT_SUPERVISION_FAILED; |
| 388 | } |
| 389 | if (output) { |
| 390 | memset(output, 0, sizeof(*output)); |
| 391 | } |
| 392 | if (output_limit > 0 && (!output || !watcher_git_output_create(output))) { |
| 393 | return WATCHER_GIT_SUPERVISION_FAILED; |
| 394 | } |
| 395 | |
| 396 | #ifdef _WIN32 |
| 397 | char git_executable[CBM_SZ_4K]; |
| 398 | if (!watcher_resolve_git_executable(git_executable)) { |
| 399 | watcher_git_output_cleanup(output); |
| 400 | return WATCHER_GIT_SUPERVISION_FAILED; |
| 401 | } |
| 402 | #endif |
| 403 | |
| 404 | cbm_proc_opts_t options = { |
| 405 | #ifdef _WIN32 |
| 406 | .bin = git_executable, |
| 407 | #else |
| 408 | .bin = argv[0], |
| 409 | #endif |
| 410 | .argv = argv, |
| 411 | .log_file = output_limit > 0 ? output->path : NULL, |
| 412 | /* A wall deadline below is authoritative. Binary `status -z` output |
| 413 | * deliberately has no line-based quiet-timeout semantics. */ |
| 414 | .quiet_timeout_ms = 0, |
| 415 | .cancel_grace_ms = CBM_SUBPROCESS_DEFAULT_CANCEL_GRACE_MS, |
| 416 | .delete_log_on_exit = false, |
| 417 | .strip_git_repo_env = true, /* #2003: an inherited GIT_DIR must not redirect -C */ |
| 418 | }; |
| 419 | cbm_subprocess_t *process = NULL; |
| 420 | if (cbm_subprocess_spawn(&options, &process) != 0) { |
| 421 | watcher_git_output_cleanup(output); |
| 422 | return WATCHER_GIT_SUPERVISION_FAILED; |
| 423 | } |
| 424 | |
| 425 | bool cancelled = false; |
| 426 | bool deadline_expired = false; |
| 427 | bool output_exceeded = false; |
| 428 | bool publication_conflict = false; |
| 429 | cbm_mutex_lock(&w->projects_lock); |
| 430 | if (state->active_git) { |
| 431 | publication_conflict = true; |
| 432 | cancelled = cbm_subprocess_request_cancel(process); |
| 433 | } else { |
| 434 | state->active_git = process; |
| 435 | } |
| 436 | if (!cancelled && (atomic_load_explicit(&w->stopped, memory_order_acquire) || |
| 437 | !atomic_load_explicit(&state->registered, memory_order_acquire))) { |
| 438 | cancelled = cbm_subprocess_request_cancel(process); |
| 439 | } |
| 440 | cbm_mutex_unlock(&w->projects_lock); |
no test coverage detected