diff --git a/src/unix/pty.cc b/src/unix/pty.cc index 57121b67..141fe1e2 100644 --- a/src/unix/pty.cc +++ b/src/unix/pty.cc @@ -93,6 +93,13 @@ NAN_METHOD(PtyOpen); NAN_METHOD(PtyResize); NAN_METHOD(PtyGetProc); +#if defined(TIOCSIG) || defined(TIOCSIGNAL) +#define DEFINE_PTY_KILL +NAN_METHOD(PtyKill); +#else +#warning "The function PtyKill will be unavailable because the ioctls TIOCSIG and TIOCSIGNAL don't exist" +#endif + /** * Functions */ @@ -347,6 +354,29 @@ NAN_METHOD(PtyOpen) { return info.GetReturnValue().Set(obj); } +#ifdef DEFINE_PTY_KILL +NAN_METHOD(PtyKill) { + Nan::HandleScope scope; + + if (info.Length() != 2 || + !info[0]->IsNumber() || + !info[1]->IsNumber()) { + return Nan::ThrowError("Usage: pty.kill(fd, signal)"); + } + + int fd = info[0]->IntegerValue(); + int signal = info[1]->IntegerValue(); + +#if defined(TIOCSIG) + if (ioctl(fd, TIOCSIG, signal) == -1) + return Nan::ThrowError("ioctl(2) failed."); +#elif defined(TIOCSIGNAL) + if (ioctl(fd, TIOCSIGNAL, signal) == -1) + return Nan::ThrowError("ioctl(2) failed."); +#endif +} +#endif + NAN_METHOD(PtyResize) { Nan::HandleScope scope; @@ -706,6 +736,11 @@ NAN_MODULE_INIT(init) { Nan::Set(target, Nan::New("open").ToLocalChecked(), Nan::New(PtyOpen)->GetFunction()); +#ifdef DEFINE_PTY_KILL + Nan::Set(target, + Nan::New("kill").ToLocalChecked(), + Nan::New(PtyKill)->GetFunction()); +#endif Nan::Set(target, Nan::New("resize").ToLocalChecked(), Nan::New(PtyResize)->GetFunction()); diff --git a/src/unixTerminal.ts b/src/unixTerminal.ts index 6763c17b..81d8f7c7 100644 --- a/src/unixTerminal.ts +++ b/src/unixTerminal.ts @@ -6,6 +6,7 @@ import * as net from 'net'; import * as path from 'path'; import * as tty from 'tty'; +import * as os from 'os'; import { Terminal, DEFAULT_COLS, DEFAULT_ROWS } from './terminal'; import { ProcessEnv, IPtyForkOptions, IPtyOpenOptions } from './interfaces'; import { ArgvOrCommandLine } from './types'; @@ -231,9 +232,20 @@ export class UnixTerminal extends Terminal { } public kill(signal?: string): void { - try { - process.kill(this.pid, signal || 'SIGHUP'); - } catch (e) { /* swallow */ } + signal = signal || 'SIGHUP'; + if (signal in os.constants.signals) { + try { + // pty.kill will not be available on systems which don't support + // the TIOCSIG/TIOCSIGNAL ioctl + if (pty.kill && signal !== 'SIGHUP') { + pty.kill(this._fd, os.constants.signals[signal]); + } else { + process.kill(this.pid, signal); + } + } catch (e) { /* swallow */ } + } else { + throw new Error('Unknown signal: ' + signal); + } } /**