How to prevent SIGPIPEs (or handle them properly)

前端 未结 10 1773
名媛妹妹
名媛妹妹 2020-11-22 07:27

I have a small server program that accepts connections on a TCP or local UNIX socket, reads a simple command and (depending on the command) sends a reply.

The problem

10条回答
  •  清歌不尽
    2020-11-22 08:11

    Another method is to change the socket so it never generates SIGPIPE on write(). This is more convenient in libraries, where you might not want a global signal handler for SIGPIPE.

    On most BSD-based (MacOS, FreeBSD...) systems, (assuming you are using C/C++), you can do this with:

    int set = 1;
    setsockopt(sd, SOL_SOCKET, SO_NOSIGPIPE, (void *)&set, sizeof(int));
    

    With this in effect, instead of the SIGPIPE signal being generated, EPIPE will be returned.

提交回复
热议问题