Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed unix version of .kill function to send signals to all processes #169

Merged
merged 4 commits into from
Dec 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/unix/pty.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -706,6 +736,11 @@ NAN_MODULE_INIT(init) {
Nan::Set(target,
Nan::New<v8::String>("open").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(PtyOpen)->GetFunction());
#ifdef DEFINE_PTY_KILL
Nan::Set(target,
Nan::New<v8::String>("kill").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(PtyKill)->GetFunction());
#endif
Nan::Set(target,
Nan::New<v8::String>("resize").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(PtyResize)->GetFunction());
Expand Down
18 changes: 15 additions & 3 deletions src/unixTerminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
}
}

/**
Expand Down