2021-06-17 21:32:24 +00:00
|
|
|
#include <cerrno>
|
|
|
|
#include <cstring>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <csignal>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <fstream>
|
|
|
|
|
2021-11-14 04:29:48 +00:00
|
|
|
#include <media-layer/core.h>
|
|
|
|
|
2021-06-17 21:32:24 +00:00
|
|
|
#include "../common/common.h"
|
|
|
|
|
|
|
|
// Track Client State
|
|
|
|
static int _client_is_alive = 0;
|
|
|
|
static int _client_status = 0;
|
|
|
|
static void update_client_state(int is_alive, int status) {
|
|
|
|
_client_is_alive = is_alive;
|
|
|
|
_client_status = status;
|
|
|
|
}
|
|
|
|
// Check State Of Proxy And Exit If Invalid
|
|
|
|
void _check_proxy_state() {
|
|
|
|
// Check Client State
|
|
|
|
if (!_client_is_alive) {
|
2022-03-06 23:22:28 +00:00
|
|
|
void_write_cache(); // Child Is Dead, No Reason To Send A Dead Process Data
|
2022-05-15 17:51:28 +00:00
|
|
|
char *exit_status = NULL;
|
|
|
|
get_exit_status_string(_client_status, &exit_status);
|
|
|
|
PROXY_ERR("Client Terminated%s", exit_status);
|
2021-06-17 21:32:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start Proxy Client
|
|
|
|
static pid_t _client_pid;
|
|
|
|
static void sigchld_handler(__attribute__((unused)) int sig) {
|
|
|
|
// Track
|
|
|
|
int status;
|
|
|
|
|
|
|
|
// Reap
|
|
|
|
int saved_errno = errno;
|
|
|
|
// Only waitpid() Proxy Client, Other Sub-Processes Are Handled By pclose()
|
|
|
|
if (waitpid(_client_pid, &status, WNOHANG) == _client_pid) {
|
|
|
|
// Handle Client Death
|
2022-05-16 22:56:19 +00:00
|
|
|
untrack_child(_client_pid);
|
2021-06-17 21:32:24 +00:00
|
|
|
update_client_state(0, status);
|
|
|
|
}
|
|
|
|
errno = saved_errno;
|
|
|
|
}
|
|
|
|
static void start_media_layer_proxy_client(int read, int write) {
|
|
|
|
// Reap Children
|
|
|
|
struct sigaction sa;
|
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
sa.sa_flags = SA_NOCLDSTOP;
|
|
|
|
sa.sa_handler = &sigchld_handler;
|
|
|
|
if (sigaction(SIGCHLD, &sa, NULL) == -1) {
|
|
|
|
PROXY_ERR("Unable To Install Signal Handler: %s", strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fork And Start
|
|
|
|
pid_t ret = fork();
|
|
|
|
if (ret == -1) {
|
|
|
|
PROXY_ERR("Unable To Launch Client: %s", strerror(errno));
|
|
|
|
} else if (ret == 0) {
|
|
|
|
// Child Process
|
|
|
|
|
|
|
|
// Prepare Arguments
|
|
|
|
char *read_str = NULL;
|
|
|
|
safe_asprintf(&read_str, "%i", read);
|
|
|
|
char *write_str = NULL;
|
|
|
|
safe_asprintf(&write_str, "%i", write);
|
2022-03-14 23:09:25 +00:00
|
|
|
const char *argv[] = {"media-layer-proxy-client", read_str, write_str, NULL};
|
2021-06-17 21:32:24 +00:00
|
|
|
|
|
|
|
// Run
|
2022-03-14 23:09:25 +00:00
|
|
|
safe_execvpe(argv, (const char *const *) environ);
|
2021-06-17 21:32:24 +00:00
|
|
|
} else {
|
|
|
|
// Parent Process
|
|
|
|
_client_pid = ret;
|
2022-05-14 02:36:12 +00:00
|
|
|
track_child(_client_pid);
|
2021-06-17 21:32:24 +00:00
|
|
|
}
|
|
|
|
update_client_state(1, 0);
|
|
|
|
}
|
|
|
|
|
2022-06-04 18:34:15 +00:00
|
|
|
// Maximize Pipe Buffer Size
|
|
|
|
static void maximize_pipe_fd_size(int fd) {
|
|
|
|
// Read Maximum Pipe Size
|
|
|
|
std::ifstream max_size_file("/proc/sys/fs/pipe-max-size");
|
|
|
|
if (!max_size_file.good()) {
|
|
|
|
PROXY_ERR("%s", "Unable To Open Maximum Pipe Size File");
|
|
|
|
}
|
|
|
|
// Read One Line
|
|
|
|
int max_size;
|
|
|
|
std::string line;
|
|
|
|
if (std::getline(max_size_file, line) && line.size() > 0) {
|
|
|
|
max_size = std::stoi(line);
|
|
|
|
} else {
|
|
|
|
PROXY_ERR("%s", "Unable To Read Maximum Pipe Size File");
|
|
|
|
}
|
|
|
|
// Close
|
|
|
|
max_size_file.close();
|
|
|
|
// Set Maximum Pipe Size
|
|
|
|
errno = 0;
|
|
|
|
if (fcntl(fd, F_SETPIPE_SZ, max_size) < max_size) {
|
|
|
|
PROXY_ERR("Unable To Set Maximum Pipe Size: %s", errno != 0 ? strerror(errno) : "Unknown Error");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
static void maximize_pipe_size(int pipe[2]) {
|
|
|
|
maximize_pipe_fd_size(pipe[0]);
|
|
|
|
maximize_pipe_fd_size(pipe[1]);
|
|
|
|
}
|
|
|
|
|
2021-06-17 21:32:24 +00:00
|
|
|
// Start Server
|
2021-11-14 04:29:48 +00:00
|
|
|
static int loaded = 0;
|
|
|
|
__attribute__((constructor)) void media_ensure_loaded() {
|
|
|
|
if (!loaded) {
|
|
|
|
loaded = 1;
|
|
|
|
|
|
|
|
// Log
|
2022-04-15 01:12:42 +00:00
|
|
|
PROXY_INFO("Starting...");
|
2021-11-14 04:29:48 +00:00
|
|
|
|
|
|
|
// Create Connection
|
|
|
|
int server_to_client_pipe[2];
|
|
|
|
safe_pipe2(server_to_client_pipe, 0);
|
2022-06-04 18:34:15 +00:00
|
|
|
maximize_pipe_size(server_to_client_pipe);
|
2021-11-14 04:29:48 +00:00
|
|
|
int client_to_server_pipe[2];
|
|
|
|
safe_pipe2(client_to_server_pipe, 0);
|
2022-06-04 18:34:15 +00:00
|
|
|
maximize_pipe_size(client_to_server_pipe);
|
2021-11-14 04:29:48 +00:00
|
|
|
// Set Connection
|
|
|
|
set_connection(client_to_server_pipe[0], server_to_client_pipe[1]);
|
|
|
|
|
|
|
|
// Start Client
|
|
|
|
start_media_layer_proxy_client(server_to_client_pipe[0], client_to_server_pipe[1]);
|
|
|
|
|
|
|
|
// Wait For Connection Message
|
|
|
|
char *str = read_string();
|
|
|
|
if (strcmp(str, CONNECTED_MSG) == 0) {
|
2022-04-15 01:12:42 +00:00
|
|
|
PROXY_INFO("Connected");
|
2021-11-14 04:29:48 +00:00
|
|
|
} else {
|
2022-04-15 01:12:42 +00:00
|
|
|
PROXY_ERR("Unable To Connect");
|
2021-11-14 04:29:48 +00:00
|
|
|
}
|
|
|
|
// Free
|
|
|
|
free(str);
|
2021-06-17 21:32:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assign Unique ID To Function
|
|
|
|
static std::unordered_map<std::string, unsigned char> &get_unique_ids() {
|
|
|
|
static std::unordered_map<std::string, unsigned char> unique_ids;
|
|
|
|
return unique_ids;
|
|
|
|
}
|
|
|
|
void _assign_unique_id(const char *name, unsigned char id) {
|
|
|
|
get_unique_ids()[name] = id;
|
|
|
|
}
|
2021-07-20 21:22:56 +00:00
|
|
|
unsigned char _get_unique_id(const char *name) {
|
|
|
|
return get_unique_ids()[name]; // Assume ID Exists
|
2021-06-17 21:32:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The Proxy Is Single-Threaded
|
|
|
|
static pthread_mutex_t proxy_mutex = PTHREAD_MUTEX_INITIALIZER;
|
2021-07-20 21:22:56 +00:00
|
|
|
void _start_proxy_call(unsigned char call_id) {
|
2021-06-17 21:32:24 +00:00
|
|
|
// Lock Proxy
|
|
|
|
pthread_mutex_lock(&proxy_mutex);
|
2021-07-20 21:22:56 +00:00
|
|
|
write_byte(call_id);
|
2021-06-17 21:32:24 +00:00
|
|
|
}
|
|
|
|
void end_proxy_call() {
|
2021-06-23 21:52:31 +00:00
|
|
|
// Flush Write Cache
|
|
|
|
flush_write_cache();
|
2021-06-17 21:32:24 +00:00
|
|
|
// Release Proxy
|
|
|
|
pthread_mutex_unlock(&proxy_mutex);
|
|
|
|
}
|