Magisk/native/jni/utils/misc.cpp

266 lines
5.3 KiB
C++
Raw Normal View History

2018-11-03 08:06:01 +01:00
/* misc.cpp - Store all functions that are unable to be catagorized clearly
2017-04-06 00:12:29 +02:00
*/
2018-11-03 08:06:01 +01:00
2017-04-06 00:12:29 +02:00
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
2017-04-07 00:21:20 +02:00
#include <fcntl.h>
2017-04-14 21:23:09 +02:00
#include <pwd.h>
2017-11-15 14:00:52 +01:00
#include <unistd.h>
#include <syscall.h>
2017-04-07 00:21:20 +02:00
#include <sys/types.h>
2017-04-22 00:33:40 +02:00
#include <sys/wait.h>
#include <sys/sysmacros.h>
2017-04-06 00:12:29 +02:00
2017-09-14 15:54:56 +02:00
#include "logging.h"
2017-04-06 00:12:29 +02:00
#include "utils.h"
2017-04-14 21:23:09 +02:00
unsigned get_shell_uid() {
struct passwd* ppwd = getpwnam("shell");
2018-11-03 08:06:01 +01:00
if (nullptr == ppwd)
2017-04-14 21:23:09 +02:00
return 2000;
return ppwd->pw_uid;
}
unsigned get_system_uid() {
struct passwd* ppwd = getpwnam("system");
2018-11-03 08:06:01 +01:00
if (nullptr == ppwd)
2017-04-14 21:23:09 +02:00
return 1000;
return ppwd->pw_uid;
}
unsigned get_radio_uid() {
struct passwd* ppwd = getpwnam("radio");
2018-11-03 08:06:01 +01:00
if (nullptr == ppwd)
2017-04-14 21:23:09 +02:00
return 1001;
return ppwd->pw_uid;
}
2017-04-07 00:21:20 +02:00
/* Check if the string only contains digits */
2018-10-13 03:46:09 +02:00
int is_num(const char *s) {
2017-04-07 00:21:20 +02: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 13:32:50 +02:00
ssize_t fdgets(char *buf, const size_t size, int fd) {
2017-06-02 22:31:01 +02:00
ssize_t len = 0;
2017-04-07 00:21:20 +02:00
buf[0] = '\0';
while (len < size - 1) {
int ret = read(fd, buf + len, 1);
if (ret < 0)
return -1;
if (ret == 0)
break;
2017-06-02 22:31:01 +02:00
if (buf[len] == '\0' || buf[len++] == '\n') {
buf[len] = '\0';
2017-04-07 00:21:20 +02:00
break;
}
}
2017-04-18 13:32:50 +02:00
buf[size - 1] = '\0';
2017-06-02 22:31:01 +02:00
return len;
2017-04-07 00:21:20 +02:00
}
int fork_dont_care() {
2017-10-13 18:08:12 +02:00
int pid = xfork();
if (pid) {
2018-11-03 08:06:01 +01:00
waitpid(pid, nullptr, 0);
return pid;
2017-10-13 18:08:12 +02:00
} else if ((pid = xfork())) {
exit(0);
}
return 0;
}
2017-12-06 18:30:48 +01:00
void gen_rand_str(char *buf, int len) {
const char base[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
int urandom;
if (access("/dev/urandom", R_OK) == 0) {
urandom = xopen("/dev/urandom", O_RDONLY | O_CLOEXEC);
} else {
mknod("/urandom", S_IFCHR | 0666, makedev(1, 9));
urandom = xopen("/urandom", O_RDONLY | O_CLOEXEC);
unlink("/urandom");
}
xxread(urandom, buf, len - 1);
close(urandom);
for (int i = 0; i < len - 1; ++i) {
buf[i] = base[buf[i] % (sizeof(base) - 1)];
}
buf[len - 1] = '\0';
}
int strend(const char *s1, const char *s2) {
size_t l1 = strlen(s1);
size_t l2 = strlen(s2);
return strcmp(s1 + l1 - l2, s2);
}
/* Original source: https://opensource.apple.com/source/cvs/cvs-19/cvs/lib/getline.c
* License: GPL 2 or later
* Adjusted to match POSIX */
#define MIN_CHUNK 64
ssize_t __getdelim(char **lineptr, size_t *n, int delim, FILE *stream) {
size_t nchars_avail;
char *read_pos;
if (!lineptr || !n || !stream) {
errno = EINVAL;
return -1;
}
if (!*lineptr) {
*n = MIN_CHUNK;
2018-11-03 08:06:01 +01:00
*lineptr = (char *) malloc(*n);
if (!*lineptr) {
errno = ENOMEM;
return -1;
}
}
nchars_avail = *n;
read_pos = *lineptr;
while (1) {
int save_errno;
2018-11-03 08:06:01 +01:00
int c = getc(stream);
save_errno = errno;
if (nchars_avail < 2) {
if (*n > MIN_CHUNK)
*n *= 2;
else
*n += MIN_CHUNK;
nchars_avail = *n + *lineptr - read_pos;
2018-11-03 08:06:01 +01:00
*lineptr = (char *) realloc(*lineptr, *n);
if (!*lineptr) {
errno = ENOMEM;
return -1;
}
read_pos = *n - nchars_avail + *lineptr;
}
if (ferror(stream)) {
errno = save_errno;
return -1;
}
if (c == EOF) {
if (read_pos == *lineptr)
return -1;
else
break;
}
*read_pos++ = c;
nchars_avail--;
if (c == delim)
break;
}
*read_pos = '\0';
return read_pos - *lineptr;
}
ssize_t __getline(char **lineptr, size_t *n, FILE *stream) {
return __getdelim(lineptr, n, '\n', stream);
}
int __fsetxattr(int fd, const char *name, const void *value, size_t size, int flags) {
return (int) syscall(__NR_fsetxattr, fd, name, value, size, flags);
}
2018-11-03 09:03:11 +01:00
/*
2018-11-26 09:06:43 +01:00
fd == nullptr -> Ignore output
2018-11-03 09:03:11 +01:00
*fd < 0 -> Open pipe and set *fd to the read end
*fd >= 0 -> STDOUT (or STDERR) will be redirected to *fd
2018-11-26 09:26:45 +01:00
*pre_exec -> A callback function called after forking, before execvp
2018-11-03 09:03:11 +01:00
*/
2018-11-26 09:26:45 +01:00
int exec_array(bool err, int *fd, void (*pre_exec)(void), const char **argv) {
2018-11-26 09:06:43 +01:00
int pipefd[2], outfd = -1;
2018-11-03 09:03:11 +01:00
if (fd) {
if (*fd < 0) {
if (xpipe2(pipefd, O_CLOEXEC) == -1)
return -1;
2018-11-26 09:06:43 +01:00
outfd = pipefd[1];
2018-11-03 09:03:11 +01:00
} else {
2018-11-26 09:06:43 +01:00
outfd = *fd;
2018-11-03 09:03:11 +01:00
}
}
int pid = xfork();
if (pid != 0) {
if (fd && *fd < 0) {
// Give the read end and close write end
*fd = pipefd[0];
close(pipefd[1]);
}
return pid;
}
2018-11-26 09:26:45 +01:00
if (outfd >= 0) {
2018-11-26 09:06:43 +01:00
xdup2(outfd, STDOUT_FILENO);
2018-11-03 09:03:11 +01:00
if (err)
2018-11-26 09:06:43 +01:00
xdup2(outfd, STDERR_FILENO);
2018-11-26 09:26:45 +01:00
close(outfd);
2018-11-03 09:03:11 +01:00
}
2018-11-26 09:26:45 +01:00
// Call the pre-exec callback
if (pre_exec)
pre_exec();
2018-11-03 09:03:11 +01:00
execvp(argv[0], (char **) argv);
PLOGE("execvp %s", argv[0]);
return -1;
}
2018-11-26 09:06:43 +01:00
static int v_exec_command(bool err, int *fd, void (*cb)(void), const char *argv0, va_list argv) {
2018-11-03 09:03:11 +01:00
// Collect va_list into vector
Vector<const char *> args;
2018-11-03 09:03:11 +01:00
args.push_back(argv0);
for (const char *arg = va_arg(argv, char*); arg; arg = va_arg(argv, char*))
args.push_back(arg);
args.push_back(nullptr);
int pid = exec_array(err, fd, cb, args.data());
return pid;
}
int exec_command_sync(const char *argv0, ...) {
va_list argv;
va_start(argv, argv0);
int pid, status;
2018-11-26 09:06:43 +01:00
pid = v_exec_command(false, nullptr, nullptr, argv0, argv);
2018-11-03 09:03:11 +01:00
va_end(argv);
if (pid < 0)
return pid;
waitpid(pid, &status, 0);
return WEXITSTATUS(status);
}
2018-11-26 09:06:43 +01:00
int exec_command(bool err, int *fd, void (*cb)(void), const char *argv0, ...) {
2018-11-03 09:03:11 +01:00
va_list argv;
va_start(argv, argv0);
int pid = v_exec_command(err, fd, cb, argv0, argv);
va_end(argv);
return pid;
}
2018-11-07 08:10:38 +01:00
char *strdup2(const char *s, size_t *size) {
size_t len = strlen(s) + 1;
char *buf = new char[len];
memcpy(buf, s, len);
if (size) *size = len;
2018-11-07 08:10:38 +01:00
return buf;
}