1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
/*
* Copyright (C) 2006 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.os;
import com.google.android.collect.Maps;
import android.annotation.UnsupportedAppUsage;
import java.util.HashMap;
import java.util.concurrent.TimeoutException;
/**
* Controls and utilities for low-level {@code init} services.
*
* @hide
*/
public class SystemService {
private static HashMap<String, State> sStates = Maps.newHashMap();
/**
* State of a known {@code init} service.
*/
public enum State {
RUNNING("running"),
STOPPING("stopping"),
STOPPED("stopped"),
RESTARTING("restarting");
State(String state) {
sStates.put(state, this);
}
}
private static Object sPropertyLock = new Object();
static {
SystemProperties.addChangeCallback(new Runnable() {
@Override
public void run() {
synchronized (sPropertyLock) {
sPropertyLock.notifyAll();
}
}
});
}
/** Request that the init daemon start a named service. */
@UnsupportedAppUsage
public static void start(String name) {
SystemProperties.set("ctl.start", name);
}
/** Request that the init daemon stop a named service. */
@UnsupportedAppUsage
public static void stop(String name) {
SystemProperties.set("ctl.stop", name);
}
/** Request that the init daemon restart a named service. */
public static void restart(String name) {
SystemProperties.set("ctl.restart", name);
}
/**
* Return current state of given service.
*/
public static State getState(String service) {
final String rawState = SystemProperties.get("init.svc." + service);
final State state = sStates.get(rawState);
if (state != null) {
return state;
} else {
return State.STOPPED;
}
}
/**
* Check if given service is {@link State#STOPPED}.
*/
public static boolean isStopped(String service) {
return State.STOPPED.equals(getState(service));
}
/**
* Check if given service is {@link State#RUNNING}.
*/
public static boolean isRunning(String service) {
return State.RUNNING.equals(getState(service));
}
/**
* Wait until given service has entered specific state.
*/
public static void waitForState(String service, State state, long timeoutMillis)
throws TimeoutException {
final long endMillis = SystemClock.elapsedRealtime() + timeoutMillis;
while (true) {
synchronized (sPropertyLock) {
final State currentState = getState(service);
if (state.equals(currentState)) {
return;
}
if (SystemClock.elapsedRealtime() >= endMillis) {
throw new TimeoutException("Service " + service + " currently " + currentState
+ "; waited " + timeoutMillis + "ms for " + state);
}
try {
sPropertyLock.wait(timeoutMillis);
} catch (InterruptedException e) {
}
}
}
}
/**
* Wait until any of given services enters {@link State#STOPPED}.
*/
public static void waitForAnyStopped(String... services) {
while (true) {
synchronized (sPropertyLock) {
for (String service : services) {
if (State.STOPPED.equals(getState(service))) {
return;
}
}
try {
sPropertyLock.wait();
} catch (InterruptedException e) {
}
}
}
}
}
|