2018-11-03 03:06:01 -04:00
|
|
|
/* misc.cpp - Store all functions that are unable to be catagorized clearly
|
2017-04-06 06:12:29 +08:00
|
|
|
*/
|
2019-07-14 17:41:51 -07:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/prctl.h>
|
|
|
|
#include <sys/sysmacros.h>
|
2017-04-06 06:12:29 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2017-04-07 06:21:20 +08:00
|
|
|
#include <fcntl.h>
|
2017-04-15 03:23:09 +08:00
|
|
|
#include <pwd.h>
|
2017-11-15 14:00:52 +01:00
|
|
|
#include <unistd.h>
|
2018-09-06 01:33:17 -04:00
|
|
|
#include <syscall.h>
|
2019-07-14 17:41:51 -07:00
|
|
|
#include <random>
|
2017-04-06 06:12:29 +08:00
|
|
|
|
2019-02-10 03:57:51 -05:00
|
|
|
#include <logging.h>
|
|
|
|
#include <utils.h>
|
2017-04-06 06:12:29 +08:00
|
|
|
|
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) {
|
2018-11-03 03:06:01 -04:00
|
|
|
waitpid(pid, nullptr, 0);
|
2017-10-09 05:05:52 +08:00
|
|
|
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
|
|
|
|
2019-02-14 17:36:18 -05:00
|
|
|
int fork_no_zombie() {
|
|
|
|
int pid = xfork();
|
|
|
|
if (pid)
|
|
|
|
return pid;
|
|
|
|
// Unblock all signals
|
|
|
|
sigset_t block_set;
|
|
|
|
sigfillset(&block_set);
|
|
|
|
pthread_sigmask(SIG_UNBLOCK, &block_set, nullptr);
|
|
|
|
prctl(PR_SET_PDEATHSIG, SIGTERM);
|
|
|
|
if (getppid() == 1)
|
|
|
|
exit(1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-07-14 17:41:51 -07:00
|
|
|
constexpr char ALPHANUM[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
|
|
|
static bool seeded = false;
|
|
|
|
static std::mt19937 gen;
|
2019-07-14 21:56:21 -07:00
|
|
|
static std::uniform_int_distribution<int> dist(0, sizeof(ALPHANUM) - 2);
|
2019-07-14 17:41:51 -07:00
|
|
|
void gen_rand_str(char *buf, int len, bool varlen) {
|
|
|
|
if (!seeded) {
|
2019-07-14 21:56:21 -07:00
|
|
|
if (access("/dev/urandom", F_OK) != 0)
|
|
|
|
mknod("/dev/urandom", 0600 | S_IFCHR, makedev(1, 9));
|
|
|
|
int fd = xopen("/dev/urandom", O_RDONLY | O_CLOEXEC);
|
|
|
|
unsigned seed;
|
|
|
|
xxread(fd, &seed, sizeof(seed));
|
|
|
|
gen.seed(seed);
|
|
|
|
close(fd);
|
2019-07-14 17:41:51 -07:00
|
|
|
seeded = true;
|
2018-02-11 03:40:09 +08:00
|
|
|
}
|
2019-07-14 17:41:51 -07:00
|
|
|
if (varlen) {
|
|
|
|
std::uniform_int_distribution<int> len_dist(len / 2, len);
|
|
|
|
len = len_dist(gen);
|
2018-02-11 03:40:09 +08:00
|
|
|
}
|
2019-07-14 17:41:51 -07:00
|
|
|
for (int i = 0; i < len - 1; ++i)
|
|
|
|
buf[i] = ALPHANUM[dist(gen)];
|
2018-02-11 03:40:09 +08:00
|
|
|
buf[len - 1] = '\0';
|
|
|
|
}
|
2018-07-21 05:12:22 +08:00
|
|
|
|
|
|
|
int strend(const char *s1, const char *s2) {
|
|
|
|
size_t l1 = strlen(s1);
|
|
|
|
size_t l2 = strlen(s2);
|
|
|
|
return strcmp(s1 + l1 - l2, s2);
|
|
|
|
}
|
2018-09-06 01:33:17 -04:00
|
|
|
|
2019-01-26 13:00:19 -05:00
|
|
|
int exec_command(exec_t &exec) {
|
|
|
|
int pipefd[2] = {-1, -1}, outfd = -1;
|
2018-11-03 04:03:11 -04:00
|
|
|
|
2019-01-26 13:00:19 -05:00
|
|
|
if (exec.fd == -1) {
|
|
|
|
if (xpipe2(pipefd, O_CLOEXEC) == -1)
|
|
|
|
return -1;
|
|
|
|
outfd = pipefd[1];
|
|
|
|
} else if (exec.fd >= 0) {
|
|
|
|
outfd = exec.fd;
|
2018-11-03 04:03:11 -04:00
|
|
|
}
|
|
|
|
|
2019-01-26 13:00:19 -05:00
|
|
|
int pid = exec.fork();
|
|
|
|
if (pid < 0) {
|
|
|
|
close(pipefd[0]);
|
|
|
|
close(pipefd[1]);
|
|
|
|
return -1;
|
|
|
|
} else if (pid) {
|
|
|
|
if (exec.fd == -1) {
|
|
|
|
exec.fd = pipefd[0];
|
2018-11-03 04:03:11 -04:00
|
|
|
close(pipefd[1]);
|
|
|
|
}
|
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
|
2018-11-26 03:26:45 -05:00
|
|
|
if (outfd >= 0) {
|
2018-11-26 03:06:43 -05:00
|
|
|
xdup2(outfd, STDOUT_FILENO);
|
2019-01-26 13:00:19 -05:00
|
|
|
if (exec.err)
|
2018-11-26 03:06:43 -05:00
|
|
|
xdup2(outfd, STDERR_FILENO);
|
2018-11-26 03:26:45 -05:00
|
|
|
close(outfd);
|
2018-11-03 04:03:11 -04:00
|
|
|
}
|
|
|
|
|
2018-11-26 03:26:45 -05:00
|
|
|
// Call the pre-exec callback
|
2019-01-26 13:00:19 -05:00
|
|
|
if (exec.pre_exec)
|
|
|
|
exec.pre_exec();
|
2018-11-03 04:03:11 -04:00
|
|
|
|
2019-01-26 13:00:19 -05:00
|
|
|
execve(exec.argv[0], (char **) exec.argv, environ);
|
|
|
|
PLOGE("execve %s", exec.argv[0]);
|
2018-12-28 16:33:26 +08:00
|
|
|
exit(-1);
|
2018-11-03 04:03:11 -04:00
|
|
|
}
|
|
|
|
|
2019-02-12 16:13:31 -05:00
|
|
|
int exec_command_sync(exec_t &exec) {
|
2019-01-26 13:00:19 -05:00
|
|
|
int pid, status;
|
|
|
|
pid = exec_command(exec);
|
|
|
|
if (pid < 0)
|
|
|
|
return -1;
|
|
|
|
waitpid(pid, &status, 0);
|
|
|
|
return WEXITSTATUS(status);
|
|
|
|
}
|
|
|
|
|
2019-02-14 17:36:18 -05:00
|
|
|
int new_daemon_thread(void *(*start_routine) (void *), void *arg, const pthread_attr_t *attr) {
|
|
|
|
pthread_t thread;
|
|
|
|
int ret = xpthread_create(&thread, attr, start_routine, arg);
|
|
|
|
if (ret == 0)
|
|
|
|
pthread_detach(thread);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-05-25 21:42:51 -07:00
|
|
|
static void *proxy_routine(void *fp) {
|
|
|
|
auto fn = reinterpret_cast<std::function<void()>*>(fp);
|
|
|
|
(*fn)();
|
|
|
|
delete fn;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
int new_daemon_thread(std::function<void()> &&fn) {
|
|
|
|
return new_daemon_thread(proxy_routine, new std::function<void()>(std::move(fn)));
|
|
|
|
}
|
|
|
|
|
2019-02-15 04:31:39 -05:00
|
|
|
static char *argv0;
|
|
|
|
static size_t name_len;
|
|
|
|
void init_argv0(int argc, char **argv) {
|
|
|
|
argv0 = argv[0];
|
|
|
|
name_len = (argv[argc - 1] - argv[0]) + strlen(argv[argc - 1]) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_nice_name(const char *name) {
|
|
|
|
memset(argv0, 0, name_len);
|
|
|
|
strlcpy(argv0, name, name_len);
|
|
|
|
prctl(PR_SET_NAME, name);
|
|
|
|
}
|
2019-02-18 03:05:13 -05:00
|
|
|
|
|
|
|
bool ends_with(const std::string_view &s1, const std::string_view &s2) {
|
|
|
|
unsigned l1 = s1.length();
|
|
|
|
unsigned l2 = s2.length();
|
|
|
|
return l1 < l2 ? false : s1.compare(l1 - l2, l2, s2) == 0;
|
|
|
|
}
|
2019-02-26 16:04:17 +08:00
|
|
|
|
|
|
|
char *rtrim(char *str) {
|
|
|
|
int len = strlen(str);
|
|
|
|
while (len > 0 && str[len - 1] == ' ')
|
|
|
|
--len;
|
|
|
|
str[len] = '\0';
|
|
|
|
return str;
|
|
|
|
}
|
2019-03-07 20:31:35 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Bionic's atoi runs through strtol().
|
|
|
|
* Use our own implementation for faster conversion.
|
|
|
|
*/
|
|
|
|
int parse_int(const char *s) {
|
|
|
|
int val = 0;
|
|
|
|
char c;
|
|
|
|
while ((c = *(s++))) {
|
|
|
|
if (c > '9' || c < '0')
|
|
|
|
return -1;
|
|
|
|
val = val * 10 + c - '0';
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
2019-10-07 00:38:02 -04:00
|
|
|
|
|
|
|
uint32_t binary_gcd(uint32_t u, uint32_t v) {
|
|
|
|
if (u == 0) return v;
|
|
|
|
if (v == 0) return u;
|
|
|
|
auto shift = __builtin_ctz(u | v);
|
|
|
|
u >>= __builtin_ctz(u);
|
|
|
|
do {
|
|
|
|
v >>= __builtin_ctz(v);
|
|
|
|
if (u > v) {
|
|
|
|
auto t = v;
|
|
|
|
v = u;
|
|
|
|
u = t;
|
|
|
|
}
|
|
|
|
v -= u;
|
|
|
|
} while (v != 0);
|
|
|
|
return u << shift;
|
|
|
|
}
|