Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3f0fa93

Browse files
committed
Chain on to SIGPIPE handler rather than just do action on default.
Always create thread-specific variable.
1 parent 0d4aa03 commit 3f0fa93

File tree

2 files changed

+19
-24
lines changed

2 files changed

+19
-24
lines changed

doc/src/sgml/libpq.sgml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.159 2004/08/16 02:12:29 tgl Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.160 2004/08/17 16:54:46 momjian Exp $
33
-->
44

55
<chapter id="libpq">
@@ -3738,8 +3738,7 @@ When <productname>PostgreSQL</> is configured without
37383738
<function>send()</> call and restores the original signal handler after
37393739
completion. When <literal>--enable-thread-safety</> is used,
37403740
<application>libpq</> installs its own <literal>SIGPIPE</> handler
3741-
before the first database connection if no custom <literal>SIGPIPE</>
3742-
handler has been installed previously. This handler uses thread-local
3741+
before the first database connection. This handler uses thread-local
37433742
storage to determine if a <literal>SIGPIPE</> signal has been generated
37443743
by a libpq <function>send()</>. If an application wants to install
37453744
its own <literal>SIGPIPE</> signal handler, it should call

src/interfaces/libpq/fe-secure.c

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
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
154154
static 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)
11901191
void
11911192
pq_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
12161206
sigpipe_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

Comments
 (0)