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
|
|
|
*/
|
2018-11-03 03:06:01 -04:00
|
|
|
|
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>
|
2017-04-07 06:21:20 +08:00
|
|
|
#include <sys/types.h>
|
2017-04-22 06:33:40 +08:00
|
|
|
#include <sys/wait.h>
|
2018-02-11 03:40:09 +08:00
|
|
|
#include <sys/sysmacros.h>
|
2019-01-19 23:59:37 -05:00
|
|
|
#include <vector>
|
2017-04-06 06:12:29 +08:00
|
|
|
|
2017-09-14 21:54:56 +08:00
|
|
|
#include "logging.h"
|
2017-04-06 06:12:29 +08:00
|
|
|
#include "utils.h"
|
|
|
|
|
2019-01-19 23:59:37 -05:00
|
|
|
using namespace std;
|
|
|
|
|
2017-04-15 03:23:09 +08:00
|
|
|
unsigned get_shell_uid() {
|
|
|
|
struct passwd* ppwd = getpwnam("shell");
|
2018-11-03 03:06:01 -04:00
|
|
|
if (nullptr == ppwd)
|
2017-04-15 03:23:09 +08:00
|
|
|
return 2000;
|
|
|
|
|
|
|
|
return ppwd->pw_uid;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned get_system_uid() {
|
|
|
|
struct passwd* ppwd = getpwnam("system");
|
2018-11-03 03:06:01 -04:00
|
|
|
if (nullptr == ppwd)
|
2017-04-15 03:23:09 +08:00
|
|
|
return 1000;
|
|
|
|
|
|
|
|
return ppwd->pw_uid;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned get_radio_uid() {
|
|
|
|
struct passwd* ppwd = getpwnam("radio");
|
2018-11-03 03:06:01 -04:00
|
|
|
if (nullptr == ppwd)
|
2017-04-15 03:23:09 +08:00
|
|
|
return 1001;
|
|
|
|
|
|
|
|
return ppwd->pw_uid;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-02-11 03:40:09 +08:00
|
|
|
void gen_rand_str(char *buf, int len) {
|
2018-09-08 23:15:58 -04:00
|
|
|
const char base[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
2018-02-11 03:40:09 +08: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-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
|
|
|
|
|
|
|
/* 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 03:06:01 -04:00
|
|
|
*lineptr = (char *) malloc(*n);
|
2018-09-06 01:33:17 -04:00
|
|
|
if (!*lineptr) {
|
|
|
|
errno = ENOMEM;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nchars_avail = *n;
|
|
|
|
read_pos = *lineptr;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
int save_errno;
|
2018-11-03 03:06:01 -04:00
|
|
|
int c = getc(stream);
|
2018-09-06 01:33:17 -04:00
|
|
|
|
|
|
|
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 03:06:01 -04:00
|
|
|
*lineptr = (char *) realloc(*lineptr, *n);
|
2018-09-06 01:33:17 -04:00
|
|
|
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 04:03:11 -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-01-26 13:00:19 -05:00
|
|
|
int exec_command_sync(const char **argv) {
|
2019-01-26 13:39:24 -05:00
|
|
|
exec_t exec { .argv = argv };
|
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);
|
|
|
|
}
|
|
|
|
|