Skip to content

Commit d7dbcb6

Browse files
authored
Merge pull request microsoft#588 from microsoft/robo/fix_proc_name
fix: retrieving process name on macOS
2 parents b347326 + 9d3c1d9 commit d7dbcb6

File tree

4 files changed

+30
-17
lines changed

4 files changed

+30
-17
lines changed

src/native.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ interface IUnixNative {
2121
fork(file: string, args: string[], parsedEnv: string[], cwd: string, cols: number, rows: number, uid: number, gid: number, useUtf8: boolean, onExitCallback: (code: number, signal: number) => void): IUnixProcess;
2222
open(cols: number, rows: number): IUnixOpenProcess;
2323
process(fd: number, pty: string): string;
24+
process(pid: number): string;
2425
resize(fd: number, cols: number, rows: number): void;
2526
}
2627

src/unix/pty.cc

+19-17
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
#include <paths.h>
5757
#include <spawn.h>
5858
#include <sys/event.h>
59-
#include <sys/sysctl.h>
6059
#include <termios.h>
6160
#endif
6261

@@ -126,8 +125,13 @@ NAN_METHOD(PtyGetProc);
126125
static int
127126
pty_nonblock(int);
128127

128+
#if defined(__APPLE__)
129+
static char *
130+
pty_getproc(int);
131+
#else
129132
static char *
130133
pty_getproc(int, char *);
134+
#endif
131135

132136
static void
133137
pty_waitpid(void *);
@@ -455,6 +459,15 @@ NAN_METHOD(PtyResize) {
455459
NAN_METHOD(PtyGetProc) {
456460
Nan::HandleScope scope;
457461

462+
#if defined(__APPLE__)
463+
if (info.Length() != 1 ||
464+
!info[0]->IsNumber()) {
465+
return Nan::ThrowError("Usage: pty.process(pid)");
466+
}
467+
468+
int pid = info[0]->IntegerValue(Nan::GetCurrentContext()).FromJust();
469+
char *name = pty_getproc(pid);
470+
#else
458471
if (info.Length() != 2 ||
459472
!info[0]->IsNumber() ||
460473
!info[1]->IsString()) {
@@ -467,6 +480,7 @@ NAN_METHOD(PtyGetProc) {
467480
char *tty = strdup(*tty_);
468481
char *name = pty_getproc(fd, tty);
469482
free(tty);
483+
#endif
470484

471485
if (name == NULL) {
472486
return info.GetReturnValue().SetUndefined();
@@ -663,25 +677,13 @@ pty_getproc(int fd, char *tty) {
663677
#elif defined(__APPLE__)
664678

665679
static char *
666-
pty_getproc(int fd, char *tty) {
667-
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, 0 };
668-
size_t size;
669-
struct kinfo_proc kp;
670-
671-
if ((mib[3] = tcgetpgrp(fd)) == -1) {
672-
return NULL;
673-
}
674-
675-
size = sizeof kp;
676-
if (sysctl(mib, 4, &kp, &size, NULL, 0) == -1) {
677-
return NULL;
678-
}
679-
680-
if (size != (sizeof kp) || *kp.kp_proc.p_comm == '\0') {
680+
pty_getproc(int pid) {
681+
char pname[MAXCOMLEN + 1];
682+
if (!proc_name(pid, pname, sizeof(pname))) {
681683
return NULL;
682684
}
683685

684-
return strdup(kp.kp_proc.p_comm);
686+
return strdup(pname);
685687
}
686688

687689
#else

src/unixTerminal.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,12 @@ if (process.platform !== 'win32') {
287287
done();
288288
}
289289
});
290+
it('should return the name of the process', (done) => {
291+
const term = new UnixTerminal('/bin/echo');
292+
assert.strictEqual(term.process, '/bin/echo');
293+
term.on('exit', () => done());
294+
term.destroy();
295+
});
290296
it('should close on exec', (done) => {
291297
const data = `
292298
var pty = require('./lib/index');

src/unixTerminal.ts

+4
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,10 @@ export class UnixTerminal extends Terminal {
255255
* Gets the name of the process.
256256
*/
257257
public get process(): string {
258+
if (process.platform === 'darwin') {
259+
return pty.process(this._pid) || this._file;
260+
}
261+
258262
return pty.process(this._fd, this._pty) || this._file;
259263
}
260264

0 commit comments

Comments
 (0)