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

kloned/main.c

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.25 2007/10/17 22:58:35 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/os.h>
00021 #include <klone/server.h>
00022 #include <klone/emb.h>
00023 #include <klone/context.h>
00024 #include <klone/utils.h>
00025 #include <klone/hook.h>
00026 #include <klone/hookprv.h>
00027 #include "main.h"
00028 #include "server_s.h"
00029 
00030 extern context_t* ctx;
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     nop_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     /* create a hook obj */
00052     dbg_err_if(hook_create(&ctx->hook));
00053 
00054     /* init embedded resources */
00055     emb_init();
00056     
00057     /* create a config obj */
00058     dbg_err_if(u_config_create(&ctx->config));
00059 
00060     /* if -f is provided load the external config file */
00061     if(ctx->ext_config)
00062     {
00063         info("loading external config file: %s", ctx->ext_config);
00064 
00065         con_err_ifm(u_file_open(ctx->ext_config, O_RDONLY, &io),
00066             "unable to access configuration file: %s", ctx->ext_config);
00067 
00068         /* if there's the embconfig then overwrite otherwise laod as-is 
00069          * (multiple keys will not get overwritten eg. dir_alias) */
00070         con_err_ifm(u_config_load_from(ctx->config, io_gets_cb, io, 0),
00071             "configuration file load error");
00072 
00073         cfg_found = 1;
00074 
00075         io_free(io);
00076         io = NULL;
00077     } else {
00078         /* if -f is not used try to load the embfs config file */
00079 
00080         /* get the io associated to the embedded configuration file (if any) */
00081         if(emb_open("/etc/kloned.conf", &io))
00082             warn("embedded /etc/kloned.conf not found");
00083 
00084         /* load the embedded config */
00085         if(io)
00086         {
00087             con_err_ifm(u_config_load_from(ctx->config, io_gets_cb, io, 0),
00088                 "embfs configuration file load error");
00089             cfg_found = 1;
00090             io_free(io);
00091             io = NULL;
00092         }
00093     }
00094 
00095     con_err_ifm(cfg_found == 0, 
00096         "missing config file (use -f file or embed /etc/kloned.conf");
00097 
00098     if(ctx->debug)
00099         u_config_print(ctx->config, 0);
00100 
00101     return 0;
00102 err:
00103     if(io)
00104         io_free(io);
00105     app_term();
00106     return ~0;
00107 }
00108 
00109 int app_term(void)
00110 {
00111     if(ctx && ctx->config)
00112     {
00113         u_config_free(ctx->config);
00114         ctx->config = NULL;
00115     }
00116 
00117     if(ctx && ctx->server)
00118     {
00119         server_free(ctx->server);
00120         ctx->server = NULL;
00121     }
00122 
00123     if(ctx && ctx->hook)
00124     {
00125         hook_free(ctx->hook);
00126         ctx->hook = NULL;
00127     }
00128 
00129     emb_term();
00130 
00131     return 0;
00132 }
00133 
00134 int app_run(void)
00135 {
00136     /* create a server object and start its main loop */
00137     dbg_err_if(server_create(ctx->config, ctx->debug, &ctx->server));
00138 
00139     if(getenv("GATEWAY_INTERFACE"))
00140         dbg_err_if(server_cgi(ctx->server));
00141     else
00142         dbg_err_if(server_loop(ctx->server));
00143 
00144 
00145     return EXIT_SUCCESS;
00146 err:
00147     return EXIT_FAILURE;
00148 }
00149