2
0
Fork 0
lighttpd2/src/main/lighttpd.c

156 lines
4.0 KiB
C
Raw Normal View History

#include <lighttpd/base.h>
#include <lighttpd/plugin_core.h>
2008-07-25 21:12:40 +00:00
2009-09-15 22:19:06 +00:00
#include <lighttpd/version.h>
2010-05-13 10:17:33 +00:00
#ifdef WITH_PROFILER
# include <lighttpd/profiler.h>
#endif
2008-07-25 21:12:40 +00:00
#ifdef HAVE_LUA_H
2009-07-31 18:36:37 +00:00
# include <lighttpd/config_lua.h>
#endif
#ifndef WITHOUT_CONFIG_PARSER
# include <lighttpd/config_parser.h>
#endif
2009-07-31 18:36:37 +00:00
#ifndef DEFAULT_LIBDIR
2010-01-03 18:47:47 +00:00
# define DEFAULT_LIBDIR "/usr/local/lib/lighttpd2"
2008-07-25 21:12:40 +00:00
#endif
2008-07-17 21:12:52 +00:00
int main(int argc, char *argv[]) {
GError *error = NULL;
2008-07-17 21:12:52 +00:00
GOptionContext *context;
liServer *srv;
2008-08-02 20:56:07 +00:00
gboolean res;
gboolean free_config_path = TRUE;
2008-07-17 21:12:52 +00:00
2008-07-18 15:05:51 +00:00
gchar *config_path = NULL;
2009-07-31 18:36:37 +00:00
const gchar *def_module_dir = DEFAULT_LIBDIR;
const gchar *module_dir = def_module_dir;
gboolean module_resident = FALSE;
2008-07-18 15:05:51 +00:00
gboolean luaconfig = FALSE;
gboolean test_config = FALSE;
gboolean show_version = FALSE;
gboolean use_angel = FALSE;
2008-07-18 15:05:51 +00:00
GOptionEntry entries[] = {
{ "config", 'c', 0, G_OPTION_ARG_FILENAME, &config_path, "filename/path of the config", "PATH" },
{ "lua", 'l', 0, G_OPTION_ARG_NONE, &luaconfig, "use the lua config frontend", NULL },
{ "test", 't', 0, G_OPTION_ARG_NONE, &test_config, "test config and exit", NULL },
2009-07-31 18:36:37 +00:00
{ "module-dir", 'm', 0, G_OPTION_ARG_STRING, &module_dir, "module directory [default: " DEFAULT_LIBDIR "]", "PATH" },
{ "module-resident", 0, 0, G_OPTION_ARG_NONE, &module_resident, "never unload modules (e.g. for valgrind)", NULL },
2008-07-18 15:05:51 +00:00
{ "version", 'v', 0, G_OPTION_ARG_NONE, &show_version, "show version and exit", NULL },
{ "angel", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &use_angel, "spawned by angel", NULL },
2008-07-18 15:05:51 +00:00
{ NULL, 0, 0, 0, NULL, NULL, NULL }
};
2008-07-17 21:12:52 +00:00
2010-05-13 10:17:33 +00:00
#ifdef WITH_PROFILER
{
/* check for environment variable LIGHTY_PROFILE_MEM */
gchar *profile_mem = getenv("LIGHTY_PROFILE_MEM");
if (profile_mem) {
/*g_mem_set_vtable(glib_mem_profiler_table);*/
li_profiler_enable(profile_mem);
atexit(li_profiler_finish);
atexit(li_profiler_dump);
/*atexit(profiler_dump_table);*/
}
}
2010-05-13 10:17:33 +00:00
#endif
2008-07-17 21:12:52 +00:00
/* parse commandline options */
context = g_option_context_new("- fast and lightweight webserver");
g_option_context_add_main_entries(context, entries, NULL);
2008-08-02 20:56:07 +00:00
res = g_option_context_parse(context, &argc, &argv, &error);
g_option_context_free(context);
if (!res) {
2008-07-17 21:12:52 +00:00
g_printerr("failed to parse command line arguments: %s\n", error->message);
g_error_free(error);
2008-07-17 21:12:52 +00:00
return 1;
}
/* -v, show version and exit */
if (show_version) {
2009-09-15 22:19:06 +00:00
g_print(PACKAGE_DESC " - a fast and lightweight webserver\n");
g_print("Build date: %s\n", PACKAGE_BUILD_DATE);
2009-07-31 18:36:37 +00:00
#ifdef LIGHTTPD_REVISION
g_print("Revision: %s\n", LIGHTTPD_REVISION);
2009-07-31 18:36:37 +00:00
#endif
return 0;
}
2008-07-18 22:11:08 +00:00
/* initialize threading */
g_thread_init(NULL);
srv = li_server_new(module_dir, module_resident);
2009-07-09 20:17:24 +00:00
li_server_loop_init(srv);
2008-07-17 21:12:52 +00:00
/* load core plugin */
srv->core_plugin = li_plugin_register(srv, "core", li_plugin_core_init, NULL);
if (use_angel) {
2009-07-09 20:17:24 +00:00
li_angel_setup(srv);
}
2008-07-17 21:12:52 +00:00
/* if no path is specified for the config, read lighttpd.conf from current directory */
if (config_path == NULL) {
2008-07-17 21:12:52 +00:00
config_path = "lighttpd.conf";
free_config_path = FALSE;
}
2008-07-17 21:12:52 +00:00
DEBUG(srv, "config path: %s", config_path);
2008-07-17 21:12:52 +00:00
if (!luaconfig) {
#ifndef WITHOUT_CONFIG_PARSER
if (!li_config_parse(srv, config_path))
return 1;
#else
g_print("standard config frontend not available\n");
return 1;
#endif
}
else {
2008-07-25 21:12:40 +00:00
#ifdef HAVE_LUA_H
li_config_lua_load(srv->L, srv, srv->main_worker, config_path, &srv->mainaction, TRUE, NULL);
/* lua config frontend */
2008-07-25 21:12:40 +00:00
#else
g_print("lua config frontend not available\n");
return 1;
#endif
2008-07-17 21:12:52 +00:00
}
if (!srv->mainaction) {
ERROR(srv, "%s", "No action handlers defined");
return 1;
}
2008-07-18 14:52:19 +00:00
/* if config should only be tested, exit here */
2008-07-17 21:12:52 +00:00
if (test_config)
return 0;
/* TRACE(srv, "%s", "Test!"); */
li_server_reached_state(srv, LI_SERVER_LOADING);
li_worker_run(srv->main_worker);
li_server_reached_state(srv, LI_SERVER_DOWN);
2008-08-06 18:46:42 +00:00
INFO(srv, "%s", "going down");
2009-07-09 20:17:24 +00:00
li_server_free(srv);
if (module_dir != def_module_dir)
g_free((gpointer)module_dir);
if (free_config_path)
g_free(config_path);
2008-08-02 20:56:07 +00:00
mempool_cleanup();
return 0;
}