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 3 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
33 changes: 33 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,27 @@ 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()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should have check the type of info[1]

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 +734,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
16 changes: 13 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,18 @@ 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 {
if (pty.kill && signal !== 'SIGHUP') {
Copy link
Member

@Tyriar Tyriar Dec 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A comment explaining why this is needed would be good here.

pty.kill(this._fd, os.constants.signals[signal]);
} else {
process.kill(this.pid, signal);
}
} catch (e) { /* swallow */ }
} else {
// Unknown signal
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's throw a new Error here to make known that nothing happened.

}
}

/**
Expand Down