2009-03-04 04:32:55 +01: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.
|
|
|
|
*/
|
|
|
|
|
2017-04-07 01:30:22 +02:00
|
|
|
#include "property_service.h"
|
|
|
|
|
2018-08-01 22:41:12 +02:00
|
|
|
#include <android/api-level.h>
|
2017-04-07 01:30:22 +02:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2017-02-18 02:48:56 +01:00
|
|
|
#include <inttypes.h>
|
2017-04-07 01:30:22 +02:00
|
|
|
#include <limits.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <stdarg.h>
|
2017-04-07 22:46:21 +02:00
|
|
|
#include <stddef.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2017-04-07 01:30:22 +02:00
|
|
|
#include <sys/mman.h>
|
2014-01-31 23:37:07 +01:00
|
|
|
#include <sys/poll.h>
|
2017-04-07 01:30:22 +02:00
|
|
|
#include <sys/select.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <unistd.h>
|
2017-10-14 01:20:19 +02:00
|
|
|
#include <wchar.h>
|
2015-03-10 16:39:45 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
|
|
|
|
#include <sys/_system_properties.h>
|
|
|
|
|
2019-02-16 21:03:19 +01:00
|
|
|
#include <map>
|
2017-04-07 01:30:22 +02:00
|
|
|
#include <memory>
|
2019-04-24 00:11:07 +02:00
|
|
|
#include <mutex>
|
2019-04-23 02:46:37 +02:00
|
|
|
#include <optional>
|
2017-06-09 20:29:23 +02:00
|
|
|
#include <queue>
|
2021-03-09 22:20:36 +01:00
|
|
|
#include <string_view>
|
2019-04-24 00:11:07 +02:00
|
|
|
#include <thread>
|
2017-04-07 01:30:22 +02:00
|
|
|
#include <vector>
|
2012-08-09 16:05:49 +02:00
|
|
|
|
2020-03-31 02:28:35 +02:00
|
|
|
#include <InitProperties.sysprop.h>
|
2017-07-06 23:20:11 +02:00
|
|
|
#include <android-base/chrono_utils.h>
|
2015-12-05 07:00:26 +01:00
|
|
|
#include <android-base/file.h>
|
2017-04-07 01:30:22 +02:00
|
|
|
#include <android-base/logging.h>
|
2020-06-29 03:42:03 +02:00
|
|
|
#include <android-base/parseint.h>
|
2017-04-24 11:52:54 +02:00
|
|
|
#include <android-base/properties.h>
|
2017-09-05 21:38:30 +02:00
|
|
|
#include <android-base/stringprintf.h>
|
2017-01-24 21:43:58 +01:00
|
|
|
#include <android-base/strings.h>
|
2017-12-14 02:58:17 +01:00
|
|
|
#include <property_info_parser/property_info_parser.h>
|
|
|
|
#include <property_info_serializer/property_info_serializer.h>
|
2017-04-07 01:30:22 +02:00
|
|
|
#include <selinux/android.h>
|
|
|
|
#include <selinux/label.h>
|
|
|
|
#include <selinux/selinux.h>
|
2015-05-08 17:30:33 +02:00
|
|
|
|
2019-04-11 17:57:24 +02:00
|
|
|
#include "debug_ramdisk.h"
|
2019-08-15 22:07:24 +02:00
|
|
|
#include "epoll.h"
|
2009-03-04 04:32:55 +01:00
|
|
|
#include "init.h"
|
2017-08-05 00:59:03 +02:00
|
|
|
#include "persistent_properties.h"
|
2017-12-11 10:40:07 +01:00
|
|
|
#include "property_type.h"
|
2019-04-23 02:46:37 +02:00
|
|
|
#include "proto_utils.h"
|
2020-10-07 01:58:19 +02:00
|
|
|
#include "second_stage_resources.h"
|
2018-05-03 08:33:52 +02:00
|
|
|
#include "selinux.h"
|
2018-02-28 19:39:01 +01:00
|
|
|
#include "subcontext.h"
|
2019-04-23 02:46:37 +02:00
|
|
|
#include "system/core/init/property_service.pb.h"
|
2010-04-14 05:35:46 +02:00
|
|
|
#include "util.h"
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2018-02-28 19:39:01 +01:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2019-01-30 05:19:05 +01:00
|
|
|
using android::base::GetProperty;
|
2020-06-29 03:42:03 +02:00
|
|
|
using android::base::ParseInt;
|
2017-12-14 02:58:17 +01:00
|
|
|
using android::base::ReadFileToString;
|
|
|
|
using android::base::Split;
|
2017-10-10 22:35:01 +02:00
|
|
|
using android::base::StartsWith;
|
2017-09-05 21:38:30 +02:00
|
|
|
using android::base::StringPrintf;
|
2017-07-06 23:20:11 +02:00
|
|
|
using android::base::Timer;
|
2017-12-14 02:58:17 +01:00
|
|
|
using android::base::Trim;
|
2019-04-23 02:46:37 +02:00
|
|
|
using android::base::unique_fd;
|
2017-12-14 02:58:17 +01:00
|
|
|
using android::base::WriteStringToFile;
|
|
|
|
using android::properties::BuildTrie;
|
2018-01-03 23:39:28 +01:00
|
|
|
using android::properties::ParsePropertyInfoFile;
|
2017-12-14 02:58:17 +01:00
|
|
|
using android::properties::PropertyInfoAreaFile;
|
|
|
|
using android::properties::PropertyInfoEntry;
|
2020-03-31 02:28:35 +02:00
|
|
|
using android::sysprop::InitProperties::is_userspace_reboot_supported;
|
2017-07-06 23:20:11 +02:00
|
|
|
|
2017-06-22 21:53:17 +02:00
|
|
|
namespace android {
|
|
|
|
namespace init {
|
2021-05-06 02:23:46 +02:00
|
|
|
constexpr auto FINGERPRINT_PROP = "ro.build.fingerprint";
|
|
|
|
constexpr auto LEGACY_FINGERPRINT_PROP = "ro.build.legacy.fingerprint";
|
|
|
|
constexpr auto ID_PROP = "ro.build.id";
|
|
|
|
constexpr auto LEGACY_ID_PROP = "ro.build.legacy.id";
|
|
|
|
constexpr auto VBMETA_DIGEST_PROP = "ro.boot.vbmeta.digest";
|
|
|
|
constexpr auto DIGEST_SIZE_USED = 8;
|
2017-06-22 21:53:17 +02:00
|
|
|
|
2017-08-05 00:59:03 +02:00
|
|
|
static bool persistent_properties_loaded = false;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2010-04-14 04:33:37 +02:00
|
|
|
static int property_set_fd = -1;
|
init: handle property messages asynchronously #2
A previous change moved property_service into its own thread, since
there was otherwise a deadlock whenever a process called by init would
try to set a property. This new thread, however, would send a message
via a blocking socket to init for each property that it received,
since init may need to take action depending on which property it is.
Unfortunately, this means that the deadlock is still possible, the
only difference is the socket's buffer must be filled before init deadlocks.
This change, therefore, adds the following:
1) A lock for instructing init to reboot
2) A lock for waiting on properties
3) A lock for queueing new properties
A previous version of this change was reverted and added locks around
all service operations and allowed the property thread to spawn
services directly. This was complex due to the fact that this code
was not designed to be multi-threaded. It was reverted due to
apparent issues during reboot. This change keeps a queue of processes
pending control messages, which it will then handle in the future. It
is less flexible but safer.
Bug: 146877356
Bug: 148236233
Bug: 150863651
Bug: 151251827
Test: multiple reboot tests, safely restarting hwservicemanager
Change-Id: Ice773436e85d3bf636bb0a892f3f6002bdf996b6
2020-03-12 22:29:25 +01:00
|
|
|
static int from_init_socket = -1;
|
2019-04-23 02:46:37 +02:00
|
|
|
static int init_socket = -1;
|
2019-10-07 17:26:33 +02:00
|
|
|
static bool accept_messages = false;
|
init: handle property messages asynchronously #2
A previous change moved property_service into its own thread, since
there was otherwise a deadlock whenever a process called by init would
try to set a property. This new thread, however, would send a message
via a blocking socket to init for each property that it received,
since init may need to take action depending on which property it is.
Unfortunately, this means that the deadlock is still possible, the
only difference is the socket's buffer must be filled before init deadlocks.
This change, therefore, adds the following:
1) A lock for instructing init to reboot
2) A lock for waiting on properties
3) A lock for queueing new properties
A previous version of this change was reverted and added locks around
all service operations and allowed the property thread to spawn
services directly. This was complex due to the fact that this code
was not designed to be multi-threaded. It was reverted due to
apparent issues during reboot. This change keeps a queue of processes
pending control messages, which it will then handle in the future. It
is less flexible but safer.
Bug: 146877356
Bug: 148236233
Bug: 150863651
Bug: 151251827
Test: multiple reboot tests, safely restarting hwservicemanager
Change-Id: Ice773436e85d3bf636bb0a892f3f6002bdf996b6
2020-03-12 22:29:25 +01:00
|
|
|
static std::mutex accept_messages_lock;
|
|
|
|
static std::thread property_service_thread;
|
2010-04-14 04:33:37 +02:00
|
|
|
|
2017-12-14 02:58:17 +01:00
|
|
|
static PropertyInfoAreaFile property_info_area;
|
|
|
|
|
init: finer grained permissions for ctl. properties
Currently, permissions for ctl. property apply to each action verb, so
if a domain has permissions for controlling service 'foo', then it can
start, stop, and restart foo.
This change implements finer grainer permissions such that permission
can be given to strictly start a given service, but not stop or
restart it. This new permission scheme is mandatory for the new
control functions, sigstop_on, sigstop_off, interface_start,
interface_stop, interface_restart.
Bug: 78511553
Test: see appropriate successes and failures based on permissions
Merged-In: I6ce915ae39954a67eb6fe1795a93cf715c352ae4
Change-Id: I6ce915ae39954a67eb6fe1795a93cf715c352ae4
(cherry picked from commit 1debdcf1cf3d45ba9185ab47a265995c676280d8)
2018-05-04 01:57:19 +02:00
|
|
|
struct PropertyAuditData {
|
|
|
|
const ucred* cr;
|
|
|
|
const char* name;
|
|
|
|
};
|
|
|
|
|
2019-04-22 19:22:41 +02:00
|
|
|
static int PropertyAuditCallback(void* data, security_class_t /*cls*/, char* buf, size_t len) {
|
|
|
|
auto* d = reinterpret_cast<PropertyAuditData*>(data);
|
|
|
|
|
|
|
|
if (!d || !d->name || !d->cr) {
|
|
|
|
LOG(ERROR) << "AuditCallback invoked with null data arguments!";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(buf, len, "property=%s pid=%d uid=%d gid=%d", d->name, d->cr->pid, d->cr->uid,
|
|
|
|
d->cr->gid);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
init: handle property messages asynchronously #2
A previous change moved property_service into its own thread, since
there was otherwise a deadlock whenever a process called by init would
try to set a property. This new thread, however, would send a message
via a blocking socket to init for each property that it received,
since init may need to take action depending on which property it is.
Unfortunately, this means that the deadlock is still possible, the
only difference is the socket's buffer must be filled before init deadlocks.
This change, therefore, adds the following:
1) A lock for instructing init to reboot
2) A lock for waiting on properties
3) A lock for queueing new properties
A previous version of this change was reverted and added locks around
all service operations and allowed the property thread to spawn
services directly. This was complex due to the fact that this code
was not designed to be multi-threaded. It was reverted due to
apparent issues during reboot. This change keeps a queue of processes
pending control messages, which it will then handle in the future. It
is less flexible but safer.
Bug: 146877356
Bug: 148236233
Bug: 150863651
Bug: 151251827
Test: multiple reboot tests, safely restarting hwservicemanager
Change-Id: Ice773436e85d3bf636bb0a892f3f6002bdf996b6
2020-03-12 22:29:25 +01:00
|
|
|
void StartSendingMessages() {
|
|
|
|
auto lock = std::lock_guard{accept_messages_lock};
|
|
|
|
accept_messages = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StopSendingMessages() {
|
|
|
|
auto lock = std::lock_guard{accept_messages_lock};
|
2020-03-27 21:57:53 +01:00
|
|
|
accept_messages = false;
|
init: handle property messages asynchronously #2
A previous change moved property_service into its own thread, since
there was otherwise a deadlock whenever a process called by init would
try to set a property. This new thread, however, would send a message
via a blocking socket to init for each property that it received,
since init may need to take action depending on which property it is.
Unfortunately, this means that the deadlock is still possible, the
only difference is the socket's buffer must be filled before init deadlocks.
This change, therefore, adds the following:
1) A lock for instructing init to reboot
2) A lock for waiting on properties
3) A lock for queueing new properties
A previous version of this change was reverted and added locks around
all service operations and allowed the property thread to spawn
services directly. This was complex due to the fact that this code
was not designed to be multi-threaded. It was reverted due to
apparent issues during reboot. This change keeps a queue of processes
pending control messages, which it will then handle in the future. It
is less flexible but safer.
Bug: 146877356
Bug: 148236233
Bug: 150863651
Bug: 151251827
Test: multiple reboot tests, safely restarting hwservicemanager
Change-Id: Ice773436e85d3bf636bb0a892f3f6002bdf996b6
2020-03-12 22:29:25 +01:00
|
|
|
}
|
|
|
|
|
2018-10-22 23:50:52 +02:00
|
|
|
bool CanReadProperty(const std::string& source_context, const std::string& name) {
|
|
|
|
const char* target_context = nullptr;
|
|
|
|
property_info_area->GetPropertyInfo(name.c_str(), &target_context, nullptr);
|
|
|
|
|
|
|
|
PropertyAuditData audit_data;
|
|
|
|
|
|
|
|
audit_data.name = name.c_str();
|
|
|
|
|
|
|
|
ucred cr = {.pid = 0, .uid = 0, .gid = 0};
|
|
|
|
audit_data.cr = &cr;
|
|
|
|
|
|
|
|
return selinux_check_access(source_context.c_str(), target_context, "file", "read",
|
|
|
|
&audit_data) == 0;
|
|
|
|
}
|
|
|
|
|
2017-12-11 10:40:07 +01:00
|
|
|
static bool CheckMacPerms(const std::string& name, const char* target_context,
|
2018-01-19 01:14:25 +01:00
|
|
|
const char* source_context, const ucred& cr) {
|
2017-12-11 10:40:07 +01:00
|
|
|
if (!target_context || !source_context) {
|
2017-12-14 02:58:17 +01:00
|
|
|
return false;
|
2017-01-24 21:43:58 +01:00
|
|
|
}
|
2012-08-09 16:05:49 +02:00
|
|
|
|
init: finer grained permissions for ctl. properties
Currently, permissions for ctl. property apply to each action verb, so
if a domain has permissions for controlling service 'foo', then it can
start, stop, and restart foo.
This change implements finer grainer permissions such that permission
can be given to strictly start a given service, but not stop or
restart it. This new permission scheme is mandatory for the new
control functions, sigstop_on, sigstop_off, interface_start,
interface_stop, interface_restart.
Bug: 78511553
Test: see appropriate successes and failures based on permissions
Merged-In: I6ce915ae39954a67eb6fe1795a93cf715c352ae4
Change-Id: I6ce915ae39954a67eb6fe1795a93cf715c352ae4
(cherry picked from commit 1debdcf1cf3d45ba9185ab47a265995c676280d8)
2018-05-04 01:57:19 +02:00
|
|
|
PropertyAuditData audit_data;
|
2017-01-24 21:43:58 +01:00
|
|
|
|
|
|
|
audit_data.name = name.c_str();
|
2018-01-19 01:14:25 +01:00
|
|
|
audit_data.cr = &cr;
|
2015-10-02 01:03:47 +02:00
|
|
|
|
2017-12-11 10:40:07 +01:00
|
|
|
bool has_access = (selinux_check_access(source_context, target_context, "property_service",
|
|
|
|
"set", &audit_data) == 0);
|
2012-08-09 16:05:49 +02:00
|
|
|
|
2017-01-24 21:43:58 +01:00
|
|
|
return has_access;
|
2012-08-09 16:05:49 +02:00
|
|
|
}
|
|
|
|
|
2018-03-01 20:00:57 +01:00
|
|
|
static uint32_t PropertySet(const std::string& name, const std::string& value, std::string* error) {
|
2017-01-24 21:43:58 +01:00
|
|
|
size_t valuelen = value.size();
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2018-02-14 01:50:08 +01:00
|
|
|
if (!IsLegalPropertyName(name)) {
|
2018-03-01 20:00:57 +01:00
|
|
|
*error = "Illegal property name";
|
2017-01-24 21:43:58 +01:00
|
|
|
return PROP_ERROR_INVALID_NAME;
|
2016-11-29 20:20:58 +01:00
|
|
|
}
|
2017-01-24 21:43:58 +01:00
|
|
|
|
2020-02-05 19:49:33 +01:00
|
|
|
if (auto result = IsLegalPropertyValue(name, value); !result.ok()) {
|
2019-07-30 18:34:41 +02:00
|
|
|
*error = result.error().message();
|
2017-10-14 01:20:19 +02:00
|
|
|
return PROP_ERROR_INVALID_VALUE;
|
|
|
|
}
|
|
|
|
|
2017-01-24 21:43:58 +01:00
|
|
|
prop_info* pi = (prop_info*) __system_property_find(name.c_str());
|
2016-11-29 20:20:58 +01:00
|
|
|
if (pi != nullptr) {
|
|
|
|
// ro.* properties are actually "write-once".
|
2017-10-10 22:35:01 +02:00
|
|
|
if (StartsWith(name, "ro.")) {
|
2018-03-01 20:00:57 +01:00
|
|
|
*error = "Read-only property was already set";
|
2017-01-24 21:43:58 +01:00
|
|
|
return PROP_ERROR_READ_ONLY_PROPERTY;
|
2016-11-29 20:20:58 +01:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-01-24 21:43:58 +01:00
|
|
|
__system_property_update(pi, value.c_str(), valuelen);
|
2009-03-04 04:32:55 +01:00
|
|
|
} else {
|
2017-01-24 21:43:58 +01:00
|
|
|
int rc = __system_property_add(name.c_str(), name.size(), value.c_str(), valuelen);
|
2015-03-20 17:45:18 +01:00
|
|
|
if (rc < 0) {
|
2018-03-01 20:00:57 +01:00
|
|
|
*error = "__system_property_add failed";
|
2017-01-24 21:43:58 +01:00
|
|
|
return PROP_ERROR_SET_FAILED;
|
2013-04-29 09:11:57 +02:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2016-11-29 20:20:58 +01:00
|
|
|
|
2016-12-05 22:12:48 +01:00
|
|
|
// Don't write properties to disk until after we have read all default
|
|
|
|
// properties to prevent them from being overwritten by default values.
|
2017-10-10 22:35:01 +02:00
|
|
|
if (persistent_properties_loaded && StartsWith(name, "persist.")) {
|
2017-08-05 00:59:03 +02:00
|
|
|
WritePersistentProperty(name, value);
|
2017-01-19 23:53:00 +01:00
|
|
|
}
|
2019-04-23 02:46:37 +02:00
|
|
|
// If init hasn't started its main loop, then it won't be handling property changed messages
|
|
|
|
// anyway, so there's no need to try to send them.
|
init: handle property messages asynchronously #2
A previous change moved property_service into its own thread, since
there was otherwise a deadlock whenever a process called by init would
try to set a property. This new thread, however, would send a message
via a blocking socket to init for each property that it received,
since init may need to take action depending on which property it is.
Unfortunately, this means that the deadlock is still possible, the
only difference is the socket's buffer must be filled before init deadlocks.
This change, therefore, adds the following:
1) A lock for instructing init to reboot
2) A lock for waiting on properties
3) A lock for queueing new properties
A previous version of this change was reverted and added locks around
all service operations and allowed the property thread to spawn
services directly. This was complex due to the fact that this code
was not designed to be multi-threaded. It was reverted due to
apparent issues during reboot. This change keeps a queue of processes
pending control messages, which it will then handle in the future. It
is less flexible but safer.
Bug: 146877356
Bug: 148236233
Bug: 150863651
Bug: 151251827
Test: multiple reboot tests, safely restarting hwservicemanager
Change-Id: Ice773436e85d3bf636bb0a892f3f6002bdf996b6
2020-03-12 22:29:25 +01:00
|
|
|
auto lock = std::lock_guard{accept_messages_lock};
|
2019-10-07 17:26:33 +02:00
|
|
|
if (accept_messages) {
|
init: handle property messages asynchronously #2
A previous change moved property_service into its own thread, since
there was otherwise a deadlock whenever a process called by init would
try to set a property. This new thread, however, would send a message
via a blocking socket to init for each property that it received,
since init may need to take action depending on which property it is.
Unfortunately, this means that the deadlock is still possible, the
only difference is the socket's buffer must be filled before init deadlocks.
This change, therefore, adds the following:
1) A lock for instructing init to reboot
2) A lock for waiting on properties
3) A lock for queueing new properties
A previous version of this change was reverted and added locks around
all service operations and allowed the property thread to spawn
services directly. This was complex due to the fact that this code
was not designed to be multi-threaded. It was reverted due to
apparent issues during reboot. This change keeps a queue of processes
pending control messages, which it will then handle in the future. It
is less flexible but safer.
Bug: 146877356
Bug: 148236233
Bug: 150863651
Bug: 151251827
Test: multiple reboot tests, safely restarting hwservicemanager
Change-Id: Ice773436e85d3bf636bb0a892f3f6002bdf996b6
2020-03-12 22:29:25 +01:00
|
|
|
PropertyChanged(name, value);
|
2019-04-23 02:46:37 +02:00
|
|
|
}
|
2017-01-24 21:43:58 +01:00
|
|
|
return PROP_SUCCESS;
|
2017-01-19 23:53:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-10 19:47:24 +01:00
|
|
|
class AsyncRestorecon {
|
2019-04-24 00:11:07 +02:00
|
|
|
public:
|
2020-03-10 19:47:24 +01:00
|
|
|
void TriggerRestorecon(const std::string& path) {
|
2019-04-24 00:11:07 +02:00
|
|
|
auto guard = std::lock_guard{mutex_};
|
2020-03-10 19:47:24 +01:00
|
|
|
paths_.emplace(path);
|
2017-06-09 20:29:23 +02:00
|
|
|
|
2020-03-10 19:47:24 +01:00
|
|
|
if (!thread_started_) {
|
|
|
|
thread_started_ = true;
|
|
|
|
std::thread{&AsyncRestorecon::ThreadFunction, this}.detach();
|
2017-06-09 20:29:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-24 00:11:07 +02:00
|
|
|
private:
|
|
|
|
void ThreadFunction() {
|
|
|
|
auto lock = std::unique_lock{mutex_};
|
2017-06-09 20:29:23 +02:00
|
|
|
|
2020-03-10 19:47:24 +01:00
|
|
|
while (!paths_.empty()) {
|
|
|
|
auto path = paths_.front();
|
|
|
|
paths_.pop();
|
2019-04-24 00:11:07 +02:00
|
|
|
|
|
|
|
lock.unlock();
|
2020-03-10 19:47:24 +01:00
|
|
|
if (selinux_android_restorecon(path.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE) != 0) {
|
|
|
|
LOG(ERROR) << "Asynchronous restorecon of '" << path << "' failed'";
|
|
|
|
}
|
|
|
|
android::base::SetProperty(kRestoreconProperty, path);
|
2019-04-24 00:11:07 +02:00
|
|
|
lock.lock();
|
|
|
|
}
|
2017-06-09 20:29:23 +02:00
|
|
|
|
2020-03-10 19:47:24 +01:00
|
|
|
thread_started_ = false;
|
2017-06-09 20:29:23 +02:00
|
|
|
}
|
|
|
|
|
2019-04-24 00:11:07 +02:00
|
|
|
std::mutex mutex_;
|
2020-03-10 19:47:24 +01:00
|
|
|
std::queue<std::string> paths_;
|
|
|
|
bool thread_started_ = false;
|
2019-04-24 00:11:07 +02:00
|
|
|
};
|
2017-06-09 20:29:23 +02:00
|
|
|
|
2018-01-19 01:14:25 +01:00
|
|
|
class SocketConnection {
|
|
|
|
public:
|
|
|
|
SocketConnection(int socket, const ucred& cred) : socket_(socket), cred_(cred) {}
|
2017-01-24 21:43:58 +01:00
|
|
|
|
2018-01-19 01:14:25 +01:00
|
|
|
bool RecvUint32(uint32_t* value, uint32_t* timeout_ms) {
|
|
|
|
return RecvFully(value, sizeof(*value), timeout_ms);
|
2017-02-22 23:54:15 +01:00
|
|
|
}
|
|
|
|
|
2018-01-19 01:14:25 +01:00
|
|
|
bool RecvChars(char* chars, size_t size, uint32_t* timeout_ms) {
|
|
|
|
return RecvFully(chars, size, timeout_ms);
|
2017-01-24 21:43:58 +01:00
|
|
|
}
|
|
|
|
|
2018-01-19 01:14:25 +01:00
|
|
|
bool RecvString(std::string* value, uint32_t* timeout_ms) {
|
|
|
|
uint32_t len = 0;
|
|
|
|
if (!RecvUint32(&len, timeout_ms)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len == 0) {
|
|
|
|
*value = "";
|
2018-01-02 16:19:57 +01:00
|
|
|
return true;
|
2018-01-19 01:14:25 +01:00
|
|
|
}
|
2017-01-24 21:43:58 +01:00
|
|
|
|
2018-01-19 01:14:25 +01:00
|
|
|
// http://b/35166374: don't allow init to make arbitrarily large allocations.
|
|
|
|
if (len > 0xffff) {
|
|
|
|
LOG(ERROR) << "sys_prop: RecvString asked to read huge string: " << len;
|
|
|
|
errno = ENOMEM;
|
|
|
|
return false;
|
|
|
|
}
|
2017-01-24 21:43:58 +01:00
|
|
|
|
2018-01-19 01:14:25 +01:00
|
|
|
std::vector<char> chars(len);
|
|
|
|
if (!RecvChars(&chars[0], len, timeout_ms)) {
|
|
|
|
return false;
|
2017-01-24 21:43:58 +01:00
|
|
|
}
|
2018-01-19 01:14:25 +01:00
|
|
|
|
|
|
|
*value = std::string(&chars[0], len);
|
|
|
|
return true;
|
2017-01-24 21:43:58 +01:00
|
|
|
}
|
|
|
|
|
2018-01-19 01:14:25 +01:00
|
|
|
bool SendUint32(uint32_t value) {
|
2019-04-23 02:46:37 +02:00
|
|
|
if (!socket_.ok()) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-01-19 01:14:25 +01:00
|
|
|
int result = TEMP_FAILURE_RETRY(send(socket_, &value, sizeof(value), 0));
|
|
|
|
return result == sizeof(value);
|
|
|
|
}
|
2017-01-24 21:43:58 +01:00
|
|
|
|
2019-04-22 22:28:28 +02:00
|
|
|
bool GetSourceContext(std::string* source_context) const {
|
|
|
|
char* c_source_context = nullptr;
|
|
|
|
if (getpeercon(socket_, &c_source_context) != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*source_context = c_source_context;
|
|
|
|
freecon(c_source_context);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-04-23 02:46:37 +02:00
|
|
|
[[nodiscard]] int Release() { return socket_.release(); }
|
2017-01-24 21:43:58 +01:00
|
|
|
|
2018-01-19 01:14:25 +01:00
|
|
|
const ucred& cred() { return cred_; }
|
2017-01-24 21:43:58 +01:00
|
|
|
|
2018-01-19 01:14:25 +01:00
|
|
|
private:
|
|
|
|
bool PollIn(uint32_t* timeout_ms) {
|
|
|
|
struct pollfd ufds[1];
|
|
|
|
ufds[0].fd = socket_;
|
|
|
|
ufds[0].events = POLLIN;
|
|
|
|
ufds[0].revents = 0;
|
|
|
|
while (*timeout_ms > 0) {
|
|
|
|
auto start_time = std::chrono::steady_clock::now();
|
|
|
|
int nr = poll(ufds, 1, *timeout_ms);
|
|
|
|
auto now = std::chrono::steady_clock::now();
|
|
|
|
auto time_elapsed =
|
|
|
|
std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time);
|
|
|
|
uint64_t millis = time_elapsed.count();
|
|
|
|
*timeout_ms = (millis > *timeout_ms) ? 0 : *timeout_ms - millis;
|
|
|
|
|
|
|
|
if (nr > 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nr == 0) {
|
|
|
|
// Timeout
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nr < 0 && errno != EINTR) {
|
|
|
|
PLOG(ERROR) << "sys_prop: error waiting for uid " << cred_.uid
|
|
|
|
<< " to send property message";
|
|
|
|
return false;
|
|
|
|
} else { // errno == EINTR
|
|
|
|
// Timer rounds milliseconds down in case of EINTR we want it to be rounded up
|
|
|
|
// to avoid slowing init down by causing EINTR with under millisecond timeout.
|
|
|
|
if (*timeout_ms > 0) {
|
|
|
|
--(*timeout_ms);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-24 21:43:58 +01:00
|
|
|
|
2018-01-19 01:14:25 +01:00
|
|
|
LOG(ERROR) << "sys_prop: timeout waiting for uid " << cred_.uid
|
|
|
|
<< " to send property message.";
|
|
|
|
return false;
|
|
|
|
}
|
2017-01-24 21:43:58 +01:00
|
|
|
|
2018-01-19 01:14:25 +01:00
|
|
|
bool RecvFully(void* data_ptr, size_t size, uint32_t* timeout_ms) {
|
|
|
|
size_t bytes_left = size;
|
|
|
|
char* data = static_cast<char*>(data_ptr);
|
|
|
|
while (*timeout_ms > 0 && bytes_left > 0) {
|
|
|
|
if (!PollIn(timeout_ms)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int result = TEMP_FAILURE_RETRY(recv(socket_, data, bytes_left, MSG_DONTWAIT));
|
|
|
|
if (result <= 0) {
|
2018-05-21 08:43:56 +02:00
|
|
|
PLOG(ERROR) << "sys_prop: recv error";
|
2018-01-19 01:14:25 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes_left -= result;
|
|
|
|
data += result;
|
|
|
|
}
|
|
|
|
|
2018-05-21 08:43:56 +02:00
|
|
|
if (bytes_left != 0) {
|
|
|
|
LOG(ERROR) << "sys_prop: recv data is not properly obtained.";
|
|
|
|
}
|
|
|
|
|
2018-01-19 01:14:25 +01:00
|
|
|
return bytes_left == 0;
|
|
|
|
}
|
|
|
|
|
2019-04-23 02:46:37 +02:00
|
|
|
unique_fd socket_;
|
2018-01-19 01:14:25 +01:00
|
|
|
ucred cred_;
|
2019-04-23 02:46:37 +02:00
|
|
|
|
2019-08-28 19:47:49 +02:00
|
|
|
DISALLOW_IMPLICIT_CONSTRUCTORS(SocketConnection);
|
|
|
|
};
|
2019-04-23 02:46:37 +02:00
|
|
|
|
2019-04-23 02:46:37 +02:00
|
|
|
static uint32_t SendControlMessage(const std::string& msg, const std::string& name, pid_t pid,
|
|
|
|
SocketConnection* socket, std::string* error) {
|
init: handle property messages asynchronously #2
A previous change moved property_service into its own thread, since
there was otherwise a deadlock whenever a process called by init would
try to set a property. This new thread, however, would send a message
via a blocking socket to init for each property that it received,
since init may need to take action depending on which property it is.
Unfortunately, this means that the deadlock is still possible, the
only difference is the socket's buffer must be filled before init deadlocks.
This change, therefore, adds the following:
1) A lock for instructing init to reboot
2) A lock for waiting on properties
3) A lock for queueing new properties
A previous version of this change was reverted and added locks around
all service operations and allowed the property thread to spawn
services directly. This was complex due to the fact that this code
was not designed to be multi-threaded. It was reverted due to
apparent issues during reboot. This change keeps a queue of processes
pending control messages, which it will then handle in the future. It
is less flexible but safer.
Bug: 146877356
Bug: 148236233
Bug: 150863651
Bug: 151251827
Test: multiple reboot tests, safely restarting hwservicemanager
Change-Id: Ice773436e85d3bf636bb0a892f3f6002bdf996b6
2020-03-12 22:29:25 +01:00
|
|
|
auto lock = std::lock_guard{accept_messages_lock};
|
2019-10-07 17:26:33 +02:00
|
|
|
if (!accept_messages) {
|
2019-04-23 02:46:37 +02:00
|
|
|
*error = "Received control message after shutdown, ignoring";
|
|
|
|
return PROP_ERROR_HANDLE_CONTROL_MESSAGE;
|
|
|
|
}
|
|
|
|
|
2020-03-10 19:47:24 +01:00
|
|
|
// We must release the fd before sending it to init, otherwise there will be a race with init.
|
|
|
|
// If init calls close() before Release(), then fdsan will see the wrong tag and abort().
|
2019-04-23 02:46:37 +02:00
|
|
|
int fd = -1;
|
2019-10-22 17:27:23 +02:00
|
|
|
if (socket != nullptr && SelinuxGetVendorAndroidVersion() > __ANDROID_API_Q__) {
|
2019-04-23 02:46:37 +02:00
|
|
|
fd = socket->Release();
|
|
|
|
}
|
|
|
|
|
init: handle property messages asynchronously #2
A previous change moved property_service into its own thread, since
there was otherwise a deadlock whenever a process called by init would
try to set a property. This new thread, however, would send a message
via a blocking socket to init for each property that it received,
since init may need to take action depending on which property it is.
Unfortunately, this means that the deadlock is still possible, the
only difference is the socket's buffer must be filled before init deadlocks.
This change, therefore, adds the following:
1) A lock for instructing init to reboot
2) A lock for waiting on properties
3) A lock for queueing new properties
A previous version of this change was reverted and added locks around
all service operations and allowed the property thread to spawn
services directly. This was complex due to the fact that this code
was not designed to be multi-threaded. It was reverted due to
apparent issues during reboot. This change keeps a queue of processes
pending control messages, which it will then handle in the future. It
is less flexible but safer.
Bug: 146877356
Bug: 148236233
Bug: 150863651
Bug: 151251827
Test: multiple reboot tests, safely restarting hwservicemanager
Change-Id: Ice773436e85d3bf636bb0a892f3f6002bdf996b6
2020-03-12 22:29:25 +01:00
|
|
|
bool queue_success = QueueControlMessage(msg, name, pid, fd);
|
|
|
|
if (!queue_success && fd != -1) {
|
|
|
|
uint32_t response = PROP_ERROR_HANDLE_CONTROL_MESSAGE;
|
|
|
|
TEMP_FAILURE_RETRY(send(fd, &response, sizeof(response), 0));
|
|
|
|
close(fd);
|
2020-03-10 19:47:24 +01:00
|
|
|
}
|
2019-04-23 02:46:37 +02:00
|
|
|
|
|
|
|
return PROP_SUCCESS;
|
|
|
|
}
|
|
|
|
|
init: finer grained permissions for ctl. properties
Currently, permissions for ctl. property apply to each action verb, so
if a domain has permissions for controlling service 'foo', then it can
start, stop, and restart foo.
This change implements finer grainer permissions such that permission
can be given to strictly start a given service, but not stop or
restart it. This new permission scheme is mandatory for the new
control functions, sigstop_on, sigstop_off, interface_start,
interface_stop, interface_restart.
Bug: 78511553
Test: see appropriate successes and failures based on permissions
Merged-In: I6ce915ae39954a67eb6fe1795a93cf715c352ae4
Change-Id: I6ce915ae39954a67eb6fe1795a93cf715c352ae4
(cherry picked from commit 1debdcf1cf3d45ba9185ab47a265995c676280d8)
2018-05-04 01:57:19 +02:00
|
|
|
bool CheckControlPropertyPerms(const std::string& name, const std::string& value,
|
|
|
|
const std::string& source_context, const ucred& cr) {
|
|
|
|
// We check the legacy method first but these properties are dontaudit, so we only log an audit
|
|
|
|
// if the newer method fails as well. We only do this with the legacy ctl. properties.
|
|
|
|
if (name == "ctl.start" || name == "ctl.stop" || name == "ctl.restart") {
|
|
|
|
// The legacy permissions model is that ctl. properties have their name ctl.<action> and
|
|
|
|
// their value is the name of the service to apply that action to. Permissions for these
|
|
|
|
// actions are based on the service, so we must create a fake name of ctl.<service> to
|
|
|
|
// check permissions.
|
|
|
|
auto control_string_legacy = "ctl." + value;
|
|
|
|
const char* target_context_legacy = nullptr;
|
|
|
|
const char* type_legacy = nullptr;
|
|
|
|
property_info_area->GetPropertyInfo(control_string_legacy.c_str(), &target_context_legacy,
|
|
|
|
&type_legacy);
|
|
|
|
|
|
|
|
if (CheckMacPerms(control_string_legacy, target_context_legacy, source_context.c_str(), cr)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto control_string_full = name + "$" + value;
|
|
|
|
const char* target_context_full = nullptr;
|
|
|
|
const char* type_full = nullptr;
|
|
|
|
property_info_area->GetPropertyInfo(control_string_full.c_str(), &target_context_full,
|
|
|
|
&type_full);
|
|
|
|
|
|
|
|
return CheckMacPerms(control_string_full, target_context_full, source_context.c_str(), cr);
|
|
|
|
}
|
|
|
|
|
2018-01-19 01:14:25 +01:00
|
|
|
// This returns one of the enum of PROP_SUCCESS or PROP_ERROR*.
|
2019-02-16 21:03:19 +01:00
|
|
|
uint32_t CheckPermissions(const std::string& name, const std::string& value,
|
|
|
|
const std::string& source_context, const ucred& cr, std::string* error) {
|
2018-02-14 01:50:08 +01:00
|
|
|
if (!IsLegalPropertyName(name)) {
|
2018-03-01 20:00:57 +01:00
|
|
|
*error = "Illegal property name";
|
2018-01-19 01:14:25 +01:00
|
|
|
return PROP_ERROR_INVALID_NAME;
|
|
|
|
}
|
2017-12-11 10:40:07 +01:00
|
|
|
|
2018-01-19 01:14:25 +01:00
|
|
|
if (StartsWith(name, "ctl.")) {
|
init: finer grained permissions for ctl. properties
Currently, permissions for ctl. property apply to each action verb, so
if a domain has permissions for controlling service 'foo', then it can
start, stop, and restart foo.
This change implements finer grainer permissions such that permission
can be given to strictly start a given service, but not stop or
restart it. This new permission scheme is mandatory for the new
control functions, sigstop_on, sigstop_off, interface_start,
interface_stop, interface_restart.
Bug: 78511553
Test: see appropriate successes and failures based on permissions
Merged-In: I6ce915ae39954a67eb6fe1795a93cf715c352ae4
Change-Id: I6ce915ae39954a67eb6fe1795a93cf715c352ae4
(cherry picked from commit 1debdcf1cf3d45ba9185ab47a265995c676280d8)
2018-05-04 01:57:19 +02:00
|
|
|
if (!CheckControlPropertyPerms(name, value, source_context, cr)) {
|
|
|
|
*error = StringPrintf("Invalid permissions to perform '%s' on '%s'", name.c_str() + 4,
|
|
|
|
value.c_str());
|
2018-01-19 01:14:25 +01:00
|
|
|
return PROP_ERROR_HANDLE_CONTROL_MESSAGE;
|
|
|
|
}
|
2017-09-05 21:38:30 +02:00
|
|
|
|
2018-01-19 01:14:25 +01:00
|
|
|
return PROP_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* target_context = nullptr;
|
|
|
|
const char* type = nullptr;
|
|
|
|
property_info_area->GetPropertyInfo(name.c_str(), &target_context, &type);
|
|
|
|
|
|
|
|
if (!CheckMacPerms(name, target_context, source_context.c_str(), cr)) {
|
2018-03-01 20:00:57 +01:00
|
|
|
*error = "SELinux permission check failed";
|
2018-01-19 01:14:25 +01:00
|
|
|
return PROP_ERROR_PERMISSION_DENIED;
|
|
|
|
}
|
|
|
|
|
2019-11-09 02:54:27 +01:00
|
|
|
if (!CheckType(type, value)) {
|
2018-03-01 20:00:57 +01:00
|
|
|
*error = StringPrintf("Property type check failed, value doesn't match expected type '%s'",
|
|
|
|
(type ?: "(null)"));
|
2018-01-19 01:14:25 +01:00
|
|
|
return PROP_ERROR_INVALID_VALUE;
|
|
|
|
}
|
|
|
|
|
2019-02-16 21:03:19 +01:00
|
|
|
return PROP_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This returns one of the enum of PROP_SUCCESS or PROP_ERROR*.
|
|
|
|
uint32_t HandlePropertySet(const std::string& name, const std::string& value,
|
2019-04-23 02:46:37 +02:00
|
|
|
const std::string& source_context, const ucred& cr,
|
|
|
|
SocketConnection* socket, std::string* error) {
|
2019-02-16 21:03:19 +01:00
|
|
|
if (auto ret = CheckPermissions(name, value, source_context, cr, error); ret != PROP_SUCCESS) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (StartsWith(name, "ctl.")) {
|
2019-04-23 02:46:37 +02:00
|
|
|
return SendControlMessage(name.c_str() + 4, value, cr.pid, socket, error);
|
2019-02-16 21:03:19 +01:00
|
|
|
}
|
|
|
|
|
2018-01-19 01:14:25 +01:00
|
|
|
// sys.powerctl is a special property that is used to make the device reboot. We want to log
|
|
|
|
// any process that sets this property to be able to accurately blame the cause of a shutdown.
|
|
|
|
if (name == "sys.powerctl") {
|
|
|
|
std::string cmdline_path = StringPrintf("proc/%d/cmdline", cr.pid);
|
|
|
|
std::string process_cmdline;
|
|
|
|
std::string process_log_string;
|
|
|
|
if (ReadFileToString(cmdline_path, &process_cmdline)) {
|
|
|
|
// Since cmdline is null deliminated, .c_str() conveniently gives us just the process
|
|
|
|
// path.
|
|
|
|
process_log_string = StringPrintf(" (%s)", process_cmdline.c_str());
|
|
|
|
}
|
|
|
|
LOG(INFO) << "Received sys.powerctl='" << value << "' from pid: " << cr.pid
|
|
|
|
<< process_log_string;
|
2020-03-31 23:36:03 +02:00
|
|
|
if (!value.empty()) {
|
|
|
|
DebugRebootLogging();
|
|
|
|
}
|
2020-03-31 02:28:35 +02:00
|
|
|
if (value == "reboot,userspace" && !is_userspace_reboot_supported().value_or(false)) {
|
|
|
|
*error = "Userspace reboot is not supported by this device";
|
|
|
|
return PROP_ERROR_INVALID_VALUE;
|
|
|
|
}
|
2018-01-19 01:14:25 +01:00
|
|
|
}
|
|
|
|
|
2019-04-24 00:11:07 +02:00
|
|
|
// If a process other than init is writing a non-empty value, it means that process is
|
|
|
|
// requesting that init performs a restorecon operation on the path specified by 'value'.
|
|
|
|
// We use a thread to do this restorecon operation to prevent holding up init, as it may take
|
|
|
|
// a long time to complete.
|
|
|
|
if (name == kRestoreconProperty && cr.pid != 1 && !value.empty()) {
|
2020-03-10 19:47:24 +01:00
|
|
|
static AsyncRestorecon async_restorecon;
|
|
|
|
async_restorecon.TriggerRestorecon(value);
|
2019-04-24 00:11:07 +02:00
|
|
|
return PROP_SUCCESS;
|
2018-03-01 20:00:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return PropertySet(name, value, error);
|
2017-01-24 21:43:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_property_set_fd() {
|
|
|
|
static constexpr uint32_t kDefaultSocketTimeout = 2000; /* ms */
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-01-25 14:08:51 +01:00
|
|
|
int s = accept4(property_set_fd, nullptr, nullptr, SOCK_CLOEXEC);
|
2016-08-23 21:50:00 +02:00
|
|
|
if (s == -1) {
|
2009-03-04 04:32:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-19 01:14:25 +01:00
|
|
|
ucred cr;
|
2016-08-23 21:50:00 +02:00
|
|
|
socklen_t cr_size = sizeof(cr);
|
2009-03-04 04:32:55 +01:00
|
|
|
if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &cr, &cr_size) < 0) {
|
|
|
|
close(s);
|
2017-02-22 23:54:15 +01:00
|
|
|
PLOG(ERROR) << "sys_prop: unable to get SO_PEERCRED";
|
2009-03-04 04:32:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-24 21:43:58 +01:00
|
|
|
SocketConnection socket(s, cr);
|
|
|
|
uint32_t timeout_ms = kDefaultSocketTimeout;
|
2017-01-19 23:53:00 +01:00
|
|
|
|
2017-01-24 21:43:58 +01:00
|
|
|
uint32_t cmd = 0;
|
|
|
|
if (!socket.RecvUint32(&cmd, &timeout_ms)) {
|
|
|
|
PLOG(ERROR) << "sys_prop: error while reading command from the socket";
|
|
|
|
socket.SendUint32(PROP_ERROR_READ_CMD);
|
2009-03-04 04:32:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-22 23:54:15 +01:00
|
|
|
switch (cmd) {
|
2017-01-24 21:43:58 +01:00
|
|
|
case PROP_MSG_SETPROP: {
|
|
|
|
char prop_name[PROP_NAME_MAX];
|
|
|
|
char prop_value[PROP_VALUE_MAX];
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-01-24 21:43:58 +01:00
|
|
|
if (!socket.RecvChars(prop_name, PROP_NAME_MAX, &timeout_ms) ||
|
|
|
|
!socket.RecvChars(prop_value, PROP_VALUE_MAX, &timeout_ms)) {
|
|
|
|
PLOG(ERROR) << "sys_prop(PROP_MSG_SETPROP): error while reading name/value from the socket";
|
|
|
|
return;
|
2013-09-14 02:21:28 +02:00
|
|
|
}
|
|
|
|
|
2017-01-24 21:43:58 +01:00
|
|
|
prop_name[PROP_NAME_MAX-1] = 0;
|
|
|
|
prop_value[PROP_VALUE_MAX-1] = 0;
|
2011-04-01 17:24:13 +02:00
|
|
|
|
2019-04-22 22:28:28 +02:00
|
|
|
std::string source_context;
|
|
|
|
if (!socket.GetSourceContext(&source_context)) {
|
|
|
|
PLOG(ERROR) << "Unable to set property '" << prop_name << "': getpeercon() failed";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-01 20:00:57 +01:00
|
|
|
const auto& cr = socket.cred();
|
|
|
|
std::string error;
|
2019-04-23 02:46:37 +02:00
|
|
|
uint32_t result =
|
|
|
|
HandlePropertySet(prop_name, prop_value, source_context, cr, nullptr, &error);
|
2018-03-01 20:00:57 +01:00
|
|
|
if (result != PROP_SUCCESS) {
|
2019-04-04 19:10:01 +02:00
|
|
|
LOG(ERROR) << "Unable to set property '" << prop_name << "' from uid:" << cr.uid
|
|
|
|
<< " gid:" << cr.gid << " pid:" << cr.pid << ": " << error;
|
2018-03-01 20:00:57 +01:00
|
|
|
}
|
|
|
|
|
2017-01-19 23:53:00 +01:00
|
|
|
break;
|
2017-01-24 21:43:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
case PROP_MSG_SETPROP2: {
|
|
|
|
std::string name;
|
|
|
|
std::string value;
|
|
|
|
if (!socket.RecvString(&name, &timeout_ms) ||
|
|
|
|
!socket.RecvString(&value, &timeout_ms)) {
|
|
|
|
PLOG(ERROR) << "sys_prop(PROP_MSG_SETPROP2): error while reading name/value from the socket";
|
|
|
|
socket.SendUint32(PROP_ERROR_READ_DATA);
|
|
|
|
return;
|
|
|
|
}
|
2017-01-24 19:38:09 +01:00
|
|
|
|
2019-04-22 22:28:28 +02:00
|
|
|
std::string source_context;
|
|
|
|
if (!socket.GetSourceContext(&source_context)) {
|
|
|
|
PLOG(ERROR) << "Unable to set property '" << name << "': getpeercon() failed";
|
|
|
|
socket.SendUint32(PROP_ERROR_PERMISSION_DENIED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-01 20:00:57 +01:00
|
|
|
const auto& cr = socket.cred();
|
|
|
|
std::string error;
|
2019-04-23 02:46:37 +02:00
|
|
|
uint32_t result = HandlePropertySet(name, value, source_context, cr, &socket, &error);
|
2018-03-01 20:00:57 +01:00
|
|
|
if (result != PROP_SUCCESS) {
|
2019-04-04 19:10:01 +02:00
|
|
|
LOG(ERROR) << "Unable to set property '" << name << "' from uid:" << cr.uid
|
|
|
|
<< " gid:" << cr.gid << " pid:" << cr.pid << ": " << error;
|
2018-03-01 20:00:57 +01:00
|
|
|
}
|
2019-08-26 18:33:40 +02:00
|
|
|
socket.SendUint32(result);
|
2017-01-24 21:43:58 +01:00
|
|
|
break;
|
|
|
|
}
|
2017-02-22 23:54:15 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
default:
|
2017-02-22 23:54:15 +01:00
|
|
|
LOG(ERROR) << "sys_prop: invalid command " << cmd;
|
2017-01-24 21:43:58 +01:00
|
|
|
socket.SendUint32(PROP_ERROR_INVALID_CMD);
|
2009-03-04 04:32:55 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-20 00:21:25 +02:00
|
|
|
uint32_t InitPropertySet(const std::string& name, const std::string& value) {
|
|
|
|
uint32_t result = 0;
|
|
|
|
ucred cr = {.pid = 1, .uid = 0, .gid = 0};
|
|
|
|
std::string error;
|
|
|
|
result = HandlePropertySet(name, value, kInitContext, cr, nullptr, &error);
|
|
|
|
if (result != PROP_SUCCESS) {
|
|
|
|
LOG(ERROR) << "Init cannot set '" << name << "' to '" << value << "': " << error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-02-16 21:03:19 +01:00
|
|
|
static bool load_properties_from_file(const char*, const char*,
|
|
|
|
std::map<std::string, std::string>*);
|
2014-03-06 23:26:36 +01: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.
|
|
|
|
*/
|
2019-02-16 21:03:19 +01:00
|
|
|
static void LoadProperties(char* data, const char* filter, const char* filename,
|
|
|
|
std::map<std::string, std::string>* properties) {
|
2014-03-06 23:26:36 +01:00
|
|
|
char *key, *value, *eol, *sol, *tmp, *fn;
|
|
|
|
size_t flen = 0;
|
|
|
|
|
2020-07-16 02:04:43 +02:00
|
|
|
static constexpr const char* const kVendorPathPrefixes[4] = {
|
2019-09-18 22:47:19 +02:00
|
|
|
"/vendor",
|
|
|
|
"/odm",
|
2020-06-25 03:29:22 +02:00
|
|
|
"/vendor_dlkm",
|
2020-07-16 02:04:43 +02:00
|
|
|
"/odm_dlkm",
|
2019-09-18 22:47:19 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const char* context = kInitContext;
|
2018-08-01 22:41:12 +02:00
|
|
|
if (SelinuxGetVendorAndroidVersion() >= __ANDROID_API_P__) {
|
2019-09-18 22:47:19 +02:00
|
|
|
for (const auto& vendor_path_prefix : kVendorPathPrefixes) {
|
|
|
|
if (StartsWith(filename, vendor_path_prefix)) {
|
|
|
|
context = kVendorContext;
|
2018-04-12 00:50:00 +02:00
|
|
|
}
|
2018-02-28 19:39:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-06 23:26:36 +01:00
|
|
|
if (filter) {
|
|
|
|
flen = strlen(filter);
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
sol = data;
|
2014-03-06 23:26:36 +01:00
|
|
|
while ((eol = strchr(sol, '\n'))) {
|
2009-03-04 04:32:55 +01:00
|
|
|
key = sol;
|
|
|
|
*eol++ = 0;
|
|
|
|
sol = eol;
|
|
|
|
|
2014-03-06 23:26:36 +01:00
|
|
|
while (isspace(*key)) key++;
|
|
|
|
if (*key == '#') continue;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2014-03-06 23:26:36 +01:00
|
|
|
tmp = eol - 2;
|
|
|
|
while ((tmp > key) && isspace(*tmp)) *tmp-- = 0;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2014-03-06 23:26:36 +01:00
|
|
|
if (!strncmp(key, "import ", 7) && flen == 0) {
|
|
|
|
fn = key + 7;
|
|
|
|
while (isspace(*fn)) fn++;
|
2012-10-13 00:23:40 +02:00
|
|
|
|
2014-03-06 23:26:36 +01:00
|
|
|
key = strchr(fn, ' ');
|
|
|
|
if (key) {
|
|
|
|
*key++ = 0;
|
|
|
|
while (isspace(*key)) key++;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2019-06-12 02:31:35 +02:00
|
|
|
std::string raw_filename(fn);
|
2019-07-31 22:59:15 +02:00
|
|
|
auto expanded_filename = ExpandProps(raw_filename);
|
|
|
|
|
2020-02-05 19:49:33 +01:00
|
|
|
if (!expanded_filename.ok()) {
|
2019-07-31 22:59:15 +02:00
|
|
|
LOG(ERROR) << "Could not expand filename ': " << expanded_filename.error();
|
2019-06-12 02:31:35 +02:00
|
|
|
continue;
|
|
|
|
}
|
2014-03-06 23:26:36 +01:00
|
|
|
|
2019-07-31 22:59:15 +02:00
|
|
|
load_properties_from_file(expanded_filename->c_str(), key, properties);
|
2014-03-06 23:26:36 +01:00
|
|
|
} 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] == '*') {
|
2019-07-09 00:09:36 +02:00
|
|
|
if (strncmp(key, filter, flen - 1) != 0) continue;
|
2014-03-06 23:26:36 +01:00
|
|
|
} else {
|
2019-07-09 00:09:36 +02:00
|
|
|
if (strcmp(key, filter) != 0) continue;
|
2014-03-06 23:26:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-28 19:39:01 +01:00
|
|
|
if (StartsWith(key, "ctl.") || key == "sys.powerctl"s ||
|
2019-04-24 00:11:07 +02:00
|
|
|
std::string{key} == kRestoreconProperty) {
|
2018-02-28 19:39:01 +01:00
|
|
|
LOG(ERROR) << "Ignoring disallowed property '" << key
|
|
|
|
<< "' with special meaning in prop file '" << filename << "'";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ucred cr = {.pid = 1, .uid = 0, .gid = 0};
|
|
|
|
std::string error;
|
2019-02-16 21:03:19 +01:00
|
|
|
if (CheckPermissions(key, value, context, cr, &error) == PROP_SUCCESS) {
|
|
|
|
auto it = properties->find(key);
|
|
|
|
if (it == properties->end()) {
|
|
|
|
(*properties)[key] = value;
|
|
|
|
} else if (it->second != value) {
|
2020-06-11 12:41:54 +02:00
|
|
|
LOG(WARNING) << "Overriding previous property '" << key << "':'" << it->second
|
|
|
|
<< "' with new value '" << value << "'";
|
2019-02-16 21:03:19 +01:00
|
|
|
it->second = value;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "Do not have permissions to set '" << key << "' to '" << value
|
2018-02-28 19:39:01 +01:00
|
|
|
<< "' in property file '" << filename << "': " << error;
|
|
|
|
}
|
2014-03-06 23:26:36 +01:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-30 23:12:33 +02: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.
|
2019-02-16 21:03:19 +01:00
|
|
|
static bool load_properties_from_file(const char* filename, const char* filter,
|
|
|
|
std::map<std::string, std::string>* properties) {
|
2015-03-28 07:20:44 +01:00
|
|
|
Timer t;
|
2017-08-03 21:54:07 +02:00
|
|
|
auto file_contents = ReadFile(filename);
|
2020-02-05 19:49:33 +01:00
|
|
|
if (!file_contents.ok()) {
|
2017-08-03 21:54:07 +02:00
|
|
|
PLOG(WARNING) << "Couldn't load property file '" << filename
|
|
|
|
<< "': " << file_contents.error();
|
2017-06-02 12:59:46 +02:00
|
|
|
return false;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2017-08-03 21:54:07 +02:00
|
|
|
file_contents->push_back('\n');
|
2018-02-28 19:39:01 +01:00
|
|
|
|
2019-02-16 21:03:19 +01:00
|
|
|
LoadProperties(file_contents->data(), filter, filename, properties);
|
2016-11-29 20:20:58 +01:00
|
|
|
LOG(VERBOSE) << "(Loading properties from " << filename << " took " << t << ".)";
|
2017-06-02 12:59:46 +02:00
|
|
|
return true;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2020-10-07 01:58:19 +02:00
|
|
|
static void LoadPropertiesFromSecondStageRes(std::map<std::string, std::string>* properties) {
|
|
|
|
std::string prop = GetRamdiskPropForSecondStage();
|
|
|
|
if (access(prop.c_str(), R_OK) != 0) {
|
|
|
|
CHECK(errno == ENOENT) << "Cannot access " << prop << ": " << strerror(errno);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
load_properties_from_file(prop.c_str(), nullptr, properties);
|
|
|
|
}
|
|
|
|
|
2017-04-24 11:52:54 +02:00
|
|
|
// persist.sys.usb.config values can't be combined on build-time when property
|
|
|
|
// files are split into each partition.
|
|
|
|
// So we need to apply the same rule of build/make/tools/post_process_props.py
|
|
|
|
// on runtime.
|
|
|
|
static void update_sys_usb_config() {
|
|
|
|
bool is_debuggable = android::base::GetBoolProperty("ro.debuggable", false);
|
|
|
|
std::string config = android::base::GetProperty("persist.sys.usb.config", "");
|
2020-02-26 14:36:59 +01:00
|
|
|
// b/150130503, add (config == "none") condition here to prevent appending
|
|
|
|
// ",adb" if "none" is explicitly defined in default prop.
|
|
|
|
if (config.empty() || config == "none") {
|
2019-08-20 00:21:25 +02:00
|
|
|
InitPropertySet("persist.sys.usb.config", is_debuggable ? "adb" : "none");
|
2017-04-24 11:52:54 +02:00
|
|
|
} else if (is_debuggable && config.find("adb") == std::string::npos &&
|
|
|
|
config.length() + 4 < PROP_VALUE_MAX) {
|
|
|
|
config.append(",adb");
|
2019-08-20 00:21:25 +02:00
|
|
|
InitPropertySet("persist.sys.usb.config", config);
|
2017-04-24 11:52:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-19 20:15:24 +02:00
|
|
|
static void load_override_properties() {
|
2015-02-04 23:46:36 +01:00
|
|
|
if (ALLOW_LOCAL_PROP_OVERRIDE) {
|
2019-02-16 21:03:19 +01:00
|
|
|
std::map<std::string, std::string> properties;
|
|
|
|
load_properties_from_file("/data/local.prop", nullptr, &properties);
|
|
|
|
for (const auto& [name, value] : properties) {
|
|
|
|
std::string error;
|
|
|
|
if (PropertySet(name, value, &error) != PROP_SUCCESS) {
|
|
|
|
LOG(ERROR) << "Could not set '" << name << "' to '" << value
|
|
|
|
<< "' in /data/local.prop: " << error;
|
|
|
|
}
|
|
|
|
}
|
2012-09-19 20:15:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-30 05:19:05 +01:00
|
|
|
// If the ro.product.[brand|device|manufacturer|model|name] properties have not been explicitly
|
|
|
|
// set, derive them from ro.product.${partition}.* properties
|
|
|
|
static void property_initialize_ro_product_props() {
|
|
|
|
const char* RO_PRODUCT_PROPS_PREFIX = "ro.product.";
|
|
|
|
const char* RO_PRODUCT_PROPS[] = {
|
|
|
|
"brand", "device", "manufacturer", "model", "name",
|
|
|
|
};
|
|
|
|
const char* RO_PRODUCT_PROPS_ALLOWED_SOURCES[] = {
|
2019-06-28 07:28:00 +02:00
|
|
|
"odm", "product", "system_ext", "system", "vendor",
|
2019-01-30 05:19:05 +01:00
|
|
|
};
|
2019-06-28 07:28:00 +02:00
|
|
|
const char* RO_PRODUCT_PROPS_DEFAULT_SOURCE_ORDER = "product,odm,vendor,system_ext,system";
|
2019-01-30 05:19:05 +01:00
|
|
|
const std::string EMPTY = "";
|
|
|
|
|
|
|
|
std::string ro_product_props_source_order =
|
|
|
|
GetProperty("ro.product.property_source_order", EMPTY);
|
|
|
|
|
|
|
|
if (!ro_product_props_source_order.empty()) {
|
|
|
|
// Verify that all specified sources are valid
|
|
|
|
for (const auto& source : Split(ro_product_props_source_order, ",")) {
|
|
|
|
// Verify that the specified source is valid
|
|
|
|
bool is_allowed_source = false;
|
|
|
|
for (const auto& allowed_source : RO_PRODUCT_PROPS_ALLOWED_SOURCES) {
|
|
|
|
if (source == allowed_source) {
|
|
|
|
is_allowed_source = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!is_allowed_source) {
|
|
|
|
LOG(ERROR) << "Found unexpected source in ro.product.property_source_order; "
|
|
|
|
"using the default property source order";
|
|
|
|
ro_product_props_source_order = RO_PRODUCT_PROPS_DEFAULT_SOURCE_ORDER;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ro_product_props_source_order = RO_PRODUCT_PROPS_DEFAULT_SOURCE_ORDER;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& ro_product_prop : RO_PRODUCT_PROPS) {
|
|
|
|
std::string base_prop(RO_PRODUCT_PROPS_PREFIX);
|
|
|
|
base_prop += ro_product_prop;
|
|
|
|
|
|
|
|
std::string base_prop_val = GetProperty(base_prop, EMPTY);
|
|
|
|
if (!base_prop_val.empty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& source : Split(ro_product_props_source_order, ",")) {
|
|
|
|
std::string target_prop(RO_PRODUCT_PROPS_PREFIX);
|
|
|
|
target_prop += source;
|
|
|
|
target_prop += '.';
|
|
|
|
target_prop += ro_product_prop;
|
|
|
|
|
|
|
|
std::string target_prop_val = GetProperty(target_prop, EMPTY);
|
|
|
|
if (!target_prop_val.empty()) {
|
|
|
|
LOG(INFO) << "Setting product property " << base_prop << " to '" << target_prop_val
|
|
|
|
<< "' (from " << target_prop << ")";
|
|
|
|
std::string error;
|
|
|
|
uint32_t res = PropertySet(base_prop, target_prop_val, &error);
|
|
|
|
if (res != PROP_SUCCESS) {
|
|
|
|
LOG(ERROR) << "Error setting product property " << base_prop << ": err=" << res
|
|
|
|
<< " (" << error << ")";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-06 02:23:46 +02:00
|
|
|
static void property_initialize_build_id() {
|
|
|
|
std::string build_id = GetProperty(ID_PROP, "");
|
|
|
|
if (!build_id.empty()) {
|
2019-01-30 05:19:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-06 02:23:46 +02:00
|
|
|
std::string legacy_build_id = GetProperty(LEGACY_ID_PROP, "");
|
|
|
|
std::string vbmeta_digest = GetProperty(VBMETA_DIGEST_PROP, "");
|
|
|
|
if (vbmeta_digest.size() < DIGEST_SIZE_USED) {
|
|
|
|
LOG(ERROR) << "vbmeta digest size too small " << vbmeta_digest;
|
|
|
|
// Still try to set the id field in the unexpected case.
|
|
|
|
build_id = legacy_build_id;
|
|
|
|
} else {
|
|
|
|
// Derive the ro.build.id by appending the vbmeta digest to the base value.
|
|
|
|
build_id = legacy_build_id + "." + vbmeta_digest.substr(0, DIGEST_SIZE_USED);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string error;
|
|
|
|
auto res = PropertySet(ID_PROP, build_id, &error);
|
|
|
|
if (res != PROP_SUCCESS) {
|
|
|
|
LOG(ERROR) << "Failed to set " << ID_PROP << " to " << build_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string ConstructBuildFingerprint(bool legacy) {
|
2019-01-30 05:19:05 +01:00
|
|
|
const std::string UNKNOWN = "unknown";
|
2021-05-06 02:23:46 +02:00
|
|
|
std::string build_fingerprint = GetProperty("ro.product.brand", UNKNOWN);
|
2019-01-30 05:19:05 +01:00
|
|
|
build_fingerprint += '/';
|
|
|
|
build_fingerprint += GetProperty("ro.product.name", UNKNOWN);
|
|
|
|
build_fingerprint += '/';
|
|
|
|
build_fingerprint += GetProperty("ro.product.device", UNKNOWN);
|
|
|
|
build_fingerprint += ':';
|
2020-10-13 21:38:48 +02:00
|
|
|
build_fingerprint += GetProperty("ro.build.version.release_or_codename", UNKNOWN);
|
2019-01-30 05:19:05 +01:00
|
|
|
build_fingerprint += '/';
|
2021-05-06 02:23:46 +02:00
|
|
|
|
|
|
|
std::string build_id =
|
|
|
|
legacy ? GetProperty(LEGACY_ID_PROP, UNKNOWN) : GetProperty(ID_PROP, UNKNOWN);
|
|
|
|
build_fingerprint += build_id;
|
2019-01-30 05:19:05 +01:00
|
|
|
build_fingerprint += '/';
|
|
|
|
build_fingerprint += GetProperty("ro.build.version.incremental", UNKNOWN);
|
|
|
|
build_fingerprint += ':';
|
|
|
|
build_fingerprint += GetProperty("ro.build.type", UNKNOWN);
|
|
|
|
build_fingerprint += '/';
|
|
|
|
build_fingerprint += GetProperty("ro.build.tags", UNKNOWN);
|
|
|
|
|
2021-05-06 02:23:46 +02:00
|
|
|
return build_fingerprint;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Derive the legacy build fingerprint if we overwrite the build id at runtime.
|
|
|
|
static void property_derive_legacy_build_fingerprint() {
|
|
|
|
std::string legacy_build_fingerprint = GetProperty(LEGACY_FINGERPRINT_PROP, "");
|
|
|
|
if (!legacy_build_fingerprint.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The device doesn't have a legacy build id, skipping the legacy fingerprint.
|
|
|
|
std::string legacy_build_id = GetProperty(LEGACY_ID_PROP, "");
|
|
|
|
if (legacy_build_id.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
legacy_build_fingerprint = ConstructBuildFingerprint(true /* legacy fingerprint */);
|
|
|
|
LOG(INFO) << "Setting property '" << LEGACY_FINGERPRINT_PROP << "' to '"
|
|
|
|
<< legacy_build_fingerprint << "'";
|
|
|
|
|
|
|
|
std::string error;
|
|
|
|
uint32_t res = PropertySet(LEGACY_FINGERPRINT_PROP, legacy_build_fingerprint, &error);
|
|
|
|
if (res != PROP_SUCCESS) {
|
|
|
|
LOG(ERROR) << "Error setting property '" << LEGACY_FINGERPRINT_PROP << "': err=" << res
|
|
|
|
<< " (" << error << ")";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the ro.build.fingerprint property has not been set, derive it from constituent pieces
|
|
|
|
static void property_derive_build_fingerprint() {
|
|
|
|
std::string build_fingerprint = GetProperty("ro.build.fingerprint", "");
|
|
|
|
if (!build_fingerprint.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
build_fingerprint = ConstructBuildFingerprint(false /* legacy fingerprint */);
|
|
|
|
LOG(INFO) << "Setting property '" << FINGERPRINT_PROP << "' to '" << build_fingerprint << "'";
|
2019-01-30 05:19:05 +01:00
|
|
|
|
|
|
|
std::string error;
|
2021-05-06 02:23:46 +02:00
|
|
|
uint32_t res = PropertySet(FINGERPRINT_PROP, build_fingerprint, &error);
|
2019-01-30 05:19:05 +01:00
|
|
|
if (res != PROP_SUCCESS) {
|
2021-05-06 02:23:46 +02:00
|
|
|
LOG(ERROR) << "Error setting property '" << FINGERPRINT_PROP << "': err=" << res << " ("
|
|
|
|
<< error << ")";
|
2019-01-30 05:19:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-31 09:52:39 +01:00
|
|
|
// If the ro.product.cpu.abilist* properties have not been explicitly
|
|
|
|
// set, derive them from ro.${partition}.product.cpu.abilist* properties.
|
|
|
|
static void property_initialize_ro_cpu_abilist() {
|
|
|
|
// From high to low priority.
|
|
|
|
const char* kAbilistSources[] = {
|
|
|
|
"product",
|
|
|
|
"odm",
|
|
|
|
"vendor",
|
|
|
|
"system",
|
|
|
|
};
|
|
|
|
const std::string EMPTY = "";
|
|
|
|
const char* kAbilistProp = "ro.product.cpu.abilist";
|
|
|
|
const char* kAbilist32Prop = "ro.product.cpu.abilist32";
|
|
|
|
const char* kAbilist64Prop = "ro.product.cpu.abilist64";
|
|
|
|
|
|
|
|
// If the properties are defined explicitly, just use them.
|
|
|
|
if (GetProperty(kAbilistProp, EMPTY) != EMPTY) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the first source defining these properties by order.
|
|
|
|
std::string abilist32_prop_val;
|
|
|
|
std::string abilist64_prop_val;
|
|
|
|
for (const auto& source : kAbilistSources) {
|
|
|
|
const auto abilist32_prop = std::string("ro.") + source + ".product.cpu.abilist32";
|
|
|
|
const auto abilist64_prop = std::string("ro.") + source + ".product.cpu.abilist64";
|
|
|
|
abilist32_prop_val = GetProperty(abilist32_prop, EMPTY);
|
|
|
|
abilist64_prop_val = GetProperty(abilist64_prop, EMPTY);
|
|
|
|
// The properties could be empty on 32-bit-only or 64-bit-only devices,
|
|
|
|
// but we cannot identify a property is empty or undefined by GetProperty().
|
|
|
|
// So, we assume both of these 2 properties are empty as undefined.
|
|
|
|
if (abilist32_prop_val != EMPTY || abilist64_prop_val != EMPTY) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge ABI lists for ro.product.cpu.abilist
|
|
|
|
auto abilist_prop_val = abilist64_prop_val;
|
|
|
|
if (abilist32_prop_val != EMPTY) {
|
|
|
|
if (abilist_prop_val != EMPTY) {
|
|
|
|
abilist_prop_val += ",";
|
|
|
|
}
|
|
|
|
abilist_prop_val += abilist32_prop_val;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set these properties
|
|
|
|
const std::pair<const char*, const std::string&> set_prop_list[] = {
|
|
|
|
{kAbilistProp, abilist_prop_val},
|
|
|
|
{kAbilist32Prop, abilist32_prop_val},
|
|
|
|
{kAbilist64Prop, abilist64_prop_val},
|
|
|
|
};
|
|
|
|
for (const auto& [prop, prop_val] : set_prop_list) {
|
|
|
|
LOG(INFO) << "Setting property '" << prop << "' to '" << prop_val << "'";
|
|
|
|
|
|
|
|
std::string error;
|
|
|
|
uint32_t res = PropertySet(prop, prop_val, &error);
|
|
|
|
if (res != PROP_SUCCESS) {
|
|
|
|
LOG(ERROR) << "Error setting property '" << prop << "': err=" << res << " (" << error
|
|
|
|
<< ")";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-20 00:21:25 +02:00
|
|
|
void PropertyLoadBootDefaults() {
|
2019-02-16 21:03:19 +01:00
|
|
|
// We read the properties and their values into a map, in order to always allow properties
|
|
|
|
// loaded in the later property files to override the properties in loaded in the earlier
|
|
|
|
// property files, regardless of if they are "ro." properties or not.
|
|
|
|
std::map<std::string, std::string> properties;
|
2020-05-11 09:47:05 +02:00
|
|
|
|
|
|
|
if (IsRecoveryMode()) {
|
|
|
|
load_properties_from_file("/prop.default", nullptr, &properties);
|
2019-02-16 21:03:19 +01:00
|
|
|
}
|
2020-05-11 09:47:05 +02:00
|
|
|
|
2020-06-29 03:42:03 +02:00
|
|
|
// /<part>/etc/build.prop is the canonical location of the build-time properties since S.
|
|
|
|
// Falling back to /<part>/defalt.prop and /<part>/build.prop only when legacy path has to
|
|
|
|
// be supported, which is controlled by the support_legacy_path_until argument.
|
|
|
|
const auto load_properties_from_partition = [&properties](const std::string& partition,
|
|
|
|
int support_legacy_path_until) {
|
|
|
|
auto path = "/" + partition + "/etc/build.prop";
|
|
|
|
if (load_properties_from_file(path.c_str(), nullptr, &properties)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// To read ro.<partition>.build.version.sdk, temporarily load the legacy paths into a
|
|
|
|
// separate map. Then by comparing its value with legacy_version, we know that if the
|
|
|
|
// partition is old enough so that we need to respect the legacy paths.
|
|
|
|
std::map<std::string, std::string> temp;
|
|
|
|
auto legacy_path1 = "/" + partition + "/default.prop";
|
|
|
|
auto legacy_path2 = "/" + partition + "/build.prop";
|
|
|
|
load_properties_from_file(legacy_path1.c_str(), nullptr, &temp);
|
|
|
|
load_properties_from_file(legacy_path2.c_str(), nullptr, &temp);
|
|
|
|
bool support_legacy_path = false;
|
|
|
|
auto version_prop_name = "ro." + partition + ".build.version.sdk";
|
|
|
|
auto it = temp.find(version_prop_name);
|
|
|
|
if (it == temp.end()) {
|
|
|
|
// This is embarassing. Without the prop, we can't determine how old the partition is.
|
|
|
|
// Let's be conservative by assuming it is very very old.
|
|
|
|
support_legacy_path = true;
|
|
|
|
} else if (int value;
|
|
|
|
ParseInt(it->second.c_str(), &value) && value <= support_legacy_path_until) {
|
|
|
|
support_legacy_path = true;
|
|
|
|
}
|
|
|
|
if (support_legacy_path) {
|
|
|
|
// We don't update temp into properties directly as it might skip any (future) logic
|
|
|
|
// for resolving duplicates implemented in load_properties_from_file. Instead, read
|
|
|
|
// the files again into the properties map.
|
|
|
|
load_properties_from_file(legacy_path1.c_str(), nullptr, &properties);
|
|
|
|
load_properties_from_file(legacy_path2.c_str(), nullptr, &properties);
|
|
|
|
} else {
|
|
|
|
LOG(FATAL) << legacy_path1 << " and " << legacy_path2 << " were not loaded "
|
|
|
|
<< "because " << version_prop_name << "(" << it->second << ") is newer "
|
|
|
|
<< "than " << support_legacy_path_until;
|
|
|
|
}
|
|
|
|
};
|
2020-05-11 08:59:44 +02:00
|
|
|
|
2020-06-29 03:42:03 +02:00
|
|
|
// Order matters here. The more the partition is specific to a product, the higher its
|
|
|
|
// precedence is.
|
2020-10-07 01:58:19 +02:00
|
|
|
LoadPropertiesFromSecondStageRes(&properties);
|
2020-06-29 03:42:03 +02:00
|
|
|
load_properties_from_file("/system/build.prop", nullptr, &properties);
|
|
|
|
load_properties_from_partition("system_ext", /* support_legacy_path_until */ 30);
|
|
|
|
// TODO(b/117892318): uncomment the following condition when vendor.imgs for aosp_* targets are
|
|
|
|
// all updated.
|
|
|
|
// if (SelinuxGetVendorAndroidVersion() <= __ANDROID_API_R__) {
|
|
|
|
load_properties_from_file("/vendor/default.prop", nullptr, &properties);
|
|
|
|
// }
|
2019-02-16 21:03:19 +01:00
|
|
|
load_properties_from_file("/vendor/build.prop", nullptr, &properties);
|
2020-06-25 03:29:22 +02:00
|
|
|
load_properties_from_file("/vendor_dlkm/etc/build.prop", nullptr, &properties);
|
2020-07-16 02:04:43 +02:00
|
|
|
load_properties_from_file("/odm_dlkm/etc/build.prop", nullptr, &properties);
|
2020-06-29 03:42:03 +02:00
|
|
|
load_properties_from_partition("odm", /* support_legacy_path_until */ 28);
|
|
|
|
load_properties_from_partition("product", /* support_legacy_path_until */ 30);
|
2020-05-11 08:59:44 +02:00
|
|
|
|
2019-08-20 00:21:25 +02:00
|
|
|
if (access(kDebugRamdiskProp, R_OK) == 0) {
|
2019-04-11 17:57:24 +02:00
|
|
|
LOG(INFO) << "Loading " << kDebugRamdiskProp;
|
|
|
|
load_properties_from_file(kDebugRamdiskProp, nullptr, &properties);
|
2019-03-04 10:53:34 +01:00
|
|
|
}
|
|
|
|
|
2019-02-16 21:03:19 +01:00
|
|
|
for (const auto& [name, value] : properties) {
|
|
|
|
std::string error;
|
|
|
|
if (PropertySet(name, value, &error) != PROP_SUCCESS) {
|
|
|
|
LOG(ERROR) << "Could not set '" << name << "' to '" << value
|
|
|
|
<< "' while loading .prop files" << error;
|
2019-01-12 18:42:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-30 05:19:05 +01:00
|
|
|
property_initialize_ro_product_props();
|
2021-05-06 02:23:46 +02:00
|
|
|
property_initialize_build_id();
|
2019-01-30 05:19:05 +01:00
|
|
|
property_derive_build_fingerprint();
|
2021-05-06 02:23:46 +02:00
|
|
|
property_derive_legacy_build_fingerprint();
|
2020-12-31 09:52:39 +01:00
|
|
|
property_initialize_ro_cpu_abilist();
|
2019-01-30 05:19:05 +01:00
|
|
|
|
2019-01-12 18:42:31 +01:00
|
|
|
update_sys_usb_config();
|
2014-06-17 00:06:21 +02:00
|
|
|
}
|
|
|
|
|
2017-12-14 02:58:17 +01:00
|
|
|
bool LoadPropertyInfoFromFile(const std::string& filename,
|
|
|
|
std::vector<PropertyInfoEntry>* property_infos) {
|
|
|
|
auto file_contents = std::string();
|
|
|
|
if (!ReadFileToString(filename, &file_contents)) {
|
|
|
|
PLOG(ERROR) << "Could not read properties from '" << filename << "'";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-03 23:39:28 +01:00
|
|
|
auto errors = std::vector<std::string>{};
|
2019-12-11 16:56:51 +01:00
|
|
|
bool require_prefix_or_exact = SelinuxGetVendorAndroidVersion() >= __ANDROID_API_R__;
|
|
|
|
ParsePropertyInfoFile(file_contents, require_prefix_or_exact, property_infos, &errors);
|
2018-01-03 23:39:28 +01:00
|
|
|
// Individual parsing errors are reported but do not cause a failed boot, which is what
|
|
|
|
// returning false would do here.
|
|
|
|
for (const auto& error : errors) {
|
|
|
|
LOG(ERROR) << "Could not read line from '" << filename << "': " << error;
|
2017-12-14 02:58:17 +01:00
|
|
|
}
|
2018-01-03 23:39:28 +01:00
|
|
|
|
2017-12-14 02:58:17 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CreateSerializedPropertyInfo() {
|
|
|
|
auto property_infos = std::vector<PropertyInfoEntry>();
|
|
|
|
if (access("/system/etc/selinux/plat_property_contexts", R_OK) != -1) {
|
|
|
|
if (!LoadPropertyInfoFromFile("/system/etc/selinux/plat_property_contexts",
|
|
|
|
&property_infos)) {
|
|
|
|
return;
|
|
|
|
}
|
2020-07-27 20:20:29 +02:00
|
|
|
// Don't check for failure here, since we don't always have all of these partitions.
|
2017-12-14 02:58:17 +01:00
|
|
|
// E.g. In case of recovery, the vendor partition will not have mounted and we
|
|
|
|
// still need the system / platform properties to function.
|
2019-10-02 10:23:32 +02:00
|
|
|
if (access("/system_ext/etc/selinux/system_ext_property_contexts", R_OK) != -1) {
|
|
|
|
LoadPropertyInfoFromFile("/system_ext/etc/selinux/system_ext_property_contexts",
|
|
|
|
&property_infos);
|
|
|
|
}
|
2017-12-07 09:05:25 +01:00
|
|
|
if (!LoadPropertyInfoFromFile("/vendor/etc/selinux/vendor_property_contexts",
|
|
|
|
&property_infos)) {
|
|
|
|
// Fallback to nonplat_* if vendor_* doesn't exist.
|
|
|
|
LoadPropertyInfoFromFile("/vendor/etc/selinux/nonplat_property_contexts",
|
|
|
|
&property_infos);
|
|
|
|
}
|
2019-02-15 09:56:28 +01:00
|
|
|
if (access("/product/etc/selinux/product_property_contexts", R_OK) != -1) {
|
|
|
|
LoadPropertyInfoFromFile("/product/etc/selinux/product_property_contexts",
|
|
|
|
&property_infos);
|
|
|
|
}
|
|
|
|
if (access("/odm/etc/selinux/odm_property_contexts", R_OK) != -1) {
|
|
|
|
LoadPropertyInfoFromFile("/odm/etc/selinux/odm_property_contexts", &property_infos);
|
|
|
|
}
|
2017-12-14 02:58:17 +01:00
|
|
|
} else {
|
|
|
|
if (!LoadPropertyInfoFromFile("/plat_property_contexts", &property_infos)) {
|
|
|
|
return;
|
|
|
|
}
|
2019-10-02 10:23:32 +02:00
|
|
|
LoadPropertyInfoFromFile("/system_ext_property_contexts", &property_infos);
|
2017-12-07 09:05:25 +01:00
|
|
|
if (!LoadPropertyInfoFromFile("/vendor_property_contexts", &property_infos)) {
|
|
|
|
// Fallback to nonplat_* if vendor_* doesn't exist.
|
|
|
|
LoadPropertyInfoFromFile("/nonplat_property_contexts", &property_infos);
|
|
|
|
}
|
2019-02-15 09:56:28 +01:00
|
|
|
LoadPropertyInfoFromFile("/product_property_contexts", &property_infos);
|
|
|
|
LoadPropertyInfoFromFile("/odm_property_contexts", &property_infos);
|
2017-12-14 02:58:17 +01:00
|
|
|
}
|
2017-12-11 10:40:07 +01:00
|
|
|
|
2017-12-14 02:58:17 +01:00
|
|
|
auto serialized_contexts = std::string();
|
|
|
|
auto error = std::string();
|
2017-12-11 10:40:07 +01:00
|
|
|
if (!BuildTrie(property_infos, "u:object_r:default_prop:s0", "string", &serialized_contexts,
|
2017-12-14 02:58:17 +01:00
|
|
|
&error)) {
|
|
|
|
LOG(ERROR) << "Unable to serialize property contexts: " << error;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr static const char kPropertyInfosPath[] = "/dev/__properties__/property_info";
|
|
|
|
if (!WriteStringToFile(serialized_contexts, kPropertyInfosPath, 0444, 0, 0, false)) {
|
|
|
|
PLOG(ERROR) << "Unable to write serialized property infos to file";
|
|
|
|
}
|
|
|
|
selinux_android_restorecon(kPropertyInfosPath, 0);
|
|
|
|
}
|
|
|
|
|
2019-08-20 00:21:25 +02:00
|
|
|
static void ExportKernelBootProps() {
|
|
|
|
constexpr const char* UNSET = "";
|
|
|
|
struct {
|
|
|
|
const char* src_prop;
|
|
|
|
const char* dst_prop;
|
|
|
|
const char* default_value;
|
|
|
|
} prop_map[] = {
|
|
|
|
// clang-format off
|
|
|
|
{ "ro.boot.serialno", "ro.serialno", UNSET, },
|
|
|
|
{ "ro.boot.mode", "ro.bootmode", "unknown", },
|
|
|
|
{ "ro.boot.baseband", "ro.baseband", "unknown", },
|
|
|
|
{ "ro.boot.bootloader", "ro.bootloader", "unknown", },
|
|
|
|
{ "ro.boot.hardware", "ro.hardware", "unknown", },
|
|
|
|
{ "ro.boot.revision", "ro.revision", "0", },
|
|
|
|
// clang-format on
|
|
|
|
};
|
|
|
|
for (const auto& prop : prop_map) {
|
|
|
|
std::string value = GetProperty(prop.src_prop, prop.default_value);
|
|
|
|
if (value != UNSET) InitPropertySet(prop.dst_prop, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ProcessKernelDt() {
|
|
|
|
if (!is_android_dt_value_expected("compatible", "android,firmware")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<DIR, int (*)(DIR*)> dir(opendir(get_android_dt_dir().c_str()), closedir);
|
|
|
|
if (!dir) return;
|
|
|
|
|
|
|
|
std::string dt_file;
|
|
|
|
struct dirent* dp;
|
|
|
|
while ((dp = readdir(dir.get())) != NULL) {
|
|
|
|
if (dp->d_type != DT_REG || !strcmp(dp->d_name, "compatible") ||
|
|
|
|
!strcmp(dp->d_name, "name")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string file_name = get_android_dt_dir() + dp->d_name;
|
|
|
|
|
|
|
|
android::base::ReadFileToString(file_name, &dt_file);
|
|
|
|
std::replace(dt_file.begin(), dt_file.end(), ',', '.');
|
|
|
|
|
|
|
|
InitPropertySet("ro.boot."s + dp->d_name, dt_file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-09 22:20:36 +01:00
|
|
|
constexpr auto ANDROIDBOOT_PREFIX = "androidboot."sv;
|
|
|
|
|
2019-08-20 00:21:25 +02:00
|
|
|
static void ProcessKernelCmdline() {
|
|
|
|
ImportKernelCmdline([&](const std::string& key, const std::string& value) {
|
2021-03-10 03:19:23 +01:00
|
|
|
if (StartsWith(key, ANDROIDBOOT_PREFIX)) {
|
2021-03-09 22:20:36 +01:00
|
|
|
InitPropertySet("ro.boot." + key.substr(ANDROIDBOOT_PREFIX.size()), value);
|
2019-08-20 00:21:25 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-29 00:15:44 +02:00
|
|
|
|
2020-12-16 01:14:37 +01:00
|
|
|
static void ProcessBootconfig() {
|
|
|
|
ImportBootconfig([&](const std::string& key, const std::string& value) {
|
2021-03-09 22:20:36 +01:00
|
|
|
if (StartsWith(key, ANDROIDBOOT_PREFIX)) {
|
|
|
|
InitPropertySet("ro.boot." + key.substr(ANDROIDBOOT_PREFIX.size()), value);
|
2020-12-16 01:14:37 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-08-20 00:21:25 +02:00
|
|
|
void PropertyInit() {
|
|
|
|
selinux_callback cb;
|
|
|
|
cb.func_audit = PropertyAuditCallback;
|
|
|
|
selinux_set_callback(SELINUX_CB_AUDIT, cb);
|
|
|
|
|
|
|
|
mkdir("/dev/__properties__", S_IRWXU | S_IXGRP | S_IXOTH);
|
|
|
|
CreateSerializedPropertyInfo();
|
|
|
|
if (__system_property_area_init()) {
|
|
|
|
LOG(FATAL) << "Failed to initialize property area";
|
|
|
|
}
|
|
|
|
if (!property_info_area.LoadDefaultPath()) {
|
|
|
|
LOG(FATAL) << "Failed to load serialized property info file";
|
|
|
|
}
|
|
|
|
|
|
|
|
// If arguments are passed both on the command line and in DT,
|
|
|
|
// properties set in DT always have priority over the command-line ones.
|
|
|
|
ProcessKernelDt();
|
|
|
|
ProcessKernelCmdline();
|
2020-12-16 01:14:37 +01:00
|
|
|
ProcessBootconfig();
|
2019-08-20 00:21:25 +02:00
|
|
|
|
|
|
|
// Propagate the kernel variables to internal variables
|
|
|
|
// used by init as well as the current required properties.
|
|
|
|
ExportKernelBootProps();
|
|
|
|
|
|
|
|
PropertyLoadBootDefaults();
|
|
|
|
}
|
|
|
|
|
2019-04-23 02:46:37 +02:00
|
|
|
static void HandleInitSocket() {
|
|
|
|
auto message = ReadMessage(init_socket);
|
2020-02-05 19:49:33 +01:00
|
|
|
if (!message.ok()) {
|
2019-04-23 02:46:37 +02:00
|
|
|
LOG(ERROR) << "Could not read message from init_dedicated_recv_socket: " << message.error();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto init_message = InitMessage{};
|
|
|
|
if (!init_message.ParseFromString(*message)) {
|
|
|
|
LOG(ERROR) << "Could not parse message from init";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (init_message.msg_case()) {
|
|
|
|
case InitMessage::kLoadPersistentProperties: {
|
|
|
|
load_override_properties();
|
|
|
|
// Read persistent properties after all default values have been loaded.
|
|
|
|
auto persistent_properties = LoadPersistentProperties();
|
|
|
|
for (const auto& persistent_property_record : persistent_properties.properties()) {
|
|
|
|
InitPropertySet(persistent_property_record.name(),
|
|
|
|
persistent_property_record.value());
|
|
|
|
}
|
|
|
|
InitPropertySet("ro.persistent_properties.ready", "true");
|
|
|
|
persistent_properties_loaded = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
LOG(ERROR) << "Unknown message type from init: " << init_message.msg_case();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void PropertyServiceThread() {
|
|
|
|
Epoll epoll;
|
2020-02-05 19:49:33 +01:00
|
|
|
if (auto result = epoll.Open(); !result.ok()) {
|
2019-04-23 02:46:37 +02:00
|
|
|
LOG(FATAL) << result.error();
|
|
|
|
}
|
|
|
|
|
2020-02-05 19:49:33 +01:00
|
|
|
if (auto result = epoll.RegisterHandler(property_set_fd, handle_property_set_fd);
|
|
|
|
!result.ok()) {
|
2019-04-23 02:46:37 +02:00
|
|
|
LOG(FATAL) << result.error();
|
|
|
|
}
|
|
|
|
|
2020-02-05 19:49:33 +01:00
|
|
|
if (auto result = epoll.RegisterHandler(init_socket, HandleInitSocket); !result.ok()) {
|
2019-04-23 02:46:37 +02:00
|
|
|
LOG(FATAL) << result.error();
|
|
|
|
}
|
|
|
|
|
2020-03-10 19:47:24 +01:00
|
|
|
while (true) {
|
2019-04-23 02:46:37 +02:00
|
|
|
auto pending_functions = epoll.Wait(std::nullopt);
|
2020-02-05 19:49:33 +01:00
|
|
|
if (!pending_functions.ok()) {
|
2019-04-23 02:46:37 +02:00
|
|
|
LOG(ERROR) << pending_functions.error();
|
|
|
|
} else {
|
|
|
|
for (const auto& function : *pending_functions) {
|
|
|
|
(*function)();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StartPropertyService(int* epoll_socket) {
|
2019-08-20 00:21:25 +02:00
|
|
|
InitPropertySet("ro.property_service.version", "2");
|
2017-01-24 21:43:58 +01:00
|
|
|
|
2019-04-23 02:46:37 +02:00
|
|
|
int sockets[2];
|
|
|
|
if (socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sockets) != 0) {
|
|
|
|
PLOG(FATAL) << "Failed to socketpair() between property_service and init";
|
|
|
|
}
|
init: handle property messages asynchronously #2
A previous change moved property_service into its own thread, since
there was otherwise a deadlock whenever a process called by init would
try to set a property. This new thread, however, would send a message
via a blocking socket to init for each property that it received,
since init may need to take action depending on which property it is.
Unfortunately, this means that the deadlock is still possible, the
only difference is the socket's buffer must be filled before init deadlocks.
This change, therefore, adds the following:
1) A lock for instructing init to reboot
2) A lock for waiting on properties
3) A lock for queueing new properties
A previous version of this change was reverted and added locks around
all service operations and allowed the property thread to spawn
services directly. This was complex due to the fact that this code
was not designed to be multi-threaded. It was reverted due to
apparent issues during reboot. This change keeps a queue of processes
pending control messages, which it will then handle in the future. It
is less flexible but safer.
Bug: 146877356
Bug: 148236233
Bug: 150863651
Bug: 151251827
Test: multiple reboot tests, safely restarting hwservicemanager
Change-Id: Ice773436e85d3bf636bb0a892f3f6002bdf996b6
2020-03-12 22:29:25 +01:00
|
|
|
*epoll_socket = from_init_socket = sockets[0];
|
2019-04-23 02:46:37 +02:00
|
|
|
init_socket = sockets[1];
|
init: handle property messages asynchronously #2
A previous change moved property_service into its own thread, since
there was otherwise a deadlock whenever a process called by init would
try to set a property. This new thread, however, would send a message
via a blocking socket to init for each property that it received,
since init may need to take action depending on which property it is.
Unfortunately, this means that the deadlock is still possible, the
only difference is the socket's buffer must be filled before init deadlocks.
This change, therefore, adds the following:
1) A lock for instructing init to reboot
2) A lock for waiting on properties
3) A lock for queueing new properties
A previous version of this change was reverted and added locks around
all service operations and allowed the property thread to spawn
services directly. This was complex due to the fact that this code
was not designed to be multi-threaded. It was reverted due to
apparent issues during reboot. This change keeps a queue of processes
pending control messages, which it will then handle in the future. It
is less flexible but safer.
Bug: 146877356
Bug: 148236233
Bug: 150863651
Bug: 151251827
Test: multiple reboot tests, safely restarting hwservicemanager
Change-Id: Ice773436e85d3bf636bb0a892f3f6002bdf996b6
2020-03-12 22:29:25 +01:00
|
|
|
StartSendingMessages();
|
2019-04-23 02:46:37 +02:00
|
|
|
|
2019-08-28 19:47:49 +02:00
|
|
|
if (auto result = CreateSocket(PROP_SERVICE_NAME, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
|
2020-02-05 19:49:33 +01:00
|
|
|
false, 0666, 0, 0, {});
|
|
|
|
result.ok()) {
|
2019-07-09 22:33:36 +02:00
|
|
|
property_set_fd = *result;
|
|
|
|
} else {
|
2019-04-23 02:46:37 +02:00
|
|
|
LOG(FATAL) << "start_property_service socket creation failed: " << result.error();
|
2015-04-25 03:50:30 +02:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-04-25 03:50:30 +02:00
|
|
|
listen(property_set_fd, 8);
|
2010-04-14 04:33:37 +02:00
|
|
|
|
init: handle property messages asynchronously #2
A previous change moved property_service into its own thread, since
there was otherwise a deadlock whenever a process called by init would
try to set a property. This new thread, however, would send a message
via a blocking socket to init for each property that it received,
since init may need to take action depending on which property it is.
Unfortunately, this means that the deadlock is still possible, the
only difference is the socket's buffer must be filled before init deadlocks.
This change, therefore, adds the following:
1) A lock for instructing init to reboot
2) A lock for waiting on properties
3) A lock for queueing new properties
A previous version of this change was reverted and added locks around
all service operations and allowed the property thread to spawn
services directly. This was complex due to the fact that this code
was not designed to be multi-threaded. It was reverted due to
apparent issues during reboot. This change keeps a queue of processes
pending control messages, which it will then handle in the future. It
is less flexible but safer.
Bug: 146877356
Bug: 148236233
Bug: 150863651
Bug: 151251827
Test: multiple reboot tests, safely restarting hwservicemanager
Change-Id: Ice773436e85d3bf636bb0a892f3f6002bdf996b6
2020-03-12 22:29:25 +01:00
|
|
|
auto new_thread = std::thread{PropertyServiceThread};
|
|
|
|
property_service_thread.swap(new_thread);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2017-06-22 21:53:17 +02:00
|
|
|
|
|
|
|
} // namespace init
|
|
|
|
} // namespace android
|