PostgreSQL Source Code git master
Loading...
Searching...
No Matches
oauth-utils.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * oauth-utils.c
4 *
5 * "Glue" helpers providing a copy of some internal APIs from libpq. At
6 * some point in the future, we might be able to deduplicate.
7 *
8 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
9 * Portions Copyright (c) 1994, Regents of the University of California
10 *
11 * IDENTIFICATION
12 * src/interfaces/libpq-oauth/oauth-utils.c
13 *
14 *-------------------------------------------------------------------------
15 */
16
17#include "postgres_fe.h"
18
19#include <signal.h>
20
21#include "oauth-utils.h"
22
23#ifndef USE_DYNAMIC_OAUTH
24#error oauth-utils.c is not supported in static builds
25#endif
26
27#ifdef LIBPQ_INT_H
28#error do not rely on libpq-int.h in dynamic builds of libpq-oauth
29#endif
30
31/*
32 * Function pointers set by libpq_oauth_init().
33 */
34
37
38/*-
39 * Initializes libpq-oauth by setting necessary callbacks.
40 *
41 * The current implementation relies on libpq_gettext to translate error
42 * messages using libpq's message domain, so libpq injects it here. We also use
43 * this chance to initialize our threadlock.
44 */
45void
51
52#ifdef ENABLE_NLS
53
54/*
55 * A shim that defers to the actual libpq_gettext().
56 */
57char *
58libpq_gettext(const char *msgid)
59{
61 {
62 /*
63 * Possible if the libpq build didn't enable NLS but the libpq-oauth
64 * build did. That's an odd mismatch, but we can handle it.
65 *
66 * Note that callers of libpq_gettext() have to treat the return value
67 * as if it were const, because builds without NLS simply pass through
68 * their argument.
69 */
70 return unconstify(char *, msgid);
71 }
72
74}
75
76#endif /* ENABLE_NLS */
77
78/*
79 * Duplicate SOCK_ERRNO* definitions from libpq-int.h, for use by
80 * pq_block/reset_sigpipe().
81 */
82#ifdef WIN32
83#define SOCK_ERRNO (WSAGetLastError())
84#define SOCK_ERRNO_SET(e) WSASetLastError(e)
85#else
86#define SOCK_ERRNO errno
87#define SOCK_ERRNO_SET(e) (errno = (e))
88#endif
89
90/*
91 * Block SIGPIPE for this thread. This is a copy of libpq's internal API.
92 */
93int
94pq_block_sigpipe(sigset_t *osigset, bool *sigpipe_pending)
95{
98
101
102 /* Block SIGPIPE and save previous mask for later reset */
104 if (SOCK_ERRNO)
105 return -1;
106
107 /* We can have a pending SIGPIPE only if it was blocked before */
109 {
110 /* Is there a pending SIGPIPE? */
111 if (sigpending(&sigset) != 0)
112 return -1;
113
115 *sigpipe_pending = true;
116 else
117 *sigpipe_pending = false;
118 }
119 else
120 *sigpipe_pending = false;
121
122 return 0;
123}
124
125/*
126 * Discard any pending SIGPIPE and reset the signal mask. This is a copy of
127 * libpq's internal API.
128 */
129void
130pq_reset_sigpipe(sigset_t *osigset, bool sigpipe_pending, bool got_epipe)
131{
133 int signo;
135
136 /* Clear SIGPIPE only if none was pending */
137 if (got_epipe && !sigpipe_pending)
138 {
139 if (sigpending(&sigset) == 0 &&
141 {
143
146
148 }
149 }
150
151 /* Restore saved block mask */
153
155}
#define unconstify(underlying_type, expr)
Definition c.h:1325
pgthreadlock_t PQgetThreadLock(void)
void(* pgthreadlock_t)(int acquire)
Definition libpq-fe.h:477
void pq_reset_sigpipe(sigset_t *osigset, bool sigpipe_pending, bool got_epipe)
void libpq_oauth_init(libpq_gettext_func gettext_impl)
Definition oauth-utils.c:46
#define SOCK_ERRNO
Definition oauth-utils.c:86
int pq_block_sigpipe(sigset_t *osigset, bool *sigpipe_pending)
Definition oauth-utils.c:94
static libpq_gettext_func libpq_gettext_impl
Definition oauth-utils.c:36
pgthreadlock_t pg_g_threadlock
Definition oauth-utils.c:35
#define SOCK_ERRNO_SET(e)
Definition oauth-utils.c:87
#define libpq_gettext(x)
Definition oauth-utils.h:44
char *(* libpq_gettext_func)(const char *msgid)
Definition oauth-utils.h:21
static int fb(int x)
#define SIGPIPE
Definition win32_port.h:163