| Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2010 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 | #include <errno.h> | 
|  | 18 | #include <fcntl.h> | 
|  | 19 | #include <stdlib.h> | 
| Olivier Bailly | b93e581 | 2010-11-17 11:47:23 -0800 | [diff] [blame] | 20 | #include <string.h> | 
| Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 21 | #include <sys/stat.h> | 
|  | 22 | #include <sys/types.h> | 
|  | 23 | #include <linux/keychord.h> | 
| Olivier Bailly | b93e581 | 2010-11-17 11:47:23 -0800 | [diff] [blame] | 24 | #include <unistd.h> | 
| Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 25 |  | 
| Tom Cherry | e249097 | 2017-03-28 16:40:41 -0700 | [diff] [blame] | 26 | #include <android-base/properties.h> | 
|  | 27 |  | 
| Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 28 | #include "init.h" | 
| Colin Cross | ed8a7d8 | 2010-04-19 17:05:34 -0700 | [diff] [blame] | 29 | #include "log.h" | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 30 | #include "service.h" | 
| Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 31 |  | 
|  | 32 | static struct input_keychord *keychords = 0; | 
|  | 33 | static int keychords_count = 0; | 
|  | 34 | static int keychords_length = 0; | 
|  | 35 | static int keychord_fd = -1; | 
|  | 36 |  | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 37 | void add_service_keycodes(Service* svc) | 
| Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 38 | { | 
|  | 39 | struct input_keychord *keychord; | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 40 | size_t i, size; | 
| Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 41 |  | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 42 | if (!svc->keycodes().empty()) { | 
| Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 43 | /* add a new keychord to the list */ | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 44 | size = sizeof(*keychord) + svc->keycodes().size() * sizeof(keychord->keycodes[0]); | 
| Elliott Hughes | f3cf438 | 2015-02-03 17:12:07 -0800 | [diff] [blame] | 45 | keychords = (input_keychord*) realloc(keychords, keychords_length + size); | 
| Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 46 | if (!keychords) { | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 47 | PLOG(ERROR) << "could not allocate keychords"; | 
| Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 48 | keychords_length = 0; | 
|  | 49 | keychords_count = 0; | 
|  | 50 | return; | 
|  | 51 | } | 
|  | 52 |  | 
|  | 53 | keychord = (struct input_keychord *)((char *)keychords + keychords_length); | 
|  | 54 | keychord->version = KEYCHORD_VERSION; | 
|  | 55 | keychord->id = keychords_count + 1; | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 56 | keychord->count = svc->keycodes().size(); | 
|  | 57 | svc->set_keychord_id(keychord->id); | 
| Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 58 |  | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 59 | for (i = 0; i < svc->keycodes().size(); i++) { | 
|  | 60 | keychord->keycodes[i] = svc->keycodes()[i]; | 
| Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 61 | } | 
|  | 62 | keychords_count++; | 
|  | 63 | keychords_length += size; | 
|  | 64 | } | 
|  | 65 | } | 
|  | 66 |  | 
| Elliott Hughes | 929f407 | 2015-04-24 21:13:44 -0700 | [diff] [blame] | 67 | static void handle_keychord() { | 
| Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 68 | int ret; | 
|  | 69 | __u16 id; | 
|  | 70 |  | 
| Colin Cross | f7ca604 | 2011-01-04 18:18:45 -0800 | [diff] [blame] | 71 | ret = read(keychord_fd, &id, sizeof(id)); | 
|  | 72 | if (ret != sizeof(id)) { | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 73 | PLOG(ERROR) << "could not read keychord id"; | 
| Colin Cross | f7ca604 | 2011-01-04 18:18:45 -0800 | [diff] [blame] | 74 | return; | 
|  | 75 | } | 
|  | 76 |  | 
| Yabin Cui | 74edcea | 2015-07-24 10:11:05 -0700 | [diff] [blame] | 77 | // Only handle keychords if adb is enabled. | 
| Tom Cherry | e249097 | 2017-03-28 16:40:41 -0700 | [diff] [blame] | 78 | std::string adb_enabled = android::base::GetProperty("init.svc.adbd", ""); | 
| Yabin Cui | 74edcea | 2015-07-24 10:11:05 -0700 | [diff] [blame] | 79 | if (adb_enabled == "running") { | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 80 | Service* svc = ServiceManager::GetInstance().FindServiceByKeychord(id); | 
| Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 81 | if (svc) { | 
| Felipe Leme | 704fe2d | 2016-07-29 08:34:39 -0700 | [diff] [blame] | 82 | LOG(INFO) << "Starting service " << svc->name() << " from keychord " << id; | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 83 | svc->Start(); | 
| Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 84 | } else { | 
| Felipe Leme | 704fe2d | 2016-07-29 08:34:39 -0700 | [diff] [blame] | 85 | LOG(ERROR) << "Service for keychord " << id << " not found"; | 
| Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 86 | } | 
| Felipe Leme | c64c982 | 2016-07-28 13:26:07 -0700 | [diff] [blame] | 87 | } else { | 
| Felipe Leme | 704fe2d | 2016-07-29 08:34:39 -0700 | [diff] [blame] | 88 | LOG(WARNING) << "Not starting service for keychord " << id << " because ADB is disabled"; | 
| Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 89 | } | 
|  | 90 | } | 
|  | 91 |  | 
| Elliott Hughes | 929f407 | 2015-04-24 21:13:44 -0700 | [diff] [blame] | 92 | void keychord_init() { | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 93 | ServiceManager::GetInstance().ForEachService(add_service_keycodes); | 
| Elliott Hughes | 929f407 | 2015-04-24 21:13:44 -0700 | [diff] [blame] | 94 |  | 
|  | 95 | // Nothing to do if no services require keychords. | 
|  | 96 | if (!keychords) { | 
|  | 97 | return; | 
|  | 98 | } | 
|  | 99 |  | 
|  | 100 | keychord_fd = TEMP_FAILURE_RETRY(open("/dev/keychord", O_RDWR | O_CLOEXEC)); | 
|  | 101 | if (keychord_fd == -1) { | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 102 | PLOG(ERROR) << "could not open /dev/keychord"; | 
| Elliott Hughes | 929f407 | 2015-04-24 21:13:44 -0700 | [diff] [blame] | 103 | return; | 
|  | 104 | } | 
|  | 105 |  | 
|  | 106 | int ret = write(keychord_fd, keychords, keychords_length); | 
|  | 107 | if (ret != keychords_length) { | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 108 | PLOG(ERROR) << "could not configure /dev/keychord " << ret; | 
| Elliott Hughes | 929f407 | 2015-04-24 21:13:44 -0700 | [diff] [blame] | 109 | close(keychord_fd); | 
|  | 110 | } | 
|  | 111 |  | 
|  | 112 | free(keychords); | 
|  | 113 | keychords = nullptr; | 
|  | 114 |  | 
|  | 115 | register_epoll_handler(keychord_fd, handle_keychord); | 
| Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 116 | } |