如何使用 ffi.C.lstat?

我想在我的ngx_lua程序中使用lstat函数获取文件的基本信息。init.lua的内容如下:

local ffi = require "ffi"
local ffi_C = ffi.C
local ffi_new = ffi.new

ffi.cdef[[
    typedef long long time_t;
    typedef struct timespec {
        time_t  tv_sec;
        long    tv_nsec;
        ...;
    };
    typedef struct stat {
        struct timespec st_mtim;
        ...;
    };

    int lstat(const char *path, struct stat *buf);
]]

buf = ffi_new("struct stat *")

function checkfile (filename, buf)
    ffi_C.lstat(filename, buf)
end

当我启动nginx时,出现了一些错误。内容如下:

  • 2014/04/25 15:00:39 [error] 26396#0: lua entry thread aborted: runtime error: /home/nginx/conf/cop/init.lua:42: /usr/local/lib/libluajit-5.1.so.2: undefined symbol: lstat stack traceback: coroutine 0: [C]: in function '__index' /home/nginx/conf/cop/init.lua:42: in function 'checkfile' /home/nginx/conf/cop/check1.lua:37: in function , context: ngx.timer
点赞
用户646619
用户646619

buf = ffi_new("struct stat *") 创建一个指向 struct stat 对象的新 指针; 它并不会分配一个实际的 struct stat 实例。你需要使用 ffi_new("struct stat")

在尝试使用 FFI 做任何事情之前,请确保你熟悉 C 的内存模型,因为使用 LuaJIT 的 FFI 基本上就是写 C 代码。它无法保护你免受解引用 NULL 等的影响。

同时,buf 是一个全局变量,你可能不想要。请务必将其定义为 local

2014-04-25 15:51:59
用户3112574
用户3112574

除了上面提到的类型问题外,还有一个更加繁琐的问题:

在glibc中,stat()和朋友们(lstat(),fstat(),...)通常是解析为__xstat()等宏。

__xstat()有3个参数,第一个指定您要作为缓冲区的stat结构体的哪个版本,但布局似乎非常依赖于libc版本。

(例如,在 C和LD_PRELOAD。open和open64被拦截,但不是stat64 中提到)

为了解决这个问题,对于luaJIT的FFI,似乎使用syscall()发送stat的编号(即arm32和x86_64上的106),并从内核中获取struct stat定义是最可靠的方法(在x86和arm32上相同,在x86_64上不同)。

您也可以潜在地使用ljsyscall(https://github.com/justincormack/ljsyscall),但我没有尝试过。

2018-03-19 19:24:30