Index: /trunk/src/VBox/Frontends/VBoxAutostart/VBoxAutostartStop.cpp
===================================================================
--- /trunk/src/VBox/Frontends/VBoxAutostart/VBoxAutostartStop.cpp	(revision 43655)
+++ /trunk/src/VBox/Frontends/VBoxAutostart/VBoxAutostartStop.cpp	(revision 43656)
@@ -158,10 +158,16 @@
                 CHECK_ERROR_BREAK(machine, COMGETTER(State)(&enmMachineState));
 
+                /* Wait until the VM changes from a transient state back. */
+                while (   enmMachineState >= MachineState_FirstTransient
+                       && enmMachineState <= MachineState_LastTransient)
+                {
+                    RTThreadSleep(1000);
+                    CHECK_ERROR_BREAK(machine, COMGETTER(State)(&enmMachineState));
+                }
+
                 /* Only power off running machines. */
-                /** @todo: What about transient VM states? */
                 if (   enmMachineState == MachineState_Running
                     || enmMachineState == MachineState_Paused)
                 {
-                    ComPtr<IMachine> sessionMachine;
                     ComPtr<IConsole> console;
                     ComPtr<IProgress> progress;
@@ -172,5 +178,4 @@
                     /* get the associated console */
                     CHECK_ERROR_BREAK(g_pSession, COMGETTER(Console)(console.asOutParam()));
-                    CHECK_ERROR_BREAK(g_pSession, COMGETTER(Machine)(sessionMachine.asOutParam()));
 
                     switch ((*it).enmAutostopType)
@@ -194,10 +199,20 @@
                             BOOL fGuestEnteredACPI = false;
                             CHECK_ERROR_BREAK(console, GetGuestEnteredACPIMode(&fGuestEnteredACPI));
-                            if (fGuestEnteredACPI)
+                            if (fGuestEnteredACPI && enmMachineState == MachineState_Running)
+                            {
                                 CHECK_ERROR_BREAK(console, PowerButton());
+
+                                serviceLog("Waiting for VM \"%ls\" to power off...\n", (*it).strId.raw());
+
+                                do
+                                {
+                                    RTThreadSleep(1000);
+                                    CHECK_ERROR_BREAK(machine, COMGETTER(State)(&enmMachineState));
+                                } while (enmMachineState == MachineState_Running);
+                            }
                             else
                             {
                                 /* Use save state instead and log this to the console. */
-                                serviceLog("The guest of VM \"%ls\" does not support ACPI shutdown, saving state...\n",
+                                serviceLog("The guest of VM \"%ls\" does not support ACPI shutdown or is currently paused, saving state...\n",
                                            (*it).strId.raw());
                                 rc = autostartSaveVMState(console);
@@ -208,6 +223,6 @@
                             serviceLog("Unknown autostop type for VM \"%ls\"\n", (*it).strId.raw());
                     }
-                }
-                g_pSession->UnlockMachine();
+                    g_pSession->UnlockMachine();
+                }
             }
         }
Index: /trunk/src/VBox/Installer/darwin/VirtualBox/VBoxAutostartDarwin.sh
===================================================================
--- /trunk/src/VBox/Installer/darwin/VirtualBox/VBoxAutostartDarwin.sh	(revision 43655)
+++ /trunk/src/VBox/Installer/darwin/VirtualBox/VBoxAutostartDarwin.sh	(revision 43656)
@@ -18,5 +18,5 @@
 #
 
-function vboxStartAllUserVms()
+function vboxStartStopAllUserVms()
 {
     # Go through the list and filter out all users without a shell and a
@@ -34,13 +34,28 @@
         fi
 
-        # Start the daemon
-        su ${user} -c "/Applications/VirtualBox.app/Contents/MacOS/VBoxAutostart --quiet --start --background --config ${1}"
-
+        case "${1}" in
+            start)
+                # Start the daemon
+                su ${user} -c "/Applications/VirtualBox.app/Contents/MacOS/VBoxAutostart --quiet --start --background --config ${CONFIG}"
+                ;;
+            stop)
+                # Start the daemon
+                su ${user} -c "/Applications/VirtualBox.app/Contents/MacOS/VBoxAutostart --quiet --stop --config ${CONFIG}"
+                ;;
+               *)
+                echo "Usage: start|stop"
+                exit 1
+        esac
     done
 }
 
-case $1 in
-    --start) vboxStartAllUserVms ${2};;
-          *) echo "Unknown option ${1}";;
-esac
+function vboxStopAllUserVms()
+{
+    vboxStartStopAllUserVms "stop"
+}
 
+CONFIG=${1}
+vboxStartStopAllUserVms "start"
+trap vboxStopAllUserVms HUP KILL TERM
+
+
Index: /trunk/src/VBox/Installer/darwin/VirtualBox/org.virtualbox.vboxautostart.plist
===================================================================
--- /trunk/src/VBox/Installer/darwin/VirtualBox/org.virtualbox.vboxautostart.plist	(revision 43655)
+++ /trunk/src/VBox/Installer/darwin/VirtualBox/org.virtualbox.vboxautostart.plist	(revision 43656)
@@ -12,5 +12,4 @@
   <array>
     <string>/Applications/VirtualBox.app/Contents/MacOS/VBoxAutostartDarwin.sh</string>
