Home | History | Annotate | Download | only in init
      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 
     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 
     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 = ServiceList::GetInstance().FindService(id, &Service::keychord_id);
     83         if (svc) {
     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             }
     89         } else {
     90             LOG(ERROR) << "Service for keychord " << id << " not found";
     91         }
     92     } else {
     93         LOG(WARNING) << "Not starting service for keychord " << id << " because ADB is disabled";
     94     }
     95 }
     96 
     97 void keychord_init() {
     98     for (const auto& service : ServiceList::GetInstance()) {
     99         add_service_keycodes(service.get());
    100     }
    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) {
    109         PLOG(ERROR) << "could not open /dev/keychord";
    110         return;
    111     }
    112 
    113     int ret = write(keychord_fd, keychords, keychords_length);
    114     if (ret != keychords_length) {
    115         PLOG(ERROR) << "could not configure /dev/keychord " << ret;
    116         close(keychord_fd);
    117     }
    118 
    119     free(keychords);
    120     keychords = nullptr;
    121 
    122     register_epoll_handler(keychord_fd, handle_keychord);
    123 }
    124 
    125 }  // namespace init
    126 }  // namespace android
    127