diff options
author | Tom Cherry | 2018-01-02 13:50:16 -0600 |
---|---|---|
committer | Tom Cherry | 2018-01-08 14:36:44 -0600 |
commit | c73497e17a1f0be9afd6903f58dea3ea5b01be08 (patch) | |
tree | 4df113b5603ea23aa5716b0e13376d8ac8a6a6e5 | |
parent | 5a7a8f2f170d7696fa6e6619a84a96788415cadb (diff) | |
download | platform-system-core-c73497e17a1f0be9afd6903f58dea3ea5b01be08.tar.gz platform-system-core-c73497e17a1f0be9afd6903f58dea3ea5b01be08.tar.xz platform-system-core-c73497e17a1f0be9afd6903f58dea3ea5b01be08.zip |
Add getprop to toolbox
Add a non-toybox version of getprop, so that we can interface with the
new C++ PropertyInfoAreaFile class to return property context
information.
Bug: 36001741
Test: Compared toolbox getprop results with toybox getprop
Change-Id: I5f98f9e895d0620a2d9686bc0608490e7d9c3120
-rw-r--r-- | property_service/libpropertyinfoparser/Android.bp | 1 | ||||
-rw-r--r-- | property_service/libpropertyinfoserializer/Android.bp | 1 | ||||
-rw-r--r-- | shell_and_utilities/README.md | 16 | ||||
-rw-r--r-- | toolbox/Android.mk | 10 | ||||
-rw-r--r-- | toolbox/getprop.cpp | 126 |
5 files changed, 145 insertions, 9 deletions
diff --git a/property_service/libpropertyinfoparser/Android.bp b/property_service/libpropertyinfoparser/Android.bp index ffaa2b304..e0cd30c2f 100644 --- a/property_service/libpropertyinfoparser/Android.bp +++ b/property_service/libpropertyinfoparser/Android.bp | |||
@@ -1,6 +1,7 @@ | |||
1 | cc_library_static { | 1 | cc_library_static { |
2 | name: "libpropertyinfoparser", | 2 | name: "libpropertyinfoparser", |
3 | host_supported: true, | 3 | host_supported: true, |
4 | vendor_available: true, | ||
4 | srcs: ["property_info_parser.cpp"], | 5 | srcs: ["property_info_parser.cpp"], |
5 | 6 | ||
6 | cpp_std: "experimental", | 7 | cpp_std: "experimental", |
diff --git a/property_service/libpropertyinfoserializer/Android.bp b/property_service/libpropertyinfoserializer/Android.bp index 0a1593b26..5de7477bc 100644 --- a/property_service/libpropertyinfoserializer/Android.bp +++ b/property_service/libpropertyinfoserializer/Android.bp | |||
@@ -1,6 +1,7 @@ | |||
1 | cc_defaults { | 1 | cc_defaults { |
2 | name: "propertyinfoserializer_defaults", | 2 | name: "propertyinfoserializer_defaults", |
3 | host_supported: true, | 3 | host_supported: true, |
4 | vendor_available: true, | ||
4 | cpp_std: "experimental", | 5 | cpp_std: "experimental", |
5 | target: { | 6 | target: { |
6 | linux: { | 7 | linux: { |
diff --git a/shell_and_utilities/README.md b/shell_and_utilities/README.md index 206204bae..c423c6950 100644 --- a/shell_and_utilities/README.md +++ b/shell_and_utilities/README.md | |||
@@ -175,18 +175,18 @@ bzip2: bzcat bzip2 bunzip2 | |||
175 | 175 | ||
176 | one-true-awk: awk | 176 | one-true-awk: awk |
177 | 177 | ||
178 | toolbox: getevent newfs\_msdos | 178 | toolbox: getevent getprop newfs\_msdos |
179 | 179 | ||
180 | toybox: acpi base64 basename blockdev cal cat chcon chgrp chmod chown | 180 | toybox: acpi base64 basename blockdev cal cat chcon chgrp chmod chown |
181 | chroot chrt cksum clear cmp comm cp cpio cut date df diff dirname dmesg | 181 | chroot chrt cksum clear cmp comm cp cpio cut date df diff dirname dmesg |
182 | dos2unix du echo env expand expr fallocate false file find flock free | 182 | dos2unix du echo env expand expr fallocate false file find flock free |
183 | getenforce getprop groups gunzip gzip head hostname hwclock id ifconfig | 183 | getenforce groups gunzip gzip head hostname hwclock id ifconfig inotifyd |
184 | inotifyd insmod ionice iorenice kill killall ln load\_policy log logname | 184 | insmod ionice iorenice kill killall ln load\_policy log logname losetup |
185 | losetup ls lsmod lsof lspci lsusb md5sum microcom mkdir mkfifo mknod | 185 | ls lsmod lsof lspci lsusb md5sum microcom mkdir mkfifo mknod mkswap |
186 | mkswap mktemp modinfo modprobe more mount mountpoint mv netstat nice | 186 | mktemp modinfo modprobe more mount mountpoint mv netstat nice nl nohup |
187 | nl nohup od paste patch pgrep pidof pkill pmap printenv printf ps pwd | 187 | od paste patch pgrep pidof pkill pmap printenv printf ps pwd readlink |
188 | readlink realpath renice restorecon rm rmdir rmmod runcon sed sendevent | 188 | realpath renice restorecon rm rmdir rmmod runcon sed sendevent seq |
189 | seq setenforce setprop setsid sha1sum sha224sum sha256sum sha384sum | 189 | setenforce setprop setsid sha1sum sha224sum sha256sum sha384sum |
190 | sha512sum sleep sort split start stat stop strings swapoff swapon sync | 190 | sha512sum sleep sort split start stat stop strings swapoff swapon sync |
191 | sysctl tac tail tar taskset tee time timeout top touch tr true truncate | 191 | sysctl tac tail tar taskset tee time timeout top touch tr true truncate |
192 | tty ulimit umount uname uniq unix2dos uptime usleep uudecode uuencode | 192 | tty ulimit umount uname uniq unix2dos uptime usleep uudecode uuencode |
diff --git a/toolbox/Android.mk b/toolbox/Android.mk index c4795a73a..d1b6114b0 100644 --- a/toolbox/Android.mk +++ b/toolbox/Android.mk | |||
@@ -13,18 +13,26 @@ OUR_TOOLS := \ | |||
13 | getevent \ | 13 | getevent \ |
14 | newfs_msdos \ | 14 | newfs_msdos \ |
15 | 15 | ||
16 | ALL_TOOLS = $(BSD_TOOLS) $(OUR_TOOLS) | 16 | OUR_CPP_TOOLS := \ |
17 | getprop \ | ||
18 | |||
19 | ALL_TOOLS = $(BSD_TOOLS) $(OUR_TOOLS) $(OUR_CPP_TOOLS) | ||
17 | 20 | ||
18 | LOCAL_SRC_FILES := \ | 21 | LOCAL_SRC_FILES := \ |
19 | toolbox.c \ | 22 | toolbox.c \ |
20 | $(patsubst %,%.c,$(OUR_TOOLS)) \ | 23 | $(patsubst %,%.c,$(OUR_TOOLS)) \ |
24 | $(patsubst %,%.cpp,$(OUR_CPP_TOOLS)) \ | ||
21 | 25 | ||
22 | LOCAL_CFLAGS += $(common_cflags) | 26 | LOCAL_CFLAGS += $(common_cflags) |
27 | LOCAL_CPPFLAGS += -std=gnu++1z | ||
23 | LOCAL_C_INCLUDES += $(LOCAL_PATH)/upstream-netbsd/include/ | 28 | LOCAL_C_INCLUDES += $(LOCAL_PATH)/upstream-netbsd/include/ |
24 | 29 | ||
25 | LOCAL_SHARED_LIBRARIES := \ | 30 | LOCAL_SHARED_LIBRARIES := \ |
31 | libbase \ | ||
26 | libcutils \ | 32 | libcutils \ |
27 | 33 | ||
34 | LOCAL_STATIC_LIBRARIES := libpropertyinfoparser | ||
35 | |||
28 | LOCAL_WHOLE_STATIC_LIBRARIES := $(patsubst %,libtoolbox_%,$(BSD_TOOLS)) | 36 | LOCAL_WHOLE_STATIC_LIBRARIES := $(patsubst %,libtoolbox_%,$(BSD_TOOLS)) |
29 | 37 | ||
30 | LOCAL_MODULE := toolbox | 38 | LOCAL_MODULE := toolbox |
diff --git a/toolbox/getprop.cpp b/toolbox/getprop.cpp new file mode 100644 index 000000000..7818ff222 --- /dev/null +++ b/toolbox/getprop.cpp | |||
@@ -0,0 +1,126 @@ | |||
1 | // | ||
2 | // Copyright (C) 2017 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 <getopt.h> | ||
18 | #include <sys/system_properties.h> | ||
19 | |||
20 | #include <iostream> | ||
21 | #include <string> | ||
22 | #include <vector> | ||
23 | |||
24 | #include <android-base/properties.h> | ||
25 | #include <property_info_parser/property_info_parser.h> | ||
26 | |||
27 | using android::base::GetProperty; | ||
28 | using android::properties::PropertyInfoAreaFile; | ||
29 | |||
30 | PropertyInfoAreaFile property_info_file; | ||
31 | |||
32 | void PrintAllProperties(bool print_property_context) { | ||
33 | std::vector<std::pair<std::string, std::string>> properties; | ||
34 | __system_property_foreach( | ||
35 | [](const prop_info* pi, void* cookie) { | ||
36 | __system_property_read_callback( | ||
37 | pi, | ||
38 | [](void* cookie, const char* name, const char* value, unsigned) { | ||
39 | auto properties = | ||
40 | reinterpret_cast<std::vector<std::pair<std::string, std::string>>*>(cookie); | ||
41 | properties->emplace_back(name, value); | ||
42 | }, | ||
43 | cookie); | ||
44 | }, | ||
45 | &properties); | ||
46 | |||
47 | std::sort(properties.begin(), properties.end()); | ||
48 | |||
49 | if (print_property_context) { | ||
50 | for (auto& [name, value] : properties) { | ||
51 | const char* context = nullptr; | ||
52 | property_info_file->GetPropertyInfo(name.c_str(), &context, nullptr); | ||
53 | value = context; | ||
54 | } | ||
55 | } | ||
56 | |||
57 | for (const auto& [name, value] : properties) { | ||
58 | std::cout << "[" << name << "]: [" << value << "]" << std::endl; | ||
59 | } | ||
60 | } | ||
61 | |||
62 | void PrintProperty(const char* name, const char* default_value, bool print_property_context) { | ||
63 | if (print_property_context) { | ||
64 | const char* context = nullptr; | ||
65 | property_info_file->GetPropertyInfo(name, &context, nullptr); | ||
66 | std::cout << context << std::endl; | ||
67 | } else { | ||
68 | std::cout << GetProperty(name, default_value) << std::endl; | ||
69 | } | ||
70 | } | ||
71 | |||
72 | extern "C" int getprop_main(int argc, char** argv) { | ||
73 | bool print_property_context = false; | ||
74 | |||
75 | while (true) { | ||
76 | static const struct option long_options[] = { | ||
77 | {"help", no_argument, nullptr, 'h'}, | ||
78 | {nullptr, 0, nullptr, 0}, | ||
79 | }; | ||
80 | |||
81 | int arg = getopt_long(argc, argv, "Z", long_options, nullptr); | ||
82 | |||
83 | if (arg == -1) { | ||
84 | break; | ||
85 | } | ||
86 | |||
87 | switch (arg) { | ||
88 | case 'h': | ||
89 | std::cout << "usage: getprop [-Z] [NAME [DEFAULT]]\n\n" | ||
90 | "Gets an Android system property, or lists them all.\n" | ||
91 | "Use -Z to return the property context instead of the property value\n" | ||
92 | << std::endl; | ||
93 | return 0; | ||
94 | case 'Z': | ||
95 | print_property_context = true; | ||
96 | break; | ||
97 | case '?': | ||
98 | return -1; | ||
99 | default: | ||
100 | std::cerr << "getprop: getopt returned invalid result: " << arg << std::endl; | ||
101 | return -1; | ||
102 | } | ||
103 | } | ||
104 | |||
105 | if (print_property_context) { | ||
106 | property_info_file.LoadDefaultPath(); | ||
107 | if (!property_info_file) { | ||
108 | std::cerr << "Unable to load property info file" << std::endl; | ||
109 | return -1; | ||
110 | } | ||
111 | } | ||
112 | |||
113 | if (optind >= argc) { | ||
114 | PrintAllProperties(print_property_context); | ||
115 | return 0; | ||
116 | } | ||
117 | |||
118 | if (optind < argc - 2) { | ||
119 | std::cerr << "getprop: Max 2 arguments (see \"getprop --help\")" << std::endl; | ||
120 | return -1; | ||
121 | } | ||
122 | |||
123 | PrintProperty(argv[optind], (optind == argc - 1) ? "" : argv[optind + 1], print_property_context); | ||
124 | |||
125 | return 0; | ||
126 | } | ||