init: Let property_get return std::string.

Bug: 22654233

Change-Id: Id6091f58432f75e966b9871256049fbe17766c10
This commit is contained in:
Yabin Cui 2015-07-24 10:11:05 -07:00
parent 89cc750e9b
commit 74edcea90e
8 changed files with 39 additions and 71 deletions

View File

@ -77,8 +77,8 @@ static void log_header() {
return;
}
char fingerprint[PROP_VALUE_MAX];
if (property_get("ro.build.fingerprint", fingerprint) == -1) {
std::string fingerprint = property_get("ro.build.fingerprint");
if (fingerprint.empty()) {
return;
}
@ -92,7 +92,7 @@ static void log_header() {
fprintf(out, "version = Android init 0.8 " __TIME__ "\n");
fprintf(out, "title = Boot chart for Android (%s)\n", date);
fprintf(out, "system.uname = %s %s %s %s\n", uts.sysname, uts.release, uts.version, uts.machine);
fprintf(out, "system.release = %s\n", fingerprint);
fprintf(out, "system.release = %s\n", fingerprint.c_str());
// TODO: use /proc/cpuinfo "model name" line for x86, "Processor" line for arm.
fprintf(out, "system.cpu = %s\n", uts.machine);
fprintf(out, "system.kernel.options = %s\n", kernel_cmdline.c_str());

View File

@ -920,9 +920,8 @@ int do_installkey(int nargs, char **args)
return -1;
}
char prop_value[PROP_VALUE_MAX] = {0};
property_get("ro.crypto.type", prop_value);
if (strcmp(prop_value, "file")) {
std::string prop_value = property_get("ro.crypto.type");
if (prop_value != "file") {
return 0;
}

View File

@ -76,7 +76,7 @@ static struct action *cur_action = NULL;
static struct command *cur_command = NULL;
static int have_console;
static char console_name[PROP_VALUE_MAX] = "/dev/console";
static std::string console_name = "/dev/console";
static time_t process_needs_restart;
static const char *ENV[32];
@ -160,7 +160,7 @@ void zap_stdio(void)
static void open_console()
{
int fd;
if ((fd = open(console_name, O_RDWR)) < 0) {
if ((fd = open(console_name.c_str(), O_RDWR)) < 0) {
fd = open("/dev/null", O_RDWR);
}
ioctl(fd, TIOCSCTTY, 0);
@ -727,12 +727,12 @@ static int keychord_init_action(int nargs, char **args)
static int console_init_action(int nargs, char **args)
{
char console[PROP_VALUE_MAX];
if (property_get("ro.boot.console", console) > 0) {
snprintf(console_name, sizeof(console_name), "/dev/%s", console);
std::string console = property_get("ro.boot.console");
if (!console.empty()) {
console_name = "/dev/" + console;
}
int fd = open(console_name, O_RDWR | O_CLOEXEC);
int fd = open(console_name.c_str(), O_RDWR | O_CLOEXEC);
if (fd >= 0)
have_console = 1;
close(fd);
@ -793,9 +793,8 @@ static void export_kernel_boot_props() {
{ "ro.boot.revision", "ro.revision", "0", },
};
for (size_t i = 0; i < ARRAY_SIZE(prop_map); i++) {
char value[PROP_VALUE_MAX];
int rc = property_get(prop_map[i].src_prop, value);
property_set(prop_map[i].dst_prop, (rc > 0) ? value : prop_map[i].default_value);
std::string value = property_get(prop_map[i].src_prop);
property_set(prop_map[i].dst_prop, (!value.empty()) ? value.c_str() : prop_map[i].default_value);
}
}
@ -1054,8 +1053,8 @@ int main(int argc, char** argv) {
queue_builtin_action(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");
// Don't mount filesystems or start core system services in charger mode.
char bootmode[PROP_VALUE_MAX];
if (property_get("ro.bootmode", bootmode) > 0 && strcmp(bootmode, "charger") == 0) {
std::string bootmode = property_get("ro.bootmode");
if (bootmode == "charger") {
action_for_each_trigger("charger", action_add_queue_tail);
} else {
action_for_each_trigger("late-init", action_add_queue_tail);

View File

@ -247,9 +247,7 @@ int expand_props(char *dst, const char *src, int dst_size)
while (*src_ptr && left > 0) {
char *c;
char prop[PROP_NAME_MAX + 1];
char prop_val[PROP_VALUE_MAX];
int prop_len = 0;
int prop_val_len;
c = strchr(src_ptr, '$');
if (!c) {
@ -307,14 +305,14 @@ int expand_props(char *dst, const char *src, int dst_size)
goto err;
}
prop_val_len = property_get(prop, prop_val);
if (!prop_val_len) {
std::string prop_val = property_get(prop);
if (prop_val.empty()) {
ERROR("property '%s' doesn't exist while expanding '%s'\n",
prop, src);
goto err;
}
ret = push_chars(&dst_ptr, &left, prop_val, prop_val_len);
ret = push_chars(&dst_ptr, &left, prop_val.c_str(), prop_val.size());
if (ret < 0)
goto err_nospace;
src_ptr = c;
@ -586,17 +584,13 @@ void queue_property_triggers(const char *name, const char *value)
} else {
const char* equals = strchr(test, '=');
if (equals) {
char prop_name[PROP_NAME_MAX + 1];
char value[PROP_VALUE_MAX];
int length = equals - test;
if (length <= PROP_NAME_MAX) {
int ret;
memcpy(prop_name, test, length);
prop_name[length] = 0;
std::string prop_name(test, length);
std::string value = property_get(prop_name.c_str());
/* does the property exist, and match the trigger value? */
ret = property_get(prop_name, value);
if (ret > 0 && (!strcmp(equals + 1, value) ||
if (!value.empty() && (!strcmp(equals + 1, value.c_str()) ||
!strcmp(equals + 1, "*"))) {
continue;
}

View File

@ -64,19 +64,18 @@ void add_service_keycodes(struct service *svc)
static void handle_keychord() {
struct service *svc;
char adb_enabled[PROP_VALUE_MAX];
int ret;
__u16 id;
// Only handle keychords if adb is enabled.
property_get("init.svc.adbd", adb_enabled);
ret = read(keychord_fd, &id, sizeof(id));
if (ret != sizeof(id)) {
ERROR("could not read keychord id\n");
return;
}
if (!strcmp(adb_enabled, "running")) {
// Only handle keychords if adb is enabled.
std::string adb_enabled = property_get("init.svc.adbd");
if (adb_enabled == "running") {
svc = service_find_by_keychord(id);
if (svc) {
INFO("Starting service %s from keychord\n", svc->name);

View File

@ -141,9 +141,10 @@ static int check_perms(const char *name, char *sctx)
return check_mac_perms(name, sctx);
}
int __property_get(const char *name, char *value)
{
return __system_property_get(name, value);
std::string property_get(const char* name) {
char value[PROP_VALUE_MAX] = {0};
__system_property_get(name, value);
return value;
}
static void write_persistent_property(const char *name, const char *value)
@ -491,9 +492,8 @@ bool properties_initialized() {
static void load_override_properties() {
if (ALLOW_LOCAL_PROP_OVERRIDE) {
char debuggable[PROP_VALUE_MAX];
int ret = property_get("ro.debuggable", debuggable);
if (ret && (strcmp(debuggable, "1") == 0)) {
std::string debuggable = property_get("ro.debuggable");
if (debuggable == "1") {
load_properties_from_file(PROP_PATH_LOCAL_OVERRIDE, NULL);
}
}
@ -511,19 +511,17 @@ void load_persist_props(void) {
}
void load_recovery_id_prop() {
char fstab_filename[PROP_VALUE_MAX + sizeof(FSTAB_PREFIX)];
char propbuf[PROP_VALUE_MAX];
int ret = property_get("ro.hardware", propbuf);
if (!ret) {
std::string ro_hardware = property_get("ro.hardware");
if (ro_hardware.empty()) {
ERROR("ro.hardware not set - unable to load recovery id\n");
return;
}
snprintf(fstab_filename, sizeof(fstab_filename), FSTAB_PREFIX "%s", propbuf);
std::string fstab_filename = FSTAB_PREFIX + ro_hardware;
std::unique_ptr<fstab, void(*)(fstab*)> tab(fs_mgr_read_fstab(fstab_filename),
std::unique_ptr<fstab, void(*)(fstab*)> tab(fs_mgr_read_fstab(fstab_filename.c_str()),
fs_mgr_free_fstab);
if (!tab) {
ERROR("unable to read fstab %s: %s\n", fstab_filename, strerror(errno));
ERROR("unable to read fstab %s: %s\n", fstab_filename.c_str(), strerror(errno));
return;
}

View File

@ -19,6 +19,7 @@
#include <stddef.h>
#include <sys/system_properties.h>
#include <string>
extern void property_init(void);
extern void property_load_boot_defaults(void);
@ -26,30 +27,9 @@ extern void load_persist_props(void);
extern void load_all_props(void);
extern void start_property_service(void);
void get_property_workspace(int *fd, int *sz);
extern int __property_get(const char *name, char *value);
std::string property_get(const char* name);
extern int property_set(const char *name, const char *value);
extern bool properties_initialized();
#ifndef __clang__
extern void __property_get_size_error()
__attribute__((__error__("property_get called with too small buffer")));
#else
extern void __property_get_size_error();
#endif
static inline
__attribute__ ((always_inline))
__attribute__ ((gnu_inline))
#ifndef __clang__
__attribute__ ((artificial))
#endif
int property_get(const char *name, char *value)
{
size_t value_len = __builtin_object_size(value, 0);
if (value_len != PROP_VALUE_MAX)
__property_get_size_error();
return __property_get(name, value);
}
#endif /* _INIT_PROPERTY_H */

View File

@ -59,11 +59,10 @@ int ueventd_main(int argc, char **argv)
cb.func_log = selinux_klog_callback;
selinux_set_callback(SELINUX_CB_LOG, cb);
char hardware[PROP_VALUE_MAX];
property_get("ro.hardware", hardware);
std::string hardware = property_get("ro.hardware");
ueventd_parse_config_file("/ueventd.rc");
ueventd_parse_config_file(android::base::StringPrintf("/ueventd.%s.rc", hardware).c_str());
ueventd_parse_config_file(android::base::StringPrintf("/ueventd.%s.rc", hardware.c_str()).c_str());
device_init();