From 31c53388425d84408a6c28374f4a3c3b667e12e5 Mon Sep 17 00:00:00 2001 From: TurBoss Date: Fri, 8 May 2026 02:14:24 +0200 Subject: [PATCH 1/4] check if db_tool exists --- src/emc/tooldata/tooldata_db.cc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/emc/tooldata/tooldata_db.cc b/src/emc/tooldata/tooldata_db.cc index 32df1388636..38053907c7d 100644 --- a/src/emc/tooldata/tooldata_db.cc +++ b/src/emc/tooldata/tooldata_db.cc @@ -256,6 +256,10 @@ int tooldata_db_init(char progname_plus_args[],int random_toolchanger) snprintf(db_childname,sizeof(db_childname),"%s",child_argv[0]); is_random_toolchanger = random_toolchanger; + if (access(child_argv[0], F_OK)){ + fprintf(stderr,"!!!!db_init: <%s> not found\n",child_argv[0]); + return -1; + } if (access(child_argv[0],X_OK)) { fprintf(stderr,"!!!!db_init: <%s> not executable\n",child_argv[0]); return -1; From 4c6cadb278292e97c7f786787f1bb6268aee4bec Mon Sep 17 00:00:00 2001 From: TurBoss Date: Fri, 8 May 2026 02:30:37 +0200 Subject: [PATCH 2/4] search for tool_db in PATH env variable --- src/emc/tooldata/tooldata_db.cc | 58 ++++++++++++++++++++++++++++----- 1 file changed, 50 insertions(+), 8 deletions(-) diff --git a/src/emc/tooldata/tooldata_db.cc b/src/emc/tooldata/tooldata_db.cc index 38053907c7d..26efadf2b6e 100644 --- a/src/emc/tooldata/tooldata_db.cc +++ b/src/emc/tooldata/tooldata_db.cc @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -30,6 +31,7 @@ #define DB_VERSION "v2.1" #define MAX_DB_PROGRAM_ARGS 10 #define UNEXPECTED_MSG fprintf(stderr,"UNEXPECTED %s %d\n",__FILE__,__LINE__); +#define MAX_CMD_LEN 1024 static bool db_live = 0; static bool db_show = 0; // use environmental var: DB_SHOW @@ -243,6 +245,13 @@ int tooldata_db_init(char progname_plus_args[],int random_toolchanger) char* child_argv[MAX_DB_PROGRAM_ARGS] = {0}; char* saveptr; char* token = strtok_r(progname_plus_args, " ", &saveptr); + int n; + char* fork_argv[MAX_DB_PROGRAM_ARGS] = {0}; + char* cp; + char* envpath; + char prog_name[MAX_CMD_LEN+1] = "\0"; + char prog_path[MAX_CMD_LEN+1]; + struct stat stat_buf; while (token != NULL) { child_argv[child_argc] = token; child_argc++; @@ -255,17 +264,50 @@ int tooldata_db_init(char progname_plus_args[],int random_toolchanger) } snprintf(db_childname,sizeof(db_childname),"%s",child_argv[0]); is_random_toolchanger = random_toolchanger; - - if (access(child_argv[0], F_OK)){ - fprintf(stderr,"!!!!db_init: <%s> not found\n",child_argv[0]); + envpath = getenv( (char*)"PATH"); + if ( envpath != NULL ) { + while ( *envpath != '\0' ) { + /* copy a single directory from the PATH env variable */ + n = 0; + while ( (*envpath != ':') && (*envpath != '\0') && (n < MAX_CMD_LEN)) { + prog_path[n++] = *envpath++; + } + /* append '/' and program name */ + if ( n < MAX_CMD_LEN ) { + prog_path[n++] = '/'; + } + cp = child_argv[0]; + while ((*cp != '\0') && ( n < MAX_CMD_LEN)) { + prog_path[n++] = *cp++; + } + prog_path[n] = '\0'; + //fprintf(stdout,"Trying '%s'\n",prog_path); + if ( stat(prog_path, &stat_buf) != 0 ) { + /* no luck, clear prog_path to indicate failure */ + prog_path[0] = '\0'; + /* and get ready to try the next directory */ + if ( *envpath == ':' ) { + envpath++; + } + } else { + /* success, break out of loop */ + strncpy(prog_name, child_argv[0], MAX_CMD_LEN); + fork_argv[0] = prog_path; + + break; + } + } + } + if (access(prog_path, F_OK)){ + fprintf(stderr,"!!!!db_init: <%s> not found\n",prog_name); return -1; } - if (access(child_argv[0],X_OK)) { - fprintf(stderr,"!!!!db_init: <%s> not executable\n",child_argv[0]); + if (access(prog_path,X_OK)) { + fprintf(stderr,"!!!!db_init: <%s> not executable\n",prog_name); return -1; } //fprintf(stderr,"=====db_childname=%s\n",db_childname); - if (0 == fork_create(child_argc,child_argv) ) { + if (0 == fork_create(child_argc,fork_argv) ) { //fprintf(stderr,"=====db_init forked %s\n",child_argv[0]); } else { fprintf(stderr,"!!!!!db_init: fork FAIL\n"); @@ -275,7 +317,7 @@ int tooldata_db_init(char progname_plus_args[],int random_toolchanger) // block for response fprintf(stderr,"====Waiting for %s version reply from %s\n", - DB_VERSION,child_argv[0]); + DB_VERSION,prog_name); char reply[CANON_TOOL_ENTRY_LEN]; if (read_reply(reply,sizeof(reply)) < 0 ) { @@ -290,7 +332,7 @@ int tooldata_db_init(char progname_plus_args[],int random_toolchanger) db_live = 0; return -1; } else { - fprintf(stderr,"====Connected to %s\n",child_argv[0]); + fprintf(stderr,"====Connected to %s\n",prog_name); } } if (db_debug) { From 99ef0a570a65dd875ef02d1305b6c58dfb71c09a Mon Sep 17 00:00:00 2001 From: TurBoss Date: Sat, 9 May 2026 15:00:08 +0200 Subject: [PATCH 3/4] check if DB_PROGRAM is absolute, relative or in PATH --- src/emc/tooldata/tooldata_db.cc | 94 +++++++++++++++++++-------------- 1 file changed, 55 insertions(+), 39 deletions(-) diff --git a/src/emc/tooldata/tooldata_db.cc b/src/emc/tooldata/tooldata_db.cc index 26efadf2b6e..e57ebd2ef65 100644 --- a/src/emc/tooldata/tooldata_db.cc +++ b/src/emc/tooldata/tooldata_db.cc @@ -246,11 +246,11 @@ int tooldata_db_init(char progname_plus_args[],int random_toolchanger) char* saveptr; char* token = strtok_r(progname_plus_args, " ", &saveptr); int n; - char* fork_argv[MAX_DB_PROGRAM_ARGS] = {0}; + char* fork_argv[PATH_MAX] = {0}; char* cp; char* envpath; - char prog_name[MAX_CMD_LEN+1] = "\0"; - char prog_path[MAX_CMD_LEN+1]; + char prog_name[PATH_MAX] = "\0"; + char prog_path[PATH_MAX]; struct stat stat_buf; while (token != NULL) { child_argv[child_argc] = token; @@ -264,46 +264,62 @@ int tooldata_db_init(char progname_plus_args[],int random_toolchanger) } snprintf(db_childname,sizeof(db_childname),"%s",child_argv[0]); is_random_toolchanger = random_toolchanger; - envpath = getenv( (char*)"PATH"); - if ( envpath != NULL ) { - while ( *envpath != '\0' ) { - /* copy a single directory from the PATH env variable */ - n = 0; - while ( (*envpath != ':') && (*envpath != '\0') && (n < MAX_CMD_LEN)) { - prog_path[n++] = *envpath++; + if (db_childname[0] != '/') { // prog path not absolute + if(strchr(db_childname, '/') != NULL){ // prog path is relative + strncpy(prog_path, db_childname, PATH_MAX); + } + else { // just prog name, search in the actual directory or PATH + envpath = getenv( (char*)"PATH"); + if ( envpath != NULL ) { + while ( *envpath != '\0' ) { + /* copy a single directory from the PATH env variable */ + n = 0; + while ( (*envpath != ':') && (*envpath != '\0') && (n < MAX_CMD_LEN)) { + prog_path[n++] = *envpath++; + } + /* append '/' and program name */ + if ( n < MAX_CMD_LEN ) { + prog_path[n++] = '/'; + } + cp = db_childname; + while ((*cp != '\0') && ( n < MAX_CMD_LEN)) { + prog_path[n++] = *cp++; + } + prog_path[n] = '\0'; + fprintf(stdout,"Trying '%s'\n",prog_path); + if ( stat(prog_path, &stat_buf) != 0 ) { + /* no luck, clear prog_path to indicate failure */ + prog_path[0] = '\0'; + /* and get ready to try the next directory */ + if ( *envpath == ':' ) { + envpath++; + } + } else { + /* success, break out of loop */ + + break; + } + } + } - /* append '/' and program name */ - if ( n < MAX_CMD_LEN ) { - prog_path[n++] = '/'; - } - cp = child_argv[0]; - while ((*cp != '\0') && ( n < MAX_CMD_LEN)) { - prog_path[n++] = *cp++; - } - prog_path[n] = '\0'; - //fprintf(stdout,"Trying '%s'\n",prog_path); - if ( stat(prog_path, &stat_buf) != 0 ) { - /* no luck, clear prog_path to indicate failure */ - prog_path[0] = '\0'; - /* and get ready to try the next directory */ - if ( *envpath == ':' ) { - envpath++; - } - } else { - /* success, break out of loop */ - strncpy(prog_name, child_argv[0], MAX_CMD_LEN); - fork_argv[0] = prog_path; - - break; - } - } + } + } + else { + strncpy(prog_path, db_childname, PATH_MAX); + } + if (prog_path[0] == '\0'){ + strncpy(prog_name, db_childname, PATH_MAX); + fork_argv[0] = prog_name; + } + else { + fork_argv[0] = prog_path; } if (access(prog_path, F_OK)){ - fprintf(stderr,"!!!!db_init: <%s> not found\n",prog_name); + fprintf(stderr,"!!!!db_init: <%s> not found\n",fork_argv[0]); return -1; } if (access(prog_path,X_OK)) { - fprintf(stderr,"!!!!db_init: <%s> not executable\n",prog_name); + fprintf(stderr,"!!!!db_init: <%s> not executable\n",fork_argv[0]); return -1; } //fprintf(stderr,"=====db_childname=%s\n",db_childname); @@ -317,7 +333,7 @@ int tooldata_db_init(char progname_plus_args[],int random_toolchanger) // block for response fprintf(stderr,"====Waiting for %s version reply from %s\n", - DB_VERSION,prog_name); + DB_VERSION,fork_argv[0]); char reply[CANON_TOOL_ENTRY_LEN]; if (read_reply(reply,sizeof(reply)) < 0 ) { @@ -332,7 +348,7 @@ int tooldata_db_init(char progname_plus_args[],int random_toolchanger) db_live = 0; return -1; } else { - fprintf(stderr,"====Connected to %s\n",prog_name); + fprintf(stderr,"====Connected to %s\n",fork_argv[0]); } } if (db_debug) { From e5eaa410ff0878f277e690e7bfcaf07bfe493d14 Mon Sep 17 00:00:00 2001 From: TurBoss Date: Sat, 9 May 2026 15:44:36 +0200 Subject: [PATCH 4/4] fix: wrong size --- src/emc/tooldata/tooldata_db.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/emc/tooldata/tooldata_db.cc b/src/emc/tooldata/tooldata_db.cc index e57ebd2ef65..b48f621b66f 100644 --- a/src/emc/tooldata/tooldata_db.cc +++ b/src/emc/tooldata/tooldata_db.cc @@ -246,7 +246,7 @@ int tooldata_db_init(char progname_plus_args[],int random_toolchanger) char* saveptr; char* token = strtok_r(progname_plus_args, " ", &saveptr); int n; - char* fork_argv[PATH_MAX] = {0}; + char* fork_argv[MAX_DB_PROGRAM_ARGS] = {0}; char* cp; char* envpath; char prog_name[PATH_MAX] = "\0";