]> git.e2factory.org Git - e2factory.git/commitdiff
e2lib: implement forkpty
authorTobias Ulmer <tu@emlix.com>
Tue, 12 Feb 2019 18:06:05 +0000 (19:06 +0100)
committerTobias Ulmer <tu@emlix.com>
Tue, 12 Feb 2019 18:06:05 +0000 (19:06 +0100)
Signed-off-by: Tobias Ulmer <tu@emlix.com>
generic/e2lib.lua
generic/le2lib.c

index 9823787cd841443ed9fcd3bcfd906849abf51834..aec3579ba5f8fe4bd09bf1ee56cfb0727a1f914b 100644 (file)
@@ -294,6 +294,30 @@ function e2lib.fork()
     return pid
 end
 
+--- Forkpty. Like fork but open a PTY.
+-- @return PID of child or false on error. PID of 0 means child process.
+-- @return File Descriptor for pseudo terminal (parent only).
+--         Error object on failure.
+-- @return Path to PTY device node.
+function e2lib.forkpty()
+    local rc, re
+    local pid, fdm, ptyname
+
+    pid, fdm, ptyname = le2lib.forkpty()
+    if not pid then
+        return false, err.new("failed to fork new pty: %s", fdm)
+    end
+    if pid == 0 then
+        -- child, return early
+        return pid
+    end
+
+    assert(type(fdm) == "number")
+    assert(type(ptyname) == "string")
+
+    return pid, fdm, ptyname
+end
+
 --- Set umask value.
 -- @param mask New umask value.
 -- @return Previous umask value.
index e5b0b7eae140b1347ccfd0ddc7f35c104760b29f..bac7f735f155165f2dc8f658f311359b5048b80b 100644 (file)
@@ -33,6 +33,7 @@
 #include <poll.h>
 #include <fcntl.h>
 #include <ctype.h>
+#include <pty.h>
 
 #include <lua.h>
 #include <lualib.h>
@@ -884,6 +885,29 @@ signal_received(lua_State *L)
        return 2;
 }
 
+static int
+do_forkpty(lua_State *L)
+{
+       int cpid, fdm;
+       char ptyname[PATH_MAX];
+
+       cpid = forkpty(&fdm, ptyname, NULL, NULL);
+       if (cpid < 0) {
+               lua_pushboolean(L, 0);
+               lua_pushstring(L, strerror(errno));
+               return 2;
+       }
+
+       lua_pushnumber(L, cpid);
+       if (cpid == 0)
+               return 1;
+
+       lua_pushnumber(L, fdm);
+       lua_pushstring(L, ptyname);
+
+       return 3;
+}
+
 static luaL_Reg lib[] = {
        { "chdir", change_directory },
        { "chmod", do_chmod },
@@ -893,6 +917,7 @@ static luaL_Reg lib[] = {
        { "execvp", do_execvp },
        { "exists", file_exists },
        { "fork", lua_fork },
+       { "forkpty", do_forkpty },
        { "getpid", do_getpid },
        { "hardlink", do_hardlink },
        { "kill", do_kill },