views:

197

answers:

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.

A: 

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.

EffoStaff Effo
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
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.
EffoStaff Effo
use dup2() if do redirection.
EffoStaff Effo