summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2022-04-05 23:43:18 +0800
committerruki <[email protected]>2022-04-05 23:43:18 +0800
commitc7e96adbab7d833ca63ac4fb73061f9949a46de0 (patch)
treebc4d9695f18c0d1525e4f8029013b5d5921faa8a
parent4724bc52df2b22357103d6569f9664c5651a3ebe (diff)
add os.getpid
-rw-r--r--core/src/xmake/engine.c6
-rw-r--r--core/src/xmake/makefile1
-rw-r--r--core/src/xmake/os/getpid.c54
-rw-r--r--xmake/core/base/os.lua11
-rw-r--r--xmake/core/sandbox/modules/os.lua1
5 files changed, 71 insertions, 2 deletions
diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c
index 7183140e0..e91e5ac3e 100644
--- a/core/src/xmake/engine.c
+++ b/core/src/xmake/engine.c
@@ -112,6 +112,7 @@ tb_int_t xm_os_emptydir(lua_State* lua);
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);
+tb_int_t xm_os_getpid(lua_State* lua);
#ifndef TB_CONFIG_OS_WINDOWS
tb_int_t xm_os_getuid(lua_State* lua);
tb_int_t xm_os_getgid(lua_State* lua);
@@ -289,9 +290,10 @@ static luaL_Reg const g_os_functions[] =
, { "syserror", xm_os_syserror }
, { "filesize", xm_os_filesize }
, { "getwinsize", xm_os_getwinsize}
+, { "getpid", xm_os_getpid }
#ifndef TB_CONFIG_OS_WINDOWS
-, { "uid", xm_os_getuid }
-, { "gid", xm_os_getgid }
+, { "getuid", xm_os_getuid }
+, { "getgid", 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 c07ba7153..d82ff51f9 100644
--- a/core/src/xmake/makefile
+++ b/core/src/xmake/makefile
@@ -46,6 +46,7 @@ xmake_C_FILES += \
os/getwinsize \
os/getuid \
os/getgid \
+ os/getpid \
os/getown \
io/stdfile \
io/file_size \
diff --git a/core/src/xmake/os/getpid.c b/core/src/xmake/os/getpid.c
new file mode 100644
index 000000000..7738652d0
--- /dev/null
+++ b/core/src/xmake/os/getpid.c
@@ -0,0 +1,54 @@
+/*!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 ruki
+ * @file getpid.c
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * trace
+ */
+#define TB_TRACE_MODULE_NAME "getpid"
+#define TB_TRACE_MODULE_DEBUG (0)
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "prefix.h"
+#ifdef TB_CONFIG_OS_WINDOWS
+# include <windows.h>
+#else
+# include <unistd.h>
+# include <errno.h>
+#endif
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * implementation
+ */
+tb_int_t xm_os_getpid(lua_State* lua)
+{
+ // check
+ tb_assert_and_check_return_val(lua, 0);
+
+#ifdef TB_CONFIG_OS_WINDOWS
+ lua_pushinteger(lua, (tb_int_t)GetCurrentProcessId());
+#else
+ lua_pushinteger(lua, (tb_int_t)getpid());
+#endif
+ return 1;
+}
+
diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua
index 39530eb0f..daff4d75b 100644
--- a/xmake/core/base/os.lua
+++ b/xmake/core/base/os.lua
@@ -33,6 +33,7 @@ local process = require("base/process")
-- save original interfaces
os._getuid = os._getuid or os.getuid or os.uid -- for old core
os._getgid = os._getgid or os.getgid or os.gid
+os._getpid = os._getpid or os.getpid
os._exit = os._exit or os.exit
os._mkdir = os._mkdir or os.mkdir
os._rmdir = os._rmdir or os.rmdir
@@ -991,6 +992,16 @@ function os.getgid(...)
return os._GID
end
+-- get pid
+function os.getpid(...)
+ local pid = os._PID
+ if pid == nil then
+ pid = os._getpid()
+ os._PID = pid
+ end
+ return pid
+end
+
-- check the current command is running as root
function os.isroot()
return os.getuid().euid == 0
diff --git a/xmake/core/sandbox/modules/os.lua b/xmake/core/sandbox/modules/os.lua
index c23d7f366..3782bc42f 100644
--- a/xmake/core/sandbox/modules/os.lua
+++ b/xmake/core/sandbox/modules/os.lua
@@ -75,6 +75,7 @@ sandbox_os.programfile = os.programfile
sandbox_os.projectdir = os.projectdir
sandbox_os.projectfile = os.projectfile
sandbox_os.getwinsize = os.getwinsize
+sandbox_os.getpid = os.getpid
-- syserror code
sandbox_os.SYSERR_UNKNOWN = os.SYSERR_UNKNOWN