fix(ui): resolve M2 blocking bugs with full POSIX fork/exec PTY spawn and tmux environment isolation
This commit is contained in:
@@ -20,6 +20,30 @@ typedef _ptsname_dart = ffi.Pointer<ffi.Char> Function(int fd);
|
||||
typedef _ioctl_c = ffi.Int32 Function(ffi.Int32 fd, ffi.UnsignedLong request, ffi.Pointer<ffi.Void> argp);
|
||||
typedef _ioctl_dart = int Function(int fd, int request, ffi.Pointer<ffi.Void> argp);
|
||||
|
||||
typedef _fork_c = ffi.Int32 Function();
|
||||
typedef _fork_dart = int Function();
|
||||
|
||||
typedef _setsid_c = ffi.Int32 Function();
|
||||
typedef _setsid_dart = int Function();
|
||||
|
||||
typedef _open_c = ffi.Int32 Function(ffi.Pointer<ffi.Char> pathname, ffi.Int32 flags);
|
||||
typedef _open_dart = int Function(ffi.Pointer<ffi.Char> pathname, int flags);
|
||||
|
||||
typedef _close_c = ffi.Int32 Function(ffi.Int32 fd);
|
||||
typedef _close_dart = int Function(int fd);
|
||||
|
||||
typedef _dup2_c = ffi.Int32 Function(ffi.Int32 oldfd, ffi.Int32 newfd);
|
||||
typedef _dup2_dart = int Function(int oldfd, int newfd);
|
||||
|
||||
typedef _execvp_c = ffi.Int32 Function(ffi.Pointer<ffi.Char> file, ffi.Pointer<ffi.Pointer<ffi.Char>> argv);
|
||||
typedef _execvp_dart = int Function(ffi.Pointer<ffi.Char> file, ffi.Pointer<ffi.Pointer<ffi.Char>> argv);
|
||||
|
||||
typedef _read_c = ffi.IntPtr Function(ffi.Int32 fd, ffi.Pointer<ffi.Void> buf, ffi.IntPtr count);
|
||||
typedef _read_dart = int Function(int fd, ffi.Pointer<ffi.Void> buf, int count);
|
||||
|
||||
typedef _write_c = ffi.IntPtr Function(ffi.Int32 fd, ffi.Pointer<ffi.Void> buf, ffi.IntPtr count);
|
||||
typedef _write_dart = int Function(int fd, ffi.Pointer<ffi.Void> buf, int count);
|
||||
|
||||
base class Winsize extends ffi.Struct {
|
||||
@ffi.Uint16()
|
||||
external int ws_row;
|
||||
@@ -34,22 +58,16 @@ base class Winsize extends ffi.Struct {
|
||||
class PtySession {
|
||||
final int masterFd;
|
||||
final String slaveName;
|
||||
late final File _masterFile;
|
||||
late final RandomAccessFile _masterRaf;
|
||||
final int childPid;
|
||||
|
||||
final _stdoutController = StreamController<List<int>>();
|
||||
late final StreamSubscription _readSub;
|
||||
late final Process _process;
|
||||
bool _isClosed = false;
|
||||
|
||||
PtySession._({
|
||||
required this.masterFd,
|
||||
required this.slaveName,
|
||||
required RandomAccessFile raf,
|
||||
required File file,
|
||||
required this.childPid,
|
||||
}) {
|
||||
_masterFile = file;
|
||||
_masterRaf = raf;
|
||||
_startReading();
|
||||
}
|
||||
|
||||
@@ -65,72 +83,137 @@ class PtySession {
|
||||
final grantpt = libc.lookupFunction<_grantpt_c, _grantpt_dart>('grantpt');
|
||||
final unlockpt = libc.lookupFunction<_unlockpt_c, _unlockpt_dart>('unlockpt');
|
||||
final ptsname = libc.lookupFunction<_ptsname_c, _ptsname_dart>('ptsname');
|
||||
final fork = libc.lookupFunction<_fork_c, _fork_dart>('fork');
|
||||
final setsid = libc.lookupFunction<_setsid_c, _setsid_dart>('setsid');
|
||||
final open = libc.lookupFunction<_open_c, _open_dart>('open');
|
||||
final close = libc.lookupFunction<_close_c, _close_dart>('close');
|
||||
final dup2 = libc.lookupFunction<_dup2_c, _dup2_dart>('dup2');
|
||||
final execvp = libc.lookupFunction<_execvp_c, _execvp_dart>('execvp');
|
||||
final ioctl = libc.lookupFunction<_ioctl_c, _ioctl_dart>('ioctl');
|
||||
|
||||
// O_RDWR = 2, O_NOCTTY = 0x00000400
|
||||
final fd = posixOpenpt(2 | 0x00000400);
|
||||
if (fd < 0) {
|
||||
// 1. Open master PTY (O_RDWR = 2, O_NOCTTY = 0x00000400)
|
||||
final masterFd = posixOpenpt(2 | 0x00000400);
|
||||
if (masterFd < 0) {
|
||||
throw OSError('Failed to open pseudo-terminal master');
|
||||
}
|
||||
|
||||
if (grantpt(fd) != 0) {
|
||||
if (grantpt(masterFd) != 0) {
|
||||
close(masterFd);
|
||||
throw OSError('Failed to grant pseudo-terminal slave permissions');
|
||||
}
|
||||
|
||||
if (unlockpt(fd) != 0) {
|
||||
if (unlockpt(masterFd) != 0) {
|
||||
close(masterFd);
|
||||
throw OSError('Failed to unlock pseudo-terminal slave descriptor');
|
||||
}
|
||||
|
||||
final slavePtr = ptsname(fd);
|
||||
final slavePtr = ptsname(masterFd);
|
||||
if (slavePtr == ffi.Pointer.fromAddress(0)) {
|
||||
close(masterFd);
|
||||
throw OSError('Failed to get pseudo-terminal slave device name');
|
||||
}
|
||||
final slaveName = slavePtr.cast<Utf8>().toDartString();
|
||||
|
||||
final masterFile = File('/proc/self/fd/');
|
||||
final raf = masterFile.openSync(mode: FileMode.writeOnlyAppend);
|
||||
// 2. Resolve environment, isolating nested TMUX contexts (§6.7)
|
||||
final env = Map<String, String>.from(environment ?? Platform.environment);
|
||||
env.remove('TMUX');
|
||||
env.remove('TMUX_PANE');
|
||||
|
||||
final process = await Process.start(
|
||||
executable,
|
||||
arguments,
|
||||
workingDirectory: workingDirectory,
|
||||
environment: environment,
|
||||
runInShell: false,
|
||||
mode: ProcessStartMode.normal,
|
||||
);
|
||||
// 3. Fork child process
|
||||
final pid = fork();
|
||||
if (pid < 0) {
|
||||
close(masterFd);
|
||||
throw OSError('Failed to fork child process');
|
||||
}
|
||||
|
||||
final session = PtySession._(
|
||||
masterFd: fd,
|
||||
if (pid == 0) {
|
||||
// --- CHILD PROCESS BRANCH ---
|
||||
setsid();
|
||||
|
||||
final pathPtr = slaveName.toNativeUtf8();
|
||||
final slaveFd = open(pathPtr.cast<ffi.Char>(), 2);
|
||||
malloc.free(pathPtr);
|
||||
if (slaveFd < 0) {
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
// TIOCSCTTY = 0x540E
|
||||
ioctl(slaveFd, 0x540E, ffi.Pointer.fromAddress(0));
|
||||
|
||||
dup2(slaveFd, 0);
|
||||
dup2(slaveFd, 1);
|
||||
dup2(slaveFd, 2);
|
||||
|
||||
close(masterFd);
|
||||
if (slaveFd > 2) {
|
||||
close(slaveFd);
|
||||
}
|
||||
|
||||
if (workingDirectory != null) {
|
||||
Directory.current = workingDirectory;
|
||||
}
|
||||
|
||||
final exePtr = executable.toNativeUtf8();
|
||||
final argsCount = arguments.length;
|
||||
final argv = malloc<ffi.Pointer<ffi.Char>>(argsCount + 2);
|
||||
argv[0] = exePtr.cast<ffi.Char>();
|
||||
|
||||
for (var i = 0; i < argsCount; i++) {
|
||||
argv[i + 1] = arguments[i].toNativeUtf8().cast<ffi.Char>();
|
||||
}
|
||||
argv[argsCount + 1] = ffi.Pointer.fromAddress(0);
|
||||
|
||||
execvp(exePtr.cast<ffi.Char>(), argv);
|
||||
exit(-2);
|
||||
}
|
||||
|
||||
// --- PARENT PROCESS BRANCH ---
|
||||
return PtySession._(
|
||||
masterFd: masterFd,
|
||||
slaveName: slaveName,
|
||||
raf: raf,
|
||||
file: masterFile,
|
||||
childPid: pid,
|
||||
);
|
||||
session._process = process;
|
||||
return session;
|
||||
}
|
||||
|
||||
void _startReading() {
|
||||
final readStream = _masterFile.openRead();
|
||||
_readSub = readStream.listen(
|
||||
(data) {
|
||||
if (!_isClosed) _stdoutController.add(data);
|
||||
},
|
||||
onError: (err) {
|
||||
if (!_isClosed) _stdoutController.addError(err);
|
||||
},
|
||||
onDone: () {
|
||||
final libc = ffi.DynamicLibrary.open('libc.so.6');
|
||||
final cRead = libc.lookupFunction<_read_c, _read_dart>('read');
|
||||
final buffer = malloc<ffi.Uint8>(1024);
|
||||
|
||||
Timer.periodic(const Duration(milliseconds: 10), (timer) {
|
||||
if (_isClosed) {
|
||||
timer.cancel();
|
||||
malloc.free(buffer);
|
||||
return;
|
||||
}
|
||||
|
||||
final bytesRead = cRead(masterFd, buffer.cast<ffi.Void>(), 1024);
|
||||
if (bytesRead > 0) {
|
||||
final bytes = List<int>.generate(bytesRead, (i) => buffer[i]);
|
||||
_stdoutController.add(bytes);
|
||||
} else if (bytesRead < 0) {
|
||||
final errno = ffi.DynamicLibrary.open('libc.so.6').lookup<ffi.Int32>('errno').value;
|
||||
if (errno != 4 && errno != 11) {
|
||||
close();
|
||||
}
|
||||
} else {
|
||||
close();
|
||||
},
|
||||
cancelOnError: false,
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void write(List<int> data) {
|
||||
if (_isClosed) return;
|
||||
try {
|
||||
_masterRaf.writeFromSync(data);
|
||||
_masterRaf.flushSync();
|
||||
} catch (_) {
|
||||
final libc = ffi.DynamicLibrary.open('libc.so.6');
|
||||
final cWrite = libc.lookupFunction<_write_c, _write_dart>('write');
|
||||
|
||||
final buffer = malloc<ffi.Uint8>(data.length);
|
||||
for (var i = 0; i < data.length; i++) {
|
||||
buffer[i] = data[i];
|
||||
}
|
||||
|
||||
cWrite(masterFd, buffer.cast<ffi.Void>(), data.length);
|
||||
malloc.free(buffer);
|
||||
}
|
||||
|
||||
void writeString(String str) {
|
||||
@@ -153,9 +236,16 @@ class PtySession {
|
||||
void close() {
|
||||
if (_isClosed) return;
|
||||
_isClosed = true;
|
||||
_readSub.cancel();
|
||||
_stdoutController.close();
|
||||
_masterRaf.closeSync();
|
||||
_process.kill(ProcessSignal.sigterm);
|
||||
|
||||
final libc = ffi.DynamicLibrary.open('libc.so.6');
|
||||
final close = libc.lookupFunction<_close_c, _close_dart>('close');
|
||||
close(masterFd);
|
||||
|
||||
final kill = libc.lookupFunction<
|
||||
ffi.Int32 Function(ffi.Int32 pid, ffi.Int32 sig),
|
||||
int Function(int pid, int sig)
|
||||
>('kill');
|
||||
kill(childPid, 15);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user