The activation flow (self-path detection, simple-copy activate) is the same * on every platform: Windows ships ONE binary, exactly like Linux and macOS. */ Detect the running binary's path at runtime. The return value distinguishes * OS-reported identity (safe ownership evidence) from the ~/.local/bin fallback * used only as an install copy source. */
| 10620 | * OS-reported identity (safe ownership evidence) from the ~/.local/bin fallback |
| 10621 | * used only as an install copy source. */ |
| 10622 | static bool cbm_detect_self_path(char *buf, size_t buf_sz, const char *home) { |
| 10623 | buf[0] = '\0'; |
| 10624 | bool exact = false; |
| 10625 | #ifdef _WIN32 |
| 10626 | /* GetModuleFileNameA renders the module path through the ANSI code page, |
| 10627 | * which mangles non-ASCII install paths (café_日本語 -> caf?_???). Resolve |
| 10628 | * wide and convert to UTF-8 so the returned path survives verbatim. */ |
| 10629 | char *module_path = cbm_module_path_utf8(); |
| 10630 | size_t length = module_path ? strlen(module_path) : 0U; |
| 10631 | exact = module_path && length > 0U && length < buf_sz; |
| 10632 | if (exact) { |
| 10633 | memcpy(buf, module_path, length + 1U); |
| 10634 | cbm_normalize_path_sep(buf); |
| 10635 | } else { |
| 10636 | buf[0] = '\0'; |
| 10637 | } |
| 10638 | free(module_path); |
| 10639 | #elif defined(__APPLE__) |
| 10640 | uint32_t sp_sz = (uint32_t)buf_sz; |
| 10641 | exact = _NSGetExecutablePath(buf, &sp_sz) == 0; |
| 10642 | if (!exact) { |
| 10643 | buf[0] = '\0'; |
| 10644 | } |
| 10645 | #elif defined(__FreeBSD__) || defined(__NetBSD__) |
| 10646 | #if defined(__NetBSD__) |
| 10647 | int mib[4] = {CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME}; |
| 10648 | #else |
| 10649 | int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1}; |
| 10650 | #endif |
| 10651 | size_t cb = buf_sz; |
| 10652 | exact = sysctl(mib, 4, buf, &cb, NULL, 0) == 0 && cb > 0; |
| 10653 | if (!exact) { |
| 10654 | buf[0] = '\0'; |
| 10655 | } |
| 10656 | #else |
| 10657 | ssize_t sp_len = readlink("/proc/self/exe", buf, buf_sz - SKIP_ONE); |
| 10658 | exact = sp_len > 0 && (size_t)sp_len < buf_sz - SKIP_ONE; |
| 10659 | if (exact) { |
| 10660 | buf[sp_len] = '\0'; |
| 10661 | } else { |
| 10662 | buf[0] = '\0'; |
| 10663 | } |
| 10664 | #endif |
| 10665 | if (!buf[0]) { |
| 10666 | #ifdef _WIN32 |
| 10667 | snprintf(buf, buf_sz, "%s/.local/bin/codebase-memory-mcp.exe", home); |
| 10668 | #else |
| 10669 | snprintf(buf, buf_sz, "%s/.local/bin/codebase-memory-mcp", home); |
| 10670 | #endif |
| 10671 | } |
| 10672 | return exact; |
| 10673 | } |
| 10674 | |
| 10675 | /* Is the running binary owned by an external package manager rather than by us? |
| 10676 | * |
no test coverage detected