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-08-20 13:36:32 +00:00
|
|
|
#include <stdarg.h>
|
2017-04-06 22:21:20 +00:00
|
|
|
#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-06-08 14:56:21 +00:00
|
|
|
#include <sched.h>
|
2017-11-15 13:00:52 +00:00
|
|
|
#include <unistd.h>
|
2017-12-06 17:30:48 +00:00
|
|
|
#include <libgen.h>
|
2018-09-06 05:33:17 +00:00
|
|
|
#include <syscall.h>
|
2017-04-06 22:21:20 +00:00
|
|
|
#include <sys/types.h>
|
2017-04-15 10:33:16 +00:00
|
|
|
#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>
|
2017-12-06 17:30:48 +00:00
|
|
|
#include <sys/inotify.h>
|
2018-02-10 19:40:09 +00:00
|
|
|
#include <sys/sysmacros.h>
|
2017-04-05 22:12:29 +00:00
|
|
|
|
2017-09-14 13:54:56 +00:00
|
|
|
#include "logging.h"
|
2017-04-05 22:12:29 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
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
|
|
|
/* 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) {
|
2018-06-16 21:16:52 +00:00
|
|
|
if (access(filename, R_OK) != 0)
|
|
|
|
return 1;
|
2017-04-05 22:12:29 +00:00
|
|
|
char *line = NULL;
|
|
|
|
size_t len = 0;
|
|
|
|
ssize_t read;
|
|
|
|
|
2017-09-05 18:25:40 +00:00
|
|
|
FILE *fp = xfopen(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 */
|
2018-10-13 01:46:09 +00:00
|
|
|
int is_num(const char *s) {
|
2017-04-06 22:21:20 +00: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 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';
|
2018-01-12 21:49:03 +00:00
|
|
|
while (len < size - 1) {
|
|
|
|
int ret = read(fd, buf + len, 1);
|
|
|
|
if (ret < 0)
|
|
|
|
return -1;
|
|
|
|
if (ret == 0)
|
|
|
|
break;
|
2017-06-02 20:31:01 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
2018-07-05 23:32:16 +00:00
|
|
|
*setenv -> A callback function which sets up a vector of environment variables
|
2017-06-07 19:20:49 +00:00
|
|
|
*/
|
2018-07-05 23:32:16 +00:00
|
|
|
int exec_array(int err, int *fd, void (*setenv)(struct vector *), 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
|
|
|
|
2017-08-30 18:20:06 +00:00
|
|
|
// Setup environment
|
2018-07-13 14:14:32 +00:00
|
|
|
char **envp;
|
2017-08-30 18:20:06 +00:00
|
|
|
struct vector env;
|
|
|
|
vec_init(&env);
|
2018-07-05 23:32:16 +00:00
|
|
|
if (setenv) {
|
|
|
|
setenv(&env);
|
2017-08-30 18:20:06 +00:00
|
|
|
envp = (char **) vec_entry(&env);
|
|
|
|
} else {
|
|
|
|
extern char **environ;
|
|
|
|
envp = environ;
|
|
|
|
}
|
2017-08-26 18:17:37 +00:00
|
|
|
|
2017-10-13 16:08:12 +00:00
|
|
|
int pid = xfork();
|
2017-04-18 11:32:50 +00:00
|
|
|
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-08-30 18:20:06 +00:00
|
|
|
vec_deep_destroy(&env);
|
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);
|
2018-07-05 23:32:16 +00:00
|
|
|
if (err)
|
|
|
|
xdup2(writeEnd, STDERR_FILENO);
|
2017-04-21 22:33:40 +00:00
|
|
|
}
|
|
|
|
|
2018-07-13 14:14:32 +00:00
|
|
|
environ = envp;
|
|
|
|
execvp(argv[0], argv);
|
|
|
|
PLOGE("execvp %s", argv[0]);
|
2017-04-18 11:32:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2017-04-21 22:33:40 +00:00
|
|
|
|
2018-07-05 23:32:16 +00:00
|
|
|
static int v_exec_command(int err, int *fd, void (*setenv)(struct vector*), const char *argv0, va_list argv) {
|
|
|
|
// Collect va_list into vector
|
|
|
|
struct vector args;
|
|
|
|
vec_init(&args);
|
|
|
|
vec_push_back(&args, strdup(argv0));
|
|
|
|
for (void *arg = va_arg(argv, void*); arg; arg = va_arg(argv, void*))
|
|
|
|
vec_push_back(&args, strdup(arg));
|
|
|
|
vec_push_back(&args, NULL);
|
|
|
|
int pid = exec_array(err, fd, setenv, (char **) vec_entry(&args));
|
|
|
|
vec_deep_destroy(&args);
|
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
|
2017-08-20 13:36:32 +00: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);
|
|
|
|
}
|
|
|
|
|
2018-07-05 23:32:16 +00:00
|
|
|
int exec_command(int err, int *fd, void (*setenv)(struct vector*), const char *argv0, ...) {
|
2017-08-20 13:36:32 +00:00
|
|
|
va_list argv;
|
|
|
|
va_start(argv, argv0);
|
2018-07-05 23:32:16 +00:00
|
|
|
int pid = v_exec_command(err, fd, setenv, argv0, argv);
|
2017-08-20 13:36:32 +00:00
|
|
|
va_end(argv);
|
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
|
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
|
2018-01-10 19:06:20 +00:00
|
|
|
ret = xsetns(fd, 0);
|
2017-06-08 14:56:21 +00:00
|
|
|
close(fd);
|
|
|
|
return ret;
|
|
|
|
}
|
2017-10-08 21:05:52 +00:00
|
|
|
|
|
|
|
int fork_dont_care() {
|
2017-10-13 16:08:12 +00:00
|
|
|
int pid = xfork();
|
2017-10-08 21:05:52 +00:00
|
|
|
if (pid) {
|
|
|
|
waitpid(pid, NULL, 0);
|
|
|
|
return pid;
|
2017-10-13 16:08:12 +00:00
|
|
|
} else if ((pid = xfork())) {
|
2017-10-08 21:05:52 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2017-12-06 17:30:48 +00: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);
|
|
|
|
}
|
2018-02-10 19:40:09 +00:00
|
|
|
|
|
|
|
void gen_rand_str(char *buf, int len) {
|
2018-09-09 03:15:58 +00:00
|
|
|
const char base[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
2018-02-10 19:40:09 +00:00
|
|
|
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';
|
|
|
|
}
|
2018-07-20 21:12:22 +00: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 05:33:17 +00:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
*lineptr = malloc(*n);
|
|
|
|
if (!*lineptr) {
|
|
|
|
errno = ENOMEM;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nchars_avail = *n;
|
|
|
|
read_pos = *lineptr;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
int save_errno;
|
|
|
|
register 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;
|
|
|
|
*lineptr = 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);
|
|
|
|
}
|