Skip to content

Commit

Permalink
Bug 1232687: Add system-service interface to HAL, r=gsvelto
Browse files Browse the repository at this point in the history
The new HAL interface allows for starting, stopping, and querying
the status of system services. How these operations are performed
depends on the underlying system.

The current implementation for Gonk already contains a workaround
from Bluetooth, where quickly restarted system services require a
cool-down time between retrys.
  • Loading branch information
tdz committed Jan 4, 2016
1 parent 8f85c82 commit 27283de
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 17 deletions.
34 changes: 26 additions & 8 deletions hal/Hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ Vibrate(const nsTArray<uint32_t>& pattern, const WindowIdentifier &id)
*gLastIDToVibrate = id.AsArray();
}

// Don't forward our ID if we are not in the sandbox, because hal_impl
// Don't forward our ID if we are not in the sandbox, because hal_impl
// doesn't need it, and we don't want it to be tempted to read it. The
// empty identifier will assert if it's used.
PROXY_IF_SANDBOXED(Vibrate(pattern, InSandbox() ? id : WindowIdentifier()));
Expand Down Expand Up @@ -183,7 +183,7 @@ CancelVibrate(const WindowIdentifier &id)
// the same window. All other cancellation requests are ignored.

if (InSandbox() || (gLastIDToVibrate && *gLastIDToVibrate == id.AsArray())) {
// Don't forward our ID if we are not in the sandbox, because hal_impl
// Don't forward our ID if we are not in the sandbox, because hal_impl
// doesn't need it, and we don't want it to be tempted to read it. The
// empty identifier will assert if it's used.
PROXY_IF_SANDBOXED(CancelVibrate(InSandbox() ? id : WindowIdentifier()));
Expand Down Expand Up @@ -496,14 +496,14 @@ NotifySystemTimezoneChange(const SystemTimezoneChangeInformation& aSystemTimezon
sSystemTimezoneChangeObservers.BroadcastInformation(aSystemTimezoneChangeInfo);
}

void
void
AdjustSystemClock(int64_t aDeltaMilliseconds)
{
AssertMainThread();
PROXY_IF_SANDBOXED(AdjustSystemClock(aDeltaMilliseconds));
}

void
void
SetTimezone(const nsCString& aTimezoneSpec)
{
AssertMainThread();
Expand Down Expand Up @@ -542,7 +542,7 @@ static SensorObserverList* gSensorObservers = nullptr;
static SensorObserverList &
GetSensorObservers(SensorType sensor_type) {
MOZ_ASSERT(sensor_type < NUM_SENSOR_TYPE);

if(!gSensorObservers) {
gSensorObservers = new SensorObserverList[NUM_SENSOR_TYPE];
}
Expand All @@ -554,7 +554,7 @@ RegisterSensorObserver(SensorType aSensor, ISensorObserver *aObserver) {
SensorObserverList &observers = GetSensorObservers(aSensor);

AssertMainThread();

observers.AddObserver(aObserver);
if(observers.Length() == 1) {
EnableSensorNotifications(aSensor);
Expand Down Expand Up @@ -590,7 +590,7 @@ NotifySensorChange(const SensorData &aSensorData) {
SensorObserverList &observers = GetSensorObservers(aSensorData.sensor());

AssertMainThread();

observers.Broadcast(aSensorData);
}

Expand Down Expand Up @@ -760,7 +760,7 @@ static SwitchObserverList *sSwitchObserverLists = nullptr;

static SwitchObserverList&
GetSwitchObserverList(SwitchDevice aDevice) {
MOZ_ASSERT(0 <= aDevice && aDevice < NUM_SWITCH_DEVICE);
MOZ_ASSERT(0 <= aDevice && aDevice < NUM_SWITCH_DEVICE);
if (sSwitchObserverLists == nullptr) {
sSwitchObserverLists = new SwitchObserverList[NUM_SWITCH_DEVICE];
}
Expand Down Expand Up @@ -1194,5 +1194,23 @@ bool IsHeadphoneEventFromInputDev()
RETURN_PROXY_IF_SANDBOXED(IsHeadphoneEventFromInputDev(), false);
}

nsresult StartSystemService(const char* aSvcName, const char* aArgs)
{
AssertMainThread();
RETURN_PROXY_IF_SANDBOXED(StartSystemService(aSvcName, aArgs), NS_ERROR_FAILURE);
}

void StopSystemService(const char* aSvcName)
{
AssertMainThread();
PROXY_IF_SANDBOXED(StopSystemService(aSvcName));
}

bool SystemServiceIsRunning(const char* aSvcName)
{
AssertMainThread();
RETURN_PROXY_IF_SANDBOXED(SystemServiceIsRunning(aSvcName), false);
}

} // namespace hal
} // namespace mozilla
15 changes: 15 additions & 0 deletions hal/Hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,21 @@ uint32_t GetTotalSystemMemoryLevel();
*/
bool IsHeadphoneEventFromInputDev();

/**
* Start the system service with the specified name and arguments.
*/
nsresult StartSystemService(const char* aSvcName, const char* aArgs);

/**
* Stop the system service with the specified name.
*/
void StopSystemService(const char* aSvcName);

/**
* Determine whether the system service with the specified name is running.
*/
bool SystemServiceIsRunning(const char* aSvcName);

} // namespace MOZ_HAL_NAMESPACE
} // namespace mozilla

Expand Down
35 changes: 35 additions & 0 deletions hal/fallback/FallbackSystemService.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set sw=2 ts=8 et ft=cpp : */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "Hal.h"

namespace mozilla {
namespace hal_impl {

nsresult
StartSystemService(const char* aSvcName, const char* aArgs)
{
MOZ_ASSERT(NS_IsMainThread());

return NS_ERROR_NOT_IMPLEMENTED;
}

void
StopSystemService(const char* aSvcName)
{
MOZ_ASSERT(NS_IsMainThread());
}

bool
SystemServiceIsRunning(const char* aSvcName)
{
MOZ_ASSERT(NS_IsMainThread());

return false;
}

} // namespace hal_impl
} // namespace mozilla
131 changes: 131 additions & 0 deletions hal/gonk/SystemService.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set sw=2 ts=8 et ft=cpp : */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "Hal.h"

#include <cutils/properties.h>
#include <stdio.h>
#include <string.h>

#include "HalLog.h"
#include "nsITimer.h"
#include "mozilla/unused.h"

namespace mozilla {
namespace hal_impl {

static const int sRetryInterval = 100; // ms

bool
SystemServiceIsRunning(const char* aSvcName)
{
MOZ_ASSERT(NS_IsMainThread());

char key[PROPERTY_KEY_MAX];
auto res = snprintf(key, sizeof(key), "init.svc.%s", aSvcName);

if (res < 0) {
HAL_ERR("snprintf: %s", strerror(errno));
return false;
} else if (static_cast<size_t>(res) >= sizeof(key)) {
HAL_ERR("snprintf: trunctated service name %s", aSvcName);
return false;
}

char value[PROPERTY_VALUE_MAX];
NS_WARN_IF(property_get(key, value, "") < 0);

return !strcmp(value, "running");
}

class StartSystemServiceTimerCallback final : public nsITimerCallback
{
NS_DECL_THREADSAFE_ISUPPORTS;

public:
StartSystemServiceTimerCallback(const char* aSvcName, const char* aArgs)
: mSvcName(aSvcName)
, mArgs(aArgs)
{
MOZ_COUNT_CTOR_INHERITED(StartSystemServiceTimerCallback,
nsITimerCallback);
}

NS_IMETHOD Notify(nsITimer* aTimer) override
{
MOZ_ASSERT(NS_IsMainThread());

return StartSystemService(mSvcName.get(), mArgs.get());
}

protected:
~StartSystemServiceTimerCallback()
{
MOZ_COUNT_DTOR_INHERITED(StartSystemServiceTimerCallback,
nsITimerCallback);
}

private:
nsCString mSvcName;
nsCString mArgs;
};

NS_IMPL_ISUPPORTS0(StartSystemServiceTimerCallback);

nsresult
StartSystemService(const char* aSvcName, const char* aArgs)
{
MOZ_ASSERT(NS_IsMainThread());

char value[PROPERTY_VALUE_MAX];
auto res = snprintf(value, sizeof(value), "%s:%s", aSvcName, aArgs);

if (res < 0) {
HAL_ERR("snprintf: %s", strerror(errno));
return NS_ERROR_FAILURE;
} else if (static_cast<size_t>(res) >= sizeof(value)) {
HAL_ERR("snprintf: trunctated service name %s", aSvcName);
return NS_ERROR_OUT_OF_MEMORY;
}

if (NS_WARN_IF(property_set("ctl.start", value) < 0)) {
return NS_ERROR_FAILURE;
}

/* If the system service is not running, re-try later to start it.
*
* This condition happens when we restart a service immediately
* after it crashed, as the service state remains 'stopping'
* instead of 'stopped'. Due to the limitation of property service,
* hereby add delay. See Bug 1143925 Comment 41.
*/
if (!SystemServiceIsRunning(aSvcName)) {
nsCOMPtr<nsITimer> timer = do_CreateInstance("@mozilla.org/timer;1");
if (!timer) {
return NS_ERROR_FAILURE;
}

RefPtr<StartSystemServiceTimerCallback> timerCallback =
new StartSystemServiceTimerCallback(aSvcName, aArgs);

timer->InitWithCallback(timerCallback,
sRetryInterval,
nsITimer::TYPE_ONE_SHOT);
}

return NS_OK;
}

void
StopSystemService(const char* aSvcName)
{
MOZ_ASSERT(NS_IsMainThread());

Unused << NS_WARN_IF(property_set("ctl.stop", aSvcName));
}

} // namespace hal_impl
} // namespace mozilla
2 changes: 2 additions & 0 deletions hal/moz.build
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ elif CONFIG['MOZ_WIDGET_TOOLKIT'] == 'gonk':
'gonk/GonkFMRadio.cpp',
'gonk/GonkSensor.cpp',
'gonk/GonkSwitch.cpp',
'gonk/SystemService.cpp',
'gonk/UeventPoller.cpp',
'linux/LinuxMemory.cpp',
'linux/LinuxPower.cpp',
Expand Down Expand Up @@ -136,6 +137,7 @@ if CONFIG['MOZ_WIDGET_TOOLKIT'] != 'gonk':
'fallback/FallbackProcessPriority.cpp',
'fallback/FallbackScreenPower.cpp',
'fallback/FallbackSwitch.cpp',
'fallback/FallbackSystemService.cpp',
'fallback/FallbackThreadPriority.cpp',
'fallback/FallbackTime.cpp',
'fallback/FallbackWakeLocks.cpp',
Expand Down
Loading

0 comments on commit 27283de

Please sign in to comment.