2323#include "foundation/log.h"
2424#include "foundation/platform.h"
2525#include "foundation/compat.h"
26+ #include "foundation/compat_fs.h"
2627#include "foundation/str_util.h"
2728#include "foundation/compat_thread.h"
2829
3940#include <process.h>
4041#include <psapi.h> /* GetProcessMemoryInfo */
4142#else
43+ #include <sys/stat.h>
4244#include <unistd.h>
4345#include <sys/wait.h>
4446#endif
@@ -582,9 +584,107 @@ static void handle_adr_save(cbm_http_conn_t *c, const cbm_http_req_t *req) {
582584
583585static char g_binary_path [1024 ] = {0 };
584586
587+ static bool copy_path (char * out , size_t outsz , const char * path ) {
588+ if (!out || outsz == 0 || !path || !path [0 ]) {
589+ return false;
590+ }
591+ int n = snprintf (out , outsz , "%s" , path );
592+ return n > 0 && (size_t )n < outsz ;
593+ }
594+
595+ #ifndef _WIN32
596+ static bool is_executable_file (const char * path ) {
597+ struct stat st ;
598+ return path && stat (path , & st ) == 0 && S_ISREG (st .st_mode ) && access (path , X_OK ) == 0 ;
599+ }
600+
601+ static bool resolve_from_path (const char * name , char * out , size_t outsz ) {
602+ const char * path = getenv ("PATH" );
603+ if (!name || !name [0 ] || strchr (name , '/' ) || !path || !path [0 ]) {
604+ return false;
605+ }
606+
607+ const char * cur = path ;
608+ while (* cur ) {
609+ const char * colon = strchr (cur , ':' );
610+ size_t dir_len = colon ? (size_t )(colon - cur ) : strlen (cur );
611+ if (dir_len > 0 && dir_len < 900 ) {
612+ char candidate [1024 ];
613+ int n = snprintf (candidate , sizeof (candidate ), "%.*s/%s" , (int )dir_len , cur , name );
614+ if (n > 0 && (size_t )n < sizeof (candidate ) && is_executable_file (candidate )) {
615+ return copy_path (out , outsz , candidate );
616+ }
617+ }
618+ if (!colon ) {
619+ break ;
620+ }
621+ cur = colon + 1 ;
622+ }
623+ return false;
624+ }
625+
626+ static bool resolve_self_executable (char * out , size_t outsz ) {
627+ #if defined(__APPLE__ )
628+ char buf [1024 ];
629+ uint32_t sz = sizeof (buf );
630+ if (_NSGetExecutablePath (buf , & sz ) == 0 && buf [0 ]) {
631+ return copy_path (out , outsz , buf );
632+ }
633+ return false;
634+ #else
635+ char buf [1024 ];
636+ ssize_t len = readlink ("/proc/self/exe" , buf , sizeof (buf ) - 1 );
637+ if (len > 0 ) {
638+ buf [len ] = '\0' ;
639+ return copy_path (out , outsz , buf );
640+ }
641+ return false;
642+ #endif
643+ }
644+ #else
645+ static bool resolve_self_executable (char * out , size_t outsz ) {
646+ char buf [1024 ];
647+ DWORD n = GetModuleFileNameA (NULL , buf , (DWORD )sizeof (buf ));
648+ if (n > 0 && n < sizeof (buf )) {
649+ return copy_path (out , outsz , buf );
650+ }
651+ return false;
652+ }
653+ #endif
654+
655+ bool cbm_http_server_resolve_binary_path (const char * argv0 , char * out , size_t outsz ) {
656+ if (!out || outsz == 0 ) {
657+ return false;
658+ }
659+ out [0 ] = '\0' ;
660+
661+ #ifndef _WIN32
662+ if (argv0 && strchr (argv0 , '/' ) && is_executable_file (argv0 )) {
663+ return copy_path (out , outsz , argv0 );
664+ }
665+ if (resolve_from_path (argv0 , out , outsz )) {
666+ return true;
667+ }
668+ #else
669+ if (argv0 && argv0 [0 ]) {
670+ DWORD attrs = GetFileAttributesA (argv0 );
671+ if (attrs != INVALID_FILE_ATTRIBUTES && !(attrs & FILE_ATTRIBUTE_DIRECTORY )) {
672+ return copy_path (out , outsz , argv0 );
673+ }
674+ }
675+ #endif
676+
677+ if (resolve_self_executable (out , outsz )) {
678+ return true;
679+ }
680+ return copy_path (out , outsz , argv0 );
681+ }
682+
585683void cbm_http_server_set_binary_path (const char * path ) {
586684 if (path ) {
587- snprintf (g_binary_path , sizeof (g_binary_path ), "%s" , path );
685+ if (!cbm_http_server_resolve_binary_path (path , g_binary_path , sizeof (g_binary_path ))) {
686+ g_binary_path [0 ] = '\0' ;
687+ }
588688 }
589689}
590690
@@ -597,16 +697,7 @@ static void *index_thread_fn(void *arg) {
597697 const char * bin = g_binary_path ;
598698 char self_path [1024 ] = {0 };
599699 if (!bin [0 ]) {
600- #ifdef _WIN32
601- GetModuleFileNameA (NULL , self_path , sizeof (self_path ));
602- #elif defined(__APPLE__ )
603- uint32_t sz = sizeof (self_path );
604- _NSGetExecutablePath (self_path , & sz );
605- #else
606- ssize_t len = readlink ("/proc/self/exe" , self_path , sizeof (self_path ) - 1 );
607- if (len > 0 )
608- self_path [len ] = '\0' ;
609- #endif
700+ cbm_http_server_resolve_binary_path (NULL , self_path , sizeof (self_path ));
610701 bin = self_path [0 ] ? self_path : "codebase-memory-mcp" ;
611702 }
612703
0 commit comments