a sandboxed Lua backend for FastCGI
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

126 lines
2.4 KiB

  1. #include <stdlib.h>
  2. #include <unistd.h>
  3. #include <string.h>
  4. #include <lua5.1/lua.h>
  5. #include <lua5.1/lauxlib.h>
  6. #include <lua5.1/lualib.h>
  7. #include "config.h"
  8. // Checks if a file exists
  9. static int LF_fileexists(char *path)
  10. {
  11. if(access(path, F_OK) == 0){ return 1; }
  12. else { return 0; }
  13. }
  14. // Create configuration with default settings
  15. LF_config *LF_createconfig()
  16. {
  17. LF_config *c = malloc(sizeof(LF_config));
  18. if(c == NULL){ return NULL; }
  19. // Default settings
  20. c->listen = "127.0.0.1:9222";
  21. c->backlog = 100;
  22. c->threads = 1;
  23. c->sandbox = 1;
  24. c->mem_max = 65536;
  25. c->output_max = 65536;
  26. c->cpu_usec = 500000;
  27. c->cpu_sec = 0;
  28. return c;
  29. }
  30. // Load configuration
  31. int LF_loadconfig(LF_config *cfg, char *path)
  32. {
  33. if(!LF_fileexists(path)){ return 1; }
  34. lua_State *l = luaL_newstate();
  35. if(luaL_loadfile(l, path) || lua_pcall(l,0,1,0)){
  36. printf("%s\n", lua_tostring(l, -1));
  37. lua_close(l);
  38. return 1;
  39. }
  40. lua_settop(l, 1);
  41. if(lua_istable(l, 1)){
  42. lua_pushstring(l, "listen");
  43. lua_rawget(l, 1);
  44. if(lua_isstring(l, 2)){
  45. size_t len = 0;
  46. const char *str = lua_tolstring(l, 2, &len);
  47. if(len > 0){
  48. cfg->listen = malloc(len+1);
  49. memcpy(cfg->listen, str, len+1);
  50. }
  51. }
  52. lua_settop(l, 1);
  53. lua_pushstring(l, "backlog");
  54. lua_rawget(l, 1);
  55. if(lua_isnumber(l, 2)){ cfg->backlog = lua_tonumber(l, 2); }
  56. lua_settop(l, 1);
  57. lua_pushstring(l, "threads");
  58. lua_rawget(l, 1);
  59. if(lua_isnumber(l, 2)){ cfg->threads = lua_tonumber(l, 2); }
  60. lua_settop(l, 1);
  61. lua_pushstring(l, "sandbox");
  62. lua_rawget(l, 1);
  63. if(lua_isboolean(l, 2)){ cfg->sandbox = lua_toboolean(l, 2); }
  64. lua_settop(l, 1);
  65. lua_pushstring(l, "mem_max");
  66. lua_rawget(l, 1);
  67. if(lua_isnumber(l, 2)){ cfg->mem_max = lua_tonumber(l, 2); }
  68. lua_settop(l, 1);
  69. lua_pushstring(l, "cpu_usec");
  70. lua_rawget(l, 1);
  71. if(lua_isnumber(l, 2)){ cfg->cpu_usec = lua_tonumber(l, 2); }
  72. lua_settop(l, 1);
  73. lua_pushstring(l, "cpu_sec");
  74. lua_rawget(l, 1);
  75. if(lua_isnumber(l, 2)){ cfg->cpu_sec = lua_tonumber(l, 2); }
  76. lua_settop(l, 1);
  77. lua_pushstring(l, "output_max");
  78. lua_rawget(l, 1);
  79. if(lua_isnumber(l, 2)){ cfg->output_max = lua_tonumber(l, 2); }
  80. lua_settop(l, 1);
  81. lua_pushstring(l, "content_type");
  82. lua_rawget(l, 1);
  83. if(lua_isstring(l, 2)){
  84. size_t len = 0;
  85. const char *str = lua_tolstring(l, 2, &len);
  86. if(len > 0){
  87. cfg->content_type = malloc(len+1);
  88. memcpy(cfg->content_type, str, len+1);
  89. }
  90. }
  91. }
  92. lua_close(l);
  93. return 0;
  94. }