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

fix: retrieving process name on macOS #588

Merged
merged 1 commit into from
Mar 31, 2023
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
1 change: 1 addition & 0 deletions src/native.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ interface IUnixNative {
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;
open(cols: number, rows: number): IUnixOpenProcess;
process(fd: number, pty: string): string;
process(pid: number): string;
resize(fd: number, cols: number, rows: number): void;
}

Expand Down
36 changes: 19 additions & 17 deletions src/unix/pty.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
#include <paths.h>
#include <spawn.h>
#include <sys/event.h>
#include <sys/sysctl.h>
#include <termios.h>
#endif

Expand Down Expand Up @@ -126,8 +125,13 @@ NAN_METHOD(PtyGetProc);
static int
pty_nonblock(int);

#if defined(__APPLE__)
static char *
pty_getproc(int);
#else
static char *
pty_getproc(int, char *);
#endif

static void
pty_waitpid(void *);
Expand Down Expand Up @@ -455,6 +459,15 @@ NAN_METHOD(PtyResize) {
NAN_METHOD(PtyGetProc) {
Nan::HandleScope scope;

#if defined(__APPLE__)
if (info.Length() != 1 ||
!info[0]->IsNumber()) {
return Nan::ThrowError("Usage: pty.process(pid)");
}

int pid = info[0]->IntegerValue(Nan::GetCurrentContext()).FromJust();
char *name = pty_getproc(pid);
#else
if (info.Length() != 2 ||
!info[0]->IsNumber() ||
!info[1]->IsString()) {
Expand All @@ -467,6 +480,7 @@ NAN_METHOD(PtyGetProc) {
char *tty = strdup(*tty_);
char *name = pty_getproc(fd, tty);
free(tty);
#endif

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

static char *
pty_getproc(int fd, char *tty) {
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, 0 };
size_t size;
struct kinfo_proc kp;

if ((mib[3] = tcgetpgrp(fd)) == -1) {
return NULL;
}

size = sizeof kp;
if (sysctl(mib, 4, &kp, &size, NULL, 0) == -1) {
return NULL;
}

if (size != (sizeof kp) || *kp.kp_proc.p_comm == '\0') {
pty_getproc(int pid) {
char pname[MAXCOMLEN + 1];
if (!proc_name(pid, pname, sizeof(pname))) {
return NULL;
}

return strdup(kp.kp_proc.p_comm);
return strdup(pname);
Comment on lines +680 to +686
Copy link
Member

Choose a reason for hiding this comment

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

I think this is similar to tmux's technique, are we going to regress anywhere by doing it this way? I don't understand how it worked before.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I went based on the implementation of proc_name which is used in much widely across the kernel code https://github.com/apple/darwin-xnu/blob/2ff845c2e033bd0ff64b5b6aa6063a1f8f65aa32/bsd/kern/kern_proc.c#L1029-L1044 and it seems reliable.

}

#else
Expand Down
6 changes: 6 additions & 0 deletions src/unixTerminal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,12 @@ if (process.platform !== 'win32') {
done();
}
});
it('should return the name of the process', (done) => {
const term = new UnixTerminal('/bin/echo');
assert.strictEqual(term.process, '/bin/echo');
term.on('exit', () => done());
term.destroy();
});
it('should close on exec', (done) => {
const data = `
var pty = require('./lib/index');
Expand Down
4 changes: 4 additions & 0 deletions src/unixTerminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ export class UnixTerminal extends Terminal {
* Gets the name of the process.
*/
public get process(): string {
if (process.platform === 'darwin') {
return pty.process(this._pid) || this._file;
}

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

Expand Down