Skip to content

Commit

Permalink
mx_proc: Add mx_proc_get_comm
Browse files Browse the repository at this point in the history
Add function to get "comm"-value of a process.
  • Loading branch information
donald committed Sep 16, 2021
1 parent 16a3711 commit 89aca1f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
24 changes: 24 additions & 0 deletions mx_proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -484,3 +484,27 @@ pid_t mx_proc_get_parent(pid_t pid) {
return -1;
return parent;
}

/*
* get "comm" value of a process.
* buf MUST point to char[16] array
* return NULL on any error, otherwise pointer to buf
*/
char *mx_proc_get_comm(pid_t pid, char *buf) {
char commfilename[32]; // "/proc/2147483647/comm" = 22
FILE *commfile;
char *p;

snprintf(commfilename, sizeof(commfilename), "/proc/%d/comm", pid);
commfile = fopen(commfilename, "r");
if (commfile == NULL)
return NULL;
p = fgets(buf, 16, commfile);
fclose(commfile);
if (p == NULL)
return NULL;
p = strrchr(buf, '\n');
if (p != NULL)
*p = '\0';
return (char *)buf;
}
1 change: 1 addition & 0 deletions mx_proc.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,5 @@ int mx_proc_tree_free(struct mx_proc_tree **tree);

struct mx_proc_info *mx_proc_tree_proc_info(struct mx_proc_tree *tree, pid_t pid);
pid_t mx_proc_get_parent(pid_t pid);
char *mx_proc_get_comm(pid_t pid, char *buf);
#endif

0 comments on commit 89aca1f

Please sign in to comment.