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
main(int argc __unused,char * argv[]__unused)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 if (dirname == "." || dirname == "..") {
47 continue;
48 }
49 std::string sysfs(sysfs_base + dirname + "/name");
50 struct stat st;
51 if (stat(sysfs.c_str(), &st)) {
52 continue;
53 }
54 std::string content;
55 if (!android::base::ReadFileToString(sysfs, &content, true)) {
56 continue;
57 }
58 if (content.empty()) {
59 continue;
60 }
61 // Removing the last \n character
62 content.erase(content.end() - 1);
63 std::string dev("/dev/" + dirname);
64 std::string propname("vendor.ser." + content);
65 property_set(propname.c_str(), dev.c_str());
66 }
67 }
68 return 0;
69 }
70