2019-07-15 00:41:51 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/prctl.h>
|
|
|
|
#include <sys/sysmacros.h>
|
2017-04-06 22:21:20 +00:00
|
|
|
#include <fcntl.h>
|
2017-04-14 19:23:09 +00:00
|
|
|
#include <pwd.h>
|
2017-11-15 13:00:52 +00:00
|
|
|
#include <unistd.h>
|
2018-09-06 05:33:17 +00:00
|
|
|
#include <syscall.h>
|
2019-07-15 00:41:51 +00:00
|
|
|
#include <random>
|
2020-04-26 06:19:36 +00:00
|
|
|
#include <string>
|
2017-04-05 22:12:29 +00:00
|
|
|
|
2022-05-12 09:03:42 +00:00
|
|
|
#include <base.hpp>
|
2017-04-05 22:12:29 +00:00
|
|
|
|
2020-04-26 06:19:36 +00:00
|
|
|
using namespace std;
|
|
|
|
|
2017-10-08 21:05:52 +00:00
|
|
|
int fork_dont_care() {
|
2020-12-31 06:11:24 +00:00
|
|
|
if (int pid = xfork()) {
|
|
|
|
waitpid(pid, nullptr, 0);
|
|
|
|
return pid;
|
|
|
|
} else if (xfork()) {
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
return 0;
|
2017-10-08 21:05:52 +00:00
|
|
|
}
|
2017-12-06 17:30:48 +00:00
|
|
|
|
2020-12-18 00:54:53 +00:00
|
|
|
int fork_no_orphan() {
|
2020-12-31 06:11:24 +00:00
|
|
|
int pid = xfork();
|
|
|
|
if (pid)
|
|
|
|
return pid;
|
2021-10-17 11:36:18 +00:00
|
|
|
prctl(PR_SET_PDEATHSIG, SIGKILL);
|
2020-12-31 06:11:24 +00:00
|
|
|
if (getppid() == 1)
|
|
|
|
exit(1);
|
|
|
|
return 0;
|
2019-02-14 22:36:18 +00:00
|
|
|
}
|
|
|
|
|
2020-04-19 06:19:19 +00:00
|
|
|
int gen_rand_str(char *buf, int len, bool varlen) {
|
2021-11-16 11:20:07 +00:00
|
|
|
constexpr char ALPHANUM[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
2022-03-03 03:50:47 +00:00
|
|
|
static std::mt19937 gen([]{
|
|
|
|
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));
|
2021-11-16 11:20:07 +00:00
|
|
|
return seed;
|
|
|
|
}());
|
2022-03-03 03:50:47 +00:00
|
|
|
std::uniform_int_distribution<int> dist(0, sizeof(ALPHANUM) - 2);
|
2020-12-31 06:11:24 +00:00
|
|
|
if (varlen) {
|
2022-03-03 03:50:47 +00:00
|
|
|
std::uniform_int_distribution<int> len_dist(len / 2, len);
|
2020-12-31 06:11:24 +00:00
|
|
|
len = len_dist(gen);
|
|
|
|
}
|
|
|
|
for (int i = 0; i < len - 1; ++i)
|
|
|
|
buf[i] = ALPHANUM[dist(gen)];
|
|
|
|
buf[len - 1] = '\0';
|
|
|
|
return len - 1;
|
2018-02-10 19:40:09 +00:00
|
|
|
}
|
2018-07-20 21:12:22 +00:00
|
|
|
|
2019-01-26 18:00:19 +00:00
|
|
|
int exec_command(exec_t &exec) {
|
2022-08-19 09:21:52 +00:00
|
|
|
auto pipefd = array<int, 2>{-1, -1};
|
2020-12-31 06:11:24 +00:00
|
|
|
int outfd = -1;
|
|
|
|
|
|
|
|
if (exec.fd == -1) {
|
|
|
|
if (xpipe2(pipefd, O_CLOEXEC) == -1)
|
|
|
|
return -1;
|
|
|
|
outfd = pipefd[1];
|
|
|
|
} else if (exec.fd >= 0) {
|
|
|
|
outfd = exec.fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
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];
|
|
|
|
close(pipefd[1]);
|
|
|
|
}
|
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unblock all signals
|
|
|
|
sigset_t set;
|
|
|
|
sigfillset(&set);
|
|
|
|
pthread_sigmask(SIG_UNBLOCK, &set, nullptr);
|
|
|
|
|
|
|
|
if (outfd >= 0) {
|
|
|
|
xdup2(outfd, STDOUT_FILENO);
|
|
|
|
if (exec.err)
|
|
|
|
xdup2(outfd, STDERR_FILENO);
|
|
|
|
close(outfd);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call the pre-exec callback
|
|
|
|
if (exec.pre_exec)
|
|
|
|
exec.pre_exec();
|
|
|
|
|
|
|
|
execve(exec.argv[0], (char **) exec.argv, environ);
|
|
|
|
PLOGE("execve %s", exec.argv[0]);
|
|
|
|
exit(-1);
|
2018-11-03 08:03:11 +00:00
|
|
|
}
|
|
|
|
|
2019-02-12 21:13:31 +00:00
|
|
|
int exec_command_sync(exec_t &exec) {
|
2020-12-31 06:11:24 +00:00
|
|
|
int pid = exec_command(exec);
|
|
|
|
if (pid < 0)
|
|
|
|
return -1;
|
|
|
|
int status;
|
|
|
|
waitpid(pid, &status, 0);
|
|
|
|
return WEXITSTATUS(status);
|
2019-01-26 18:00:19 +00:00
|
|
|
}
|
|
|
|
|
2021-01-07 06:21:17 +00:00
|
|
|
int new_daemon_thread(thread_entry entry, void *arg) {
|
2020-12-31 06:11:24 +00:00
|
|
|
pthread_t thread;
|
2021-01-07 06:21:17 +00:00
|
|
|
pthread_attr_t attr;
|
|
|
|
pthread_attr_init(&attr);
|
|
|
|
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
2022-08-19 09:21:52 +00:00
|
|
|
errno = pthread_create(&thread, &attr, entry, arg);
|
|
|
|
if (errno) {
|
|
|
|
PLOGE("pthread_create");
|
|
|
|
}
|
|
|
|
return errno;
|
2019-02-14 22:36:18 +00:00
|
|
|
}
|
|
|
|
|
2019-02-15 09:31:39 +00:00
|
|
|
static char *argv0;
|
|
|
|
static size_t name_len;
|
|
|
|
void init_argv0(int argc, char **argv) {
|
2020-12-31 06:11:24 +00:00
|
|
|
argv0 = argv[0];
|
|
|
|
name_len = (argv[argc - 1] - argv[0]) + strlen(argv[argc - 1]) + 1;
|
2019-02-15 09:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void set_nice_name(const char *name) {
|
2020-12-31 06:11:24 +00:00
|
|
|
memset(argv0, 0, name_len);
|
2022-09-15 08:17:05 +00:00
|
|
|
strscpy(argv0, name, name_len);
|
2020-12-31 06:11:24 +00:00
|
|
|
prctl(PR_SET_NAME, name);
|
2019-02-15 09:31:39 +00:00
|
|
|
}
|
2019-02-18 08:05:13 +00:00
|
|
|
|
2019-03-08 01:31:35 +00:00
|
|
|
/*
|
|
|
|
* Bionic's atoi runs through strtol().
|
|
|
|
* Use our own implementation for faster conversion.
|
|
|
|
*/
|
2021-10-23 21:38:30 +00:00
|
|
|
int parse_int(string_view s) {
|
2020-12-31 06:11:24 +00:00
|
|
|
int val = 0;
|
2021-10-23 21:38:30 +00:00
|
|
|
for (char c : s) {
|
|
|
|
if (!c) break;
|
2020-12-31 06:11:24 +00:00
|
|
|
if (c > '9' || c < '0')
|
|
|
|
return -1;
|
|
|
|
val = val * 10 + c - '0';
|
|
|
|
}
|
|
|
|
return val;
|
2019-03-08 01:31:35 +00:00
|
|
|
}
|
2019-10-07 04:38:02 +00:00
|
|
|
|
|
|
|
uint32_t binary_gcd(uint32_t u, uint32_t v) {
|
2020-12-31 06:11:24 +00:00
|
|
|
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;
|
2019-10-07 04:38:02 +00:00
|
|
|
}
|
2019-11-01 07:07:12 +00:00
|
|
|
|
|
|
|
int switch_mnt_ns(int pid) {
|
2020-12-31 06:11:24 +00:00
|
|
|
char mnt[32];
|
2022-09-09 11:29:50 +00:00
|
|
|
ssprintf(mnt, sizeof(mnt), "/proc/%d/ns/mnt", pid);
|
2020-12-31 06:11:24 +00:00
|
|
|
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 = xsetns(fd, 0);
|
|
|
|
close(fd);
|
|
|
|
return ret;
|
2019-11-01 07:07:12 +00:00
|
|
|
}
|
2020-04-26 06:19:36 +00:00
|
|
|
|
|
|
|
string &replace_all(string &str, string_view from, string_view to) {
|
2020-12-31 06:11:24 +00:00
|
|
|
size_t pos = 0;
|
|
|
|
while((pos = str.find(from, pos)) != string::npos) {
|
|
|
|
str.replace(pos, from.length(), to);
|
|
|
|
pos += to.length();
|
|
|
|
}
|
|
|
|
return str;
|
2020-04-26 06:19:36 +00:00
|
|
|
}
|
2021-05-25 17:21:54 +00:00
|
|
|
|
2022-03-02 04:09:59 +00:00
|
|
|
template <class T>
|
|
|
|
static auto split_impl(T s, T delims) {
|
|
|
|
vector<std::decay_t<T>> result;
|
2021-05-25 17:21:54 +00:00
|
|
|
size_t base = 0;
|
|
|
|
size_t found;
|
|
|
|
while (true) {
|
2022-03-02 04:09:59 +00:00
|
|
|
found = s.find_first_of(delims, base);
|
2021-05-25 17:21:54 +00:00
|
|
|
result.push_back(s.substr(base, found - base));
|
2022-03-02 04:09:59 +00:00
|
|
|
if (found == string::npos)
|
|
|
|
break;
|
2021-05-25 17:21:54 +00:00
|
|
|
base = found + 1;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2022-03-02 04:09:59 +00:00
|
|
|
|
|
|
|
vector<string> split(const string &s, const string &delims) {
|
|
|
|
return split_impl<const string&>(s, delims);
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<string_view> split_ro(string_view s, string_view delims) {
|
|
|
|
return split_impl<string_view>(s, delims);
|
|
|
|
}
|
2022-09-09 11:29:50 +00:00
|
|
|
|
|
|
|
#undef vsnprintf
|
|
|
|
int vssprintf(char *dest, size_t size, const char *fmt, va_list ap) {
|
|
|
|
return std::min(vsnprintf(dest, size, fmt, ap), (int) size - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ssprintf(char *dest, size_t size, const char *fmt, ...) {
|
|
|
|
va_list va;
|
|
|
|
va_start(va, fmt);
|
|
|
|
int r = vssprintf(dest, size, fmt, va);
|
|
|
|
va_end(va);
|
|
|
|
return r;
|
|
|
|
}
|
2022-09-15 08:17:05 +00:00
|
|
|
|
|
|
|
#undef strlcpy
|
|
|
|
size_t strscpy(char *dest, const char *src, size_t size) {
|
|
|
|
return std::min(strlcpy(dest, src, size), size - 1);
|
|
|
|
}
|