Skip to content

Commit

Permalink
Redirect should now work
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Dec 27, 2020
1 parent 484b495 commit 393883b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
13 changes: 11 additions & 2 deletions pkg/proc/native/exec_darwin.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ spawn(char *argv0, char **argv, int size,
mach_port_t *port_set,
mach_port_t *exception_port,
mach_port_t *notification_port,
int disable_aslr) {
int disable_aslr,
int fd_stdin,
int fd_stdout,
int fd_stderr) {
kern_return_t kret = 0;

posix_spawn_file_actions_t file_actions;
Expand Down Expand Up @@ -55,9 +58,15 @@ spawn(char *argv0, char **argv, int size,

posix_spawnattr_setflags(&attributes, flags);

posix_spawn_file_actions_init(&file_actions);

posix_spawn_file_actions_adddup2(&file_actions, fd_stdin, STDIN_FILENO);
posix_spawn_file_actions_adddup2(&file_actions, fd_stdout, STDOUT_FILENO);
posix_spawn_file_actions_adddup2(&file_actions, fd_stderr, STDERR_FILENO);

pid_t pid;

posix_spawnp(&pid, argv0, NULL, &attributes,
posix_spawnp(&pid, argv0, &file_actions, &attributes,
argv,
environ);

Expand Down
2 changes: 1 addition & 1 deletion pkg/proc/native/exec_darwin.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
#include <signal.h>

int
spawn(char *, char **, int, char *, task_t*, mach_port_t*, mach_port_t*, mach_port_t*, int);
spawn(char *, char **, int, char *, task_t*, mach_port_t*, mach_port_t*, mach_port_t*, int, int, int, int);
2 changes: 1 addition & 1 deletion pkg/proc/native/proc_darwin.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ mach_port_wait(mach_port_t port_set, task_t *task, int nonblocking) {
// Propagate task back to Go
*task = msg->task.name;

// printf("Caught exception; exception_type (0x%02x): %s, code_count: 0x%02x, msg->code[0]: 0x%016llx, msg->code[1]: 0x%016llx\n", exception_type, exception_string(exception_type), code_count, msg->code[0], msg->code[1]);
printf("Caught exception; exception_type (0x%02x): %s, code_count: 0x%02x, msg->code[0]: 0x%016llx, msg->code[1]: 0x%016llx\n", exception_type, exception_string(exception_type), code_count, msg->code[0], msg->code[1]);

if (thread_suspend(msg->thread.name) != KERN_SUCCESS) return 0;

Expand Down
15 changes: 13 additions & 2 deletions pkg/proc/native/proc_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type osProcessDetails struct {
// Launch creates and begins debugging a new process. Uses a
// POSIX-style spawn in combination with PT_ATTACHEXC on Darwin
// which will turn Unix signals into Mach exceptions.
func Launch(cmd []string, wd string, flags proc.LaunchFlags, _ []string, _ string, _ [3]string) (*proc.Target, error) {
func Launch(cmd []string, wd string, flags proc.LaunchFlags, _ []string, _ string, redirects [3]string) (*proc.Target, error) {
argv0Go, err := filepath.Abs(cmd[0])
if err != nil {
return nil, err
Expand Down Expand Up @@ -67,6 +67,11 @@ func Launch(cmd []string, wd string, flags proc.LaunchFlags, _ []string, _ strin
disableASLR = 1
}

stdin, stdout, stderr, closefn, err := openRedirects(redirects, false)
if err != nil {
return nil, err
}

dbp := newProcess(0)
defer func() {
if err != nil && dbp.pid != 0 {
Expand All @@ -79,9 +84,15 @@ func Launch(cmd []string, wd string, flags proc.LaunchFlags, _ []string, _ strin
C.CString(wd),
&dbp.os.task, &dbp.os.portSet, &dbp.os.exceptionPort,
&dbp.os.notificationPort,
C.int(disableASLR))
C.int(disableASLR),
C.int(stdin.Fd()),
C.int(stdout.Fd()),
C.int(stderr.Fd()),
)
pid = int(ret)
})
closefn()

if pid <= 0 {
return nil, fmt.Errorf("could not spawn: %d", pid)
}
Expand Down

0 comments on commit 393883b

Please sign in to comment.