Commit a6fae4f7 authored by Renato Figueiro Maia's avatar Renato Figueiro Maia
Browse files

[OPENBUS-2571] Todos erros e falhas são registrados no arquivo de log

git-svn-id: https://subversion.tecgraf.puc-rio.br/engdist/openbus/core/branches/02_00_00@156114 ae0415b3-e90b-0410-900d-d0be9363c56b
parent 7b12d9f9
-- $Id$
local _G = require "_G"
local assert = _G.assert
local ipairs = _G.ipairs
local require = _G.require
local select = _G.select
......@@ -14,6 +13,7 @@ local math = require "math"
local inf = math.huge
local io = require "io"
local openfile = io.open
local stderr = io.stderr
local os = require "os"
......@@ -48,7 +48,22 @@ local OfferRegistry = require "openbus.core.services.OfferRegistry"
return function(...)
log.viewer.labels[running()] = "busservices"
local errcode = {
InvalidLeaseTime = 2,
InvalidExpirationGap = 3,
InvalidPasswordPenaltyTime = 4,
InvalidNumberOfPasswordLimitedTries = 5,
MissingPasswordValidationParameter = 6,
InvalidPasswordValidationLimit = 7,
InvalidPasswordValidationRate = 8,
UnableToLoadPasswordValidator = 9,
UnableToInitializePasswordValidator = 10,
UnableToReadPrivateKey = 11,
UnableToOpenDatabase = 12,
NoPasswordValidators = 13,
}
-- configuration parameters parser
local Configs = ConfigArgs{
host = "*",
......@@ -78,12 +93,23 @@ return function(...)
logaddress = false,
}
-- parse configuration file
Configs:configs("configs", getenv("OPENBUS_CONFIG") or "openbus.cfg")
log:level(Configs.loglevel)
log:version(msg.CopyrightNotice)
do -- parse configuration file
local path = getenv("OPENBUS_CONFIG")
if path == nil then
path = "openbus.cfg"
local file = openfile(path)
if file == nil then goto done end
file:close()
end
Configs:configs("configs", path)
::done::
end
-- parse command line parameters
do
io.write(msg.CopyrightNotice, "\n")
local argidx, errmsg = Configs(...)
if not argidx or argidx <= select("#", ...) then
if errmsg ~= nil then
......@@ -153,50 +179,105 @@ Options:
end
-- setup log files
setuplog(log, Configs.loglevel, Configs.logfile)
log:version(msg.CopyrightNotice)
local logfile = setuplog(log, Configs.loglevel, Configs.logfile)
if logfile ~= nil then OPENBUS_SETLOGPATH(logfile) end
log:config(msg.CoreServicesLogLevel:tag{value=Configs.loglevel})
setuplog(oillog, Configs.oilloglevel, Configs.oillogfile)
log:config(msg.OilLogLevel:tag{value=Configs.oilloglevel})
-- validate time parameters
assert(Configs.leasetime > 0 and Configs.leasetime%1 == 0,
msg.InvalidLeaseTime:tag{value=Configs.leasetime})
assert(Configs.expirationgap > 0,
msg.InvalidExpirationGap:tag{value=Configs.expirationgap})
assert(Configs.badpasswordpenalty >= 0,
msg.InvalidPasswordPenaltyTime:tag{value=Configs.badpasswordpenalty})
assert(Configs.badpasswordtries > 0 and Configs.badpasswordtries%1 == 0,
msg.InvalidNumberOfPasswordLimitedTries:tag{value=Configs.badpasswordtries})
assert((Configs.badpasswordlimit ~= inf) == (Configs.badpasswordrate ~= inf),
msg.MissingPasswordValidationParameter:tag{
if Configs.leasetime%1 ~= 0 or Configs.leasetime < 1 then
log:misconfig(msg.InvalidLeaseTime:tag{value = Configs.leasetime})
return errcode.InvalidLeaseTime
elseif Configs.expirationgap <= 0 then
log:misconfig(msg.InvalidExpirationGap:tag{value = Configs.expirationgap})
return errcode.InvalidExpirationGap
elseif Configs.badpasswordpenalty < 0 then
log:misconfig(msg.InvalidPasswordPenaltyTime:tag{
value = Configs.badpasswordpenalty,
})
return errcode.InvalidPasswordPenaltyTime
elseif Configs.badpasswordtries%1 ~= 0 or Configs.badpasswordtries < 1 then
log:misconfig(msg.InvalidNumberOfPasswordLimitedTries:tag{
value = Configs.badpasswordtries,
})
return errcode.InvalidNumberOfPasswordLimitedTries
elseif (Configs.badpasswordlimit~=inf) ~= (Configs.badpasswordrate~=inf) then
log:misconfig(msg.MissingPasswordValidationParameter:tag{
missing = (Configs.badpasswordlimit == inf)
and "badpasswordlimit"
or "badpasswordrate"
})
assert(Configs.badpasswordlimit >= 1,
msg.InvalidPasswordValidationLimit:tag{value=Configs.badpasswordlimit})
assert(Configs.badpasswordrate > 0,
msg.InvalidPasswordValidationRate:tag{value=Configs.badpasswordlimitrate})
return errcode.MissingPasswordValidationParameter
elseif Configs.badpasswordlimit < 1 then
log:misconfig(msg.InvalidPasswordValidationLimit:tag{
value = Configs.badpasswordlimit,
})
return errcode.InvalidPasswordValidationLimit
elseif Configs.badpasswordrate <= 0 then
log:misconfig(msg.InvalidPasswordValidationRate:tag{
value = Configs.badpasswordlimitrate,
})
return errcode.InvalidPasswordValidationRate
end
-- create a set of admin users
local adminUsers = {}
for _, admin in ipairs(Configs.admin) do
adminUsers[admin] = true
log:config(msg.AdministrativeRightsGranted:tag{entity=admin})
-- load private key
local prvkey, errmsg = readprivatekey(Configs.privatekey)
if prvkey == nil then
log:misconfig(msg.UnableToReadPrivateKey:tag{
path = Configs.privatekey,
error = errmsg,
})
return errcode.UnableToReadPrivateKey
end
-- open database
local database, errmsg = opendb(Configs.database)
if database == nil then
log:misconfig(msg.UnableToOpenDatabase:tag{
path = Configs.database,
error = errmsg,
})
return errcode.UnableToOpenDatabase
end
-- load all password validators to be used
local validators = {}
for index, package in ipairs(Configs.validator) do
local ok, result = pcall(require, package)
if not ok then
log:misconfig(msg.UnableToLoadPasswordValidator:tag{
validator = package,
error = result,
})
return errcode.UnableToLoadPasswordValidator
end
local validate, errmsg = result(Configs)
if validate == nil then
log:misconfig(msg.UnableToInitializePasswordValidator:tag{
validator = package,
error = errmsg,
})
return errcode.UnableToInitializePasswordValidator
end
validators[#validators+1] = {
name = package,
validate = assert(require(package)(Configs)),
validate = validate,
}
log:config(msg.PasswordValidatorLoaded:tag{name=package})
end
assert(#validators>0, msg.NoPasswordValidators)
if #validators == 0 then
log:misconfig(msg.NoPasswordValidators)
return errcode.NoPasswordValidators
end
-- create a set of admin users
local adminUsers = {}
for _, admin in ipairs(Configs.admin) do
adminUsers[admin] = true
log:config(msg.AdministrativeRightsGranted:tag{entity=admin})
end
-- setup bus access
local address = { host=Configs.host, port=Configs.port }
log:config(msg.ServicesListeningAddress:tag(address))
......@@ -209,7 +290,7 @@ Options:
legacy = ACS.IAccessControlService
end
iceptor = access.Interceptor{
prvkey = assert(readprivatekey(Configs.privatekey)),
prvkey = prvkey,
orb = orb,
legacy = legacy,
}
......@@ -243,7 +324,7 @@ Options:
init = function()
local params = {
access = iceptor,
database = assert(opendb(Configs.database)),
database = database,
leaseTime = Configs.leasetime,
expirationGap = Configs.expirationgap,
passwordPenaltyTime = Configs.badpasswordpenalty,
......
......@@ -25,26 +25,6 @@
#define LUA_OK 0
#endif
#if !defined(LUA_PROMPT)
#define LUA_PROMPT "> "
#define LUA_PROMPT2 ">> "
#endif
#if !defined(LUA_PROGNAME)
#define LUA_PROGNAME "lua"
#endif
#if !defined(LUA_MAXINPUT)
#define LUA_MAXINPUT 512
#endif
#if !defined(LUA_INIT)
#define LUA_INIT "LUA_INIT"
#endif
#define LUA_INITVERSION \
LUA_INIT "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR
......@@ -52,6 +32,8 @@ static lua_State *globalL = NULL;
static const char *progpath = NULL;
static char *logpath = NULL;
static const char *callerchunk =
#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM == 501
" require 'compat52'"
......@@ -93,15 +75,30 @@ static void lstop (lua_State *L, lua_Debug *ar) {
static void laction (int i) {
signal(i, SIG_DFL); /* if another SIGINT happens before lstop,
terminate process (default action) */
terminate process (default action) */
lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
}
static int l_setlogpath (lua_State *L) {
size_t len;
const char *path = luaL_checklstring(L, 1, &len);
if (logpath) luaL_error(L, "log file already defined (path=%s)", logpath);
++len;
logpath = (char *)malloc((len)*sizeof(char));
strncpy(logpath, path, len);
return 0;
}
static void l_message (const char *pname, const char *msg) {
if (pname) fprintf(stderr, "%s: ", pname);
fprintf(stderr, "%s\n", msg);
fflush(stderr);
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);
}
......@@ -171,13 +168,13 @@ static int getargs (lua_State *L, char **argv) {
static int dostring (lua_State *L, const char *s, const char *name) {
int status = luaL_loadbuffer(L, s, strlen(s), name);
if (status == LUA_OK) status = docall(L, 0, LUA_MULTRET);
return report(L, status);
return status;
}
static int pmain (lua_State *L) {
char **argv = (char **)lua_touserdata(L, 2);
int status = 0;
int status = LUA_OK;
/* open standard libraries */
luaL_checkversion(L);
lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */
......@@ -195,16 +192,16 @@ static int pmain (lua_State *L) {
}
}
/* preload libraries and global variables */
lua_pushstring(L, OPENBUS_MAIN); lua_setglobal(L, "OPENBUS_MAIN");
lua_pushstring(L, OPENBUS_PROGNAME); lua_setglobal(L, "OPENBUS_PROGNAME");
#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM == 501
luapreload_luacompat52(L);
#endif
luapreload_extralibraries(L);
/* ??? */
if (status == LUA_OK) {
/* preload libraries and global variables */
lua_pushstring(L, OPENBUS_MAIN); lua_setglobal(L, "OPENBUS_MAIN");
lua_pushstring(L, OPENBUS_PROGNAME); lua_setglobal(L, "OPENBUS_PROGNAME");
lua_pushcfunction(L, l_setlogpath); lua_setglobal(L, "OPENBUS_SETLOGPATH");
#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM == 501
luapreload_luacompat52(L);
#endif
luapreload_extralibraries(L);
/* execute main module */
status = dostring(L, callerchunk, "MAIN_THREAD");
if (status == LUA_OK) {
......@@ -226,7 +223,7 @@ static int pmain (lua_State *L) {
int main (int argc, char **argv) {
int status, result;
int status, result = EXIT_FAILURE;
lua_State *L = luaL_newstate(); /* create state */
if (L == NULL) {
l_message(argv[0], "cannot create Lua state: not enough memory");
......@@ -237,9 +234,10 @@ int main (int argc, char **argv) {
lua_pushinteger(L, argc); /* 1st argument */
lua_pushlightuserdata(L, argv); /* 2nd argument */
status = lua_pcall(L, 2, 1, 0);
result = lua_tointeger(L, -1); /* get result */
if (status == LUA_OK) result = lua_tointeger(L, -1); /* get result */
finalreport(L, status);
lua_close(L);
return (status == LUA_OK) ? result : EXIT_FAILURE;
if (logpath != NULL) free((void*)logpath);
return result;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment