From root@darla.swimsuit.roskildebc.dk  Wed Jan  7 06:21:36 1998
Received: from darla.swimsuit.roskildebc.dk (pm6-41.image.dk [194.234.173.105])
          by hub.freebsd.org (8.8.7/8.8.7) with ESMTP id GAA02672
          for <FreeBSD-gnats-submit@freebsd.org>; Wed, 7 Jan 1998 06:21:27 -0800 (PST)
          (envelope-from root@darla.swimsuit.roskildebc.dk)
Received: (from root@localhost)
	by darla.swimsuit.roskildebc.dk (8.8.8/8.8.7) id PAA00453;
	Wed, 7 Jan 1998 15:21:11 +0100 (CET)
	(envelope-from root)
Message-Id: <199801071421.PAA00453@darla.swimsuit.roskildebc.dk>
Date: Wed, 7 Jan 1998 15:21:11 +0100 (CET)
From: leifn@image.dk
Reply-To: leifn@image.dk
To: FreeBSD-gnats-submit@freebsd.org
Subject: halt/reboot does not execute /etc/rc.shutdown as kill -INT 1 does.
X-Send-Pr-Version: 3.2

>Number:         5451
>Category:       bin
>Synopsis:       [PATCH] halt/reboot does not execute /etc/rc.shutdown as kill -INT 1
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    ru
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Thu Jan  8 14:32:52 PST 1998
>Closed-Date:    Thu Jul 1 06:41:16 PDT 1999
>Last-Modified:  Thu Jul  1 06:45:57 PDT 1999
>Originator:     Leif Neland
>Release:        FreeBSD 2.2.5-STABLE i386
>Organization:
>Environment:

	

>Description:

	When shutting down, halt/reboot just kill -TERM all processes
        in a random order.
        kill -HUP 1 runs /etc/rc.shutdown, where commands can be put
        to stop service in a specified order, e.g. stop a service, 
        which logs to syslog, then stop syslog. Or stop application, then
        stop database containing the applications data.

>How-To-Repeat:

echo "/bin/echo Abandon ship">/etc/rc.shutdown
kill -HUP 1
<message is shown>
<reboot>
reboot
<message is not shown>

>Fix:
	

diff -c of reboot.8, reboot.c, init.8 and init.c enclosed.

*** reboot.8	Sat Aug 24 03:39:59 1996
--- reboot.8.NE	Wed Jan  7 12:48:34 1998
***************
*** 31,37 ****
  .\"
  .\"	@(#)reboot.8	8.1 (Berkeley) 6/9/93
  .\"
! .Dd June 9, 1993
  .Dt REBOOT 8
  .Os
  .Sh NAME
--- 31,37 ----
  .\"
  .\"	@(#)reboot.8	8.1 (Berkeley) 6/9/93
  .\"
! .Dd Jan 6, 1998
  .Dt REBOOT 8
  .Os
  .Sh NAME
***************
*** 98,103 ****
--- 98,119 ----
  .Xr shutdown 8
  utility is used when the system needs to be halted or restarted, giving
  users advance warning of their impending doom.
+ .Pp
+ If 
+ .Nm halt
+ and
+ .Nm reboot
+ is not invoked with the "impatient" -n or -q flags, 
+ .Nm init
+ is sent signal SIGINT, SIGUSR1 or SIGUSR2 to reboot, halt or powerdown,
+ instead of 
+ .Nm halt/reboot
+ doing it itself. This allows
+ .Nm init
+ to execute
+ .Nm /etc/rc.shutdown,
+ to shut down system services gracefully.
+ 
  .Sh SEE ALSO
  .Xr utmp 5 ,
  .Xr boot 8 ,
*** reboot.c	Sun Sep 14 21:41:49 1997
--- reboot.c.NE	Wed Jan  7 12:57:59 1998
***************
*** 33,38 ****
--- 33,54 ----
   *	$Id: reboot.c,v 1.3.2.3 1997/09/14 19:41:49 jkh Exp $
   */
  
+ /* Changes by Leif Neland, leifn@image.dk: 6 jan 1998
+  *
+  * When init shuts multiuser mode down, the script /etc/rc.shutdown is 
+  * executed. However reboot and halt doesn't signal init to shutdown, it
+  * just kills processes itself.
+  * 
+  * I have modified init to accept SIGUSR1 meaning shutdown and halt,
+  * and accept SIGUSR2 meaning shutdown and poweroff.
+  *
+  * I have modified reboot to sending SIGINT, SIGUSR1 and SIGUSR2, if
+  * reboot, halt or reboot -p is called.
+  *
+  * However, init sync's, so if -n, nosync is wanted, init is not signaled,
+  * instead we just let reboot kill processes as usual.
+  *
+  */
  #ifndef lint
  static char copyright[] =
  "@(#) Copyright (c) 1980, 1986, 1993\n\
***************
*** 122,138 ****
  	}
  	logwtmp("~", "shutdown", "");
  
  	/*
  	 * Do a sync early on, so disks start transfers while we're off
  	 * killing processes.  Don't worry about writes done before the
  	 * processes die, the reboot system call syncs the disks.
  	 */
! 	if (!nflag)
  		sync();
  
! 	/* Just stop init -- if we fail, we'll restart it. */
! 	if (kill(1, SIGTSTP) == -1)
! 		err(1, "SIGTSTP init");
  
  	/* Ignore the SIGHUP we get when our parent shell dies. */
  	(void)signal(SIGHUP, SIG_IGN);
--- 138,195 ----
  	}
  	logwtmp("~", "shutdown", "");
  
+ 	/* Stop init spawning gettys -- if we fail, we'll restart it. */
+ 	if (kill(1, SIGTSTP) == -1)
+ 		err(1, "SIGTSTP init");
+ 
  	/*
  	 * Do a sync early on, so disks start transfers while we're off
  	 * killing processes.  Don't worry about writes done before the
  	 * processes die, the reboot system call syncs the disks.
+ 	 *
+ 	 * if the nflag is not set, just tell init to reboot/
+ 	 * halt/powerdown, by sending it a SIGINT/SIGUSR1/SIGUSR2.
+ 	 * This will allow init to execute /etc/rc.shutdown.
+ 	 * 
  	 */
! 	if (!nflag) {
  		sync();
  
! 		/* Try letting init handle it. If signal accepted,
! 		 * then exit and let init do its stuff, else 
! 		 * we do it ourselves 
! 		 */
! 		if (howto == RB_AUTOBOOT) {
! 			if (kill(1, SIGINT) == -1) {
! 				err(1, "SIGINT init") ;
! 			} else {
! #ifdef NOISY
! 				printf("I have told init to reboot\n");
! #endif
! 				exit(0);
! 			}
! 		}
! 		if (howto & RB_POWEROFF) {
! 			if (kill(1, SIGUSR2) == -1) {
! 				err(1, "SIGUSR2 init") ;
! 			} else {
! #ifdef NOISY
! 				printf("I have told init to poweroff\n");
! #endif
! 				exit(0);
! 			}
! 		}
! 		if (howto & RB_HALT) {
! 			if (kill(1, SIGUSR1) == -1) {
! 				err(1, "SIGUSR1 init") ;
! 			} else {
! #ifdef NOISY
! 				printf("I have told init to halt\n");
! #endif
! 				exit(0);
! 			}
! 		}
! 	}
  
  	/* Ignore the SIGHUP we get when our parent shell dies. */
  	(void)signal(SIGHUP, SIG_IGN);
*** init.8	Mon Aug 18 05:30:04 1997
--- init.8.NE	Wed Jan  7 12:48:10 1998
***************
*** 35,41 ****
  .\"     @(#)init.8	8.3 (Berkeley) 4/18/94
  .\"	$Id: init.8,v 1.4.2.3 1997/08/18 03:30:04 davidn Exp $
  .\"
! .Dd April 18, 1994
  .Dt INIT 8
  .Os BSD 4
  .Sh NAME
--- 35,41 ----
  .\"     @(#)init.8	8.3 (Berkeley) 4/18/94
  .\"	$Id: init.8,v 1.4.2.3 1997/08/18 03:30:04 davidn Exp $
  .\"
! .Dd Jan 6, 1997
  .Dt INIT 8
  .Os BSD 4
  .Sh NAME
***************
*** 249,254 ****
--- 249,262 ----
  as
  .Nm innd
  (the InterNetNews server).
+ .Pp
+ .Dq Li "kill \-USR1 1"
+ and
+ .Dq Li "kill \-USR2 1"
+ works like
+ .Dq Li "kill \-INT 1"
+ except instead of rebooting after the system is shut down, the machine is
+ either halted or powered down (hardware permitting).
  .Pp
  The role of
  .Nm init
*** init.c	Mon Aug 18 05:30:04 1997
--- init.c.NE	Wed Jan  7 14:59:00 1998
***************
*** 98,103 ****
--- 98,104 ----
  #define RESOURCE_RC		"daemon"
  #define RESOURCE_WINDOW 	"default"
  #define RESOURCE_GETTY		"default"
+ #define FAKE_SYSTEM_V_INIT
  
  void handle __P((sig_t, ...));
  void delset __P((sigset_t *, ...));
***************
*** 130,135 ****
--- 131,138 ----
  #define TRUE	1
  
  int Reboot = FALSE;
+ int Halt = FALSE;
+ int Powerdown = FALSE;
  
  int devfs;
  
***************
*** 202,209 ****
  
  	/* System V users like to reexec init. */
  	if (getpid() != 1)
  		errx(1, "already running");
! 
  	/*
  	 * Note that this does NOT open a file...
  	 * Does 'init' deserve its own facility number?
--- 205,242 ----
  
  	/* System V users like to reexec init. */
  	if (getpid() != 1)
+         	{
+ #ifdef FAKE_SYSTEM_5_INIT
+ 	/* So give them what they want */
+ 		if (argc == 1)
+ 			{c = argv[1][1];
+ 				switch (c) {
+ 			case '0':
+ 				kill(1,SIGUSR1);
+ 				break;
+ 			case '1':
+ 				kill(1,SIGTERM);
+ 				break;
+ 			case '6':
+ 				kill(1,SIGINT);
+ 				break;
+ 			case 'p':
+ 				kill(1,SIGUSR2);
+ 				break;
+ 			case 'q':
+ 				kill(1,SIGHUP);
+ 				break;
+ 			case 'c':
+ 				kill(1,SIGTSTP);
+ 				break;
+ 			default:
+ 				warning("unrecognized option: '%c'", c);
+ 				break;
+ 			}
+ 		}
+ #endif
  		errx(1, "already running");
! 	}
  	/*
  	 * Note that this does NOT open a file...
  	 * Does 'init' deserve its own facility number?
***************
*** 258,268 ****
  	handle(badsys, SIGSYS, 0);
  	handle(disaster, SIGABRT, SIGFPE, SIGILL, SIGSEGV,
  	       SIGBUS, SIGXCPU, SIGXFSZ, 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, SIGINT, SIGTERM, SIGTSTP, SIGALRM, 0);
  	sigprocmask(SIG_SETMASK, &mask, (sigset_t *) 0);
  	sigemptyset(&sa.sa_mask);
  	sa.sa_flags = 0;
--- 291,303 ----
  	handle(badsys, SIGSYS, 0);
  	handle(disaster, SIGABRT, SIGFPE, SIGILL, SIGSEGV,
  	       SIGBUS, SIGXCPU, SIGXFSZ, 0);
! 	handle(transition_handler, SIGHUP, SIGINT, SIGTERM, SIGTSTP, SIGUSR1, 
! 	       SIGUSR2, 0);
  	handle(alrm_handler, SIGALRM, 0);
  	sigfillset(&mask);
  	delset(&mask, SIGABRT, SIGFPE, SIGILL, SIGSEGV, SIGBUS, SIGSYS,  
! 		SIGXCPU, SIGXFSZ, SIGHUP, SIGINT, SIGTERM, SIGTSTP, SIGALRM, 
! 		SIGUSR1, SIGUSR2, 0);
  	sigprocmask(SIG_SETMASK, &mask, (sigset_t *) 0);
  	sigemptyset(&sa.sa_mask);
  	sa.sa_flags = 0;
***************
*** 594,600 ****
  		sync();
  		alarm(2);
  		pause();
! 		reboot(RB_AUTOBOOT);
  		_exit(0);
  	}
  
--- 629,635 ----
  		sync();
  		alarm(2);
  		pause();
! 		reboot(Powerdown ? RB_POWEROFF : Halt ? RB_HALT : RB_AUTOBOOT);
  		_exit(0);
  	}
  
***************
*** 1234,1239 ****
--- 1268,1277 ----
  	case SIGHUP:
  		requested_transition = clean_ttys;
  		break;
+ 	case SIGUSR2:
+ 		Powerdown = TRUE;
+ 	case SIGUSR1:
+ 		Halt = TRUE;
  	case SIGINT:
  		Reboot = TRUE;
  	case SIGTERM:
***************
*** 1412,1417 ****
--- 1450,1456 ----
  	/* Try to run the rc.shutdown script within a period of time */
  	(void) runshutdown();
      
+ 	
  	for (i = 0; i < 2; ++i) {
  		if (kill(-1, death_sigs[i]) == -1 && errno == ESRCH)
  			return (state_func_t) single_user;
>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: gnats-admin->freebsd-bugs 
Responsible-Changed-By: steve 
Responsible-Changed-When: Thu Jan 8 14:44:31 PST 1998 
Responsible-Changed-Why:  
Misfiled PR. 

From: Leif Neland <leifn@image.dk>
To: freebsd-gnats-submit@freebsd.org, leifn@image.dk
Cc:  Subject: Re: bin/5451: halt/reboot does not execute /etc/rc.shutdown as kill -INT 1
Date: Wed, 25 Feb 1998 11:56:26 +0100

 If this is categorized as a misfiled PR, what should I have done with it
 instead?

From: Bill Fenner <fenner@parc.xerox.com>
To: Leif Neland <leifn@image.dk>
Cc: freebsd-gnats-submit@freebsd.org
Subject: Re: bin/5451: halt/reboot does not execute /etc/rc.shutdown as kill -INT 1 
Date: Thu, 26 Feb 1998 09:26:01 PST

 The "misfiled" was simply because GNATS filed it in the "pending"
 category - it may have been unable to parse some of the fields in your
 submission.  At the same time, we've had some weird GNATS problems so
 there may not have been anything wrong with your bug report at all.
 "misfiled" definitely doesn't have anything to do with the content of
 your submission, and it's now in the queue properly.
 
   Bill
State-Changed-From-To: open->suspended 
State-Changed-By: phk 
State-Changed-When: Thu Apr 30 22:46:20 PDT 1998 
State-Changed-Why:  
Awaiting committer 
Responsible-Changed-From-To: freebsd-bugs->roberto 
Responsible-Changed-By: roberto 
Responsible-Changed-When: Fri May 1 12:00:06 PDT 1998 
Responsible-Changed-Why:  
I'm taking this one. 
Responsible-Changed-From-To: roberto->ru 
Responsible-Changed-By: ru 
Responsible-Changed-When: Wed Jun 16 13:03:50 PDT 1999 
Responsible-Changed-Why:  
I'll try to do it faster :-) 
State-Changed-From-To: suspended->feedback 
State-Changed-By: ru 
State-Changed-When: Wed Jun 23 00:06:37 PDT 1999 
State-Changed-Why:  
- init(8) will now halt the system if sent USR1 signal, or halt and 
turn the power off if sent SIGUSR2. 
System V run-level support is provided but turned off by default. 

- shutdown(8) will now signal init(8) instead of executing halt(8) 
or reboot(8) when halting or rebooting the system, thus taking 
/etc/rc.shutdown into account. `-o' flag is provided for backward 
compatibility. 

- reboot(8) (aka halt(8)) is left untouched, except the manual page 
is slightly updated to clarify some details. 

- MFC is following soon 
State-Changed-From-To: feedback->closed 
State-Changed-By: ru 
State-Changed-When: Thu Jul 1 06:41:16 PDT 1999 
State-Changed-Why:  
Fixed in both -CURRENT and -STABLE. 
System V command line syntax is ``on'' by default. 
>Unformatted:
does
