Skip to content

Commit

Permalink
directvt#464 WIP: XLinkVT mode (fds)
Browse files Browse the repository at this point in the history
  • Loading branch information
o-sdn-o committed Dec 1, 2023
1 parent ee128ac commit f2c603c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 44 deletions.
8 changes: 4 additions & 4 deletions src/netxs/apps.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,9 +453,9 @@ namespace netxs::app::shared
{
boss.LISTEN(tier::anycast, e2::form::upon::started, root, -, (cmd, cwd, env, patch))
{
boss.start(patch, [cmd, cwd, env](auto r, auto w)
boss.start(patch, [cmd, cwd, env](auto fds)
{
os::dtvt::connect(cmd, cwd, env, r, w);
os::dtvt::connect(cmd, cwd, env, fds);
return cmd;
});
};
Expand Down Expand Up @@ -514,9 +514,9 @@ namespace netxs::app::shared
auto& window_inst = *window;
boss.LISTEN(tier::anycast, e2::form::upon::started, root, -, (cmd, cwd, env, patch))
{
boss.start(patch, [&, cmd, cwd, env](auto r, auto w)
boss.start(patch, [&, cmd, cwd, env](auto fds)
{
term_inst.start(cmd, cwd, env, r, w);
term_inst.start(cmd, cwd, env, fds);
return cmd;
});
};
Expand Down
36 changes: 21 additions & 15 deletions src/netxs/desktopio/consrv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ struct consrv
return inst;
}
template<class Term, class Proc>
auto attach(Term& terminal, text cmd, text cwd, text env, twod win, Proc trailer, fd_t r, fd_t w)
auto attach(Term& terminal, text cmd, text cwd, text env, twod win, Proc trailer, fdrw fdlink)
{
auto err_code = 0;
auto startinf = STARTUPINFOEXW{ sizeof(STARTUPINFOEXW) };
Expand All @@ -66,18 +66,17 @@ struct consrv
}
start();
auto handle_count = 3;
//todo close unused handles
if (r == os::invalid_fd)
if (fdlink)
{
startinf.StartupInfo.hStdInput = nt::console::handle(condrv, "\\Input", true); // Windows8's cmd.exe requires that handles.
startinf.StartupInfo.hStdOutput = nt::console::handle(condrv, "\\Output", true); //
startinf.StartupInfo.hStdError = nt::console::handle(startinf.StartupInfo.hStdOutput); //
handle_count = 2;
startinf.StartupInfo.hStdInput = fdlink->r;
startinf.StartupInfo.hStdOutput = fdlink->w;
}
else
{
handle_count = 2;
startinf.StartupInfo.hStdInput = r;
startinf.StartupInfo.hStdOutput = w;
startinf.StartupInfo.hStdInput = nt::console::handle(condrv, "\\Input", true); // Windows8's cmd.exe requires that handles.
startinf.StartupInfo.hStdOutput = nt::console::handle(condrv, "\\Output", true); //
startinf.StartupInfo.hStdError = nt::console::handle(startinf.StartupInfo.hStdOutput); //
}
startinf.StartupInfo.dwX = 0;
startinf.StartupInfo.dwY = 0;
Expand Down Expand Up @@ -124,6 +123,12 @@ struct consrv
: nullptr,
&startinf.StartupInfo, // lpStartupInfo (ptr to STARTUPINFOEX)
&procsinf); // lpProcessInformation
if (!fdlink)
{
os::close(startinf.StartupInfo.hStdInput);
os::close(startinf.StartupInfo.hStdOutput);
os::close(startinf.StartupInfo.hStdError);
}
if (ret == 0)
{
prochndl = { os::invalid_fd };
Expand Down Expand Up @@ -5224,7 +5229,7 @@ struct consrv : ipc::stdcon
return ptr::shared<consrv>(terminal);
}
template<class Term, class Proc>
auto attach(Term& terminal, text cmd, text cwd, text env, twod win, Proc trailer, fd_t r, fd_t w)
auto attach(Term& terminal, text cmd, text cwd, text env, twod win, Proc trailer, fdrw fdlink)
{
auto fdm = os::syscall{ ::posix_openpt(O_RDWR | O_NOCTTY) }; // Get master TTY.
auto rc1 = os::syscall{ ::grantpt(fdm.value) }; // Grant master TTY file access.
Expand All @@ -5244,16 +5249,17 @@ struct consrv : ipc::stdcon
winsz(win); // TTY resize can be done only after assigning a controlling TTY (BSD-requirement).
os::dtvt::active = faux; // Logger update.
os::dtvt::client = {}; //
if (r == os::invalid_fd)
if (fdlink)
{
::dup2(fds.value, STDIN_FILENO); os::stdin_fd = STDIN_FILENO;
::dup2(fds.value, STDOUT_FILENO); os::stdout_fd = STDOUT_FILENO;
::dup2(fdlink->r, STDIN_FILENO); os::stdin_fd = STDIN_FILENO;
::dup2(fdlink->w, STDOUT_FILENO); os::stdout_fd = STDOUT_FILENO;
::dup2(fds.value, STDERR_FILENO); os::stderr_fd = STDERR_FILENO;
fdlink.reset();
}
else
{
::dup2(r, STDIN_FILENO); os::stdin_fd = STDIN_FILENO;
::dup2(w, STDOUT_FILENO); os::stdout_fd = STDOUT_FILENO;
::dup2(fds.value, STDIN_FILENO); os::stdin_fd = STDIN_FILENO;
::dup2(fds.value, STDOUT_FILENO); os::stdout_fd = STDOUT_FILENO;
::dup2(fds.value, STDERR_FILENO); os::stderr_fd = STDERR_FILENO;
}
os::fdscleanup();
Expand Down
29 changes: 15 additions & 14 deletions src/netxs/desktopio/system.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,8 @@ namespace netxs::os
}
};

