Skip to content

Commit

Permalink
improve setting command path in MacOS
Browse files Browse the repository at this point in the history
  • Loading branch information
HiGarfield committed Sep 28, 2024
1 parent ddef98a commit c9f297a
Showing 1 changed file with 37 additions and 11 deletions.
48 changes: 37 additions & 11 deletions src/process_iterator_apple.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,22 +93,48 @@ int init_process_iterator(struct process_iterator *it, struct process_filter *fi
return 0;
}

static void pti2proc(struct proc_taskallinfo *ti, struct process *process)
static int get_process_path(int pid, char *pathbuf, size_t buf_size)
{
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')
int mib[3] = {CTL_KERN, KERN_ARGMAX, pid};
size_t len;
char **argv;

if (sysctl(mib, 3, &len, &len, NULL, 0) == -1)
{
perror("sysctl");
return -1;
}

argv = (char **)malloc(len);
if (argv == NULL)
{
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);
perror("malloc");
return -1;
}
else

mib[1] = KERN_PROCARGS2;

if (sysctl(mib, 3, argv, &len, NULL, 0) == -1)
{
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);
perror("sysctl");
free(argv);
return -1;
}
process->command[process->max_cmd_len] = '\0';

strcpy(pathbuf, argv[0]);

free(argv);

return 0;
}

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;
get_process_path(ti->pbsd.pbi_pid, process->command, sizeof(process->command));
process->max_cmd_len = sizeof(process->command) - 1;
}

static int get_process_pti(pid_t pid, struct proc_taskallinfo *ti)
Expand Down

0 comments on commit c9f297a

Please sign in to comment.