Update native su connect broadcast code

Use -p <pkg> for supported platforms
This commit is contained in:
topjohnwu 2019-01-26 14:53:49 -05:00
parent d32b788988
commit 1c61feb368

View File

@ -17,8 +17,10 @@
#include "utils.h" #include "utils.h"
#include "su.h" #include "su.h"
#define AM_PATH "/system/bin/app_process", "/system/bin", "com.android.commands.am.Am" #define BROADCAST_BOOT_COMPLETED \
#define RECEIVER "a.h" "/system/bin/app_process", "/system/bin", "com.android.commands.am.Am", \
"broadcast", nullptr, nullptr, "-a", "android.intent.action.BOOT_COMPLETED", \
"-f", "0x00000020"
static inline const char *get_command(const struct su_request *to) { static inline const char *get_command(const struct su_request *to) {
if (to->command[0]) if (to->command[0])
@ -28,18 +30,27 @@ static inline const char *get_command(const struct su_request *to) {
return DEFAULT_SHELL; return DEFAULT_SHELL;
} }
static void silent_run(const char *args[]) { static void silent_run(const char **args, struct su_info *info) {
if (fork_dont_care()) char component[128];
return; if (SDK_INT >= 22) {
int zero = open("/dev/zero", O_RDONLY | O_CLOEXEC); args[4] = "-p";
xdup2(zero, 0); args[5] = info->str[SU_MANAGER];
int null = open("/dev/null", O_WRONLY | O_CLOEXEC); } else {
xdup2(null, 1); sprintf(component, "%s/a.h", info->str[SU_MANAGER]);
xdup2(null, 2); args[4] = "-n";
setenv("CLASSPATH", "/system/framework/am.jar", 1); args[5] = component;
execv(args[0], (char **) args); }
PLOGE("exec am"); exec_t exec {
_exit(EXIT_FAILURE); .pre_exec = []() -> void {
int null = xopen("/dev/null", O_WRONLY | O_CLOEXEC);
dup2(null, STDOUT_FILENO);
dup2(null, STDERR_FILENO);
setenv("CLASSPATH", "/system/framework/am.jar", 1);
},
.fork = fork_dont_care,
.argv = args
};
exec_command(exec);
} }
static void setup_user(char *user, struct su_info *info) { static void setup_user(char *user, struct su_info *info) {
@ -72,14 +83,8 @@ void app_log(struct su_context *ctx) {
char policy[2]; char policy[2];
sprintf(policy, "%d", ctx->info->access.policy); sprintf(policy, "%d", ctx->info->access.policy);
char component[128];
sprintf(component, "%s/" RECEIVER, ctx->info->str[SU_MANAGER]);
const char *cmd[] = { const char *cmd[] = {
AM_PATH, "broadcast", BROADCAST_BOOT_COMPLETED,
"-a", "android.intent.action.BOOT_COMPLETED",
"-n", component,
"-f", "0x00000020",
"--user", user, "--user", user,
"--es", "action", "log", "--es", "action", "log",
"--ei", "from.uid", fromUid, "--ei", "from.uid", fromUid,
@ -90,7 +95,7 @@ void app_log(struct su_context *ctx) {
"--ez", "notify", ctx->info->access.notify ? "true" : "false", "--ez", "notify", ctx->info->access.notify ? "true" : "false",
nullptr nullptr
}; };
silent_run(cmd); silent_run(cmd, ctx->info);
} }
void app_notify(struct su_context *ctx) { void app_notify(struct su_context *ctx) {
@ -105,39 +110,29 @@ void app_notify(struct su_context *ctx) {
char policy[2]; char policy[2];
sprintf(policy, "%d", ctx->info->access.policy); sprintf(policy, "%d", ctx->info->access.policy);
char component[128];
sprintf(component, "%s/" RECEIVER, ctx->info->str[SU_MANAGER]);
const char *cmd[] = { const char *cmd[] = {
AM_PATH, "broadcast", BROADCAST_BOOT_COMPLETED,
"-a", "android.intent.action.BOOT_COMPLETED", "--user", user,
"-n", component, "--es", "action", "notify",
"-f", "0x00000020", "--ei", "from.uid", fromUid,
"--user", user, "--ei", "policy", policy,
"--es", "action", "notify", nullptr
"--ei", "from.uid", fromUid,
"--ei", "policy", policy,
nullptr
}; };
silent_run(cmd); silent_run(cmd, ctx->info);
} }
void app_connect(const char *socket, struct su_info *info) { void app_connect(const char *socket, struct su_info *info) {
char user[8]; char user[8];
setup_user(user, info); setup_user(user, info);
char component[128];
sprintf(component, "%s/" RECEIVER, info->str[SU_MANAGER]);
const char *cmd[] = { const char *cmd[] = {
AM_PATH, "broadcast", BROADCAST_BOOT_COMPLETED,
"-a", "android.intent.action.BOOT_COMPLETED",
"-n", component,
"-f", "0x00000020",
"--user", user, "--user", user,
"--es", "action", "request", "--es", "action", "request",
"--es", "socket", socket, "--es", "socket", socket,
nullptr nullptr
}; };
silent_run(cmd); silent_run(cmd, info);
} }
void socket_send_request(int fd, struct su_info *info) { void socket_send_request(int fd, struct su_info *info) {