Hash :
58a36064
Author :
Thomas de Grivel
Date :
2024-09-10T18:41:53
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
/* kc3
* Copyright 2022,2023,2024 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software granted the above
* copyright notice and this permission paragraph are included in all
* copies and substantial portions of this software.
*
* THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
* PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
#include <signal.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <libkc3/kc3.h>
#include "config.h"
int main (int argc, char **argv)
{
bool daemonize = true;
s_ident daemonize_ident;
s_tag daemonize_value;
char log_buf[64] = {0};
s32 log_fd = -1;
s_str log_str = {0};
s_call call = {0};
const s_sym *module = NULL;
char *p;
int r = 1;
time_t t;
s_tag tmp = {0};
const struct tm *utc = NULL;
kc3_init(NULL, &argc, &argv);
if (argc > 0 && argv[0] && argv[0][0] == '-') {
p = argv[0] + 1;
while (*p) {
switch (*p) {
case 'd':
daemonize = false;
break;
default:
err_write_1("kc3_httpd: unknown flag: -");
err_write_u8(*p);
err_write_1("\n");
assert(! "kc3_httpd: unknown flag");
kc3_clean(NULL);
return 1;
}
p++;
}
argc--;
argv++;
}
if (daemonize) {
if ((t = time(NULL)) == -1) {
kc3_clean(NULL);
return 1;
}
if (! (utc = gmtime(&t))) {
kc3_clean(NULL);
return 1;
}
strftime(log_buf, sizeof(log_buf) - 1, "log/kc3_httpd_%F_%T.log",
utc);
str_init_1(&log_str, NULL, log_buf);
if (! file_open_w(&log_str, &log_fd)) {
str_clean(&log_str);
kc3_clean(NULL);
return 1;
}
buf_file_close(&g_kc3_env.out);
dup2(log_fd, 1);
buf_fd_open_w(&g_kc3_env.out, 1);
buf_file_close(&g_kc3_env.err);
dup2(log_fd, 2);
buf_fd_open_w(&g_kc3_env.err, 2);
buf_file_close(&g_kc3_env.in);
close(0);
}
ident_init(&daemonize_ident, &g_sym_KC3, sym_1("daemonize"));
tag_init_bool(&daemonize_value, daemonize);
env_def(&g_kc3_env, &daemonize_ident, &daemonize_value);
io_puts("KC3 HTTPd loading, please wait...");
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
err_puts("http_event_base_new: signal");
assert(! "http_event_base_new: signal");
kc3_clean(NULL);
return 1;
}
module = sym_1("HTTPd");
if (! module_load(module)) {
kc3_clean(NULL);
return 1;
}
call_init(&call);
call.ident.module = module;
call.ident.sym = sym_1("main");
//FIXME
if (argc >= 2)
call.arguments = list_new_str_1
(NULL, argv[0], list_new_str_1
(NULL, argv[1], NULL));
else if (argc == 1)
call.arguments = list_new_str_1(NULL, argv[0], NULL);
else
call.arguments = NULL;
if (! eval_call(&call, &tmp))
goto clean;
switch (tmp.type) {
case TAG_U8:
r = tmp.data.u8;
break;
default:
err_write_1("invalid return type from main: ");
err_inspect_tag(&tmp);
err_write_1("\n");
}
clean:
tag_clean(&tmp);
call_clean(&call);
kc3_clean(NULL);
return r;
}