hal/linux/LinuxPower.cpp

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "Hal.h"
michael@0 7
michael@0 8 #include <unistd.h>
michael@0 9 #include <sys/reboot.h>
michael@0 10 #include "nsIObserverService.h"
michael@0 11 #include "mozilla/Services.h"
michael@0 12
michael@0 13 namespace mozilla {
michael@0 14 namespace hal_impl {
michael@0 15
michael@0 16 void
michael@0 17 Reboot()
michael@0 18 {
michael@0 19 nsCOMPtr<nsIObserverService> obsServ = services::GetObserverService();
michael@0 20 if (obsServ) {
michael@0 21 obsServ->NotifyObservers(nullptr, "system-reboot", nullptr);
michael@0 22 }
michael@0 23 sync();
michael@0 24 reboot(RB_AUTOBOOT);
michael@0 25 }
michael@0 26
michael@0 27 void
michael@0 28 PowerOff()
michael@0 29 {
michael@0 30 nsCOMPtr<nsIObserverService> obsServ = services::GetObserverService();
michael@0 31 if (obsServ) {
michael@0 32 obsServ->NotifyObservers(nullptr, "system-power-off", nullptr);
michael@0 33 }
michael@0 34 sync();
michael@0 35 reboot(RB_POWER_OFF);
michael@0 36 }
michael@0 37
michael@0 38 // Structure to specify how watchdog pthread is going to work.
michael@0 39 typedef struct watchdogParam
michael@0 40 {
michael@0 41 hal::ShutdownMode mode; // Specify how to shutdown the system.
michael@0 42 int32_t timeoutSecs; // Specify the delayed seconds to shutdown the system.
michael@0 43
michael@0 44 watchdogParam(hal::ShutdownMode aMode, int32_t aTimeoutSecs)
michael@0 45 : mode(aMode), timeoutSecs(aTimeoutSecs) {}
michael@0 46 } watchdogParam_t;
michael@0 47
michael@0 48 // Function to complusively shut down the system with a given mode.
michael@0 49 static void
michael@0 50 QuitHard(hal::ShutdownMode aMode)
michael@0 51 {
michael@0 52 switch (aMode)
michael@0 53 {
michael@0 54 case hal::eHalShutdownMode_PowerOff:
michael@0 55 PowerOff();
michael@0 56 break;
michael@0 57 case hal::eHalShutdownMode_Reboot:
michael@0 58 Reboot();
michael@0 59 break;
michael@0 60 case hal::eHalShutdownMode_Restart:
michael@0 61 // Don't let signal handlers affect forced shutdown.
michael@0 62 kill(0, SIGKILL);
michael@0 63 // If we can't SIGKILL our process group, something is badly
michael@0 64 // wrong. Trying to deliver a catch-able signal to ourselves can
michael@0 65 // invoke signal handlers and might cause problems. So try
michael@0 66 // _exit() and hope we go away.
michael@0 67 _exit(1);
michael@0 68 break;
michael@0 69 default:
michael@0 70 MOZ_CRASH();
michael@0 71 }
michael@0 72 }
michael@0 73
michael@0 74 // Function to complusively shut down the system with a given mode when timeout.
michael@0 75 static void*
michael@0 76 ForceQuitWatchdog(void* aParamPtr)
michael@0 77 {
michael@0 78 watchdogParam_t* paramPtr = reinterpret_cast<watchdogParam_t*>(aParamPtr);
michael@0 79 if (paramPtr->timeoutSecs > 0 && paramPtr->timeoutSecs <= 30) {
michael@0 80 // If we shut down normally before the timeout, this thread will
michael@0 81 // be harmlessly reaped by the OS.
michael@0 82 TimeStamp deadline =
michael@0 83 (TimeStamp::Now() + TimeDuration::FromSeconds(paramPtr->timeoutSecs));
michael@0 84 while (true) {
michael@0 85 TimeDuration remaining = (deadline - TimeStamp::Now());
michael@0 86 int sleepSeconds = int(remaining.ToSeconds());
michael@0 87 if (sleepSeconds <= 0) {
michael@0 88 break;
michael@0 89 }
michael@0 90 sleep(sleepSeconds);
michael@0 91 }
michael@0 92 }
michael@0 93 hal::ShutdownMode mode = paramPtr->mode;
michael@0 94 delete paramPtr;
michael@0 95 QuitHard(mode);
michael@0 96 return nullptr;
michael@0 97 }
michael@0 98
michael@0 99 void
michael@0 100 StartForceQuitWatchdog(hal::ShutdownMode aMode, int32_t aTimeoutSecs)
michael@0 101 {
michael@0 102 // Force-quits are intepreted a little more ferociously on Gonk,
michael@0 103 // because while Gecko is in the process of shutting down, the user
michael@0 104 // can't call 911, for example. And if we hang on shutdown, bad
michael@0 105 // things happen. So, make sure that doesn't happen.
michael@0 106 if (aTimeoutSecs <= 0) {
michael@0 107 return;
michael@0 108 }
michael@0 109
michael@0 110 // Use a raw pthread here to insulate ourselves from bugs in other
michael@0 111 // Gecko code that we're trying to protect!
michael@0 112 //
michael@0 113 // Note that we let the watchdog in charge of releasing |paramPtr|
michael@0 114 // if the pthread is successfully created.
michael@0 115 watchdogParam_t* paramPtr = new watchdogParam_t(aMode, aTimeoutSecs);
michael@0 116 pthread_t watchdog;
michael@0 117 if (pthread_create(&watchdog, nullptr,
michael@0 118 ForceQuitWatchdog,
michael@0 119 reinterpret_cast<void*>(paramPtr))) {
michael@0 120 // Better safe than sorry.
michael@0 121 delete paramPtr;
michael@0 122 QuitHard(aMode);
michael@0 123 }
michael@0 124 // The watchdog thread is off and running now.
michael@0 125 }
michael@0 126
michael@0 127 } // hal_impl
michael@0 128 } // mozilla

mercurial