blob: 700ac91b9c3cc7a138e245c58d27dee61687b2b6 [file] [log] [blame]
San Mehatc41d1c82009-05-14 14:58:45 -07001#include <errno.h>
2
3#include <sysutils/ServiceManager.h>
4
5#define LOG_TAG "Service"
6#include <cutils/log.h>
7#include <cutils/properties.h>
8
9ServiceManager::ServiceManager() {
10}
11
12int ServiceManager::start(const char *name) {
13 if (isRunning(name)) {
14 LOGW("Service '%s' is already running", name);
15 return 0;
16 }
17
18 LOGD("Starting service '%s'", name);
19 property_set("ctl.start", name);
20
21 int count = 200;
22 while(count--) {
23 sched_yield();
24 if (isRunning(name))
25 break;
26 }
27 if (!count) {
28 LOGW("Timed out waiting for service '%s' to start", name);
29 errno = ETIMEDOUT;
30 return -1;
31 }
32 LOGD("Sucessfully started '%s'", name);
33 return 0;
34}
35
36int ServiceManager::stop(const char *name) {
37 if (!isRunning(name)) {
38 LOGW("Service '%s' is already stopped", name);
39 return 0;
40 }
41
42 LOGD("Stopping service '%s'", name);
43 property_set("ctl.stop", name);
44
45 int count = 200;
46 while(count--) {
47 sched_yield();
48 if (!isRunning(name))
49 break;
50 }
51
52 if (!count) {
53 LOGW("Timed out waiting for service '%s' to stop", name);
54 errno = ETIMEDOUT;
55 return -1;
56 }
57 LOGD("Sucessfully stopped '%s'", name);
58 return 0;
59}
60
61bool ServiceManager::isRunning(const char *name) {
62 char propVal[PROPERTY_VALUE_MAX];
63 char propName[255];
64
65 snprintf(propName, sizeof(propVal), "init.svc.%s", name);
66
67
68 if (property_get(propName, propVal, NULL)) {
69 if (!strcmp(propVal, "running"))
70 return true;
71 }
72 return false;
73}