简体   繁体   English

可以写入数据报套接字会引发SIGPIPE吗?

[英]Can writes to a datagram socket ever raise SIGPIPE?

I'm working with some code that needs to be safe against killing the caller due to SIGPIPE , but the only socket writes it's performing are going to datagram sockets (both UDP and Unix domain datagram sockets). 我正在使用一些代码,这些代码需要安全防止由于SIGPIPE而导致调用者被杀,但唯一的套接字写入它正在执行的是数据报套接字(UDP和Unix域数据报套接字)。 Do I need to worry about SIGPIPE ? 我需要担心SIGPIPE吗? I'm using connect on the socket, but preliminary testing (on Linux) showed that I just get ECONNREFUSED on send if there's nobody listening on the Unix domain socket. 我在套接字上使用connect ,但初步测试(在Linux上)表明如果没有人在Unix域套接字上监听,我只会在发送时获得ECONNREFUSED Not sure what happens with UDP. 不确定UDP会发生什么。

I can wrap the whole thing in hacks to get rid of SIGPIPE , but if it's a non-issue I'd rather save the overhead and keep the code complexity down. 我可以将整个东西包装在黑客中以摆脱SIGPIPE ,但如果它不是问题,我宁愿节省开销并保持代码复杂性。

The open group is one thing, and Apple is another. 开放组是一回事,Apple是另一回事。 It is definitely possible to get a SIGPIPE on iOS when writing to a dead UDP socket, as some of my crash logs revealed lately. 当写入死UDP套接字时,绝对有可能在iOS上获得一个SIGPIPE,因为我最近发现了一些崩溃日志。 iOS tends to close UDP sockets while the app is in the background, writing to these sockets can pop a SIGPIPE. iOS倾向于在应用程序处于后台时关闭UDP套接字,写入这些套接字可以弹出SIGPIPE。
From my crash log (courtesy of testflightapp): 从我的崩溃日志(由testflightapp提供):

Exception Latest Victim Occurrences 最新受害者发生的例外情况
SIGPIPE SIGPIPE
2 libsystem_c.dylib 0x32df47ec _sigtramp + 48 2 libsystem_c.dylib 0x32df47ec _sigtramp + 48
3 instant talk 0x0005b10e -[IPRSNetDatagramSocket send:size:to:] (iprs_iphone_net.m:671)... 3即时通话0x0005b10e - [IPRSNetDatagramSocket发送:大小:到:](iprs_iphone_net.m:671)...

Don't recall this happening on Linux, Solaris or Windows - though I never tried to close a socket and then write to it. 不记得在Linux,Solaris或Windows上发生这种情况 - 尽管我从未试图关闭套接字然后写入它。

The answer is in the specification for send : 答案在send规范中:

[EPIPE] The socket is shut down for writing, or the socket is connection-mode and is no longer connected. [EPIPE]插座关闭以进行写入,或者插座处于连接模式且不再连接。 In the latter case, and if the socket is of type SOCK_STREAM or SOCK_SEQPACKET and the MSG_NOSIGNAL flag is not set, the SIGPIPE signal is generated to the calling thread. 在后一种情况下,如果套接字的类型为SOCK_STREAM或SOCK_SEQPACKET且未设置MSG_NOSIGNAL标志,则会向调用线程生成SIGPIPE信号。

http://pubs.opengroup.org/onlinepubs/9699919799/functions/send.html http://pubs.opengroup.org/onlinepubs/9699919799/functions/send.html

Thus, no, writes to datagram sockets do not generate SIGPIPE or an EPIPE error. 因此,不,写入数据报套接字不会生成SIGPIPEEPIPE错误。

According to man 2 write on my Debian box, 根据man 2 write在我的Debian框中man 2 write

EPIPE: fd is connected to a pipe or socket whose reading end is closed. EPIPE:fd连接到读取端关闭的管道或插座。 When this happens the writing process will also receive a SIGPIPE signal. 当发生这种情况时,写入过程也将收到SIGPIPE信号。 (Thus, the write return value is seen only if the program catches, blocks or ignores this signal.) (因此,仅当程序捕获,阻止或忽略此信号时才会看到写入返回值。)

It appears that it is possible to get SIGPIPE when writing to a socket, but it's not clear whether it can happen for UDP sockets specifically. 在写入套接字时似乎可以获得SIGPIPE,但是不清楚它是否可以专门用于UDP套接字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM