I'm looking for a way to exec a process (as in, `execve(2)`) and write 
something to its stdin.

Context: I am writing a tool that does some argument processing and then 
executes another (arbitrary) program, and passes the processed data to that 
other program's stdin. The processed data cannot be written to disk because 
of security constraints, hence using stdin. This does not need to be 
portable as it will only run on Linux.

I can accomplish this with an `exec.Command`, but I would prefer not to 
because:

* my go executable will still appear in the process chain, and ideally it 
would not (I would rather it be replaced by the new process)
* I need the exit code from executing my go tool to be the exit code of the 
other process that gets executed. With `exec.Command` I need to write 
custom code to read and return the right exit code from the child; with 
`execve` this would happen automatically.

So the next obvious choice is `syscall.Exec`. This solves the above 
problems, but there's a new problem: no way that I can see to send data to 
the new process's stdin. 

Exec Example: https://play.golang.org/p/HMY8QysTtE8 (Note: I'm using `awk` 
in this example, but that is just an example. My real code will execute 
other programs. In the playground you will not see the output from `awk` 
but if you run locally you will.)

So I tried `syscall.ForkExec`, which lets me use a pipe to write data to 
the new process. But the other program now has a different pid and the exit 
code is not returned. i.e., it has not replaced my go process---it's being 
exec'd in the forked process. 

ForkExec Example: https://play.golang.org/p/ZhoAljhUFGp (Note: I'm using 
`awk` in this example, but that is just an example. My real code will 
execute other programs. In the playground you will not see the output from 
`awk` but if you run locally you will.)

Is there a way to accomplish this in go? Normally what I'd do is `fork` a 
process that would handle writing to a pipe, `dup` the read end of the pipe 
to stdin and then `exec` to the process I want to run, but `syscall` 
doesn't have a `Fork`. I could probably use `import "C"` but at that point 
I would just rather write the tool in C.

Thanks for any suggestions!

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/0b245b98-81c7-4eb0-b3a7-61eca2db2a59n%40googlegroups.com.

Reply via email to