From 4724bc52df2b22357103d6569f9664c5651a3ebe Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 5 Apr 2022 23:37:32 +0800 Subject: improve get uid --- core/src/xmake/engine.c | 8 +-- core/src/xmake/makefile | 4 +- core/src/xmake/os/getgid.c | 151 +++++++++++++++++++++++++++++++++++++++++++++ core/src/xmake/os/getuid.c | 151 +++++++++++++++++++++++++++++++++++++++++++++ core/src/xmake/os/gid.c | 151 --------------------------------------------- core/src/xmake/os/uid.c | 151 --------------------------------------------- xmake/core/base/os.lua | 20 +++--- 7 files changed, 318 insertions(+), 318 deletions(-) create mode 100644 core/src/xmake/os/getgid.c create mode 100644 core/src/xmake/os/getuid.c delete mode 100644 core/src/xmake/os/gid.c delete mode 100644 core/src/xmake/os/uid.c diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c index 005d2eebe..7183140e0 100644 --- a/core/src/xmake/engine.c +++ b/core/src/xmake/engine.c @@ -113,8 +113,8 @@ tb_int_t xm_os_syserror(lua_State* lua); tb_int_t xm_os_strerror(lua_State* lua); tb_int_t xm_os_getwinsize(lua_State* lua); #ifndef TB_CONFIG_OS_WINDOWS -tb_int_t xm_os_uid(lua_State* lua); -tb_int_t xm_os_gid(lua_State* lua); +tb_int_t xm_os_getuid(lua_State* lua); +tb_int_t xm_os_getgid(lua_State* lua); tb_int_t xm_os_getown(lua_State* lua); #endif @@ -290,8 +290,8 @@ static luaL_Reg const g_os_functions[] = , { "filesize", xm_os_filesize } , { "getwinsize", xm_os_getwinsize} #ifndef TB_CONFIG_OS_WINDOWS -, { "uid", xm_os_uid } -, { "gid", xm_os_gid } +, { "uid", xm_os_getuid } +, { "gid", xm_os_getgid } , { "getown", xm_os_getown } #endif , { tb_null, tb_null } diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile index 2203b81b7..c07ba7153 100644 --- a/core/src/xmake/makefile +++ b/core/src/xmake/makefile @@ -44,8 +44,8 @@ xmake_C_FILES += \ os/strerror \ os/filesize \ os/getwinsize \ - os/uid \ - os/gid \ + os/getuid \ + os/getgid \ os/getown \ io/stdfile \ io/file_size \ diff --git a/core/src/xmake/os/getgid.c b/core/src/xmake/os/getgid.c new file mode 100644 index 000000000..a089f855e --- /dev/null +++ b/core/src/xmake/os/getgid.c @@ -0,0 +1,151 @@ +/*!A cross-platform build utility based on Lua + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2015-present, TBOOX Open Source Group. + * + * @author TitanSnow + * @file getgid.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "getgid" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#ifndef TB_CONFIG_OS_WINDOWS +# include +# include +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ +#ifndef TB_CONFIG_OS_WINDOWS + +// get & set gid +tb_int_t xm_os_getgid(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + tb_int_t rgidset = -1; + tb_int_t egidset = -1; + tb_int_t argc = lua_gettop(lua); + if (argc == 1) + { + if (lua_istable(lua, 1)) + { + // os.getgid({["rgid"] = rgid, ["egid"] = egid}) + lua_getfield(lua, 1, "rgid"); + lua_getfield(lua, 1, "egid"); + if (!lua_isnil(lua, -1)) + { + if (!lua_isnumber(lua, -1)) + { + lua_pushfstring(lua, "invalid field type(%s) in `egid` for os.getgid", luaL_typename(lua, -1)); + lua_error(lua); + return 0; + } + egidset = (tb_int_t)lua_tonumber(lua, -1); + } + lua_pop(lua, 1); + if (!lua_isnil(lua, -1)) + { + if (!lua_isnumber(lua, -1)) + { + lua_pushfstring(lua, "invalid field type(%s) in `rgid` for os.getgid", luaL_typename(lua, -1)); + lua_error(lua); + return 0; + } + rgidset = (tb_int_t)lua_tonumber(lua, -1); + } + lua_pop(lua, 1); + } + else if (lua_isnumber(lua, 1)) + { + // os.getgid(gid) + rgidset = egidset = (tb_int_t)lua_tonumber(lua, 1); + } + else + { + lua_pushfstring(lua, "invalid argument type(%s) for os.getgid", luaL_typename(lua, 1)); + lua_error(lua); + return 0; + } + } + else if (argc == 2) + { + // os.getgid(rgid, egid) + if (!lua_isnil(lua, 1)) + { + if (!lua_isnumber(lua, 1)) + { + lua_pushfstring(lua, "invalid argument type(%s) for os.getgid", luaL_typename(lua, 1)); + lua_error(lua); + return 0; + } + rgidset = (tb_int_t)lua_tonumber(lua, 1); + } + if (!lua_isnil(lua, 2)) + { + if (!lua_isnumber(lua, 2)) + { + lua_pushfstring(lua, "invalid argument type(%s) for os.getgid", luaL_typename(lua, 2)); + lua_error(lua); + return 0; + } + egidset = (tb_int_t)lua_tonumber(lua, 2); + } + } + else if (argc != 0) + { + lua_pushstring(lua, "invalid argument count for os.getgid"); + lua_error(lua); + return 0; + } + + // store return value + lua_newtable(lua); + + if (rgidset != -1 || egidset != -1) + { + // set rgid & egid + lua_pushstring(lua, "errno"); + lua_pushinteger(lua, setregid(rgidset, egidset) != 0 ? errno : 0); + lua_settable(lua, -3); + } + + // get gid & egid + gid_t gid = getgid(); + gid_t egid = getegid(); + + // push + lua_pushstring(lua, "rgid"); + lua_pushinteger(lua, gid); + lua_settable(lua, -3); + lua_pushstring(lua, "egid"); + lua_pushinteger(lua, egid); + lua_settable(lua, -3); + + // ok + return 1; +} + +#endif diff --git a/core/src/xmake/os/getuid.c b/core/src/xmake/os/getuid.c new file mode 100644 index 000000000..9c2c2fa2c --- /dev/null +++ b/core/src/xmake/os/getuid.c @@ -0,0 +1,151 @@ +/*!A cross-platform build utility based on Lua + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2015-present, TBOOX Open Source Group. + * + * @author TitanSnow + * @file getuid.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "getuid" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#ifndef TB_CONFIG_OS_WINDOWS +# include +# include +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ +#ifndef TB_CONFIG_OS_WINDOWS + +// get & set uid +tb_int_t xm_os_getuid(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + tb_int_t ruidset = -1; + tb_int_t euidset = -1; + tb_int_t argc = lua_gettop(lua); + if (argc == 1) + { + if (lua_istable(lua, 1)) + { + // os.getuid({["ruid"] = ruid, ["euid"] = euid}) + lua_getfield(lua, 1, "ruid"); + lua_getfield(lua, 1, "euid"); + if (!lua_isnil(lua, -1)) + { + if (!lua_isnumber(lua, -1)) + { + lua_pushfstring(lua, "invalid field type(%s) in `euid` for os.getuid", luaL_typename(lua, -1)); + lua_error(lua); + return 0; + } + euidset = (tb_int_t)lua_tonumber(lua, -1); + } + lua_pop(lua, 1); + if (!lua_isnil(lua, -1)) + { + if (!lua_isnumber(lua, -1)) + { + lua_pushfstring(lua, "invalid field type(%s) in `ruid` for os.getuid", luaL_typename(lua, -1)); + lua_error(lua); + return 0; + } + ruidset = (tb_int_t)lua_tonumber(lua, -1); + } + lua_pop(lua, 1); + } + else if (lua_isnumber(lua, 1)) + { + // os.getuid(uid) + ruidset = euidset = (tb_int_t)lua_tonumber(lua, 1); + } + else + { + lua_pushfstring(lua, "invalid argument type(%s) for os.getuid", luaL_typename(lua, 1)); + lua_error(lua); + return 0; + } + } + else if (argc == 2) + { + // os.getuid(ruid, euid) + if (!lua_isnil(lua, 1)) + { + if (!lua_isnumber(lua, 1)) + { + lua_pushfstring(lua, "invalid argument type(%s) for os.getuid", luaL_typename(lua, 1)); + lua_error(lua); + return 0; + } + ruidset = (tb_int_t)lua_tonumber(lua, 1); + } + if (!lua_isnil(lua, 2)) + { + if (!lua_isnumber(lua, 2)) + { + lua_pushfstring(lua, "invalid argument type(%s) for os.getuid", luaL_typename(lua, 2)); + lua_error(lua); + return 0; + } + euidset = (tb_int_t)lua_tonumber(lua, 2); + } + } + else if (argc != 0) + { + lua_pushstring(lua, "invalid argument count for os.getuid"); + lua_error(lua); + return 0; + } + + // store return value + lua_newtable(lua); + + if (ruidset != -1 || euidset != -1) + { + // set ruid & euid + lua_pushstring(lua, "errno"); + lua_pushinteger(lua, setreuid(ruidset, euidset) != 0 ? errno : 0); + lua_settable(lua, -3); + } + + // get uid & euid + uid_t uid = getuid(); + uid_t euid = geteuid(); + + // push + lua_pushstring(lua, "ruid"); + lua_pushinteger(lua, uid); + lua_settable(lua, -3); + lua_pushstring(lua, "euid"); + lua_pushinteger(lua, euid); + lua_settable(lua, -3); + + // ok + return 1; +} + +#endif diff --git a/core/src/xmake/os/gid.c b/core/src/xmake/os/gid.c deleted file mode 100644 index 8a05cd145..000000000 --- a/core/src/xmake/os/gid.c +++ /dev/null @@ -1,151 +0,0 @@ -/*!A cross-platform build utility based on Lua - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Copyright (C) 2015-present, TBOOX Open Source Group. - * - * @author TitanSnow - * @file gid.c - * - */ - -/* ////////////////////////////////////////////////////////////////////////////////////// - * trace - */ -#define TB_TRACE_MODULE_NAME "gid" -#define TB_TRACE_MODULE_DEBUG (0) - -/* ////////////////////////////////////////////////////////////////////////////////////// - * includes - */ -#include "prefix.h" -#ifndef TB_CONFIG_OS_WINDOWS -# include -# include -#endif - -/* ////////////////////////////////////////////////////////////////////////////////////// - * implementation - */ -#ifndef TB_CONFIG_OS_WINDOWS - -// get & set gid -tb_int_t xm_os_gid(lua_State* lua) -{ - // check - tb_assert_and_check_return_val(lua, 0); - - tb_int_t rgidset = -1; - tb_int_t egidset = -1; - tb_int_t argc = lua_gettop(lua); - if (argc == 1) - { - if (lua_istable(lua, 1)) - { - // os.gid({["rgid"] = rgid, ["egid"] = egid}) - lua_getfield(lua, 1, "rgid"); - lua_getfield(lua, 1, "egid"); - if (!lua_isnil(lua, -1)) - { - if (!lua_isnumber(lua, -1)) - { - lua_pushfstring(lua, "invalid field type(%s) in `egid` for os.gid", luaL_typename(lua, -1)); - lua_error(lua); - return 0; - } - egidset = (tb_int_t)lua_tonumber(lua, -1); - } - lua_pop(lua, 1); - if (!lua_isnil(lua, -1)) - { - if (!lua_isnumber(lua, -1)) - { - lua_pushfstring(lua, "invalid field type(%s) in `rgid` for os.gid", luaL_typename(lua, -1)); - lua_error(lua); - return 0; - } - rgidset = (tb_int_t)lua_tonumber(lua, -1); - } - lua_pop(lua, 1); - } - else if (lua_isnumber(lua, 1)) - { - // os.gid(gid) - rgidset = egidset = (tb_int_t)lua_tonumber(lua, 1); - } - else - { - lua_pushfstring(lua, "invalid argument type(%s) for os.gid", luaL_typename(lua, 1)); - lua_error(lua); - return 0; - } - } - else if (argc == 2) - { - // os.gid(rgid, egid) - if (!lua_isnil(lua, 1)) - { - if (!lua_isnumber(lua, 1)) - { - lua_pushfstring(lua, "invalid argument type(%s) for os.gid", luaL_typename(lua, 1)); - lua_error(lua); - return 0; - } - rgidset = (tb_int_t)lua_tonumber(lua, 1); - } - if (!lua_isnil(lua, 2)) - { - if (!lua_isnumber(lua, 2)) - { - lua_pushfstring(lua, "invalid argument type(%s) for os.gid", luaL_typename(lua, 2)); - lua_error(lua); - return 0; - } - egidset = (tb_int_t)lua_tonumber(lua, 2); - } - } - else if (argc != 0) - { - lua_pushstring(lua, "invalid argument count for os.gid"); - lua_error(lua); - return 0; - } - - // store return value - lua_newtable(lua); - - if (rgidset != -1 || egidset != -1) - { - // set rgid & egid - lua_pushstring(lua, "errno"); - lua_pushinteger(lua, setregid(rgidset, egidset) != 0 ? errno : 0); - lua_settable(lua, -3); - } - - // get gid & egid - gid_t gid = getgid(); - gid_t egid = getegid(); - - // push - lua_pushstring(lua, "rgid"); - lua_pushinteger(lua, gid); - lua_settable(lua, -3); - lua_pushstring(lua, "egid"); - lua_pushinteger(lua, egid); - lua_settable(lua, -3); - - // ok - return 1; -} - -#endif diff --git a/core/src/xmake/os/uid.c b/core/src/xmake/os/uid.c deleted file mode 100644 index bdaee036f..000000000 --- a/core/src/xmake/os/uid.c +++ /dev/null @@ -1,151 +0,0 @@ -/*!A cross-platform build utility based on Lua - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Copyright (C) 2015-present, TBOOX Open Source Group. - * - * @author TitanSnow - * @file uid.c - * - */ - -/* ////////////////////////////////////////////////////////////////////////////////////// - * trace - */ -#define TB_TRACE_MODULE_NAME "uid" -#define TB_TRACE_MODULE_DEBUG (0) - -/* ////////////////////////////////////////////////////////////////////////////////////// - * includes - */ -#include "prefix.h" -#ifndef TB_CONFIG_OS_WINDOWS -# include -# include -#endif - -/* ////////////////////////////////////////////////////////////////////////////////////// - * implementation - */ -#ifndef TB_CONFIG_OS_WINDOWS - -// get & set uid -tb_int_t xm_os_uid(lua_State* lua) -{ - // check - tb_assert_and_check_return_val(lua, 0); - - tb_int_t ruidset = -1; - tb_int_t euidset = -1; - tb_int_t argc = lua_gettop(lua); - if (argc == 1) - { - if (lua_istable(lua, 1)) - { - // os.uid({["ruid"] = ruid, ["euid"] = euid}) - lua_getfield(lua, 1, "ruid"); - lua_getfield(lua, 1, "euid"); - if (!lua_isnil(lua, -1)) - { - if (!lua_isnumber(lua, -1)) - { - lua_pushfstring(lua, "invalid field type(%s) in `euid` for os.uid", luaL_typename(lua, -1)); - lua_error(lua); - return 0; - } - euidset = (tb_int_t)lua_tonumber(lua, -1); - } - lua_pop(lua, 1); - if (!lua_isnil(lua, -1)) - { - if (!lua_isnumber(lua, -1)) - { - lua_pushfstring(lua, "invalid field type(%s) in `ruid` for os.uid", luaL_typename(lua, -1)); - lua_error(lua); - return 0; - } - ruidset = (tb_int_t)lua_tonumber(lua, -1); - } - lua_pop(lua, 1); - } - else if (lua_isnumber(lua, 1)) - { - // os.uid(uid) - ruidset = euidset = (tb_int_t)lua_tonumber(lua, 1); - } - else - { - lua_pushfstring(lua, "invalid argument type(%s) for os.uid", luaL_typename(lua, 1)); - lua_error(lua); - return 0; - } - } - else if (argc == 2) - { - // os.uid(ruid, euid) - if (!lua_isnil(lua, 1)) - { - if (!lua_isnumber(lua, 1)) - { - lua_pushfstring(lua, "invalid argument type(%s) for os.uid", luaL_typename(lua, 1)); - lua_error(lua); - return 0; - } - ruidset = (tb_int_t)lua_tonumber(lua, 1); - } - if (!lua_isnil(lua, 2)) - { - if (!lua_isnumber(lua, 2)) - { - lua_pushfstring(lua, "invalid argument type(%s) for os.uid", luaL_typename(lua, 2)); - lua_error(lua); - return 0; - } - euidset = (tb_int_t)lua_tonumber(lua, 2); - } - } - else if (argc != 0) - { - lua_pushstring(lua, "invalid argument count for os.uid"); - lua_error(lua); - return 0; - } - - // store return value - lua_newtable(lua); - - if (ruidset != -1 || euidset != -1) - { - // set ruid & euid - lua_pushstring(lua, "errno"); - lua_pushinteger(lua, setreuid(ruidset, euidset) != 0 ? errno : 0); - lua_settable(lua, -3); - } - - // get uid & euid - uid_t uid = getuid(); - uid_t euid = geteuid(); - - // push - lua_pushstring(lua, "ruid"); - lua_pushinteger(lua, uid); - lua_settable(lua, -3); - lua_pushstring(lua, "euid"); - lua_pushinteger(lua, euid); - lua_settable(lua, -3); - - // ok - return 1; -} - -#endif diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua index 823d3d009..39530eb0f 100644 --- a/xmake/core/base/os.lua +++ b/xmake/core/base/os.lua @@ -31,8 +31,8 @@ local string = require("base/string") local process = require("base/process") -- save original interfaces -os._uid = os._uid or os.uid -os._gid = os._gid or os.gid +os._getuid = os._getuid or os.getuid or os.uid -- for old core +os._getgid = os._getgid or os.getgid or os.gid os._exit = os._exit or os.exit os._mkdir = os._mkdir or os.mkdir os._rmdir = os._rmdir or os.rmdir @@ -609,7 +609,7 @@ function os.tmpdir(opt) local subdir = os._TMPSUBDIR if not subdir then local name = "." .. xmake._NAME - subdir = path.join((os._FAKEROOT and (name .. "fake") or name) .. (os.uid().euid or ""), os.date("%y%m%d")) + subdir = path.join((os._FAKEROOT and (name .. "fake") or name) .. (os.getuid().euid or ""), os.date("%y%m%d")) os._TMPSUBDIR = subdir end @@ -974,26 +974,26 @@ function os.nuldev(input) end -- get uid -function os.uid(...) +function os.getuid(...) os._UID = {} - if os._uid then - os._UID = os._uid(...) or {} + if os._getuid then + os._UID = os._getuid(...) or {} end return os._UID end -- get gid -function os.gid(...) +function os.getgid(...) os._GID = {} - if os._gid then - os._GID = os._gid(...) or {} + if os._getgid then + os._GID = os._getgid(...) or {} end return os._GID end -- check the current command is running as root function os.isroot() - return os.uid().euid == 0 + return os.getuid().euid == 0 end -- is case-insensitive filesystem? -- cgit v1.3.1