2021-07-01 17:21:44 +02:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2012-2015, Christopher Jeffrey (MIT License)
|
|
|
|
* Copyright (c) 2017, Daniel Imms (MIT License)
|
2024-02-24 07:47:12 +01:00
|
|
|
* Copyright (c) 2021, 2024 Leroy Hopson (MIT License)
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MIT
|
2021-07-01 17:21:44 +02:00
|
|
|
*
|
|
|
|
* pty.cc:
|
|
|
|
* This file is responsible for starting processes
|
|
|
|
* with pseudo-terminal file descriptors.
|
|
|
|
*
|
|
|
|
* See:
|
|
|
|
* man pty
|
|
|
|
* man tty_ioctl
|
|
|
|
* man termios
|
|
|
|
* man forkpty
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Includes
|
|
|
|
*/
|
|
|
|
|
2024-02-24 07:47:12 +01:00
|
|
|
#if !defined(_WIN32) && !defined(_PTY_DISABLED)
|
|
|
|
|
|
|
|
#include "pty_unix.h"
|
2022-12-29 10:52:13 +01:00
|
|
|
#include <godot_cpp/variant/callable.hpp>
|
2021-07-02 19:27:34 +02:00
|
|
|
#include <uv.h>
|
|
|
|
|
2021-07-01 17:21:44 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
2021-07-01 17:25:13 +02:00
|
|
|
#include <string.h>
|
2021-07-01 17:21:44 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
2021-07-01 17:25:13 +02:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
2021-07-01 17:21:44 +02:00
|
|
|
|
|
|
|
/* forkpty */
|
|
|
|
/* http://www.gnu.org/software/gnulib/manual/html_node/forkpty.html */
|
|
|
|
#if defined(__GLIBC__) || defined(__CYGWIN__)
|
|
|
|
#include <pty.h>
|
|
|
|
#elif defined(__APPLE__) || defined(__OpenBSD__) || defined(__NetBSD__)
|
|
|
|
#include <util.h>
|
|
|
|
#elif defined(__FreeBSD__)
|
|
|
|
#include <libutil.h>
|
|
|
|
#elif defined(__sun)
|
|
|
|
#include <stropts.h> /* for I_PUSH */
|
|
|
|
#else
|
|
|
|
#include <pty.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <termios.h> /* tcgetattr, tty_ioctl */
|
|
|
|
|
|
|
|
/* Some platforms name VWERASE and VDISCARD differently */
|
|
|
|
#if !defined(VWERASE) && defined(VWERSE)
|
2021-07-01 17:25:13 +02:00
|
|
|
#define VWERASE VWERSE
|
2021-07-01 17:21:44 +02:00
|
|
|
#endif
|
|
|
|
#if !defined(VDISCARD) && defined(VDISCRD)
|
2021-07-01 17:25:13 +02:00
|
|
|
#define VDISCARD VDISCRD
|
2021-07-01 17:21:44 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* environ for execvpe */
|
|
|
|
/* node/src/node_child_process.cc */
|
|
|
|
#if defined(__APPLE__) && !TARGET_OS_IPHONE
|
|
|
|
#include <crt_externs.h>
|
|
|
|
#define environ (*_NSGetEnviron())
|
|
|
|
#else
|
|
|
|
extern char **environ;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* for pty_getproc */
|
|
|
|
#if defined(__linux__)
|
|
|
|
#include <stdint.h>
|
2021-07-01 17:25:13 +02:00
|
|
|
#include <stdio.h>
|
2021-07-01 17:21:44 +02:00
|
|
|
#elif defined(__APPLE__)
|
|
|
|
#include <libproc.h>
|
2021-07-01 17:25:13 +02:00
|
|
|
#include <sys/sysctl.h>
|
2021-07-01 17:21:44 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* NSIG - macro for highest signal + 1, should be defined */
|
|
|
|
#ifndef NSIG
|
|
|
|
#define NSIG 32
|
|
|
|
#endif
|
|
|
|
|
2021-07-02 19:27:34 +02:00
|
|
|
using namespace godot;
|
|
|
|
|
2021-07-01 17:21:44 +02:00
|
|
|
/**
|
|
|
|
* Structs
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct pty_baton {
|
2022-12-29 10:52:13 +01:00
|
|
|
Callable cb;
|
2021-07-01 17:21:44 +02:00
|
|
|
int exit_code;
|
|
|
|
int signal_code;
|
|
|
|
pid_t pid;
|
|
|
|
uv_async_t async;
|
|
|
|
uv_thread_t tid;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Functions
|
|
|
|
*/
|
|
|
|
|
2021-07-01 17:25:13 +02:00
|
|
|
static int pty_execvpe(const char *, char **, char **);
|
2021-07-01 17:21:44 +02:00
|
|
|
|
2021-07-01 17:25:13 +02:00
|
|
|
static int pty_nonblock(int);
|
2021-07-01 17:21:44 +02:00
|
|
|
|
2021-07-01 17:25:13 +02:00
|
|
|
static char *pty_getproc(int, char *);
|
2021-07-01 17:21:44 +02:00
|
|
|
|
2021-07-01 17:25:13 +02:00
|
|
|
static int pty_openpty(int *, int *, char *, const struct termios *,
|
|
|
|
const struct winsize *);
|
2021-07-01 17:21:44 +02:00
|
|
|
|
2021-07-01 17:25:13 +02:00
|
|
|
static pid_t pty_forkpty(int *, char *, const struct termios *,
|
|
|
|
const struct winsize *);
|
2021-07-01 17:21:44 +02:00
|
|
|
|
2021-07-01 17:25:13 +02:00
|
|
|
static void pty_waitpid(void *);
|
2021-07-01 17:21:44 +02:00
|
|
|
|
2021-07-01 17:25:13 +02:00
|
|
|
static void pty_after_waitpid(uv_async_t *);
|
2021-07-01 17:21:44 +02:00
|
|
|
|
2021-07-01 17:25:13 +02:00
|
|
|
static void pty_after_close(uv_handle_t *);
|
2021-07-01 17:21:44 +02:00
|
|
|
|
2022-12-29 10:52:13 +01:00
|
|
|
Array PTYUnix::fork(String p_file, int _ignored, PackedStringArray p_args,
|
2024-01-06 10:42:07 +01:00
|
|
|
PackedStringArray p_env, String p_cwd, int p_cols,
|
|
|
|
int p_rows, int p_uid, int p_gid, bool p_utf8,
|
|
|
|
Callable p_on_exit) {
|
2021-07-01 17:21:44 +02:00
|
|
|
// file
|
2022-12-29 10:52:13 +01:00
|
|
|
char *file = strdup(p_file.utf8().get_data());
|
2021-07-01 17:21:44 +02:00
|
|
|
|
|
|
|
// args
|
|
|
|
int i = 0;
|
2021-07-02 19:27:34 +02:00
|
|
|
int argc = p_args.size();
|
2021-07-01 17:21:44 +02:00
|
|
|
int argl = argc + 1 + 1;
|
2021-07-01 17:25:13 +02:00
|
|
|
char **argv = new char *[argl];
|
2021-07-02 19:27:34 +02:00
|
|
|
argv[0] = strdup(file);
|
2021-07-01 17:25:13 +02:00
|
|
|
argv[argl - 1] = NULL;
|
2021-07-01 17:21:44 +02:00
|
|
|
for (; i < argc; i++) {
|
2022-12-29 10:52:13 +01:00
|
|
|
char *arg = strdup(p_args[i].utf8().get_data());
|
2021-07-02 19:27:34 +02:00
|
|
|
argv[i + 1] = strdup(arg);
|
2021-07-01 17:21:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// env
|
|
|
|
i = 0;
|
2021-07-02 19:27:34 +02:00
|
|
|
int envc = p_env.size();
|
2021-07-01 17:25:13 +02:00
|
|
|
char **env = new char *[envc + 1];
|
2021-07-01 17:21:44 +02:00
|
|
|
env[envc] = NULL;
|
|
|
|
for (; i < envc; i++) {
|
2022-12-29 10:52:13 +01:00
|
|
|
char *pairs = strdup(p_env[i].utf8().get_data());
|
2021-07-02 19:27:34 +02:00
|
|
|
env[i] = strdup(pairs);
|
2021-07-01 17:21:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// cwd
|
2022-12-29 10:52:13 +01:00
|
|
|
char *cwd = strdup(p_cwd.utf8().get_data());
|
2021-07-01 17:21:44 +02:00
|
|
|
|
|
|
|
// size
|
|
|
|
struct winsize winp;
|
2021-07-02 19:27:34 +02:00
|
|
|
winp.ws_col = p_cols;
|
|
|
|
winp.ws_row = p_rows;
|
2021-07-01 17:21:44 +02:00
|
|
|
winp.ws_xpixel = 0;
|
|
|
|
winp.ws_ypixel = 0;
|
|
|
|
|
|
|
|
// termios
|
|
|
|
struct termios t = termios();
|
|
|
|
struct termios *term = &t;
|
|
|
|
term->c_iflag = ICRNL | IXON | IXANY | IMAXBEL | BRKINT;
|
2021-07-02 19:27:34 +02:00
|
|
|
if (p_utf8) {
|
2021-07-01 17:21:44 +02:00
|
|
|
#if defined(IUTF8)
|
|
|
|
term->c_iflag |= IUTF8;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
term->c_oflag = OPOST | ONLCR;
|
|
|
|
term->c_cflag = CREAD | CS8 | HUPCL;
|
2021-07-01 17:25:13 +02:00
|
|
|
term->c_lflag =
|
|
|
|
ICANON | ISIG | IEXTEN | ECHO | ECHOE | ECHOK | ECHOKE | ECHOCTL;
|
2021-07-01 17:21:44 +02:00
|
|
|
|
|
|
|
term->c_cc[VEOF] = 4;
|
|
|
|
term->c_cc[VEOL] = -1;
|
|
|
|
term->c_cc[VEOL2] = -1;
|
|
|
|
term->c_cc[VERASE] = 0x7f;
|
|
|
|
term->c_cc[VWERASE] = 23;
|
|
|
|
term->c_cc[VKILL] = 21;
|
|
|
|
term->c_cc[VREPRINT] = 18;
|
|
|
|
term->c_cc[VINTR] = 3;
|
|
|
|
term->c_cc[VQUIT] = 0x1c;
|
|
|
|
term->c_cc[VSUSP] = 26;
|
|
|
|
term->c_cc[VSTART] = 17;
|
|
|
|
term->c_cc[VSTOP] = 19;
|
|
|
|
term->c_cc[VLNEXT] = 22;
|
|
|
|
term->c_cc[VDISCARD] = 15;
|
|
|
|
term->c_cc[VMIN] = 1;
|
|
|
|
term->c_cc[VTIME] = 0;
|
|
|
|
|
2021-07-01 17:25:13 +02:00
|
|
|
#if (__APPLE__)
|
2021-07-01 17:21:44 +02:00
|
|
|
term->c_cc[VDSUSP] = 25;
|
|
|
|
term->c_cc[VSTATUS] = 20;
|
2021-07-01 17:25:13 +02:00
|
|
|
#endif
|
2021-07-01 17:21:44 +02:00
|
|
|
|
|
|
|
cfsetispeed(term, B38400);
|
|
|
|
cfsetospeed(term, B38400);
|
|
|
|
|
|
|
|
// uid / gid
|
2021-07-02 19:27:34 +02:00
|
|
|
int uid = p_uid;
|
|
|
|
int gid = p_gid;
|
2021-07-01 17:21:44 +02:00
|
|
|
|
|
|
|
// fork the pty
|
|
|
|
int master = -1;
|
|
|
|
|
|
|
|
sigset_t newmask, oldmask;
|
|
|
|
struct sigaction sig_action;
|
|
|
|
|
|
|
|
// temporarily block all signals
|
|
|
|
// this is needed due to a race condition in openpty
|
|
|
|
// and to avoid running signal handlers in the child
|
|
|
|
// before exec* happened
|
|
|
|
sigfillset(&newmask);
|
|
|
|
pthread_sigmask(SIG_SETMASK, &newmask, &oldmask);
|
|
|
|
|
|
|
|
pid_t pid = pty_forkpty(&master, nullptr, term, &winp);
|
|
|
|
|
|
|
|
if (!pid) {
|
|
|
|
// remove all signal handler from child
|
|
|
|
sig_action.sa_handler = SIG_DFL;
|
|
|
|
sig_action.sa_flags = 0;
|
|
|
|
sigemptyset(&sig_action.sa_mask);
|
2021-07-01 17:25:13 +02:00
|
|
|
for (int i = 0; i < NSIG; i++) { // NSIG is a macro for all signals + 1
|
2021-07-01 17:21:44 +02:00
|
|
|
sigaction(i, &sig_action, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// reenable signals
|
|
|
|
pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
|
|
|
|
|
|
|
|
if (pid) {
|
2021-07-01 17:25:13 +02:00
|
|
|
for (i = 0; i < argl; i++)
|
2021-07-02 19:27:34 +02:00
|
|
|
std::free(argv[i]);
|
2021-07-01 17:21:44 +02:00
|
|
|
delete[] argv;
|
2021-07-01 17:25:13 +02:00
|
|
|
for (i = 0; i < envc; i++)
|
2021-07-02 19:27:34 +02:00
|
|
|
std::free(env[i]);
|
2021-07-01 17:21:44 +02:00
|
|
|
delete[] env;
|
2021-07-02 19:27:34 +02:00
|
|
|
std::free(cwd);
|
2021-07-01 17:21:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (pid) {
|
2021-07-01 17:25:13 +02:00
|
|
|
case -1:
|
2021-07-02 19:27:34 +02:00
|
|
|
ERR_PRINT("forkpty(3) failed.");
|
2022-12-29 10:52:13 +01:00
|
|
|
return Array::make(FAILED);
|
2021-07-01 17:25:13 +02:00
|
|
|
case 0:
|
|
|
|
if (strlen(cwd)) {
|
|
|
|
if (chdir(cwd) == -1) {
|
|
|
|
perror("chdir(2) failed.");
|
|
|
|
_exit(1);
|
2021-07-01 17:21:44 +02:00
|
|
|
}
|
2021-07-01 17:25:13 +02:00
|
|
|
}
|
2021-07-01 17:21:44 +02:00
|
|
|
|
2021-07-01 17:25:13 +02:00
|
|
|
if (uid != -1 && gid != -1) {
|
|
|
|
if (setgid(gid) == -1) {
|
|
|
|
perror("setgid(2) failed.");
|
|
|
|
_exit(1);
|
2021-07-01 17:21:44 +02:00
|
|
|
}
|
2021-07-01 17:25:13 +02:00
|
|
|
if (setuid(uid) == -1) {
|
|
|
|
perror("setuid(2) failed.");
|
|
|
|
_exit(1);
|
|
|
|
}
|
|
|
|
}
|
2021-07-01 17:21:44 +02:00
|
|
|
|
2021-07-01 17:25:13 +02:00
|
|
|
pty_execvpe(argv[0], argv, env);
|
2021-07-01 17:21:44 +02:00
|
|
|
|
2021-07-01 17:25:13 +02:00
|
|
|
perror("execvp(3) failed.");
|
|
|
|
_exit(1);
|
|
|
|
default:
|
|
|
|
if (pty_nonblock(master) == -1) {
|
2021-07-02 19:27:34 +02:00
|
|
|
ERR_PRINT("Could not set master fd to nonblocking.");
|
2022-12-29 10:52:13 +01:00
|
|
|
return Array::make(FAILED);
|
2021-07-01 17:25:13 +02:00
|
|
|
}
|
|
|
|
|
2022-12-29 10:52:13 +01:00
|
|
|
Dictionary result;
|
2021-07-02 19:27:34 +02:00
|
|
|
result["fd"] = (int)master;
|
|
|
|
result["pid"] = (int)pid;
|
|
|
|
result["pty"] = ptsname(master);
|
2021-07-01 17:25:13 +02:00
|
|
|
|
|
|
|
pty_baton *baton = new pty_baton();
|
|
|
|
baton->exit_code = 0;
|
|
|
|
baton->signal_code = 0;
|
2021-07-02 19:27:34 +02:00
|
|
|
baton->cb = p_on_exit;
|
2021-07-01 17:25:13 +02:00
|
|
|
baton->pid = pid;
|
|
|
|
baton->async.data = baton;
|
|
|
|
|
|
|
|
uv_async_init(uv_default_loop(), &baton->async, pty_after_waitpid);
|
|
|
|
|
|
|
|
uv_thread_create(&baton->tid, pty_waitpid, static_cast<void *>(baton));
|
2021-07-01 17:21:44 +02:00
|
|
|
|
2022-12-29 10:52:13 +01:00
|
|
|
return Array::make(OK, result);
|
2021-07-01 17:21:44 +02:00
|
|
|
}
|
|
|
|
|
2022-12-29 10:52:13 +01:00
|
|
|
return Array::make(FAILED);
|
2021-07-01 17:21:44 +02:00
|
|
|
}
|
|
|
|
|
2021-07-02 19:27:34 +02:00
|
|
|
Array PTYUnix::open(int p_cols, int p_rows) {
|
2021-07-01 17:21:44 +02:00
|
|
|
// size
|
|
|
|
struct winsize winp;
|
2021-07-02 19:27:34 +02:00
|
|
|
winp.ws_col = p_cols;
|
|
|
|
winp.ws_row = p_rows;
|
2021-07-01 17:21:44 +02:00
|
|
|
winp.ws_xpixel = 0;
|
|
|
|
winp.ws_ypixel = 0;
|
|
|
|
|
|
|
|
// pty
|
|
|
|
int master, slave;
|
|
|
|
int ret = pty_openpty(&master, &slave, nullptr, NULL, &winp);
|
|
|
|
|
|
|
|
if (ret == -1) {
|
2021-07-02 19:27:34 +02:00
|
|
|
ERR_PRINT("openpty(3) failed.");
|
2022-12-29 10:52:13 +01:00
|
|
|
return Array::make(FAILED);
|
2021-07-01 17:21:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pty_nonblock(master) == -1) {
|
2021-07-02 19:27:34 +02:00
|
|
|
ERR_PRINT("Could not set master fd to nonblocking.");
|
2022-12-29 10:52:13 +01:00
|
|
|
return Array::make(FAILED);
|
2021-07-01 17:21:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pty_nonblock(slave) == -1) {
|
2021-07-02 19:27:34 +02:00
|
|
|
ERR_PRINT("Could not set slave fd to nonblocking.");
|
2022-12-29 10:52:13 +01:00
|
|
|
return Array::make(FAILED);
|
2021-07-01 17:21:44 +02:00
|
|
|
}
|
|
|
|
|
2022-12-29 10:52:13 +01:00
|
|
|
Dictionary dict;
|
2021-07-02 19:27:34 +02:00
|
|
|
dict["master"] = master;
|
|
|
|
dict["slave"] = slave;
|
|
|
|
dict["pty"] = ptsname(master);
|
2021-07-01 17:21:44 +02:00
|
|
|
|
2022-12-29 10:52:13 +01:00
|
|
|
return Array::make(OK, dict);
|
2021-07-01 17:21:44 +02:00
|
|
|
}
|
|
|
|
|
2022-12-29 10:52:13 +01:00
|
|
|
Error PTYUnix::resize(int p_fd, int p_cols, int p_rows) {
|
2021-07-02 19:27:34 +02:00
|
|
|
int fd = p_fd;
|
2021-07-01 17:21:44 +02:00
|
|
|
|
|
|
|
struct winsize winp;
|
2021-07-02 19:27:34 +02:00
|
|
|
winp.ws_col = p_cols;
|
|
|
|
winp.ws_row = p_rows;
|
2021-07-01 17:21:44 +02:00
|
|
|
winp.ws_xpixel = 0;
|
|
|
|
winp.ws_ypixel = 0;
|
|
|
|
|
|
|
|
if (ioctl(fd, TIOCSWINSZ, &winp) == -1) {
|
|
|
|
switch (errno) {
|
2024-02-24 07:47:12 +01:00
|
|
|
// TODO: Fixme!
|
|
|
|
//case EBADF:
|
|
|
|
// RETURN_UV_ERR(UV_EBADF)
|
|
|
|
//case EFAULT:
|
|
|
|
// RETURN_UV_ERR(UV_EFAULT)
|
|
|
|
//case EINVAL:
|
|
|
|
// RETURN_UV_ERR(UV_EINVAL);
|
|
|
|
//case ENOTTY:
|
|
|
|
// RETURN_UV_ERR(UV_ENOTTY);
|
2021-07-01 17:21:44 +02:00
|
|
|
}
|
2021-07-02 19:27:34 +02:00
|
|
|
ERR_PRINT("ioctl(2) failed");
|
2022-12-29 10:52:13 +01:00
|
|
|
return FAILED;
|
2021-07-01 17:21:44 +02:00
|
|
|
}
|
|
|
|
|
2022-12-29 10:52:13 +01:00
|
|
|
return OK;
|
2021-07-01 17:21:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Foreground Process Name
|
|
|
|
*/
|
2021-07-02 19:27:34 +02:00
|
|
|
String PTYUnix::process(int p_fd, String p_tty) {
|
|
|
|
int fd = p_fd;
|
2021-07-01 17:21:44 +02:00
|
|
|
|
2022-12-29 10:52:13 +01:00
|
|
|
char *tty = strdup(p_tty.utf8().get_data());
|
2021-07-01 17:21:44 +02:00
|
|
|
char *name = pty_getproc(fd, tty);
|
2021-07-02 19:27:34 +02:00
|
|
|
std::free(tty);
|
2021-07-01 17:21:44 +02:00
|
|
|
|
|
|
|
if (name == NULL) {
|
2021-07-02 19:27:34 +02:00
|
|
|
return "";
|
2021-07-01 17:21:44 +02:00
|
|
|
}
|
|
|
|
|
2021-07-02 19:27:34 +02:00
|
|
|
String name_ = String(name);
|
|
|
|
std::free(name);
|
|
|
|
return name_;
|
2021-07-01 17:21:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* execvpe
|
|
|
|
*/
|
|
|
|
|
|
|
|
// execvpe(3) is not portable.
|
|
|
|
// http://www.gnu.org/software/gnulib/manual/html_node/execvpe.html
|
2021-07-01 17:25:13 +02:00
|
|
|
static int pty_execvpe(const char *file, char **argv, char **envp) {
|
2021-07-01 17:21:44 +02:00
|
|
|
char **old = environ;
|
|
|
|
environ = envp;
|
|
|
|
int ret = execvp(file, argv);
|
|
|
|
environ = old;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Nonblocking FD
|
|
|
|
*/
|
|
|
|
|
2021-07-01 17:25:13 +02:00
|
|
|
static int pty_nonblock(int fd) {
|
2021-07-01 17:21:44 +02:00
|
|
|
int flags = fcntl(fd, F_GETFL, 0);
|
2021-07-01 17:25:13 +02:00
|
|
|
if (flags == -1)
|
|
|
|
return -1;
|
2021-07-01 17:21:44 +02:00
|
|
|
return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* pty_waitpid
|
|
|
|
* Wait for SIGCHLD to read exit status.
|
|
|
|
*/
|
|
|
|
|
2021-07-01 17:25:13 +02:00
|
|
|
static void pty_waitpid(void *data) {
|
2021-07-01 17:21:44 +02:00
|
|
|
int ret;
|
|
|
|
int stat_loc;
|
|
|
|
|
2021-07-01 17:25:13 +02:00
|
|
|
pty_baton *baton = static_cast<pty_baton *>(data);
|
2021-07-01 17:21:44 +02:00
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
|
|
|
|
if ((ret = waitpid(baton->pid, &stat_loc, 0)) != baton->pid) {
|
|
|
|
if (ret == -1 && errno == EINTR) {
|
|
|
|
return pty_waitpid(baton);
|
|
|
|
}
|
|
|
|
if (ret == -1 && errno == ECHILD) {
|
|
|
|
// XXX node v0.8.x seems to have this problem.
|
|
|
|
// waitpid is already handled elsewhere.
|
|
|
|
;
|
|
|
|
} else {
|
2021-07-02 19:27:34 +02:00
|
|
|
// assert(false);
|
2021-07-01 17:21:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (WIFEXITED(stat_loc)) {
|
|
|
|
baton->exit_code = WEXITSTATUS(stat_loc); // errno?
|
|
|
|
}
|
|
|
|
|
|
|
|
if (WIFSIGNALED(stat_loc)) {
|
|
|
|
baton->signal_code = WTERMSIG(stat_loc);
|
|
|
|
}
|
|
|
|
|
|
|
|
uv_async_send(&baton->async);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* pty_after_waitpid
|
|
|
|
* Callback after exit status has been read.
|
|
|
|
*/
|
|
|
|
|
2021-07-01 17:25:13 +02:00
|
|
|
static void pty_after_waitpid(uv_async_t *async) {
|
|
|
|
pty_baton *baton = static_cast<pty_baton *>(async->data);
|
2021-07-01 17:21:44 +02:00
|
|
|
|
2021-07-02 19:27:34 +02:00
|
|
|
Array argv = Array::make(baton->exit_code, baton->signal_code);
|
2021-07-01 17:21:44 +02:00
|
|
|
|
2024-01-06 10:42:07 +01:00
|
|
|
if (baton->cb.is_valid()) {
|
2022-12-29 10:52:13 +01:00
|
|
|
baton->cb.callv(argv);
|
2024-01-06 10:42:07 +01:00
|
|
|
baton->cb = (Variant) nullptr;
|
2022-06-23 15:30:27 +02:00
|
|
|
}
|
2021-07-01 17:21:44 +02:00
|
|
|
|
|
|
|
uv_close((uv_handle_t *)async, pty_after_close);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* pty_after_close
|
|
|
|
* uv_close() callback - free handle data
|
|
|
|
*/
|
|
|
|
|
2021-07-01 17:25:13 +02:00
|
|
|
static void pty_after_close(uv_handle_t *handle) {
|
2021-07-01 17:21:44 +02:00
|
|
|
uv_async_t *async = (uv_async_t *)handle;
|
2021-07-01 17:25:13 +02:00
|
|
|
pty_baton *baton = static_cast<pty_baton *>(async->data);
|
2021-07-01 17:21:44 +02:00
|
|
|
delete baton;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* pty_getproc
|
|
|
|
* Taken from tmux.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Taken from: tmux (http://tmux.sourceforge.net/)
|
|
|
|
// Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
|
|
|
|
// Copyright (c) 2009 Joshua Elsasser <josh@elsasser.org>
|
|
|
|
// Copyright (c) 2009 Todd Carson <toc@daybefore.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
// IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
|
|
|
#if defined(__linux__)
|
|
|
|
|
2021-07-01 17:25:13 +02:00
|
|
|
static char *pty_getproc(int fd, char *tty) {
|
2021-07-01 17:21:44 +02:00
|
|
|
FILE *f;
|
|
|
|
char *path, *buf;
|
|
|
|
size_t len;
|
|
|
|
int ch;
|
|
|
|
pid_t pgrp;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if ((pgrp = tcgetpgrp(fd)) == -1) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = asprintf(&path, "/proc/%lld/cmdline", (long long)pgrp);
|
2021-07-01 17:25:13 +02:00
|
|
|
if (r == -1 || path == NULL)
|
|
|
|
return NULL;
|
2021-07-01 17:21:44 +02:00
|
|
|
|
|
|
|
if ((f = fopen(path, "r")) == NULL) {
|
|
|
|
free(path);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(path);
|
|
|
|
|
|
|
|
len = 0;
|
|
|
|
buf = NULL;
|
|
|
|
while ((ch = fgetc(f)) != EOF) {
|
2021-07-01 17:25:13 +02:00
|
|
|
if (ch == '\0')
|
|
|
|
break;
|
2021-07-01 17:21:44 +02:00
|
|
|
buf = (char *)realloc(buf, len + 2);
|
2021-07-01 17:25:13 +02:00
|
|
|
if (buf == NULL)
|
|
|
|
return NULL;
|
2021-07-01 17:21:44 +02:00
|
|
|
buf[len++] = ch;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buf != NULL) {
|
|
|
|
buf[len] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(__APPLE__)
|
|
|
|
|
2021-07-01 17:25:13 +02:00
|
|
|
static char *pty_getproc(int fd, char *tty) {
|
|
|
|
int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, 0};
|
2021-07-01 17:21:44 +02:00
|
|
|
size_t size;
|
|
|
|
struct kinfo_proc kp;
|
|
|
|
|
|
|
|
if ((mib[3] = tcgetpgrp(fd)) == -1) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = sizeof kp;
|
|
|
|
if (sysctl(mib, 4, &kp, &size, NULL, 0) == -1) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size != (sizeof kp) || *kp.kp_proc.p_comm == '\0') {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return strdup(kp.kp_proc.p_comm);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2021-07-01 17:25:13 +02:00
|
|
|
static char *pty_getproc(int fd, char *tty) { return NULL; }
|
2021-07-01 17:21:44 +02:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* openpty(3) / forkpty(3)
|
|
|
|
*/
|
|
|
|
|
2021-07-01 17:25:13 +02:00
|
|
|
static int pty_openpty(int *amaster, int *aslave, char *name,
|
|
|
|
const struct termios *termp,
|
|
|
|
const struct winsize *winp) {
|
2021-07-01 17:21:44 +02:00
|
|
|
#if defined(__sun)
|
|
|
|
char *slave_name;
|
|
|
|
int slave;
|
|
|
|
int master = open("/dev/ptmx", O_RDWR | O_NOCTTY);
|
2021-07-01 17:25:13 +02:00
|
|
|
if (master == -1)
|
|
|
|
return -1;
|
|
|
|
if (amaster)
|
|
|
|
*amaster = master;
|
2021-07-01 17:21:44 +02:00
|
|
|
|
2021-07-01 17:25:13 +02:00
|
|
|
if (grantpt(master) == -1)
|
|
|
|
goto err;
|
|
|
|
if (unlockpt(master) == -1)
|
|
|
|
goto err;
|
2021-07-01 17:21:44 +02:00
|
|
|
|
|
|
|
slave_name = ptsname(master);
|
2021-07-01 17:25:13 +02:00
|
|
|
if (slave_name == NULL)
|
|
|
|
goto err;
|
|
|
|
if (name)
|
|
|
|
strcpy(name, slave_name);
|
2021-07-01 17:21:44 +02:00
|
|
|
|
|
|
|
slave = open(slave_name, O_RDWR | O_NOCTTY);
|
2021-07-01 17:25:13 +02:00
|
|
|
if (slave == -1)
|
|
|
|
goto err;
|
|
|
|
if (aslave)
|
|
|
|
*aslave = slave;
|
2021-07-01 17:21:44 +02:00
|
|
|
|
|
|
|
ioctl(slave, I_PUSH, "ptem");
|
|
|
|
ioctl(slave, I_PUSH, "ldterm");
|
|
|
|
ioctl(slave, I_PUSH, "ttcompat");
|
|
|
|
|
2021-07-01 17:25:13 +02:00
|
|
|
if (termp)
|
|
|
|
tcsetattr(slave, TCSAFLUSH, termp);
|
|
|
|
if (winp)
|
|
|
|
ioctl(slave, TIOCSWINSZ, winp);
|
2021-07-01 17:21:44 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err:
|
|
|
|
close(master);
|
|
|
|
return -1;
|
|
|
|
#else
|
|
|
|
return openpty(amaster, aslave, name, (termios *)termp, (winsize *)winp);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-07-01 17:25:13 +02:00
|
|
|
static pid_t pty_forkpty(int *amaster, char *name, const struct termios *termp,
|
|
|
|
const struct winsize *winp) {
|
2021-07-01 17:21:44 +02:00
|
|
|
#if defined(__sun)
|
|
|
|
int master, slave;
|
|
|
|
|
|
|
|
int ret = pty_openpty(&master, &slave, name, termp, winp);
|
2021-07-01 17:25:13 +02:00
|
|
|
if (ret == -1)
|
|
|
|
return -1;
|
|
|
|
if (amaster)
|
|
|
|
*amaster = master;
|
2021-07-01 17:21:44 +02:00
|
|
|
|
|
|
|
pid_t pid = fork();
|
|
|
|
|
|
|
|
switch (pid) {
|
2021-07-01 17:25:13 +02:00
|
|
|
case -1: // error in fork, we are still in parent
|
|
|
|
close(master);
|
|
|
|
close(slave);
|
|
|
|
return -1;
|
|
|
|
case 0: // we are in the child process
|
|
|
|
close(master);
|
|
|
|
setsid();
|
2021-07-01 17:21:44 +02:00
|
|
|
|
|
|
|
#if defined(TIOCSCTTY)
|
2021-07-01 17:25:13 +02:00
|
|
|
// glibc does this
|
|
|
|
if (ioctl(slave, TIOCSCTTY, NULL) == -1) {
|
|
|
|
_exit(1);
|
|
|
|
}
|
2021-07-01 17:21:44 +02:00
|
|
|
#endif
|
|
|
|
|
2021-07-01 17:25:13 +02:00
|
|
|
dup2(slave, 0);
|
|
|
|
dup2(slave, 1);
|
|
|
|
dup2(slave, 2);
|
2021-07-01 17:21:44 +02:00
|
|
|
|
2021-07-01 17:25:13 +02:00
|
|
|
if (slave > 2)
|
2021-07-01 17:21:44 +02:00
|
|
|
close(slave);
|
2021-07-01 17:25:13 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
default: // we are in the parent process
|
|
|
|
close(slave);
|
|
|
|
return pid;
|
2021-07-01 17:21:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
#else
|
|
|
|
return forkpty(amaster, name, (termios *)termp, (winsize *)winp);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Init
|
|
|
|
*/
|
|
|
|
|
2022-12-29 10:52:13 +01:00
|
|
|
void PTYUnix::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("_init"), &PTYUnix::_init);
|
|
|
|
ClassDB::bind_method(D_METHOD("fork"), &PTYUnix::fork);
|
|
|
|
ClassDB::bind_method(D_METHOD("open"), &PTYUnix::open);
|
|
|
|
ClassDB::bind_method(D_METHOD("resize"), &PTYUnix::resize);
|
|
|
|
ClassDB::bind_method(D_METHOD("process"), &PTYUnix::process);
|
2021-07-01 17:21:44 +02:00
|
|
|
}
|
|
|
|
|
2022-12-29 10:52:13 +01:00
|
|
|
void PTYUnix::_init() {}
|
2024-02-24 07:47:12 +01:00
|
|
|
|
|
|
|
#endif
|