From 966fe80c0c5e9483d732a99443fc35e7f43fb6de Mon Sep 17 00:00:00 2001 From: Daniel Roethlisberger Date: Fri, 14 Nov 2014 01:01:14 +0100 Subject: [PATCH] Move process information code from sys to proc --- proc.c | 45 ++++++++++++++++++++++++++++++++++++++++++--- proc.h | 4 ++-- pxyconn.c | 10 +++++----- sys.c | 36 ------------------------------------ sys.h | 2 -- 5 files changed, 49 insertions(+), 48 deletions(-) diff --git a/proc.c b/proc.c index 7f99f50..117ac5e 100644 --- a/proc.c +++ b/proc.c @@ -47,7 +47,7 @@ #ifdef HAVE_DARWIN_LIBPROC 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) { pid_t *pids = NULL; @@ -144,10 +144,10 @@ errout1: return ret; } -#else /* HAVE_DARWIN_LIBPROC */ +#else /* !HAVE_DARWIN_LIBPROC */ 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) { *result = -1; return 0; @@ -155,4 +155,43 @@ proc_lookup_by_addr(pid_t *result, UNUSED struct sockaddr *dst_addr, #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: */ diff --git a/proc.h b/proc.h index fce624c..8819e9d 100644 --- a/proc.h +++ b/proc.h @@ -36,8 +36,8 @@ #include -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 */ diff --git a/pxyconn.c b/pxyconn.c index 7bfe1ce..cd14edf 100644 --- a/pxyconn.c +++ b/pxyconn.c @@ -1580,12 +1580,12 @@ pxy_bev_eventcb(struct bufferevent *bev, short events, void *arg) } /* fetch process info */ - if (proc_lookup_by_addr(&ctx->pid, - (struct sockaddr*)&ctx->addr, - ctx->addrlen) == 0 && + if (proc_pid_for_addr(&ctx->pid, + (struct sockaddr*)&ctx->addr, + ctx->addrlen) == 0 && ctx->pid != -1 && - sys_proc_info(ctx->pid, &ctx->exec_path, - &ctx->uid, &ctx->gid) == 0) { + proc_get_info(ctx->pid, &ctx->exec_path, + &ctx->uid, &ctx->gid) == 0) { /* fetch user/group names */ ctx->user = sys_user_str(ctx->uid); ctx->group = sys_group_str(ctx->gid); diff --git a/sys.c b/sys.c index 502e32f..b4f8bd5 100644 --- a/sys.c +++ b/sys.c @@ -197,42 +197,6 @@ sys_pidf_close(int fd, const char *fn) 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. * Returns an allocated buffer which must be freed by caller, or NULL on error. diff --git a/sys.h b/sys.h index 2412c48..64d0d11 100644 --- a/sys.h +++ b/sys.h @@ -41,8 +41,6 @@ int sys_pidf_open(const char *) NONNULL(1) WUNRES; int sys_pidf_write(int) WUNRES; 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_group_str(gid_t) WUNRES MALLOC;