Module Name:    src
Committed By:   christos
Date:           Mon Jun 15 19:45:31 UTC 2015

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

Log Message:
staircase flattening police.


To generate a diff of this commit:
cvs rdiff -u -r1.6 -r1.7 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_wrapper.c
diff -u src/sys/dev/filemon/filemon_wrapper.c:1.6 src/sys/dev/filemon/filemon_wrapper.c:1.7
--- src/sys/dev/filemon/filemon_wrapper.c:1.6	Mon Jun 15 14:11:36 2015
+++ src/sys/dev/filemon/filemon_wrapper.c	Mon Jun 15 15:45:31 2015
@@ -1,3 +1,5 @@
+/*	$NetBSD: filemon_wrapper.c,v 1.7 2015/06/15 19:45:31 christos Exp $	*/
+
 /*
  * Copyright (c) 2010, Juniper Networks, Inc.
  *
@@ -24,7 +26,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: filemon_wrapper.c,v 1.6 2015/06/15 18:11:36 sjg Exp $");
+__KERNEL_RCSID(0, "$NetBSD: filemon_wrapper.c,v 1.7 2015/06/15 19:45:31 christos Exp $");
 
 #include <sys/param.h>
 #include <sys/types.h>
@@ -41,27 +43,27 @@ static int
 filemon_wrapper_chdir(struct lwp * l, const struct sys_chdir_args * uap,
     register_t * retval)
 {
-	int ret;
 	int error;
 	size_t done;
 	struct filemon *filemon;
 	
-	if ((ret = sys_chdir(l, uap, retval)) == 0) {
-		filemon = filemon_lookup(curproc);
+	if ((error = sys_chdir(l, uap, retval)) != 0)
+		return 0;
 
-		if (filemon) {
+	filemon = filemon_lookup(curproc);
+	if (filemon == NULL)
+		return 0;
 
-			error = copyinstr(SCARG(uap, path), filemon->fm_fname1,
-			    sizeof(filemon->fm_fname1), &done);
-			if (error == 0) {
-				filemon_printf(filemon,
-				    "C %d %s\n",
-				    curproc->p_pid, filemon->fm_fname1);
-			}
-			rw_exit(&filemon->fm_mtx);
-		}
-	}
-	return (ret);
+	error = copyinstr(SCARG(uap, path), filemon->fm_fname1,
+	    sizeof(filemon->fm_fname1), &done);
+	if (error)
+		goto out;
+
+	filemon_printf(filemon, "C %d %s\n",
+	    curproc->p_pid, filemon->fm_fname1);
+out:
+	rw_exit(&filemon->fm_mtx);
+	return 0;
 }
 
 static int
@@ -69,232 +71,234 @@ filemon_wrapper_execve(struct lwp * l, s
     register_t * retval)
 {
 	char fname[MAXPATHLEN];
-	int ret;
 	int error;
 	size_t done;
 	struct filemon *filemon;
 	
-	error = copyinstr(SCARG(uap, path), fname, sizeof(fname), &done);
+	if ((error = sys_execve(l, uap, retval)) != EJUSTRETURN)
+		return 0;
 
-	if ((ret = sys_execve(l, uap, retval)) == EJUSTRETURN && error == 0) {
-		filemon = filemon_lookup(curproc);
+	filemon = filemon_lookup(curproc);
+	if (filemon == NULL)
+		return 0;
 
-		if (filemon) {
-			filemon_printf(filemon, "E %d %s\n",
-			    curproc->p_pid, fname);
-			rw_exit(&filemon->fm_mtx);
-		}
-	}
-	return (ret);
+	error = copyinstr(SCARG(uap, path), fname, sizeof(fname), &done);
+	if (error)
+		goto out;
+
+	filemon_printf(filemon, "E %d %s\n", curproc->p_pid, fname);
+out:
+	rw_exit(&filemon->fm_mtx);
+	return 0;
 }
 
 
 static int
 filemon_wrapper_fork(struct lwp * l, const void *v, register_t * retval)
 {
-	int ret;
+	int error;
 	struct filemon *filemon;
 
-	if ((ret = sys_fork(l, v, retval)) == 0) {
-		filemon = filemon_lookup(curproc);
+	if ((error = sys_fork(l, v, retval)) != 0)
+		return error;
 
-		if (filemon) {
-			filemon_printf(filemon, "F %d %ld\n",
-			    curproc->p_pid, (long) retval[0]);
-			rw_exit(&filemon->fm_mtx);
-		}
-	}
-	return (ret);
+	filemon = filemon_lookup(curproc);
+	if (filemon == NULL)
+		return 0;
+
+	filemon_printf(filemon, "F %d %ld\n", curproc->p_pid, (long) retval[0]);
+
+	rw_exit(&filemon->fm_mtx);
+	return 0;
 }
 
 static int
 filemon_wrapper_vfork(struct lwp * l, const void *v, register_t * retval)
 {
-	int ret;
+	int error;
 	struct filemon *filemon;
 
-	if ((ret = sys_vfork(l, v, retval)) == 0) {
-		filemon = filemon_lookup(curproc);
+	if ((error = sys_vfork(l, v, retval)) != 0)
+		return error;
+
+	filemon = filemon_lookup(curproc);
+	if (filemon == NULL)
+		return 0;
+
+	filemon_printf(filemon, "F %d %ld\n", curproc->p_pid, (long) retval[0]);
 
-		if (filemon) {
-			filemon_printf(filemon, "F %d %ld\n",
-			    curproc->p_pid, (long) retval[0]);
-			rw_exit(&filemon->fm_mtx);
-		}
+	rw_exit(&filemon->fm_mtx);
+	return 0;
+}
+
+static void
+filemon_flags(struct filemon * filemon, int f)
+{
+	if (f & O_RDWR) {
+		/* we want a separate R record */
+		filemon_printf(filemon, "R %d %s\n", curproc->p_pid,
+		    filemon->fm_fname1);
 	}
