2017-04-05 22:12:29 +00:00
|
|
|
/* misc.c - Store all functions that are unable to be catagorized clearly
|
|
|
|
*/
|
|
|
|
|
2017-06-08 14:56:21 +00:00
|
|
|
#define _GNU_SOURCE
|
2017-04-05 22:12:29 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2017-04-06 22:21:20 +00:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2017-04-14 19:23:09 +00:00
|
|
|
#include <pwd.h>
|
2017-04-17 08:36:49 +00:00
|
|
|
#include <signal.h>
|
2017-04-30 17:58:52 +00:00
|
|
|
#include <errno.h>
|
2017-06-08 14:56:21 +00:00
|
|
|
#include <sched.h>
|
2017-04-06 22:21:20 +00:00
|
|
|
#include <sys/types.h>
|
2017-04-15 10:33:16 +00:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/mount.h>
|
2017-04-21 22:33:40 +00:00
|
|
|
#include <sys/wait.h>
|
2017-05-03 17:13:04 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <selinux/selinux.h>
|
2017-04-05 22:12:29 +00:00
|
|
|
|
2017-04-07 23:37:43 +00:00
|
|
|
#include "magisk.h"
|
2017-04-05 22:12:29 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
2017-04-17 08:36:49 +00:00
|
|
|
int quit_signals[] = { SIGALRM, SIGABRT, SIGHUP, SIGPIPE, SIGQUIT, SIGTERM, SIGINT, 0 };
|
|
|
|
|
2017-04-14 19:23:09 +00: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-05 22:12:29 +00:00
|
|
|
int check_data() {
|
2017-05-01 20:55:55 +00:00
|
|
|
int ret = 0;
|
2017-04-14 19:23:09 +00:00
|
|
|
char buffer[4096];
|
2017-04-05 22:12:29 +00:00
|
|
|
FILE *fp = xfopen("/proc/mounts", "r");
|
2017-04-14 19:23:09 +00:00
|
|
|
while (fgets(buffer, sizeof(buffer), fp)) {
|
|
|
|
if (strstr(buffer, " /data ")) {
|
2017-05-01 20:55:55 +00:00
|
|
|
if (strstr(buffer, "tmpfs") == NULL)
|
|
|
|
ret = 1;
|
|
|
|
break;
|
2017-04-05 22:12:29 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-01 20:55:55 +00:00
|
|
|
fclose(fp);
|
|
|
|
return ret;
|
2017-04-05 22:12:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* All the string should be freed manually!! */
|
2017-04-20 14:45:56 +00:00
|
|
|
int file_to_vector(const char* filename, struct vector *v) {
|
2017-04-05 22:12:29 +00:00
|
|
|
char *line = NULL;
|
|
|
|
size_t len = 0;
|
|
|
|
ssize_t read;
|
|
|
|
|
2017-06-02 20:31:01 +00:00
|
|
|
FILE *fp = fopen(filename, "r");
|
2017-04-20 14:45:56 +00:00
|
|
|
if (fp == NULL)
|
|
|
|
return 1;
|
|
|
|
|
2017-04-05 22:12:29 +00: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 14:45:56 +00: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-05 22:12:29 +00:00
|
|
|
}
|
2017-04-06 22:21:20 +00:00
|
|
|
|
|
|
|
/* Check if the string only contains digits */
|
|
|
|
int isNum(const char *s) {
|
|
|
|
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 11:32:50 +00:00
|
|
|
ssize_t fdgets(char *buf, const size_t size, int fd) {
|
2017-06-02 20:31:01 +00:00
|
|
|
ssize_t len = 0;
|
2017-04-06 22:21:20 +00:00
|
|
|
buf[0] = '\0';
|
2017-06-02 20:31:01 +00:00
|
|
|
while (read(fd, buf + len, 1) >= 0 && len < size - 1) {
|
|
|
|
if (buf[len] == '\0' || buf[len++] == '\n') {
|
|
|
|
buf[len] = '\0';
|
2017-04-06 22:21:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-04-18 11:32:50 +00:00
|
|
|
buf[size - 1] = '\0';
|
2017-06-02 20:31:01 +00:00
|
|
|
return len;
|
2017-04-06 22:21:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Call func for each process */
|
|
|
|
void ps(void (*func)(int)) {
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *entry;
|
|
|
|
|
2017-04-21 22:33:40 +00:00
|
|
|
if (!(dir = xopendir("/proc")))
|
|
|
|
return;
|
2017-04-06 22:21:20 +00:00
|
|
|
|
|
|
|
while ((entry = xreaddir(dir))) {
|
|
|
|
if (entry->d_type == DT_DIR) {
|
|
|
|
if (isNum(entry->d_name))
|
|
|
|
func(atoi(entry->d_name));
|
|
|
|
}
|
|
|
|
}
|
2017-07-31 17:24:27 +00:00
|
|
|
|
2017-04-06 22:21:20 +00: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 15:57:21 +00:00
|
|
|
int fd;
|
2017-04-06 22:21:20 +00:00
|
|
|
snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
|
2017-04-21 15:57:21 +00:00
|
|
|
if ((fd = open(buf, O_RDONLY)) == -1)
|
|
|
|
return;
|
2017-04-18 11:32:50 +00:00
|
|
|
if (fdgets(buf, sizeof(buf), fd) == 0) {
|
2017-04-06 22:21:20 +00:00
|
|
|
snprintf(buf, sizeof(buf), "/proc/%d/comm", pid);
|
|
|
|
close(fd);
|
2017-04-21 15:57:21 +00:00
|
|
|
if ((fd = open(buf, O_RDONLY)) == -1)
|
|
|
|
return;
|
2017-04-18 11:32:50 +00:00
|
|
|
fdgets(buf, sizeof(buf), fd);
|
2017-04-06 22:21:20 +00:00
|
|
|
}
|
2017-05-03 18:58:37 +00:00
|
|
|
if (strcmp(buf, ps_filter_pattern) == 0) {
|
2017-04-06 22:21:20 +00: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 10:10:54 +00:00
|
|
|
|
|
|
|
int create_links(const char *bin, const char *path) {
|
|
|
|
char self[PATH_MAX], linkpath[PATH_MAX];
|
|
|
|
if (bin == NULL) {
|
2017-04-15 10:33:16 +00:00
|
|
|
xreadlink("/proc/self/exe", self, sizeof(self));
|
2017-04-15 10:10:54 +00:00
|
|
|
bin = self;
|
|
|
|
}
|
|
|
|
int ret = 0;
|
|
|
|
for (int i = 0; applet[i]; ++i) {
|
2017-04-15 10:33:16 +00:00
|
|
|
snprintf(linkpath, sizeof(linkpath), "%s/%s", path, applet[i]);
|
2017-04-15 10:10:54 +00:00
|
|
|
unlink(linkpath);
|
|
|
|
ret |= symlink(bin, linkpath);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2017-04-15 10:33:16 +00:00
|
|
|
|
|
|
|
#define DEV_BLOCK "/dev/block"
|
|
|
|
|
2017-07-31 17:24:27 +00:00
|
|
|
void unlock_blocks() {
|
2017-04-15 10:33:16 +00:00
|
|
|
char path[PATH_MAX];
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *entry;
|
|
|
|
int fd, OFF = 0;
|
|
|
|
|
|
|
|
if (!(dir = xopendir(DEV_BLOCK)))
|
|
|
|
return;
|
|
|
|
|
|
|
|
while((entry = readdir(dir))) {
|
2017-06-07 03:32:35 +00:00
|
|
|
if (entry->d_type == DT_BLK &&
|
|
|
|
strstr(entry->d_name, "ram") == NULL &&
|
|
|
|
strstr(entry->d_name, "loop") == NULL &&
|
|
|
|
strstr(entry->d_name, "dm-0") == NULL) {
|
2017-04-15 10:33:16 +00:00
|
|
|
snprintf(path, sizeof(path), "%s/%s", DEV_BLOCK, entry->d_name);
|
|
|
|
if ((fd = xopen(path, O_RDONLY)) < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (ioctl(fd, BLKROSET, &OFF) == -1)
|
2017-04-17 08:36:49 +00:00
|
|
|
PLOGE("unlock %s", path);
|
2017-04-15 10:33:16 +00:00
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dir);
|
|
|
|
}
|
2017-04-15 18:42:24 +00:00
|
|
|
|
2017-04-17 08:36:49 +00: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 11:32:50 +00:00
|
|
|
|
2017-06-07 19:20:49 +00:00
|
|
|
/*
|
|
|
|
fd == NULL -> Ignore output
|
2017-07-10 14:29:53 +00:00
|
|
|
*fd < 0 -> Open pipe and set *fd to the read end
|
|
|
|
*fd >= 0 -> STDOUT (or STDERR) will be redirected to *fd
|
2017-06-07 19:20:49 +00:00
|
|
|
*/
|
|
|
|
int run_command(int err, int *fd, const char *path, char *const argv[]) {
|
2017-07-02 15:04:57 +00:00
|
|
|
int pipefd[2], writeEnd = -1;
|
2017-04-21 22:33:40 +00:00
|
|
|
|
|
|
|
if (fd) {
|
2017-07-10 14:29:53 +00:00
|
|
|
if (*fd < 0) {
|
|
|
|
if (xpipe2(pipefd, O_CLOEXEC) == -1)
|
2017-06-07 19:20:49 +00:00
|
|
|
return -1;
|
|
|
|
writeEnd = pipefd[1];
|
2017-07-10 14:29:53 +00:00
|
|
|
} else {
|
|
|
|
writeEnd = *fd;
|
2017-06-07 19:20:49 +00:00
|
|
|
}
|
2017-04-21 22:33:40 +00:00
|
|
|
}
|
2017-04-18 11:32:50 +00:00
|
|
|
|
|
|
|
int pid = fork();
|
|
|
|
if (pid != 0) {
|
2017-07-15 19:54:06 +00:00
|
|
|
if (fd && *fd < 0) {
|
|
|
|
// Give the read end and close write end
|
|
|
|
*fd = pipefd[0];
|
|
|
|
close(pipefd[1]);
|
|
|
|
}
|
2017-04-18 11:32:50 +00:00
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
|
2017-04-21 22:33:40 +00:00
|
|
|
if (fd) {
|
2017-06-07 19:20:49 +00:00
|
|
|
xdup2(writeEnd, STDOUT_FILENO);
|
|
|
|
if (err) xdup2(writeEnd, STDERR_FILENO);
|
2017-04-21 22:33:40 +00:00
|
|
|
}
|
|
|
|
|
2017-04-18 11:32:50 +00:00
|
|
|
execv(path, argv);
|
|
|
|
PLOGE("execv");
|
|
|
|
return -1;
|
|
|
|
}
|
2017-04-21 22:33:40 +00:00
|
|
|
|
2017-04-30 17:58:52 +00:00
|
|
|
int mkdir_p(const char *pathname, mode_t mode) {
|
|
|
|
char *path = strdup(pathname), *p;
|
|
|
|
errno = 0;
|
|
|
|
for (p = path + 1; *p; ++p) {
|
|
|
|
if (*p == '/') {
|
|
|
|
*p = '\0';
|
|
|
|
if (mkdir(path, mode) == -1) {
|
|
|
|
if (errno != EEXIST)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*p = '/';
|
2017-04-21 22:33:40 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-30 17:58:52 +00:00
|
|
|
if (mkdir(path, mode) == -1) {
|
|
|
|
if (errno != EEXIST)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
free(path);
|
|
|
|
return 0;
|
|
|
|
}
|
2017-04-21 22:33:40 +00:00
|
|
|
|
2017-04-30 17:58:52 +00:00
|
|
|
int bind_mount(const char *from, const char *to) {
|
|
|
|
int ret = xmount(from, to, NULL, MS_BIND, NULL);
|
2017-07-31 17:24:27 +00:00
|
|
|
#ifdef MAGISK_DEBUG
|
2017-04-30 17:58:52 +00:00
|
|
|
LOGD("bind_mount: %s -> %s\n", from, to);
|
2017-06-11 08:51:44 +00:00
|
|
|
#else
|
2017-05-03 18:39:53 +00:00
|
|
|
LOGI("bind_mount: %s\n", to);
|
2017-06-11 08:51:44 +00:00
|
|
|
#endif
|
2017-04-30 17:58:52 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int open_new(const char *filename) {
|
|
|
|
return xopen(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
2017-04-21 22:33:40 +00:00
|
|
|
}
|
2017-05-03 17:13:04 +00:00
|
|
|
|
|
|
|
// file/link -> file/link only!!
|
|
|
|
int cp_afc(const char *source, const char *target) {
|
|
|
|
struct stat buf;
|
|
|
|
xlstat(source, &buf);
|
|
|
|
unlink(target);
|
|
|
|
char *con;
|
|
|
|
if (S_ISREG(buf.st_mode)) {
|
|
|
|
int sfd, tfd;
|
|
|
|
sfd = xopen(source, O_RDONLY);
|
|
|
|
tfd = xopen(target, O_WRONLY | O_CREAT | O_TRUNC);
|
|
|
|
xsendfile(tfd, sfd, NULL, buf.st_size);
|
2017-07-01 06:05:54 +00:00
|
|
|
fclone_attr(sfd, tfd);
|
2017-05-03 17:13:04 +00:00
|
|
|
close(sfd);
|
|
|
|
close(tfd);
|
|
|
|
} else if (S_ISLNK(buf.st_mode)) {
|
|
|
|
char buffer[PATH_MAX];
|
|
|
|
xreadlink(source, buffer, sizeof(buffer));
|
|
|
|
xsymlink(buffer, target);
|
2017-06-11 19:29:01 +00:00
|
|
|
lchown(target, buf.st_uid, buf.st_gid);
|
2017-05-03 17:13:04 +00:00
|
|
|
lgetfilecon(source, &con);
|
|
|
|
lsetfilecon(target, con);
|
|
|
|
free(con);
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int clone_dir(const char *source, const char *target) {
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *entry;
|
2017-05-04 20:16:00 +00:00
|
|
|
char *s_path, *t_path;
|
2017-05-03 17:13:04 +00:00
|
|
|
|
|
|
|
if (!(dir = xopendir(source)))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
s_path = xmalloc(PATH_MAX);
|
|
|
|
t_path = xmalloc(PATH_MAX);
|
|
|
|
|
2017-05-04 20:16:00 +00:00
|
|
|
mkdir_p(target, 0755);
|
|
|
|
clone_attr(source, target);
|
2017-05-03 17:13:04 +00:00
|
|
|
|
|
|
|
while ((entry = xreaddir(dir))) {
|
|
|
|
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
|
|
|
|
continue;
|
|
|
|
snprintf(s_path, PATH_MAX, "%s/%s", source, entry->d_name);
|
|
|
|
snprintf(t_path, PATH_MAX, "%s/%s", target, entry->d_name);
|
|
|
|
switch (entry->d_type) {
|
|
|
|
case DT_DIR:
|
|
|
|
clone_dir(s_path, t_path);
|
|
|
|
break;
|
|
|
|
case DT_REG:
|
|
|
|
case DT_LNK:
|
|
|
|
cp_afc(s_path, t_path);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(s_path);
|
|
|
|
free(t_path);
|
|
|
|
|
|
|
|
closedir(dir);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rm_rf(const char *target) {
|
2017-06-02 21:52:49 +00:00
|
|
|
if (access(target, F_OK) == -1)
|
|
|
|
return 0;
|
2017-07-01 06:05:54 +00:00
|
|
|
// Use external rm command, saves a lot of headache and issues
|
|
|
|
char command[PATH_MAX];
|
|
|
|
snprintf(command, sizeof(command), "rm -rf %s", target);
|
|
|
|
return system(command);
|
2017-05-03 17:13:04 +00:00
|
|
|
}
|
2017-05-04 20:16:00 +00:00
|
|
|
|
|
|
|
void clone_attr(const char *source, const char *target) {
|
|
|
|
struct stat buf;
|
|
|
|
xstat(source, &buf);
|
|
|
|
chmod(target, buf.st_mode & 0777);
|
|
|
|
chown(target, buf.st_uid, buf.st_gid);
|
|
|
|
char *con;
|
|
|
|
lgetfilecon(source, &con);
|
|
|
|
lsetfilecon(target, con);
|
|
|
|
free(con);
|
|
|
|
}
|
2017-05-05 08:13:26 +00:00
|
|
|
|
2017-07-01 06:05:54 +00:00
|
|
|
void fclone_attr(const int sourcefd, const int targetfd) {
|
|
|
|
struct stat buf;
|
|
|
|
fstat(sourcefd, &buf);
|
|
|
|
fchmod(targetfd, buf.st_mode & 0777);
|
|
|
|
fchown(targetfd, buf.st_uid, buf.st_gid);
|
|
|
|
char *con;
|
|
|
|
fgetfilecon(sourcefd, &con);
|
|
|
|
fsetfilecon(sourcefd, con);
|
|
|
|
free(con);
|
|
|
|
}
|
|
|
|
|
2017-05-05 08:13:26 +00: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-02 19:58:26 +00:00
|
|
|
|
2017-06-08 14:56:21 +00: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
|
|
|
|
ret = setns(fd, 0);
|
|
|
|
close(fd);
|
|
|
|
return ret;
|
|
|
|
}
|