Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 13 Aug 2013 03:07:49 +0000 (UTC)
From:      Mark Johnston <markj@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r254266 - in head: share/man/man9 sys/kern sys/sys
Message-ID:  <201308130307.r7D37nZl017329@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: markj
Date: Tue Aug 13 03:07:49 2013
New Revision: 254266
URL: http://svnweb.freebsd.org/changeset/base/254266

Log:
  Add event handlers for module load and unload events. The load handlers are
  called after the module has been loaded, and the unload handlers are called
  before the module is unloaded. Moreover, the module unload handlers may
  return an error to prevent the unload from proceeding.
  
  Reviewed by:	avg
  MFC after:	2 weeks

Modified:
  head/share/man/man9/EVENTHANDLER.9
  head/sys/kern/kern_linker.c
  head/sys/sys/eventhandler.h

Modified: head/share/man/man9/EVENTHANDLER.9
==============================================================================
--- head/share/man/man9/EVENTHANDLER.9	Tue Aug 13 02:31:46 2013	(r254265)
+++ head/share/man/man9/EVENTHANDLER.9	Tue Aug 13 03:07:49 2013	(r254266)
@@ -23,7 +23,7 @@
 .\" SUCH DAMAGE.
 .\" $FreeBSD$
 .\"
-.Dd May 11, 2012
+.Dd August 1, 2013
 .Dt EVENTHANDLER 9
 .Os
 .Sh NAME
@@ -199,6 +199,12 @@ Callbacks invoked when a new network int
 Callbacks invoked when a network interface is taken down.
 .It Vt bpf_track
 Callbacks invoked when a BPF listener attaches to/detaches from network interface.
+.It Vt mod_load
+Callbacks invoked after a module has been loaded.
+.It Vt mod_unload
+Callbacks invoked before a module is about to be unloaded.
+These callbacks may be used to return an error and prevent the unload from
+proceeding.
 .It Vt power_profile_change
 Callbacks invoked when the power profile of the system changes.
 .It Vt process_exec

Modified: head/sys/kern/kern_linker.c
==============================================================================
--- head/sys/kern/kern_linker.c	Tue Aug 13 02:31:46 2013	(r254265)
+++ head/sys/kern/kern_linker.c	Tue Aug 13 03:07:49 2013	(r254266)
@@ -45,6 +45,7 @@ __FBSDID("$FreeBSD$");
 #include <sys/module.h>
 #include <sys/mount.h>
 #include <sys/linker.h>
+#include <sys/eventhandler.h>
 #include <sys/fcntl.h>
 #include <sys/jail.h>
 #include <sys/libkern.h>
@@ -1046,6 +1047,9 @@ kern_kldload(struct thread *td, const ch
 	lf->userrefs++;
 	if (fileid != NULL)
 		*fileid = lf->id;
+
+	EVENTHANDLER_INVOKE(mod_load, lf);
+
 #ifdef HWPMC_HOOKS
 	KLD_DOWNGRADE();
 	pkm.pm_file = lf->filename;
@@ -1101,8 +1105,10 @@ kern_kldunload(struct thread *td, int fi
 	if (lf) {
 		KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs));
 
-		/* Check if there are DTrace probes enabled on this file. */
-		if (lf->nenabled > 0) {
+		EVENTHANDLER_INVOKE(mod_unload, lf, &error);
+		if (error != 0)
+			error = EBUSY;
+		else if (lf->nenabled > 0) {
 			printf("kldunload: attempt to unload file that has"
 			    " DTrace probes enabled\n");
 			error = EBUSY;

Modified: head/sys/sys/eventhandler.h
==============================================================================
--- head/sys/sys/eventhandler.h	Tue Aug 13 02:31:46 2013	(r254265)
+++ head/sys/sys/eventhandler.h	Tue Aug 13 03:07:49 2013	(r254266)
@@ -266,4 +266,11 @@ EVENTHANDLER_DECLARE(nmbclusters_change,
 EVENTHANDLER_DECLARE(nmbufs_change, uma_zone_chfn);
 EVENTHANDLER_DECLARE(maxsockets_change, uma_zone_chfn);
 
+/* Module load and unload events */
+struct linker_file;
+typedef void (*mod_load_fn)(void *, struct linker_file *);
+typedef void (*mod_unload_fn)(void *, struct linker_file *, int *);
+EVENTHANDLER_DECLARE(mod_load, mod_load_fn);
+EVENTHANDLER_DECLARE(mod_unload, mod_unload_fn);
+
 #endif /* SYS_EVENTHANDLER_H */



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201308130307.r7D37nZl017329>