| Mark Salyzyn | 66ce3e0 | 2016-09-28 10:07:20 -0700 | [diff] [blame] | 1 | /* | 
|  | 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 Mehat | c41d1c8 | 2009-05-14 14:58:45 -0700 | [diff] [blame] | 19 | #include <errno.h> | 
| Mark Salyzyn | cfd5b08 | 2016-10-17 14:28:00 -0700 | [diff] [blame] | 20 | #include <stdio.h> | 
| Olivier Bailly | b93e581 | 2010-11-17 11:47:23 -0800 | [diff] [blame] | 21 | #include <string.h> | 
| Elliott Hughes | b7788fd | 2017-02-28 09:54:36 -0800 | [diff] [blame] | 22 | #include <sys/system_properties.h> | 
| Mark Salyzyn | cfd5b08 | 2016-10-17 14:28:00 -0700 | [diff] [blame] | 23 | #include <unistd.h> | 
| San Mehat | c41d1c8 | 2009-05-14 14:58:45 -0700 | [diff] [blame] | 24 |  | 
| Elliott Hughes | b7788fd | 2017-02-28 09:54:36 -0800 | [diff] [blame] | 25 | #include <android-base/properties.h> | 
|  | 26 | #include <android-base/stringprintf.h> | 
| Mark Salyzyn | cfd5b08 | 2016-10-17 14:28:00 -0700 | [diff] [blame] | 27 | #include <log/log.h> | 
| Mark Salyzyn | 66ce3e0 | 2016-09-28 10:07:20 -0700 | [diff] [blame] | 28 | #include <sysutils/ServiceManager.h> | 
| San Mehat | c41d1c8 | 2009-05-14 14:58:45 -0700 | [diff] [blame] | 29 |  | 
|  | 30 | ServiceManager::ServiceManager() { | 
|  | 31 | } | 
|  | 32 |  | 
| Elliott Hughes | b7788fd | 2017-02-28 09:54:36 -0800 | [diff] [blame] | 33 | // 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. | 
|  | 38 | static constexpr size_t SERVICE_NAME_MAX = PROP_VALUE_MAX; | 
| David 'Digit' Turner | af174f0 | 2011-01-17 02:22:22 +0100 | [diff] [blame] | 39 |  | 
|  | 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 Mehat | c41d1c8 | 2009-05-14 14:58:45 -0700 | [diff] [blame] | 48 | int ServiceManager::start(const char *name) { | 
| David 'Digit' Turner | af174f0 | 2011-01-17 02:22:22 +0100 | [diff] [blame] | 49 | if (strlen(name) > SERVICE_NAME_MAX) { | 
|  | 50 | SLOGE("Service name '%s' is too long", name); | 
|  | 51 | return 0; | 
|  | 52 | } | 
| Elliott Hughes | b7788fd | 2017-02-28 09:54:36 -0800 | [diff] [blame] | 53 |  | 
| San Mehat | c41d1c8 | 2009-05-14 14:58:45 -0700 | [diff] [blame] | 54 | if (isRunning(name)) { | 
| San Mehat | 7e8529a | 2010-03-25 09:31:42 -0700 | [diff] [blame] | 55 | SLOGW("Service '%s' is already running", name); | 
| San Mehat | c41d1c8 | 2009-05-14 14:58:45 -0700 | [diff] [blame] | 56 | return 0; | 
|  | 57 | } | 
|  | 58 |  | 
| San Mehat | 7e8529a | 2010-03-25 09:31:42 -0700 | [diff] [blame] | 59 | SLOGD("Starting service '%s'", name); | 
| Elliott Hughes | b7788fd | 2017-02-28 09:54:36 -0800 | [diff] [blame] | 60 | android::base::SetProperty("ctl.start", name); | 
| San Mehat | c41d1c8 | 2009-05-14 14:58:45 -0700 | [diff] [blame] | 61 |  | 
| David 'Digit' Turner | af174f0 | 2011-01-17 02:22:22 +0100 | [diff] [blame] | 62 | int count = SLEEP_MAX_USEC; | 
|  | 63 | while(count > 0) { | 
|  | 64 | usleep(SLEEP_MIN_USEC); | 
|  | 65 | count -= SLEEP_MIN_USEC; | 
| San Mehat | c41d1c8 | 2009-05-14 14:58:45 -0700 | [diff] [blame] | 66 | if (isRunning(name)) | 
|  | 67 | break; | 
|  | 68 | } | 
| David 'Digit' Turner | af174f0 | 2011-01-17 02:22:22 +0100 | [diff] [blame] | 69 | if (count <= 0) { | 
| San Mehat | 7e8529a | 2010-03-25 09:31:42 -0700 | [diff] [blame] | 70 | SLOGW("Timed out waiting for service '%s' to start", name); | 
| San Mehat | c41d1c8 | 2009-05-14 14:58:45 -0700 | [diff] [blame] | 71 | errno = ETIMEDOUT; | 
|  | 72 | return -1; | 
|  | 73 | } | 
| San Mehat | 7e8529a | 2010-03-25 09:31:42 -0700 | [diff] [blame] | 74 | SLOGD("Sucessfully started '%s'", name); | 
| San Mehat | c41d1c8 | 2009-05-14 14:58:45 -0700 | [diff] [blame] | 75 | return 0; | 
|  | 76 | } | 
|  | 77 |  | 
|  | 78 | int ServiceManager::stop(const char *name) { | 
| David 'Digit' Turner | af174f0 | 2011-01-17 02:22:22 +0100 | [diff] [blame] | 79 | if (strlen(name) > SERVICE_NAME_MAX) { | 
|  | 80 | SLOGE("Service name '%s' is too long", name); | 
|  | 81 | return 0; | 
|  | 82 | } | 
| Elliott Hughes | b7788fd | 2017-02-28 09:54:36 -0800 | [diff] [blame] | 83 |  | 
| San Mehat | c41d1c8 | 2009-05-14 14:58:45 -0700 | [diff] [blame] | 84 | if (!isRunning(name)) { | 
| San Mehat | 7e8529a | 2010-03-25 09:31:42 -0700 | [diff] [blame] | 85 | SLOGW("Service '%s' is already stopped", name); | 
| San Mehat | c41d1c8 | 2009-05-14 14:58:45 -0700 | [diff] [blame] | 86 | return 0; | 
|  | 87 | } | 
|  | 88 |  | 
| San Mehat | 7e8529a | 2010-03-25 09:31:42 -0700 | [diff] [blame] | 89 | SLOGD("Stopping service '%s'", name); | 
| Elliott Hughes | b7788fd | 2017-02-28 09:54:36 -0800 | [diff] [blame] | 90 | android::base::SetProperty("ctl.stop", name); | 
| San Mehat | c41d1c8 | 2009-05-14 14:58:45 -0700 | [diff] [blame] | 91 |  | 
| David 'Digit' Turner | af174f0 | 2011-01-17 02:22:22 +0100 | [diff] [blame] | 92 | int count = SLEEP_MAX_USEC; | 
|  | 93 | while(count > 0) { | 
|  | 94 | usleep(SLEEP_MIN_USEC); | 
|  | 95 | count -= SLEEP_MIN_USEC; | 
| San Mehat | c41d1c8 | 2009-05-14 14:58:45 -0700 | [diff] [blame] | 96 | if (!isRunning(name)) | 
|  | 97 | break; | 
|  | 98 | } | 
|  | 99 |  | 
| David 'Digit' Turner | af174f0 | 2011-01-17 02:22:22 +0100 | [diff] [blame] | 100 | if (count <= 0) { | 
| San Mehat | 7e8529a | 2010-03-25 09:31:42 -0700 | [diff] [blame] | 101 | SLOGW("Timed out waiting for service '%s' to stop", name); | 
| San Mehat | c41d1c8 | 2009-05-14 14:58:45 -0700 | [diff] [blame] | 102 | errno = ETIMEDOUT; | 
|  | 103 | return -1; | 
|  | 104 | } | 
| David 'Digit' Turner | af174f0 | 2011-01-17 02:22:22 +0100 | [diff] [blame] | 105 | SLOGD("Successfully stopped '%s'", name); | 
| San Mehat | c41d1c8 | 2009-05-14 14:58:45 -0700 | [diff] [blame] | 106 | return 0; | 
|  | 107 | } | 
|  | 108 |  | 
|  | 109 | bool ServiceManager::isRunning(const char *name) { | 
| Elliott Hughes | b7788fd | 2017-02-28 09:54:36 -0800 | [diff] [blame] | 110 | std::string property_name = android::base::StringPrintf("init.svc.%s", name); | 
|  | 111 | return (android::base::GetProperty(property_name, "") == "running"); | 
| San Mehat | c41d1c8 | 2009-05-14 14:58:45 -0700 | [diff] [blame] | 112 | } |