-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8626958
commit 8c351bc
Showing
5 changed files
with
186 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
void free_argv(char **); | ||
char **kinfo_getargv(pid_t pid); | ||
|
||
#ifdef __NetBSD__ | ||
struct kinfo_proc *kinfo_getallproc(int *); | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#include <sys/cdefs.h> | ||
#include <sys/param.h> | ||
#include <sys/sysctl.h> | ||
#include <sys/proc.h> | ||
#include <stdlib.h> | ||
|
||
#if defined(__NetBSD__) | ||
#define kinfo_proc kinfo_proc2 | ||
#endif | ||
|
||
#include "extern.h" | ||
|
||
/* | ||
* Sort processes by pid | ||
*/ | ||
static int | ||
kinfo_proc_compare(const void *a, const void *b) | ||
{ | ||
|
||
return ((const struct kinfo_proc *)a)->p_pid - | ||
((const struct kinfo_proc *)b)->p_pid; | ||
} | ||
|
||
static void | ||
kinfo_proc_sort(struct kinfo_proc *kipp, int count) | ||
{ | ||
|
||
qsort(kipp, count, sizeof(*kipp), kinfo_proc_compare); | ||
} | ||
|
||
struct kinfo_proc * | ||
kinfo_getallproc(int *cntp) | ||
{ | ||
struct kinfo_proc *kipp; | ||
size_t len; | ||
int mib[6]; | ||
|
||
mib[0] = CTL_KERN; | ||
mib[1] = KERN_PROC2; | ||
mib[2] = KERN_PROC_ALL; | ||
mib[3] = 0; | ||
mib[4] = sizeof(struct kinfo_proc2); | ||
mib[5] = 0; | ||
|
||
len = 0; | ||
if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) | ||
return (NULL); | ||
mib[5] = (int) (len / sizeof(struct kinfo_proc2)); | ||
|
||
kipp = malloc(len); | ||
if (kipp == NULL) | ||
return (NULL); | ||
|
||
if (sysctl(mib, 6, kipp, &len, NULL, 0) < 0) | ||
goto bad; | ||
*cntp = len / sizeof(*kipp); | ||
kinfo_proc_sort(kipp, len / sizeof(*kipp)); | ||
return (kipp); | ||
bad: | ||
*cntp = 0; | ||
free(kipp); | ||
return (NULL); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#include <sys/cdefs.h> | ||
#include <sys/param.h> | ||
#include <sys/sysctl.h> | ||
#include <sys/proc.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
#include "extern.h" | ||
|
||
static int | ||
memnchr(const void *p, int c, size_t len) | ||
{ | ||
int n = 0; | ||
|
||
for (size_t i = 0; i < len; i++) | ||
if (*((const unsigned char *)p + i) == c) | ||
n++; | ||
|
||
return n; | ||
} | ||
|
||
void | ||
free_argv(char **argv) | ||
{ | ||
char **argvp = argv; | ||
|
||
while (*argvp != NULL) { | ||
free(*argvp); | ||
argvp++; | ||
} | ||
|
||
free(argv); | ||
} | ||
|
||
char ** | ||
kinfo_getargv(pid_t pid) | ||
{ | ||
char *buf = NULL; | ||
char **argv; | ||
int mib[4]; | ||
int i, argc; | ||
size_t off = 0, len = ARG_MAX; | ||
|
||
buf = malloc(len); | ||
if (buf == NULL) | ||
return (NULL); | ||
|
||
mib[0] = CTL_KERN; | ||
#if defined(__FreeBSD__) | ||
mib[1] = KERN_PROC; | ||
mib[2] = KERN_PROC_ARGS; | ||
mib[3] = pid; | ||
#elif defined(__NetBSD__) | ||
mib[1] = KERN_PROC_ARGS; | ||
mib[2] = pid; | ||
mib[3] = KERN_PROC_ARGV; | ||
#endif | ||
|
||
if (sysctl(mib, 4, buf, &len, NULL, 0) < 0) | ||
goto bad; | ||
buf[len] = '\0'; | ||
|
||
argc = memnchr(buf, '\0', len); | ||
argv = malloc((argc + 1) * sizeof(char *)); | ||
if (argv == NULL) | ||
goto bad; | ||
|
||
for (i = 0; i < argc; i++) { | ||
argv[i] = strdup(buf + off); | ||
if (argv[i] == NULL) { | ||
free_argv(argv); | ||
goto bad; | ||
} | ||
off += strlen(argv[i]) + 1; | ||
} | ||
argv[argc] = NULL; | ||
|
||
free(buf); | ||
return (argv); | ||
|
||
bad: | ||
free(buf); | ||
return (NULL); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters