PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
libpq.h File Reference
#include <netinet/in.h>
#include "lib/stringinfo.h"
#include "libpq/libpq-be.h"
#include "storage/latch.h"
Include dependency graph for libpq.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  PQcommMethods
 

Macros

#define pq_comm_reset()   (PqCommMethods->comm_reset())
 
#define pq_flush()   (PqCommMethods->flush())
 
#define pq_flush_if_writable()   (PqCommMethods->flush_if_writable())
 
#define pq_is_send_pending()   (PqCommMethods->is_send_pending())
 
#define pq_putmessage(msgtype, s, len)   (PqCommMethods->putmessage(msgtype, s, len))
 
#define pq_putmessage_noblock(msgtype, s, len)   (PqCommMethods->putmessage_noblock(msgtype, s, len))
 
#define pq_startcopyout()   (PqCommMethods->startcopyout())
 
#define pq_endcopyout(errorAbort)   (PqCommMethods->endcopyout(errorAbort))
 

Functions

int StreamServerPort (int family, char *hostName, unsigned short portNumber, char *unixSocketDir, pgsocket ListenSocket[], int MaxListen)
 
int StreamConnection (pgsocket server_fd, Port *port)
 
void StreamClose (pgsocket sock)
 
void TouchSocketFiles (void)
 
void RemoveSocketFiles (void)
 
void pq_init (void)
 
int pq_getbytes (char *s, size_t len)
 
int pq_getstring (StringInfo s)
 
void pq_startmsgread (void)
 
void pq_endmsgread (void)
 
bool pq_is_reading_msg (void)
 
int pq_getmessage (StringInfo s, int maxlen)
 
int pq_getbyte (void)
 
int pq_peekbyte (void)
 
int pq_getbyte_if_available (unsigned char *c)
 
int pq_putbytes (const char *s, size_t len)
 
int secure_initialize (bool isServerStart)
 
bool secure_loaded_verify_locations (void)
 
void secure_destroy (void)
 
int secure_open_server (Port *port)
 
void secure_close (Port *port)
 
ssize_t secure_read (Port *port, void *ptr, size_t len)
 
ssize_t secure_write (Port *port, void *ptr, size_t len)
 
ssize_t secure_raw_read (Port *port, void *ptr, size_t len)
 
ssize_t secure_raw_write (Port *port, const void *ptr, size_t len)
 

Variables

PGDLLIMPORT PQcommMethodsPqCommMethods
 
char * ssl_cert_file
 
char * ssl_key_file
 
char * ssl_ca_file
 
char * ssl_crl_file
 
bool ssl_loaded_verify_locations
 
WaitEventSetFeBeWaitSet
 
char * SSLCipherSuites
 
char * SSLECDHCurve
 
bool SSLPreferServerCiphers
 

Macro Definition Documentation

#define pq_comm_reset ( )    (PqCommMethods->comm_reset())

Definition at line 38 of file libpq.h.

Referenced by PostgresMain().

#define pq_endcopyout (   errorAbort)    (PqCommMethods->endcopyout(errorAbort))

Definition at line 47 of file libpq.h.

Referenced by DoCopyTo(), errfinish(), SendCopyEnd(), and socket_comm_reset().

#define pq_flush_if_writable ( )    (PqCommMethods->flush_if_writable())

Definition at line 40 of file libpq.h.

Referenced by WalSndKeepaliveIfNecessary(), WalSndLoop(), WalSndWaitForWal(), and WalSndWriteData().

#define pq_is_send_pending ( )    (PqCommMethods->is_send_pending())

Definition at line 41 of file libpq.h.

Referenced by WalSndDone(), WalSndLoop(), WalSndWaitForWal(), and WalSndWriteData().

#define pq_putmessage (   msgtype,
  s,
  len 
)    (PqCommMethods->putmessage(msgtype, s, len))
#define pq_putmessage_noblock (   msgtype,
  s,
  len 
)    (PqCommMethods->putmessage_noblock(msgtype, s, len))

Definition at line 44 of file libpq.h.

Referenced by ProcessRepliesIfAny(), WalSndKeepalive(), WalSndWriteData(), and XLogSendPhysical().

#define pq_startcopyout ( )    (PqCommMethods->startcopyout())

Definition at line 46 of file libpq.h.

Referenced by SendCopyBegin().

Function Documentation

void pq_endmsgread ( void  )

Definition at line 1234 of file pqcomm.c.

References Assert, and PqCommReadingMsg.

Referenced by CopyFrom(), ProcessRepliesIfAny(), ProcessStartupPacket(), and SocketBackend().

1235 {
1237 
1238  PqCommReadingMsg = false;
1239 }
#define Assert(condition)
Definition: c.h:675
static bool PqCommReadingMsg
Definition: pqcomm.c:150
int pq_getbyte ( void  )

Definition at line 1000 of file pqcomm.c.

References Assert, pq_recvbuf(), PqCommReadingMsg, PqRecvBuffer, PqRecvLength, and PqRecvPointer.

Referenced by CheckSCRAMAuth(), CopyGetData(), recv_password_packet(), and SocketBackend().

1001 {
1003 
1004  while (PqRecvPointer >= PqRecvLength)
1005  {
1006  if (pq_recvbuf()) /* If nothing in buffer, then recv some */
1007  return EOF; /* Failed to recv data */
1008  }
1009  return (unsigned char) PqRecvBuffer[PqRecvPointer++];
1010 }
static char PqRecvBuffer[PQ_RECV_BUFFER_SIZE]
Definition: pqcomm.c:142
static int PqRecvLength
Definition: pqcomm.c:144
static int pq_recvbuf(void)
Definition: pqcomm.c:939
#define Assert(condition)
Definition: c.h:675
static int PqRecvPointer
Definition: pqcomm.c:143
static bool PqCommReadingMsg
Definition: pqcomm.c:150
int pq_getbyte_if_available ( unsigned char *  c)

Definition at line 1040 of file pqcomm.c.

References Assert, COMMERROR, EAGAIN, EINTR, ereport, errcode_for_socket_access(), errmsg(), EWOULDBLOCK, MyProcPort, PqCommReadingMsg, PqRecvBuffer, PqRecvLength, PqRecvPointer, secure_read(), and socket_set_nonblocking().

Referenced by ProcessRepliesIfAny().

1041 {
1042  int r;
1043 
1045 
1047  {
1049  return 1;
1050  }
1051 
1052  /* Put the socket into non-blocking mode */
1053  socket_set_nonblocking(true);
1054 
1055  r = secure_read(MyProcPort, c, 1);
1056  if (r < 0)
1057  {
1058  /*
1059  * Ok if no data available without blocking or interrupted (though
1060  * EINTR really shouldn't happen with a non-blocking socket). Report
1061  * other errors.
1062  */
1063  if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
1064  r = 0;
1065  else
1066  {
1067  /*
1068  * Careful: an ereport() that tries to write to the client would
1069  * cause recursion to here, leading to stack overflow and core
1070  * dump! This message must go *only* to the postmaster log.
1071  */
1074  errmsg("could not receive data from client: %m")));
1075  r = EOF;
1076  }
1077  }
1078  else if (r == 0)
1079  {
1080  /* EOF detected */
1081  r = EOF;
1082  }
1083 
1084  return r;
1085 }
#define EWOULDBLOCK
Definition: win32.h:291
static char PqRecvBuffer[PQ_RECV_BUFFER_SIZE]
Definition: pqcomm.c:142
struct Port * MyProcPort
Definition: globals.c:41
ssize_t secure_read(Port *port, void *ptr, size_t len)
Definition: be-secure.c:137
#define EAGAIN
Definition: win32.h:283
static void socket_set_nonblocking(bool nonblocking)
Definition: pqcomm.c:922
static int PqRecvLength
Definition: pqcomm.c:144
char * c
#define COMMERROR
Definition: elog.h:30
#define ereport(elevel, rest)
Definition: elog.h:122
int errcode_for_socket_access(void)
Definition: elog.c:669
#define EINTR
Definition: win32.h:285
#define Assert(condition)
Definition: c.h:675
int errmsg(const char *fmt,...)
Definition: elog.c:797
static int PqRecvPointer
Definition: pqcomm.c:143
static bool PqCommReadingMsg
Definition: pqcomm.c:150
int pq_getbytes ( char *  s,
size_t  len 
)

Definition at line 1094 of file pqcomm.c.

References Assert, pq_recvbuf(), PqCommReadingMsg, PqRecvBuffer, PqRecvLength, and PqRecvPointer.

Referenced by CopyGetData(), GetOldFunctionMessage(), pq_getmessage(), and ProcessStartupPacket().

1095 {
1096  size_t amount;
1097 
1099 
1100  while (len > 0)
1101  {
1102  while (PqRecvPointer >= PqRecvLength)
1103  {
1104  if (pq_recvbuf()) /* If nothing in buffer, then recv some */
1105  return EOF; /* Failed to recv data */
1106  }
1107  amount = PqRecvLength - PqRecvPointer;
1108  if (amount > len)
1109  amount = len;
1110  memcpy(s, PqRecvBuffer + PqRecvPointer, amount);
1111  PqRecvPointer += amount;
1112  s += amount;
1113  len -= amount;
1114  }
1115  return 0;
1116 }
static char PqRecvBuffer[PQ_RECV_BUFFER_SIZE]
Definition: pqcomm.c:142
static int PqRecvLength
Definition: pqcomm.c:144
static int pq_recvbuf(void)
Definition: pqcomm.c:939
#define Assert(condition)
Definition: c.h:675
static int PqRecvPointer
Definition: pqcomm.c:143
static bool PqCommReadingMsg
Definition: pqcomm.c:150
int pq_getmessage ( StringInfo  s,
int  maxlen 
)

Definition at line 1272 of file pqcomm.c.

References Assert, COMMERROR, StringInfoData::data, enlargeStringInfo(), ereport, errcode(), errmsg(), StringInfoData::len, PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, pq_discardbytes(), pq_getbytes(), PqCommReadingMsg, and resetStringInfo().

Referenced by CheckSCRAMAuth(), CopyGetData(), ProcessRepliesIfAny(), recv_password_packet(), and SocketBackend().

1273 {
1274  int32 len;
1275 
1277 
1278  resetStringInfo(s);
1279 
1280  /* Read message length word */
1281  if (pq_getbytes((char *) &len, 4) == EOF)
1282  {
1284  (errcode(ERRCODE_PROTOCOL_VIOLATION),
1285  errmsg("unexpected EOF within message length word")));
1286  return EOF;
1287  }
1288 
1289  len = ntohl(len);
1290 
1291  if (len < 4 ||
1292  (maxlen > 0 && len > maxlen))
1293  {
1295  (errcode(ERRCODE_PROTOCOL_VIOLATION),
1296  errmsg("invalid message length")));
1297  return EOF;
1298  }
1299 
1300  len -= 4; /* discount length itself */
1301 
1302  if (len > 0)
1303  {
1304  /*
1305  * Allocate space for message. If we run out of room (ridiculously
1306  * large message), we will elog(ERROR), but we want to discard the
1307  * message body so as not to lose communication sync.
1308  */
1309  PG_TRY();
1310  {
1311  enlargeStringInfo(s, len);
1312  }
1313  PG_CATCH();
1314  {
1315  if (pq_discardbytes(len) == EOF)
1317  (errcode(ERRCODE_PROTOCOL_VIOLATION),
1318  errmsg("incomplete message from client")));
1319 
1320  /* we discarded the rest of the message so we're back in sync. */
1321  PqCommReadingMsg = false;
1322  PG_RE_THROW();
1323  }
1324  PG_END_TRY();
1325 
1326  /* And grab the message */
1327  if (pq_getbytes(s->data, len) == EOF)
1328  {
1330  (errcode(ERRCODE_PROTOCOL_VIOLATION),
1331  errmsg("incomplete message from client")));
1332  return EOF;
1333  }
1334  s->len = len;
1335  /* Place a trailing null per StringInfo convention */
1336  s->data[len] = '\0';
1337  }
1338 
1339  /* finished reading the message. */
1340  PqCommReadingMsg = false;
1341 
1342  return 0;
1343 }
static int pq_discardbytes(size_t len)
Definition: pqcomm.c:1128
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:256
int pq_getbytes(char *s, size_t len)
Definition: pqcomm.c:1094
#define COMMERROR
Definition: elog.h:30
void enlargeStringInfo(StringInfo str, int needed)
Definition: stringinfo.c:245
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:62
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_CATCH()
Definition: elog.h:293
#define Assert(condition)
Definition: c.h:675
#define PG_RE_THROW()
Definition: elog.h:314
int errmsg(const char *fmt,...)
Definition: elog.c:797
static bool PqCommReadingMsg
Definition: pqcomm.c:150
#define PG_TRY()
Definition: elog.h:284
#define PG_END_TRY()
Definition: elog.h:300
int pq_getstring ( StringInfo  s)

Definition at line 1166 of file pqcomm.c.

References appendBinaryStringInfo(), Assert, i, pq_recvbuf(), PqCommReadingMsg, PqRecvBuffer, PqRecvLength, PqRecvPointer, and resetStringInfo().

Referenced by GetOldFunctionMessage(), and SocketBackend().

1167 {
1168  int i;
1169 
1171 
1172  resetStringInfo(s);
1173 
1174  /* Read until we get the terminating '\0' */
1175  for (;;)
1176  {
1177  while (PqRecvPointer >= PqRecvLength)
1178  {
1179  if (pq_recvbuf()) /* If nothing in buffer, then recv some */
1180  return EOF; /* Failed to recv data */
1181  }
1182 
1183  for (i = PqRecvPointer; i < PqRecvLength; i++)
1184  {
1185  if (PqRecvBuffer[i] == '\0')
1186  {
1187  /* include the '\0' in the copy */
1189  i - PqRecvPointer + 1);
1190  PqRecvPointer = i + 1; /* advance past \0 */
1191  return 0;
1192  }
1193  }
1194 
1195  /* If we're here we haven't got the \0 in the buffer yet. */
1197  PqRecvLength - PqRecvPointer);
1199  }
1200 }
static char PqRecvBuffer[PQ_RECV_BUFFER_SIZE]
Definition: pqcomm.c:142
static int PqRecvLength
Definition: pqcomm.c:144
static int pq_recvbuf(void)
Definition: pqcomm.c:939
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:62
#define Assert(condition)
Definition: c.h:675
int i
static int PqRecvPointer
Definition: pqcomm.c:143
static bool PqCommReadingMsg
Definition: pqcomm.c:150
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:208
void pq_init ( void  )

Definition at line 194 of file pqcomm.c.

References AddWaitEventToSet(), COMMERROR, CreateWaitEventSet(), DoingCopyOut, ereport, errmsg(), MemoryContextAlloc(), MyLatch, MyProcPort, NULL, on_proc_exit(), pg_set_noblock(), PQ_SEND_BUFFER_SIZE, PqCommBusy, PqCommReadingMsg, PqRecvLength, PqRecvPointer, PqSendBuffer, PqSendBufferSize, PqSendPointer, PqSendStart, Port::sock, socket_close(), TopMemoryContext, WL_LATCH_SET, WL_POSTMASTER_DEATH, and WL_SOCKET_WRITEABLE.

Referenced by BackendInitialize().

195 {
196  /* initialize state variables */
200  PqCommBusy = false;
201  PqCommReadingMsg = false;
202  DoingCopyOut = false;
203 
204  /* set up process-exit hook to close the socket */
206 
207  /*
208  * In backends (as soon as forked) we operate the underlying socket in
209  * nonblocking mode and use latches to implement blocking semantics if
210  * needed. That allows us to provide safely interruptible reads and
211  * writes.
212  *
213  * Use COMMERROR on failure, because ERROR would try to send the error to
214  * the client, which might require changing the mode again, leading to
215  * infinite recursion.
216  */
217 #ifndef WIN32
220  (errmsg("could not set socket to nonblocking mode: %m")));
221 #endif
222 
225  NULL, NULL);
228 }
#define WL_SOCKET_WRITEABLE
Definition: latch.h:126
static bool DoingCopyOut
Definition: pqcomm.c:151
struct Port * MyProcPort
Definition: globals.c:41
int AddWaitEventToSet(WaitEventSet *set, uint32 events, pgsocket fd, Latch *latch, void *user_data)
Definition: latch.c:664
void on_proc_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:292
static char * PqSendBuffer
Definition: pqcomm.c:137
static int PqSendStart
Definition: pqcomm.c:140
WaitEventSet * FeBeWaitSet
Definition: pqcomm.c:186
pgsocket sock
Definition: libpq-be.h:118
WaitEventSet * CreateWaitEventSet(MemoryContext context, int nevents)
Definition: latch.c:521
static int PqRecvLength
Definition: pqcomm.c:144
static int PqSendPointer
Definition: pqcomm.c:139
#define COMMERROR
Definition: elog.h:30
#define ereport(elevel, rest)
Definition: elog.h:122
MemoryContext TopMemoryContext
Definition: mcxt.c:43
static int PqSendBufferSize
Definition: pqcomm.c:138
static bool PqCommBusy
Definition: pqcomm.c:149
#define WL_POSTMASTER_DEATH
Definition: latch.h:128
#define NULL
Definition: c.h:229
bool pg_set_noblock(pgsocket sock)
Definition: noblock.c:25
static void socket_close(int code, Datum arg)
Definition: pqcomm.c:256
int errmsg(const char *fmt,...)
Definition: elog.c:797
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:707
struct Latch * MyLatch
Definition: globals.c:52
static int PqRecvPointer
Definition: pqcomm.c:143
static bool PqCommReadingMsg
Definition: pqcomm.c:150
#define WL_LATCH_SET
Definition: latch.h:124
#define PQ_SEND_BUFFER_SIZE
Definition: pqcomm.c:134
bool pq_is_reading_msg ( void  )

Definition at line 1250 of file pqcomm.c.

References PqCommReadingMsg.

Referenced by PostgresMain().

1251 {
1252  return PqCommReadingMsg;
1253 }
static bool PqCommReadingMsg
Definition: pqcomm.c:150
int pq_peekbyte ( void  )

Definition at line 1019 of file pqcomm.c.

References Assert, pq_recvbuf(), PqCommReadingMsg, PqRecvBuffer, PqRecvLength, and PqRecvPointer.

Referenced by recv_password_packet().

1020 {
1022 
1023  while (PqRecvPointer >= PqRecvLength)
1024  {
1025  if (pq_recvbuf()) /* If nothing in buffer, then recv some */
1026  return EOF; /* Failed to recv data */
1027  }
1028  return (unsigned char) PqRecvBuffer[PqRecvPointer];
1029 }
static char PqRecvBuffer[PQ_RECV_BUFFER_SIZE]
Definition: pqcomm.c:142
static int PqRecvLength
Definition: pqcomm.c:144
static int pq_recvbuf(void)
Definition: pqcomm.c:939
#define Assert(condition)
Definition: c.h:675
static int PqRecvPointer
Definition: pqcomm.c:143
static bool PqCommReadingMsg
Definition: pqcomm.c:150
int pq_putbytes ( const char *  s,
size_t  len 
)

Definition at line 1353 of file pqcomm.c.

References Assert, DoingCopyOut, internal_putbytes(), and PqCommBusy.

Referenced by CopySendEndOfRow(), and socket_endcopyout().

1354 {
1355  int res;
1356 
1357  /* Should only be called by old-style COPY OUT */
1359  /* No-op if reentrant call */
1360  if (PqCommBusy)
1361  return 0;
1362  PqCommBusy = true;
1363  res = internal_putbytes(s, len);
1364  PqCommBusy = false;
1365  return res;
1366 }
static bool DoingCopyOut
Definition: pqcomm.c:151
static int internal_putbytes(const char *s, size_t len)
Definition: pqcomm.c:1369
static bool PqCommBusy
Definition: pqcomm.c:149
#define Assert(condition)
Definition: c.h:675
void pq_startmsgread ( void  )

Definition at line 1210 of file pqcomm.c.

References ereport, errcode(), errmsg(), FATAL, and PqCommReadingMsg.

Referenced by CheckSCRAMAuth(), CopyGetData(), ProcessRepliesIfAny(), ProcessStartupPacket(), ReceiveCopyBegin(), recv_password_packet(), and SocketBackend().

1211 {
1212  /*
1213  * There shouldn't be a read active already, but let's check just to be
1214  * sure.
1215  */
1216  if (PqCommReadingMsg)
1217  ereport(FATAL,
1218  (errcode(ERRCODE_PROTOCOL_VIOLATION),
1219  errmsg("terminating connection because protocol synchronization was lost")));
1220 
1221  PqCommReadingMsg = true;
1222 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define FATAL
Definition: elog.h:52
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
static bool PqCommReadingMsg
Definition: pqcomm.c:150
void RemoveSocketFiles ( void  )

Definition at line 889 of file pqcomm.c.

References lfirst, NIL, and unlink().

Referenced by CloseServerPorts().

890 {
891  ListCell *l;
892 
893  /* Loop through all created sockets... */
894  foreach(l, sock_paths)
895  {
896  char *sock_path = (char *) lfirst(l);
897 
898  /* Ignore any error. */
899  (void) unlink(sock_path);
900  }
901  /* Since we're about to exit, no need to reclaim storage */
902  sock_paths = NIL;
903 }
#define NIL
Definition: pg_list.h:69
static List * sock_paths
Definition: pqcomm.c:125
int unlink(const char *filename)
#define lfirst(lc)
Definition: pg_list.h:106
void secure_close ( Port port)

Definition at line 125 of file be-secure.c.

References be_tls_close(), and Port::ssl_in_use.

Referenced by ConnFree(), and socket_close().

126 {
127 #ifdef USE_SSL
128  if (port->ssl_in_use)
129  be_tls_close(port);
130 #endif
131 }
bool ssl_in_use
Definition: libpq-be.h:181
void be_tls_close(Port *port)
void secure_destroy ( void  )

Definition at line 86 of file be-secure.c.

References be_tls_destroy().

Referenced by SIGHUP_handler().

87 {
88 #ifdef USE_SSL
90 #endif
91 }
void be_tls_destroy(void)
int secure_initialize ( bool  isServerStart)

Definition at line 73 of file be-secure.c.

References be_tls_init().

Referenced by PostmasterMain(), and SIGHUP_handler().

74 {
75 #ifdef USE_SSL
76  return be_tls_init(isServerStart);
77 #else
78  return 0;
79 #endif
80 }
int be_tls_init(bool isServerStart)
bool secure_loaded_verify_locations ( void  )

Definition at line 97 of file be-secure.c.

References ssl_loaded_verify_locations.

Referenced by ClientAuthentication().

98 {
99 #ifdef USE_SSL
101 #else
102  return false;
103 #endif
104 }
bool ssl_loaded_verify_locations
int secure_open_server ( Port port)

Definition at line 110 of file be-secure.c.

References be_tls_open_server().

Referenced by ProcessStartupPacket().

111 {
112  int r = 0;
113 
114 #ifdef USE_SSL
115  r = be_tls_open_server(port);
116 #endif
117 
118  return r;
119 }
int be_tls_open_server(Port *port)
ssize_t secure_raw_read ( Port port,
void *  ptr,
size_t  len 
)

Definition at line 216 of file be-secure.c.

References pgwin32_noblock, recv, and Port::sock.

Referenced by my_sock_read(), and secure_read().

217 {
218  ssize_t n;
219 
220  /*
221  * Try to read from the socket without blocking. If it succeeds we're
222  * done, otherwise we'll wait for the socket using the latch mechanism.
223  */
224 #ifdef WIN32
225  pgwin32_noblock = true;
226 #endif
227  n = recv(port->sock, ptr, len, 0);
228 #ifdef WIN32
229  pgwin32_noblock = false;
230 #endif
231 
232  return n;
233 }
int pgwin32_noblock
Definition: socket.c:28
#define recv(s, buf, len, flags)
Definition: win32.h:375
pgsocket sock
Definition: libpq-be.h:118
ssize_t secure_raw_write ( Port port,
const void *  ptr,
size_t  len 
)

Definition at line 302 of file be-secure.c.

References pgwin32_noblock, send, and Port::sock.

Referenced by my_sock_write(), and secure_write().

303 {
304  ssize_t n;
305 
306 #ifdef WIN32
307  pgwin32_noblock = true;
308 #endif
309  n = send(port->sock, ptr, len, 0);
310 #ifdef WIN32
311  pgwin32_noblock = false;
312 #endif
313 
314  return n;
315 }
#define send(s, buf, len, flags)
Definition: win32.h:376
int pgwin32_noblock
Definition: socket.c:28
pgsocket sock
Definition: libpq-be.h:118
ssize_t secure_read ( Port port,
void *  ptr,
size_t  len 
)

Definition at line 137 of file be-secure.c.

References Assert, be_tls_read(), EAGAIN, ereport, errcode(), errmsg(), WaitEvent::events, EWOULDBLOCK, FATAL, FeBeWaitSet, ModifyWaitEvent(), MyLatch, NULL, ProcessClientReadInterrupt(), ResetLatch(), secure_raw_read(), Port::ssl_in_use, WAIT_EVENT_CLIENT_READ, WaitEventSetWait(), WL_LATCH_SET, WL_POSTMASTER_DEATH, and WL_SOCKET_READABLE.

Referenced by pq_getbyte_if_available(), and pq_recvbuf().

138 {
139  ssize_t n;
140  int waitfor;
141 
142 retry:
143 #ifdef USE_SSL
144  waitfor = 0;
145  if (port->ssl_in_use)
146  {
147  n = be_tls_read(port, ptr, len, &waitfor);
148  }
149  else
150 #endif
151  {
152  n = secure_raw_read(port, ptr, len);
153  waitfor = WL_SOCKET_READABLE;
154  }
155 
156  /* In blocking mode, wait until the socket is ready */
157  if (n < 0 && !port->noblock && (errno == EWOULDBLOCK || errno == EAGAIN))
158  {
159  WaitEvent event;
160 
161  Assert(waitfor);
162 
163  ModifyWaitEvent(FeBeWaitSet, 0, waitfor, NULL);
164 
165  WaitEventSetWait(FeBeWaitSet, -1 /* no timeout */ , &event, 1,
167 
168  /*
169  * If the postmaster has died, it's not safe to continue running,
170  * because it is the postmaster's job to kill us if some other backend
171  * exists uncleanly. Moreover, we won't run very well in this state;
172  * helper processes like walwriter and the bgwriter will exit, so
173  * performance may be poor. Finally, if we don't exit, pg_ctl will be
174  * unable to restart the postmaster without manual intervention, so no
175  * new connections can be accepted. Exiting clears the deck for a
176  * postmaster restart.
177  *
178  * (Note that we only make this check when we would otherwise sleep on
179  * our latch. We might still continue running for a while if the
180  * postmaster is killed in mid-query, or even through multiple queries
181  * if we never have to wait for read. We don't want to burn too many
182  * cycles checking for this very rare condition, and this should cause
183  * us to exit quickly in most cases.)
184  */
185  if (event.events & WL_POSTMASTER_DEATH)
186  ereport(FATAL,
187  (errcode(ERRCODE_ADMIN_SHUTDOWN),
188  errmsg("terminating connection due to unexpected postmaster exit")));
189 
190  /* Handle interrupt. */
191  if (event.events & WL_LATCH_SET)
192  {
195 
196  /*
197  * We'll retry the read. Most likely it will return immediately
198  * because there's still no data available, and we'll wait for the
199  * socket to become ready again.
200  */
201  }
202  goto retry;
203  }
204 
205  /*
206  * Process interrupts that happened while (or before) receiving. Note that
207  * we signal that we're not blocking, which will prevent some types of
208  * interrupts from being processed.
209  */
211 
212  return n;
213 }
#define EWOULDBLOCK
Definition: win32.h:291
ssize_t be_tls_read(Port *port, void *ptr, size_t len, int *waitfor)
int errcode(int sqlerrcode)
Definition: elog.c:575
void ModifyWaitEvent(WaitEventSet *set, int pos, uint32 events, Latch *latch)
Definition: latch.c:735
bool ssl_in_use
Definition: libpq-be.h:181
#define WL_SOCKET_READABLE
Definition: latch.h:125
WaitEventSet * FeBeWaitSet
Definition: pqcomm.c:186
void ResetLatch(volatile Latch *latch)
Definition: latch.c:498
#define EAGAIN
Definition: win32.h:283
#define FATAL
Definition: elog.h:52
uint32 events
Definition: latch.h:133
#define ereport(elevel, rest)
Definition: elog.h:122
#define WL_POSTMASTER_DEATH
Definition: latch.h:128
void ProcessClientReadInterrupt(bool blocked)
Definition: postgres.c:516
ssize_t secure_raw_read(Port *port, void *ptr, size_t len)
Definition: be-secure.c:216
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
int errmsg(const char *fmt,...)
Definition: elog.c:797
struct Latch * MyLatch
Definition: globals.c:52
#define WL_LATCH_SET
Definition: latch.h:124
int WaitEventSetWait(WaitEventSet *set, long timeout, WaitEvent *occurred_events, int nevents, uint32 wait_event_info)
Definition: latch.c:918
ssize_t secure_write ( Port port,
void *  ptr,
size_t  len 
)

Definition at line 240 of file be-secure.c.

References Assert, be_tls_write(), EAGAIN, ereport, errcode(), errmsg(), WaitEvent::events, EWOULDBLOCK, FATAL, FeBeWaitSet, ModifyWaitEvent(), MyLatch, NULL, ProcessClientWriteInterrupt(), ResetLatch(), secure_raw_write(), Port::ssl_in_use, WAIT_EVENT_CLIENT_WRITE, WaitEventSetWait(), WL_LATCH_SET, WL_POSTMASTER_DEATH, and WL_SOCKET_WRITEABLE.

Referenced by internal_flush().

241 {
242  ssize_t n;
243  int waitfor;
244 
245 retry:
246  waitfor = 0;
247 #ifdef USE_SSL
248  if (port->ssl_in_use)
249  {
250  n = be_tls_write(port, ptr, len, &waitfor);
251  }
252  else
253 #endif
254  {
255  n = secure_raw_write(port, ptr, len);
256  waitfor = WL_SOCKET_WRITEABLE;
257  }
258 
259  if (n < 0 && !port->noblock && (errno == EWOULDBLOCK || errno == EAGAIN))
260  {
261  WaitEvent event;
262 
263  Assert(waitfor);
264 
265  ModifyWaitEvent(FeBeWaitSet, 0, waitfor, NULL);
266 
267  WaitEventSetWait(FeBeWaitSet, -1 /* no timeout */ , &event, 1,
269 
270  /* See comments in secure_read. */
271  if (event.events & WL_POSTMASTER_DEATH)
272  ereport(FATAL,
273  (errcode(ERRCODE_ADMIN_SHUTDOWN),
274  errmsg("terminating connection due to unexpected postmaster exit")));
275 
276  /* Handle interrupt. */
277  if (event.events & WL_LATCH_SET)
278  {
281 
282  /*
283  * We'll retry the write. Most likely it will return immediately
284  * because there's still no data available, and we'll wait for the
285  * socket to become ready again.
286  */
287  }
288  goto retry;
289  }
290 
291  /*
292  * Process interrupts that happened while (or before) sending. Note that
293  * we signal that we're not blocking, which will prevent some types of
294  * interrupts from being processed.
295  */
297 
298  return n;
299 }
#define EWOULDBLOCK
Definition: win32.h:291
#define WL_SOCKET_WRITEABLE
Definition: latch.h:126
ssize_t secure_raw_write(Port *port, const void *ptr, size_t len)
Definition: be-secure.c:302
int errcode(int sqlerrcode)
Definition: elog.c:575
void ModifyWaitEvent(WaitEventSet *set, int pos, uint32 events, Latch *latch)
Definition: latch.c:735
bool ssl_in_use
Definition: libpq-be.h:181
WaitEventSet * FeBeWaitSet
Definition: pqcomm.c:186
void ResetLatch(volatile Latch *latch)
Definition: latch.c:498
#define EAGAIN
Definition: win32.h:283
#define FATAL
Definition: elog.h:52
uint32 events
Definition: latch.h:133
ssize_t be_tls_write(Port *port, void *ptr, size_t len, int *waitfor)
#define ereport(elevel, rest)
Definition: elog.h:122
#define WL_POSTMASTER_DEATH
Definition: latch.h:128
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
int errmsg(const char *fmt,...)
Definition: elog.c:797
struct Latch * MyLatch
Definition: globals.c:52
#define WL_LATCH_SET
Definition: latch.h:124
int WaitEventSetWait(WaitEventSet *set, long timeout, WaitEvent *occurred_events, int nevents, uint32 wait_event_info)
Definition: latch.c:918
void ProcessClientWriteInterrupt(bool blocked)
Definition: postgres.c:554
void StreamClose ( pgsocket  sock)

Definition at line 844 of file pqcomm.c.

References closesocket.

Referenced by ClosePostmasterPorts(), CloseServerPorts(), ConnCreate(), and ServerLoop().

845 {
846  closesocket(sock);
847 }
#define closesocket
Definition: port.h:331
int StreamConnection ( pgsocket  server_fd,
Port port 
)

Definition at line 716 of file pqcomm.c.

References accept, SockAddr::addr, elog, ereport, errcode_for_socket_access(), errmsg(), IS_AF_UNIX, Port::laddr, LOG, pg_usleep(), PGINVALID_SOCKET, PQ_SEND_BUFFER_SIZE, pq_setkeepalivescount(), pq_setkeepalivesidle(), pq_setkeepalivesinterval(), Port::raddr, SockAddr::salen, Port::sock, STATUS_ERROR, STATUS_OK, tcp_keepalives_count, tcp_keepalives_idle, and tcp_keepalives_interval.

Referenced by ConnCreate().

717 {
718  /* accept connection and fill in the client (remote) address */
719  port->raddr.salen = sizeof(port->raddr.addr);
720  if ((port->sock = accept(server_fd,
721  (struct sockaddr *) &port->raddr.addr,
722  &port->raddr.salen)) == PGINVALID_SOCKET)
723  {
724  ereport(LOG,
726  errmsg("could not accept new connection: %m")));
727 
728  /*
729  * If accept() fails then postmaster.c will still see the server
730  * socket as read-ready, and will immediately try again. To avoid
731  * uselessly sucking lots of CPU, delay a bit before trying again.
732  * (The most likely reason for failure is being out of kernel file
733  * table slots; we can do little except hope some will get freed up.)
734  */
735  pg_usleep(100000L); /* wait 0.1 sec */
736  return STATUS_ERROR;
737  }
738 
739  /* fill in the server (local) address */
740  port->laddr.salen = sizeof(port->laddr.addr);
741  if (getsockname(port->sock,
742  (struct sockaddr *) &port->laddr.addr,
743  &port->laddr.salen) < 0)
744  {
745  elog(LOG, "getsockname() failed: %m");
746  return STATUS_ERROR;
747  }
748 
749  /* select NODELAY and KEEPALIVE options if it's a TCP connection */
750  if (!IS_AF_UNIX(port->laddr.addr.ss_family))
751  {
752  int on;
753 #ifdef WIN32
754  int oldopt;
755  int optlen;
756  int newopt;
757 #endif
758 
759 #ifdef TCP_NODELAY
760  on = 1;
761  if (setsockopt(port->sock, IPPROTO_TCP, TCP_NODELAY,
762  (char *) &on, sizeof(on)) < 0)
763  {
764  elog(LOG, "setsockopt(%s) failed: %m", "TCP_NODELAY");
765  return STATUS_ERROR;
766  }
767 #endif
768  on = 1;
769  if (setsockopt(port->sock, SOL_SOCKET, SO_KEEPALIVE,
770  (char *) &on, sizeof(on)) < 0)
771  {
772  elog(LOG, "setsockopt(%s) failed: %m", "SO_KEEPALIVE");
773  return STATUS_ERROR;
774  }
775 
776 #ifdef WIN32
777 
778  /*
779  * This is a Win32 socket optimization. The OS send buffer should be
780  * large enough to send the whole Postgres send buffer in one go, or
781  * performance suffers. The Postgres send buffer can be enlarged if a
782  * very large message needs to be sent, but we won't attempt to
783  * enlarge the OS buffer if that happens, so somewhat arbitrarily
784  * ensure that the OS buffer is at least PQ_SEND_BUFFER_SIZE * 4.
785  * (That's 32kB with the current default).
786  *
787  * The default OS buffer size used to be 8kB in earlier Windows
788  * versions, but was raised to 64kB in Windows 2012. So it shouldn't
789  * be necessary to change it in later versions anymore. Changing it
790  * unnecessarily can even reduce performance, because setting
791  * SO_SNDBUF in the application disables the "dynamic send buffering"
792  * feature that was introduced in Windows 7. So before fiddling with
793  * SO_SNDBUF, check if the current buffer size is already large enough
794  * and only increase it if necessary.
795  *
796  * See https://support.microsoft.com/kb/823764/EN-US/ and
797  * https://msdn.microsoft.com/en-us/library/bb736549%28v=vs.85%29.aspx
798  */
799  optlen = sizeof(oldopt);
800  if (getsockopt(port->sock, SOL_SOCKET, SO_SNDBUF, (char *) &oldopt,
801  &optlen) < 0)
802  {
803  elog(LOG, "getsockopt(%s) failed: %m", "SO_SNDBUF");
804  return STATUS_ERROR;
805  }
806  newopt = PQ_SEND_BUFFER_SIZE * 4;
807  if (oldopt < newopt)
808  {
809  if (setsockopt(port->sock, SOL_SOCKET, SO_SNDBUF, (char *) &newopt,
810  sizeof(newopt)) < 0)
811  {
812  elog(LOG, "setsockopt(%s) failed: %m", "SO_SNDBUF");
813  return STATUS_ERROR;
814  }
815  }
816 #endif
817 
818  /*
819  * Also apply the current keepalive parameters. If we fail to set a
820  * parameter, don't error out, because these aren't universally
821  * supported. (Note: you might think we need to reset the GUC
822  * variables to 0 in such a case, but it's not necessary because the
823  * show hooks for these variables report the truth anyway.)
824  */
828  }
829 
830  return STATUS_OK;
831 }
#define accept(s, addr, addrlen)
Definition: win32.h:372
int pq_setkeepalivesinterval(int interval, Port *port)
Definition: pqcomm.c:1812
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define STATUS_ERROR
Definition: c.h:976
#define LOG
Definition: elog.h:26
pgsocket sock
Definition: libpq-be.h:118
int tcp_keepalives_idle
Definition: guc.c:471
SockAddr raddr
Definition: libpq-be.h:122
void pg_usleep(long microsec)
Definition: signal.c:53
int pq_setkeepalivesidle(int idle, Port *port)
Definition: pqcomm.c:1730
#define IS_AF_UNIX(fam)
Definition: ip.h:24
ACCEPT_TYPE_ARG3 salen
Definition: pqcomm.h:65
#define ereport(elevel, rest)
Definition: elog.h:122
#define STATUS_OK
Definition: c.h:975
int tcp_keepalives_interval
Definition: guc.c:472
int errcode_for_socket_access(void)
Definition: elog.c:669
SockAddr laddr
Definition: libpq-be.h:121
#define PGINVALID_SOCKET
Definition: port.h:24
int pq_setkeepalivescount(int count, Port *port)
Definition: pqcomm.c:1888
int errmsg(const char *fmt,...)
Definition: elog.c:797
int tcp_keepalives_count
Definition: guc.c:473
#define elog
Definition: elog.h:219
#define PQ_SEND_BUFFER_SIZE
Definition: pqcomm.c:134
int StreamServerPort ( int  family,
char *  hostName,
unsigned short  portNumber,
char *  unixSocketDir,
pgsocket  ListenSocket[],
int  MaxListen 
)

Definition at line 330 of file pqcomm.c.

References _, addrinfo::ai_family, addrinfo::ai_flags, addrinfo::ai_next, AI_PASSIVE, addrinfo::ai_socktype, bind, closesocket, ereport, errcode_for_socket_access(), errhint(), errmsg(), fd(), gai_strerror, IS_AF_UNIX, listen, LOG, MaxBackends, MAXPGPATH, MemSet, NI_MAXHOST, NI_NUMERICHOST, NULL, pg_freeaddrinfo_all(), pg_getaddrinfo_all(), pg_getnameinfo_all(), PG_SOMAXCONN, PGINVALID_SOCKET, snprintf(), socket, STATUS_ERROR, STATUS_OK, UNIXSOCK_PATH, and UNIXSOCK_PATH_BUFLEN.

Referenced by PostmasterMain().

333 {
334  pgsocket fd;
335  int err;
336  int maxconn;
337  int ret;
338  char portNumberStr[32];
339  const char *familyDesc;
340  char familyDescBuf[64];
341  const char *addrDesc;
342  char addrBuf[NI_MAXHOST];
343  char *service;
344  struct addrinfo *addrs = NULL,
345  *addr;
346  struct addrinfo hint;
347  int listen_index = 0;
348  int added = 0;
349 
350 #ifdef HAVE_UNIX_SOCKETS
351  char unixSocketPath[MAXPGPATH];
352 #endif
353 #if !defined(WIN32) || defined(IPV6_V6ONLY)
354  int one = 1;
355 #endif
356 
357  /* Initialize hint structure */
358  MemSet(&hint, 0, sizeof(hint));
359  hint.ai_family = family;
360  hint.ai_flags = AI_PASSIVE;
361  hint.ai_socktype = SOCK_STREAM;
362 
363 #ifdef HAVE_UNIX_SOCKETS
364  if (family == AF_UNIX)
365  {
366  /*
367  * Create unixSocketPath from portNumber and unixSocketDir and lock
368  * that file path
369  */
370  UNIXSOCK_PATH(unixSocketPath, portNumber, unixSocketDir);
371  if (strlen(unixSocketPath) >= UNIXSOCK_PATH_BUFLEN)
372  {
373  ereport(LOG,
374  (errmsg("Unix-domain socket path \"%s\" is too long (maximum %d bytes)",
375  unixSocketPath,
376  (int) (UNIXSOCK_PATH_BUFLEN - 1))));
377  return STATUS_ERROR;
378  }
379  if (Lock_AF_UNIX(unixSocketDir, unixSocketPath) != STATUS_OK)
380  return STATUS_ERROR;
381  service = unixSocketPath;
382  }
383  else
384 #endif /* HAVE_UNIX_SOCKETS */
385  {
386  snprintf(portNumberStr, sizeof(portNumberStr), "%d", portNumber);
387  service = portNumberStr;
388  }
389 
390  ret = pg_getaddrinfo_all(hostName, service, &hint, &addrs);
391  if (ret || !addrs)
392  {
393  if (hostName)
394  ereport(LOG,
395  (errmsg("could not translate host name \"%s\", service \"%s\" to address: %s",
396  hostName, service, gai_strerror(ret))));
397  else
398  ereport(LOG,
399  (errmsg("could not translate service \"%s\" to address: %s",
400  service, gai_strerror(ret))));
401  if (addrs)
402  pg_freeaddrinfo_all(hint.ai_family, addrs);
403  return STATUS_ERROR;
404  }
405 
406  for (addr = addrs; addr; addr = addr->ai_next)
407  {
408  if (!IS_AF_UNIX(family) && IS_AF_UNIX(addr->ai_family))
409  {
410  /*
411  * Only set up a unix domain socket when they really asked for it.
412  * The service/port is different in that case.
413  */
414  continue;
415  }
416 
417  /* See if there is still room to add 1 more socket. */
418  for (; listen_index < MaxListen; listen_index++)
419  {
420  if (ListenSocket[listen_index] == PGINVALID_SOCKET)
421  break;
422  }
423  if (listen_index >= MaxListen)
424  {
425  ereport(LOG,
426  (errmsg("could not bind to all requested addresses: MAXLISTEN (%d) exceeded",
427  MaxListen)));
428  break;
429  }
430 
431  /* set up address family name for log messages */
432  switch (addr->ai_family)
433  {
434  case AF_INET:
435  familyDesc = _("IPv4");
436  break;
437 #ifdef HAVE_IPV6
438  case AF_INET6:
439  familyDesc = _("IPv6");
440  break;
441 #endif
442 #ifdef HAVE_UNIX_SOCKETS
443  case AF_UNIX:
444  familyDesc = _("Unix");
445  break;
446 #endif
447  default:
448  snprintf(familyDescBuf, sizeof(familyDescBuf),
449  _("unrecognized address family %d"),
450  addr->ai_family);
451  familyDesc = familyDescBuf;
452  break;
453  }
454 
455  /* set up text form of address for log messages */
456 #ifdef HAVE_UNIX_SOCKETS
457  if (addr->ai_family == AF_UNIX)
458  addrDesc = unixSocketPath;
459  else
460 #endif
461  {
462  pg_getnameinfo_all((const struct sockaddr_storage *) addr->ai_addr,
463  addr->ai_addrlen,
464  addrBuf, sizeof(addrBuf),
465  NULL, 0,
467  addrDesc = addrBuf;
468  }
469 
470  if ((fd = socket(addr->ai_family, SOCK_STREAM, 0)) == PGINVALID_SOCKET)
471  {
472  ereport(LOG,
474  /* translator: first %s is IPv4, IPv6, or Unix */
475  errmsg("could not create %s socket for address \"%s\": %m",
476  familyDesc, addrDesc)));
477  continue;
478  }
479 
480 #ifndef WIN32
481 
482  /*
483  * Without the SO_REUSEADDR flag, a new postmaster can't be started
484  * right away after a stop or crash, giving "address already in use"
485  * error on TCP ports.
486  *
487  * On win32, however, this behavior only happens if the
488  * SO_EXLUSIVEADDRUSE is set. With SO_REUSEADDR, win32 allows multiple
489  * servers to listen on the same address, resulting in unpredictable
490  * behavior. With no flags at all, win32 behaves as Unix with
491  * SO_REUSEADDR.
492  */
493  if (!IS_AF_UNIX(addr->ai_family))
494  {
495  if ((setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
496  (char *) &one, sizeof(one))) == -1)
497  {
498  ereport(LOG,
500  /* translator: first %s is IPv4, IPv6, or Unix */
501  errmsg("setsockopt(SO_REUSEADDR) failed for %s address \"%s\": %m",
502  familyDesc, addrDesc)));
503  closesocket(fd);
504  continue;
505  }
506  }
507 #endif
508 
509 #ifdef IPV6_V6ONLY
510  if (addr->ai_family == AF_INET6)
511  {
512  if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY,
513  (char *) &one, sizeof(one)) == -1)
514  {
515  ereport(LOG,
517  /* translator: first %s is IPv4, IPv6, or Unix */
518  errmsg("setsockopt(IPV6_V6ONLY) failed for %s address \"%s\": %m",
519  familyDesc, addrDesc)));
520  closesocket(fd);
521  continue;
522  }
523  }
524 #endif
525 
526  /*
527  * Note: This might fail on some OS's, like Linux older than
528  * 2.4.21-pre3, that don't have the IPV6_V6ONLY socket option, and map
529  * ipv4 addresses to ipv6. It will show ::ffff:ipv4 for all ipv4
530  * connections.
531  */
532  err = bind(fd, addr->ai_addr, addr->ai_addrlen);
533  if (err < 0)
534  {
535  ereport(LOG,
537  /* translator: first %s is IPv4, IPv6, or Unix */
538  errmsg("could not bind %s address \"%s\": %m",
539  familyDesc, addrDesc),
540  (IS_AF_UNIX(addr->ai_family)) ?
541  errhint("Is another postmaster already running on port %d?"
542  " If not, remove socket file \"%s\" and retry.",
543  (int) portNumber, service) :
544  errhint("Is another postmaster already running on port %d?"
545  " If not, wait a few seconds and retry.",
546  (int) portNumber)));
547  closesocket(fd);
548  continue;
549  }
550 
551 #ifdef HAVE_UNIX_SOCKETS
552  if (addr->ai_family == AF_UNIX)
553  {
554  if (Setup_AF_UNIX(service) != STATUS_OK)
555  {
556  closesocket(fd);
557  break;
558  }
559  }
560 #endif
561 
562  /*
563  * Select appropriate accept-queue length limit. PG_SOMAXCONN is only
564  * intended to provide a clamp on the request on platforms where an
565  * overly large request provokes a kernel error (are there any?).
566  */
567  maxconn = MaxBackends * 2;
568  if (maxconn > PG_SOMAXCONN)
569  maxconn = PG_SOMAXCONN;
570 
571  err = listen(fd, maxconn);
572  if (err < 0)
573  {
574  ereport(LOG,
576  /* translator: first %s is IPv4, IPv6, or Unix */
577  errmsg("could not listen on %s address \"%s\": %m",
578  familyDesc, addrDesc)));
579  closesocket(fd);
580  continue;
581  }
582 
583 #ifdef HAVE_UNIX_SOCKETS
584  if (addr->ai_family == AF_UNIX)
585  ereport(LOG,
586  (errmsg("listening on Unix socket \"%s\"",
587  addrDesc)));
588  else
589 #endif
590  ereport(LOG,
591  /* translator: first %s is IPv4 or IPv6 */
592  (errmsg("listening on %s address \"%s\", port %d",
593  familyDesc, addrDesc, (int) portNumber)));
594 
595  ListenSocket[listen_index] = fd;
596  added++;
597  }
598 
599  pg_freeaddrinfo_all(hint.ai_family, addrs);
600 
601  if (!added)
602  return STATUS_ERROR;
603 
604  return STATUS_OK;
605 }
void pg_freeaddrinfo_all(int hint_ai_family, struct addrinfo *ai)
Definition: ip.c:88
#define UNIXSOCK_PATH(path, port, sockdir)
Definition: pqcomm.h:70
int errhint(const char *fmt,...)
Definition: elog.c:987
#define NI_NUMERICHOST
Definition: getaddrinfo.h:78
#define UNIXSOCK_PATH_BUFLEN
Definition: pqcomm.h:86
#define closesocket
Definition: port.h:331
#define socket(af, type, protocol)
Definition: win32.h:369
#define STATUS_ERROR
Definition: c.h:976
#define MemSet(start, val, len)
Definition: c.h:857
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define LOG
Definition: elog.h:26
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define gai_strerror
Definition: getaddrinfo.h:146
int pg_getaddrinfo_all(const char *hostname, const char *servname, const struct addrinfo *hintp, struct addrinfo **result)
Definition: ip.c:57
#define NI_MAXHOST
Definition: getaddrinfo.h:88
#define bind(s, addr, addrlen)
Definition: win32.h:370
#define IS_AF_UNIX(fam)
Definition: ip.h:24
#define MAXPGPATH
int MaxBackends
Definition: globals.c:127
#define AI_PASSIVE
Definition: getaddrinfo.h:62
int pgsocket
Definition: port.h:22
#define ereport(elevel, rest)
Definition: elog.h:122
#define STATUS_OK
Definition: c.h:975
int errcode_for_socket_access(void)
Definition: elog.c:669
static pgsocket ListenSocket[MAXLISTEN]
Definition: postmaster.c:216
#define listen(s, backlog)
Definition: win32.h:371
#define PGINVALID_SOCKET
Definition: port.h:24
#define PG_SOMAXCONN
#define NULL
Definition: c.h:229
int pg_getnameinfo_all(const struct sockaddr_storage *addr, int salen, char *node, int nodelen, char *service, int servicelen, int flags)
Definition: ip.c:122
struct addrinfo * ai_next
Definition: getaddrinfo.h:107
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define _(x)
Definition: elog.c:84
void TouchSocketFiles ( void  )

