00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "klone_conf.h"
00012 #include <sys/types.h>
00013 #include <sys/stat.h>
00014 #include <stdio.h>
00015 #include <stdlib.h>
00016 #include <unistd.h>
00017 #include <signal.h>
00018 #include <fcntl.h>
00019 #include <u/libu.h>
00020 #include <klone/klone.h>
00021 #include <klone/server.h>
00022 #include <klone/emb.h>
00023 #include <klone/context.h>
00024 #include <klone/utils.h>
00025 #include "main.h"
00026 #include "server_s.h"
00027
00028 extern context_t* ctx;
00029 extern int modules_init(context_t *);
00030 extern int modules_term(context_t *);
00031
00032 static char *io_gets_cb(void *arg, char *buf, size_t size)
00033 {
00034 io_t *io = (io_t*)arg;
00035
00036 dbg_err_if (arg == NULL);
00037 dbg_err_if (buf == NULL);
00038
00039 dbg_err_if(io_gets(io, buf, size) <= 0);
00040
00041 return buf;
00042 err:
00043 return NULL;
00044 }
00045
00046 int app_init(void)
00047 {
00048 io_t *io = NULL;
00049 int cfg_found = 0;
00050
00051
00052 emb_init();
00053
00054
00055 dbg_err_if(u_config_create(&ctx->config));
00056
00057
00058 dbg_if(emb_open("/etc/kloned.conf", &io));
00059
00060
00061 if(io)
00062 {
00063 dbg_err_if(u_config_load_from(ctx->config, io_gets_cb, io, 0));
00064 cfg_found = 1;
00065 io_free(io);
00066 io = NULL;
00067 }
00068
00069
00070 if(ctx->ext_config)
00071 {
00072 dbg("loading external config file: %s", ctx->ext_config);
00073 dbg_err_if(u_file_open(ctx->ext_config, O_RDONLY, &io));
00074
00075 dbg_err_if(u_config_load_from(ctx->config, io_gets_cb, io, 1));
00076 cfg_found = 1;
00077
00078 io_free(io);
00079 io = NULL;
00080 }
00081
00082 con_err_ifm(cfg_found == 0,
00083 "missing config file (use -f file or embed /etc/kloned.conf");
00084
00085 if(ctx->debug)
00086 u_config_print(ctx->config, 0);
00087
00088 dbg_err_if(modules_init(ctx));
00089
00090 return 0;
00091 err:
00092 if(io)
00093 io_free(io);
00094 app_term();
00095 return ~0;
00096 }
00097
00098 int app_term(void)
00099 {
00100 modules_term(ctx);
00101
00102 if(ctx && ctx->config)
00103 {
00104 u_config_free(ctx->config);
00105 ctx->config = NULL;
00106 }
00107
00108 if(ctx && ctx->server)
00109 {
00110 server_free(ctx->server);
00111 ctx->server = NULL;
00112 }
00113
00114 emb_term();
00115
00116 return 0;
00117 }
00118
00119 int app_run(void)
00120 {
00121
00122 dbg_err_if(server_create(ctx->config, !ctx->daemon, &ctx->server));
00123
00124 if(getenv("GATEWAY_INTERFACE"))
00125 dbg_err_if(server_cgi(ctx->server));
00126 else
00127 dbg_err_if(server_loop(ctx->server));
00128
00129 return EXIT_SUCCESS;
00130 err:
00131 return EXIT_FAILURE;
00132 }
00133