blob: e686ce1e4ff83abbb093ba1935499e6add1ea6cd [file] [log] [blame]
Colin Crossa8666952010-04-13 19:20:44 -07001/*
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 Cherry81f5d3e2017-06-22 12:53:17 -070017#include "keychords.h"
18
Colin Crossa8666952010-04-13 19:20:44 -070019#include <fcntl.h>
20#include <stdlib.h>
21#include <sys/stat.h>
22#include <sys/types.h>
23#include <linux/keychord.h>
Olivier Baillyb93e5812010-11-17 11:47:23 -080024#include <unistd.h>
Colin Crossa8666952010-04-13 19:20:44 -070025
Tom Cherry3f5eaae52017-04-06 16:30:22 -070026#include <android-base/logging.h>
Tom Cherryccf23532017-03-28 16:40:41 -070027#include <android-base/properties.h>
28
Colin Crossa8666952010-04-13 19:20:44 -070029#include "init.h"
Tom Cherry81f5d3e2017-06-22 12:53:17 -070030
31namespace android {
32namespace init {
Colin Crossa8666952010-04-13 19:20:44 -070033
34static struct input_keychord *keychords = 0;
35static int keychords_count = 0;
36static int keychords_length = 0;
37static int keychord_fd = -1;
38
Tom Cherrybac32992015-07-31 12:45:25 -070039void add_service_keycodes(Service* svc)
Colin Crossa8666952010-04-13 19:20:44 -070040{
41 struct input_keychord *keychord;
Tom Cherrybac32992015-07-31 12:45:25 -070042 size_t i, size;
Colin Crossa8666952010-04-13 19:20:44 -070043
Tom Cherrybac32992015-07-31 12:45:25 -070044 if (!svc->keycodes().empty()) {
Colin Crossa8666952010-04-13 19:20:44 -070045 /* add a new keychord to the list */
Tom Cherrybac32992015-07-31 12:45:25 -070046 size = sizeof(*keychord) + svc->keycodes().size() * sizeof(keychord->keycodes[0]);
Elliott Hughesf3cf4382015-02-03 17:12:07 -080047 keychords = (input_keychord*) realloc(keychords, keychords_length + size);
Colin Crossa8666952010-04-13 19:20:44 -070048 if (!keychords) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070049 PLOG(ERROR) << "could not allocate keychords";
Colin Crossa8666952010-04-13 19:20:44 -070050 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 Cherrybac32992015-07-31 12:45:25 -070058 keychord->count = svc->keycodes().size();
59 svc->set_keychord_id(keychord->id);
Colin Crossa8666952010-04-13 19:20:44 -070060
Tom Cherrybac32992015-07-31 12:45:25 -070061 for (i = 0; i < svc->keycodes().size(); i++) {
62 keychord->keycodes[i] = svc->keycodes()[i];
Colin Crossa8666952010-04-13 19:20:44 -070063 }
64 keychords_count++;
65 keychords_length += size;
66 }
67}
68
Elliott Hughes929f4072015-04-24 21:13:44 -070069static void handle_keychord() {
Colin Crossa8666952010-04-13 19:20:44 -070070 int ret;
71 __u16 id;
72
Colin Crossf7ca6042011-01-04 18:18:45 -080073 ret = read(keychord_fd, &id, sizeof(id));
74 if (ret != sizeof(id)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070075 PLOG(ERROR) << "could not read keychord id";
Colin Crossf7ca6042011-01-04 18:18:45 -080076 return;
77 }
78
Yabin Cui74edcea2015-07-24 10:11:05 -070079 // Only handle keychords if adb is enabled.
Tom Cherryccf23532017-03-28 16:40:41 -070080 std::string adb_enabled = android::base::GetProperty("init.svc.adbd", "");
Yabin Cui74edcea2015-07-24 10:11:05 -070081 if (adb_enabled == "running") {
Tom Cherry911b9b12017-07-27 16:20:58 -070082 Service* svc = ServiceList::GetInstance().FindService(id, &Service::keychord_id);
Colin Crossa8666952010-04-13 19:20:44 -070083 if (svc) {
Tom Cherry702ca9a2017-08-25 10:36:52 -070084 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 Crossa8666952010-04-13 19:20:44 -070089 } else {
Felipe Leme704fe2d2016-07-29 08:34:39 -070090 LOG(ERROR) << "Service for keychord " << id << " not found";
Colin Crossa8666952010-04-13 19:20:44 -070091 }
Felipe Lemec64c9822016-07-28 13:26:07 -070092 } else {
Felipe Leme704fe2d2016-07-29 08:34:39 -070093 LOG(WARNING) << "Not starting service for keychord " << id << " because ADB is disabled";
Colin Crossa8666952010-04-13 19:20:44 -070094 }
95}
96
Elliott Hughes929f4072015-04-24 21:13:44 -070097void keychord_init() {
Tom Cherry911b9b12017-07-27 16:20:58 -070098 for (const auto& service : ServiceList::GetInstance()) {
99 add_service_keycodes(service.get());
100 }
Elliott Hughes929f4072015-04-24 21:13:44 -0700101
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 Hughesf86b5a62016-06-24 15:12:21 -0700109 PLOG(ERROR) << "could not open /dev/keychord";
Elliott Hughes929f4072015-04-24 21:13:44 -0700110 return;
111 }
112
113 int ret = write(keychord_fd, keychords, keychords_length);
114 if (ret != keychords_length) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700115 PLOG(ERROR) << "could not configure /dev/keychord " << ret;
Elliott Hughes929f4072015-04-24 21:13:44 -0700116 close(keychord_fd);
117 }
118
119 free(keychords);
120 keychords = nullptr;
121
122 register_epoll_handler(keychord_fd, handle_keychord);
Colin Crossa8666952010-04-13 19:20:44 -0700123}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700124
125} // namespace init
126} // namespace android