Subject: Re: ctrl+alt+del
To: None <netbsd-users@netbsd.org>
From: Takahiro Kambe <taca@sky.yamashina.kyoto.jp>
List: netbsd-users
Date: 07/16/2001 01:22:49
----Next_Part(Mon_Jul_16_01:22:44_2001_542)--
Content-Type: Text/Plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

In message <200106140257.f5E2vN206244@edge.sky.yamashina.kyoto.jp>
	on Thu, 14 Jun 2001 11:57:23 +0900,
	Takahiro Kambe <taca@sky.yamashina.kyoto.jp> wrote:
> Then, ctrl+alt+del cause:
> 
> 0. Do nothing.
> 1. Invoke DDB.
> 2. Gracefully shutdown halt the system without relying userland
>    process at all.
> 3. Gracefully shutdown reboot the system without relying userland
>    process at all.
> 
> Those scheme should be select by sysctl(8) at least.
I implemented "ctrl+alt+del special" differ policy above for my real
world job on NetBSD 1.5.1.

With after applying attached patch,

1. Compile kernel with CONSOLE_HALT.
2. Re-build init(8) and sysctl(8).

Now set kern.console_halt to 1 enable halting the system "ctrl+alt+del".

This scheme is:

o Always halt the system.
o Gracefully halt the system with relying userland process init(8).
o Don't rely on DDB.

This idea almost borrowed from FreeBSD 2.2.8.

--
Takahiro Kambe <taca@sky.yamashina.kyoto.jp>


----Next_Part(Mon_Jul_16_01:22:44_2001_542)--
Content-Type: Text/Plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename=diff

Index: sbin/init/init.c
===================================================================
RCS file: /usr/pkg/libdata/cvs/netbsd/sbin/init/init.c,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 init.c
--- sbin/init/init.c	2000/03/21 07:17:40	1.1.1.1
+++ sbin/init/init.c	2001/07/13 07:30:43
@@ -86,6 +86,10 @@
 #include <pwd.h>
 #endif
 
+#ifndef LETS_GET_SMALL
+#include <sys/reboot.h>
+#endif
+
 #include "pathnames.h"
 
 /*
@@ -177,6 +181,11 @@
 session_t *find_session __P((pid_t));
 DB *session_db;
 
+#ifndef LETS_GET_SMALL
+void sigint_handler __P((int));
+static int do_reboot = 0;
+#endif
+
 #ifdef MSDOSFS_ROOT
 static void msdosfs_root __P((void));
 #endif
@@ -263,11 +272,11 @@
 	handle(badsys, SIGSYS, 0);
 	handle(disaster, SIGABRT, SIGFPE, SIGILL, SIGSEGV,
 	       SIGBUS, SIGXCPU, SIGXFSZ, 0);
-	handle(transition_handler, SIGHUP, SIGTERM, SIGTSTP, 0);
+	handle(transition_handler, SIGHUP, SIGINT, SIGTERM, SIGTSTP, 0);
 	handle(alrm_handler, SIGALRM, 0);
 	sigfillset(&mask);
 	delset(&mask, SIGABRT, SIGFPE, SIGILL, SIGSEGV, SIGBUS, SIGSYS,
-		SIGXCPU, SIGXFSZ, SIGHUP, SIGTERM, SIGTSTP, SIGALRM, 0);
+		SIGXCPU, SIGXFSZ, SIGHUP, SIGTERM, SIGTSTP, SIGALRM, SIGINT, 0);
 	sigprocmask(SIG_SETMASK, &mask, (sigset_t *) 0);
 	sigemptyset(&sa.sa_mask);
 	sa.sa_flags = 0;
@@ -599,6 +608,15 @@
 	if (from_securitylevel > 0)
 		setsecuritylevel(0);
 
+#ifndef LETS_GET_SMALL
+	if (do_reboot) {
+		sync();
+		alarm(3);
+		reboot(RB_HALT, NULL);
+		_exit(0);
+	}
+#endif
+
 	if ((pid = fork()) == 0) {
 		/*
 		 * Start the single user session.
@@ -1178,6 +1196,9 @@
 	case SIGHUP:
 		requested_transition = clean_ttys;
 		break;
+	case SIGINT:
+		do_reboot = 1;
+		/* FALLTHROUGH */
 	case SIGTERM:
 		requested_transition = death;
 		break;
Index: sys/conf/files
===================================================================
RCS file: /usr/pkg/libdata/cvs/netbsd/sys/conf/files,v
retrieving revision 1.1.1.7.2.9
diff -u -r1.1.1.7.2.9 files
--- sys/conf/files	2001/04/04 05:47:12	1.1.1.7.2.9
+++ sys/conf/files	2001/07/13 07:30:43
@@ -11,6 +11,7 @@
 defopt	RTC_OFFSET
 defopt	DEFCORENAME
 defopt	UCONSOLE
+defopt	CONSOLE_HALT
 
 defopt	MULTIPROCESSOR
 
Index: sys/dev/wscons/wskbd.c
===================================================================
RCS file: /usr/pkg/libdata/cvs/netbsd/sys/dev/wscons/wskbd.c,v
retrieving revision 1.1.1.2
diff -u -r1.1.1.2 wskbd.c
--- sys/dev/wscons/wskbd.c	2000/03/24 02:57:30	1.1.1.2
+++ sys/dev/wscons/wskbd.c	2001/07/13 07:30:43
@@ -85,6 +85,7 @@
  * to `wscons_events' and passes them up to the appropriate reader.
  */
 
+#include "opt_console_halt.h"
 #include "opt_ddb.h"
 
 #include <sys/param.h>
@@ -102,6 +103,9 @@
 #include <sys/errno.h>
 #include <sys/fcntl.h>
 #include <sys/vnode.h>
+#ifdef CONSOLE_HALT
+#include <sys/reboot.h>
+#endif
 
 #include <dev/wscons/wsconsio.h>
 #include <dev/wscons/wskbdvar.h>
@@ -277,6 +281,10 @@
 };
 #endif
 
+#ifdef CONSOLE_HALT
+extern int console_halt;
+#endif
+
 #if NWSDISPLAY > 0
 static void wskbd_repeat __P((void *v));
 #endif
@@ -1325,6 +1333,15 @@
 		return (1);
 	case KS_Cmd_ResetEmul:
 		wsdisplay_reset(sc->sc_displaydv, WSDISPLAY_RESETEMUL);
+#ifdef CONSOLE_HALT
+		if (console_halt && sc->sc_isconsole) {
+			printf("halt requested from console\n");
+			if (initproc != NULL)
+				psignal(initproc, SIGINT);
+			else
+				cpu_reboot(RB_HALT | RB_NOSYNC, NULL);
+		}
+#endif
 		return (1);
 	case KS_Cmd_ResetClose:
 		wsdisplay_reset(sc->sc_displaydv, WSDISPLAY_RESETCLOSE);
Index: sys/kern/kern_sysctl.c
===================================================================
RCS file: /usr/pkg/libdata/cvs/netbsd/sys/kern/kern_sysctl.c,v
retrieving revision 1.1.1.5.2.4
diff -u -r1.1.1.5.2.4 kern_sysctl.c
--- sys/kern/kern_sysctl.c	2000/08/14 07:58:55	1.1.1.5.2.4
+++ sys/kern/kern_sysctl.c	2001/07/13 07:30:43
@@ -42,6 +42,7 @@
  * sysctl system call.
  */
 
+#include "opt_console_halt.h"
 #include "opt_ddb.h"
 #include "opt_insecure.h"
 #include "opt_defcorename.h"
@@ -252,6 +253,10 @@
 extern	int	kern_logsigexit;
 extern	fixpt_t	ccpu;
 
+#ifdef CONSOLE_HALT
+int console_halt = 0;
+#endif
+
 /*
  * kernel related system variables.
  */
@@ -472,6 +477,9 @@
 			consdev = NODEV;
 		return (sysctl_rdstruct(oldp, oldlenp, newp, &consdev,
 		    sizeof consdev));
+	case KERN_CONSOLE_HALT:
+		error = sysctl_int(oldp, oldlenp, newp, newlen, &console_halt);
+		return (error);
 	default:
 		return (EOPNOTSUPP);
 	}
Index: sys/sys/sysctl.h
===================================================================
RCS file: /usr/pkg/libdata/cvs/netbsd/sys/sys/sysctl.h,v
retrieving revision 1.1.1.4.2.3
diff -u -r1.1.1.4.2.3 sysctl.h
--- sys/sys/sysctl.h	2000/07/26 03:08:28	1.1.1.4.2.3
+++ sys/sys/sysctl.h	2001/07/13 07:30:44
@@ -164,7 +164,8 @@
 #define	KERN_SYSVIPC_INFO	52	/* number of valid kern ids */
 #define	KERN_MSGBUF		53	/* kernel message buffer */
 #define	KERN_CONSDEV		54	/* dev_t: console terminal device */
-#define	KERN_MAXID		55	/* number of valid kern ids */
+#define	KERN_CONSOLE_HALT	55	/* int: allow console halt */
+#define	KERN_MAXID		56	/* number of valid kern ids */
 
 #define	CTL_KERN_NAMES { \
 	{ 0, 0 }, \
@@ -222,6 +223,7 @@
 	{ "sysvipc_info", CTLTYPE_STRUCT }, \
 	{ "msgbuf", CTLTYPE_STRUCT }, \
 	{ "consdev", CTLTYPE_STRUCT }, \
+	{ "console_halt", CTLTYPE_INT }, \
 }
 
 /*

----Next_Part(Mon_Jul_16_01:22:44_2001_542)----