From c128bdf97eea73c0853e7cea995551e1931b4b3c Mon Sep 17 00:00:00 2001
From: Sascha Wildner <saw@online.de>
Date: Fri, 1 Aug 2014 00:39:20 +0200
Subject: [PATCH] automatic moused

---
 etc/Makefile               |  4 ++--
 etc/defaults/rc.conf       |  2 ++
 etc/devd.conf              | 32 ++++++++++++++--------------
 etc/rc.d/moused            | 34 +++++++++++++++++++++++++++---
 etc/rc.subr                | 52 +++++++++++++++++++++++++++++++++++++---------
 sys/vfs/devfs/devfs_core.c | 20 ++++++++++++++++++
 6 files changed, 113 insertions(+), 31 deletions(-)

diff --git a/etc/Makefile b/etc/Makefile
index 6ef876f..73d5f96 100644
--- a/etc/Makefile
+++ b/etc/Makefile
@@ -7,7 +7,7 @@ SUBDIR=	sendmail
 
 # Files that should be installed read-only (444)
 #
-BINUPDATE= apmd.conf fbtab gettytab network.subr \
+BINUPDATE= apmd.conf devd.conf fbtab gettytab network.subr \
 	pf.os \
 	protocols \
 	rc rc.firewall6 \
@@ -25,7 +25,7 @@ BINUPDATE+=${.CURDIR}/../usr.bin/mail/misc/mail.rc \
 #
 BIN1=	amd.map auth.conf \
 	crontab csh.cshrc csh.login csh.logout \
-	devd.conf devtab dhclient.conf dm.conf dntpd.conf \
+	devtab dhclient.conf dm.conf dntpd.conf \
 	ftpusers group \
 	hosts hosts.allow hosts.equiv hosts.lpd \
 	inetd.conf login.access login.conf \
diff --git a/etc/defaults/rc.conf b/etc/defaults/rc.conf
index bbdc08f..8df0775 100644
--- a/etc/defaults/rc.conf
+++ b/etc/defaults/rc.conf
@@ -304,6 +304,8 @@ font8x14="NO"		# font 8x14 from /usr/share/syscons/fonts/* (or NO).
 font8x8="NO"		# font 8x8 from /usr/share/syscons/fonts/* (or NO).
 blanktime="300"		# blank time (in seconds) or "NO" to turn it off.
 saver="NO"		# screen saver: Uses /modules/${saver}_saver.ko
+moused_nondefault_enable="YES" # Treat non-default mice as enabled unless
+			       # specifically overriden in rc.conf(5).
 moused_enable="NO"	# Run the mouse daemon.
 moused_type="auto"	# See man page for rc.conf(5) for available settings.
 moused_port="/dev/psm0"	# Set to your mouse port.
