1 /*
2  * Copyright (C) 2020 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 /* this program is used to read a set of system properties and their values
18  * from the emulator program and set them in the currently-running emulated
19  * system. It does so by connecting to the 'boot-properties' qemud service.
20  *
21  * This file parses the sys/class/virtio-ports/<id>/name
22  * and set up vendor.qemu.vport.modem=/dev/<id> so that reference-ril
23  * can open it later
24  */
25 
26 #include <fstream>
27 #include <string>
28 #include <android-base/strings.h>
29 #include <log/log.h>
30 #include <cutils/properties.h>
31 
32 #include <dirent.h>
33 #include <error.h>
34 #include <string.h>
35 #include <unistd.h>
36 
set_port_prop(const char * filename,const char * portname)37 static void set_port_prop(const char* filename, const char* portname) {
38     std::ifstream myfile(filename);
39     if (myfile.is_open()) {
40         const std::string portdev = std::string{"/dev/"} + portname;
41 
42         for (std::string line; std::getline(myfile, line); ) {
43             std::string serialname = android::base::Trim(line);
44             if (serialname.empty()) {
45                 continue;
46             }
47             serialname = std::string("vendor.qemu.vport.") + serialname;
48             if(property_set(serialname.c_str(), portdev.c_str()) < 0) {
49                 ALOGW("could not set property '%s' to '%s'", serialname.c_str(),
50                       portdev.c_str());
51             } else {
52                 ALOGI("successfully set property '%s' to '%s'", serialname.c_str(), portdev.c_str());
53             }
54         }
55         myfile.close();
56     } else {
57         ALOGW("could not open '%s'", filename);
58     }
59 }
60 
close_dir(DIR * dp)61 static void close_dir(DIR *dp) { closedir(dp); }
62 
read_virio_ports_dir(const char * cpath)63 static void read_virio_ports_dir(const char *cpath)
64 {
65     std::unique_ptr<DIR, decltype(&close_dir)> mydp(opendir(cpath),
66                                                     &close_dir);
67 
68     if (!mydp) {
69         ALOGW("cannot open dir %s; %s\n", cpath, strerror(errno));
70         return;
71     }
72 
73     const std::string path(cpath);
74 
75     struct dirent *files;
76     while ((files = readdir(mydp.get())) != NULL) {
77         if (strcmp(files->d_name, ".") == 0 ||
78             strcmp(files->d_name, "..") == 0) {
79             continue;
80         }
81 
82         std::string filename = path + std::string("/") + std::string(files->d_name) + "/name";
83         set_port_prop(filename.c_str(), files->d_name);
84     }
85 }
86 
parse_virtio_serial()87 void parse_virtio_serial() {
88     read_virio_ports_dir("/sys/class/virtio-ports");
89 }
90 
91 
92 
93