[lxc-devel] [PATCH] lxc-shutdown: use posix shell instead of bash

Natanael Copa ncopa at alpinelinux.org
Thu Dec 27 08:52:30 UTC 2012


- avoid getopt --longoptions
- use 'which' instead of 'type' to detect existance of tools
- specify -s SIG<signame> with kill

Signed-off-by: Natanael Copa <ncopa at alpinelinux.org>
---
dash didn't complain when I tested it, but it did not shut down the
container due to busybox init uses other signals for poweroff/reboot.

The poweroff/reboot signal should probably be configurable but thats
other issue.

 src/lxc/lxc-shutdown.in | 52 ++++++++++++++++++++++++++-----------------------
 1 file changed, 28 insertions(+), 24 deletions(-)

diff --git a/src/lxc/lxc-shutdown.in b/src/lxc/lxc-shutdown.in
index c0d1702..cf1d603 100644
--- a/src/lxc/lxc-shutdown.in
+++ b/src/lxc/lxc-shutdown.in
@@ -1,4 +1,4 @@
-#!/bin/bash
+#!/bin/sh
 
 # (C) Copyright Canonical 2011,2012
 
@@ -41,30 +41,30 @@ dolxcstop()
     exit 0
 }
 
-shortoptions='hn:rwt:'
-longoptions='help,name:,wait,reboot,timeout:'
-
-timeout="-1"
-
-getopt=$(getopt -o $shortoptions --longoptions  $longoptions -- "$@")
-if [ $? != 0 ]; then
+usage_err() {
+    [ -n "$1" ] && echo "$1" >&2
     usage
-    exit 1;
-fi
+    exit 1
+}
+
+optarg_check() {
+    [ -n "$2" ] || usage_err "option '$1' requires an argument"
+}
 
-eval set -- "$getopt"
+timeout="-1"
 
 reboot=0
 dowait=0
 
-while true; do
-    case "$1" in
+while [ $# -gt 0 ]; do
+    opt="$1"
+    shift
+    case "$opt" in
     -h|--help)
         usage
-        exit 1
         ;;
     -n|--name)
-        shift
+        optarg_check $opt "$1"
         lxc_name=$1
         shift
         ;;
@@ -77,19 +77,23 @@ while true; do
         shift
         ;;
     -t|--timeout)
-        shift
+        optarg_check $opt "$1"
         timeout=$1
         dowait=1
         shift
         ;;
     --)
-        shift
         break;;
+    -?)
+        usage_err "unknown option '$opt'"
+        ;;
+    -*)
+        # split opts -abc into -a -b -c
+        set -- $(echo "${opt#-}" | sed 's/\(.\)/ -\1/g') "$@"
+        ;;
     *)
-        echo $1
-        usage
+        usage_err "unknown option '$opt'"
         exit 1
-        ;;
     esac
 done
 
@@ -104,8 +108,8 @@ if [ "$(id -u)" != "0" ]; then
    exit 1
 fi
 
-type lxc-info > /dev/null || { echo "lxc-info not found."; exit 1; }
-type lxc-wait > /dev/null || { echo "lxc-wait not found."; exit 1; }
+which lxc-info > /dev/null || { echo "lxc-info not found."; exit 1; }
+which lxc-wait > /dev/null || { echo "lxc-wait not found."; exit 1; }
 
 pid=`lxc-info -n $lxc_name -p 2>/dev/null | awk '{ print $2 }'`
 if [ "$pid" = "-1" ]; then
@@ -114,10 +118,10 @@ if [ "$pid" = "-1" ]; then
 fi
 
 if [ $reboot -eq 1 ]; then
-    kill -INT $pid
+    kill -s SIGINT $pid
     exit 0
 else
-    kill -PWR $pid
+    kill -s SIGPWR $pid
 fi
 
 if [ $dowait -eq 0 ]; then
-- 
1.8.0.2





More information about the lxc-devel mailing list