2018-06-13 22:06:16 +02:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2018-01-02 20:50:16 +01:00
|
|
|
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <sys/system_properties.h>
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <android-base/properties.h>
|
|
|
|
#include <property_info_parser/property_info_parser.h>
|
|
|
|
|
|
|
|
using android::base::GetProperty;
|
|
|
|
using android::properties::PropertyInfoAreaFile;
|
|
|
|
|
|
|
|
PropertyInfoAreaFile property_info_file;
|
|
|
|
|
2018-01-11 17:35:49 +01:00
|
|
|
enum class ResultType {
|
|
|
|
Value,
|
|
|
|
Context,
|
|
|
|
Type,
|
|
|
|
};
|
|
|
|
|
|
|
|
void PrintAllProperties(ResultType result_type) {
|
2018-01-02 20:50:16 +01:00
|
|
|
std::vector<std::pair<std::string, std::string>> 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<std::vector<std::pair<std::string, std::string>>*>(cookie);
|
|
|
|
properties->emplace_back(name, value);
|
|
|
|
},
|
|
|
|
cookie);
|
|
|
|
},
|
|
|
|
&properties);
|
|
|
|
|
|
|
|
std::sort(properties.begin(), properties.end());
|
|
|
|
|
2018-01-11 17:35:49 +01:00
|
|
|
if (result_type != ResultType::Value) {
|
2018-01-02 20:50:16 +01:00
|
|
|
for (auto& [name, value] : properties) {
|
|
|
|
const char* context = nullptr;
|
2018-01-11 17:35:49 +01:00
|
|
|
const char* type = nullptr;
|
|
|
|
property_info_file->GetPropertyInfo(name.c_str(), &context, &type);
|
|
|
|
if (result_type == ResultType::Context) {
|
|
|
|
value = context;
|
|
|
|
} else {
|
|
|
|
value = type;
|
|
|
|
}
|
2018-01-02 20:50:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& [name, value] : properties) {
|
|
|
|
std::cout << "[" << name << "]: [" << value << "]" << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-11 17:35:49 +01:00
|
|
|
void PrintProperty(const char* name, const char* default_value, ResultType result_type) {
|
|
|
|
switch (result_type) {
|
|
|
|
case ResultType::Value:
|
|
|
|
std::cout << GetProperty(name, default_value) << std::endl;
|
|
|
|
break;
|
|
|
|
case ResultType::Context: {
|
|
|
|
const char* context = nullptr;
|
|
|
|
property_info_file->GetPropertyInfo(name, &context, nullptr);
|
|
|
|
std::cout << context << std::endl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ResultType::Type: {
|
|
|
|
const char* type = nullptr;
|
|
|
|
property_info_file->GetPropertyInfo(name, nullptr, &type);
|
|
|
|
std::cout << type << std::endl;
|
|
|
|
break;
|
|
|
|
}
|
2018-01-02 20:50:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int getprop_main(int argc, char** argv) {
|
2018-01-11 17:35:49 +01:00
|
|
|
auto result_type = ResultType::Value;
|
2018-01-02 20:50:16 +01:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
static const struct option long_options[] = {
|
|
|
|
{"help", no_argument, nullptr, 'h'},
|
|
|
|
{nullptr, 0, nullptr, 0},
|
|
|
|
};
|
|
|
|
|
2018-01-11 17:35:49 +01:00
|
|
|
int arg = getopt_long(argc, argv, "TZ", long_options, nullptr);
|
2018-01-02 20:50:16 +01:00
|
|
|
|
|
|
|
if (arg == -1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (arg) {
|
|
|
|
case 'h':
|
2018-01-11 17:35:49 +01:00
|
|
|
std::cout << "usage: getprop [-TZ] [NAME [DEFAULT]]\n"
|
|
|
|
"\n"
|
2018-01-02 20:50:16 +01:00
|
|
|
"Gets an Android system property, or lists them all.\n"
|
2018-01-11 17:35:49 +01:00
|
|
|
"\n"
|
|
|
|
"-T\tShow property types instead of values\n"
|
|
|
|
"-Z\tShow property contexts instead of values\n"
|
2018-01-02 20:50:16 +01:00
|
|
|
<< std::endl;
|
|
|
|
return 0;
|
2018-01-11 17:35:49 +01:00
|
|
|
case 'T':
|
|
|
|
if (result_type != ResultType::Value) {
|
|
|
|
std::cerr << "Only one of -T or -Z may be specified" << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
result_type = ResultType::Type;
|
|
|
|
break;
|
2018-01-02 20:50:16 +01:00
|
|
|
case 'Z':
|
2018-01-11 17:35:49 +01:00
|
|
|
if (result_type != ResultType::Value) {
|
|
|
|
std::cerr << "Only one of -T or -Z may be specified" << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
result_type = ResultType::Context;
|
2018-01-02 20:50:16 +01:00
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
return -1;
|
|
|
|
default:
|
|
|
|
std::cerr << "getprop: getopt returned invalid result: " << arg << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-11 17:35:49 +01:00
|
|
|
if (result_type != ResultType::Value) {
|
2018-01-02 20:50:16 +01:00
|
|
|
property_info_file.LoadDefaultPath();
|
|
|
|
if (!property_info_file) {
|
|
|
|
std::cerr << "Unable to load property info file" << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (optind >= argc) {
|
2018-01-11 17:35:49 +01:00
|
|
|
PrintAllProperties(result_type);
|
2018-01-02 20:50:16 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (optind < argc - 2) {
|
|
|
|
std::cerr << "getprop: Max 2 arguments (see \"getprop --help\")" << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-01-11 17:35:49 +01:00
|
|
|
PrintProperty(argv[optind], (optind == argc - 1) ? "" : argv[optind + 1], result_type);
|
2018-01-02 20:50:16 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|