-	return (ret);
+
+	filemon_printf(filemon, "%c %d %s\n", (f & O_ACCMODE) ? 'W' : 'R',
+	    curproc->p_pid, filemon->fm_fname1);
 }
 
 static int
 filemon_wrapper_open(struct lwp * l, struct sys_open_args * uap,
     register_t * retval)
 {
-	int ret;
 	int error;
 	size_t done;
 	struct filemon *filemon;
 
-	if ((ret = sys_open(l, uap, retval)) == 0) {
-		filemon = filemon_lookup(curproc);
+	if ((error = sys_open(l, uap, retval)) != 0)
+		return error;
 
-		if (filemon) {
-			error = copyinstr(SCARG(uap, path), filemon->fm_fname1,
-			    sizeof(filemon->fm_fname1), &done);
-			if (error == 0) {
-				if (SCARG(uap, flags) & O_RDWR) {
-					/* we want a separate R record */
-					filemon_printf(filemon,
-						"R %d %s\n",
-						curproc->p_pid,
-						filemon->fm_fname1);
-				}			
-				filemon_printf(filemon,
-				    "%c %d %s\n",
-				    (SCARG(uap, flags) & O_ACCMODE) ? 'W' : 'R',
-				    curproc->p_pid, filemon->fm_fname1);
-			}
-			rw_exit(&filemon->fm_mtx);
-		}
-	}
-	return (ret);
+	filemon = filemon_lookup(curproc);
+	if (filemon == NULL)
+		return 0;
+
+	error = copyinstr(SCARG(uap, path), filemon->fm_fname1,
+	    sizeof(filemon->fm_fname1), &done);
+	if (error)
+		goto out;
+
+	filemon_flags(filemon, SCARG(uap, flags));
+out:
+	rw_exit(&filemon->fm_mtx);
+	return 0;
 }
 
 static int
 filemon_wrapper_openat(struct lwp * l, struct sys_openat_args * uap,
     register_t * retval)
 {
-	int ret;
 	int error;
 	size_t done;
 	struct filemon *filemon;
 
-	if ((ret = sys_openat(l, uap, retval)) == 0) {
-		filemon = filemon_lookup(curproc);
+	if ((error = sys_openat(l, uap, retval)) != 0)
+		return error;
 
-		if (filemon) {
-			error = copyinstr(SCARG(uap, path), filemon->fm_fname1,
-			    sizeof(filemon->fm_fname1), &done);
-			if (error == 0) {
-				if (filemon->fm_fname1[0] != '/' &&
-				    SCARG(uap, fd) != AT_FDCWD) {
-					/*
-					 * Rats we cannot just treat like open.
-					 * Output an 'A' record as a clue.
-					 */
-					filemon_printf(filemon,
-						"A %d %s\n",
-						curproc->p_pid,
-						filemon->fm_fname1);
-				}
-				if (SCARG(uap, oflags) & O_RDWR) {
-					/* we want a separate R record */
-					filemon_printf(filemon,
-						"R %d %s\n",
-						curproc->p_pid,
-						filemon->fm_fname1);
-				}			
-				filemon_printf(filemon,
-				    "%c %d %s\n",
-				    (SCARG(uap, oflags) & O_ACCMODE) ? 'W' : 'R',
-				    curproc->p_pid, filemon->fm_fname1);
-			}
-			rw_exit(&filemon->fm_mtx);
-		}
-	}
-	return (ret);
+	filemon = filemon_lookup(curproc);
+	if (filemon == NULL)
+		return 0;
+
+	error = copyinstr(SCARG(uap, path), filemon->fm_fname1,
+	    sizeof(filemon->fm_fname1), &done);
+	if (error)
+		goto out;
+
+	if (filemon->fm_fname1[0] != '/' && SCARG(uap, fd) != AT_FDCWD) {
+		/*
+		 * Rats we cannot just treat like open.
+		 * Output an 'A' record as a clue.
+		 */
+		filemon_printf(filemon, "A %d %s\n", curproc->p_pid,
+		    filemon->fm_fname1);
+	}
+
+	filemon_flags(filemon, SCARG(uap, oflags));
+out:
+	rw_exit(&filemon->fm_mtx);
+	return 0;
 }
 
 static int
 filemon_wrapper_rename(struct lwp * l, struct sys_rename_args * uap,
     register_t * retval)
 {
-	int ret;
 	int error;
 	size_t done;
 	struct filemon *filemon;
 
-	if ((ret = sys_rename(l, uap, retval)) == 0) {
-		filemon = filemon_lookup(curproc);
+	if ((error = sys_rename(l, uap, retval)) != 0)
+		return error;
 
-		if (filemon) {
-			error = copyinstr(SCARG(uap, from), filemon->fm_fname1,
-			    sizeof(filemon->fm_fname1), &done);
-			if (error == 0) 
-				error = copyinstr(SCARG(uap, to),
-				    filemon->fm_fname2,
-				    sizeof(filemon->fm_fname2), &done);
-			if (error == 0) {
-				filemon_printf(filemon,
-				    "M %d '%s' '%s'\n",
-				    curproc->p_pid, filemon->fm_fname1,
-				    filemon->fm_fname2);
-			}
-			rw_exit(&filemon->fm_mtx);
-		}
-	}
-	return (ret);
+	filemon = filemon_lookup(curproc);
+	if (filemon == NULL)
+		return 0;
+
+	error = copyinstr(SCARG(uap, from), filemon->fm_fname1,
+	    sizeof(filemon->fm_fname1), &done);
+	if (error)
+		goto out;
+
+	error = copyinstr(SCARG(uap, to), filemon->fm_fname2,
+	    sizeof(filemon->fm_fname2), &done);
+	if (error)
+		goto out;
+
+	filemon_printf(filemon, "M %d '%s' '%s'\n", curproc->p_pid,
+	    filemon->fm_fname1, filemon->fm_fname2);
+out:
+	rw_exit(&filemon->fm_mtx);
+	return 0;
 }
 
 static int
 filemon_wrapper_link(struct lwp * l, struct sys_link_args * uap,
     register_t * retval)
 {
-	int ret;
 	int error;
 	size_t done;
 	struct filemon *filemon;
 
-	if ((ret = sys_link(l, uap, retval)) == 0) {
-		filemon = filemon_lookup(curproc);
+	if ((error = sys_link(l, uap, retval)) != 0)
+		return error;
 
-		if (filemon) {
-			error = copyinstr(SCARG(uap, path),
-			    filemon->fm_fname1, sizeof(filemon->fm_fname1),
-			    &done);
-			if (error == 0)
-				error = copyinstr(SCARG(uap, link),
-				    filemon->fm_fname2,
-				    sizeof(filemon->fm_fname2), &done);
-			if (error == 0) {
-				filemon_printf(filemon, "L %d '%s' '%s'\n",
-				    curproc->p_pid, filemon->fm_fname1,
-				    filemon->fm_fname2);
-			}
-			rw_exit(&filemon->fm_mtx);
-		}
-	}
-	return (ret);
+	filemon = filemon_lookup(curproc);
+	if (filemon == NULL)
+		return 0;
+
+	error = copyinstr(SCARG(uap, path), filemon->fm_fname1,
+	    sizeof(filemon->fm_fname1), &done);
+	if (error)
+		goto out;
+
+	error = copyinstr(SCARG(uap, link), filemon->fm_fname2,
+	    sizeof(filemon->fm_fname2), &done);
+	if (error)
+		goto out;
+
+	filemon_printf(filemon, "L %d '%s' '%s'\n", curproc->p_pid,
+	    filemon->fm_fname1, filemon->fm_fname2);
+out:
+	rw_exit(&filemon->fm_mtx);
+	return 0;
 }
 
 static int
 filemon_wrapper_symlink(struct lwp * l, struct sys_symlink_args * uap,
     register_t * retval)
 {
-	int ret;
 	int error;
 	size_t done;
 	struct filemon *filemon;
 
-	if ((ret = sys_symlink(l, uap, retval)) == 0) {
-		filemon = filemon_lookup(curproc);
+	if ((error = sys_symlink(l, uap, retval)) != 0)
+		return error;
 
-		if (filemon) {
-			error = copyinstr(SCARG(uap, path),
-			    filemon->fm_fname1,
-			    sizeof(filemon->fm_fname1), &done);
-			if (error == 0)
-				error = copyinstr(SCARG(uap, link),
-				    filemon->fm_fname2,
-				    sizeof(filemon->fm_fname2), &done);
-			if (error == 0) {
-				filemon_printf(filemon, "L %d '%s' '%s'\n",
-				    curproc->p_pid, filemon->fm_fname1,
-				    filemon->fm_fname2);
-			}
-			rw_exit(&filemon->fm_mtx);
-		}
-	}
-	return (ret);
+	filemon = filemon_lookup(curproc);
+	if (filemon == NULL)
+		return 0;
+
+	error = copyinstr(SCARG(uap, path), filemon->fm_fname1,
+	    sizeof(filemon->fm_fname1), &done);
+	if (error)
+		goto out;
+
+	error = copyinstr(SCARG(uap, link), filemon->fm_fname2,
+	    sizeof(filemon->fm_fname2), &done);
+	if (error)
+		goto out;
+
+	filemon_printf(filemon, "L %d '%s' '%s'\n", curproc->p_pid,
+	    filemon->fm_fname1, filemon->fm_fname2);
+out:
+	rw_exit(&filemon->fm_mtx);
+	return 0;
 }
 
 
