2009-03-04 11:32:55 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 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 <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <errno.h>
|
2014-02-01 06:37:07 +08:00
|
|
|
#include <sys/poll.h>
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2015-03-10 23:39:45 +08:00
|
|
|
#include <memory>
|
|
|
|
|
2009-03-04 11:32:55 +08:00
|
|
|
#include <cutils/misc.h>
|
|
|
|
#include <cutils/sockets.h>
|
2013-05-21 05:22:01 +08:00
|
|
|
#include <cutils/multiuser.h>
|
2009-03-04 11:32:55 +08:00
|
|
|
|
|
|
|
#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
|
|
|
|
#include <sys/_system_properties.h>
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <sys/select.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <private/android_filesystem_config.h>
|
|
|
|
|
2012-08-09 22:05:49 +08:00
|
|
|
#include <selinux/selinux.h>
|
|
|
|
#include <selinux/label.h>
|
|
|
|
|
2015-05-08 23:30:33 +08:00
|
|
|
#include <fs_mgr.h>
|
2015-12-05 14:00:26 +08:00
|
|
|
#include <android-base/file.h>
|
2015-05-08 23:30:33 +08:00
|
|
|
#include "bootimg.h"
|
|
|
|
|
2009-03-04 11:32:55 +08:00
|
|
|
#include "property_service.h"
|
|
|
|
#include "init.h"
|
2010-04-14 11:35:46 +08:00
|
|
|
#include "util.h"
|
2010-04-20 08:05:34 +08:00
|
|
|
#include "log.h"
|
2009-03-04 11:32:55 +08:00
|
|
|
|
|
|
|
#define PERSISTENT_PROPERTY_DIR "/data/property"
|
2015-05-08 23:30:33 +08:00
|
|
|
#define FSTAB_PREFIX "/fstab."
|
|
|
|
#define RECOVERY_MOUNT_POINT "/recovery"
|
2009-03-04 11:32:55 +08:00
|
|
|
|
|
|
|
static int persistent_properties_loaded = 0;
|
|
|
|
|
2010-04-14 10:33:37 +08:00
|
|
|
static int property_set_fd = -1;
|
|
|
|
|
2015-03-28 14:20:44 +08:00
|
|
|
void property_init() {
|
|
|
|
if (__system_property_area_init()) {
|
2015-12-02 09:16:53 +08:00
|
|
|
ERROR("Failed to initialize property area\n");
|
|
|
|
exit(1);
|
2015-03-28 14:20:44 +08:00
|
|
|
}
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
2015-10-02 07:03:47 +08:00
|
|
|
static int check_mac_perms(const char *name, char *sctx, struct ucred *cr)
|
2012-08-09 22:05:49 +08:00
|
|
|
{
|
|
|
|
char *tctx = NULL;
|
|
|
|
int result = 0;
|
2015-10-02 07:03:47 +08:00
|
|
|
property_audit_data audit_data;
|
2012-08-09 22:05:49 +08:00
|
|
|
|
|
|
|
if (!sctx)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (!sehandle_prop)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (selabel_lookup(sehandle_prop, &tctx, name, 1) != 0)
|
|
|
|
goto err;
|
|
|
|
|
2015-10-02 07:03:47 +08:00
|
|
|
audit_data.name = name;
|
|
|
|
audit_data.cr = cr;
|
|
|
|
|
|
|
|
if (selinux_check_access(sctx, tctx, "property_service", "set", reinterpret_cast<void*>(&audit_data)) == 0)
|
2012-08-09 22:05:49 +08:00
|
|
|
result = 1;
|
|
|
|
|
|
|
|
freecon(tctx);
|
|
|
|
err:
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-10-02 07:03:47 +08:00
|
|
|
static int check_control_mac_perms(const char *name, char *sctx, struct ucred *cr)
|
2012-08-09 22:05:49 +08:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Create a name prefix out of ctl.<service name>
|
|
|
|
* The new prefix allows the use of the existing
|
|
|
|
* property service backend labeling while avoiding
|
|
|
|
* mislabels based on true property prefixes.
|
|
|
|
*/
|
|
|
|
char ctl_name[PROP_VALUE_MAX+4];
|
|
|
|
int ret = snprintf(ctl_name, sizeof(ctl_name), "ctl.%s", name);
|
|
|
|
|
|
|
|
if (ret < 0 || (size_t) ret >= sizeof(ctl_name))
|
|
|
|
return 0;
|
|
|
|
|
2015-10-02 07:03:47 +08:00
|
|
|
return check_mac_perms(ctl_name, sctx, cr);
|
2012-08-09 22:05:49 +08:00
|
|
|
}
|
|
|
|
|
2009-03-04 11:32:55 +08:00
|
|
|
/*
|
|
|
|
* Checks permissions for setting system properties.
|
|
|
|
* Returns 1 if uid allowed, 0 otherwise.
|
|
|
|
*/
|
2015-10-02 07:03:47 +08:00
|
|
|
static int check_perms(const char *name, char *sctx, struct ucred *cr)
|
2009-03-04 11:32:55 +08:00
|
|
|
{
|
|
|
|
if(!strncmp(name, "ro.", 3))
|
|
|
|
name +=3;
|
|
|
|
|
2015-10-02 07:03:47 +08:00
|
|
|
return check_mac_perms(name, sctx, cr);
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
2015-07-25 01:11:05 +08:00
|
|
|
std::string property_get(const char* name) {
|
|
|
|
char value[PROP_VALUE_MAX] = {0};
|
|
|
|
__system_property_get(name, value);
|
|
|
|
return value;
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
2009-08-26 10:13:20 +08:00
|
|
|
static void write_persistent_property(const char *name, const char *value)
|
2009-03-04 11:32:55 +08:00
|
|
|
{
|
2012-10-03 05:59:59 +08:00
|
|
|
char tempPath[PATH_MAX];
|
2009-03-04 11:32:55 +08:00
|
|
|
char path[PATH_MAX];
|
2012-10-03 05:59:59 +08:00
|
|
|
int fd;
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2012-10-03 05:59:59 +08:00
|
|
|
snprintf(tempPath, sizeof(tempPath), "%s/.temp.XXXXXX", PERSISTENT_PROPERTY_DIR);
|
|
|
|
fd = mkstemp(tempPath);
|
2009-03-04 11:32:55 +08:00
|
|
|
if (fd < 0) {
|
2015-03-21 08:05:56 +08:00
|
|
|
ERROR("Unable to write persistent property to temp file %s: %s\n", tempPath, strerror(errno));
|
2009-08-26 10:13:20 +08:00
|
|
|
return;
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
write(fd, value, strlen(value));
|
2014-03-28 19:12:47 +08:00
|
|
|
fsync(fd);
|
2009-03-04 11:32:55 +08:00
|
|
|
close(fd);
|
|
|
|
|
2012-10-03 05:59:59 +08:00
|
|
|
snprintf(path, sizeof(path), "%s/%s", PERSISTENT_PROPERTY_DIR, name);
|
2009-03-04 11:32:55 +08:00
|
|
|
if (rename(tempPath, path)) {
|
|
|
|
unlink(tempPath);
|
|
|
|
ERROR("Unable to rename persistent property file %s to %s\n", tempPath, path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-14 08:21:28 +08:00
|
|
|
static bool is_legal_property_name(const char* name, size_t namelen)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
if (namelen >= PROP_NAME_MAX) return false;
|
|
|
|
if (namelen < 1) return false;
|
|
|
|
if (name[0] == '.') return false;
|
|
|
|
if (name[namelen - 1] == '.') return false;
|
|
|
|
|
|
|
|
/* Only allow alphanumeric, plus '.', '-', or '_' */
|
|
|
|
/* Don't allow ".." to appear in a property name */
|
|
|
|
for (i = 0; i < namelen; i++) {
|
|
|
|
if (name[i] == '.') {
|
2013-09-17 02:30:48 +08:00
|
|
|
// i=0 is guaranteed to never have a dot. See above.
|
|
|
|
if (name[i-1] == '.') return false;
|
2013-09-14 08:21:28 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (name[i] == '_' || name[i] == '-') continue;
|
|
|
|
if (name[i] >= 'a' && name[i] <= 'z') continue;
|
|
|
|
if (name[i] >= 'A' && name[i] <= 'Z') continue;
|
|
|
|
if (name[i] >= '0' && name[i] <= '9') continue;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-21 00:45:18 +08:00
|
|
|
static int property_set_impl(const char* name, const char* value) {
|
2012-10-03 05:59:59 +08:00
|
|
|
size_t namelen = strlen(name);
|
|
|
|
size_t valuelen = strlen(value);
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2013-09-14 08:21:28 +08:00
|
|
|
if (!is_legal_property_name(name, namelen)) return -1;
|
|
|
|
if (valuelen >= PROP_VALUE_MAX) return -1;
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2015-06-10 02:02:55 +08:00
|
|
|
if (strcmp("selinux.reload_policy", name) == 0 && strcmp("1", value) == 0) {
|
|
|
|
if (selinux_reload_policy() != 0) {
|
|
|
|
ERROR("Failed to reload policy\n");
|
|
|
|
}
|
|
|
|
} else if (strcmp("selinux.restorecon_recursive", name) == 0 && valuelen > 0) {
|
|
|
|
if (restorecon_recursive(value) != 0) {
|
|
|
|
ERROR("Failed to restorecon_recursive %s\n", value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-21 00:45:18 +08:00
|
|
|
prop_info* pi = (prop_info*) __system_property_find(name);
|
2009-03-04 11:32:55 +08:00
|
|
|
|
|
|
|
if(pi != 0) {
|
|
|
|
/* ro.* properties may NEVER be modified once set */
|
|
|
|
if(!strncmp(name, "ro.", 3)) return -1;
|
|
|
|
|
2013-01-24 15:09:48 +08:00
|
|
|
__system_property_update(pi, value, valuelen);
|
2009-03-04 11:32:55 +08:00
|
|
|
} else {
|
2015-03-21 00:45:18 +08:00
|
|
|
int rc = __system_property_add(name, namelen, value, valuelen);
|
|
|
|
if (rc < 0) {
|
|
|
|
return rc;
|
2013-04-29 15:11:57 +08:00
|
|
|
}
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
/* If name starts with "net." treat as a DNS property. */
|
2009-05-09 02:27:42 +08:00
|
|
|
if (strncmp("net.", name, strlen("net.")) == 0) {
|
2009-03-04 11:32:55 +08:00
|
|
|
if (strcmp("net.change", name) == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2009-08-26 10:13:20 +08:00
|
|
|
/*
|
2009-03-04 11:32:55 +08:00
|
|
|
* The 'net.change' property is a special property used track when any
|
|
|
|
* 'net.*' property name is updated. It is _ONLY_ updated here. Its value
|
|
|
|
* contains the last updated 'net.*' property.
|
|
|
|
*/
|
|
|
|
property_set("net.change", name);
|
|
|
|
} else if (persistent_properties_loaded &&
|
2009-05-09 02:27:42 +08:00
|
|
|
strncmp("persist.", name, strlen("persist.")) == 0) {
|
2009-08-26 10:13:20 +08:00
|
|
|
/*
|
2009-03-04 11:32:55 +08:00
|
|
|
* Don't write properties to disk until after we have read all default properties
|
|
|
|
* to prevent them from being overwritten by default values.
|
|
|
|
*/
|
2009-08-26 10:13:20 +08:00
|
|
|
write_persistent_property(name, value);
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
property_changed(name, value);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-21 00:45:18 +08:00
|
|
|
int property_set(const char* name, const char* value) {
|
|
|
|
int rc = property_set_impl(name, value);
|
|
|
|
if (rc == -1) {
|
2015-03-23 23:07:19 +08:00
|
|
|
ERROR("property_set(\"%s\", \"%s\") failed\n", name, value);
|
2015-03-21 00:45:18 +08:00
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2015-04-25 12:13:44 +08:00
|
|
|
static void handle_property_set_fd()
|
2009-03-04 11:32:55 +08:00
|
|
|
{
|
|
|
|
prop_msg msg;
|
|
|
|
int s;
|
|
|
|
int r;
|
|
|
|
struct ucred cr;
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
socklen_t addr_size = sizeof(addr);
|
|
|
|
socklen_t cr_size = sizeof(cr);
|
2012-08-09 22:05:49 +08:00
|
|
|
char * source_ctx = NULL;
|
2014-02-01 06:37:07 +08:00
|
|
|
struct pollfd ufds[1];
|
|
|
|
const int timeout_ms = 2 * 1000; /* Default 2 sec timeout for caller to send property. */
|
|
|
|
int nr;
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2010-04-14 10:33:37 +08:00
|
|
|
if ((s = accept(property_set_fd, (struct sockaddr *) &addr, &addr_size)) < 0) {
|
2009-03-04 11:32:55 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check socket options here */
|
|
|
|
if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &cr, &cr_size) < 0) {
|
|
|
|
close(s);
|
2012-10-03 05:59:59 +08:00
|
|
|
ERROR("Unable to receive socket options\n");
|
2009-03-04 11:32:55 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-01 06:37:07 +08:00
|
|
|
ufds[0].fd = s;
|
|
|
|
ufds[0].events = POLLIN;
|
|
|
|
ufds[0].revents = 0;
|
|
|
|
nr = TEMP_FAILURE_RETRY(poll(ufds, 1, timeout_ms));
|
|
|
|
if (nr == 0) {
|
|
|
|
ERROR("sys_prop: timeout waiting for uid=%d to send property message.\n", cr.uid);
|
|
|
|
close(s);
|
|
|
|
return;
|
|
|
|
} else if (nr < 0) {
|
2015-03-21 08:05:56 +08:00
|
|
|
ERROR("sys_prop: error waiting for uid=%d to send property message: %s\n", cr.uid, strerror(errno));
|
2014-02-01 06:37:07 +08:00
|
|
|
close(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = TEMP_FAILURE_RETRY(recv(s, &msg, sizeof(msg), MSG_DONTWAIT));
|
2009-03-04 11:32:55 +08:00
|
|
|
if(r != sizeof(prop_msg)) {
|
2015-03-21 08:05:56 +08:00
|
|
|
ERROR("sys_prop: mis-match msg size received: %d expected: %zu: %s\n",
|
|
|
|
r, sizeof(prop_msg), strerror(errno));
|
2011-03-31 03:39:56 +08:00
|
|
|
close(s);
|
2009-03-04 11:32:55 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(msg.cmd) {
|
|
|
|
case PROP_MSG_SETPROP:
|
|
|
|
msg.name[PROP_NAME_MAX-1] = 0;
|
|
|
|
msg.value[PROP_VALUE_MAX-1] = 0;
|
|
|
|
|
2013-09-14 08:21:28 +08:00
|
|
|
if (!is_legal_property_name(msg.name, strlen(msg.name))) {
|
|
|
|
ERROR("sys_prop: illegal property name. Got: \"%s\"\n", msg.name);
|
|
|
|
close(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-09 22:05:49 +08:00
|
|
|
getpeercon(s, &source_ctx);
|
|
|
|
|
2009-03-04 11:32:55 +08:00
|
|
|
if(memcmp(msg.name,"ctl.",4) == 0) {
|
2011-04-01 23:24:13 +08:00
|
|
|
// Keep the old close-socket-early behavior when handling
|
|
|
|
// ctl.* properties.
|
|
|
|
close(s);
|
2015-10-02 07:03:47 +08:00
|
|
|
if (check_control_mac_perms(msg.value, source_ctx, &cr)) {
|
2009-03-04 11:32:55 +08:00
|
|
|
handle_control_message((char*) msg.name + 4, (char*) msg.value);
|
|
|
|
} else {
|
2010-10-04 04:30:11 +08:00
|
|
|
ERROR("sys_prop: Unable to %s service ctl [%s] uid:%d gid:%d pid:%d\n",
|
|
|
|
msg.name + 4, msg.value, cr.uid, cr.gid, cr.pid);
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
} else {
|
2015-10-02 07:03:47 +08:00
|
|
|
if (check_perms(msg.name, source_ctx, &cr)) {
|
2009-03-04 11:32:55 +08:00
|
|
|
property_set((char*) msg.name, (char*) msg.value);
|
|
|
|
} else {
|
|
|
|
ERROR("sys_prop: permission denied uid:%d name:%s\n",
|
|
|
|
cr.uid, msg.name);
|
|
|
|
}
|
2011-04-01 23:24:13 +08:00
|
|
|
|
|
|
|
// Note: bionic's property client code assumes that the
|
|
|
|
// property server will not close the socket until *AFTER*
|
|
|
|
// the property is written to memory.
|
|
|
|
close(s);
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
2012-08-09 22:05:49 +08:00
|
|
|
freecon(source_ctx);
|
2009-03-04 11:32:55 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2011-04-01 23:24:13 +08:00
|
|
|
close(s);
|
2009-03-04 11:32:55 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-07 06:26:36 +08:00
|
|
|
static void load_properties_from_file(const char *, const char *);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Filter is used to decide which properties to load: NULL loads all keys,
|
|
|
|
* "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match.
|
|
|
|
*/
|
|
|
|
static void load_properties(char *data, const char *filter)
|
2009-03-04 11:32:55 +08:00
|
|
|
{
|
2014-03-07 06:26:36 +08:00
|
|
|
char *key, *value, *eol, *sol, *tmp, *fn;
|
|
|
|
size_t flen = 0;
|
|
|
|
|
|
|
|
if (filter) {
|
|
|
|
flen = strlen(filter);
|
|
|
|
}
|
2009-03-04 11:32:55 +08:00
|
|
|
|
|
|
|
sol = data;
|
2014-03-07 06:26:36 +08:00
|
|
|
while ((eol = strchr(sol, '\n'))) {
|
2009-03-04 11:32:55 +08:00
|
|
|
key = sol;
|
|
|
|
*eol++ = 0;
|
|
|
|
sol = eol;
|
|
|
|
|
2014-03-07 06:26:36 +08:00
|
|
|
while (isspace(*key)) key++;
|
|
|
|
if (*key == '#') continue;
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2014-03-07 06:26:36 +08:00
|
|
|
tmp = eol - 2;
|
|
|
|
while ((tmp > key) && isspace(*tmp)) *tmp-- = 0;
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2014-03-07 06:26:36 +08:00
|
|
|
if (!strncmp(key, "import ", 7) && flen == 0) {
|
|
|
|
fn = key + 7;
|
|
|
|
while (isspace(*fn)) fn++;
|
2012-10-13 06:23:40 +08:00
|
|
|
|
2014-03-07 06:26:36 +08:00
|
|
|
key = strchr(fn, ' ');
|
|
|
|
if (key) {
|
|
|
|
*key++ = 0;
|
|
|
|
while (isspace(*key)) key++;
|
|
|
|
}
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2014-03-07 06:26:36 +08:00
|
|
|
load_properties_from_file(fn, key);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
value = strchr(key, '=');
|
|
|
|
if (!value) continue;
|
|
|
|
*value++ = 0;
|
|
|
|
|
|
|
|
tmp = value - 2;
|
|
|
|
while ((tmp > key) && isspace(*tmp)) *tmp-- = 0;
|
|
|
|
|
|
|
|
while (isspace(*value)) value++;
|
|
|
|
|
|
|
|
if (flen > 0) {
|
|
|
|
if (filter[flen - 1] == '*') {
|
|
|
|
if (strncmp(key, filter, flen - 1)) continue;
|
|
|
|
} else {
|
|
|
|
if (strcmp(key, filter)) continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
property_set(key, value);
|
|
|
|
}
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-07 06:26:36 +08:00
|
|
|
/*
|
|
|
|
* Filter is used to decide which properties to load: NULL loads all keys,
|
|
|
|
* "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match.
|
|
|
|
*/
|
2015-03-28 14:20:44 +08:00
|
|
|
static void load_properties_from_file(const char* filename, const char* filter) {
|
|
|
|
Timer t;
|
2015-02-07 04:19:48 +08:00
|
|
|
std::string data;
|
2015-03-28 14:20:44 +08:00
|
|
|
if (read_file(filename, &data)) {
|
2015-05-13 04:54:41 +08:00
|
|
|
data.push_back('\n');
|
2015-02-07 04:19:48 +08:00
|
|
|
load_properties(&data[0], filter);
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
2015-03-28 14:20:44 +08:00
|
|
|
NOTICE("(Loading properties from %s took %.2fs.)\n", filename, t.duration());
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
2015-03-10 23:39:45 +08:00
|
|
|
static void load_persistent_properties() {
|
|
|
|
persistent_properties_loaded = 1;
|
2012-10-03 05:59:59 +08:00
|
|
|
|
2015-03-10 23:39:45 +08:00
|
|
|
std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir(PERSISTENT_PROPERTY_DIR), closedir);
|
|
|
|
if (!dir) {
|
|
|
|
ERROR("Unable to open persistent property directory \"%s\": %s\n",
|
|
|
|
PERSISTENT_PROPERTY_DIR, strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
2012-10-03 05:59:59 +08:00
|
|
|
|
2015-03-10 23:39:45 +08:00
|
|
|
struct dirent* entry;
|
|
|
|
while ((entry = readdir(dir.get())) != NULL) {
|
|
|
|
if (strncmp("persist.", entry->d_name, strlen("persist."))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (entry->d_type != DT_REG) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open the file and read the property value.
|
|
|
|
int fd = openat(dirfd(dir.get()), entry->d_name, O_RDONLY | O_NOFOLLOW);
|
|
|
|
if (fd == -1) {
|
|
|
|
ERROR("Unable to open persistent property file \"%s\": %s\n",
|
|
|
|
entry->d_name, strerror(errno));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct stat sb;
|
|
|
|
if (fstat(fd, &sb) == -1) {
|
|
|
|
ERROR("fstat on property file \"%s\" failed: %s\n", entry->d_name, strerror(errno));
|
2012-10-03 05:59:59 +08:00
|
|
|
close(fd);
|
2015-03-10 23:39:45 +08:00
|
|
|
continue;
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
2009-08-26 10:13:20 +08:00
|
|
|
|
2015-03-10 23:39:45 +08:00
|
|
|
// File must not be accessible to others, be owned by root/root, and
|
|
|
|
// not be a hard link to any other file.
|
|
|
|
if (((sb.st_mode & (S_IRWXG | S_IRWXO)) != 0) || (sb.st_uid != 0) || (sb.st_gid != 0) ||
|
|
|
|
(sb.st_nlink != 1)) {
|
|
|
|
ERROR("skipping insecure property file %s (uid=%u gid=%u nlink=%u mode=%o)\n",
|
|
|
|
entry->d_name, (unsigned int)sb.st_uid, (unsigned int)sb.st_gid,
|
|
|
|
(unsigned int)sb.st_nlink, sb.st_mode);
|
|
|
|
close(fd);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
char value[PROP_VALUE_MAX];
|
|
|
|
int length = read(fd, value, sizeof(value) - 1);
|
|
|
|
if (length >= 0) {
|
|
|
|
value[length] = 0;
|
|
|
|
property_set(entry->d_name, value);
|
|
|
|
} else {
|
|
|
|
ERROR("Unable to read persistent property file %s: %s\n",
|
|
|
|
entry->d_name, strerror(errno));
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
2015-03-28 14:20:44 +08:00
|
|
|
void property_load_boot_defaults() {
|
2012-10-13 06:23:40 +08:00
|
|
|
load_properties_from_file(PROP_PATH_RAMDISK_DEFAULT, NULL);
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
2012-09-20 02:15:24 +08:00
|
|
|
static void load_override_properties() {
|
2015-02-05 06:46:36 +08:00
|
|
|
if (ALLOW_LOCAL_PROP_OVERRIDE) {
|
2015-07-25 01:11:05 +08:00
|
|
|
std::string debuggable = property_get("ro.debuggable");
|
|
|
|
if (debuggable == "1") {
|
2015-02-05 06:46:36 +08:00
|
|
|
load_properties_from_file(PROP_PATH_LOCAL_OVERRIDE, NULL);
|
|
|
|
}
|
2012-09-20 02:15:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-09 09:01:29 +08:00
|
|
|
/* When booting an encrypted system, /data is not mounted when the
|
|
|
|
* property service is started, so any properties stored there are
|
|
|
|
* not loaded. Vold triggers init to load these properties once it
|
|
|
|
* has mounted /data.
|
|
|
|
*/
|
2015-03-28 14:20:44 +08:00
|
|
|
void load_persist_props(void) {
|
2012-09-20 02:15:24 +08:00
|
|
|
load_override_properties();
|
2011-03-09 09:01:29 +08:00
|
|
|
/* Read persistent properties after all default values have been loaded. */
|
|
|
|
load_persistent_properties();
|
|
|
|
}
|
|
|
|
|
2015-05-08 23:30:33 +08:00
|
|
|
void load_recovery_id_prop() {
|
2015-07-25 01:11:05 +08:00
|
|
|
std::string ro_hardware = property_get("ro.hardware");
|
|
|
|
if (ro_hardware.empty()) {
|
2015-05-08 23:30:33 +08:00
|
|
|
ERROR("ro.hardware not set - unable to load recovery id\n");
|
|
|
|
return;
|
|
|
|
}
|
2015-07-25 01:11:05 +08:00
|
|
|
std::string fstab_filename = FSTAB_PREFIX + ro_hardware;
|
2015-05-08 23:30:33 +08:00
|
|
|
|
2015-07-25 01:11:05 +08:00
|
|
|
std::unique_ptr<fstab, void(*)(fstab*)> tab(fs_mgr_read_fstab(fstab_filename.c_str()),
|
2015-05-08 23:30:33 +08:00
|
|
|
fs_mgr_free_fstab);
|
|
|
|
if (!tab) {
|
2015-07-25 01:11:05 +08:00
|
|
|
ERROR("unable to read fstab %s: %s\n", fstab_filename.c_str(), strerror(errno));
|
2015-05-08 23:30:33 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fstab_rec* rec = fs_mgr_get_entry_for_mount_point(tab.get(), RECOVERY_MOUNT_POINT);
|
|
|
|
if (rec == NULL) {
|
|
|
|
ERROR("/recovery not specified in fstab\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fd = open(rec->blk_device, O_RDONLY);
|
|
|
|
if (fd == -1) {
|
|
|
|
ERROR("error opening block device %s: %s\n", rec->blk_device, strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
boot_img_hdr hdr;
|
|
|
|
if (android::base::ReadFully(fd, &hdr, sizeof(hdr))) {
|
|
|
|
std::string hex = bytes_to_hex(reinterpret_cast<uint8_t*>(hdr.id), sizeof(hdr.id));
|
|
|
|
property_set("ro.recovery_id", hex.c_str());
|
|
|
|
} else {
|
|
|
|
ERROR("error reading /recovery: %s\n", strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2015-07-02 05:40:56 +08:00
|
|
|
void load_system_props() {
|
2012-10-13 06:23:40 +08:00
|
|
|
load_properties_from_file(PROP_PATH_SYSTEM_BUILD, NULL);
|
2014-11-11 09:01:33 +08:00
|
|
|
load_properties_from_file(PROP_PATH_VENDOR_BUILD, NULL);
|
2014-03-07 06:26:36 +08:00
|
|
|
load_properties_from_file(PROP_PATH_FACTORY, "ro.*");
|
2015-05-08 23:30:33 +08:00
|
|
|
load_recovery_id_prop();
|
2014-06-17 06:06:21 +08:00
|
|
|
}
|
|
|
|
|
2015-03-28 14:20:44 +08:00
|
|
|
void start_property_service() {
|
2015-04-25 09:50:30 +08:00
|
|
|
property_set_fd = create_socket(PROP_SERVICE_NAME, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
|
|
|
|
0666, 0, 0, NULL);
|
|
|
|
if (property_set_fd == -1) {
|
|
|
|
ERROR("start_property_service socket creation failed: %s\n", strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2015-04-25 09:50:30 +08:00
|
|
|
listen(property_set_fd, 8);
|
2010-04-14 10:33:37 +08:00
|
|
|
|
2015-04-25 12:13:44 +08:00
|
|
|
register_epoll_handler(property_set_fd, handle_property_set_fd);
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|