diff options
author | Steven Barth <steven@midlink.org> | 2008-11-30 13:19:45 +0000 |
---|---|---|
committer | Steven Barth <steven@midlink.org> | 2008-11-30 13:19:45 +0000 |
commit | b33943a6e8596c1ddfc1b771a995d3cf21e81cd6 (patch) | |
tree | 6f67cdea044e708a599a06712491b5c60db6f954 /libs/lucittpd/src/lib | |
parent | a7e7c31f8c659b55c1adb0863a8f2f66d3452d2b (diff) |
Merge LuCIttpd
Diffstat (limited to 'libs/lucittpd/src/lib')
-rw-r--r-- | libs/lucittpd/src/lib/log.c | 45 | ||||
-rw-r--r-- | libs/lucittpd/src/lib/luaplugin.c | 383 | ||||
-rw-r--r-- | libs/lucittpd/src/lib/signal.c | 52 | ||||
-rw-r--r-- | libs/lucittpd/src/lib/uci.c | 206 |
4 files changed, 686 insertions, 0 deletions
diff --git a/libs/lucittpd/src/lib/log.c b/libs/lucittpd/src/lib/log.c new file mode 100644 index 000000000..b6ce8c28e --- /dev/null +++ b/libs/lucittpd/src/lib/log.c @@ -0,0 +1,45 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. + * + * Provided by fon.com + * Copyright (C) 2008 John Crispin <blogic@openwrt.org> + */ + +#include <stdio.h> +#include <syslog.h> +#include <stdarg.h> + +static int daemonize = 0; + +void log_start(int daemon) +{ + daemonize = daemon; + openlog("lucittpd", 0, 0); +} + +void log_printf(char *fmt, ...) +{ + char p[256]; + va_list ap; + + va_start(ap, fmt); + vsnprintf(p, 256, fmt, ap); + va_end(ap); + + if(daemonize) + syslog(10, p); + else + printf(p); +} diff --git a/libs/lucittpd/src/lib/luaplugin.c b/libs/lucittpd/src/lib/luaplugin.c new file mode 100644 index 000000000..6a0e1caad --- /dev/null +++ b/libs/lucittpd/src/lib/luaplugin.c @@ -0,0 +1,383 @@ +/* + * luaplugin - fast lua plugin indexing + * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include <sys/types.h> +#include <sys/time.h> +#include <sys/cdefs.h> + +#ifndef _POSIX_C_SOURCE +#define _POSIX_C_SOURCE /* XXX: portability hack for timestamp */ +#endif + +#include <sys/stat.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <stdio.h> +#include <errno.h> +#include <limits.h> +#include <glob.h> + +#include <lualib.h> +#include <lauxlib.h> +#include <lib/list.h> +#include <lib/luaplugin.h> + +//#define DEBUG 1 +#ifdef DEBUG +#define DPRINTF(...) fprintf(stderr, __VA_ARGS__) +#else +#define DPRINTF(...) do {} while (0) +#endif + +/** + * list_for_each_offset - iterate over a list, start with the provided pointer + * @pos: the &struct list_head to use as a loop cursor. + * @head: the head for your list. + */ +#define list_for_each_offset(pos, head, offset) \ + for (pos = (offset)->next; pos != (offset); \ + pos = ((pos->next == (head)) && ((offset) != (head)) ? (head)->next : pos->next)) + +static char pbuf[PATH_MAX]; +static void load_module(struct luaplugin_ctx *ctx, struct luaplugin_entry *e); + +static struct luaplugin_entry * +find_entry(struct luaplugin_ctx *ctx, const char *name, bool modname) +{ + struct list_head *p; + + if (!ctx->last) + ctx->last = &ctx->entries; + + list_for_each_offset(p, &ctx->entries, ctx->last) { + struct luaplugin_entry *e; + const char *cmp; + + e = container_of(p, struct luaplugin_entry, list); + if (modname) + cmp = e->module; + else + cmp = e->name; + + if (!strcmp(cmp, name)) + return e; + } + return NULL; +} + +static struct luaplugin_entry * +new_entry(struct luaplugin_ctx *ctx, const char *name, const char *modname) +{ + struct luaplugin_entry *e; + char *c; + + e = malloc(sizeof(struct luaplugin_entry)); + if (!e) + goto error; + + memset(e, 0, sizeof(struct luaplugin_entry)); + INIT_LIST_HEAD(&e->list); + e->ctx = ctx; + e->loaded = false; + + e->name = strdup(name); + if (!e->name) + goto error1; + + e->module = strdup(modname); + if (!e->module) + goto error2; + + /* strip filename extension */ + c = strrchr(e->module, '.'); + if (c) + *c = 0; + + /* lua namespace: replace / with . */ + c = e->module; + while ((c = strchr(c, '/')) != NULL) { + *c = '.'; + } + return e; + +error2: + free(e->name); +error1: + free(e); +error: + return NULL; +} + +static const char *module_loader = +"loader = function (newgt, filename)\n" +" setmetatable(newgt, { __index = _G })\n" +" local f = loadfile(filename)\n" +" if (type(f) == \"function\") then\n" +" setfenv(f, newgt)\n" +" f()\n" +" else\n" +" error(f)\n" +" end\n" +"end\n"; + +static void +access_plugin_table (lua_State *L, const char *modname, bool set) +{ + const char *e; + + lua_pushvalue(L, LUA_GLOBALSINDEX); + do { + bool _set = true; + + e = strchr(modname, '.'); + if (e == NULL) { + e = modname + strlen(modname); + _set = set; + } + + lua_pushlstring(L, modname, e - modname); + lua_rawget(L, -2); + if (lua_isnil(L, -1) || + /* no such field or last field */ + (lua_istable(L, -1) && (*e != '.'))) { + lua_pop(L, 1); /* remove this result */ + + if (_set) { + if (*e != '.') + lua_pushvalue(L, -2); /* use table from given index */ + else + lua_createtable(L, 0, 1); /* new table for field */ + } + + lua_pushlstring(L, modname, e - modname); + + if (_set) { + lua_pushvalue(L, -2); + lua_settable(L, -4); /* set new table into field */ + } else { + lua_gettable(L, -2); + } + } + else if (!lua_istable(L, -1)) { /* field has a non-table value? */ + lua_pop(L, 2 + !!set); /* remove table and values */ + return; + } + lua_remove(L, -2); /* remove previous table */ + modname = e + 1; + } while (*e == '.'); + if (set) + lua_pop(L, 2); +} + + +static void +load_module(struct luaplugin_ctx *ctx, struct luaplugin_entry *e) +{ + lua_State *L = ctx->L; + int ret; + + /* grab the loader wrapper function */ + ret = luaL_dostring(L, module_loader); + if (ret) + return; + + lua_getglobal(L, "loader"); + lua_pushnil(L); + lua_setglobal(L, "loader"); + + e->loaded = true; + e->reload = false; + + /* new environment table for function call */ + lua_newtable(L); + + /* register the table globally */ + lua_pushvalue(L, -1); + access_plugin_table(L, e->module, true); + + lua_pushstring(L, e->name); + + if (lua_pcall(L, 2, 0, 0) != 0) { + const char *err = "unknown error"; + + if (lua_isstring(L, -1)) + err = lua_tostring(L, -1); + + fprintf(stderr, err); + } +} + +static void +free_entry(struct luaplugin_ctx *ctx, struct luaplugin_entry *e) +{ + lua_State *L = ctx->L; + + if (e->loaded && L) { + /* allow the gc to free the module */ + lua_pushnil(L); + access_plugin_table(L, e->module, true); + } + list_del(&e->list); + free(e->name); + free(e->module); + free(e); +} + +static void +__luaplugin_scan(struct luaplugin_ctx *ctx, int base_len, int rec) +{ + int gl_flags = GLOB_NOESCAPE | GLOB_NOSORT | GLOB_MARK; + glob_t gl; + int i; + + strncpy(pbuf + base_len, "*.lua", PATH_MAX - base_len); + if (glob(pbuf, gl_flags, NULL, &gl) < 0) { + globfree(&gl); + return; + } + + for (i = 0; i < gl.gl_pathc; i++) { + const char *entry = gl.gl_pathv[i]; + struct luaplugin_entry *e; + struct stat st; + int elen; + + elen = strlen(entry); + + /* should not happen */ + if ((elen <= base_len) || (strncmp(entry, pbuf, base_len) != 0)) { + fprintf(stderr, "[%s] sanity check failed in %s(%d)!\n", __FILE__, __func__, __LINE__); + continue; + } + + /* descend into subdirectories */ + if (entry[elen - 1] == '/') { + strncpy(pbuf + base_len, entry + base_len, PATH_MAX - base_len); + __luaplugin_scan(ctx, base_len, rec + 1); + pbuf[base_len] = '\0'; + continue; + } + + if (stat(gl.gl_pathv[i], &st)) + continue; + + if ((st.st_mode & S_IFMT) != S_IFREG) + continue; + + e = find_entry(ctx, entry + base_len, false); + if (!e) { + e = new_entry(ctx, entry, entry + base_len); + list_add_tail(&e->list, &ctx->entries); + } + if (!e) + continue; + + e->checked = ctx->checked; + e->reload = (e->timestamp < st.st_mtime); + e->timestamp = st.st_mtime; + } + globfree(&gl); +} + +int +luaplugin_call(struct luaplugin_entry *e, int narg) +{ + struct luaplugin_ctx *ctx = e->ctx; + lua_State *L = ctx->L; + const char *func; + int ret; + + func = luaL_checkstring(L, -1 - narg); + + /* grab a reference to the plugin's table */ + access_plugin_table(L, e->module, false); + lua_getfield(L, -1, func); + if (!lua_isfunction(L, -1)) { + lua_pop(L, narg + 1); + ret = -ENOENT; + goto done; + } + + /* replace function name with a ref to the function */ + lua_replace(L, -3 - narg); + + /* pop the table */ + lua_pop(L, 1); + ret = lua_pcall(L, narg, 0, 0); + + if (ret != 0) { + fprintf(stderr, lua_tostring(L, -1)); + } + +done: + return ret; +} + +void +luaplugin_scan(struct luaplugin_ctx *ctx) +{ + struct list_head *tmp, *p; + + sprintf(pbuf, "%s/", ctx->path); + + ctx->checked++; + __luaplugin_scan(ctx, strlen(pbuf), 0); + + /* expire old entries */ + list_for_each_safe(p, tmp, &ctx->entries) { + struct luaplugin_entry *e = container_of(p, struct luaplugin_entry, list); + if (e->checked < ctx->checked) + free_entry(ctx, e); + else if (e->reload) + load_module(ctx, e); + } +} + +int +luaplugin_init(struct luaplugin_ctx *ctx, const char *path) +{ + memset(ctx, 0, sizeof(struct luaplugin_ctx)); + INIT_LIST_HEAD(&ctx->entries); + ctx->path = path; + + ctx->L = luaL_newstate(); + if (!ctx->L) + return -ENOMEM; + + luaL_openlibs(ctx->L); + + /* disable the module functionality, a plugin is restricted to its own environment */ + /* + lua_pushcfunction(ctx->L, luaplugin_module); + lua_setfield(ctx->L, LUA_GLOBALSINDEX, "module"); + */ + + return 0; +} + +void +luaplugin_done(struct luaplugin_ctx *ctx) +{ + struct list_head *p, *tmp; + + lua_close(ctx->L); + ctx->L = NULL; + + list_for_each_safe(p, tmp, &ctx->entries) { + struct luaplugin_entry *e; + e = container_of(p, struct luaplugin_entry, list); + free_entry(ctx, e); + } +} diff --git a/libs/lucittpd/src/lib/signal.c b/libs/lucittpd/src/lib/signal.c new file mode 100644 index 000000000..2b11f47cb --- /dev/null +++ b/libs/lucittpd/src/lib/signal.c @@ -0,0 +1,52 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. + * + * Provided by fon.com + * Copyright (C) 2008 John Crispin <blogic@openwrt.org> + */ + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <signal.h> +#include <sys/types.h> +#include <sys/wait.h> + +#include <lib/log.h> + +void handler_INT(int signo) +{ + log_printf("away we go\n"); + exit(0); +} + +void handler_CHLD(int signo) +{ + while(waitpid(-1, NULL, WNOHANG) > 0); +} + +void setup_signals(void) +{ + struct sigaction s1, s2, s3; + s1.sa_handler = handler_INT; + s1.sa_flags = 0; + sigaction(SIGINT, &s1, NULL); + s2.sa_handler = handler_INT; + s2.sa_flags = 0; + sigaction(SIGTERM, &s2, NULL); + s3.sa_handler = handler_CHLD; + s3.sa_flags = SA_RESTART; + sigaction(SIGCHLD, &s3, NULL); +} diff --git a/libs/lucittpd/src/lib/uci.c b/libs/lucittpd/src/lib/uci.c new file mode 100644 index 000000000..33254ee5e --- /dev/null +++ b/libs/lucittpd/src/lib/uci.c @@ -0,0 +1,206 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. + * + * Copyright (C) 2008 John Crispin <blogic@openwrt.org> + */ + +#include <string.h> +#include <stdlib.h> + +#include <uci.h> +#include <lib/list.h> +#include <lib/log.h> +#include <lib/uci.h> + +static struct uci_ptr ptr; +static struct uci_package *p = NULL; + +static inline int ucix_get_ptr(struct uci_context *ctx, const char *p, const char *s, const char *o, const char *t) +{ + memset(&ptr, 0, sizeof(ptr)); + ptr.package = p; + ptr.section = s; + ptr.option = o; + ptr.value = t; + return uci_lookup_ptr(ctx, &ptr, NULL, true); +} + +struct uci_context* ucix_init(const char *config_file) +{ + struct uci_context *ctx = uci_alloc_context(); + uci_add_history_path(ctx, "/var/state"); + uci_set_savedir(ctx, "/var/state/"); + if(uci_load(ctx, config_file, &p) != UCI_OK) + { + log_printf("%s/%s is missing or corrupt\n", ctx->savedir, config_file); + return NULL; + } + return ctx; +} + +struct uci_context* ucix_init_path(const char *path, const char *config_file) +{ + struct uci_context *ctx = uci_alloc_context(); + if(path) + uci_set_confdir(ctx, path); + if(uci_load(ctx, config_file, NULL) != UCI_OK) + { + log_printf("%s/%s is missing or corrupt\n", ctx->savedir, config_file); + return NULL; + } + return ctx; +} + +void ucix_cleanup(struct uci_context *ctx) +{ + uci_free_context(ctx); +} + +void ucix_save(struct uci_context *ctx) +{ + uci_set_savedir(ctx, "/tmp/.uci"); + uci_save(ctx, p); +} + +void ucix_save_state(struct uci_context *ctx) +{ + uci_save(ctx, p); +} + +int ucix_get_option_list(struct uci_context *ctx, const char *p, + const char *s, const char *o, struct list_head *l) +{ + struct uci_element *e = NULL; + if(ucix_get_ptr(ctx, p, s, o, NULL)) + return 1; + if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) + return 1; + e = ptr.last; + switch (e->type) + { + case UCI_TYPE_OPTION: + switch(ptr.o->type) { + case UCI_TYPE_LIST: + uci_foreach_element(&ptr.o->v.list, e) + { + struct ucilist *ul = malloc(sizeof(struct ucilist)); + ul->val = strdup((e->name)?(e->name):("")); + INIT_LIST_HEAD(&ul->list); + list_add(&ul->list, l); + } + break; + default: + break; + } + break; + default: + return 1; + } + + return 0; +} + +const char* ucix_get_option(struct uci_context *ctx, const char *p, const char *s, const char *o) +{ + struct uci_element *e = NULL; + const char *value = NULL; + if(ucix_get_ptr(ctx, p, s, o, NULL)) + return NULL; + if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) + return NULL; + e = ptr.last; + switch (e->type) + { + case UCI_TYPE_SECTION: + value = uci_to_section(e)->type; + break; + case UCI_TYPE_OPTION: + switch(ptr.o->type) { + case UCI_TYPE_STRING: + value = ptr.o->v.string; + break; + default: + value = NULL; + break; + } + break; + default: + return 0; + } + + return value; +} + +int ucix_get_option_int(struct uci_context *ctx, const char *p, const char *s, const char *o, int def) +{ + const char *tmp = ucix_get_option(ctx, p, s, o); + int ret = def; + + if (tmp) + ret = atoi(tmp); + return ret; +} + +void ucix_add_section(struct uci_context *ctx, const char *p, const char *s, const char *t) +{ + if(ucix_get_ptr(ctx, p, s, NULL, t)) + return; + uci_set(ctx, &ptr); +} + +void ucix_add_option(struct uci_context *ctx, const char *p, const char *s, const char *o, const char *t) +{ + if(ucix_get_ptr(ctx, p, s, o, (t)?(t):(""))) + return; + uci_set(ctx, &ptr); +} + +void ucix_add_option_int(struct uci_context *ctx, const char *p, const char *s, const char *o, int t) +{ + char tmp[64]; + snprintf(tmp, 64, "%d", t); + ucix_add_option(ctx, p, s, o, tmp); +} + +void ucix_del(struct uci_context *ctx, const char *p, const char *s, const char *o) +{ + if(!ucix_get_ptr(ctx, p, s, o, NULL)) + uci_delete(ctx, &ptr); +} + +void ucix_revert(struct uci_context *ctx, const char *p, const char *s, const char *o) +{ + if(!ucix_get_ptr(ctx, p, s, o, NULL)) + uci_revert(ctx, &ptr); +} + +void ucix_for_each_section_type(struct uci_context *ctx, + const char *p, const char *t, + void (*cb)(const char*, void*), void *priv) +{ + struct uci_element *e; + if(ucix_get_ptr(ctx, p, NULL, NULL, NULL)) + return; + uci_foreach_element(&ptr.p->sections, e) + if (!strcmp(t, uci_to_section(e)->type)) + cb(e->name, priv); +} + +int ucix_commit(struct uci_context *ctx, const char *p) +{ + if(ucix_get_ptr(ctx, p, NULL, NULL, NULL)) + return 1; + return uci_commit(ctx, &ptr.p, false); +} |