1

I have a server application that writes to a popen("myCommand", "w") file descriptor in a separate thread and if the command passed to popen() results in any output to stdout or stderr, the my application exits. However, this is only an issue when my server application was invoked via inetd, if I used ssh to launch the server, it does not have this issue.

Similarly, when my server application reads from a popen("myCommand2", "r") file descriptor in a separate thread and if the command passed to popen() results in any output to stderr (stdin is going to my pipe), the application exits. Again, this only occurs with inetd summoning, not ssh summoning.

WilliamKF
  • 41,123
  • 68
  • 193
  • 295
  • Inetd works by redirecting standard i/o of the launched server. It's possible you're mangling this. It would be helpful if you could specify how your server 'exits'. Does it crash? Leave an exit code? – Duck Oct 19 '09 at 15:51
  • I don't know how it exits, that's is part of the issue. I see that destructors are run however. I am unable to attach via gdb, so I don't know what is failing. I also get no error message that is visible to me. I do know via printfs inserted into the code that the call to write() or read() is where it fails. – WilliamKF Oct 20 '09 at 00:33

1 Answers1

0

you need to close all existed fds of the process before open the pipe, then do i/o redirection. that's because if inetd, the process runs as a daemon.

Test
  • 1,697
  • 1
  • 11
  • 10
  • Could you please elaborate? The server summoned via inetd is crashing when it reads or writes to the pipe provided by popen when that pipe writes to stderr which was not redirected in the call to popen. – WilliamKF Oct 19 '09 at 14:45
  • please first ensure you added full path name to the called command, e.g. per popen("myCommand", "w"), you'd write popen("/home/myCommand",...), otherwise popen() would fail so any read/write the pipe would cause segault etc, unless your called command is in the PATH. if not the case, please call daemon(0, 1) to simulate inetd, before call daemon(), you'd better call setsid() and filter SIGINT,SIGHUP,SIGQUIT,SIGPIPE,SIGTTIN,SIGTTOU and SIGTERM. after the simulation, you may found the cause. – Test Oct 20 '09 at 13:49
  • use dup2() if do redirection. – Test Oct 20 '09 at 13:52