I have two applications running on Embedded Linux board. One runs as a daemon and other acts as an interface for it. They communicate with each other using Unix sockets. As to handle any abnormal termination of socket, I tried terminating the interface application [ctr+c]. But as a result, the daemon application crashes. Since the socket is terminated, I get the socket send failed error on daemon side, which is expected but after that the daemon crashes. I am at a loss as to where exactly should I look for debugging this problem.
Advertisement
Answer
Have you set the socket in your daemon to non-blocking mode ?
Suppose your code looks like the following:
while(1) { connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
/* then you use the fd */ func(connfd);
}
Based on the man page:
” On success, accept() return a nonnegative integer that is a descriptor for the accepted socket. On error, -1 is returned, and errno is set appropriately.
and
If no pending connections are present on the queue, and the socket is not marked as nonblocking, accept() blocks the caller until a connection is present. If the socket is marked nonblocking and no pending connections are present on the queue, accept() fails with the error EAGAIN or EWOULDBLOCK. “
Therefore, it means if you are in non-blocking mode, you should check the return value of accept() instead of using it directly because the fd value would be -1.
The above is just one common possibility. If it is not the case, you can try to use “sudo strace -p process_id” or carry out the core file analysis to understand why it is crashed.