From nobody@FreeBSD.org  Wed Jun 27 02:00:37 2007
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52])
	by hub.freebsd.org (Postfix) with ESMTP id 48C1116A41F
	for <freebsd-gnats-submit@FreeBSD.org>; Wed, 27 Jun 2007 02:00:37 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (www.freebsd.org [69.147.83.33])
	by mx1.freebsd.org (Postfix) with ESMTP id 2145D13C448
	for <freebsd-gnats-submit@FreeBSD.org>; Wed, 27 Jun 2007 02:00:37 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.13.1/8.13.1) with ESMTP id l5R20bTD011730
	for <freebsd-gnats-submit@FreeBSD.org>; Wed, 27 Jun 2007 02:00:37 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.13.1/8.13.1/Submit) id l5R20anl011729;
	Wed, 27 Jun 2007 02:00:36 GMT
	(envelope-from nobody)
Message-Id: <200706270200.l5R20anl011729@www.freebsd.org>
Date: Wed, 27 Jun 2007 02:00:36 GMT
From: Stef Walter <stef@memberwebs.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: shutdown should fall back to exec reboot/halt when init is not running
X-Send-Pr-Version: www-3.0

>Number:         114059
>Category:       bin
>Synopsis:       [patch] shutdown(8) should fall back to exec reboot/halt when init is not running
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Wed Jun 27 02:10:04 GMT 2007
>Closed-Date:    
>Last-Modified:  Thu Feb 14 10:37:18 UTC 2008
>Originator:     Stef Walter
>Release:        FreeBSD 6.2
>Organization:
>Environment:
FreeBSD jail-devel-6x.ws.local 6.2-RELEASE-p1 FreeBSD 6.2-RELEASE-p1 #2: Tue Jun 26 15:55:31 UTC 2007     root@jail-devel-6x.ws.local:/usr/src/sys/i386/compile/JAIL-DEVEL  i386

>Description:
When running in a jail or other environment where where init (ie:
process 1) is not running, shutdown should call the reboot/halt commands.
In a jail environment these are often replaced by custom scripts which
handle restarting the jail.

Currently it is possible to call shutdown with the '-o' flag. 

This patch adds adds a fallback if the kill() call fails. The patch also
displays an error message if the kill() call fails for some other reason.


>How-To-Repeat:

>Fix:


Patch attached with submission follows:

--- sbin/shutdown/shutdown.c.orig	Wed Jun 27 01:41:11 2007
+++ sbin/shutdown/shutdown.c	Wed Jun 27 01:52:21 2007
@@ -57,6 +57,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <errno.h>
 
 #ifdef DEBUG
 #undef _PATH_NOLOGIN
@@ -328,6 +329,7 @@
 die_you_gravy_sucking_pig_dog()
 {
 	char *empty_environ[] = { NULL };
+	int sig;
 
 	syslog(LOG_NOTICE, "%s by %s: %s",
 	    doreboot ? "reboot" : dohalt ? "halt" : dopower ? "power-down" : 
@@ -351,11 +353,20 @@
 	(void)printf("\nkill -HUP 1\n");
 #else
 	if (!oflag) {
-		(void)kill(1, doreboot ? SIGINT :	/* reboot */
-			      dohalt ? SIGUSR1 :	/* halt */
-			      dopower ? SIGUSR2 :	/* power-down */
-			      SIGTERM);			/* single-user */
-	} else {
+		sig =  doreboot ? SIGINT : 	/* reboot */
+		       dohalt ? SIGUSR1 :	/* halt */
+		       dopower ? SIGUSR2 :	/* power-down */
+		       SIGTERM;         	/* single-user */
+
+		if (kill(1, sig) < 0) {
+			if (errno == ESRCH)
+				oflag = 1; /* try calling reboot */
+			else 
+				syslog(LOG_ERR, "shutdown: can't signal init (process 1): %m");
+		}
+	}
+
+	if (oflag) {
 		if (doreboot) {
 			execle(_PATH_REBOOT, "reboot", "-l", nosync, 
 				(char *)NULL, empty_environ);


>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->edwin 
Responsible-Changed-By: edwin 
Responsible-Changed-When: Tue Jan 29 12:59:39 UTC 2008 
Responsible-Changed-Why:  
Handle with mentor 

http://www.freebsd.org/cgi/query-pr.cgi?pr=114059 
Responsible-Changed-From-To: edwin->freebsd-bugs 
Responsible-Changed-By: edwin 
Responsible-Changed-When: Thu Feb 14 10:34:20 UTC 2008 
Responsible-Changed-Why:  

Give back into the pool until later. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=114059 
>Unformatted:
