Move process information code from sys to proc

pull/13/head
Daniel Roethlisberger 10 years ago
parent 6adaf00540
commit 966fe80c0c

@ -47,7 +47,7 @@
#ifdef HAVE_DARWIN_LIBPROC #ifdef HAVE_DARWIN_LIBPROC
int int
proc_lookup_by_addr(pid_t *result, struct sockaddr *dst_addr, proc_pid_for_addr(pid_t *result, struct sockaddr *dst_addr,
UNUSED socklen_t dst_addrlen) UNUSED socklen_t dst_addrlen)
{ {
pid_t *pids = NULL; pid_t *pids = NULL;
@ -144,10 +144,10 @@ errout1:
return ret; return ret;
} }
#else /* HAVE_DARWIN_LIBPROC */ #else /* !HAVE_DARWIN_LIBPROC */
int int
proc_lookup_by_addr(pid_t *result, UNUSED struct sockaddr *dst_addr, proc_pid_for_addr(pid_t *result, UNUSED struct sockaddr *dst_addr,
UNUSED socklen_t dst_addrlen) { UNUSED socklen_t dst_addrlen) {
*result = -1; *result = -1;
return 0; return 0;
@ -155,4 +155,43 @@ proc_lookup_by_addr(pid_t *result, UNUSED struct sockaddr *dst_addr,
#endif /* !HAVE_DARWIN_LIBPROC */ #endif /* !HAVE_DARWIN_LIBPROC */
/*
* Fetch process info for the given pid.
* On success, returns 0 and fills in path, uid, and gid.
* Caller must free returned path string.
* Returns -1 on failure, or if unsupported on this platform.
*/
int
proc_get_info(pid_t pid, char **path, uid_t *uid, gid_t *gid) {
#if HAVE_DARWIN_LIBPROC
/* fetch process structure */
struct proc_bsdinfo bsd_info;
if (proc_pidinfo(pid, PROC_PIDTBSDINFO, 0, &bsd_info,
sizeof(bsd_info)) == -1) {
return -1;
}
*uid = bsd_info.pbi_uid;
*gid = bsd_info.pbi_gid;
/* fetch process path */
*path = malloc(PROC_PIDPATHINFO_MAXSIZE);
if (!*path) {
return -1;
}
int path_len = proc_pidpath(pid, *path, PROC_PIDPATHINFO_MAXSIZE);
if (path_len == -1) {
free(*path);
return -1;
}
return 0;
#else /* !HAVE_DARWIN_LIBPROC */
/* unsupported */
return -1;
#endif /* !HAVE_DARWIN_LIBPROC */
}
/* vim: set noet ft=c: */ /* vim: set noet ft=c: */

@ -36,8 +36,8 @@
#include <event2/util.h> #include <event2/util.h>
int proc_lookup_by_addr(pid_t *, struct sockaddr *, socklen_t) WUNRES NONNULL(1,2); int proc_pid_for_addr(pid_t *, struct sockaddr *, socklen_t) WUNRES NONNULL(1,2);
int proc_get_info(pid_t, char **, uid_t *, gid_t *) WUNRES NONNULL(2,3,4);
#endif /* !PROC_H */ #endif /* !PROC_H */

@ -1580,12 +1580,12 @@ pxy_bev_eventcb(struct bufferevent *bev, short events, void *arg)
} }
/* fetch process info */ /* fetch process info */
if (proc_lookup_by_addr(&ctx->pid, if (proc_pid_for_addr(&ctx->pid,
(struct sockaddr*)&ctx->addr, (struct sockaddr*)&ctx->addr,
ctx->addrlen) == 0 && ctx->addrlen) == 0 &&
ctx->pid != -1 && ctx->pid != -1 &&
sys_proc_info(ctx->pid, &ctx->exec_path, proc_get_info(ctx->pid, &ctx->exec_path,
&ctx->uid, &ctx->gid) == 0) { &ctx->uid, &ctx->gid) == 0) {
/* fetch user/group names */ /* fetch user/group names */
ctx->user = sys_user_str(ctx->uid); ctx->user = sys_user_str(ctx->uid);
ctx->group = sys_group_str(ctx->gid); ctx->group = sys_group_str(ctx->gid);

36
sys.c

@ -197,42 +197,6 @@ sys_pidf_close(int fd, const char *fn)
close(fd); close(fd);
} }
/*
* Fetch process info for the given pid.
* On success, returns 0 and fills in path, uid, and gid.
* Caller must free returned path string.
* Returns -1 on failure, or if unsupported on this platform.
*/
int
sys_proc_info(pid_t pid, char **path, uid_t *uid, gid_t *gid) {
#if HAVE_DARWIN_LIBPROC
/* fetch process structure */
struct proc_bsdinfo bsd_info;
if (proc_pidinfo(pid, PROC_PIDTBSDINFO, 0, &bsd_info, sizeof(bsd_info)) == -1) {
return -1;
}
*uid = bsd_info.pbi_uid;
*gid = bsd_info.pbi_gid;
/* fetch process path */
*path = malloc(PROC_PIDPATHINFO_MAXSIZE);
if (!*path) {
return -1;
}
int path_len = proc_pidpath(pid, *path, PROC_PIDPATHINFO_MAXSIZE);
if (path_len == -1) {
free(*path);
return -1;
}
return 0;
#else
/* unsupported */
return -1;
#endif
}
/* /*
* Converts a local uid into a printable string representation. * Converts a local uid into a printable string representation.
* Returns an allocated buffer which must be freed by caller, or NULL on error. * Returns an allocated buffer which must be freed by caller, or NULL on error.

@ -41,8 +41,6 @@ int sys_pidf_open(const char *) NONNULL(1) WUNRES;
int sys_pidf_write(int) WUNRES; int sys_pidf_write(int) WUNRES;
void sys_pidf_close(int, const char *) NONNULL(2); void sys_pidf_close(int, const char *) NONNULL(2);
int sys_proc_info(pid_t, char **, uid_t *, gid_t *) WUNRES NONNULL(2,3,4);
char * sys_user_str(uid_t) WUNRES MALLOC; char * sys_user_str(uid_t) WUNRES MALLOC;
char * sys_group_str(gid_t) WUNRES MALLOC; char * sys_group_str(gid_t) WUNRES MALLOC;

Loading…
Cancel
Save