-    <string>--start</string>
     <string>/etc/vbox/autostart.cfg</string>
   </array>
Index: /trunk/src/VBox/Installer/linux/vboxautostart-service.sh
===================================================================
--- /trunk/src/VBox/Installer/linux/vboxautostart-service.sh	(revision 43655)
+++ /trunk/src/VBox/Installer/linux/vboxautostart-service.sh	(revision 43656)
@@ -291,23 +291,15 @@
     [ -z "$VBOXAUTOSTART_CONFIG" ] && exit 0
 
-    exit 0
-
-    #begin_msg "Stopping VirtualBox VMs configured for autostop";
-    #vboxdrvrunning || {
-    #    fail_msg "VirtualBox kernel module not loaded!"
-    #    exit 0
-    #}
-    #PARAMS="--stop"
-    #[ -n "$VBOXAUTOSTART_CONFIG" ] && PARAMS="$PARAMS -c $VBOXAUTOSTART_CONFIG"
+    PARAMS="--stop --config $VBOXAUTOSTART_CONFIG"
 
     # prevent inheriting this setting to VBoxSVC
-    #unset VBOX_RELEASE_LOG_DEST
-
-    #for user in `ls $VBOXAUTOSTART_DB/*.stop`
-    #do
-    #    start_daemon `basename $user | sed -ne "s/\(.*\).stop/\1/p"` $binary $PARAMS > /dev/null 2>&1
-    #done
-
-    #return $RETVAL
+    unset VBOX_RELEASE_LOG_DEST
+
+    for user in `ls $VBOXAUTOSTART_DB/*.stop`
+    do
+        start_daemon `basename $user | sed -ne "s/\(.*\).stop/\1/p"` $binary $PARAMS > /dev/null 2>&1
+    done
+
+    return $RETVAL
 }
 
Index: /trunk/src/VBox/Installer/solaris/smf-vboxautostart.sh
===================================================================
--- /trunk/src/VBox/Installer/solaris/smf-vboxautostart.sh	(revision 43655)
+++ /trunk/src/VBox/Installer/solaris/smf-vboxautostart.sh	(revision 43656)
@@ -65,6 +65,42 @@
     ;;
     stop)
-        # Kill service contract
-        smf_kill_contract $2 TERM 1
+        if [ ! -x /opt/VirtualBox/VBoxAutostart ]; then
+            echo "ERROR: /opt/VirtualBox/VBoxAutostart does not exist."
+            return $SMF_EXIT_ERR_CONFIG
+        fi
+
+        if [ ! -f /opt/VirtualBox/VBoxAutostart ]; then
+            echo "ERROR: /opt/VirtualBox/VBoxAutostart does not exist."
+            return $SMF_EXIT_ERR_CONFIG
+        fi
+
+        # Get svc configuration
+        VW_CONFIG=`/usr/bin/svcprop -p config/config $SMF_FMRI 2>/dev/null`
+        [ $? != 0 ] && VW_CONFIG=
+        VW_ROTATE=`/usr/bin/svcprop -p config/logrotate $SMF_FMRI 2>/dev/null`
+        [ $? != 0 ] && VW_ROTATE=
+        VW_LOGSIZE=`/usr/bin/svcprop -p config/logsize $SMF_FMRI 2>/dev/null`
+        [ $? != 0 ] && VW_LOGSIZE=
+        VW_LOGINTERVAL=`/usr/bin/svcprop -p config/loginterval $SMF_FMRI 2>/dev/null`
+        [ $? != 0 ] && VW_LOGINTERVAL=
+
+        # Provide sensible defaults
+        [ -z "$VW_CONFIG" ] && VW_CONFIG=/etc/vbox/autostart.cfg
+        [ -z "$VW_ROTATE" ] && VW_ROTATE=10
+        [ -z "$VW_LOGSIZE" ] && VW_LOGSIZE=104857600
+        [ -z "$VW_LOGINTERVAL" ] && VW_LOGINTERVAL=86400
+
+        # Get all users
+        for VW_USER in `logins -g staff`
+        do
+            exec su - "$VW_USER" -c "/opt/VirtualBox/VBoxAutostart --stop --config \"$VW_CONFIG\" --logrotate \"$VW_ROTATE\" --logsize \"$VW_LOGSIZE\" --loginterval \"$VW_LOGINTERVAL\""
+
+            VW_EXIT=$?
+            if [ $VW_EXIT != 0 ]; then
+                echo "VBoxAutostart failed with $VW_EXIT."
+                VW_EXIT=1
+                break
+            fi
+        done
     ;;
     *)
Index: /trunk/src/VBox/Installer/solaris/virtualbox-autostart.xml
===================================================================
--- /trunk/src/VBox/Installer/solaris/virtualbox-autostart.xml	(revision 43655)
+++ /trunk/src/VBox/Installer/solaris/virtualbox-autostart.xml	(revision 43656)
@@ -71,6 +71,6 @@
             type='method'
             name='stop'
-            exec=':kill'
-            timeout_seconds='15'>
+            exec='/opt/VirtualBox/smf-vboxautostart.sh %m'
+            timeout_seconds='0'>
             <method_context>
                 <method_credential user='root' group='root' />
