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

backend.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: backend.c,v 1.24 2006/10/11 16:31:42 tat Exp $
00009  */
00010 
00011 #include <unistd.h>
00012 #include <klone/backend.h>
00013 #include <klone/server.h>
00014 #include <klone/klog.h>
00015 #include <klone/context.h>
00016 #include <u/libu.h>
00017 #include "klone_conf.h"
00018 
00019 extern backend_t be_http;
00020 
00021 #ifdef HAVE_LIBOPENSSL
00022 extern backend_t be_https;
00023 #endif
00024 
00025 backend_t *backend_list[] = { 
00026     &be_http, 
00027 #ifdef HAVE_LIBOPENSSL
00028     &be_https, 
00029 #endif
00030     0 };
00031 
00032 
00033 static int backend_set_model(backend_t *be, const char *v)
00034 {
00035     dbg_return_if (v == NULL, ~0);
00036     dbg_return_if (be == NULL, ~0);
00037 
00038     if(!strcasecmp(v, "fork"))
00039         be->model = SERVER_MODEL_FORK;
00040     else if(!strcasecmp(v, "iterative"))
00041         be->model = SERVER_MODEL_ITERATIVE;
00042     else if(!strcasecmp(v, "prefork"))
00043         be->model = SERVER_MODEL_PREFORK;
00044     else
00045         warn_err("unknown server model [%s]", v);
00046 
00047     return 0;
00048 err:
00049     return ~0;
00050 }
00051 
00052 int backend_create(const char *proto, u_config_t *config, backend_t **pbe)
00053 {
00054     backend_t *be = NULL, **pp;
00055     const char *v;
00056 
00057     dbg_return_if (proto == NULL, ~0);
00058     dbg_return_if (config == NULL, ~0);
00059     dbg_return_if (pbe == NULL, ~0);
00060     
00061     be = u_zalloc(sizeof(backend_t));
00062     dbg_err_if(be == NULL);
00063 
00064     /* find the requested backend type */
00065     for(pp = backend_list; *pp != NULL; ++pp)
00066         if(!strcasecmp((*pp)->proto, proto))
00067             break; /* found */
00068 
00069     warn_err_ifm(*pp == NULL, "backend type \"%s\" not found", proto);
00070 
00071     /* copy static backend struct fields */
00072     memcpy(be, *pp, sizeof(backend_t));
00073 
00074     be->config = config;
00075     #ifdef OS_WIN
00076     be->model = SERVER_MODEL_ITERATIVE;  /* default */
00077     #else
00078     be->model = SERVER_MODEL_PREFORK;  /* default */
00079     #endif
00080 
00081     if((v = u_config_get_subkey_value(config, "model")) != NULL)
00082         dbg_err_if(backend_set_model(be, v));
00083 
00084     if(be->model == SERVER_MODEL_FORK)
00085     {
00086         /* max # of child allowed to run at once */
00087         dbg_err_if(u_config_get_subkey_value_i(config, "fork.max_child", 
00088             SERVER_MAX_BACKEND_CHILD, (int *)&be->max_child));
00089     }
00090 
00091     if(be->model == SERVER_MODEL_PREFORK)
00092     {
00093         /* max # of child allowed to run at once */
00094         dbg_err_if(u_config_get_subkey_value_i(config, "prefork.max_child", 
00095             SERVER_MAX_BACKEND_CHILD, (int *)&be->max_child));
00096         
00097         /* # of child to run at startup */
00098         dbg_err_if(u_config_get_subkey_value_i(config, "prefork.start_child", 
00099             SERVER_PREFORK_START_CHILD, (int *)&be->start_child));
00100 
00101         /* max # of requests a child process will handle before it dies */
00102         dbg_err_if(u_config_get_subkey_value_i(config, 
00103             "prefork.max_requests_per_child", SERVER_PREFORK_MAX_RQ_CHILD, 
00104             (int *)&be->max_rq_xchild));
00105 
00106         /* set start_child child to be forked when possible */
00107         be->fork_child = be->start_child;
00108     }
00109 
00110     /* call backend initialization function */
00111     if(be->cb_init)
00112         warn_err_ifm(be->cb_init(be), "backend (%s) init error", proto);
00113 
00114     *pbe = be;
00115 
00116     return 0;
00117 err:
00118     U_FREE(be);
00119     return ~0;
00120 }
00121 
00122 int backend_serve(backend_t *be, int fd)
00123 {
00124     dbg_return_if (be == NULL, ~0);
00125     dbg_return_if (fd < 0, ~0);
00126     
00127     dbg_err_if(be->cb_serve == NULL);
00128 
00129     be->cb_serve(be, fd);
00130 
00131     return 0;
00132 err:
00133     return ~0;
00134 }
00135 
00136 int backend_free(backend_t *be)
00137 {
00138     if(be)
00139     {
00140         /* children must not call klog_close (see comment in server.c) */
00141         if(be->klog && ctx->pipc == 0)
00142             klog_close(be->klog);
00143         if(be->cb_term)
00144             be->cb_term(be);
00145         U_FREE(be);
00146     }
00147     return 0;
00148 }
00149 

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