openbuslua.c 6.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
** $Id: lua.c,v 1.206 2012/09/29 20:07:06 roberto Exp $
** Lua stand-alone interpreter
** See Copyright Notice in lua.h
*/


#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
12
#include <time.h>
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM == 501
#include <compat-5.2.h>
#include <luacompat52.h>
#endif

#include <luuid.h>
#include <lce.h>
#include <luasec.h>
#include <lfs.h>
#include <luavararg.h>
#include <luastruct.h>
#include <luasocket.h>
#include <loop.h>
#include <luatuple.h>
#include <luacothread.h>
#include <luaidl.h>
#include <oil.h>
#include <luascs.h>
#include <luaopenbus.h>




static lua_State *globalL = NULL;

static char *logpath = NULL;

static const char *callerchunk =
#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM == 501
" require 'compat52'"
#endif
" local _G = require '_G'"
" local error = _G.error"
" local tostring = _G.tostring"

" local coroutine = require 'coroutine'"
" local newthread = coroutine.create"

" local debug = require 'debug'"
" local traceback = debug.traceback"

" local cothread = require 'cothread'"
" local step = cothread.step"
" local run = cothread.run"

" local log = require 'openbus.util.logger'"

" function cothread.error(thread, errmsg, ...)"
"   errmsg = traceback(thread, tostring(errmsg))"
"   log:unexpected(errmsg)"
"   error(errmsg)"
" end"

" return function(f, ...)"
"   return run(step(newthread(f), ...)) or OPENBUS_EXITCODE"
" end";



static int traceback (lua_State *L) {
  const char *msg = lua_tostring(L, 1);
  if (msg)
    luaL_traceback(L, L, msg, 1);
  else if (!lua_isnoneornil(L, 1)) {  /* is there an error object? */
    if (!luaL_callmeta(L, 1, "__tostring"))  /* try its 'tostring' metamethod */
      lua_pushliteral(L, "(no error message)");
  }
  return 1;
}


LUALIB_API int openbuslua_setlogpath (const char *path) {
  if (logpath) free(logpath);
  if (path) {
    size_t len = strlen(path);
    logpath = (char *)malloc((len)*sizeof(char));
    if (logpath) strncpy(logpath, path, len);
    return logpath ? LUA_OK : LUA_ERRMEM;
  }
  return LUA_OK;
}


LUALIB_API void openbuslua_logmessage (const char *pname, const char *msg) {
  FILE *out = NULL;
  if (logpath) out = fopen(logpath, "a");
  if (out == NULL) out = stderr;
  if (pname) fprintf(out, "%s: ", pname);
  fprintf(out, "%s\n", msg);
  fflush(out);
  if (out != stderr) fclose(out);
}


LUALIB_API int openbuslua_report (lua_State *L, int status) {
  if (status != LUA_OK && !lua_isnil(L, -1)) {
    const char *msg = lua_tostring(L, -1);
    if (msg == NULL) msg = "(error object is not a string)";
    openbuslua_logmessage(NULL, msg);
    lua_pop(L, 1);
    /* force a complete garbage collection in case of errors */
    lua_gc(L, LUA_GCCOLLECT, 0);
  }
  return status;
}


125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
/*
** By default, Lua uses gmtime/localtime, except when POSIX is available,
** where it uses gmtime_r/localtime_r
*/
#if defined(LUA_USE_GMTIME_R)

#define l_localtime(t,r)  localtime_r(t,r)

#elif !defined(l_gmtime)

#define l_localtime(t,r)    ((void)r, localtime(t))

#endif


static void laction (int i) {
  struct tm tmr, *stm;
  time_t t;
  lua_close(globalL);
  globalL = NULL;
  t = time(NULL);
  stm = l_localtime(&t, &tmr);
  if (stm == NULL) {
    openbuslua_logmessage(NULL,
      "--/--/---- --:--:-- [uptime]    process terminated forcefully");
  } else {
    char buff[63];
    strftime(buff, sizeof(buff),
      "%d/%m/%Y %H:%M:%S [uptime]    process terminated forcefully", stm);
    openbuslua_logmessage(NULL, buff);
  }
  exit(1);
}


160 161 162 163 164 165 166
LUALIB_API int openbuslua_call (lua_State *L, int narg, int nres) {
  int status;
  int base = lua_gettop(L) - narg;  /* function index */
  lua_pushcfunction(L, traceback);  /* push traceback function */
  lua_insert(L, base);  /* put it under chunk and args */
  globalL = L;  /* to be available to 'laction' */
  signal(SIGINT, laction);
167
  signal(SIGTERM, laction);
168 169
  status = lua_pcall(L, narg, nres, base);
  signal(SIGINT, SIG_DFL);
170
  signal(SIGTERM, SIG_DFL);
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
  lua_remove(L, base);  /* remove traceback function */
  return status;
}


LUALIB_API int openbuslua_dostring (lua_State *L, const char *s, const char *name) {
  int status = luaL_loadbuffer(L, s, strlen(s), name);
  if (status == LUA_OK) status = openbuslua_call(L, 0, LUA_MULTRET);
  return status;
}


LUALIB_API int openbuslua_pushargs (lua_State *L, char **argv) {
  int narg;
  int i;
  int argc = 0;
  while (argv[argc]) argc++;  /* count total number of arguments */
  narg = argc-1;  /* number of arguments to the script */
  luaL_checkstack(L, narg, "too many arguments to script");
  for (i=1; i < argc; i++) lua_pushstring(L, argv[i]);
  return narg;
}


LUALIB_API int openbuslua_init (lua_State *L, int interactive, int debugmode) {
  int status = LUA_OK;
  /* open standard libraries */
  luaL_checkversion(L);
  lua_gc(L, LUA_GCSTOP, 0);  /* stop collector during initialization */
  luaL_openlibs(L);  /* open libraries */
  lua_gc(L, LUA_GCRESTART, 0);

  /* export to Lua defined C constants */
  if (debugmode) {
    lua_pushboolean(L, 1);
    lua_setfield(L, LUA_REGISTRYINDEX, "OPENBUS_DEBUG");
    status = openbuslua_dostring(L,
      " if OPENBUS_CODEREV ~= nil then"
      "   OPENBUS_CODEREV = OPENBUS_CODEREV..'-DEBUG'"
      " end"
      " table.insert(package.searchers, (table.remove(package.searchers, 1)))",
      "SET_DEBUG");
  }
  
  /* open extended libraries */
  if (status == LUA_OK) {
    /* preload libraries and global variables */
#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM == 501
    luapreload_luacompat52(L);
#endif
    /* preload binded C libraries */
#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM > 501
    luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
#else
    luaL_findtable(L, LUA_GLOBALSINDEX, "package.preload", 1);
#endif
    lua_pushcfunction(L,luaopen_uuid);lua_setfield(L,-2,"uuid");
    lua_pushcfunction(L,luaopen_lfs);lua_setfield(L,-2,"lfs");
    lua_pushcfunction(L,luaopen_vararg);lua_setfield(L,-2,"vararg");
    lua_pushcfunction(L,luaopen_struct);lua_setfield(L,-2,"struct");
    lua_pushcfunction(L,luaopen_socket_core);lua_setfield(L,-2,"socket.core");
    lua_pop(L, 1);  /* pop 'package.preload' table */
    /* preload other C libraries */
    luapreload_lce(L);
    luapreload_luasec(L);
    /* preload script libraries */
    luapreload_loop(L);
    luapreload_luatuple(L);
    luapreload_luacothread(L);
    luapreload_luaidl(L);
    luapreload_oil(L);
    luapreload_luascs(L);
    luapreload_luaopenbus(L);
    /* push main module executor */
    status = openbuslua_dostring(L, callerchunk, "MAIN_THREAD");
    if ((status == LUA_OK) && interactive) {
      lua_pushvalue(L, -1);
      lua_getglobal(L, "require");
      lua_pushstring(L, "openbus.console.costdin");
      status = lua_pcall(L, 1, 1, 0);
      if (status == LUA_OK) {
        status = openbuslua_call(L, 1, 0);
      }
    }
  }
  return status;
}