Showing with 81 additions and 60 deletions.
  1. +4 −3 .gitmodules
  2. +1 −1 deps/saneopt
  3. +72 −52 src/forza.c
  4. +4 −4 src/plugins/start.c
@@ -1,9 +1,10 @@
[submodule "deps/libuv"]
path = deps/libuv
url = https://github.com/joyent/libuv.git
[submodule "deps/saneopt"]
path = deps/saneopt
url = https://github.com/mmalecki/saneopt.git
[submodule "deps/env"]
path = deps/env
url = https://github.com/mmalecki/env.git
[submodule "deps/saneopt"]
path = deps/saneopt
url = https://github.com/mmalecki/saneopt.git
branch = refactor
Submodule saneopt updated 6 files
+26 −16 Makefile
+1 −0 config.mk
+51 −40 include/saneopt.h
+30 −0 src/main.c
+268 −113 src/saneopt.c
+0 −295 test/test-saneopt.c
@@ -25,7 +25,7 @@ static uv_pipe_t child_ipc;

static uv_process_t* child = NULL;

static saneopt_t* opt;
static saneopt_t* saneopt;

void on_process_exit(uv_process_t* process, int exit_status, int term_signal) {
int i;
@@ -154,7 +154,7 @@ void on_connect(int status) {
}

for (i = 0; i < PLUGIN_COUNT; i++) {
plugins[i].saneopt = opt;
plugins[i].saneopt = saneopt;
if (_plugin_init_calls[i](&plugins[i]) != 0) {
fprintf(stderr, "error initializing plugin %i\n", i);
}
@@ -181,13 +181,10 @@ void forza__on_sigterm() {
}

int main(int argc, char *argv[]) {
char* host;
char* hostname;
char* user;
char* name;
char* port_str;
saneopt_option_t *host, *hostname, *user, *name, *port_str, *version,
*logfile;
int port;
int i, c, v = 0;
int i, c = 0;
uv_interface_address_t* addresses;
uv_err_t err;

@@ -198,65 +195,88 @@ int main(int argc, char *argv[]) {

loop = uv_default_loop();

saneopt = saneopt_init();

saneopt->header = "forza";

host = saneopt_option(saneopt, "host");
saneopt_alias(host, "h");
host->desc = "Host to connect to";

port_str = saneopt_option(saneopt, "port");
saneopt_alias(port_str, "p");
port_str->desc = "Port to connect to";

hostname = saneopt_option(saneopt, "hostname");
hostname->desc = "Local hostname to bind to";

user = saneopt_option(saneopt, "user");
user->desc = "Application owner's username";

name = saneopt_option(saneopt, "name");
name->desc = "Application name";

#ifdef _LOGS_H
logfile = saneopt_option(saneopt, "log");
logfile->desc = "Location where the logfile is to be saved";
#endif

version = saneopt_option(saneopt, "version");
saneopt_alias(version, "v");
version->desc = "Print version and exit";
version->requires_value = 0;

saneopt_parse(saneopt, argc, argv);

if (version->available) {
printf("%s\n", FORZA_VERSION_HASH);
}

#ifdef FORZA_VERSION_HASH
printf("forza %s\n", FORZA_VERSION_HASH);
// saneopt doesn't allow options without input
for (v = 0; v < argc; v++) {
if (strcmp(argv[v], "-v") == 0 || strcmp(argv[v], "--version") == 0) {
return 0;
}
}
#else
printf("forza\n");
#endif

opt = saneopt_init(argc - 1, argv + 1);

saneopt_alias(opt, "host", "h");
saneopt_alias(opt, "port", "p");

host = saneopt_get(opt, "host");
port_str = saneopt_get(opt, "port");
hostname = saneopt_get(opt, "hostname");
user = saneopt_get(opt, "app-user");
name = saneopt_get(opt, "app-name");
arguments = saneopt_arguments(opt);

if (host == NULL || port_str == NULL) {
if (host->value == NULL || port_str->value == NULL) {
fprintf(stderr, "Host and port required\n");
return 2;
}

sscanf(port_str, "%d", &port);
if (port <= 0 || port > 65535) {
fprintf(stderr, "Port has to be <= 0 and > 65535\n");
return 3;
else if (host->value == NULL) {
fprintf(stderr, "Host required.\n");
}

if (hostname == NULL) {
hostname = malloc(256 * sizeof(*hostname));
err = uv_interface_addresses(&addresses, &c);
if (err.code != UV_OK) {
fprintf(stderr, "uv_interface_addresses: %s\n", uv_err_name(uv_last_error(loop)));
return 1;
else {
sscanf(port_str->value, "%d", &port);
if (port <= 0 || port > 65535) {
fprintf(stderr, "Port has to be <= 0 and > 65535\n");
}
for (i = 0; i < c; i++) {
/* For now, only grab the first non-internal, non 0.0.0.0 interface.
* TODO: Make this smarter.
*/
if (addresses[i].is_internal) continue;
uv_ip4_name(&addresses[i].address.address4, hostname, 255 * sizeof(*hostname));
if (strcmp(hostname, "0.0.0.0") != 0) break;

if (hostname->value == NULL) {
hostname->value = malloc(256 * sizeof(char));
err = uv_interface_addresses(&addresses, &c);
if (err.code != UV_OK) {
fprintf(stderr, "uv_interface_addresses: %s\n", uv_err_name(uv_last_error(loop)));
return 1;
}
for (i = 0; i < c; i++) {
/* For now, only grab the first non-internal, non 0.0.0.0 interface.
* TODO: Make this smarter.
*/
if (addresses[i].is_internal) continue;
uv_ip4_name(&addresses[i].address.address4, hostname->value, 255 * sizeof(char));
if (strcmp(hostname->value, "0.0.0.0") != 0) break;
}
uv_free_interface_addresses(addresses, c);
}
uv_free_interface_addresses(addresses, c);
}

forza_connect(host, port, hostname, user, name, on_connect);
forza_connect(host->value, port, hostname->value, user->value, name->value, on_connect);

uv_run(loop, UV_RUN_DEFAULT);
uv_run(loop, UV_RUN_DEFAULT);

free(hostname);
free(opt);
return 0;
}

return 0;
saneopt_free(saneopt);
return 1;
}
@@ -108,7 +108,7 @@ void start__process_exit_cb(int exit_status, int term_signal) {
}

int start_init(forza_plugin_t* plugin) {
char* log_file_path;
saneopt_option_t* log_file_path;
size_t size = PATHMAX / sizeof(*lib_path);

lib_path = malloc(size);
@@ -126,13 +126,13 @@ int start_init(forza_plugin_t* plugin) {
strcpy(strrchr(lib_path, '/') + 1, "libinterposed.so");
#endif

log_file_path = saneopt_get(plugin->saneopt, "start-log");
log_file_path = saneopt_option(plugin->saneopt, "log");

if (log_file_path != NULL) {
if (log_file_path->value != NULL) {
log_file_fd = uv_fs_open(
loop,
&log_file,
log_file_path,
log_file_path->value,
O_WRONLY | O_CREAT | O_TRUNC,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH,
NULL