| 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> | 
| Mark Salyzyn | cfd5b08 | 2016-10-17 14:28:00 -0700 | [diff] [blame] | 22 | #include <unistd.h> | 
| San Mehat | c41d1c8 | 2009-05-14 14:58:45 -0700 | [diff] [blame] | 23 |  | 
| San Mehat | c41d1c8 | 2009-05-14 14:58:45 -0700 | [diff] [blame] | 24 | #include <cutils/properties.h> | 
| Mark Salyzyn | cfd5b08 | 2016-10-17 14:28:00 -0700 | [diff] [blame] | 25 | #include <log/log.h> | 
| Mark Salyzyn | 66ce3e0 | 2016-09-28 10:07:20 -0700 | [diff] [blame] | 26 | #include <sysutils/ServiceManager.h> | 
| San Mehat | c41d1c8 | 2009-05-14 14:58:45 -0700 | [diff] [blame] | 27 |  | 
|  | 28 | ServiceManager::ServiceManager() { | 
|  | 29 | } | 
|  | 30 |  | 
| David 'Digit' Turner | af174f0 | 2011-01-17 02:22:22 +0100 | [diff] [blame] | 31 | /* The service name should not exceed SERVICE_NAME_MAX to avoid | 
|  | 32 | * some weird things. This is due to the fact that: | 
|  | 33 | * | 
|  | 34 | * - Starting a service is done by writing its name to the "ctl.start" | 
|  | 35 | *   system property. This triggers the init daemon to actually start | 
|  | 36 | *   the service for us. | 
|  | 37 | * | 
|  | 38 | * - Stopping the service is done by writing its name to "ctl.stop" | 
|  | 39 | *   in a similar way. | 
|  | 40 | * | 
|  | 41 | * - Reading the status of a service is done by reading the property | 
|  | 42 | *   named "init.svc.<name>" | 
|  | 43 | * | 
|  | 44 | * If strlen(<name>) > (PROPERTY_KEY_MAX-1)-9, then you can start/stop | 
|  | 45 | * the service by writing to ctl.start/stop, but you won't be able to | 
|  | 46 | * read its state due to the truncation of "init.svc.<name>" into a | 
|  | 47 | * zero-terminated buffer of PROPERTY_KEY_MAX characters. | 
|  | 48 | */ | 
|  | 49 | #define SERVICE_NAME_MAX  (PROPERTY_KEY_MAX-10) | 
|  | 50 |  | 
|  | 51 | /* The maximum amount of time to wait for a service to start or stop, | 
|  | 52 | * in micro-seconds (really an approximation) */ | 
|  | 53 | #define  SLEEP_MAX_USEC     2000000  /* 2 seconds */ | 
|  | 54 |  | 
|  | 55 | /* The minimal sleeping interval between checking for the service's state | 
|  | 56 | * when looping for SLEEP_MAX_USEC */ | 
|  | 57 | #define  SLEEP_MIN_USEC      200000  /* 200 msec */ | 
|  | 58 |  | 
| San Mehat | c41d1c8 | 2009-05-14 14:58:45 -0700 | [diff] [blame] | 59 | int ServiceManager::start(const char *name) { | 
| David 'Digit' Turner | af174f0 | 2011-01-17 02:22:22 +0100 | [diff] [blame] | 60 | if (strlen(name) > SERVICE_NAME_MAX) { | 
|  | 61 | SLOGE("Service name '%s' is too long", name); | 
|  | 62 | return 0; | 
|  | 63 | } | 
| San Mehat | c41d1c8 | 2009-05-14 14:58:45 -0700 | [diff] [blame] | 64 | if (isRunning(name)) { | 
| San Mehat | 7e8529a | 2010-03-25 09:31:42 -0700 | [diff] [blame] | 65 | SLOGW("Service '%s' is already running", name); | 
| San Mehat | c41d1c8 | 2009-05-14 14:58:45 -0700 | [diff] [blame] | 66 | return 0; | 
|  | 67 | } | 
|  | 68 |  | 
| San Mehat | 7e8529a | 2010-03-25 09:31:42 -0700 | [diff] [blame] | 69 | SLOGD("Starting service '%s'", name); | 
| San Mehat | c41d1c8 | 2009-05-14 14:58:45 -0700 | [diff] [blame] | 70 | property_set("ctl.start", name); | 
|  | 71 |  | 
| David 'Digit' Turner | af174f0 | 2011-01-17 02:22:22 +0100 | [diff] [blame] | 72 | int count = SLEEP_MAX_USEC; | 
|  | 73 | while(count > 0) { | 
|  | 74 | usleep(SLEEP_MIN_USEC); | 
|  | 75 | count -= SLEEP_MIN_USEC; | 
| San Mehat | c41d1c8 | 2009-05-14 14:58:45 -0700 | [diff] [blame] | 76 | if (isRunning(name)) | 
|  | 77 | break; | 
|  | 78 | } | 
| David 'Digit' Turner | af174f0 | 2011-01-17 02:22:22 +0100 | [diff] [blame] | 79 | if (count <= 0) { | 
| San Mehat | 7e8529a | 2010-03-25 09:31:42 -0700 | [diff] [blame] | 80 | SLOGW("Timed out waiting for service '%s' to start", name); | 
| San Mehat | c41d1c8 | 2009-05-14 14:58:45 -0700 | [diff] [blame] | 81 | errno = ETIMEDOUT; | 
|  | 82 | return -1; | 
|  | 83 | } | 
| San Mehat | 7e8529a | 2010-03-25 09:31:42 -0700 | [diff] [blame] | 84 | SLOGD("Sucessfully started '%s'", name); | 
| San Mehat | c41d1c8 | 2009-05-14 14:58:45 -0700 | [diff] [blame] | 85 | return 0; | 
|  | 86 | } | 
|  | 87 |  | 
|  | 88 | int ServiceManager::stop(const char *name) { | 
| David 'Digit' Turner | af174f0 | 2011-01-17 02:22:22 +0100 | [diff] [blame] | 89 | if (strlen(name) > SERVICE_NAME_MAX) { | 
|  | 90 | SLOGE("Service name '%s' is too long", name); | 
|  | 91 | return 0; | 
|  | 92 | } | 
| San Mehat | c41d1c8 | 2009-05-14 14:58:45 -0700 | [diff] [blame] | 93 | if (!isRunning(name)) { | 
| San Mehat | 7e8529a | 2010-03-25 09:31:42 -0700 | [diff] [blame] | 94 | SLOGW("Service '%s' is already stopped", name); | 
| San Mehat | c41d1c8 | 2009-05-14 14:58:45 -0700 | [diff] [blame] | 95 | return 0; | 
|  | 96 | } | 
|  | 97 |  | 
| San Mehat | 7e8529a | 2010-03-25 09:31:42 -0700 | [diff] [blame] | 98 | SLOGD("Stopping service '%s'", name); | 
| San Mehat | c41d1c8 | 2009-05-14 14:58:45 -0700 | [diff] [blame] | 99 | property_set("ctl.stop", name); | 
|  | 100 |  | 
| David 'Digit' Turner | af174f0 | 2011-01-17 02:22:22 +0100 | [diff] [blame] | 101 | int count = SLEEP_MAX_USEC; | 
|  | 102 | while(count > 0) { | 
|  | 103 | usleep(SLEEP_MIN_USEC); | 
|  | 104 | count -= SLEEP_MIN_USEC; | 
| San Mehat | c41d1c8 | 2009-05-14 14:58:45 -0700 | [diff] [blame] | 105 | if (!isRunning(name)) | 
|  | 106 | break; | 
|  | 107 | } | 
|  | 108 |  | 
| David 'Digit' Turner | af174f0 | 2011-01-17 02:22:22 +0100 | [diff] [blame] | 109 | if (count <= 0) { | 
| San Mehat | 7e8529a | 2010-03-25 09:31:42 -0700 | [diff] [blame] | 110 | SLOGW("Timed out waiting for service '%s' to stop", name); | 
| San Mehat | c41d1c8 | 2009-05-14 14:58:45 -0700 | [diff] [blame] | 111 | errno = ETIMEDOUT; | 
|  | 112 | return -1; | 
|  | 113 | } | 
| David 'Digit' Turner | af174f0 | 2011-01-17 02:22:22 +0100 | [diff] [blame] | 114 | SLOGD("Successfully stopped '%s'", name); | 
| San Mehat | c41d1c8 | 2009-05-14 14:58:45 -0700 | [diff] [blame] | 115 | return 0; | 
|  | 116 | } | 
|  | 117 |  | 
|  | 118 | bool ServiceManager::isRunning(const char *name) { | 
|  | 119 | char propVal[PROPERTY_VALUE_MAX]; | 
| David 'Digit' Turner | af174f0 | 2011-01-17 02:22:22 +0100 | [diff] [blame] | 120 | char propName[PROPERTY_KEY_MAX]; | 
|  | 121 | int  ret; | 
| San Mehat | c41d1c8 | 2009-05-14 14:58:45 -0700 | [diff] [blame] | 122 |  | 
| David 'Digit' Turner | af174f0 | 2011-01-17 02:22:22 +0100 | [diff] [blame] | 123 | ret = snprintf(propName, sizeof(propName), "init.svc.%s", name); | 
|  | 124 | if (ret > (int)sizeof(propName)-1) { | 
|  | 125 | SLOGD("Service name '%s' is too long", name); | 
|  | 126 | return false; | 
|  | 127 | } | 
| San Mehat | c41d1c8 | 2009-05-14 14:58:45 -0700 | [diff] [blame] | 128 |  | 
|  | 129 | if (property_get(propName, propVal, NULL)) { | 
|  | 130 | if (!strcmp(propVal, "running")) | 
|  | 131 | return true; | 
|  | 132 | } | 
|  | 133 | return false; | 
|  | 134 | } |