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