I'm ok with my Go program being in the process table briefly; the goal was just to not include it in the process chain of the exec'd program. And the exit code handling is trivial, yes, but a bit undesirable to my mind: https://stackoverflow.com/a/10385867. Admittedly this is a matter of taste (and wanting to explore & understand `syscall` better) as much as anything.
I did get the inverted solution working: https://play.golang.org/p/l3ScCv1lHVw On Wednesday, September 30, 2020 at 6:56:28 PM UTC-4 Kurtis Rader wrote: > The way you describe the requirements there is no solution. Not in Go, C, > or any other language. You could try to invert the problem by creating a > pipe, forking your Go program, having the forked copy write to the pipe, > then exec the other program and have it read from the pipe. But that still > leaves your Go program in the process table; albeit under a new PID. I > don't understand why you can't do it in the straightforward fashion using > exec.Command. The "custom code to read and return the right exit code from > the child" is trivial unless you need to propagate the exit status that > occurs when the process terminates due to a signal. > > On Wed, Sep 30, 2020 at 3:33 PM Chris Tierney <christ...@gmail.com> wrote: > >> 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...@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 >> >> <https://groups.google.com/d/msgid/golang-nuts/0b245b98-81c7-4eb0-b3a7-61eca2db2a59n%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> > > > -- > Kurtis Rader > Caretaker of the exceptional canines Junior and Hank > -- 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/bcb52bb2-eae8-4e8b-aec0-e28ffab61c81n%40googlegroups.com.