kloned/main.c

00001 /*
00002  * Copyright (c) 2005-2012 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.27 2008/05/08 17:19:27 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 /* embfs load config driver */
00033 static int drv_io_open(const char *uri, void **parg);
00034 static int drv_io_close(void *arg);
00035 static char *drv_io_gets(void *arg, char *buf, size_t size);
00036 
00037 static u_config_driver_t drv_embfs = { 
00038     drv_io_open, drv_io_close, drv_io_gets, NULL 
00039 };
00040 
00041 static int drv_io_open(const char *uri, void **parg)
00042 {
00043     io_t *io = NULL;
00044 
00045     dbg_err_if(uri == NULL);
00046     dbg_err_if(strlen(uri) == 0);
00047     dbg_err_if(parg == NULL);
00048 
00049     warn_err_ifm(emb_open(uri, &io), 
00050             "unable to open embedded resource: %s", uri);
00051 
00052     *parg = io;
00053 
00054     return 0;
00055 err: 
00056     if(io)
00057         io_free(io);
00058     return ~0;
00059 }
00060 
00061 static int drv_io_close(void *arg)
00062 {
00063     io_t *io = (io_t*) arg;
00064 
00065     dbg_err_if(io == NULL);
00066 
00067     io_free(io);
00068 
00069     return 0;
00070 err: 
00071     return ~0;
00072 }
00073 
00074 static char *drv_io_gets(void *arg, char *buf, size_t size)
00075 {
00076     io_t *io = (io_t*)arg;
00077 
00078     dbg_err_if (arg == NULL);
00079     dbg_err_if (buf == NULL);
00080     
00081     nop_err_if(io_gets(io, buf, size) <= 0);
00082 
00083     return buf;
00084 err: 
00085     return NULL;
00086 }
00087 
00088 int app_init(void)
00089 {
00090     io_t *io = NULL;
00091 
00092     /* create a hook obj */
00093     dbg_err_if(hook_create(&ctx->hook));
00094 
00095     /* init embedded resources */
00096     emb_init();
00097 
00098     /* if -f is provided load the external config file */
00099     if(ctx->ext_config)
00100     {
00101         u_info("loading external config file: %s", ctx->ext_config);
00102 
00103         con_err_ifm(u_config_load_from_file(ctx->ext_config, &ctx->config),
00104                     "unable to load the configuration file: %s", 
00105                     ctx->ext_config);
00106     } else {
00107         /* if -f is not used load the default embfs config file */
00108         con_err_ifm(u_config_load_from_drv("/etc/kloned.conf", &drv_embfs, 0,
00109                     &ctx->config), 
00110                     "embfs configuration file load error");
00111     }
00112 
00113     if (ctx->cmd_config) {
00114         /* override with command-line arg config from standard input */
00115         con_err_ifm(u_config_load(ctx->config, 0, 1), 
00116                     "command-line configuration load error");
00117     }
00118 
00119     if(ctx->debug)
00120         u_config_print(ctx->config, 0);
00121 
00122 #ifdef SSL_CYASSL
00123     if(ctx->debug)
00124     {
00125         /* works if CyaSSL has been compiled with --enable-debug */
00126         CyaSSL_Debugging_ON();
00127     }
00128 #endif
00129 
00130     return 0;
00131 err:
00132     if(io)
00133         io_free(io);
00134     app_term();
00135     return ~0;
00136 }
00137 
00138 int app_term(void)
00139 {
00140     if(ctx && ctx->config)
00141     {
00142         u_config_free(ctx->config);
00143         ctx->config = NULL;
00144     }
00145 
00146     if(ctx && ctx->server)
00147     {
00148         server_free(ctx->server);
00149         ctx->server = NULL;
00150     }
00151 
00152     if(ctx && ctx->hook)
00153     {
00154         hook_free(ctx->hook);
00155         ctx->hook = NULL;
00156     }
00157 
00158     emb_term();
00159 
00160     return 0;
00161 }
00162 
00163 int app_run(void)
00164 {
00165     /* create a server object and start its main loop */
00166     dbg_err_if(server_create(ctx->config, ctx->debug, &ctx->server));
00167 
00168     if(getenv("GATEWAY_INTERFACE"))
00169         dbg_err_if(server_cgi(ctx->server));
00170     else
00171         dbg_err_if(server_loop(ctx->server));
00172 
00173 
00174     return EXIT_SUCCESS;
00175 err:
00176     return EXIT_FAILURE;
00177 }
00178 

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