Skip to content

Commit dad7595

Browse files
authored
fix: sub process title on macOS (nodejs#616)
1 parent 822ce3d commit dad7595

File tree

4 files changed

+57
-9
lines changed

4 files changed

+57
-9
lines changed

src/native.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ interface IWinptyNative {
2121
interface IUnixNative {
2222
fork(file: string, args: string[], parsedEnv: string[], cwd: string, cols: number, rows: number, uid: number, gid: number, useUtf8: boolean, helperPath: string, onExitCallback: (code: number, signal: number) => void): IUnixProcess;
2323
open(cols: number, rows: number): IUnixOpenProcess;
24-
process(fd: number, pty: string): string;
25-
process(pid: number): string;
24+
process(fd: number, pty?: string): string;
2625
resize(fd: number, cols: number, rows: number): void;
2726
}
2827

src/unix/pty.cc

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#include <paths.h>
5959
#include <spawn.h>
6060
#include <sys/event.h>
61+
#include <sys/sysctl.h>
6162
#include <termios.h>
6263
#endif
6364

@@ -480,8 +481,8 @@ NAN_METHOD(PtyGetProc) {
480481
return Nan::ThrowError("Usage: pty.process(pid)");
481482
}
482483

483-
int pid = info[0]->IntegerValue(Nan::GetCurrentContext()).FromJust();
484-
char *name = pty_getproc(pid);
484+
int fd = info[0]->IntegerValue(Nan::GetCurrentContext()).FromJust();
485+
char *name = pty_getproc(fd);
485486
#else
486487
if (info.Length() != 2 ||
487488
!info[0]->IsNumber() ||
@@ -692,13 +693,25 @@ pty_getproc(int fd, char *tty) {
692693
#elif defined(__APPLE__)
693694

694695
static char *
695-
pty_getproc(int pid) {
696-
char pname[MAXCOMLEN + 1];
697-
if (!proc_name(pid, pname, sizeof(pname))) {
696+
pty_getproc(int fd) {
697+
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, 0 };
698+
size_t size;
699+
struct kinfo_proc kp;
700+
701+
if ((mib[3] = tcgetpgrp(fd)) == -1) {
702+
return NULL;
703+
}
704+
705+
size = sizeof kp;
706+
if (sysctl(mib, 4, &kp, &size, NULL, 0) == -1) {
707+
return NULL;
708+
}
709+
710+
if (size != (sizeof kp) || *kp.kp_proc.p_comm == '\0') {
698711
return NULL;
699712
}
700713

701-
return strdup(pname);
714+
return strdup(kp.kp_proc.p_comm);
702715
}
703716

704717
#else

src/unixTerminal.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import * as tty from 'tty';
1111
import * as fs from 'fs';
1212
import { constants } from 'os';
1313
import { pollUntil } from './testUtils.test';
14+
import { pid } from 'process';
1415

1516
const FIXTURES_PATH = path.normalize(path.join(__dirname, '..', 'fixtures', 'utf8-character.txt'));
1617

@@ -262,6 +263,40 @@ if (process.platform !== 'win32') {
262263
term.on('exit', () => done());
263264
term.destroy();
264265
});
266+
it('should return the name of the sub process', (done) => {
267+
const data = `
268+
var pty = require('./lib/index');
269+
var ptyProcess = pty.spawn('zsh', ['-c', 'python3'], {
270+
env: process.env
271+
});
272+
ptyProcess.on('data', function (data) {
273+
if (ptyProcess.process === 'Python') {
274+
console.log('title', ptyProcess.process);
275+
}
276+
});
277+
setTimeout(() => null, 500);
278+
console.log('ready', ptyProcess.pid);
279+
`;
280+
const p = cp.spawn('node', ['-e', data]);
281+
let sub = '';
282+
let pid = '';
283+
p.stdout.on('data', (data) => {
284+
if (!data.toString().indexOf('title')) {
285+
sub = data.toString().split(' ')[1].slice(0, -1);
286+
setTimeout(() => {
287+
process.kill(parseInt(pid), 'SIGINT');
288+
p.kill('SIGINT');
289+
}, 200);
290+
} else if (!data.toString().indexOf('ready')) {
291+
pid = data.toString().split(' ')[1].slice(0, -1);
292+
}
293+
});
294+
p.on('exit', () => {
295+
assert.notStrictEqual(pid, '');
296+
assert.strictEqual(sub, 'Python');
297+
done();
298+
});
299+
});
265300
it('should close on exec', (done) => {
266301
const data = `
267302
var pty = require('./lib/index');

src/unixTerminal.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,8 @@ export class UnixTerminal extends Terminal {
263263
*/
264264
public get process(): string {
265265
if (process.platform === 'darwin') {
266-
return pty.process(this._pid) || this._file;
266+
const title = pty.process(this._fd);
267+
return (title !== 'kernel_task' ) ? title : this._file;
267268
}
268269

269270
return pty.process(this._fd, this._pty) || this._file;

0 commit comments

Comments
 (0)