ioctl_helper.lua (1182B)
1 local base = string.match(..., "(.-)[^%.]+%.[^%.]+$") 2 local ffi = require("ffi") 3 local e_assert = require(base.."helper.errno").assert 4 5 local ffi_errno, ffi_string = ffi.errno, ffi.string 6 7 local module = {} 8 9 function module.check_0(func) 10 return function(self, ...) 11 ffi_errno(0) 12 e_assert("ioctl", func(self.fd, ...) == 0, 2) 13 end 14 end 15 16 function module.check_0_argwrap(func, wrap) 17 return function(self, ...) 18 local arg = wrap(...) 19 ffi_errno(0) 20 e_assert("ioctl", func(self.fd, arg.raw) == 0, 2) 21 end 22 end 23 24 function module.check_1(func) 25 return function(self, ...) 26 ffi_errno(0) 27 local res, x0 = func(self.fd, ...) 28 e_assert("ioctl", res == 0, 2) 29 return x0 30 end 31 end 32 33 function module.check_1_reswrap(func, wrap) 34 return function(self, ...) 35 ffi_errno(0) 36 local res, x0 = func(self.fd, ...) 37 e_assert("ioctl", res == 0, 2) 38 return wrap(x0) 39 end 40 end 41 42 local char_ary = ffi.typeof("char[?]") 43 function module.string_prop(ioctl) 44 return function(self, len) 45 len = len or 4096 46 local buf = char_ary(len) 47 local res = ioctl(self.fd, buf) 48 e_assert("ioctl", res >= 0, 2) 49 return ffi_string(buf, res-1) 50 end 51 end 52 53 54 return module