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 "keychords.h"
18
19 #include <fcntl.h>
20 #include <stdlib.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <linux/keychord.h>
24 #include <unistd.h>
25
26 #include <android-base/logging.h>
27 #include <android-base/properties.h>
28
29 #include "init.h"
30
31 namespace android {
32 namespace init {
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
add_service_keycodes(Service * svc)39 void add_service_keycodes(Service* svc)
40 {
41 struct input_keychord *keychord;
42 size_t i, size;
43
44 if (!svc->keycodes().empty()) {
45 /* add a new keychord to the list */
46 size = sizeof(*keychord) + svc->keycodes().size() * sizeof(keychord->keycodes[0]);
47 keychords = (input_keychord*) realloc(keychords, keychords_length + size);
48 if (!keychords) {
49 PLOG(ERROR) << "could not allocate keychords";
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;
58 keychord->count = svc->keycodes().size();
59 svc->set_keychord_id(keychord->id);
60
61 for (i = 0; i < svc->keycodes().size(); i++) {
62 keychord->keycodes[i] = svc->keycodes()[i];
63 }
64 keychords_count++;
65 keychords_length += size;
66 }
67 }
68
handle_keychord()69 static void handle_keychord() {
70 int ret;
71 __u16 id;
72
73 ret = read(keychord_fd, &id, sizeof(id));
74 if (ret != sizeof(id)) {
75 PLOG(ERROR) << "could not read keychord id";
76 return;
77 }
78
79 // Only handle keychords if adb is enabled.
80 std::string adb_enabled = android::base::GetProperty("init.svc.adbd", "");
81 if (adb_enabled == "running") {
82 Service* svc = ServiceManager::GetInstance().FindServiceByKeychord(id);
83 if (svc) {
84 LOG(INFO) << "Starting service " << svc->name() << " from keychord " << id;
85 svc->Start();
86 } else {
87 LOG(ERROR) << "Service for keychord " << id << " not found";
88 }
89 } else {
90 LOG(WARNING) << "Not starting service for keychord " << id << " because ADB is disabled";
91 }
92 }
93
keychord_init()94 void keychord_init() {
95 ServiceManager::GetInstance().ForEachService(add_service_keycodes);
96
97 // Nothing to do if no services require keychords.
98 if (!keychords) {
99 return;
100 }
101
102 keychord_fd = TEMP_FAILURE_RETRY(open("/dev/keychord", O_RDWR | O_CLOEXEC));
103 if (keychord_fd == -1) {
104 PLOG(ERROR) << "could not open /dev/keychord";
105 return;
106 }
107
108 int ret = write(keychord_fd, keychords, keychords_length);
109 if (ret != keychords_length) {
110 PLOG(ERROR) << "could not configure /dev/keychord " << ret;
111 close(keychord_fd);
112 }
113
114 free(keychords);
115 keychords = nullptr;
116
117 register_epoll_handler(keychord_fd, handle_keychord);
118 }
119
120 } // namespace init
121 } // namespace android
122