blob: c7aa1f706098dffa6ca6605363785a3f7612d124 [file] [log] [blame]
Mark Salyzyn66ce3e02016-09-28 10:07:20 -07001/*
2 * Copyright (C) 2009-2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Service"
18
San Mehatc41d1c82009-05-14 14:58:45 -070019#include <errno.h>
Mark Salyzyncfd5b082016-10-17 14:28:00 -070020#include <stdio.h>
Olivier Baillyb93e5812010-11-17 11:47:23 -080021#include <string.h>
Elliott Hughesb7788fd2017-02-28 09:54:36 -080022#include <sys/system_properties.h>
Mark Salyzyncfd5b082016-10-17 14:28:00 -070023#include <unistd.h>
San Mehatc41d1c82009-05-14 14:58:45 -070024
Elliott Hughesb7788fd2017-02-28 09:54:36 -080025#include <android-base/properties.h>
26#include <android-base/stringprintf.h>
Mark Salyzyncfd5b082016-10-17 14:28:00 -070027#include <log/log.h>
Mark Salyzyn66ce3e02016-09-28 10:07:20 -070028#include <sysutils/ServiceManager.h>
San Mehatc41d1c82009-05-14 14:58:45 -070029
30ServiceManager::ServiceManager() {
31}
32
Elliott Hughesb7788fd2017-02-28 09:54:36 -080033// The length of a service name should not exceed SERVICE_NAME_MAX. Starting
34// a service is done by writing its name to the "ctl.start" system property
35// and stopping a service is done by writing its name to "ctl.stop". If a
36// service name is too long to fit in a property, you won't be able to start
37// or stop it.
38static constexpr size_t SERVICE_NAME_MAX = PROP_VALUE_MAX;
David 'Digit' Turneraf174f02011-01-17 02:22:22 +010039
40/* The maximum amount of time to wait for a service to start or stop,
41 * in micro-seconds (really an approximation) */
42#define SLEEP_MAX_USEC 2000000 /* 2 seconds */
43
44/* The minimal sleeping interval between checking for the service's state
45 * when looping for SLEEP_MAX_USEC */
46#define SLEEP_MIN_USEC 200000 /* 200 msec */
47
San Mehatc41d1c82009-05-14 14:58:45 -070048int ServiceManager::start(const char *name) {
David 'Digit' Turneraf174f02011-01-17 02:22:22 +010049 if (strlen(name) > SERVICE_NAME_MAX) {
50 SLOGE("Service name '%s' is too long", name);
51 return 0;
52 }
Elliott Hughesb7788fd2017-02-28 09:54:36 -080053
San Mehatc41d1c82009-05-14 14:58:45 -070054 if (isRunning(name)) {
San Mehat7e8529a2010-03-25 09:31:42 -070055 SLOGW("Service '%s' is already running", name);
San Mehatc41d1c82009-05-14 14:58:45 -070056 return 0;
57 }
58
San Mehat7e8529a2010-03-25 09:31:42 -070059 SLOGD("Starting service '%s'", name);
Elliott Hughesb7788fd2017-02-28 09:54:36 -080060 android::base::SetProperty("ctl.start", name);
San Mehatc41d1c82009-05-14 14:58:45 -070061
David 'Digit' Turneraf174f02011-01-17 02:22:22 +010062 int count = SLEEP_MAX_USEC;
63 while(count > 0) {
64 usleep(SLEEP_MIN_USEC);
65 count -= SLEEP_MIN_USEC;
San Mehatc41d1c82009-05-14 14:58:45 -070066 if (isRunning(name))
67 break;
68 }
David 'Digit' Turneraf174f02011-01-17 02:22:22 +010069 if (count <= 0) {
San Mehat7e8529a2010-03-25 09:31:42 -070070 SLOGW("Timed out waiting for service '%s' to start", name);
San Mehatc41d1c82009-05-14 14:58:45 -070071 errno = ETIMEDOUT;
72 return -1;
73 }
San Mehat7e8529a2010-03-25 09:31:42 -070074 SLOGD("Sucessfully started '%s'", name);
San Mehatc41d1c82009-05-14 14:58:45 -070075 return 0;
76}
77
78int ServiceManager::stop(const char *name) {
David 'Digit' Turneraf174f02011-01-17 02:22:22 +010079 if (strlen(name) > SERVICE_NAME_MAX) {
80 SLOGE("Service name '%s' is too long", name);
81 return 0;
82 }
Elliott Hughesb7788fd2017-02-28 09:54:36 -080083
San Mehatc41d1c82009-05-14 14:58:45 -070084 if (!isRunning(name)) {
San Mehat7e8529a2010-03-25 09:31:42 -070085 SLOGW("Service '%s' is already stopped", name);
San Mehatc41d1c82009-05-14 14:58:45 -070086 return 0;
87 }
88
San Mehat7e8529a2010-03-25 09:31:42 -070089 SLOGD("Stopping service '%s'", name);
Elliott Hughesb7788fd2017-02-28 09:54:36 -080090 android::base::SetProperty("ctl.stop", name);
San Mehatc41d1c82009-05-14 14:58:45 -070091
David 'Digit' Turneraf174f02011-01-17 02:22:22 +010092 int count = SLEEP_MAX_USEC;
93 while(count > 0) {
94 usleep(SLEEP_MIN_USEC);
95 count -= SLEEP_MIN_USEC;
San Mehatc41d1c82009-05-14 14:58:45 -070096 if (!isRunning(name))
97 break;
98 }
99
David 'Digit' Turneraf174f02011-01-17 02:22:22 +0100100 if (count <= 0) {
San Mehat7e8529a2010-03-25 09:31:42 -0700101 SLOGW("Timed out waiting for service '%s' to stop", name);
San Mehatc41d1c82009-05-14 14:58:45 -0700102 errno = ETIMEDOUT;
103 return -1;
104 }
David 'Digit' Turneraf174f02011-01-17 02:22:22 +0100105 SLOGD("Successfully stopped '%s'", name);
San Mehatc41d1c82009-05-14 14:58:45 -0700106 return 0;
107}
108
109bool ServiceManager::isRunning(const char *name) {
Elliott Hughesb7788fd2017-02-28 09:54:36 -0800110 std::string property_name = android::base::StringPrintf("init.svc.%s", name);
111 return (android::base::GetProperty(property_name, "") == "running");
San Mehatc41d1c82009-05-14 14:58:45 -0700112}