1111 *
1212 *
1313 * IDENTIFICATION
14- * $PostgreSQL: pgsql/src/interfaces/libpq/fe-secure.c,v 1.46 2004/08/17 04:24:23 tgl Exp $
14+ * $PostgreSQL: pgsql/src/interfaces/libpq/fe-secure.c,v 1.47 2004/08/17 16:54:47 momjian Exp $
1515 *
1616 * NOTES
1717 * The client *requires* a valid server certificate. Since
@@ -152,7 +152,8 @@ static SSL_CTX *SSL_context = NULL;
152152
153153#ifdef ENABLE_THREAD_SAFETY
154154static void sigpipe_handler_ignore_send (int signo );
155- pthread_key_t pq_thread_in_send = 0 ;
155+ pthread_key_t pq_thread_in_send = 0 ; /* initializer needed on Darwin */
156+ static pqsigfunc pq_pipe_handler ;
156157#endif
157158
158159/* ------------------------------------------------------------ */
@@ -1190,23 +1191,12 @@ PQgetssl(PGconn *conn)
11901191void
11911192pq_check_sigpipe_handler (void )
11921193{
1193- pqsigfunc pipehandler ;
1194-
1194+ pthread_key_create (& pq_thread_in_send , NULL );
11951195 /*
1196- * If the app hasn't set a SIGPIPE handler, define our own
1197- * that ignores SIGPIPE on libpq send() and does SIG_DFL
1198- * for other SIGPIPE cases.
1196+ * Find current pipe handler and chain on to it.
11991197 */
1200- pipehandler = pqsignalinquire (SIGPIPE );
1201- if (pipehandler == SIG_DFL ) /* not set by application */
1202- {
1203- /*
1204- * Create key first because the signal handler might be called
1205- * right after being installed.
1206- */
1207- pthread_key_create (& pq_thread_in_send , NULL );
1208- pqsignal (SIGPIPE , sigpipe_handler_ignore_send );
1209- }
1198+ pq_pipe_handler = pqsignalinquire (SIGPIPE );
1199+ pqsignal (SIGPIPE , sigpipe_handler_ignore_send );
12101200}
12111201
12121202/*
@@ -1216,12 +1206,18 @@ void
12161206sigpipe_handler_ignore_send (int signo )
12171207{
12181208 /*
1219- * If we have gotten a SIGPIPE outside send(), exit.
1220- * Synchronous signals are delivered to the thread
1221- * that caused the signal.
1209+ * If we have gotten a SIGPIPE outside send(), chain or
1210+ * exit if we are at the end of the chain.
1211+ * Synchronous signals are delivered to the thread that
1212+ * caused the signal.
12221213 */
12231214 if (!PQinSend ())
1224- exit (128 + SIGPIPE ); /* typical return value for SIG_DFL */
1215+ {
1216+ if (pq_pipe_handler == SIG_DFL ) /* not set by application */
1217+ exit (128 + SIGPIPE ); /* typical return value for SIG_DFL */
1218+ else
1219+ (* pq_pipe_handler )(signo ); /* call original handler */
1220+ }
12251221}
12261222#endif
12271223#endif
0 commit comments