2017-04-06 06:12:29 +08:00
|
|
|
/* misc.c - Store all functions that are unable to be catagorized clearly
|
|
|
|
*/
|
|
|
|
|
2017-06-08 22:56:21 +08:00
|
|
|
#define _GNU_SOURCE
|
2017-04-06 06:12:29 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2017-08-20 21:36:32 +08:00
|
|
|
#include <stdarg.h>
|
2017-04-07 06:21:20 +08:00
|
|
|
#include <fcntl.h>
|
2017-04-15 03:23:09 +08:00
|
|
|
#include <pwd.h>
|
2017-04-17 16:36:49 +08:00
|
|
|
#include <signal.h>
|
2017-06-08 22:56:21 +08:00
|
|
|
#include <sched.h>
|
2017-11-15 14:00:52 +01:00
|
|
|
#include <unistd.h>
|
2017-12-07 01:30:48 +08:00
|
|
|
#include <libgen.h>
|
2017-04-07 06:21:20 +08:00
|
|
|
#include <sys/types.h>
|
2017-04-15 18:33:16 +08:00
|
|
|
#include <sys/mount.h>
|
2017-04-22 06:33:40 +08:00
|
|
|
#include <sys/wait.h>
|
2017-05-04 01:13:04 +08:00
|
|
|
#include <sys/stat.h>
|
2017-12-07 01:30:48 +08:00
|
|
|
#include <sys/inotify.h>
|
2017-04-06 06:12:29 +08:00
|
|
|
|
2017-09-14 21:54:56 +08:00
|
|
|
#include "logging.h"
|
2017-04-06 06:12:29 +08:00
|
|
|
#include "utils.h"
|
2017-12-23 04:58:51 +08:00
|
|
|
#include "resetprop.h"
|
2017-04-06 06:12:29 +08:00
|
|
|
|
2017-04-15 03:23:09 +08:00
|
|
|
unsigned get_shell_uid() {
|
|
|
|
struct passwd* ppwd = getpwnam("shell");
|
|
|
|
if (NULL == ppwd)
|
|
|
|
return 2000;
|
|
|
|
|
|
|
|
return ppwd->pw_uid;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned get_system_uid() {
|
|
|
|
struct passwd* ppwd = getpwnam("system");
|
|
|
|
if (NULL == ppwd)
|
|
|
|
return 1000;
|
|
|
|
|
|
|
|
return ppwd->pw_uid;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned get_radio_uid() {
|
|
|
|
struct passwd* ppwd = getpwnam("radio");
|
|
|
|
if (NULL == ppwd)
|
|
|
|
return 1001;
|
|
|
|
|
|
|
|
return ppwd->pw_uid;
|
|
|
|
}
|
|
|
|
|
2017-04-06 06:12:29 +08:00
|
|
|
int check_data() {
|
2017-12-23 04:58:51 +08:00
|
|
|
struct vector v;
|
|
|
|
vec_init(&v);
|
|
|
|
file_to_vector("/proc/mounts", &v);
|
|
|
|
char *line, *crypto;
|
|
|
|
int mnt = 0;
|
|
|
|
vec_for_each(&v, line) {
|
|
|
|
if (strstr(line, " /data ")) {
|
|
|
|
if (strstr(line, "tmpfs") == NULL)
|
|
|
|
mnt = 1;
|
2017-05-02 04:55:55 +08:00
|
|
|
break;
|
2017-04-06 06:12:29 +08:00
|
|
|
}
|
|
|
|
}
|
2017-12-23 04:58:51 +08:00
|
|
|
vec_deep_destroy(&v);
|
|
|
|
// /data is mounted and not tmpfs and data is unencrypted or vold is started
|
|
|
|
return mnt && (((crypto = getprop("ro.crypto.state")) && strcmp(crypto, "unencrypted") == 0)
|
|
|
|
|| getprop("init.svc.vold"));
|
2017-04-06 06:12:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* All the string should be freed manually!! */
|
2017-04-20 22:45:56 +08:00
|
|
|
int file_to_vector(const char* filename, struct vector *v) {
|
2017-04-06 06:12:29 +08:00
|
|
|
char *line = NULL;
|
|
|
|
size_t len = 0;
|
|
|
|
ssize_t read;
|
|
|
|
|
2017-09-06 02:25:40 +08:00
|
|
|
FILE *fp = xfopen(filename, "r");
|
2017-04-20 22:45:56 +08:00
|
|
|
if (fp == NULL)
|
|
|
|
return 1;
|
|
|
|
|
2017-04-06 06:12:29 +08:00
|
|
|
while ((read = getline(&line, &len, fp)) != -1) {
|
|
|
|
// Remove end newline
|
|
|
|
if (line[read - 1] == '\n')
|
|
|
|
line[read - 1] = '\0';
|
|
|
|
vec_push_back(v, line);
|
|
|
|
line = NULL;
|
|
|
|
}
|
2017-04-20 22:45:56 +08:00
|
|
|
fclose(fp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int vector_to_file(const char *filename, struct vector *v) {
|
|
|
|
FILE *fp = xfopen(filename, "w");
|
|
|
|
if (fp == NULL)
|
|
|
|
return 1;
|
|
|
|
char *line;
|
|
|
|
vec_for_each(v, line) {
|
|
|
|
fprintf(fp, "%s\n", line);
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
return 0;
|
2017-04-06 06:12:29 +08:00
|
|
|
}
|
2017-04-07 06:21:20 +08:00
|
|
|
|
|
|
|
/* Check if the string only contains digits */
|
2017-08-20 21:36:32 +08:00
|
|
|
static int is_num(const char *s) {
|
2017-04-07 06:21:20 +08:00
|
|
|
int len = strlen(s);
|
|
|
|
for (int i = 0; i < len; ++i)
|
|
|
|
if (s[i] < '0' || s[i] > '9')
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read a whole line from file descriptor */
|
2017-04-18 19:32:50 +08:00
|
|
|
ssize_t fdgets(char *buf, const size_t size, int fd) {
|
2017-06-03 04:31:01 +08:00
|
|
|
ssize_t len = 0;
|
2017-04-07 06:21:20 +08:00
|
|
|
buf[0] = '\0';
|
2018-01-13 05:49:03 +08:00
|
|
|
while (len < size - 1) {
|
|
|
|
int ret = read(fd, buf + len, 1);
|
|
|
|
if (ret < 0)
|
|
|
|
return -1;
|
|
|
|
if (ret == 0)
|
|
|
|
break;
|
2017-06-03 04:31:01 +08:00
|
|
|
if (buf[len] == '\0' || buf[len++] == '\n') {
|
|
|
|
buf[len] = '\0';
|
2017-04-07 06:21:20 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-04-18 19:32:50 +08:00
|
|
|
buf[size - 1] = '\0';
|
2017-06-03 04:31:01 +08:00
|
|
|
return len;
|
2017-04-07 06:21:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Call func for each process */
|
|
|
|
void ps(void (*func)(int)) {
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *entry;
|
|
|
|
|
2017-04-22 06:33:40 +08:00
|
|
|
if (!(dir = xopendir("/proc")))
|
|
|
|
return;
|
2017-04-07 06:21:20 +08:00
|
|
|
|
|
|
|
while ((entry = xreaddir(dir))) {
|
|
|
|
if (entry->d_type == DT_DIR) {
|
2017-08-20 21:36:32 +08:00
|
|
|
if (is_num(entry->d_name))
|
2017-04-07 06:21:20 +08:00
|
|
|
func(atoi(entry->d_name));
|
|
|
|
}
|
|
|
|
}
|
2017-08-01 01:24:27 +08:00
|
|
|
|
2017-04-07 06:21:20 +08:00
|
|
|
closedir(dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Internal usage
|
|
|
|
static void (*ps_filter_cb)(int);
|
|
|
|
static const char *ps_filter_pattern;
|
|
|
|
static void proc_name_filter(int pid) {
|
|
|
|
char buf[64];
|
2017-04-21 23:57:21 +08:00
|
|
|
int fd;
|
2017-04-07 06:21:20 +08:00
|
|
|
snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
|
2017-10-14 00:08:12 +08:00
|
|
|
if (access(buf, R_OK) == -1 || (fd = xopen(buf, O_RDONLY)) == -1)
|
2017-04-21 23:57:21 +08:00
|
|
|
return;
|
2017-04-18 19:32:50 +08:00
|
|
|
if (fdgets(buf, sizeof(buf), fd) == 0) {
|
2017-04-07 06:21:20 +08:00
|
|
|
snprintf(buf, sizeof(buf), "/proc/%d/comm", pid);
|
|
|
|
close(fd);
|
2017-10-14 00:08:12 +08:00
|
|
|
if (access(buf, R_OK) == -1 || (fd = xopen(buf, O_RDONLY)) == -1)
|
2017-04-21 23:57:21 +08:00
|
|
|
return;
|
2017-04-18 19:32:50 +08:00
|
|
|
fdgets(buf, sizeof(buf), fd);
|
2017-04-07 06:21:20 +08:00
|
|
|
}
|
2017-05-04 02:58:37 +08:00
|
|
|
if (strcmp(buf, ps_filter_pattern) == 0) {
|
2017-04-07 06:21:20 +08:00
|
|
|
ps_filter_cb(pid);
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Call func with process name filtered with pattern */
|
|
|
|
void ps_filter_proc_name(const char *pattern, void (*func)(int)) {
|
|
|
|
ps_filter_cb = func;
|
|
|
|
ps_filter_pattern = ((pattern == NULL) ? "" : pattern);
|
|
|
|
ps(proc_name_filter);
|
|
|
|
}
|
2017-04-15 18:10:54 +08:00
|
|
|
|
2017-08-01 01:24:27 +08:00
|
|
|
void unlock_blocks() {
|
2017-04-15 18:33:16 +08:00
|
|
|
DIR *dir;
|
|
|
|
struct dirent *entry;
|
2017-10-08 22:00:22 +08:00
|
|
|
int fd, dev, OFF = 0;
|
2017-04-15 18:33:16 +08:00
|
|
|
|
2017-10-08 22:00:22 +08:00
|
|
|
if ((dev = xopen("/dev/block", O_RDONLY | O_CLOEXEC)) < 0)
|
2017-04-15 18:33:16 +08:00
|
|
|
return;
|
2017-10-14 00:08:12 +08:00
|
|
|
dir = xfdopendir(dev);
|
2017-04-15 18:33:16 +08:00
|
|
|
|
|
|
|
while((entry = readdir(dir))) {
|
2017-10-08 22:00:22 +08:00
|
|
|
if (entry->d_type == DT_BLK) {
|
|
|
|
if ((fd = openat(dev, entry->d_name, O_RDONLY)) < 0)
|
2017-04-15 18:33:16 +08:00
|
|
|
continue;
|
|
|
|
if (ioctl(fd, BLKROSET, &OFF) == -1)
|
2017-10-08 22:00:22 +08:00
|
|
|
PLOGE("unlock %s", entry->d_name);
|
2017-04-15 18:33:16 +08:00
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
}
|
2017-10-08 22:00:22 +08:00
|
|
|
close(dev);
|
2017-04-15 18:33:16 +08:00
|
|
|
}
|
2017-04-16 02:42:24 +08:00
|
|
|
|
2017-04-17 16:36:49 +08:00
|
|
|
void setup_sighandlers(void (*handler)(int)) {
|
|
|
|
struct sigaction act;
|
|
|
|
memset(&act, 0, sizeof(act));
|
|
|
|
act.sa_handler = handler;
|
|
|
|
for (int i = 0; quit_signals[i]; ++i) {
|
|
|
|
sigaction(quit_signals[i], &act, NULL);
|
|
|
|
}
|
|
|
|
}
|
2017-04-18 19:32:50 +08:00
|
|
|
|
2017-06-08 03:20:49 +08:00
|
|
|
/*
|
|
|
|
fd == NULL -> Ignore output
|
2017-07-10 22:29:53 +08:00
|
|
|
*fd < 0 -> Open pipe and set *fd to the read end
|
|
|
|
*fd >= 0 -> STDOUT (or STDERR) will be redirected to *fd
|
2017-08-01 15:34:16 +08:00
|
|
|
*cb -> A callback function which runs after fork
|
2017-06-08 03:20:49 +08:00
|
|
|
*/
|
2017-08-31 02:20:06 +08:00
|
|
|
static int v_exec_command(int err, int *fd, void (*setupenv)(struct vector*), const char *argv0, va_list argv) {
|
2017-07-02 23:04:57 +08:00
|
|
|
int pipefd[2], writeEnd = -1;
|
2017-04-22 06:33:40 +08:00
|
|
|
|
|
|
|
if (fd) {
|
2017-07-10 22:29:53 +08:00
|
|
|
if (*fd < 0) {
|
|
|
|
if (xpipe2(pipefd, O_CLOEXEC) == -1)
|
2017-06-08 03:20:49 +08:00
|
|
|
return -1;
|
|
|
|
writeEnd = pipefd[1];
|
2017-07-10 22:29:53 +08:00
|
|
|
} else {
|
|
|
|
writeEnd = *fd;
|
2017-06-08 03:20:49 +08:00
|
|
|
}
|
2017-04-22 06:33:40 +08:00
|
|
|
}
|
2017-04-18 19:32:50 +08:00
|
|
|
|
2017-08-27 02:17:37 +08:00
|
|
|
// Collect va_list into vector
|
2017-08-31 02:20:06 +08:00
|
|
|
struct vector args;
|
|
|
|
vec_init(&args);
|
|
|
|
vec_push_back(&args, strdup(argv0));
|
2017-08-27 02:17:37 +08:00
|
|
|
for (void *arg = va_arg(argv, void*); arg; arg = va_arg(argv, void*))
|
2017-08-31 02:20:06 +08:00
|
|
|
vec_push_back(&args, strdup(arg));
|
|
|
|
vec_push_back(&args, NULL);
|
|
|
|
|
|
|
|
// Setup environment
|
|
|
|
char *const *envp;
|
|
|
|
struct vector env;
|
|
|
|
vec_init(&env);
|
|
|
|
if (setupenv) {
|
|
|
|
setupenv(&env);
|
|
|
|
envp = (char **) vec_entry(&env);
|
|
|
|
} else {
|
|
|
|
extern char **environ;
|
|
|
|
envp = environ;
|
|
|
|
}
|
2017-08-27 02:17:37 +08:00
|
|
|
|
2017-10-14 00:08:12 +08:00
|
|
|
int pid = xfork();
|
2017-04-18 19:32:50 +08:00
|
|
|
if (pid != 0) {
|
2017-07-16 03:54:06 +08:00
|
|
|
if (fd && *fd < 0) {
|
|
|
|
// Give the read end and close write end
|
|
|
|
*fd = pipefd[0];
|
|
|
|
close(pipefd[1]);
|
|
|
|
}
|
2017-08-31 02:20:06 +08:00
|
|
|
vec_deep_destroy(&args);
|
|
|
|
vec_deep_destroy(&env);
|
2017-04-18 19:32:50 +08:00
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
|
2017-04-22 06:33:40 +08:00
|
|
|
if (fd) {
|
2017-06-08 03:20:49 +08:00
|
|
|
xdup2(writeEnd, STDOUT_FILENO);
|
|
|
|
if (err) xdup2(writeEnd, STDERR_FILENO);
|
2017-04-22 06:33:40 +08:00
|
|
|
}
|
|
|
|
|
2017-08-31 02:20:06 +08:00
|
|
|
execvpe(argv0, (char **) vec_entry(&args), envp);
|
|
|
|
PLOGE("execvpe");
|
2017-04-18 19:32:50 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2017-04-22 06:33:40 +08:00
|
|
|
|
2017-08-20 21:36:32 +08:00
|
|
|
int exec_command_sync(char *const argv0, ...) {
|
|
|
|
va_list argv;
|
|
|
|
va_start(argv, argv0);
|
|
|
|
int pid, status;
|
|
|
|
pid = v_exec_command(0, NULL, NULL, argv0, argv);
|
|
|
|
va_end(argv);
|
|
|
|
if (pid < 0)
|
|
|
|
return pid;
|
|
|
|
waitpid(pid, &status, 0);
|
|
|
|
return WEXITSTATUS(status);
|
|
|
|
}
|
|
|
|
|
2017-08-31 02:20:06 +08:00
|
|
|
int exec_command(int err, int *fd, void (*setupenv)(struct vector*), const char *argv0, ...) {
|
2017-08-20 21:36:32 +08:00
|
|
|
va_list argv;
|
|
|
|
va_start(argv, argv0);
|
2017-08-31 02:20:06 +08:00
|
|
|
int pid = v_exec_command(err, fd, setupenv, argv0, argv);
|
2017-08-20 21:36:32 +08:00
|
|
|
va_end(argv);
|
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
|
2017-05-01 01:58:52 +08:00
|
|
|
int bind_mount(const char *from, const char *to) {
|
|
|
|
int ret = xmount(from, to, NULL, MS_BIND, NULL);
|
2017-08-01 01:24:27 +08:00
|
|
|
#ifdef MAGISK_DEBUG
|
2017-12-31 21:54:39 +08:00
|
|
|
LOGI("bind_mount: %s <- %s\n", to, from);
|
2017-06-11 16:51:44 +08:00
|
|
|
#else
|
2017-05-04 02:39:53 +08:00
|
|
|
LOGI("bind_mount: %s\n", to);
|
2017-06-11 16:51:44 +08:00
|
|
|
#endif
|
2017-05-01 01:58:52 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-05-05 16:13:26 +08:00
|
|
|
void get_client_cred(int fd, struct ucred *cred) {
|
|
|
|
socklen_t ucred_length = sizeof(*cred);
|
|
|
|
if(getsockopt(fd, SOL_SOCKET, SO_PEERCRED, cred, &ucred_length))
|
|
|
|
PLOGE("getsockopt");
|
|
|
|
}
|
2017-06-03 03:58:26 +08:00
|
|
|
|
2017-06-08 22:56:21 +08:00
|
|
|
int switch_mnt_ns(int pid) {
|
|
|
|
char mnt[32];
|
|
|
|
snprintf(mnt, sizeof(mnt), "/proc/%d/ns/mnt", pid);
|
|
|
|
if(access(mnt, R_OK) == -1) return 1; // Maybe process died..
|
|
|
|
|
|
|
|
int fd, ret;
|
|
|
|
fd = xopen(mnt, O_RDONLY);
|
|
|
|
if (fd < 0) return 1;
|
|
|
|
// Switch to its namespace
|
2018-01-11 03:06:20 +08:00
|
|
|
ret = xsetns(fd, 0);
|
2017-06-08 22:56:21 +08:00
|
|
|
close(fd);
|
|
|
|
return ret;
|
|
|
|
}
|
2017-10-09 05:05:52 +08:00
|
|
|
|
|
|
|
int fork_dont_care() {
|
2017-10-14 00:08:12 +08:00
|
|
|
int pid = xfork();
|
2017-10-09 05:05:52 +08:00
|
|
|
if (pid) {
|
|
|
|
waitpid(pid, NULL, 0);
|
|
|
|
return pid;
|
2017-10-14 00:08:12 +08:00
|
|
|
} else if ((pid = xfork())) {
|
2017-10-09 05:05:52 +08:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2017-12-07 01:30:48 +08:00
|
|
|
|
|
|
|
void wait_till_exists(const char *target) {
|
|
|
|
if (access(target, F_OK) == 0)
|
|
|
|
return;
|
|
|
|
int fd = inotify_init();
|
|
|
|
char *dir = dirname(target);
|
|
|
|
char crap[PATH_MAX];
|
|
|
|
inotify_add_watch(fd, dir, IN_CREATE);
|
|
|
|
while (1) {
|
|
|
|
struct inotify_event event;
|
|
|
|
read(fd, &event, sizeof(event));
|
|
|
|
read(fd, crap, event.len);
|
|
|
|
if (access(target, F_OK) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|