Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/dev/filemon remove a bunch of repetitive code by introdu...



details:   https://anonhg.NetBSD.org/src/rev/610bce46d900
branches:  trunk
changeset: 794876:610bce46d900
user:      christos <christos%NetBSD.org@localhost>
date:      Thu Mar 27 18:27:34 2014 +0000

description:
remove a bunch of repetitive code by introducing filemon_printf.

diffstat:

 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(-)

diffs (truncated from 319 to 300 lines):

diff -r f647ff9d92a4 -r 610bce46d900 sys/dev/filemon/filemon.c
--- a/sys/dev/filemon/filemon.c Thu Mar 27 18:22:56 2014 +0000
+++ b/sys/dev/filemon/filemon.c Thu Mar 27 18:27:34 2014 +0000
@@ -24,7 +24,7 @@
  */
 
 #include <sys/cdefs.h>
-__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 <sys/param.h>
 #include <sys/kernel.h>
@@ -121,16 +121,26 @@
            &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 -r f647ff9d92a4 -r 610bce46d900 sys/dev/filemon/filemon.h
--- a/sys/dev/filemon/filemon.h Thu Mar 27 18:22:56 2014 +0000
+++ b/sys/dev/filemon/filemon.h Thu Mar 27 18:27:34 2014 +0000
@@ -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 @@
 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 -r f647ff9d92a4 -r 610bce46d900 sys/dev/filemon/filemon_wrapper.c
--- a/sys/dev/filemon/filemon_wrapper.c Thu Mar 27 18:22:56 2014 +0000
+++ b/sys/dev/filemon/filemon_wrapper.c Thu Mar 27 18:27:34 2014 +0000
@@ -24,7 +24,7 @@
  */
 
 #include <sys/cdefs.h>
-__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 <sys/param.h>
 #include <sys/types.h>
@@ -44,7 +44,6 @@
        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 @@
                        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 @@
        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 = 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 @@
 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 @@
 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 @@
        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 @@
                        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 @@
        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->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 @@
        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->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 @@
        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->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 @@
 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 @@
        int ret;
        int error;
        size_t done;



Home | Main Index | Thread Index | Old Index