[tarantool-patches] Re: [PATCH] lua: pwd: fix passwd and group traversal

Vladislav Shpilevoy v.shpilevoy at tarantool.org
Fri Aug 23 23:26:05 MSK 2019


Hi! Thanks for the patch!

> The primary reason of this commit is to overcome a startup hang on
> CentOS 6.
> 
> https://github.com/tarantool/tarantool/issues/4428
> https://github.com/tarantool/tarantool/issues/4447
> https://github.com/tarantool/tarantool/issues/4271
> https://github.com/tarantool/tarantool/tree/Totktonada/gh-4447-fix-centos-6-startup-fail-full-ci
> 
> Waiting for CI:
> https://gitlab.com/tarantool/tarantool/pipelines/77906160
> 
> I manually verified that tarantool starts on CentOS 6 after the change.
> Also verified that the module can be used now on FreeBSD 12.0.
> 
> I understood that the commit formally changes a behaviour that can be
> visible for a user: it allows to pass cdata<struct passwd> to
> pwd.getgr() and to pass cdata<struct group> to pwd.getgr(). It is
> unlikely that one will want to use it or will find this ability by an
> occasion. Maybe only with pwd.getpw(1LL) or so, but luajit will report
> an error in the case. My intention was to keep the commit short and
> avoid refactoring of the module code.

But this module is not a rocket science, even its whole rewrite would
not be hard. Here the refactoring is quite simple, please, consider
my proposal on the branch and below. You can squash it or keep - anyway
LGTM. Note, I didn't test my 'fixes', so perhaps they are totally wrong.

=========================================================================

diff --git a/src/lua/pwd.lua b/src/lua/pwd.lua
index ab0229a72..7a585a3f2 100644
--- a/src/lua/pwd.lua
+++ b/src/lua/pwd.lua
@@ -108,17 +108,7 @@ end
 
 local pwgr_errstr = "get%s failed [errno %d]: %s"
 
-local function getgr(gid)
-    if gid == nil then
-        gid = tonumber(ffi.C.getgid())
-    end
-    local gr = type(gid) == 'cdata' and gid or _getgr(gid)
-    if gr == nil then
-        if errno() ~= 0 then
-            error(pwgr_errstr:format('gr', errno(), errno.strerror()))
-        end
-        return nil
-    end
+local function grtotable(gr)
     local gr_mem, group_members = gr.gr_mem, {}
     local i = 0
     while true do
@@ -137,25 +127,42 @@ local function getgr(gid)
     return group
 end
 
-local function getpw(uid)
-    if uid == nil then
-        uid = tonumber(ffi.C.getuid())
+local function getgr(gid)
+    if gid == nil then
+        gid = tonumber(ffi.C.getgid())
     end
-    local pw = type(uid) == 'cdata' and uid or _getpw(uid)
-    if pw == nil then
+    local gr = _getgr(gid)
+    if gr == nil then
         if errno() ~= 0 then
-            error(pwgr_errstr:format('pw', errno(), errno.strerror()))
+            error(pwgr_errstr:format('gr', errno(), errno.strerror()))
         end
         return nil
     end
-    local user = {
+    return grtotable(gr)
+end
+
+local function pwtotable(pw)
+    return {
         name    = ffi.string(pw.pw_name),
         id      = tonumber(pw.pw_uid),
         group   = getgr(pw.pw_gid),
         workdir = ffi.string(pw.pw_dir),
         shell   = ffi.string(pw.pw_shell),
     }
-    return user
+end
+
+local function getpw(uid)
+    if uid == nil then
+        uid = tonumber(ffi.C.getuid())
+    end
+    local pw = _getpw(uid)
+    if pw == nil then
+        if errno() ~= 0 then
+            error(pwgr_errstr:format('pw', errno(), errno.strerror()))
+        end
+        return nil
+    end
+    return pwtotable(pw)
 end
 
 local function getpwall()
@@ -170,7 +177,7 @@ local function getpwall()
             end
             break
         end
-        table.insert(pws, getpw(pw))
+        table.insert(pws, pwtotable(pw))
     end
     ffi.C.endpwent()
     return pws
@@ -188,7 +195,7 @@ local function getgrall()
             end
             break
         end
-        table.insert(grs, getgr(gr))
+        table.insert(grs, grtotable(gr))
     end
     ffi.C.endgrent()
     return grs




More information about the Tarantool-patches mailing list