문제

I have a parent process which listens for incoming connections on a socket and after accepting a client, It will pass the associated file descriptor and some other date to the child (through a named pipe).

From this moment on, child process will provide service to the client and the parent keeps listening for new clients.

My problem is that when I try to write on file descriptors in child process, I will get an error (Bad file descriptor) and write fails. My guess is that since these file descriptors are created after fork, child cannot simply use them and they only belong to the parent process.

So Is this the reason of write failure? Is there any possible way for child to use these file descriptors?


Solution
As it was mentioned in the answer by loreb, It is not possible to achieve the desired outcome in this manner. I did a temporary fix by using another pipe to direct the response from child to parent. Since file descriptors belong to parent, it can forward the response back to client through a socket.

도움이 되었습니까?

해결책

To pass a file descriptor you need to AF_UNIX sockets, not named pipes. You'll need sendmsg(3), which is insanely weird to use, so you'd better look up some example on the web -- eg io_passfd in libowfat.

Edit: in case anyone stumbles upon this answer, don't use an AF_UNIX client/server, use socketpair(2)

다른 팁

Maybe you should try closing and reopening of that FD.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top