1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/hal/linux/LinuxPower.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,128 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "Hal.h" 1.10 + 1.11 +#include <unistd.h> 1.12 +#include <sys/reboot.h> 1.13 +#include "nsIObserverService.h" 1.14 +#include "mozilla/Services.h" 1.15 + 1.16 +namespace mozilla { 1.17 +namespace hal_impl { 1.18 + 1.19 +void 1.20 +Reboot() 1.21 +{ 1.22 + nsCOMPtr<nsIObserverService> obsServ = services::GetObserverService(); 1.23 + if (obsServ) { 1.24 + obsServ->NotifyObservers(nullptr, "system-reboot", nullptr); 1.25 + } 1.26 + sync(); 1.27 + reboot(RB_AUTOBOOT); 1.28 +} 1.29 + 1.30 +void 1.31 +PowerOff() 1.32 +{ 1.33 + nsCOMPtr<nsIObserverService> obsServ = services::GetObserverService(); 1.34 + if (obsServ) { 1.35 + obsServ->NotifyObservers(nullptr, "system-power-off", nullptr); 1.36 + } 1.37 + sync(); 1.38 + reboot(RB_POWER_OFF); 1.39 +} 1.40 + 1.41 +// Structure to specify how watchdog pthread is going to work. 1.42 +typedef struct watchdogParam 1.43 +{ 1.44 + hal::ShutdownMode mode; // Specify how to shutdown the system. 1.45 + int32_t timeoutSecs; // Specify the delayed seconds to shutdown the system. 1.46 + 1.47 + watchdogParam(hal::ShutdownMode aMode, int32_t aTimeoutSecs) 1.48 + : mode(aMode), timeoutSecs(aTimeoutSecs) {} 1.49 +} watchdogParam_t; 1.50 + 1.51 +// Function to complusively shut down the system with a given mode. 1.52 +static void 1.53 +QuitHard(hal::ShutdownMode aMode) 1.54 +{ 1.55 + switch (aMode) 1.56 + { 1.57 + case hal::eHalShutdownMode_PowerOff: 1.58 + PowerOff(); 1.59 + break; 1.60 + case hal::eHalShutdownMode_Reboot: 1.61 + Reboot(); 1.62 + break; 1.63 + case hal::eHalShutdownMode_Restart: 1.64 + // Don't let signal handlers affect forced shutdown. 1.65 + kill(0, SIGKILL); 1.66 + // If we can't SIGKILL our process group, something is badly 1.67 + // wrong. Trying to deliver a catch-able signal to ourselves can 1.68 + // invoke signal handlers and might cause problems. So try 1.69 + // _exit() and hope we go away. 1.70 + _exit(1); 1.71 + break; 1.72 + default: 1.73 + MOZ_CRASH(); 1.74 + } 1.75 +} 1.76 + 1.77 +// Function to complusively shut down the system with a given mode when timeout. 1.78 +static void* 1.79 +ForceQuitWatchdog(void* aParamPtr) 1.80 +{ 1.81 + watchdogParam_t* paramPtr = reinterpret_cast<watchdogParam_t*>(aParamPtr); 1.82 + if (paramPtr->timeoutSecs > 0 && paramPtr->timeoutSecs <= 30) { 1.83 + // If we shut down normally before the timeout, this thread will 1.84 + // be harmlessly reaped by the OS. 1.85 + TimeStamp deadline = 1.86 + (TimeStamp::Now() + TimeDuration::FromSeconds(paramPtr->timeoutSecs)); 1.87 + while (true) { 1.88 + TimeDuration remaining = (deadline - TimeStamp::Now()); 1.89 + int sleepSeconds = int(remaining.ToSeconds()); 1.90 + if (sleepSeconds <= 0) { 1.91 + break; 1.92 + } 1.93 + sleep(sleepSeconds); 1.94 + } 1.95 + } 1.96 + hal::ShutdownMode mode = paramPtr->mode; 1.97 + delete paramPtr; 1.98 + QuitHard(mode); 1.99 + return nullptr; 1.100 +} 1.101 + 1.102 +void 1.103 +StartForceQuitWatchdog(hal::ShutdownMode aMode, int32_t aTimeoutSecs) 1.104 +{ 1.105 + // Force-quits are intepreted a little more ferociously on Gonk, 1.106 + // because while Gecko is in the process of shutting down, the user 1.107 + // can't call 911, for example. And if we hang on shutdown, bad 1.108 + // things happen. So, make sure that doesn't happen. 1.109 + if (aTimeoutSecs <= 0) { 1.110 + return; 1.111 + } 1.112 + 1.113 + // Use a raw pthread here to insulate ourselves from bugs in other 1.114 + // Gecko code that we're trying to protect! 1.115 + // 1.116 + // Note that we let the watchdog in charge of releasing |paramPtr| 1.117 + // if the pthread is successfully created. 1.118 + watchdogParam_t* paramPtr = new watchdogParam_t(aMode, aTimeoutSecs); 1.119 + pthread_t watchdog; 1.120 + if (pthread_create(&watchdog, nullptr, 1.121 + ForceQuitWatchdog, 1.122 + reinterpret_cast<void*>(paramPtr))) { 1.123 + // Better safe than sorry. 1.124 + delete paramPtr; 1.125 + QuitHard(aMode); 1.126 + } 1.127 + // The watchdog thread is off and running now. 1.128 +} 1.129 + 1.130 +} // hal_impl 1.131 +} // mozilla