From c73497e17a1f0be9afd6903f58dea3ea5b01be08 Mon Sep 17 00:00:00 2001 From: Tom Cherry Date: Tue, 2 Jan 2018 11:50:16 -0800 Subject: [PATCH] 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 --- .../libpropertyinfoparser/Android.bp | 1 + .../libpropertyinfoserializer/Android.bp | 1 + shell_and_utilities/README.md | 16 +-- toolbox/Android.mk | 10 +- toolbox/getprop.cpp | 126 ++++++++++++++++++ 5 files changed, 145 insertions(+), 9 deletions(-) create mode 100644 toolbox/getprop.cpp 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 @@ cc_library_static { name: "libpropertyinfoparser", host_supported: true, + vendor_available: true, srcs: ["property_info_parser.cpp"], 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 @@ cc_defaults { name: "propertyinfoserializer_defaults", host_supported: true, + vendor_available: true, cpp_std: "experimental", target: { 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 one-true-awk: awk -toolbox: getevent newfs\_msdos +toolbox: getevent getprop newfs\_msdos toybox: acpi base64 basename blockdev cal cat chcon chgrp chmod chown chroot chrt cksum clear cmp comm cp cpio cut date df diff dirname dmesg dos2unix du echo env expand expr fallocate false file find flock free -getenforce getprop groups gunzip gzip head hostname hwclock id ifconfig -inotifyd insmod ionice iorenice kill killall ln load\_policy log logname -losetup ls lsmod lsof lspci lsusb md5sum microcom mkdir mkfifo mknod -mkswap mktemp modinfo modprobe more mount mountpoint mv netstat nice -nl nohup od paste patch pgrep pidof pkill pmap printenv printf ps pwd -readlink realpath renice restorecon rm rmdir rmmod runcon sed sendevent -seq setenforce setprop setsid sha1sum sha224sum sha256sum sha384sum +getenforce groups gunzip gzip head hostname hwclock id ifconfig inotifyd +insmod ionice iorenice kill killall ln load\_policy log logname losetup +ls lsmod lsof lspci lsusb md5sum microcom mkdir mkfifo mknod mkswap +mktemp modinfo modprobe more mount mountpoint mv netstat nice nl nohup +od paste patch pgrep pidof pkill pmap printenv printf ps pwd readlink +realpath renice restorecon rm rmdir rmmod runcon sed sendevent seq +setenforce setprop setsid sha1sum sha224sum sha256sum sha384sum sha512sum sleep sort split start stat stop strings swapoff swapon sync sysctl tac tail tar taskset tee time timeout top touch tr true truncate 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 := \ getevent \ newfs_msdos \ -ALL_TOOLS = $(BSD_TOOLS) $(OUR_TOOLS) +OUR_CPP_TOOLS := \ + getprop \ + +ALL_TOOLS = $(BSD_TOOLS) $(OUR_TOOLS) $(OUR_CPP_TOOLS) LOCAL_SRC_FILES := \ toolbox.c \ $(patsubst %,%.c,$(OUR_TOOLS)) \ + $(patsubst %,%.cpp,$(OUR_CPP_TOOLS)) \ LOCAL_CFLAGS += $(common_cflags) +LOCAL_CPPFLAGS += -std=gnu++1z LOCAL_C_INCLUDES += $(LOCAL_PATH)/upstream-netbsd/include/ LOCAL_SHARED_LIBRARIES := \ + libbase \ libcutils \ +LOCAL_STATIC_LIBRARIES := libpropertyinfoparser + LOCAL_WHOLE_STATIC_LIBRARIES := $(patsubst %,libtoolbox_%,$(BSD_TOOLS)) 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 @@ +// +// Copyright (C) 2017 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#include +#include + +#include +#include +#include + +#include +#include + +using android::base::GetProperty; +using android::properties::PropertyInfoAreaFile; + +PropertyInfoAreaFile property_info_file; + +void PrintAllProperties(bool print_property_context) { + std::vector> properties; + __system_property_foreach( + [](const prop_info* pi, void* cookie) { + __system_property_read_callback( + pi, + [](void* cookie, const char* name, const char* value, unsigned) { + auto properties = + reinterpret_cast>*>(cookie); + properties->emplace_back(name, value); + }, + cookie); + }, + &properties); + + std::sort(properties.begin(), properties.end()); + + if (print_property_context) { + for (auto& [name, value] : properties) { + const char* context = nullptr; + property_info_file->GetPropertyInfo(name.c_str(), &context, nullptr); + value = context; + } + } + + for (const auto& [name, value] : properties) { + std::cout << "[" << name << "]: [" << value << "]" << std::endl; + } +} + +void PrintProperty(const char* name, const char* default_value, bool print_property_context) { + if (print_property_context) { + const char* context = nullptr; + property_info_file->GetPropertyInfo(name, &context, nullptr); + std::cout << context << std::endl; + } else { + std::cout << GetProperty(name, default_value) << std::endl; + } +} + +extern "C" int getprop_main(int argc, char** argv) { + bool print_property_context = false; + + while (true) { + static const struct option long_options[] = { + {"help", no_argument, nullptr, 'h'}, + {nullptr, 0, nullptr, 0}, + }; + + int arg = getopt_long(argc, argv, "Z", long_options, nullptr); + + if (arg == -1) { + break; + } + + switch (arg) { + case 'h': + std::cout << "usage: getprop [-Z] [NAME [DEFAULT]]\n\n" + "Gets an Android system property, or lists them all.\n" + "Use -Z to return the property context instead of the property value\n" + << std::endl; + return 0; + case 'Z': + print_property_context = true; + break; + case '?': + return -1; + default: + std::cerr << "getprop: getopt returned invalid result: " << arg << std::endl; + return -1; + } + } + + if (print_property_context) { + property_info_file.LoadDefaultPath(); + if (!property_info_file) { + std::cerr << "Unable to load property info file" << std::endl; + return -1; + } + } + + if (optind >= argc) { + PrintAllProperties(print_property_context); + return 0; + } + + if (optind < argc - 2) { + std::cerr << "getprop: Max 2 arguments (see \"getprop --help\")" << std::endl; + return -1; + } + + PrintProperty(argv[optind], (optind == argc - 1) ? "" : argv[optind + 1], print_property_context); + + return 0; +}