Magisk/native/jni/include/daemon.h
topjohnwu 25c557248c Use ContentProvider call method for communication
Previously, we use either BroadcastReceivers or Activities to receive
messages from our native daemon, but both have their own downsides.
Some OEMs blocks broadcasts if the app is not running in the background,
regardless of who the caller is. Activities on the other hand, despite
working 100% of the time, will steal the focus of the current foreground
app, even though we are just doing some logging and showing a toast.
In addition, since stubs for hiding Magisk Manager is introduced, our
only communication method is left with the broadcast option, as
only broadcasting allows targeting a specific package name, not a
component name (which will be obfuscated in the case of stubs).

To make sure root requests will work on all devices, Magisk had to do
some experiments every boot to test whether broadcast is deliverable or
not. This makes the whole thing even more complicated then ever.

So lets take a look at another kind of component in Android apps:
ContentProviders. It is a vital part of Android's ecosystem, and as far
as I know no OEMs will block requests to ContentProviders (or else
tons of functionality will break catastrophically). Starting at API 11,
the system supports calling a specific method in ContentProviders,
optionally sending extra data along with the method call. This is
perfect for the native daemon to start a communication with Magisk
Manager. Another cool thing is that we no longer need to know the
component name of the reciever, as ContentProviders identify themselves
with an "authority" name, which in Magisk Manager's case is tied to the
package name. We already have a mechanism to keep track of our current
manager package name, so this works out of the box.

So yay! No more flaky broadcast tests, no more stupid OEMs blocking
broadcasts for some bizzare reasons. This method should in theory
work on almost all devices and situations.
2019-11-04 14:32:28 -05:00

93 lines
2.0 KiB
C++

#pragma once
#include <pthread.h>
#include <sys/un.h>
#include <sys/socket.h>
#include <string>
#include <vector>
// Commands require connecting to daemon
enum {
DO_NOTHING = 0,
SUPERUSER,
CHECK_VERSION,
CHECK_VERSION_CODE,
POST_FS_DATA,
LATE_START,
BOOT_COMPLETE,
MAGISKHIDE,
SQLITE_CMD,
REMOVE_MODULES,
};
// Return codes for daemon
enum {
DAEMON_ERROR = -1,
DAEMON_SUCCESS = 0,
ROOT_REQUIRED,
DAEMON_LAST
};
// daemon.cpp
int connect_daemon(bool create = false);
// socket.cpp
socklen_t setup_sockaddr(struct sockaddr_un *sun, const char *name);
int create_rand_socket(struct sockaddr_un *sun);
int socket_accept(int sockfd, int timeout);
void get_client_cred(int fd, struct ucred *cred);
int recv_fd(int sockfd);
void send_fd(int sockfd, int fd);
int read_int(int fd);
int read_int_be(int fd);
void write_int(int fd, int val);
void write_int_be(int fd, int val);
char *read_string(int fd);
char *read_string_be(int fd);
void write_string(int fd, const char *val);
void write_string_be(int fd, const char *val);
void write_key_value(int fd, const char *key, const char *val);
void write_key_token(int fd, const char *key, int tok);
/***************
* Boot Stages *
***************/
void unlock_blocks();
void post_fs_data(int client);
void late_start(int client);
void boot_complete(int client);
void remove_modules();
/*************
* Scripting *
*************/
void exec_script(const char *script);
void exec_common_script(const char *stage);
void exec_module_script(const char *stage, const std::vector<std::string> &module_list);
void migrate_img(const char *img);
void install_apk(const char *apk);
/**************
* MagiskHide *
**************/
void magiskhide_handler(int client);
/*************
* Superuser *
*************/
void su_daemon_handler(int client, struct ucred *credential);
/*********************
* Daemon Global Vars
*********************/
extern int SDK_INT;
extern bool RECOVERY_MODE;
#define APP_DATA_DIR (SDK_INT >= 24 ? "/data/user_de" : "/data/user")