Main Page | Modules | Data Structures | Directories | File List | Data Fields | Globals

kloned/main.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2005, 2006 by KoanLogic s.r.l. <http://www.koanlogic.com>
00003  * All rights reserved.
00004  *
00005  * This file is part of KLone, and as such it is subject to the license stated
00006  * in the LICENSE file which you have received as part of this distribution.
00007  *
00008  * $Id: main.c,v 1.19 2006/10/12 08:35:46 tat Exp $
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     /* init embedded resources */
00052     emb_init();
00053     
00054     /* create a config obj */
00055     dbg_err_if(u_config_create(&ctx->config));
00056 
00057     /* get the io associated to the embedded configuration file (if any) */
00058     if(emb_open("/etc/kloned.conf", &io))
00059         warn("embedded /etc/kloned.conf not found");
00060 
00061     /* load the embedded config */
00062     if(io)
00063     {
00064         con_err_ifm(u_config_load_from(ctx->config, io_gets_cb, io, 0),
00065             "configuration file load error");
00066         cfg_found = 1;
00067         io_free(io);
00068         io = NULL;
00069     }
00070 
00071     /* load the external (-f command line switch) config file */
00072     if(ctx->ext_config)
00073     {
00074         info("loading external config file: %s", ctx->ext_config);
00075 
00076         con_err_ifm(u_file_open(ctx->ext_config, O_RDONLY, &io),
00077             "unable to access configuration file: %s", ctx->ext_config);
00078 
00079         con_err_ifm(u_config_load_from(ctx->config, io_gets_cb, io, 1),
00080             "configuration file load error");
00081 
00082         cfg_found = 1;
00083 
00084         io_free(io);
00085         io = NULL;
00086     }
00087 
00088     con_err_ifm(cfg_found == 0, 
00089         "missing config file (use -f file or embed /etc/kloned.conf");
00090 
00091     if(ctx->debug)
00092         u_config_print(ctx->config, 0);
00093 
00094     dbg_err_if(modules_init(ctx));
00095 
00096     return 0;
00097 err:
00098     if(io)
00099         io_free(io);
00100     app_term();
00101     return ~0;
00102 }
00103 
00104 int app_term(void)
00105 {
00106     modules_term(ctx);
00107 
00108     if(ctx && ctx->config)
00109     {
00110         u_config_free(ctx->config);
00111         ctx->config = NULL;
00112     }
00113 
00114     if(ctx && ctx->server)
00115     {
00116         server_free(ctx->server);
00117         ctx->server = NULL;
00118     }
00119 
00120     emb_term();
00121 
00122     return 0;
00123 }
00124 
00125 int app_run(void)
00126 {
00127     /* create a server object and start its main loop */
00128     dbg_err_if(server_create(ctx->config, !ctx->daemon, &ctx->server));
00129 
00130     if(getenv("GATEWAY_INTERFACE"))
00131         dbg_err_if(server_cgi(ctx->server));
00132     else
00133         dbg_err_if(server_loop(ctx->server));
00134 
00135     return EXIT_SUCCESS;
00136 err:
00137     return EXIT_FAILURE;
00138 }
00139 

←Products
© 2005-2006 - KoanLogic S.r.l. - All rights reserved