MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / main_run_daemon_ctl

Function main_run_daemon_ctl

src/main.c:2541–2782  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2539}
2540
2541static int main_run_daemon_ctl(int argc, char **argv, const cbm_daemon_ipc_endpoint_t *endpoint,
2542 const cbm_daemon_build_identity_t *identity,
2543 const char *executable_path) {
2544 const char *subcommand = NULL;
2545 bool open_browser = false;
2546 int requested_port = 0;
2547 bool arguments_valid = true;
2548 for (int index = 1; index < argc; index++) {
2549 if (strcmp(argv[index], "daemon") == 0) {
2550 continue;
2551 }
2552 if (strcmp(argv[index], "start") == 0 || strcmp(argv[index], "stop") == 0 ||
2553 strcmp(argv[index], "status") == 0) {
2554 subcommand = argv[index];
2555 } else if (strcmp(argv[index], "--open") == 0) {
2556 open_browser = true;
2557 } else if (strncmp(argv[index], "--port=", 7) == 0) {
2558 requested_port = atoi(argv[index] + 7);
2559 if (requested_port <= 0 || requested_port >= MAIN_MAX_PORT) {
2560 (void)fprintf(stderr, "error: --port requires a value between 1 and 65535\n");
2561 return EXIT_FAILURE;
2562 }
2563 } else {
2564 (void)fprintf(stderr, "error: unknown daemon option: %s\n", argv[index]);
2565 arguments_valid = false;
2566 break;
2567 }
2568 }
2569 if (!arguments_valid || !subcommand) {
2570 (void)fprintf(stderr, "usage: codebase-memory-mcp daemon <start|stop|status> "
2571 "[--open] [--port=N]\n");
2572 return EXIT_FAILURE;
2573 }
2574
2575 cbm_daemon_runtime_status_t status;
2576 bool active = cbm_daemon_runtime_request_status(endpoint, identity,
2577 MAIN_DAEMON_CTL_PROBE_TIMEOUT_MS, &status);
2578
2579 if (strcmp(subcommand, "status") == 0) {
2580 if (!active) {
2581 if (status.muted_endpoint_holder_pid != 0) {
2582 /* Alive process, dead runtime (2026-08-29 zombie class).
2583 * "not running" here sent the operator hunting through
2584 * processes, pipes, and logs by hand; name the holder and the
2585 * recovery instead. */
2586 printf("daemon: not responding (endpoint held by pid %llu)\n",
2587 (unsigned long long)status.muted_endpoint_holder_pid);
2588 printf("hint: that process holds the daemon endpoint but answered nothing; "
2589 "its runtime is likely dead. Terminate pid %llu, then run "
2590 "`codebase-memory-mcp daemon start`.\n",
2591 (unsigned long long)status.muted_endpoint_holder_pid);
2592 return EXIT_FAILURE;
2593 }
2594 printf("daemon: not running\n");
2595 printf("hint: `codebase-memory-mcp daemon start` keeps a daemon warm so CLI "
2596 "commands and hooks skip the per-command startup cost.\n");
2597 return EXIT_FAILURE;
2598 }

Callers 1

mainFunction · 0.85

Tested by

no test coverage detected