Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better handling of system constants in the file-log plugin #787

Merged
merged 1 commit into from
Dec 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .luacheckrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
redefined = false
unused_args = false
globals = {"ngx", "dao", "app", "configuration"}
globals = {"ngx", "dao", "app", "configuration", "process_id"}

files["kong/"] = {
std = "luajit"
Expand Down
1 change: 1 addition & 0 deletions kong-0.5.4-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies = {
"luasec ~> 0.5-2",

"lua_uuid ~> 0.1-8",
"lua_system_constants ~> 0.1-2",
"luatz ~> 0.3-1",
"yaml ~> 1.1.2-1",
"lapis ~> 1.3.1-1",
Expand Down
25 changes: 12 additions & 13 deletions kong/plugins/file-log/log.lua
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
-- Copyright (C) Mashape, Inc.

local ffi = require "ffi"
local bit = require "bit"
local cjson = require "cjson"
local fd_util = require "kong.plugins.file-log.fd_util"
local system_constants = require "lua_system_constants"
local basic_serializer = require "kong.plugins.log-serializers.basic"

ffi.cdef[[
int open(char * filename, int flags, int mode);
int write(int fd, void * ptr, int numbytes);
]]

local octal = function(n) return tonumber(n, 8) end

local O_CREAT = octal('0100')
local O_APPEND = octal('02000')
local O_WRONLY = octal('0001')

local S_IWUSR = octal('00200')
local S_IRUSR = octal('00400')
local S_IXUSR = octal('00100')
char *strerror(int errnum);
]]

local function string_to_char(str)
return ffi.cast("uint8_t*", str)
Expand All @@ -34,8 +26,15 @@ local function log(premature, conf, message)

local fd = fd_util.get_fd(conf.path)
if not fd then
fd = ffi.C.open(string_to_char(conf.path), bit.bor(O_CREAT, O_APPEND, O_WRONLY), bit.bor(S_IWUSR, S_IRUSR, S_IXUSR))
fd_util.set_fd(conf.path, fd)
fd = ffi.C.open(string_to_char(conf.path),
bit.bor(system_constants.O_WRONLY(), system_constants.O_CREAT(), system_constants.O_APPEND()),
bit.bor(system_constants.S_IWUSR(), system_constants.S_IRUSR(), system_constants.S_IXUSR()))
if fd < 0 then
local errno = ffi.errno()
ngx.log(ngx.ERR, "[file-log] failed to open the file: ", ffi.string(ffi.C.strerror(errno)))
else
fd_util.set_fd(conf.path, fd)
end
end

ffi.C.write(fd, string_to_char(message), string.len(message))
Expand Down