Skip to content

Commit

Permalink
improve command name comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
HiGarfield committed Sep 28, 2024
1 parent ddef98a commit 570b383
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 38 deletions.
15 changes: 2 additions & 13 deletions src/process_group.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,14 @@ pid_t find_process_by_name(char *process_name)
struct process_iterator it;
struct process proc;
struct process_filter filter;
static char process_basename[PATH_MAX + 1];
static char command_basename[PATH_MAX + 1];
strncpy(process_basename, basename(process_name),
sizeof(process_basename) - 1);
process_basename[sizeof(process_basename) - 1] = '\0';
const char *process_basename = basename(process_name);
filter.pid = 0;
filter.include_children = 0;
init_process_iterator(&it, &filter);
while (get_next_process(&it, &proc) != -1)
{
int cmp_len;
strncpy(command_basename, basename(proc.command),
sizeof(command_basename) - 1);
command_basename[sizeof(command_basename) - 1] = '\0';
cmp_len = proc.max_cmd_len -
(strlen(proc.command) - strlen(command_basename));
/* process found */
if (cmp_len > 0 && command_basename[0] != '\0' &&
strncmp(command_basename, process_basename, cmp_len) == 0)
if (strcmp(basename(proc.command), process_basename) == 0)
{
if (pid < 0)
{
Expand Down
3 changes: 0 additions & 3 deletions src/process_iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ struct process

/* Absolute path of the executable file */
char command[PATH_MAX + 1];

/* Maximum command length */
int max_cmd_len;
};

/**
Expand Down
12 changes: 1 addition & 11 deletions src/process_iterator_apple.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,7 @@ static void pti2proc(struct proc_taskallinfo *ti, struct process *process)
process->pid = ti->pbsd.pbi_pid;
process->ppid = ti->pbsd.pbi_ppid;
process->cputime = ti->ptinfo.pti_total_user / 1e6 + ti->ptinfo.pti_total_system / 1e6;
if (ti->pbsd.pbi_name[0] != '\0')
{
process->max_cmd_len = MIN(sizeof(process->command), sizeof(ti->pbsd.pbi_name)) - 1;
strncpy(process->command, ti->pbsd.pbi_name, process->max_cmd_len);
}
else
{
process->max_cmd_len = MIN(sizeof(process->command), sizeof(ti->pbsd.pbi_comm)) - 1;
strncpy(process->command, ti->pbsd.pbi_comm, process->max_cmd_len);
}
process->command[process->max_cmd_len] = '\0';
proc_pidpath(ti->pbsd.pbi_pid, process->command, sizeof(process->command));
}

static int get_process_pti(pid_t pid, struct proc_taskallinfo *ti)
Expand Down
9 changes: 5 additions & 4 deletions src/process_iterator_freebsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,15 @@ int init_process_iterator(struct process_iterator *it, struct process_filter *fi
static void kproc2proc(kvm_t *kd, struct kinfo_proc *kproc, struct process *proc)
{
char **args;
size_t len_max;
proc->pid = kproc->ki_pid;
proc->ppid = kproc->ki_ppid;
proc->cputime = kproc->ki_runtime / 1000.0;
proc->max_cmd_len = sizeof(proc->command) - 1;
if ((args = kvm_getargv(kd, kproc, sizeof(proc->command))) != NULL)
len_max = sizeof(proc->command);
if ((args = kvm_getargv(kd, kproc, len_max)) != NULL)
{
strncpy(proc->command, args[0], proc->max_cmd_len);
proc->command[proc->max_cmd_len] = '\0';
strncpy(proc->command, args[0], len_max);
proc->command[len_max] = '\0';
}
else
{
Expand Down
4 changes: 0 additions & 4 deletions src/process_iterator_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,6 @@ static int read_process_info(pid_t pid, struct process *p)
{
ret = -1;
}
else
{
p->max_cmd_len = sizeof(p->command) - 1;
}
fclose(fd);
}
else
Expand Down
4 changes: 1 addition & 3 deletions tests/process_iterator_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ static void test_process_name(void)
struct process_filter filter;
static char command_basename[PATH_MAX + 1];
static char process_basename[PATH_MAX + 1];
int cmp_len;
filter.pid = getpid();
filter.include_children = 0;
init_process_iterator(&it, &filter);
Expand All @@ -214,8 +213,7 @@ static void test_process_name(void)
command_basename[sizeof(command_basename) - 1] = '\0';
strncpy(process_basename, basename(process.command), sizeof(process_basename) - 1);
process_basename[sizeof(process_basename) - 1] = '\0';
cmp_len = process.max_cmd_len - (strlen(process.command) - strlen(process_basename));
assert(strncmp(command_basename, process_basename, cmp_len) == 0);
assert(strcmp(command_basename, process_basename) == 0);
assert(get_next_process(&it, &process) != 0);
close_process_iterator(&it);
}
Expand Down

0 comments on commit 570b383

Please sign in to comment.