using fdrw = sptr<sock>;

struct fire
{
flag fired{};
Expand Down Expand Up @@ -3254,7 +3256,7 @@ namespace netxs::os
return dtvt::mode;
}();

auto connect(text cmd, text cwd, text env, fd_t r, fd_t w)
auto connect(text cmd, text cwd, text env, fdrw fds)
{
log("%%New process '%cmd%' at the %path%", prompt::dtvt, utf::debase(cmd), cwd.empty() ? "current directory"s : "'" + cwd + "'");
auto result = true;
Expand All @@ -3277,8 +3279,8 @@ namespace netxs::os
attrbuff.resize(attrsize);
startinf.lpAttributeList = reinterpret_cast<LPPROC_THREAD_ATTRIBUTE_LIST>(attrbuff.data());
startinf.StartupInfo.dwFlags = STARTF_USESTDHANDLES;
startinf.StartupInfo.hStdInput = r;
startinf.StartupInfo.hStdOutput = w;
startinf.StartupInfo.hStdInput = fds->r;
startinf.StartupInfo.hStdOutput = fds->w;
result = true
&& ::InitializeProcThreadAttributeList(startinf.lpAttributeList, 1, 0, &attrsize)
&& ::UpdateProcThreadAttribute(startinf.lpAttributeList,
Expand Down Expand Up @@ -3319,9 +3321,10 @@ namespace netxs::os
{
os::dtvt::active = true;
::setsid(); // Dissociate from existing controlling terminal (create a new session without a controlling terminal).
::dup2(r, STDIN_FILENO); os::stdin_fd = STDIN_FILENO;
::dup2(w, STDOUT_FILENO); os::stdout_fd = STDOUT_FILENO;
::close(STDERR_FILENO); os::stderr_fd = os::invalid_fd;
::dup2(fds->r, STDIN_FILENO); os::stdin_fd = STDIN_FILENO;
::dup2(fds->w, STDOUT_FILENO); os::stdout_fd = STDOUT_FILENO;
::close(STDERR_FILENO); os::stderr_fd = os::invalid_fd;
fds.reset();
if (cwd.size())
{
auto err = std::error_code{};
Expand Down Expand Up @@ -3360,8 +3363,6 @@ namespace netxs::os
}

#endif
os::close(r); // Close inheritable handles to avoid deadlocking at process exit.
os::close(w); // Only when all write handles to the pipe are closed, the ReadFile function returns zero.
return result;
}

Expand Down Expand Up @@ -3427,7 +3428,7 @@ namespace netxs::os
}
termlink = ipc::stdcon{ m_pipe_r, m_pipe_w };

auto cmd = connect(s_pipe_r, s_pipe_w);
auto cmd = connect(ptr::shared<sock>(s_pipe_r, s_pipe_w));

attached.exchange(!!termlink);
if (attached)
Expand Down Expand Up @@ -3486,7 +3487,7 @@ namespace netxs::os
writebuf = {};
if (termlink) termlink->cleanup(io_log);
}
void create(auto& terminal, text cmd, text cwd, text env, twod win, fd_t r, fd_t w)
void create(auto& terminal, text cmd, text cwd, text env, twod win, fdrw fds)
{
if (terminal.io_log) log("%%New TTY of size %win_size%", prompt::vtty, win);
log("%%New process '%cmd%' at the %path%", prompt::vtty, utf::debase(cmd), cwd.empty() ? "current directory"s : "'" + cwd + "'");
Expand All @@ -3508,7 +3509,7 @@ namespace netxs::os
}
}
};
auto errcode = termlink->attach(terminal, cmd, cwd, env, win, trailer, r, w);
auto errcode = termlink->attach(terminal, cmd, cwd, env, win, trailer, fds);
if (errcode)
{
terminal.onexit(errcode, "Process creation error \r\n"s
Expand Down Expand Up @@ -3537,13 +3538,13 @@ namespace netxs::os
guard.lock();
}
}
void runapp(auto& terminal, text cmd, text cwd, text env, twod win, fd_t r = os::invalid_fd, fd_t w = os::invalid_fd)
void runapp(auto& terminal, text cmd, text cwd, text env, twod win, fdrw fds = {})
{
signaled.exchange(faux);
stdwrite = std::thread{[&, cmd, cwd, env, win, r, w]
stdwrite = std::thread{[&, cmd, cwd, env, win, fds]
{
if (terminal.io_log) log(prompt::vtty, "Writing thread started", ' ', utf::to_hex_0x(stdwrite.get_id()));
create(terminal, cmd, cwd, env, win, r, w);
create(terminal, cmd, cwd, env, win, fds);
writer(terminal);
if (terminal.io_log) log(prompt::vtty, "Writing thread ended", ' ', utf::to_hex_0x(stdwrite.get_id()));
}};
Expand Down
16 changes: 5 additions & 11 deletions src/netxs/desktopio/terminal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6208,8 +6208,7 @@ namespace netxs::ui
text envvar; // term: Environment block.
text curdir; // term: Current working directory.
text cmdarg; // term: Startup command line arguments.
os::fd_t r_link; // term: .
os::fd_t w_link; // term: .
os::fdrw fdlink; // term: Optional DirectVT uplink.
hook onerun; // term: One-shot token for restart session.
twod origin; // term: Viewport position.
twod follow; // term: Viewport follows cursor (bool: X, Y).
Expand Down Expand Up @@ -7110,21 +7109,16 @@ namespace netxs::ui
{
if (!ipccon)
{
ipccon.runapp(*this, cmdarg, curdir, envvar, target->panel, r_link, w_link);
ipccon.runapp(*this, cmdarg, curdir, envvar, target->panel, fdlink);
}
}
void start(text cmd, text cwd, text env, os::fd_t r = os::invalid_fd, os::fd_t w = os::invalid_fd)
void start(text cmd, text cwd, text env, os::fdrw fds = {})
{
cmdarg = cmd;
curdir = cwd;
envvar = env;
//todo close unused handles (ipc::stdcon)
r_link = r;
w_link = w;
if (!ipccon)
{
ipccon.runapp(*this, cmd, cwd, env, target->panel, r, w);
}
fdlink = fds;
start();
}
void close()
{
Expand Down

0 comments on commit f2c603c

Please sign in to comment.