Definition at line 859 of file pqcomm.c.

References lfirst, and NULL.

Referenced by ServerLoop().

860 {
861  ListCell *l;
862 
863  /* Loop through all created sockets... */
864  foreach(l, sock_paths)
865  {
866  char *sock_path = (char *) lfirst(l);
867 
868  /*
869  * utime() is POSIX standard, utimes() is a common alternative. If we
870  * have neither, there's no way to affect the mod or access time of
871  * the socket :-(
872  *
873  * In either path, we ignore errors; there's no point in complaining.
874  */
875 #ifdef HAVE_UTIME
876  utime(sock_path, NULL);
877 #else /* !HAVE_UTIME */
878 #ifdef HAVE_UTIMES
879  utimes(sock_path, NULL);
880 #endif /* HAVE_UTIMES */
881 #endif /* HAVE_UTIME */
882  }
883 }
static List * sock_paths
Definition: pqcomm.c:125
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106

Variable Documentation

WaitEventSet* FeBeWaitSet

Definition at line 186 of file pqcomm.c.

Referenced by secure_read(), secure_write(), SwitchBackToLocalLatch(), and SwitchToSharedLatch().

PGDLLIMPORT PQcommMethods* PqCommMethods

Definition at line 184 of file pqcomm.c.

Referenced by pq_redirect_to_shm_mq(), and pq_set_parallel_master().

char* ssl_ca_file

Definition at line 45 of file be-secure.c.

Referenced by be_tls_init().

char* ssl_cert_file

Definition at line 43 of file be-secure.c.

Referenced by be_tls_init().

char* ssl_crl_file

Definition at line 46 of file be-secure.c.

Referenced by be_tls_init().

char* ssl_key_file

Definition at line 44 of file be-secure.c.

Referenced by be_tls_init().

bool ssl_loaded_verify_locations
char* SSLCipherSuites

Definition at line 53 of file be-secure.c.

Referenced by be_tls_init().

char* SSLECDHCurve

Definition at line 56 of file be-secure.c.

Referenced by initialize_ecdh().

bool SSLPreferServerCiphers

Definition at line 59 of file be-secure.c.

Referenced by be_tls_init().