Search code examples
cserverforkparent-childsigpipe

How to terminate a process on SIGPIPE?


I have a server program that uses IPv4. The server has to attend multiple calls from client, so it creates a child process every time it accepts a client socket connection to deal with the client.

The child process is expected to read from client and then write to client socket. If it receives a SIGPIPE (from a bad client that closes before writing), it should terminate as the child's job is done. Do I explicitly declare a signal handler or does SIGPIPE terminate it by default? I am a beginner so please accept my ignorance if any.


Solution

  • From man 7 signal:

    The entries in the "Action" column of the table below specify the default
    disposition for each signal, as follows:
    
        Term   Default action is to terminate the process.
        [...]
    

    Then, later on in the table:

    Signal      Standard   Action   Comment
    ────────────────────────────────────────────────────────────────────────
    [...]
    SIGPIPE      P1990      Term    Broken pipe: write to pipe with no
                                    readers; see pipe(7)
    

    So you don't need to do anything, the default signal handler will terminate the process.