static int virgo__lua_handle_crash(lua_State *L) { const char *lua_err; const char *lua_tb; char* lua_msg; size_t nlen; int rv; virgo_t* v; /* grab the error for reporting to stderr */ lua_err = lua_tostring(L, -1); /* Push the exception into virgo for the dumper */ lua_getglobal(L, "virgo"); lua_insert(L, -2); lua_setfield(L, -2, "exception"); lua_pop(L, 1); /* do dump */ v = virgo__lua_context(L); if (virgo__argv_has_flag(v, NULL, "--production") == 1){ virgo__force_dump(); } /* push a traceback onto the stack */ lua_getglobal(L, "require"); lua_pushliteral(L, "debug"); lua_call(L, 1, 1); lua_getfield(L, -1, "traceback"); lua_pushliteral(L, ""); /* skip the current function in the traceback */ lua_pushnumber(L, 2); rv = lua_pcall(L, 2, 1, 0); if (rv != 0){ lua_pushstring(L, lua_err); fprintf(stderr, "%s", "Warning: could not generate a lua traceback."); return 1; } /* grab the traceback and concat it with the error string */ lua_tb = lua_tostring(L, -1); nlen = strlen(lua_err) + strlen(lua_tb) + 1; lua_msg = malloc(nlen); lua_msg[0] = '\0'; strcat(lua_msg, lua_err); strcat(lua_msg, lua_tb); /* push the new error string back onto the stack */ lua_pushstring(L, lua_msg); free((void*)lua_msg); return 1; }
virgo_error_t* virgo_init(virgo_t *v) { virgo_error_t* err; if (virgo__argv_has_flag(v, "-h", "--help") == 1) { return virgo_error_create(VIRGO_EHELPREQ, "--help was passed");; } if (virgo__argv_has_flag(v, "-v", "--version") == 1) { return virgo_error_create(VIRGO_EVERSIONREQ, "--version was passed");; } #ifdef _WIN32 if (virgo__argv_has_flag(v, NULL, "--service-install") == 1) { return virgo__service_install(v); } if (virgo__argv_has_flag(v, NULL, "--service-delete") == 1) { return virgo__service_delete(v); } #endif err = virgo__lua_init(v); if (err ) { return err; } err = virgo__agent_conf_init(v); if (err) { return err; } return VIRGO_SUCCESS; }
virgo_error_t* virgo_run(virgo_t *v) { virgo_error_t* err; #ifndef _WIN32 if (virgo__argv_has_flag(v, "-D", "--detach") == 1) { err = virgo_detach(); if (err != VIRGO_SUCCESS) { return err; } } #endif err = virgo__log_rotate(v); if (err) { return err; } err = virgo__conf_init(v); if (err) { return err; } #ifdef _WIN32 err = virgo__service_handler(v); #else /* TOOD: restart support */ err = virgo__lua_run(v); #endif if (err) { return err; } return VIRGO_SUCCESS; }
int virgo_argv_has_help(virgo_t *v) { return virgo__argv_has_flag(v, "-h", "--help"); }