linux - C++ & OpenSSL: SIGPIPE when writing in closed pipe -
i'm coding c++ ssl server tcp connections on linux. when program uses ssl_write()
write closed pipe, sigpipe-exception gets thrown causes program shut down. know normal behaviour. program should not die when peer not closes connection correctly.
i have googled lot , tried pretty found, seems nothing working me. signal(sigpipe,sig_ign)
not work - exception still gets thrown (same signal(sigpipe, somekindofhandler)
.
the gdb output:
program received signal sigpipe, broken pipe. 0x00007ffff6b23ccd in write () /lib/x86_64-linux-gnu/libpthread.so.0 (gdb) #0 0x00007ffff6b23ccd in write () /lib/x86_64-linux-gnu/libpthread.so.0 #1 0x00007ffff7883835 in ?? () /lib/x86_64-linux-gnu/libcrypto.so.1.0.0 #2 0x00007ffff7881687 in bio_write () /lib/x86_64-linux-gnu/libcrypto.so.1.0.0 #3 0x00007ffff7b9d3e0 in ?? () /lib/x86_64-linux-gnu/libssl.so.1.0.0 #4 0x00007ffff7b9db04 in ?? () /lib/x86_64-linux-gnu/libssl.so.1.0.0 #5 0x000000000042266a in netinterface::sendtosubscribers(bool) () @ ../bether/netinterface.h:181 #6 0x0000000000425834 in main () @ ../bether/main.cpp:111
about code:
i'm using thread waiting new connections , accepting them. thread puts connection information (bio & ssl) static map inside netinterface
class. every 5 seconds netinterface::sendtosubscribers()
executed main()
. function accesses static map , sends data every connection in there. function sigpipe comes from. have used signal(sigpipe,sig_ign)
in main()
(obviously before 5-seconds loop) , in netinterface::sendtosubscribers()
, not working anywhere.
thanks help!
you have call function sigaction change behavior either ignore sigpipe or handle in specific way own signal handler. please don't use function signal, it's obsolete.
http://man7.org/linux/man-pages/man2/sigaction.2.html
one way (i haven't compiled code should this):
void sigpipe_handler(int signal) { ... } int main() { struct sigaction sh; struct sigaction osh; sh.sa_handler = &sigpipe_handler; //can set sig_ign // restart interrupted system calls sh.sa_flags = sa_restart; // block every signal during handler sigemptyset(&sh.sa_mask); if (sigaction(sigpipe, &sh, &osh) < 0) { return -1; } ... }
if program multithreaded, little different have less control on thread receive signal. depends on type of signal. sigpipe, sent pthread generated signal. nevertheless, sigaction should work ok.
it possible set mask in main thread , subsequently created pthreads inherit signal mask. otherwise, signal mask can set in each thread.
sigset_t blockedsignal; sigemptyset(&blockedsignal); sigaddset(&blockedsignal, sigpipe); pthread_sigmask(sig_block, &blockedsignal, null);
however, if block signal, pending process , possible delivered. case, use sigtimedwait @ end of thread. sigaction set @ main thread or in thread generated sigpipe should work well.
Comments
Post a Comment