1 /*
2  * Copyright (C) 2021 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  * Copied from device/google/cuttlefish/guest/commands/vport_trigger in Android R
17  * commit hash :69e0935b2929b26c5fc29646eef1d33c39e344f1
18  * (Migrate vport_trigger's Android.mk to Android.bp)
19  *
20  */
21 
22 #include <cutils/properties.h>
23 
24 #include <sys/cdefs.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 
28 #include <dirent.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31 
32 #include <cerrno>
33 #include <climits>
34 #include <string>
35 #include "android-base/file.h"
36 #include "android-base/logging.h"
37 
38 int main(int argc __unused, char* argv[] __unused) {
39     const char sysfs_base[] = "/sys/class/virtio-ports/";
40     DIR* dir = opendir(sysfs_base);
41 
42     if (dir) {
43         dirent* dp;
44         while ((dp = readdir(dir)) != nullptr) {
45             std::string dirname = dp->d_name;
46             std::string sysfs(sysfs_base + dirname + "/name");
47             struct stat st;
48             if (stat(sysfs.c_str(), &st)) {
49                 continue;
50             }
51             std::string content;
52             if (!android::base::ReadFileToString(sysfs, &content, true)) {
53                 continue;
54             }
55             if (content.empty()) {
56                 continue;
57             }
58             // Removing the last \n character
59             content.erase(content.end() - 1);
60             std::string dev("/dev/" + dirname);
61             std::string propname("vendor.ser." + content);
62             property_set(propname.c_str(), dev.c_str());
63         }
64     }
65     return 0;
66 }
67