Merge "init: Let property_get return std::string."

This commit is contained in:
Yabin Cui 2015-07-24 18:16:06 +00:00 committed by Gerrit Code Review
commit addf7a964b
8 changed files with 39 additions and 71 deletions

View file

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

View file

@ -76,7 +76,7 @@ static struct action *cur_action = NULL;
static struct command *cur_command = NULL; static struct command *cur_command = NULL;
static int have_console; 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 time_t process_needs_restart;
static const char *ENV[32]; static const char *ENV[32];
@ -160,7 +160,7 @@ void zap_stdio(void)
static void open_console() static void open_console()
{ {
int fd; 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); fd = open("/dev/null", O_RDWR);
} }
ioctl(fd, TIOCSCTTY, 0); 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) static int console_init_action(int nargs, char **args)
{ {
char console[PROP_VALUE_MAX]; std::string console = property_get("ro.boot.console");
if (property_get("ro.boot.console", console) > 0) { if (!console.empty()) {
snprintf(console_name, sizeof(console_name), "/dev/%s", console); 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) if (fd >= 0)
have_console = 1; have_console = 1;
close(fd); close(fd);
@ -793,9 +793,8 @@ static void export_kernel_boot_props() {
{ "ro.boot.revision", "ro.revision", "0", }, { "ro.boot.revision", "ro.revision", "0", },
}; };
for (size_t i = 0; i < ARRAY_SIZE(prop_map); i++) { for (size_t i = 0; i < ARRAY_SIZE(prop_map); i++) {
char value[PROP_VALUE_MAX]; std::string value = property_get(prop_map[i].src_prop);
int rc = property_get(prop_map[i].src_prop, value); property_set(prop_map[i].dst_prop, (!value.empty()) ? value.c_str() : prop_map[i].default_value);
property_set(prop_map[i].dst_prop, (rc > 0) ? value : 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"); 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. // Don't mount filesystems or start core system services in charger mode.
char bootmode[PROP_VALUE_MAX]; std::string bootmode = property_get("ro.bootmode");
if (property_get("ro.bootmode", bootmode) > 0 && strcmp(bootmode, "charger") == 0) { if (bootmode == "charger") {
action_for_each_trigger("charger", action_add_queue_tail); action_for_each_trigger("charger", action_add_queue_tail);
} else { } else {
action_for_each_trigger("late-init", action_add_queue_tail); 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) { while (*src_ptr && left > 0) {
char *c; char *c;
char prop[PROP_NAME_MAX + 1]; char prop[PROP_NAME_MAX + 1];
char prop_val[PROP_VALUE_MAX];
int prop_len = 0; int prop_len = 0;
int prop_val_len;
c = strchr(src_ptr, '$'); c = strchr(src_ptr, '$');
if (!c) { if (!c) {
@ -307,14 +305,14 @@ int expand_props(char *dst, const char *src, int dst_size)
goto err; goto err;
} }
prop_val_len = property_get(prop, prop_val); std::string prop_val = property_get(prop);
if (!prop_val_len) { if (prop_val.empty()) {
ERROR("property '%s' doesn't exist while expanding '%s'\n", ERROR("property '%s' doesn't exist while expanding '%s'\n",
prop, src); prop, src);
goto err; 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) if (ret < 0)
goto err_nospace; goto err_nospace;
src_ptr = c; src_ptr = c;
@ -586,17 +584,13 @@ void queue_property_triggers(const char *name, const char *value)
} else { } else {
const char* equals = strchr(test, '='); const char* equals = strchr(test, '=');
if (equals) { if (equals) {
char prop_name[PROP_NAME_MAX + 1];
char value[PROP_VALUE_MAX];
int length = equals - test; int length = equals - test;
if (length <= PROP_NAME_MAX) { if (length <= PROP_NAME_MAX) {
int ret; std::string prop_name(test, length);
memcpy(prop_name, test, length); std::string value = property_get(prop_name.c_str());
prop_name[length] = 0;
/* does the property exist, and match the trigger value? */ /* does the property exist, and match the trigger value? */
ret = property_get(prop_name, value); if (!value.empty() && (!strcmp(equals + 1, value.c_str()) ||
if (ret > 0 && (!strcmp(equals + 1, value) ||
!strcmp(equals + 1, "*"))) { !strcmp(equals + 1, "*"))) {
continue; continue;
} }

View file

@ -64,19 +64,18 @@ void add_service_keycodes(struct service *svc)
static void handle_keychord() { static void handle_keychord() {
struct service *svc; struct service *svc;
char adb_enabled[PROP_VALUE_MAX];
int ret; int ret;
__u16 id; __u16 id;
// Only handle keychords if adb is enabled.
property_get("init.svc.adbd", adb_enabled);
ret = read(keychord_fd, &id, sizeof(id)); ret = read(keychord_fd, &id, sizeof(id));
if (ret != sizeof(id)) { if (ret != sizeof(id)) {
ERROR("could not read keychord id\n"); ERROR("could not read keychord id\n");
return; 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); svc = service_find_by_keychord(id);
if (svc) { if (svc) {
INFO("Starting service %s from keychord\n", svc->name); 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); return check_mac_perms(name, sctx);
} }
int __property_get(const char *name, char *value) std::string property_get(const char* name) {
{ char value[PROP_VALUE_MAX] = {0};
return __system_property_get(name, value); __system_property_get(name, value);
return value;
} }
static void write_persistent_property(const char *name, const char *value) static void write_persistent_property(const char *name, const char *value)
@ -491,9 +492,8 @@ bool properties_initialized() {
static void load_override_properties() { static void load_override_properties() {
if (ALLOW_LOCAL_PROP_OVERRIDE) { if (ALLOW_LOCAL_PROP_OVERRIDE) {
char debuggable[PROP_VALUE_MAX]; std::string debuggable = property_get("ro.debuggable");
int ret = property_get("ro.debuggable", debuggable); if (debuggable == "1") {
if (ret && (strcmp(debuggable, "1") == 0)) {
load_properties_from_file(PROP_PATH_LOCAL_OVERRIDE, NULL); load_properties_from_file(PROP_PATH_LOCAL_OVERRIDE, NULL);
} }
} }
@ -511,19 +511,17 @@ void load_persist_props(void) {
} }
void load_recovery_id_prop() { void load_recovery_id_prop() {
char fstab_filename[PROP_VALUE_MAX + sizeof(FSTAB_PREFIX)]; std::string ro_hardware = property_get("ro.hardware");
char propbuf[PROP_VALUE_MAX]; if (ro_hardware.empty()) {
int ret = property_get("ro.hardware", propbuf);
if (!ret) {
ERROR("ro.hardware not set - unable to load recovery id\n"); ERROR("ro.hardware not set - unable to load recovery id\n");
return; 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); fs_mgr_free_fstab);
if (!tab) { 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; return;
} }

View file

@ -19,6 +19,7 @@
#include <stddef.h> #include <stddef.h>
#include <sys/system_properties.h> #include <sys/system_properties.h>
#include <string>
extern void property_init(void); extern void property_init(void);
extern void property_load_boot_defaults(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 load_all_props(void);
extern void start_property_service(void); extern void start_property_service(void);
void get_property_workspace(int *fd, int *sz); 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 int property_set(const char *name, const char *value);
extern bool properties_initialized(); 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 */ #endif /* _INIT_PROPERTY_H */

View file

@ -59,11 +59,10 @@ int ueventd_main(int argc, char **argv)
cb.func_log = selinux_klog_callback; cb.func_log = selinux_klog_callback;
selinux_set_callback(SELINUX_CB_LOG, cb); selinux_set_callback(SELINUX_CB_LOG, cb);
char hardware[PROP_VALUE_MAX]; std::string hardware = property_get("ro.hardware");
property_get("ro.hardware", hardware);
ueventd_parse_config_file("/ueventd.rc"); 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(); device_init();