diff --git a/etc/devd.conf b/etc/devd.conf
index 7a22f00..0dd1c32 100644
--- a/etc/devd.conf
+++ b/etc/devd.conf
@@ -124,14 +124,14 @@ detach 100 {
 #	action "/etc/rc.d/moused quietstart $cdev";
 #};
 
-#notify 100 {
-#	match "system" "DEVFS";
-#	match "subsystem" "CDEV";
-#	match "type" "CREATE";
-#	match "cdev" "ums[0-9]+";
-#
-#	action "/etc/rc.d/moused quietstart $cdev";
-#};
+notify 100 {
+	match "system" "DEVFS";
+	match "subsystem" "CDEV";
+	match "type" "CREATE";
+	match "cdev" "ums[0-9]+";
+
+	action "/etc/rc.d/moused quietstart $cdev";
+};
 
 #notify 100 {
 #	match "system" "DEVFS";
@@ -142,14 +142,14 @@ detach 100 {
 #	action "/etc/rc.d/moused quietstart $cdev";
 #};
 
-#notify 100 {
-#	match "system" "DEVFS";
-#	match "subsystem" "CDEV";
-#	match "type" "DESTROY";
-#	match "cdev" "ums[0-9]+";
-#
-#	action "/etc/rc.d/moused stop $cdev";
-#};
+notify 100 {
+	match "system" "DEVFS";
+	match "subsystem" "CDEV";
+	match "type" "DESTROY";
+	match "cdev" "ums[0-9]+";
+
+	action "/etc/rc.d/moused stop $cdev";
+};
 
 # Firmware download into the ActiveWire board. After the firmware download is
 # done, the device detaches and reappears as something new and shiny
diff --git a/etc/rc.d/moused b/etc/rc.d/moused
index f2fdd31..c942ed8 100644
--- a/etc/rc.d/moused
+++ b/etc/rc.d/moused
@@ -16,15 +16,44 @@ command="/usr/sbin/${name}"
 start_cmd="moused_start"
 stop_cmd="moused_stop"
 sig_stop="-INT"
+load_rc_config $name
 
 # stdin must be redirected because it might be for a serial console
 #
 viddev=/dev/ttyv0
 
+# Set the pid file and variable name. The second argument, if it exists, is
+# expected to be the mouse device.
+#
+if [ -n "$2" ]; then
+	eval moused_$2_enable=\${moused_$2_enable-${moused_nondefault_enable}}
+	rcvar="moused_${2}_enable"
+	pidarg="-I $pidfile"
+fi
+
 moused_start()
 {
+	# Set the mouse device and get any related variables. If
+	# a moused device has been specified on the commandline, then
+	# rc.conf(5) variables defined for that device take precedence
+	# over the generic moused_* variables. The only exception is
+	# the moused_port variable, which if not defined sets it to the
+	# passed in device name.
+	#
+	ms=$1
+	if [ -n "$ms" ]; then
+		eval myflags=\${moused_${ms}_flags-$moused_flags}
+		eval myport=\${moused_${ms}_port-/dev/$ms}
+		eval mytype=\${moused_${ms}_type-$moused_type}
+	else
+		ms="default"
+		myflags="$moused_flags"
+		myport="$moused_port"
+		mytype="$moused_type"
+	fi
+
 	echo -n 'Starting moused:'
-	/usr/sbin/moused ${moused_flags} -p ${moused_port} -t ${moused_type}
+	/usr/sbin/moused ${myflags} -p ${myport} -t ${mytype}
 
 	_mousechar_arg=
 	case ${mousechar_start} in
@@ -53,5 +82,4 @@ moused_stop()
 	vidcontrol < ${viddev} -m off
 }
 
-load_rc_config $name
-run_rc_command "$1"
+run_rc_command $*
diff --git a/etc/rc.subr b/etc/rc.subr
index f8eb674..804d2e8 100644
--- a/etc/rc.subr
+++ b/etc/rc.subr
@@ -461,6 +461,20 @@ get_pidfile_from_conf()
 }
 
 #
+# check_startmsgs
+#	If rc_quiet is set (usually as a result of using faststart at
+#	boot time) check if rc_startmsgs is enabled.
+#
+check_startmsgs()
+{
+	if [ -n "$rc_quiet" ]; then
+		checkyesno rc_startmsgs
+	else
+		return 0
+	fi
+}
+
+#
 # run_rc_command argument
 #	Search for argument in the list of supported commands, which is:
 #		"start stop restart rcvar status poll ${extra_commands}"
@@ -470,9 +484,10 @@ get_pidfile_from_conf()
 #	If argument has a given prefix, then change the operation as follows:
 #		Prefix	Operation
 #		------	---------
-#		fast	Skip the pid check, and set rc_fast=yes
+#		fast	Skip the pid check, and set rc_fast=yes, rc_quiet=yes
 #		force	Set ${rcvar} to YES, and set rc_force=yes
 #		one	Set ${rcvar} to YES
+#		quiet	Don't output some diagnostics, and set rc_quiet=yes
 #
 #	The following globals are used:
 #
@@ -598,6 +613,8 @@ get_pidfile_from_conf()
 #
 #	rc_force	Not empty if "force" was provided (q.v.)
 #
+#	rc_quiet	Not empty if "quiet" was provided
+#
 #
 dummy_rc_command()
 {
@@ -626,10 +643,17 @@ run_rc_command()
 		err 3 'run_rc_command: $name is not set.'
 	fi
 
+	# Don't repeat the first argument when passing additional command-
+	# line arguments to the command subroutines.
+	#
+	shift 1
+	rc_extra_args="$*"
+
 	case "$rc_arg" in
 	fast*)				# "fast" prefix; don't check pid
 		rc_arg=${rc_arg#fast}
 		rc_fast=yes
+		rc_quiet=yes
 		;;
 	force*)				# "force" prefix; always start
 		rc_arg=${rc_arg#force}
@@ -643,6 +667,12 @@ run_rc_command()
 		if [ -n "${rcvar}" ]; then
 			eval ${rcvar}=YES
 		fi
+		;;
+	quiet*)				# "quiet" prefix; omit some messages
+		_rc_prefix=quiet
+		rc_arg=${rc_arg#${_rc_prefix}}
+		rc_quiet=yes
+		;;
 	esac
 
 	eval _override_command=\$${name}_program
@@ -716,7 +746,7 @@ run_rc_command()
 					#
 			if [ -n "$_precmd" ]; then
 				debug "run_rc_command: evaluating ${_precmd}()."
-				eval $_precmd
+				eval $_precmd $rc_extra_args
 
 				_return=$?
 				check_early_term $rc_arg $_return "$rc_force" || return $(($?-1))
@@ -724,14 +754,14 @@ run_rc_command()
 
 			if [ -n "$_cmd" ]; then
 				debug "run_rc_command: evaluating ${_cmd}()."
-				eval $_cmd
+				eval $_cmd $rc_extra_args
 				_return=$?
 				check_early_term $rc_arg $_return "$rc_force" || return $(($?-1))
 			fi
 
 			if [ -n "$_postcmd" ]; then
 				debug "run_rc_command: evaluating ${_postcmd}()."
-				eval $_postcmd
+				eval $_postcmd $rc_extra_args
 				_return=$?
 				check_early_term $rc_arg $_return "" || return $(($?-1))
 			fi
@@ -753,7 +783,9 @@ run_rc_command()
 
 		start)
 			if [ -n "$rc_pid" ]; then
-				echo "${name} already running? (pid=$rc_pid)."
+				if [ -z "$rc_quiet" ]; then
+					echo "${name} already running? (pid=$rc_pid)."
+				fi
 				exit 1
 			fi
 
@@ -918,7 +950,7 @@ $command $rc_flags $command_args"
 
 		restart)
 			if [ -n "$_precmd" ]; then
-				eval $_precmd
+				eval $_precmd $rc_extra_args
 				_return=$?
 				check_early_term $rc_arg $_return "$rc_force" || return $(($?-1))
 			fi
@@ -930,12 +962,12 @@ $command $rc_flags $command_args"
 			fi
 			_rc_restart_done=YES
 
-			( $0 ${rc_force:+force}stop )
-			$0 ${rc_force:+force}start
+			( $0 ${rc_force:+force}stop $rc_extra_args )
+			$0 ${rc_force:+force}start $rc_extra_args
 			_return=$?
 
 			if [ -n "$_postcmd" ]; then
-				eval $_postcmd
+				eval $_postcmd $rc_extra_args
 				adjust_return_code $?
 				_return=$?
 			fi
@@ -1089,7 +1121,7 @@ load_rc_config()
 #
 rc_usage()
 {
-	echo -n 1>&2 "Usage: $0 [fast|force|one]("
+	echo -n 1>&2 "Usage: $0 [fast|force|one|quiet]("
 
 	_sep=
 	for _elem in $*; do
diff --git a/sys/vfs/devfs/devfs_core.c b/sys/vfs/devfs/devfs_core.c
index 0033e7a..5ee694c 100644
--- a/sys/vfs/devfs/devfs_core.c
+++ b/sys/vfs/devfs/devfs_core.c
@@ -34,6 +34,7 @@
 #include <sys/param.h>
 #include <sys/systm.h>
 #include <sys/kernel.h>
+#include <sys/bus.h>
 #include <sys/mount.h>
 #include <sys/vnode.h>
 #include <sys/types.h>
@@ -1234,6 +1235,23 @@ devfs_msg_exec(devfs_msg_t msg)
 	lockmgr(&devfs_lock, LK_RELEASE);
 }
 
+static void
+notify(cdev_t dev, const char *ev)
+{
+	static const char prefix[] = "cdev=";
+	char *data;
+	int namelen;
+
+	if (cold)
+		return;
+	namelen = strlen(dev->si_name);
+	data = kmalloc(namelen + sizeof(prefix), M_TEMP, M_WAITOK);
+	memcpy(data, prefix, sizeof(prefix) - 1);
+	memcpy(data + sizeof(prefix) - 1, dev->si_name, namelen + 1);
+	devctl_notify("DEVFS", "CDEV", ev, data);
+	kfree(data, M_TEMP);
+}
+
 /*
  * Worker function to insert a new dev into the dev list and initialize its
  * permissions. It also calls devfs_propagate_dev which in turn propagates
@@ -1255,6 +1273,7 @@ devfs_create_dev_worker(cdev_t dev, uid_t uid, gid_t gid, int perms)
 	devfs_propagate_dev(dev, 1);
 
 	udev_event_attach(dev, NULL, 0);
+	notify(dev, "CREATE");
 
 	return 0;
 }
@@ -1275,6 +1294,7 @@ devfs_destroy_dev_worker(cdev_t dev)
 	error = devfs_unlink_dev(dev);
 	devfs_propagate_dev(dev, 0);
 
+	notify(dev, "DESTROY");
 	udev_event_detach(dev, NULL, 0);
 
 	if (error == 0)
-- 
1.9.3

