From 2cd2c9ba7b23919692885444482245452e754ef3 Mon Sep 17 00:00:00 2001 From: christos Date: Thu, 27 Mar 2014 18:27:34 +0000 Subject: [PATCH] remove a bunch of repetitive code by introducing filemon_printf. --- sys/dev/filemon/filemon.c | 26 ++++++--- sys/dev/filemon/filemon.h | 3 +- sys/dev/filemon/filemon_wrapper.c | 87 +++++-------------------------- 3 files changed, 33 insertions(+), 83 deletions(-) diff --git a/sys/dev/filemon/filemon.c b/sys/dev/filemon/filemon.c index 596bbe1a6703..a13c55a12611 100644 --- a/sys/dev/filemon/filemon.c +++ b/sys/dev/filemon/filemon.c @@ -24,7 +24,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: filemon.c,v 1.6 2014/03/16 05:20:27 dholland Exp $"); +__KERNEL_RCSID(0, "$NetBSD: filemon.c,v 1.7 2014/03/27 18:27:34 christos Exp $"); #include #include @@ -121,16 +121,26 @@ filemon_output(struct filemon * filemon, char *msg, size_t len) &auio, curlwp->l_cred, FOF_UPDATE_OFFSET); } +void +filemon_printf(struct filemon *filemon, const char *fmt, ...) +{ + size_t len; + va_list ap; + + va_start(ap, fmt); + len = vsnprintf(filemon->fm_msgbufr, sizeof(filemon->fm_msgbufr), + fmt, ap); + va_end(ap); + if (len > sizeof(filemon->fm_msgbufr)) + len = sizeof(filemon->fm_msgbufr); + filemon_output(filemon, filemon->fm_msgbufr, len); +} + static void filemon_comment(struct filemon * filemon) { - int len; - - len = snprintf(filemon->fm_msgbufr, sizeof(filemon->fm_msgbufr), - "# filemon version %d\n# Target pid %d\nV %d\n", - FILEMON_VERSION, curproc->p_pid, FILEMON_VERSION); - - filemon_output(filemon, filemon->fm_msgbufr, len); + filemon_printf(filemon, "# filemon version %d\n# Target pid %d\nV %d\n", + FILEMON_VERSION, curproc->p_pid, FILEMON_VERSION); } diff --git a/sys/dev/filemon/filemon.h b/sys/dev/filemon/filemon.h index 95f445e51d5e..560175d6df98 100644 --- a/sys/dev/filemon/filemon.h +++ b/sys/dev/filemon/filemon.h @@ -1,4 +1,4 @@ -/* $NetBSD: filemon.h,v 1.4 2012/11/19 22:20:10 sjg Exp $ */ +/* $NetBSD: filemon.h,v 1.5 2014/03/27 18:27:34 christos Exp $ */ /* * Copyright (c) 2010, Juniper Networks, Inc. * @@ -49,6 +49,7 @@ struct filemon * filemon_lookup(struct proc *); void filemon_output(struct filemon *, char *, size_t); void filemon_wrapper_install(void); int filemon_wrapper_deinstall(void); +void filemon_printf(struct filemon *, const char *, ...) __printflike(2, 3); #endif #endif diff --git a/sys/dev/filemon/filemon_wrapper.c b/sys/dev/filemon/filemon_wrapper.c index 712e90094041..4d0419928b15 100644 --- a/sys/dev/filemon/filemon_wrapper.c +++ b/sys/dev/filemon/filemon_wrapper.c @@ -24,7 +24,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: filemon_wrapper.c,v 1.4 2012/11/19 22:20:10 sjg Exp $"); +__KERNEL_RCSID(0, "$NetBSD: filemon_wrapper.c,v 1.5 2014/03/27 18:27:34 christos Exp $"); #include #include @@ -44,7 +44,6 @@ filemon_wrapper_chdir(struct lwp * l, const struct sys_chdir_args * uap, int ret; int error; size_t done; - size_t len; struct filemon *filemon; if ((ret = sys_chdir(l, uap, retval)) == 0) { @@ -55,13 +54,9 @@ filemon_wrapper_chdir(struct lwp * l, const struct sys_chdir_args * uap, error = copyinstr(SCARG(uap, path), filemon->fm_fname1, sizeof(filemon->fm_fname1), &done); if (error == 0) { - len = snprintf(filemon->fm_msgbufr, - sizeof(filemon->fm_msgbufr), + filemon_printf(filemon, "C %d %s\n", curproc->p_pid, filemon->fm_fname1); - - filemon_output(filemon, filemon->fm_msgbufr, - len); } rw_exit(&filemon->fm_mtx); } @@ -77,7 +72,6 @@ filemon_wrapper_execve(struct lwp * l, struct sys_execve_args * uap, int ret; int error; size_t done; - size_t len; struct filemon *filemon; error = copyinstr(SCARG(uap, path), fname, sizeof(fname), &done); @@ -86,12 +80,8 @@ filemon_wrapper_execve(struct lwp * l, struct sys_execve_args * uap, filemon = filemon_lookup(curproc); if (filemon) { - - len = snprintf(filemon->fm_msgbufr, sizeof(filemon->fm_msgbufr), - "E %d %s\n", + filemon_printf(filemon, "E %d %s\n", curproc->p_pid, fname); - - filemon_output(filemon, filemon->fm_msgbufr, len); rw_exit(&filemon->fm_mtx); } } @@ -103,20 +93,14 @@ static int filemon_wrapper_fork(struct lwp * l, const void *v, register_t * retval) { int ret; - size_t len; struct filemon *filemon; if ((ret = sys_fork(l, v, retval)) == 0) { filemon = filemon_lookup(curproc); if (filemon) { - len = snprintf(filemon->fm_msgbufr, - sizeof(filemon->fm_msgbufr), - "F %d %ld\n", + filemon_printf(filemon, "F %d %ld\n", curproc->p_pid, (long) retval[0]); - - filemon_output(filemon, filemon->fm_msgbufr, len); - rw_exit(&filemon->fm_mtx); } } @@ -127,20 +111,14 @@ static int filemon_wrapper_vfork(struct lwp * l, const void *v, register_t * retval) { int ret; - size_t len; struct filemon *filemon; if ((ret = sys_vfork(l, v, retval)) == 0) { filemon = filemon_lookup(curproc); if (filemon) { - len = snprintf(filemon->fm_msgbufr, - sizeof(filemon->fm_msgbufr), - "F %d %ld\n", + filemon_printf(filemon, "F %d %ld\n", curproc->p_pid, (long) retval[0]); - - filemon_output(filemon, filemon->fm_msgbufr, len); - rw_exit(&filemon->fm_mtx); } } @@ -154,7 +132,6 @@ filemon_wrapper_open(struct lwp * l, struct sys_open_args * uap, int ret; int error; size_t done; - size_t len; struct filemon *filemon; if ((ret = sys_open(l, uap, retval)) == 0) { @@ -166,23 +143,15 @@ filemon_wrapper_open(struct lwp * l, struct sys_open_args * uap, if (error == 0) { if (SCARG(uap, flags) & O_RDWR) { /* we want a separate R record */ - len = snprintf(filemon->fm_msgbufr, - sizeof(filemon->fm_msgbufr), + filemon_printf(filemon, "R %d %s\n", curproc->p_pid, filemon->fm_fname1); - - filemon_output(filemon, - filemon->fm_msgbufr, len); } - len = snprintf(filemon->fm_msgbufr, - sizeof(filemon->fm_msgbufr), + filemon_printf(filemon, "%c %d %s\n", (SCARG(uap, flags) & O_ACCMODE) ? 'W' : 'R', curproc->p_pid, filemon->fm_fname1); - - filemon_output(filemon, filemon->fm_msgbufr, - len); } rw_exit(&filemon->fm_mtx); } @@ -197,7 +166,6 @@ filemon_wrapper_rename(struct lwp * l, struct sys_rename_args * uap, int ret; int error; size_t done; - size_t len; struct filemon *filemon; if ((ret = sys_rename(l, uap, retval)) == 0) { @@ -211,14 +179,10 @@ filemon_wrapper_rename(struct lwp * l, struct sys_rename_args * uap, filemon->fm_fname2, sizeof(filemon->fm_fname2), &done); if (error == 0) { - len = snprintf(filemon->fm_msgbufr, - sizeof(filemon->fm_msgbufr), + filemon_printf(filemon, "M %d '%s' '%s'\n", curproc->p_pid, filemon->fm_fname1, filemon->fm_fname2); - - filemon_output(filemon, filemon->fm_msgbufr, - len); } rw_exit(&filemon->fm_mtx); } @@ -233,7 +197,6 @@ filemon_wrapper_link(struct lwp * l, struct sys_link_args * uap, int ret; int error; size_t done; - size_t len; struct filemon *filemon; if ((ret = sys_link(l, uap, retval)) == 0) { @@ -248,13 +211,9 @@ filemon_wrapper_link(struct lwp * l, struct sys_link_args * uap, filemon->fm_fname2, sizeof(filemon->fm_fname2), &done); if (error == 0) { - len = snprintf(filemon->fm_msgbufr, - sizeof(filemon->fm_msgbufr), "L %d '%s' '%s'\n", + filemon_printf(filemon, "L %d '%s' '%s'\n", curproc->p_pid, filemon->fm_fname1, filemon->fm_fname2); - - filemon_output(filemon, filemon->fm_msgbufr, - len); } rw_exit(&filemon->fm_mtx); } @@ -269,7 +228,6 @@ filemon_wrapper_symlink(struct lwp * l, struct sys_symlink_args * uap, int ret; int error; size_t done; - size_t len; struct filemon *filemon; if ((ret = sys_symlink(l, uap, retval)) == 0) { @@ -284,14 +242,9 @@ filemon_wrapper_symlink(struct lwp * l, struct sys_symlink_args * uap, filemon->fm_fname2, sizeof(filemon->fm_fname2), &done); if (error == 0) { - len = snprintf(filemon->fm_msgbufr, - sizeof(filemon->fm_msgbufr), - "L %d '%s' '%s'\n", + filemon_printf(filemon, "L %d '%s' '%s'\n", curproc->p_pid, filemon->fm_fname1, filemon->fm_fname2); - - filemon_output(filemon, filemon->fm_msgbufr, - len); } rw_exit(&filemon->fm_mtx); } @@ -304,24 +257,16 @@ static void filemon_wrapper_sys_exit(struct lwp * l, struct sys_exit_args * uap, register_t * retval) { - size_t len; struct filemon *filemon; filemon = filemon_lookup(curproc); if (filemon) { - len = snprintf(filemon->fm_msgbufr, - sizeof(filemon->fm_msgbufr), "X %d %d\n", + filemon_printf(filemon, "X %d %d\n", curproc->p_pid, SCARG(uap, rval)); - - filemon_output(filemon, filemon->fm_msgbufr, len); - /* Check if the monitored process is about to exit. */ if (filemon->fm_pid == curproc->p_pid) { - len = snprintf(filemon->fm_msgbufr, - sizeof(filemon->fm_msgbufr), "# Bye bye\n"); - - filemon_output(filemon, filemon->fm_msgbufr, len); + filemon_printf(filemon, "# Bye bye\n"); } rw_exit(&filemon->fm_mtx); } @@ -335,7 +280,6 @@ filemon_wrapper_unlink(struct lwp * l, struct sys_unlink_args * uap, int ret; int error; size_t done; - size_t len; struct filemon *filemon; if ((ret = sys_unlink(l, uap, retval)) == 0) { @@ -346,13 +290,8 @@ filemon_wrapper_unlink(struct lwp * l, struct sys_unlink_args * uap, filemon->fm_fname1, sizeof(filemon->fm_fname1), &done); if (error == 0) { - len = snprintf(filemon->fm_msgbufr, - sizeof(filemon->fm_msgbufr), - "D %d %s\n", + filemon_printf(filemon, "D %d %s\n", curproc->p_pid, filemon->fm_fname1); - - filemon_output(filemon, filemon->fm_msgbufr, - len); } rw_exit(&filemon->fm_mtx); }