1
0
mirror of https://github.com/postgres/postgres.git synced 2025-10-24 01:29:19 +03:00

IPv6 cleanups.

Kurt Roeckx
Andrew Dunstan
This commit is contained in:
Bruce Momjian
2003-06-12 07:36:51 +00:00
parent e5549a272d
commit b4cea00a1f
20 changed files with 905 additions and 586 deletions

View File

@@ -13,7 +13,7 @@
*
* Copyright (c) 2001-2003, PostgreSQL Global Development Group
*
* $Header: /cvsroot/pgsql/src/backend/postmaster/pgstat.c,v 1.36 2003/05/15 16:35:29 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/postmaster/pgstat.c,v 1.37 2003/06/12 07:36:51 momjian Exp $
* ----------
*/
#include "postgres.h"
@@ -22,7 +22,9 @@
#include <fcntl.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
@@ -36,6 +38,7 @@
#include "catalog/pg_shadow.h"
#include "catalog/pg_database.h"
#include "libpq/pqsignal.h"
#include "libpq/libpq.h"
#include "mb/pg_wchar.h"
#include "miscadmin.h"
#include "utils/memutils.h"
@@ -69,7 +72,7 @@ bool pgstat_is_running = false;
*/
static int pgStatSock = -1;
static int pgStatPipe[2];
static struct sockaddr_in pgStatAddr;
static struct sockaddr_storage pgStatAddr;
static int pgStatPmPipe[2] = {-1, -1};
static int pgStatPid;
@@ -141,7 +144,9 @@ static void pgstat_recv_resetcounter(PgStat_MsgResetcounter *msg, int len);
void
pgstat_init(void)
{
int alen;
ACCEPT_TYPE_ARG3 alen;
struct addrinfo *addr, hints;
int ret;
/*
* Force start of collector daemon if something to collect
@@ -174,7 +179,24 @@ pgstat_init(void)
/*
* Create the UDP socket for sending and receiving statistic messages
*/
if ((pgStatSock = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
hints.ai_flags = AI_PASSIVE;
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = 0;
hints.ai_addrlen = 0;
hints.ai_addr = NULL;
hints.ai_canonname = NULL;
hints.ai_next = NULL;
ret = getaddrinfo2("localhost", NULL, &hints, &addr);
if (ret || !addr)
{
elog(LOG, "PGSTAT: getaddrinfo2() failed: %s",
gai_strerror(ret));
goto startup_failed;
}
if ((pgStatSock = socket(addr->ai_family,
addr->ai_socktype, addr->ai_protocol)) < 0)
{
elog(LOG, "PGSTAT: socket() failed: %m");
goto startup_failed;
@@ -184,16 +206,16 @@ pgstat_init(void)
* Bind it to a kernel assigned port on localhost and get the assigned
* port via getsockname().
*/
pgStatAddr.sin_family = AF_INET;
pgStatAddr.sin_port = htons(0);
inet_aton("127.0.0.1", &(pgStatAddr.sin_addr));
alen = sizeof(pgStatAddr);
if (bind(pgStatSock, (struct sockaddr *) & pgStatAddr, alen) < 0)
if (bind(pgStatSock, addr->ai_addr, addr->ai_addrlen) < 0)
{
elog(LOG, "PGSTAT: bind(127.0.0.1) failed: %m");
elog(LOG, "PGSTAT: bind() failed: %m");
goto startup_failed;
}
if (getsockname(pgStatSock, (struct sockaddr *) & pgStatAddr, &alen) < 0)
freeaddrinfo2(hints.ai_family, addr);
addr = NULL;
alen = sizeof(pgStatAddr);
if (getsockname(pgStatSock, (struct sockaddr *)&pgStatAddr, &alen) < 0)
{
elog(LOG, "PGSTAT: getsockname() failed: %m");
goto startup_failed;
@@ -235,6 +257,11 @@ pgstat_init(void)
return;
startup_failed:
if (addr)
{
freeaddrinfo2(hints.ai_family, addr);
}
if (pgStatSock >= 0)
closesocket(pgStatSock);
pgStatSock = -1;
@@ -1496,7 +1523,7 @@ pgstat_recvbuffer(void)
int msg_send = 0; /* next send index in buffer */
int msg_recv = 0; /* next receive index */
int msg_have = 0; /* number of bytes stored */
struct sockaddr_in fromaddr;
struct sockaddr_storage fromaddr;
int fromlen;
bool overflow = false;
@@ -1601,9 +1628,9 @@ pgstat_recvbuffer(void)
if (FD_ISSET(pgStatSock, &rfds))
{
fromlen = sizeof(fromaddr);
len = recvfrom(pgStatSock,
(char *) &input_buffer, sizeof(PgStat_Msg), 0,
(struct sockaddr *) &fromaddr, &fromlen);
len = recvfrom(pgStatSock, (char *) &input_buffer,
sizeof(PgStat_Msg), 0,
(struct sockaddr *) &fromaddr, &fromlen);
if (len < 0)
{
elog(LOG, "PGSTATBUFF: recvfrom() failed: %m");
@@ -1629,9 +1656,7 @@ pgstat_recvbuffer(void)
* kernel-level check due to having used connect(), but let's
* do it anyway.)
*/
if (fromaddr.sin_addr.s_addr != pgStatAddr.sin_addr.s_addr)
continue;
if (fromaddr.sin_port != pgStatAddr.sin_port)
if (memcmp(&fromaddr, &pgStatAddr, fromlen))
continue;
/*

View File

@@ -37,7 +37,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.332 2003/06/11 06:56:06 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.333 2003/06/12 07:36:51 momjian Exp $
*
* NOTES
*
@@ -168,14 +168,9 @@ int ReservedBackends;
static char *progname = (char *) NULL;
/*
* Default Values
*/
static int ServerSock_INET = INVALID_SOCK; /* stream socket server */
#ifdef HAVE_UNIX_SOCKETS
static int ServerSock_UNIX = INVALID_SOCK; /* stream socket server */
#endif
/* The sockets we're listening to. */
#define MAXLISTEN 10
int ListenSocket[MAXLISTEN];
/* Used to reduce macros tests */
#ifdef EXEC_BACKEND
@@ -384,10 +379,11 @@ reg_reply(DNSServiceRegistrationReplyErrorType errorCode, void *context)
int
PostmasterMain(int argc, char *argv[])
{
int opt;
int status;
int opt;
int status;
char original_extraoptions[MAXPGPATH];
char *potential_DataDir = NULL;
char *potential_DataDir = NULL;
int i;
*original_extraoptions = '\0';
@@ -713,32 +709,53 @@ PostmasterMain(int argc, char *argv[])
/*
* Establish input sockets.
*/
for (i = 0; i < MAXLISTEN; i++)
{
ListenSocket[i] = -1;
}
if (NetServer)
{
#ifdef HAVE_IPV6
/* Try INET6 first. May fail if kernel doesn't support IP6 */
status = StreamServerPort(AF_INET6, VirtualHost,
(unsigned short) PostPortNumber,
UnixSocketDir,
&ServerSock_INET);
if (status != STATUS_OK)
if (VirtualHost && VirtualHost[0])
{
elog(LOG, "IPv6 support disabled --- perhaps the kernel does not support IPv6");
#endif
status = StreamServerPort(AF_INET, VirtualHost,
(unsigned short) PostPortNumber,
UnixSocketDir,
&ServerSock_INET);
char *p, *q;
char c = 0;
q = VirtualHost;
do
{
p = strchr(q, ' ');
if (p)
{
c = *p;
*p = '\0';
}
status = StreamServerPort(AF_UNSPEC, q,
(unsigned short) PostPortNumber,
UnixSocketDir, ListenSocket, MAXLISTEN);
if (status != STATUS_OK)
{
postmaster_error("cannot create tcpip "
"listen socket for: %s", p);
}
if (p)
{
*p = c;
q = p + 1;
}
}
while (p);
}
else
{
status = StreamServerPort(AF_UNSPEC, NULL,
(unsigned short) PostPortNumber,
UnixSocketDir, ListenSocket, MAXLISTEN);
if (status != STATUS_OK)
{
postmaster_error("cannot create INET stream port");
ExitPostmaster(1);
postmaster_error("cannot create tcpip listen "
"socket.");
}
#ifdef HAVE_IPV6
else
elog(LOG, "IPv4 socket created");
}
#endif
#ifdef USE_RENDEZVOUS
if (service_name != NULL)
{
@@ -754,10 +771,9 @@ PostmasterMain(int argc, char *argv[])
}
#ifdef HAVE_UNIX_SOCKETS
status = StreamServerPort(AF_UNIX, VirtualHost,
(unsigned short) PostPortNumber,
UnixSocketDir,
&ServerSock_UNIX);
status = StreamServerPort(AF_UNIX, NULL,
(unsigned short) PostPortNumber,
UnixSocketDir, ListenSocket, MAXLISTEN);
if (status != STATUS_OK)
{
postmaster_error("cannot create UNIX stream port");
@@ -961,12 +977,11 @@ usage(const char *progname)
static int
ServerLoop(void)
{
fd_set readmask,
writemask;
fd_set readmask, writemask;
int nSockets;
struct timeval now,
later;
struct timezone tz;
struct timeval now, later;
struct timezone tz;
int i;
gettimeofday(&now, &tz);
@@ -1065,40 +1080,22 @@ ServerLoop(void)
* New connection pending on our well-known port's socket? If so,
* fork a child process to deal with it.
*/
#ifdef HAVE_UNIX_SOCKETS
if (ServerSock_UNIX != INVALID_SOCK
&& FD_ISSET(ServerSock_UNIX, &rmask))
for (i = 0; i < MAXLISTEN; i++)
{
port = ConnCreate(ServerSock_UNIX);
if (port)
if (ListenSocket[i] != -1 && FD_ISSET(ListenSocket[i], &rmask))
{
BackendStartup(port);
port = ConnCreate(ListenSocket[i]);
if (port)
{
BackendStartup(port);
/*
* We no longer need the open socket or port structure in
* this process
*/
StreamClose(port->sock);
ConnFree(port);
}
}
#endif
if (ServerSock_INET != INVALID_SOCK
&& FD_ISSET(ServerSock_INET, &rmask))
{
port = ConnCreate(ServerSock_INET);
if (port)
{
BackendStartup(port);
/*
* We no longer need the open socket or port structure in
* this process
*/
StreamClose(port->sock);
ConnFree(port);
/*
* We no longer need the open socket
* or port structure in this process
*/
StreamClose(port->sock);
ConnFree(port);
}
}
}
@@ -1118,25 +1115,20 @@ static int
initMasks(fd_set *rmask, fd_set *wmask)
{
int nsocks = -1;
int i;
FD_ZERO(rmask);
FD_ZERO(wmask);
#ifdef HAVE_UNIX_SOCKETS
if (ServerSock_UNIX != INVALID_SOCK)
for (i = 0; i < MAXLISTEN; i++)
{
FD_SET(ServerSock_UNIX, rmask);
if (ServerSock_UNIX > nsocks)
nsocks = ServerSock_UNIX;
}
#endif
if (ServerSock_INET != INVALID_SOCK)
{
FD_SET(ServerSock_INET, rmask);
if (ServerSock_INET > nsocks)
nsocks = ServerSock_INET;
int fd = ListenSocket[i];
if (fd != -1)
{
FD_SET(fd, rmask);
if (fd > nsocks)
nsocks = fd;
}
}
return nsocks + 1;
@@ -1220,7 +1212,7 @@ ProcessStartupPacket(Port *port, bool SSLdone)
#ifdef USE_SSL
/* No SSL when disabled or on Unix sockets */
if (!EnableSSL || port->laddr.sa.sa_family != AF_INET)
if (!EnableSSL || port->laddr.addr.ss_family != AF_INET)
SSLok = 'N';
else
SSLok = 'S'; /* Support for SSL */
@@ -1560,14 +1552,18 @@ ConnFree(Port *conn)
void
ClosePostmasterPorts(bool pgstat_too)
{
int i;
/* Close the listen sockets */
if (NetServer)
StreamClose(ServerSock_INET);
ServerSock_INET = INVALID_SOCK;
#ifdef HAVE_UNIX_SOCKETS
StreamClose(ServerSock_UNIX);
ServerSock_UNIX = INVALID_SOCK;
#endif
for (i = 0; i < MAXLISTEN; i++)
{
if (ListenSocket[i] != -1)
{
StreamClose(ListenSocket[i]);
ListenSocket[i] = -1;
}
}
/* Close pgstat control sockets, unless we're starting pgstat itself */
if (pgstat_too)
pgstat_close_sockets();
@@ -2226,19 +2222,20 @@ split_opts(char **argv, int *argcp, char *s)
static int
BackendFork(Port *port)
{
char *remote_host;
char **av;
int maxac;
int ac;
char **av;
int maxac;
int ac;
char debugbuf[32];
char protobuf[32];
#ifdef EXEC_BACKEND
char pbuf[NAMEDATALEN + 256];
#endif
int i;
int status;
struct timeval now;
struct timezone tz;
int i;
int status;
struct timeval now;
struct timezone tz;
char remote_host[NI_MAXHOST];
char remote_port[NI_MAXSERV];
/*
* Let's clean up ourselves as the postmaster child
@@ -2286,63 +2283,37 @@ BackendFork(Port *port)
/*
* Get the remote host name and port for logging and status display.
*/
if (isAF_INETx(port->raddr.sa.sa_family))
remote_host[0] = '\0';
remote_port[0] = '\0';
if (!getnameinfo((struct sockaddr *)&port->raddr.addr,
port->raddr.salen,
remote_host, sizeof(remote_host),
remote_port, sizeof(remote_host),
(log_hostname ? 0 : NI_NUMERICHOST) | NI_NUMERICSERV))
{
unsigned short remote_port;
char *host_addr;
#ifdef HAVE_IPV6
char ip_hostinfo[INET6_ADDRSTRLEN];
#else
char ip_hostinfo[INET_ADDRSTRLEN];
#endif
remote_port = ntohs(port->raddr.in.sin_port);
host_addr = SockAddr_ntop(&port->raddr, ip_hostinfo,
sizeof(ip_hostinfo), 1);
remote_host = NULL;
if (log_hostname)
{
struct hostent *host_ent;
host_ent = gethostbyaddr((char *) &port->raddr.in.sin_addr,
sizeof(port->raddr.in.sin_addr),
AF_INET);
if (host_ent)
{
remote_host = palloc(strlen(host_addr) + strlen(host_ent->h_name) + 3);
sprintf(remote_host, "%s[%s]", host_ent->h_name, host_addr);
}
}
if (remote_host == NULL)
remote_host = pstrdup(host_addr);
if (Log_connections)
elog(LOG, "connection received: host=%s port=%hu",
remote_host, remote_port);
if (LogSourcePort)
{
/* modify remote_host for use in ps status */
int slen = strlen(remote_host) + 10;
char *str = palloc(slen);
snprintf(str, slen, "%s:%hu", remote_host, remote_port);
pfree(remote_host);
remote_host = str;
}
getnameinfo((struct sockaddr *)&port->raddr.addr,
port->raddr.salen,
remote_host, sizeof(remote_host),
remote_port, sizeof(remote_host),
NI_NUMERICHOST | NI_NUMERICSERV);
}
else
{
/* not AF_INET */
remote_host = "[local]";
if (Log_connections)
elog(LOG, "connection received: host=%s",
remote_host);
if (Log_connections)
{
elog(LOG, "connection received: host=%s port=%s",
remote_host, remote_port);
}
if (LogSourcePort)
{
/* modify remote_host for use in ps status */
int slen = strlen(remote_host) + 10;
char *str = palloc(slen);
snprintf(str, slen, "%s:%s", remote_host, remote_port);
strncpy(remote_host, str, sizeof(remote_host));
remote_host[sizeof(remote_host) - 1] = '\0';
pfree(str);
}
/*
@@ -2516,14 +2487,8 @@ ExitPostmaster(int status)
*
* MUST -- vadim 05-10-1999
*/
if (ServerSock_INET != INVALID_SOCK)
StreamClose(ServerSock_INET);
ServerSock_INET = INVALID_SOCK;
#ifdef HAVE_UNIX_SOCKETS
if (ServerSock_UNIX != INVALID_SOCK)
StreamClose(ServerSock_UNIX);
ServerSock_UNIX = INVALID_SOCK;
#endif
/* Should I use true instead? */
ClosePostmasterPorts(false);
proc_exit(status);
}