@@ -309,10 +313,11 @@ filemon_wrapper_sys_exit(struct lwp * l,
 	if (filemon) {
 		filemon_printf(filemon, "X %d %d\n",
 		    curproc->p_pid, SCARG(uap, rval));
+
 		/* Check if the monitored process is about to exit. */
-		if (filemon->fm_pid == curproc->p_pid) {
+		if (filemon->fm_pid == curproc->p_pid)
 			filemon_printf(filemon, "# Bye bye\n");
-		}
+
 		rw_exit(&filemon->fm_mtx);
 	}
 	sys_exit(l, uap, retval);
@@ -322,26 +327,27 @@ static int
 filemon_wrapper_unlink(struct lwp * l, struct sys_unlink_args * uap,
     register_t * retval)
 {
-	int ret;
 	int error;
 	size_t done;
 	struct filemon *filemon;
 
-	if ((ret = sys_unlink(l, uap, retval)) == 0) {
-		filemon = filemon_lookup(curproc);
+	if ((error = sys_unlink(l, uap, retval)) != 0)
+		return error;
 
-		if (filemon) {
-			error = copyinstr(SCARG(uap, path),
-			    filemon->fm_fname1,
-			    sizeof(filemon->fm_fname1), &done);
-			if (error == 0) {
-				filemon_printf(filemon, "D %d %s\n",
-				    curproc->p_pid, filemon->fm_fname1);
-			}
-			rw_exit(&filemon->fm_mtx);
-		}
-	}
-	return (ret);
+	filemon = filemon_lookup(curproc);
+	if (filemon == NULL)
+		return 0;
+
+	error = copyinstr(SCARG(uap, path), filemon->fm_fname1,
+	    sizeof(filemon->fm_fname1), &done);
+	if (error)
+		goto out;
+
+	filemon_printf(filemon, "D %d %s\n",
+	    curproc->p_pid, filemon->fm_fname1);
+out:
+	rw_exit(&filemon->fm_mtx);
+	return 0;
 }
 
 

Reply via email to