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>
20 #include <string.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/properties.h>
27 
28 #include "init.h"
29 #include "log.h"
30 #include "service.h"
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 
add_service_keycodes(Service * svc)37 void add_service_keycodes(Service* svc)
38 {
39     struct input_keychord *keychord;
40     size_t i, size;
41 
42     if (!svc->keycodes().empty()) {
43         /* add a new keychord to the list */
44         size = sizeof(*keychord) + svc->keycodes().size() * sizeof(keychord->keycodes[0]);
45         keychords = (input_keychord*) realloc(keychords, keychords_length + size);
46         if (!keychords) {
47             PLOG(ERROR) << "could not allocate keychords";
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;
56         keychord->count = svc->keycodes().size();
57         svc->set_keychord_id(keychord->id);
58 
59         for (i = 0; i < svc->keycodes().size(); i++) {
60             keychord->keycodes[i] = svc->keycodes()[i];
61         }
62         keychords_count++;
63         keychords_length += size;
64     }
65 }
66 
handle_keychord()67 static void handle_keychord() {
68     int ret;
69     __u16 id;
70 
71     ret = read(keychord_fd, &id, sizeof(id));
72     if (ret != sizeof(id)) {
73         PLOG(ERROR) << "could not read keychord id";
74         return;
75     }
76 
77     // Only handle keychords if adb is enabled.
78     std::string adb_enabled = android::base::GetProperty("init.svc.adbd", "");
79     if (adb_enabled == "running") {
80         Service* svc = ServiceManager::GetInstance().FindServiceByKeychord(id);
81         if (svc) {
82             LOG(INFO) << "Starting service " << svc->name() << " from keychord " << id;
83             svc->Start();
84         } else {
85             LOG(ERROR) << "Service for keychord " << id << " not found";
86         }
87     } else {
88         LOG(WARNING) << "Not starting service for keychord " << id << " because ADB is disabled";
89     }
90 }
91 
keychord_init()92 void keychord_init() {
93     ServiceManager::GetInstance().ForEachService(add_service_keycodes);
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) {
102         PLOG(ERROR) << "could not open /dev/keychord";
103         return;
104     }
105 
106     int ret = write(keychord_fd, keychords, keychords_length);
107     if (ret != keychords_length) {
108         PLOG(ERROR) << "could not configure /dev/keychord " << ret;
109         close(keychord_fd);
110     }
111 
112     free(keychords);
113     keychords = nullptr;
114 
115     register_epoll_handler(keychord_fd, handle_keychord);
116 }
117