Module Name:    src
Committed By:   christos
Date:           Thu Mar 27 18:27:34 UTC 2014

Modified Files:
        src/sys/dev/filemon: filemon.c filemon.h filemon_wrapper.c

Log Message:
remove a bunch of repetitive code by introducing filemon_printf.


To generate a diff of this commit:
cvs rdiff -u -r1.6 -r1.7 src/sys/dev/filemon/filemon.c
cvs rdiff -u -r1.4 -r1.5 src/sys/dev/filemon/filemon.h \
    src/sys/dev/filemon/filemon_wrapper.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/filemon/filemon.c
diff -u src/sys/dev/filemon/filemon.c:1.6 src/sys/dev/filemon/filemon.c:1.7
--- src/sys/dev/filemon/filemon.c:1.6	Sun Mar 16 01:20:27 2014
+++ src/sys/dev/filemon/filemon.c	Thu Mar 27 14:27:34 2014
@@ -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,18 +121,28 @@ filemon_output(struct filemon * filemon,
 	    &auio, curlwp->l_cred, FOF_UPDATE_OFFSET);
 }
 
-static void
-filemon_comment(struct filemon * filemon)
+void
+filemon_printf(struct filemon *filemon, const char *fmt, ...)
 {
-	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);
+	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)
+{
+	filemon_printf(filemon, "# filemon version %d\n# Target pid %d\nV %d\n",
+	   FILEMON_VERSION, curproc->p_pid, FILEMON_VERSION);
+}
+
 
 static struct filemon *
 filemon_pid_check(struct proc * p)

Index: src/sys/dev/filemon/filemon.h
diff -u src/sys/dev/filemon/filemon.h:1.4 src/sys/dev/filemon/filemon.h:1.5
--- src/sys/dev/filemon/filemon.h:1.4	Mon Nov 19 17:20:10 2012
+++ src/sys/dev/filemon/filemon.h	Thu Mar 27 14:27:34 2014
@@ -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 p
 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
Index: src/sys/dev/filemon/filemon_wrapper.c
diff -u src/sys/dev/filemon/filemon_wrapper.c:1.4 src/sys/dev/filemon/filemon_wrapper.c:1.5
--- src/sys/dev/filemon/filemon_wrapper.c:1.4	Mon Nov 19 17:20:10 2012
+++ src/sys/dev/filemon/filemon_wrapper.c	Thu Mar 27 14:27:34 2014
@@ -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 @@ filemon_wrapper_chdir(struct lwp * l, co
 	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, co
 			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, s
 	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, s
 		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, str
 	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, str
 			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, s
 	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, s
 				    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, str
 	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, str
 				    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, 
 	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, 
 				    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, s
 	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, s
 			    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);
 		}

Reply via email to