blob: c572cee2b8d687ac9068d093bdbb6c03566ebf9e [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
Colin Crossa8666952010-04-13 19:20:44 -070017#include <fcntl.h>
18#include <stdlib.h>
19#include <sys/stat.h>
20#include <sys/types.h>
21#include <linux/keychord.h>
Olivier Baillyb93e5812010-11-17 11:47:23 -080022#include <unistd.h>
Colin Crossa8666952010-04-13 19:20:44 -070023
Tom Cherry3f5eaae52017-04-06 16:30:22 -070024#include <android-base/logging.h>
Tom Cherryccf23532017-03-28 16:40:41 -070025#include <android-base/properties.h>
26
Colin Crossa8666952010-04-13 19:20:44 -070027#include "init.h"
Tom Cherrybac32992015-07-31 12:45:25 -070028#include "service.h"
Colin Crossa8666952010-04-13 19:20:44 -070029
30static struct input_keychord *keychords = 0;
31static int keychords_count = 0;
32static int keychords_length = 0;
33static int keychord_fd = -1;
34
Tom Cherrybac32992015-07-31 12:45:25 -070035void add_service_keycodes(Service* svc)
Colin Crossa8666952010-04-13 19:20:44 -070036{
37 struct input_keychord *keychord;
Tom Cherrybac32992015-07-31 12:45:25 -070038 size_t i, size;
Colin Crossa8666952010-04-13 19:20:44 -070039
Tom Cherrybac32992015-07-31 12:45:25 -070040 if (!svc->keycodes().empty()) {
Colin Crossa8666952010-04-13 19:20:44 -070041 /* add a new keychord to the list */
Tom Cherrybac32992015-07-31 12:45:25 -070042 size = sizeof(*keychord) + svc->keycodes().size() * sizeof(keychord->keycodes[0]);
Elliott Hughesf3cf4382015-02-03 17:12:07 -080043 keychords = (input_keychord*) realloc(keychords, keychords_length + size);
Colin Crossa8666952010-04-13 19:20:44 -070044 if (!keychords) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070045 PLOG(ERROR) << "could not allocate keychords";
Colin Crossa8666952010-04-13 19:20:44 -070046 keychords_length = 0;
47 keychords_count = 0;
48 return;
49 }
50
51 keychord = (struct input_keychord *)((char *)keychords + keychords_length);
52 keychord->version = KEYCHORD_VERSION;
53 keychord->id = keychords_count + 1;
Tom Cherrybac32992015-07-31 12:45:25 -070054 keychord->count = svc->keycodes().size();
55 svc->set_keychord_id(keychord->id);
Colin Crossa8666952010-04-13 19:20:44 -070056
Tom Cherrybac32992015-07-31 12:45:25 -070057 for (i = 0; i < svc->keycodes().size(); i++) {
58 keychord->keycodes[i] = svc->keycodes()[i];
Colin Crossa8666952010-04-13 19:20:44 -070059 }
60 keychords_count++;
61 keychords_length += size;
62 }
63}
64
Elliott Hughes929f4072015-04-24 21:13:44 -070065static void handle_keychord() {
Colin Crossa8666952010-04-13 19:20:44 -070066 int ret;
67 __u16 id;
68
Colin Crossf7ca6042011-01-04 18:18:45 -080069 ret = read(keychord_fd, &id, sizeof(id));
70 if (ret != sizeof(id)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070071 PLOG(ERROR) << "could not read keychord id";
Colin Crossf7ca6042011-01-04 18:18:45 -080072 return;
73 }
74
Yabin Cui74edcea2015-07-24 10:11:05 -070075 // Only handle keychords if adb is enabled.
Tom Cherryccf23532017-03-28 16:40:41 -070076 std::string adb_enabled = android::base::GetProperty("init.svc.adbd", "");
Yabin Cui74edcea2015-07-24 10:11:05 -070077 if (adb_enabled == "running") {
Tom Cherrybac32992015-07-31 12:45:25 -070078 Service* svc = ServiceManager::GetInstance().FindServiceByKeychord(id);
Colin Crossa8666952010-04-13 19:20:44 -070079 if (svc) {
Felipe Leme704fe2d2016-07-29 08:34:39 -070080 LOG(INFO) << "Starting service " << svc->name() << " from keychord " << id;
Tom Cherrybac32992015-07-31 12:45:25 -070081 svc->Start();
Colin Crossa8666952010-04-13 19:20:44 -070082 } else {
Felipe Leme704fe2d2016-07-29 08:34:39 -070083 LOG(ERROR) << "Service for keychord " << id << " not found";
Colin Crossa8666952010-04-13 19:20:44 -070084 }
Felipe Lemec64c9822016-07-28 13:26:07 -070085 } else {
Felipe Leme704fe2d2016-07-29 08:34:39 -070086 LOG(WARNING) << "Not starting service for keychord " << id << " because ADB is disabled";
Colin Crossa8666952010-04-13 19:20:44 -070087 }
88}
89
Elliott Hughes929f4072015-04-24 21:13:44 -070090void keychord_init() {
Tom Cherrybac32992015-07-31 12:45:25 -070091 ServiceManager::GetInstance().ForEachService(add_service_keycodes);
Elliott Hughes929f4072015-04-24 21:13:44 -070092
93 // Nothing to do if no services require keychords.
94 if (!keychords) {
95 return;
96 }
97
98 keychord_fd = TEMP_FAILURE_RETRY(open("/dev/keychord", O_RDWR | O_CLOEXEC));
99 if (keychord_fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700100 PLOG(ERROR) << "could not open /dev/keychord";
Elliott Hughes929f4072015-04-24 21:13:44 -0700101 return;
102 }
103
104 int ret = write(keychord_fd, keychords, keychords_length);
105 if (ret != keychords_length) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700106 PLOG(ERROR) << "could not configure /dev/keychord " << ret;
Elliott Hughes929f4072015-04-24 21:13:44 -0700107 close(keychord_fd);
108 }
109
110 free(keychords);
111 keychords = nullptr;
112
113 register_epoll_handler(keychord_fd, handle_keychord);
Colin Crossa8666952010-04-13 19:20:44 -0700114}