mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-10-15 15:34:56 +00:00
Small native code reorganization
This commit is contained in:
@@ -10,6 +10,8 @@ LOCAL_SRC_FILES := \
|
||||
prop_area.cpp \
|
||||
prop_info.cpp \
|
||||
system_properties.cpp \
|
||||
system_property_api.cpp \
|
||||
system_property_set.cpp \
|
||||
property_info_parser.cpp
|
||||
|
||||
include $(BUILD_STATIC_LIBRARY)
|
||||
|
144
native/jni/systemproperties/include/_system_properties.h
Normal file
144
native/jni/systemproperties/include/_system_properties.h
Normal file
@@ -0,0 +1,144 @@
|
||||
/*
|
||||
* Copyright (C) 2008 The Android Open Source Project
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef _INCLUDE_SYS__SYSTEM_PROPERTIES_H
|
||||
#define _INCLUDE_SYS__SYSTEM_PROPERTIES_H
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifndef _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
|
||||
#error you should #include <sys/system_properties.h> instead
|
||||
#endif
|
||||
|
||||
//#include <sys/system_properties.h>
|
||||
#include "system_properties.h"
|
||||
|
||||
__BEGIN_DECLS
|
||||
|
||||
#define PROP_SERVICE_NAME "property_service"
|
||||
#define PROP_FILENAME "/dev/__properties__"
|
||||
|
||||
#define PROP_MSG_SETPROP 1
|
||||
#define PROP_MSG_SETPROP2 0x00020001
|
||||
|
||||
#define PROP_SUCCESS 0
|
||||
#define PROP_ERROR_READ_CMD 0x0004
|
||||
#define PROP_ERROR_READ_DATA 0x0008
|
||||
#define PROP_ERROR_READ_ONLY_PROPERTY 0x000B
|
||||
#define PROP_ERROR_INVALID_NAME 0x0010
|
||||
#define PROP_ERROR_INVALID_VALUE 0x0014
|
||||
#define PROP_ERROR_PERMISSION_DENIED 0x0018
|
||||
#define PROP_ERROR_INVALID_CMD 0x001B
|
||||
#define PROP_ERROR_HANDLE_CONTROL_MESSAGE 0x0020
|
||||
#define PROP_ERROR_SET_FAILED 0x0024
|
||||
|
||||
/*
|
||||
** This was previously for testing, but now that SystemProperties is its own testable class,
|
||||
** there is never a reason to call this function and its implementation simply returns -1.
|
||||
*/
|
||||
int __system_property_set_filename(const char* __filename);
|
||||
|
||||
/*
|
||||
** Initialize the area to be used to store properties. Can
|
||||
** only be done by a single process that has write access to
|
||||
** the property area.
|
||||
*/
|
||||
int __system_property_area_init(void);
|
||||
|
||||
/* Read the global serial number of the system properties
|
||||
**
|
||||
** Called to predict if a series of cached __system_property_find
|
||||
** objects will have seen __system_property_serial values change.
|
||||
** But also aids the converse, as changes in the global serial can
|
||||
** also be used to predict if a failed __system_property_find
|
||||
** could in-turn now find a new object; thus preventing the
|
||||
** cycles of effort to poll __system_property_find.
|
||||
**
|
||||
** Typically called at beginning of a cache cycle to signal if _any_ possible
|
||||
** changes have occurred since last. If there is, one may check each individual
|
||||
** __system_property_serial to confirm dirty, or __system_property_find
|
||||
** to check if the property now exists. If a call to __system_property_add
|
||||
** or __system_property_update has completed between two calls to
|
||||
** __system_property_area_serial then the second call will return a larger
|
||||
** value than the first call. Beware of race conditions as changes to the
|
||||
** properties are not atomic, the main value of this call is to determine
|
||||
** whether the expensive __system_property_find is worth retrying to see if
|
||||
** a property now exists.
|
||||
**
|
||||
** Returns the serial number on success, -1 on error.
|
||||
*/
|
||||
uint32_t __system_property_area_serial(void);
|
||||
|
||||
/* Add a new system property. Can only be done by a single
|
||||
** process that has write access to the property area, and
|
||||
** that process must handle sequencing to ensure the property
|
||||
** does not already exist and that only one property is added
|
||||
** or updated at a time.
|
||||
**
|
||||
** Returns 0 on success, -1 if the property area is full.
|
||||
*/
|
||||
int __system_property_add(const char* __name, unsigned int __name_length, const char* __value, unsigned int __value_length);
|
||||
|
||||
/* Delete a system property. Added in resetprop
|
||||
**
|
||||
** Returns 0 on success, -1 if the property area is full.
|
||||
*/
|
||||
int __system_property_del(const char *__name);
|
||||
|
||||
/* Update the value of a system property returned by
|
||||
** __system_property_find. Can only be done by a single process
|
||||
** that has write access to the property area, and that process
|
||||
** must handle sequencing to ensure that only one property is
|
||||
** updated at a time.
|
||||
**
|
||||
** Returns 0 on success, -1 if the parameters are incorrect.
|
||||
*/
|
||||
int __system_property_update(prop_info* __pi, const char* __value, unsigned int __value_length);
|
||||
|
||||
/* Read the serial number of a system property returned by
|
||||
** __system_property_find.
|
||||
**
|
||||
** Returns the serial number on success, -1 on error.
|
||||
*/
|
||||
uint32_t __system_property_serial(const prop_info* __pi);
|
||||
|
||||
/* Initialize the system properties area in read only mode.
|
||||
* Should be done by all processes that need to read system
|
||||
* properties.
|
||||
*
|
||||
* Returns 0 on success, -1 otherwise.
|
||||
*/
|
||||
int __system_properties_init(void);
|
||||
|
||||
/* Deprecated: use __system_property_wait instead. */
|
||||
uint32_t __system_property_wait_any(uint32_t __old_serial);
|
||||
|
||||
__END_DECLS
|
||||
|
||||
#endif
|
@@ -1,14 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdio.h>
|
||||
#include <syscall.h>
|
||||
|
||||
#ifndef PR_SET_VMA
|
||||
#define PR_SET_VMA 0x53564d41
|
||||
#endif
|
||||
#ifndef PR_SET_VMA_ANON_NAME
|
||||
#define PR_SET_VMA_ANON_NAME 0
|
||||
#endif
|
||||
#define getline __getline
|
||||
#define fsetxattr(...) syscall(__NR_fsetxattr, __VA_ARGS__)
|
||||
ssize_t __getline(char **, size_t *, FILE *);
|
39
native/jni/systemproperties/include/private/bionic_defs.h
Normal file
39
native/jni/systemproperties/include/private/bionic_defs.h
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright (C) 2017 The Android Open Source Project
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef __BIONIC_PRIVATE_BIONIC_DEFS_H_
|
||||
#define __BIONIC_PRIVATE_BIONIC_DEFS_H_
|
||||
|
||||
/*
|
||||
* This label is used to mark libc/libdl symbols that may need to be replaced
|
||||
* by native bridge implementation.
|
||||
*/
|
||||
#define __BIONIC_WEAK_FOR_NATIVE_BRIDGE __attribute__((__weak__, __noinline__))
|
||||
#define __BIONIC_WEAK_VARIABLE_FOR_NATIVE_BRIDGE __attribute__((__weak__))
|
||||
|
||||
#endif /* __BIONIC_PRIVATE_BIONIC_DEFS_H_ */
|
35
native/jni/systemproperties/include/private/hacks.h
Normal file
35
native/jni/systemproperties/include/private/hacks.h
Normal file
@@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdio.h>
|
||||
#include <syscall.h>
|
||||
|
||||
// Missing defines
|
||||
#ifndef PR_SET_VMA
|
||||
#define PR_SET_VMA 0x53564d41
|
||||
#endif
|
||||
#ifndef PR_SET_VMA_ANON_NAME
|
||||
#define PR_SET_VMA_ANON_NAME 0
|
||||
#endif
|
||||
|
||||
// Missing functions
|
||||
#define fsetxattr(...) syscall(__NR_fsetxattr, __VA_ARGS__)
|
||||
#define getline __getline
|
||||
ssize_t __getline(char **, size_t *, FILE *);
|
||||
|
||||
// Rename symbols
|
||||
#pragma redefine_extname __system_property_set _system_property_set2
|
||||
#pragma redefine_extname __system_property_find _system_property_find2
|
||||
#pragma redefine_extname __system_property_read_callback _system_property_read_callback2
|
||||
#pragma redefine_extname __system_property_foreach __system_property_foreach2
|
||||
#pragma redefine_extname __system_property_wait __system_property_wait2
|
||||
#pragma redefine_extname __system_property_read __system_property_read2
|
||||
#pragma redefine_extname __system_property_get __system_property_get2
|
||||
#pragma redefine_extname __system_property_find_nth __system_property_find_nth2
|
||||
#pragma redefine_extname __system_property_set_filename __system_property_set_filename2
|
||||
#pragma redefine_extname __system_property_area_init __system_property_area_init2
|
||||
#pragma redefine_extname __system_property_area_serial __system_property_area_serial2
|
||||
#pragma redefine_extname __system_property_add __system_property_add2
|
||||
#pragma redefine_extname __system_property_update __system_property_update2
|
||||
#pragma redefine_extname __system_property_serial __system_property_serial2
|
||||
#pragma redefine_extname __system_properties_init __system_properties_init2
|
||||
#pragma redefine_extname __system_property_wait_any __system_property_wait_any2
|
101
native/jni/systemproperties/include/system_properties.h
Normal file
101
native/jni/systemproperties/include/system_properties.h
Normal file
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* Copyright (C) 2008 The Android Open Source Project
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef _INCLUDE_SYS_SYSTEM_PROPERTIES_H
|
||||
#define _INCLUDE_SYS_SYSTEM_PROPERTIES_H
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "private/hacks.h"
|
||||
|
||||
__BEGIN_DECLS
|
||||
|
||||
typedef struct prop_info prop_info;
|
||||
|
||||
#define PROP_VALUE_MAX 92
|
||||
|
||||
/*
|
||||
* Sets system property `name` to `value`, creating the system property if it doesn't already exist.
|
||||
*/
|
||||
int __system_property_set(const char* __name, const char* __value) __INTRODUCED_IN(12);
|
||||
|
||||
/*
|
||||
* Returns a `prop_info` corresponding system property `name`, or nullptr if it doesn't exist.
|
||||
* Use __system_property_read_callback to query the current value.
|
||||
*
|
||||
* Property lookup is expensive, so it can be useful to cache the result of this function.
|
||||
*/
|
||||
const prop_info* __system_property_find(const char* __name);
|
||||
|
||||
/*
|
||||
* Calls `callback` with a consistent trio of name, value, and serial number for property `pi`.
|
||||
*/
|
||||
void __system_property_read_callback(const prop_info* __pi,
|
||||
void (*__callback)(void* __cookie, const char* __name, const char* __value, uint32_t __serial),
|
||||
void* __cookie) __INTRODUCED_IN(26);
|
||||
|
||||
/*
|
||||
* Passes a `prop_info` for each system property to the provided
|
||||
* callback. Use __system_property_read_callback() to read the value.
|
||||
*
|
||||
* This method is for inspecting and debugging the property system, and not generally useful.
|
||||
*/
|
||||
int __system_property_foreach(void (*__callback)(const prop_info* __pi, void* __cookie), void* __cookie)
|
||||
__INTRODUCED_IN(19);
|
||||
|
||||
/*
|
||||
* Waits for the specific system property identified by `pi` to be updated
|
||||
* past `old_serial`. Waits no longer than `relative_timeout`, or forever
|
||||
* if `relaive_timeout` is null.
|
||||
*
|
||||
* If `pi` is null, waits for the global serial number instead.
|
||||
*
|
||||
* If you don't know the current serial, use 0.
|
||||
*
|
||||
* Returns true and updates `*new_serial_ptr` on success, or false if the call
|
||||
* timed out.
|
||||
*/
|
||||
struct timespec;
|
||||
bool __system_property_wait(const prop_info* __pi, uint32_t __old_serial, uint32_t* __new_serial_ptr, const struct timespec* __relative_timeout)
|
||||
__INTRODUCED_IN(26);
|
||||
|
||||
/* Deprecated. In Android O and above, there's no limit on property name length. */
|
||||
#define PROP_NAME_MAX 32
|
||||
/* Deprecated. Use __system_property_read_callback instead. */
|
||||
int __system_property_read(const prop_info* __pi, char* __name, char* __value);
|
||||
/* Deprecated. Use __system_property_read_callback instead. */
|
||||
int __system_property_get(const char* __name, char* __value);
|
||||
/* Deprecated. Use __system_property_foreach instead. */
|
||||
const prop_info* __system_property_find_nth(unsigned __n);
|
||||
|
||||
__END_DECLS
|
||||
|
||||
#endif
|
@@ -30,10 +30,10 @@
|
||||
|
||||
#include <stdatomic.h>
|
||||
#include <stdint.h>
|
||||
#include <sys/system_properties.h>
|
||||
//#include <sys/system_properties.h>
|
||||
#include "../system_properties.h"
|
||||
|
||||
#include "private/bionic_macros.h"
|
||||
#include "hacks.h"
|
||||
|
||||
// The C11 standard doesn't allow atomic loads from const fields,
|
||||
// though C++11 does. Fudge it until standards get straightened out.
|
||||
|
@@ -30,7 +30,8 @@
|
||||
|
||||
#include <stdint.h>
|
||||
#include <sys/param.h>
|
||||
#include <sys/system_properties.h>
|
||||
//#include <sys/system_properties.h>
|
||||
#include "../system_properties.h"
|
||||
|
||||
#include "contexts.h"
|
||||
#include "contexts_pre_split.h"
|
||||
|
131
native/jni/systemproperties/system_property_api.cpp
Normal file
131
native/jni/systemproperties/system_property_api.cpp
Normal file
@@ -0,0 +1,131 @@
|
||||
/*
|
||||
* Copyright (C) 2017 The Android Open Source Project
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
|
||||
//#include <sys/_system_properties.h>
|
||||
#include <_system_properties.h>
|
||||
|
||||
#include <system_properties/prop_area.h>
|
||||
#include <system_properties/system_properties.h>
|
||||
|
||||
#include "private/bionic_defs.h"
|
||||
|
||||
static SystemProperties system_properties;
|
||||
static_assert(__is_trivially_constructible(SystemProperties),
|
||||
"System Properties must be trivially constructable");
|
||||
|
||||
// This is public because it was exposed in the NDK. As of 2017-01, ~60 apps reference this symbol.
|
||||
// It is set to nullptr and never modified.
|
||||
__BIONIC_WEAK_VARIABLE_FOR_NATIVE_BRIDGE
|
||||
prop_area* __system_property_area__ = nullptr;
|
||||
|
||||
__BIONIC_WEAK_FOR_NATIVE_BRIDGE
|
||||
int __system_properties_init() {
|
||||
return system_properties.Init(PROP_FILENAME) ? 0 : -1;
|
||||
}
|
||||
|
||||
__BIONIC_WEAK_FOR_NATIVE_BRIDGE
|
||||
int __system_property_set_filename(const char*) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
__BIONIC_WEAK_FOR_NATIVE_BRIDGE
|
||||
int __system_property_area_init() {
|
||||
bool fsetxattr_failed = false;
|
||||
return system_properties.AreaInit(PROP_FILENAME, &fsetxattr_failed) && !fsetxattr_failed ? 0 : -1;
|
||||
}
|
||||
|
||||
__BIONIC_WEAK_FOR_NATIVE_BRIDGE
|
||||
uint32_t __system_property_area_serial() {
|
||||
return system_properties.AreaSerial();
|
||||
}
|
||||
|
||||
__BIONIC_WEAK_FOR_NATIVE_BRIDGE
|
||||
const prop_info* __system_property_find(const char* name) {
|
||||
return system_properties.Find(name);
|
||||
}
|
||||
|
||||
__BIONIC_WEAK_FOR_NATIVE_BRIDGE
|
||||
int __system_property_read(const prop_info* pi, char* name, char* value) {
|
||||
return system_properties.Read(pi, name, value);
|
||||
}
|
||||
|
||||
__BIONIC_WEAK_FOR_NATIVE_BRIDGE
|
||||
void __system_property_read_callback(const prop_info* pi,
|
||||
void (*callback)(void* cookie, const char* name,
|
||||
const char* value, uint32_t serial),
|
||||
void* cookie) {
|
||||
return system_properties.ReadCallback(pi, callback, cookie);
|
||||
}
|
||||
|
||||
__BIONIC_WEAK_FOR_NATIVE_BRIDGE
|
||||
int __system_property_get(const char* name, char* value) {
|
||||
return system_properties.Get(name, value);
|
||||
}
|
||||
|
||||
__BIONIC_WEAK_FOR_NATIVE_BRIDGE
|
||||
int __system_property_update(prop_info* pi, const char* value, unsigned int len) {
|
||||
return system_properties.Update(pi, value, len);
|
||||
}
|
||||
|
||||
__BIONIC_WEAK_FOR_NATIVE_BRIDGE
|
||||
int __system_property_add(const char* name, unsigned int namelen, const char* value,
|
||||
unsigned int valuelen) {
|
||||
return system_properties.Add(name, namelen, value, valuelen);
|
||||
}
|
||||
|
||||
__BIONIC_WEAK_FOR_NATIVE_BRIDGE
|
||||
int __system_property_del(const char* name) {
|
||||
return system_properties.Delete(name);
|
||||
}
|
||||
|
||||
__BIONIC_WEAK_FOR_NATIVE_BRIDGE
|
||||
uint32_t __system_property_serial(const prop_info* pi) {
|
||||
return system_properties.Serial(pi);
|
||||
}
|
||||
|
||||
__BIONIC_WEAK_FOR_NATIVE_BRIDGE
|
||||
uint32_t __system_property_wait_any(uint32_t old_serial) {
|
||||
return system_properties.WaitAny(old_serial);
|
||||
}
|
||||
|
||||
__BIONIC_WEAK_FOR_NATIVE_BRIDGE
|
||||
bool __system_property_wait(const prop_info* pi, uint32_t old_serial, uint32_t* new_serial_ptr,
|
||||
const timespec* relative_timeout) {
|
||||
return system_properties.Wait(pi, old_serial, new_serial_ptr, relative_timeout);
|
||||
}
|
||||
|
||||
__BIONIC_WEAK_FOR_NATIVE_BRIDGE
|
||||
const prop_info* __system_property_find_nth(unsigned n) {
|
||||
return system_properties.FindNth(n);
|
||||
}
|
||||
|
||||
__BIONIC_WEAK_FOR_NATIVE_BRIDGE
|
||||
int __system_property_foreach(void (*propfn)(const prop_info* pi, void* cookie), void* cookie) {
|
||||
return system_properties.Foreach(propfn, cookie);
|
||||
}
|
317
native/jni/systemproperties/system_property_set.cpp
Normal file
317
native/jni/systemproperties/system_property_set.cpp
Normal file
@@ -0,0 +1,317 @@
|
||||
/*
|
||||
* Copyright (C) 2017 The Android Open Source Project
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <poll.h>
|
||||
#include <stdatomic.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/uio.h>
|
||||
#include <sys/un.h>
|
||||
#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
|
||||
//#include <sys/_system_properties.h>
|
||||
#include <_system_properties.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <async_safe/log.h>
|
||||
|
||||
#include "private/bionic_defs.h"
|
||||
#include "private/bionic_macros.h"
|
||||
|
||||
static const char property_service_socket[] = "/dev/socket/" PROP_SERVICE_NAME;
|
||||
static const char* kServiceVersionPropertyName = "ro.property_service.version";
|
||||
|
||||
#define CHECK(x) /* NOP */
|
||||
|
||||
class PropertyServiceConnection {
|
||||
public:
|
||||
PropertyServiceConnection() : last_error_(0) {
|
||||
socket_ = ::socket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
|
||||
if (socket_ == -1) {
|
||||
last_error_ = errno;
|
||||
return;
|
||||
}
|
||||
|
||||
const size_t namelen = strlen(property_service_socket);
|
||||
sockaddr_un addr;
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
strlcpy(addr.sun_path, property_service_socket, sizeof(addr.sun_path));
|
||||
addr.sun_family = AF_LOCAL;
|
||||
socklen_t alen = namelen + offsetof(sockaddr_un, sun_path) + 1;
|
||||
|
||||
if (TEMP_FAILURE_RETRY(connect(socket_, reinterpret_cast<sockaddr*>(&addr), alen)) == -1) {
|
||||
last_error_ = errno;
|
||||
close(socket_);
|
||||
socket_ = -1;
|
||||
}
|
||||
}
|
||||
|
||||
bool IsValid() {
|
||||
return socket_ != -1;
|
||||
}
|
||||
|
||||
int GetLastError() {
|
||||
return last_error_;
|
||||
}
|
||||
|
||||
bool RecvInt32(int32_t* value) {
|
||||
int result = TEMP_FAILURE_RETRY(recv(socket_, value, sizeof(*value), MSG_WAITALL));
|
||||
return CheckSendRecvResult(result, sizeof(*value));
|
||||
}
|
||||
|
||||
int socket() {
|
||||
return socket_;
|
||||
}
|
||||
|
||||
~PropertyServiceConnection() {
|
||||
if (socket_ != -1) {
|
||||
close(socket_);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
bool CheckSendRecvResult(int result, int expected_len) {
|
||||
if (result == -1) {
|
||||
last_error_ = errno;
|
||||
} else if (result != expected_len) {
|
||||
last_error_ = -1;
|
||||
} else {
|
||||
last_error_ = 0;
|
||||
}
|
||||
|
||||
return last_error_ == 0;
|
||||
}
|
||||
|
||||
int socket_;
|
||||
int last_error_;
|
||||
|
||||
friend class SocketWriter;
|
||||
};
|
||||
|
||||
class SocketWriter {
|
||||
public:
|
||||
explicit SocketWriter(PropertyServiceConnection* connection)
|
||||
: connection_(connection), iov_index_(0), uint_buf_index_(0) {
|
||||
}
|
||||
|
||||
SocketWriter& WriteUint32(uint32_t value) {
|
||||
CHECK(uint_buf_index_ < kUintBufSize);
|
||||
CHECK(iov_index_ < kIovSize);
|
||||
uint32_t* ptr = uint_buf_ + uint_buf_index_;
|
||||
uint_buf_[uint_buf_index_++] = value;
|
||||
iov_[iov_index_].iov_base = ptr;
|
||||
iov_[iov_index_].iov_len = sizeof(*ptr);
|
||||
++iov_index_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
SocketWriter& WriteString(const char* value) {
|
||||
uint32_t valuelen = strlen(value);
|
||||
WriteUint32(valuelen);
|
||||
if (valuelen == 0) {
|
||||
return *this;
|
||||
}
|
||||
|
||||
CHECK(iov_index_ < kIovSize);
|
||||
iov_[iov_index_].iov_base = const_cast<char*>(value);
|
||||
iov_[iov_index_].iov_len = valuelen;
|
||||
++iov_index_;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool Send() {
|
||||
if (!connection_->IsValid()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (writev(connection_->socket(), iov_, iov_index_) == -1) {
|
||||
connection_->last_error_ = errno;
|
||||
return false;
|
||||
}
|
||||
|
||||
iov_index_ = uint_buf_index_ = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
static constexpr size_t kUintBufSize = 8;
|
||||
static constexpr size_t kIovSize = 8;
|
||||
|
||||
PropertyServiceConnection* connection_;
|
||||
iovec iov_[kIovSize];
|
||||
size_t iov_index_;
|
||||
uint32_t uint_buf_[kUintBufSize];
|
||||
size_t uint_buf_index_;
|
||||
|
||||
BIONIC_DISALLOW_IMPLICIT_CONSTRUCTORS(SocketWriter);
|
||||
};
|
||||
|
||||
struct prop_msg {
|
||||
unsigned cmd;
|
||||
char name[PROP_NAME_MAX];
|
||||
char value[PROP_VALUE_MAX];
|
||||
};
|
||||
|
||||
static int send_prop_msg(const prop_msg* msg) {
|
||||
PropertyServiceConnection connection;
|
||||
if (!connection.IsValid()) {
|
||||
return connection.GetLastError();
|
||||
}
|
||||
|
||||
int result = -1;
|
||||
int s = connection.socket();
|
||||
|
||||
const int num_bytes = TEMP_FAILURE_RETRY(send(s, msg, sizeof(prop_msg), 0));
|
||||
if (num_bytes == sizeof(prop_msg)) {
|
||||
// We successfully wrote to the property server but now we
|
||||
// wait for the property server to finish its work. It
|
||||
// acknowledges its completion by closing the socket so we
|
||||
// poll here (on nothing), waiting for the socket to close.
|
||||
// If you 'adb shell setprop foo bar' you'll see the POLLHUP
|
||||
// once the socket closes. Out of paranoia we cap our poll
|
||||
// at 250 ms.
|
||||
pollfd pollfds[1];
|
||||
pollfds[0].fd = s;
|
||||
pollfds[0].events = 0;
|
||||
const int poll_result = TEMP_FAILURE_RETRY(poll(pollfds, 1, 250 /* ms */));
|
||||
if (poll_result == 1 && (pollfds[0].revents & POLLHUP) != 0) {
|
||||
result = 0;
|
||||
} else {
|
||||
// Ignore the timeout and treat it like a success anyway.
|
||||
// The init process is single-threaded and its property
|
||||
// service is sometimes slow to respond (perhaps it's off
|
||||
// starting a child process or something) and thus this
|
||||
// times out and the caller thinks it failed, even though
|
||||
// it's still getting around to it. So we fake it here,
|
||||
// mostly for ctl.* properties, but we do try and wait 250
|
||||
// ms so callers who do read-after-write can reliably see
|
||||
// what they've written. Most of the time.
|
||||
// TODO: fix the system properties design.
|
||||
async_safe_format_log(ANDROID_LOG_WARN, "libc",
|
||||
"Property service has timed out while trying to set \"%s\" to \"%s\"",
|
||||
msg->name, msg->value);
|
||||
result = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static constexpr uint32_t kProtocolVersion1 = 1;
|
||||
static constexpr uint32_t kProtocolVersion2 = 2; // current
|
||||
|
||||
static atomic_uint_least32_t g_propservice_protocol_version(0);
|
||||
|
||||
static void detect_protocol_version() {
|
||||
char value[PROP_VALUE_MAX];
|
||||
if (__system_property_get(kServiceVersionPropertyName, value) == 0) {
|
||||
g_propservice_protocol_version = kProtocolVersion1;
|
||||
async_safe_format_log(ANDROID_LOG_WARN, "libc",
|
||||
"Using old property service protocol (\"%s\" is not set)",
|
||||
kServiceVersionPropertyName);
|
||||
} else {
|
||||
uint32_t version = static_cast<uint32_t>(atoll(value));
|
||||
if (version >= kProtocolVersion2) {
|
||||
g_propservice_protocol_version = kProtocolVersion2;
|
||||
} else {
|
||||
async_safe_format_log(ANDROID_LOG_WARN, "libc",
|
||||
"Using old property service protocol (\"%s\"=\"%s\")",
|
||||
kServiceVersionPropertyName, value);
|
||||
g_propservice_protocol_version = kProtocolVersion1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
__BIONIC_WEAK_FOR_NATIVE_BRIDGE
|
||||
int __system_property_set(const char* key, const char* value) {
|
||||
if (key == nullptr) return -1;
|
||||
if (value == nullptr) value = "";
|
||||
|
||||
if (g_propservice_protocol_version == 0) {
|
||||
detect_protocol_version();
|
||||
}
|
||||
|
||||
if (g_propservice_protocol_version == kProtocolVersion1) {
|
||||
// Old protocol does not support long names or values
|
||||
if (strlen(key) >= PROP_NAME_MAX) return -1;
|
||||
if (strlen(value) >= PROP_VALUE_MAX) return -1;
|
||||
|
||||
prop_msg msg;
|
||||
memset(&msg, 0, sizeof msg);
|
||||
msg.cmd = PROP_MSG_SETPROP;
|
||||
strlcpy(msg.name, key, sizeof msg.name);
|
||||
strlcpy(msg.value, value, sizeof msg.value);
|
||||
|
||||
return send_prop_msg(&msg);
|
||||
} else {
|
||||
// New protocol only allows long values for ro. properties only.
|
||||
if (strlen(value) >= PROP_VALUE_MAX && strncmp(key, "ro.", 3) != 0) return -1;
|
||||
// Use proper protocol
|
||||
PropertyServiceConnection connection;
|
||||
if (!connection.IsValid()) {
|
||||
errno = connection.GetLastError();
|
||||
async_safe_format_log(
|
||||
ANDROID_LOG_WARN, "libc",
|
||||
"Unable to set property \"%s\" to \"%s\": connection failed; errno=%d (%s)", key, value,
|
||||
errno, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
SocketWriter writer(&connection);
|
||||
if (!writer.WriteUint32(PROP_MSG_SETPROP2).WriteString(key).WriteString(value).Send()) {
|
||||
errno = connection.GetLastError();
|
||||
async_safe_format_log(ANDROID_LOG_WARN, "libc",
|
||||
"Unable to set property \"%s\" to \"%s\": write failed; errno=%d (%s)",
|
||||
key, value, errno, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
int result = -1;
|
||||
if (!connection.RecvInt32(&result)) {
|
||||
errno = connection.GetLastError();
|
||||
async_safe_format_log(ANDROID_LOG_WARN, "libc",
|
||||
"Unable to set property \"%s\" to \"%s\": recv failed; errno=%d (%s)",
|
||||
key, value, errno, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (result != PROP_SUCCESS) {
|
||||
async_safe_format_log(ANDROID_LOG_WARN, "libc",
|
||||
"Unable to set property \"%s\" to \"%s\": error code: 0x%x", key, value,
|
||||
result);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user