-
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
19decb3
commit 0142173
Showing
7 changed files
with
155 additions
and
23 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 |
---|---|---|
@@ -1,8 +1,27 @@ | ||
#include <sys/types.h> | ||
|
||
int memnchr(const void *, int, size_t); | ||
void free_argv(char **); | ||
char **kinfo_getargv(pid_t pid); | ||
|
||
#ifdef __NetBSD__ | ||
#define kinfo_proc kinfo_proc2 | ||
#endif | ||
|
||
#if defined(__NetBSD__) || defined(__DragonFly__) | ||
struct kinfo_proc *kinfo_getallproc(int *); | ||
#endif | ||
|
||
#ifdef __DragonFly__ | ||
struct kinfo_vmentry { | ||
int kve_type; /* Type of map entry. */ | ||
int kve_protection; /* Protection bitmask. */ | ||
char kve_path[PATH_MAX]; /* Path to VM obj, if any. */ | ||
}; | ||
|
||
#define KVME_TYPE_VNODE 2 | ||
#define KVME_PROT_EXEC 4 | ||
|
||
struct kinfo_vmentry *kinfo_getvmmap(pid_t, 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
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,78 @@ | ||
#include <sys/types.h> | ||
#include <sys/stat.h> | ||
#include <fcntl.h> | ||
#include <limits.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <unistd.h> | ||
|
||
#include "extern.h" | ||
|
||
static char * | ||
procmap(pid_t pid) | ||
{ | ||
char path[PATH_MAX]; | ||
size_t size = 65536; | ||
char *buf; | ||
int fd; | ||
|
||
(void) snprintf(path, sizeof(path), "/proc/%d/map", pid); | ||
if ((fd = open(path, O_RDONLY)) == -1) | ||
return (NULL); | ||
|
||
if ((buf = calloc(1, size)) == NULL) | ||
return (NULL); | ||
|
||
if (read(fd, buf, size - 1) == -1) | ||
goto bad; | ||
|
||
(void) close(fd); | ||
return (buf); | ||
|
||
bad: | ||
(void) close(fd); | ||
return (NULL); | ||
} | ||
|
||
/* Minimal kinfo_getvmmap */ | ||
struct kinfo_vmentry * | ||
kinfo_getvmmap(pid_t pid, int *cntp) | ||
{ | ||
struct kinfo_vmentry *kiv; | ||
struct stat st; | ||
char prot[4], type[10]; | ||
char *buf; | ||
|
||
if ((buf = procmap(pid)) == NULL) | ||
return (NULL); | ||
|
||
*cntp = memnchr(buf, '\n', strlen(buf)); | ||
kiv = calloc(*cntp, sizeof(struct kinfo_vmentry)); | ||
if (kiv == NULL) | ||
goto bad2; | ||
|
||
char *line = buf; | ||
for (int i = 0; i < *cntp; i++) { | ||
char *token = strsep(&line, "\n"); | ||
if (token == NULL) | ||
break; | ||
/* | ||
* Parse lines like this: | ||
* 0x0000000000400000 0x0000000000403000 -1 -1 0xfffff80119599400 r-x 2 0 0x0000 COW NC vnode /bin/cat | ||
*/ | ||
(void) sscanf(token, "%*x %*x %*d %*d %*x %3s %*d %*d %*x %*s %*s %10s %4095[^\n]", prot, type, &kiv[i].kve_path); | ||
kiv[i].kve_protection = (prot[2] == 'x') ? KVME_PROT_EXEC : 0; | ||
if (!strcmp(type, "vnode")) { | ||
kiv[i].kve_type |= KVME_TYPE_VNODE; | ||
if (lstat(kiv[i].kve_path, &st) < 0) | ||
kiv[i].kve_path[0] = '\0'; | ||
} | ||
} | ||
|
||
return kiv; | ||
|
||
bad2: | ||
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
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,13 @@ | ||
#include <stddef.h> | ||
|
||
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; | ||
} |