Skip to content

Commit

Permalink
prefer name matches for processes and modules instead of path matches
Browse files Browse the repository at this point in the history
  • Loading branch information
rdbo committed Sep 29, 2024
1 parent a899131 commit 8138d7f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
7 changes: 7 additions & 0 deletions include/libmem/libmem.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@
#define LM_PATH_MAX (4096) /* Fits up to 1024 4-byte UTF-8 characters */
#define LM_INST_MAX (16) /* Maximum size of a single instruction */

/* Path separator */
#ifdef _WIN32
# define LM_PATHSEP '\\'
#else
# define LM_PATHSEP '/'
#endif

/* Helpers */
#define LM_ARRLEN(arr) (sizeof(arr) / sizeof(arr[0]))
#define LM_CHECK_PROT(prot) ((prot & LM_PROT_XRW) == prot)
Expand Down
28 changes: 19 additions & 9 deletions src/common/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,32 @@ LM_EnumModules(lm_bool_t (LM_CALL *callback)(lm_module_t *module,
typedef struct {
lm_string_t name;
lm_size_t name_len;
lm_bool_t match_path;
lm_module_t *module_out;
} find_module_t;

lm_bool_t LM_CALL
find_module_callback(lm_module_t *module, lm_void_t *arg)
{
find_module_t *parg = (find_module_t *)arg;
lm_size_t len;

len = strlen(module->path);
if (len < parg->name_len)
return LM_TRUE;

/* Compare the last characters from the path against the name */
if (!strcmp(&module->path[len - parg->name_len], parg->name)) {
*parg->module_out = *module;
return LM_FALSE;
if (parg->match_path) {
lm_size_t len;

len = strlen(module->path);
if (len < parg->name_len)
return LM_TRUE;

/* Compare the last characters from the path against the name */
if (!strcmp(&module->path[len - parg->name_len], parg->name)) {
*parg->module_out = *module;
return LM_FALSE;
}
} else {
if (!strcmp(module->name, parg->name)) {
*parg->module_out = *module;
return LM_FALSE;
}
}

return LM_TRUE;
Expand All @@ -79,6 +88,7 @@ LM_FindModule(lm_string_t name,
arg.name = name;
arg.name_len = strlen(name);
arg.module_out = module_out;
arg.match_path = strchr(name, LM_PATHSEP) ? LM_TRUE : LM_FALSE;

return LM_EnumModules(find_module_callback, (lm_void_t *)&arg) == LM_TRUE && module_out->size > 0 ?
LM_TRUE : LM_FALSE;
Expand Down
17 changes: 14 additions & 3 deletions src/common/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,33 @@
typedef struct {
lm_process_t *process_out;
lm_string_t process_name;
lm_bool_t match_path;
lm_size_t len;
} find_pid_t;

lm_bool_t LM_CALL
find_process_callback(lm_process_t *process, lm_void_t *arg)
{
find_pid_t *parg = (find_pid_t *)arg;
lm_size_t len;

assert(process && parg);

len = strlen(process->path);
if (len && len >= parg->len) {
if (parg->match_path) {
lm_size_t len;

len = strlen(process->path);
if (len < parg->len)
return LM_TRUE;

if (!strcmp(&process->path[len - parg->len], parg->process_name)) {
*(parg->process_out) = *process;
return LM_FALSE;
}
} else {
if (!strcmp(process->name, parg->process_name)) {
*(parg->process_out) = *process;
return LM_FALSE;
}
}

return LM_TRUE;
Expand All @@ -62,6 +72,7 @@ LM_FindProcess(lm_string_t process_name,
arg.process_out = process_out;
arg.process_out->pid = LM_PID_BAD;
arg.process_name = process_name;
arg.match_path = strchr(process_name, LM_PATHSEP) ? LM_TRUE : LM_FALSE;
arg.len = strlen(arg.process_name);

LM_EnumProcesses(find_process_callback, (lm_void_t *)&arg);
Expand Down

0 comments on commit 8138d7f

Please sign in to comment.