Add Unix domain socket support, from Goran Thyni, goran@bildbasen.se

This commit is contained in:
Bruce Momjian 1997-11-07 20:52:15 +00:00
parent d3cf6f98c7
commit c17fa36d3c
9 changed files with 187 additions and 136 deletions

View File

@ -7,7 +7,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/libpq/hba.c,v 1.22 1997/09/08 21:43:45 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/libpq/hba.c,v 1.23 1997/11/07 20:51:27 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -855,8 +855,8 @@ authident(const char DataDir[],
/* The username returned by ident */ /* The username returned by ident */
ident(port.raddr.sin_addr, port.laddr.sin_addr, ident(port.raddr.in.sin_addr, port.laddr.in.sin_addr,
port.raddr.sin_port, port.laddr.sin_port, port.raddr.in.sin_port, port.laddr.in.sin_port,
&ident_failed, ident_username); &ident_failed, ident_username);
if (ident_failed) if (ident_failed)
@ -906,10 +906,13 @@ hba_recvauth(const Port *port, const char database[], const char user[],
*/ */
int retvalue; int retvalue;
/* UNIX socket always OK, for now */
if(port->raddr.in.sin_family == AF_UNIX)
return STATUS_OK;
/* Our eventual return value */ /* Our eventual return value */
find_hba_entry(DataDir, port->raddr.in.sin_addr, database,
find_hba_entry(DataDir, port->raddr.sin_addr, database,
&host_ok, &userauth, usermap_name, &host_ok, &userauth, usermap_name,
false /* don't find password entries of type false /* don't find password entries of type
'password' */ ); 'password' */ );

View File

@ -26,7 +26,7 @@ verify_password(char *user, char *password, Port *port,
*test_pw; *test_pw;
char salt[3]; char salt[3];
find_hba_entry(DataDir, port->raddr.sin_addr, database, find_hba_entry(DataDir, port->raddr.in.sin_addr, database,
&host_ok, &userauth, pw_file_name, true); &host_ok, &userauth, pw_file_name, true);
if (!host_ok) if (!host_ok)

View File

@ -7,7 +7,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/libpq/pqcomm.c,v 1.23 1997/09/18 20:20:39 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/libpq/pqcomm.c,v 1.24 1997/11/07 20:51:34 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -571,20 +571,27 @@ pq_async_notify()
* *
* RETURNS: STATUS_OK or STATUS_ERROR * RETURNS: STATUS_OK or STATUS_ERROR
*/ */
static char sock_path[100] = "";
static void do_unlink()
{
if (sock_path[0]) unlink(sock_path);
}
int int
StreamServerPort(char *hostName, short portName, int *fdP) StreamServerPort(char *hostName, short portName, int *fdP)
{ {
struct sockaddr_in sin; struct sockaddr_in sin;
int fd; struct sockaddr_un sun;
int fd, err, family;
int one = 1; int one = 1;
family = hostName != NULL ? AF_INET : AF_UNIX;
if (!hostName)
hostName = "localhost";
MemSet((char *) &sin, 0, sizeof sin); MemSet((char *) &sin, 0, sizeof sin);
if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) if ((fd = socket(family, SOCK_STREAM, 0)) < 0)
{ {
sprintf(PQerrormsg, sprintf(PQerrormsg,
"FATAL: StreamServerPort: socket() failed: errno=%d\n", "FATAL: StreamServerPort: socket() failed: errno=%d\n",
@ -593,7 +600,7 @@ StreamServerPort(char *hostName, short portName, int *fdP)
pqdebug("%s", PQerrormsg); pqdebug("%s", PQerrormsg);
return (STATUS_ERROR); return (STATUS_ERROR);
} }
if (family == AF_UNIX) on_exitpg(do_unlink, (caddr_t) 0);
if ((setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, if ((setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one,
sizeof(one))) == -1) sizeof(one))) == -1)
{ {
@ -604,11 +611,24 @@ StreamServerPort(char *hostName, short portName, int *fdP)
pqdebug("%s", PQerrormsg); pqdebug("%s", PQerrormsg);
return (STATUS_ERROR); return (STATUS_ERROR);
} }
if (family == AF_UNIX)
sin.sin_family = AF_INET; {
sin.sin_port = htons(portName); size_t len;
bzero(&sun, sizeof(sun));
if (bind(fd, (struct sockaddr *) & sin, sizeof sin) < 0) sun.sun_family = family;
len = UNIXSOCK_PATH(sun,portName);
strcpy(sock_path, sun.sun_path);
err = bind(fd, (struct sockaddr *) &sun, len);
}
else
{
bzero(&sin, sizeof(sin));
sin.sin_family = family;
sin.sin_addr.s_addr = htonl(INADDR_ANY);
sin.sin_port = htons(portName);
err = bind(fd, (struct sockaddr *) &sin, sizeof sin);
}
if (err < 0)
{ {
sprintf(PQerrormsg, sprintf(PQerrormsg,
"FATAL: StreamServerPort: bind() failed: errno=%d\n", "FATAL: StreamServerPort: bind() failed: errno=%d\n",
@ -645,26 +665,30 @@ StreamServerPort(char *hostName, short portName, int *fdP)
int int
StreamConnection(int server_fd, Port *port) StreamConnection(int server_fd, Port *port)
{ {
int addrlen; int len, addrlen;
int family = port->raddr.in.sin_family;
/* accept connection (and fill in the client (remote) address) */ /* accept connection (and fill in the client (remote) address) */
addrlen = sizeof(struct sockaddr_in); len = family == AF_INET ?
sizeof(struct sockaddr_in) : sizeof(struct sockaddr_un);
addrlen = len;
if ((port->sock = accept(server_fd, if ((port->sock = accept(server_fd,
(struct sockaddr *) & port->raddr, (struct sockaddr *) & port->raddr,
&addrlen)) < 0) &addrlen)) < 0)
{ {
elog(WARN, "postmaster: StreamConnection: accept: %m"); elog(WARN, "postmaster: StreamConnection: accept: %m");
return (STATUS_ERROR); return (STATUS_ERROR);
} }
/* fill in the server (local) address */ /* fill in the server (local) address */
addrlen = sizeof(struct sockaddr_in); addrlen = len;
if (getsockname(port->sock, (struct sockaddr *) & port->laddr, if (getsockname(port->sock, (struct sockaddr *) & port->laddr,
&addrlen) < 0) &addrlen) < 0)
{ {
elog(WARN, "postmaster: StreamConnection: getsockname: %m"); elog(WARN, "postmaster: StreamConnection: getsockname: %m");
return (STATUS_ERROR); return (STATUS_ERROR);
} }
if (family == AF_INET)
{ {
struct protoent *pe; struct protoent *pe;
int on = 1; int on = 1;
@ -714,45 +738,50 @@ StreamClose(int sock)
int int
StreamOpen(char *hostName, short portName, Port *port) StreamOpen(char *hostName, short portName, Port *port)
{ {
int len, err;
struct hostent *hp; struct hostent *hp;
int laddrlen = sizeof(struct sockaddr_in);
extern int errno; extern int errno;
if (!hostName)
hostName = "localhost";
/* set up the server (remote) address */ /* set up the server (remote) address */
if (!(hp = gethostbyname(hostName)) || hp->h_addrtype != AF_INET) MemSet((char *) &port->raddr, 0, sizeof(port->raddr));
{ if (hostName)
{
if (!(hp = gethostbyname(hostName)) || hp->h_addrtype != AF_INET)
{
sprintf(PQerrormsg, sprintf(PQerrormsg,
"FATAL: StreamOpen: unknown hostname: %s\n", "FATAL: StreamOpen: unknown hostname: %s\n",
hostName); hostName);
fputs(PQerrormsg, stderr); fputs(PQerrormsg, stderr);
pqdebug("%s", PQerrormsg); pqdebug("%s", PQerrormsg);
return (STATUS_ERROR); return (STATUS_ERROR);
} }
MemSet((char *) &port->raddr, 0, sizeof(port->raddr)); memmove((char *) &(port->raddr.in.sin_addr),
memmove((char *) &(port->raddr.sin_addr), (char *) hp->h_addr,
(char *) hp->h_addr, hp->h_length);
hp->h_length); port->raddr.in.sin_family = AF_INET;
port->raddr.sin_family = AF_INET; port->raddr.in.sin_port = htons(portName);
port->raddr.sin_port = htons(portName); len = sizeof(struct sockaddr_in);
}
else
{
port->raddr.un.sun_family = AF_UNIX;
len = UNIXSOCK_PATH(port->raddr.un,portName);
}
/* connect to the server */ /* connect to the server */
if ((port->sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) if ((port->sock=socket(port->raddr.in.sin_family, SOCK_STREAM, 0)) < 0)
{ {
sprintf(PQerrormsg, sprintf(PQerrormsg,
"FATAL: StreamOpen: socket() failed: errno=%d\n", "FATAL: StreamOpen: socket() failed: errno=%d\n",
errno); errno);
fputs(PQerrormsg, stderr); fputs(PQerrormsg, stderr);
pqdebug("%s", PQerrormsg); pqdebug("%s", PQerrormsg);
return (STATUS_ERROR); return (STATUS_ERROR);
} }
if (connect(port->sock, (struct sockaddr *) & port->raddr, err = connect(port->sock, (struct sockaddr*) &port->raddr, len);
sizeof(port->raddr)) < 0) if (err < 0)
{ {
sprintf(PQerrormsg, sprintf(PQerrormsg,
"FATAL: StreamOpen: connect() failed: errno=%d\n", "FATAL: StreamOpen: connect() failed: errno=%d\n",
errno); errno);
fputs(PQerrormsg, stderr); fputs(PQerrormsg, stderr);
pqdebug("%s", PQerrormsg); pqdebug("%s", PQerrormsg);
@ -761,7 +790,7 @@ StreamOpen(char *hostName, short portName, Port *port)
/* fill in the client address */ /* fill in the client address */
if (getsockname(port->sock, (struct sockaddr *) & port->laddr, if (getsockname(port->sock, (struct sockaddr *) & port->laddr,
&laddrlen) < 0) &len) < 0)
{ {
sprintf(PQerrormsg, sprintf(PQerrormsg,
"FATAL: StreamOpen: getsockname() failed: errno=%d\n", "FATAL: StreamOpen: getsockname() failed: errno=%d\n",

View File

@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/libpq/Attic/pqpacket.c,v 1.8 1997/09/08 21:43:52 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/libpq/Attic/pqpacket.c,v 1.9 1997/11/07 20:51:36 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -85,7 +85,7 @@ PacketReceive(Port *port, /* receive port */
/* /*
* Assume port->nBytes is zero unless we were interrupted during * Assume port->nBytes is zero unless we were interrupted during
* non-blocking I/O. This first recvfrom() is to get the hdr * non-blocking I/O. This first recv() is to get the hdr
* information so we know how many bytes to read. Life would be very * information so we know how many bytes to read. Life would be very
* complicated if we read too much data (buffering). * complicated if we read too much data (buffering).
*/ */
@ -98,8 +98,7 @@ PacketReceive(Port *port, /* receive port */
else else
{ {
/* peeking into the incoming message */ /* peeking into the incoming message */
cc = recvfrom(port->sock, (char *) &(buf->len), hdrLen, flag, cc = recv(port->sock, (char *) &(buf->len), hdrLen, flag);
(struct sockaddr *) & (port->raddr), &addrLen);
if (cc < hdrLen) if (cc < hdrLen)
{ {
/* if cc is negative, the system call failed */ /* if cc is negative, the system call failed */
@ -179,8 +178,7 @@ PacketReceive(Port *port, /* receive port */
*/ */
while (packetLen) while (packetLen)
{ {
cc = recvfrom(port->sock, tmp, packetLen, 0, cc = read(port->sock, tmp, packetLen);
(struct sockaddr *) & (port->raddr), &addrLen);
if (cc < 0) if (cc < 0)
return (STATUS_ERROR); return (STATUS_ERROR);
@ -224,18 +222,13 @@ PacketSend(Port *port,
PacketLen len, PacketLen len,
bool nonBlocking) bool nonBlocking)
{ {
PacketLen totalLen; PacketLen doneLen;
int addrLen = sizeof(struct sockaddr_in);
Assert(!nonBlocking); Assert(!nonBlocking);
Assert(buf); Assert(buf);
totalLen = len; doneLen = write(port->sock, buf, len);
if (doneLen < len)
len = sendto(port->sock, (Addr) buf, totalLen, /* flags */ 0,
(struct sockaddr *) & (port->raddr), addrLen);
if (len < totalLen)
{ {
sprintf(PQerrormsg, sprintf(PQerrormsg,
"FATAL: PacketSend: couldn't send complete packet: errno=%d\n", "FATAL: PacketSend: couldn't send complete packet: errno=%d\n",

View File

@ -10,7 +10,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.59 1997/10/25 01:09:55 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.60 1997/11/07 20:51:47 momjian Exp $
* *
* NOTES * NOTES
* *
@ -131,7 +131,8 @@ static char *progname = (char *) NULL;
*/ */
static char Execfile[MAXPATHLEN] = ""; static char Execfile[MAXPATHLEN] = "";
static int ServerSock = INVALID_SOCK; /* stream socket server */ static int ServerSock_INET = INVALID_SOCK; /* stream socket server */
static int ServerSock_UNIX = INVALID_SOCK; /* stream socket server */
/* /*
* Set by the -o option * Set by the -o option
@ -148,6 +149,8 @@ static char ExtraOptions[ARGV_SIZE] = "";
static int Reinit = 1; static int Reinit = 1;
static int SendStop = 0; static int SendStop = 0;
static int NetServer = 0; /* if not zero, postmaster listen for
non-local connections */
static int MultiplexedBackends = 0; static int MultiplexedBackends = 0;
static int MultiplexedBackendPort; static int MultiplexedBackendPort;
@ -244,8 +247,8 @@ PostmasterMain(int argc, char *argv[])
char *hostName; char *hostName;
int status; int status;
int silentflag = 0; int silentflag = 0;
char hostbuf[MAXHOSTNAMELEN];
bool DataDirOK; /* We have a usable PGDATA value */ bool DataDirOK; /* We have a usable PGDATA value */
char hostbuf[MAXHOSTNAMELEN];
progname = argv[0]; progname = argv[0];
@ -267,7 +270,7 @@ PostmasterMain(int argc, char *argv[])
DataDir = getenv("PGDATA"); /* default value */ DataDir = getenv("PGDATA"); /* default value */
opterr = 0; opterr = 0;
while ((opt = getopt(argc, argv, "a:B:b:D:dm:Mno:p:Ss")) != EOF) while ((opt = getopt(argc, argv, "a:B:b:D:dim:Mno:p:Ss")) != EOF)
{ {
switch (opt) switch (opt)
{ {
@ -315,6 +318,9 @@ PostmasterMain(int argc, char *argv[])
else else
DebugLvl = 1; DebugLvl = 1;
break; break;
case 'i':
NetServer = 1;
break;
case 'm': case 'm':
MultiplexedBackends = 1; MultiplexedBackends = 1;
MultiplexedBackendPort = atoi(optarg); MultiplexedBackendPort = atoi(optarg);
@ -387,11 +393,20 @@ PostmasterMain(int argc, char *argv[])
exit(1); exit(1);
} }
if (NetServer)
status = StreamServerPort(hostName, PostPortName, &ServerSock); {
status = StreamServerPort(hostName, PostPortName, &ServerSock_INET);
if (status != STATUS_OK)
{
fprintf(stderr, "%s: cannot create INET stream port\n",
progname);
exit(1);
}
}
status = StreamServerPort(NULL, PostPortName, &ServerSock_UNIX);
if (status != STATUS_OK) if (status != STATUS_OK)
{ {
fprintf(stderr, "%s: cannot create stream port\n", fprintf(stderr, "%s: cannot create UNIX stream port\n",
progname); progname);
exit(1); exit(1);
} }
@ -459,6 +474,7 @@ usage(const char *progname)
fprintf(stderr, "\t-b backend\tuse a specific backend server executable\n"); fprintf(stderr, "\t-b backend\tuse a specific backend server executable\n");
fprintf(stderr, "\t-d [1|2|3]\tset debugging level\n"); fprintf(stderr, "\t-d [1|2|3]\tset debugging level\n");
fprintf(stderr, "\t-D datadir\tset data directory\n"); fprintf(stderr, "\t-D datadir\tset data directory\n");
fprintf(stderr, "\t-i \tlisten on TCP/IP sockets as well as Unix domain socket\n");
fprintf(stderr, "\t-m \tstart up multiplexing backends\n"); fprintf(stderr, "\t-m \tstart up multiplexing backends\n");
fprintf(stderr, "\t-n\t\tdon't reinitialize shared memory after abnormal exit\n"); fprintf(stderr, "\t-n\t\tdon't reinitialize shared memory after abnormal exit\n");
fprintf(stderr, "\t-o option\tpass 'option' to each backend servers\n"); fprintf(stderr, "\t-o option\tpass 'option' to each backend servers\n");
@ -471,13 +487,11 @@ usage(const char *progname)
static int static int
ServerLoop(void) ServerLoop(void)
{ {
int serverFd = ServerSock; fd_set rmask, basemask;
fd_set rmask,
basemask;
int nSockets, int nSockets,
nSelected, nSelected,
status, status,
newFd; oldFd, newFd;
Dlelem *next, Dlelem *next,
*curr; *curr;
@ -493,10 +507,16 @@ ServerLoop(void)
int orgsigmask = sigblock(0); int orgsigmask = sigblock(0);
#endif #endif
nSockets = ServerSock + 1;
FD_ZERO(&basemask); FD_ZERO(&basemask);
FD_SET(ServerSock, &basemask); FD_SET(ServerSock_UNIX, &basemask);
nSockets = ServerSock_UNIX;
if (ServerSock_INET != INVALID_SOCK)
{
FD_SET(ServerSock_INET, &basemask);
if (ServerSock_INET > ServerSock_UNIX)
nSockets = ServerSock_INET;
}
nSockets++;
#ifdef HAVE_SIGPROCMASK #ifdef HAVE_SIGPROCMASK
sigprocmask(0, 0, &oldsigmask); sigprocmask(0, 0, &oldsigmask);
@ -542,15 +562,21 @@ ServerLoop(void)
} }
/* new connection pending on our well-known port's socket */ /* new connection pending on our well-known port's socket */
if (FD_ISSET(ServerSock, &rmask)) oldFd = -1;
if (FD_ISSET(ServerSock_UNIX, &rmask))
oldFd = ServerSock_UNIX;
else if (ServerSock_INET != INVALID_SOCK &&
FD_ISSET(ServerSock_INET, &rmask))
oldFd = ServerSock_INET;
if (oldFd >= 0)
{ {
/* /*
* connect and make an addition to PortList. If the * connect and make an addition to PortList. If the
* connection dies and we notice it, just forget about the * connection dies and we notice it, just forget about the
* whole thing. * whole thing.
*/ */
if (ConnCreate(serverFd, &newFd) == STATUS_OK) if (ConnCreate(oldFd, &newFd) == STATUS_OK)
{ {
if (newFd >= nSockets) if (newFd >= nSockets)
nSockets = newFd + 1; nSockets = newFd + 1;
@ -560,8 +586,12 @@ ServerLoop(void)
fprintf(stderr, "%s: ServerLoop: connect on %d\n", fprintf(stderr, "%s: ServerLoop: connect on %d\n",
progname, newFd); progname, newFd);
} }
else if (DebugLvl)
fprintf(stderr,
"%s: ServerLoop: connect failed: (%d) %s\n",
progname, errno, strerror(errno));
--nSelected; --nSelected;
FD_CLR(ServerSock, &rmask); FD_CLR(oldFd, &rmask);
} }
if (DebugLvl > 1) if (DebugLvl > 1)
@ -793,7 +823,7 @@ ConnStartup(Port *port, int *status,
static void static void
send_error_reply(Port *port, const char *errormsg) send_error_reply(Port *port, const char *errormsg)
{ {
int rc; /* return code from sendto */ int rc; /* return code from write */
char *reply; char *reply;
/* /*
@ -812,7 +842,7 @@ send_error_reply(Port *port, const char *errormsg)
sprintf(reply, "E%s", errormsg); sprintf(reply, "E%s", errormsg);
rc = send(port->sock, (Addr) reply, strlen(reply) + 1, /* flags */ 0); rc = write(port->sock, (Addr) reply, strlen(reply) + 1);
if (rc < 0) if (rc < 0)
fprintf(stderr, fprintf(stderr,
"%s: ServerLoop:\t\t" "%s: ServerLoop:\t\t"
@ -1269,8 +1299,8 @@ ExitPostmaster(int status)
* Not sure of the semantics here. When the Postmaster dies, should * Not sure of the semantics here. When the Postmaster dies, should
* the backends all be killed? probably not. * the backends all be killed? probably not.
*/ */
if (ServerSock != INVALID_SOCK) if (ServerSock_INET != INVALID_SOCK) close(ServerSock_INET);
close(ServerSock); if (ServerSock_UNIX != INVALID_SOCK) close(ServerSock_UNIX);
exitpg(status); exitpg(status);
} }
@ -1285,9 +1315,10 @@ dumpstatus(SIGNAL_ARGS)
fprintf(stderr, "%s: dumpstatus:\n", progname); fprintf(stderr, "%s: dumpstatus:\n", progname);
fprintf(stderr, "\tsock %d: nBytes=%d, laddr=0x%lx, raddr=0x%lx\n", fprintf(stderr, "\tsock %d: nBytes=%d, laddr=0x%lx, raddr=0x%lx\n",
port->sock, port->nBytes, port->sock, port->nBytes,
(long int) port->laddr.sin_addr.s_addr, (long int) port->laddr.in.sin_addr.s_addr,
(long int) port->raddr.sin_addr.s_addr); (long int) port->raddr.in.sin_addr.s_addr);
curr = DLGetSucc(curr); curr = DLGetSucc(curr);
} }
} }

View File

@ -7,7 +7,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.51 1997/11/02 15:25:45 vadim Exp $ * $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.52 1997/11/07 20:51:54 momjian Exp $
* *
* NOTES * NOTES
* this is the "main" module of the postgres backend and * this is the "main" module of the postgres backend and
@ -832,7 +832,6 @@ PostgresMain(int argc, char *argv[])
bool multiplexedBackend; bool multiplexedBackend;
char *hostName; /* the host name of the backend server */ char *hostName; /* the host name of the backend server */
char hostbuf[MAXHOSTNAMELEN];
int serverSock; int serverSock;
int serverPortnum = 0; int serverPortnum = 0;
int nSelected; /* number of descriptors ready from int nSelected; /* number of descriptors ready from
@ -884,15 +883,10 @@ PostgresMain(int argc, char *argv[])
#endif #endif
/* /*
* get hostname is either the environment variable PGHOST or * get hostname is either the environment variable PGHOST or NULL
* 'localhost' * NULL means Unix-socket only
*/ */
if (!(hostName = getenv("PGHOST"))) hostName = getenv("PGHOST");
{
if (gethostname(hostbuf, MAXHOSTNAMELEN) < 0)
strcpy(hostbuf, "localhost");
hostName = hostbuf;
}
DataDir = getenv("PGDATA"); /* default */ DataDir = getenv("PGDATA"); /* default */
multiplexedBackend = false; /* default */ multiplexedBackend = false; /* default */
@ -1252,7 +1246,7 @@ PostgresMain(int argc, char *argv[])
if (multiplexedBackend) if (multiplexedBackend)
{ {
if (serverPortnum == 0 || if (serverPortnum == 0 ||
StreamServerPort(hostName, serverPortnum, &serverSock) != STATUS_OK) StreamServerPort(hostName, serverPortnum, &serverSock) != STATUS_OK)
{ {
fprintf(stderr, "Postgres: cannot create stream port %d\n", serverPortnum); fprintf(stderr, "Postgres: cannot create stream port %d\n", serverPortnum);
exit(1); exit(1);
@ -1343,7 +1337,7 @@ PostgresMain(int argc, char *argv[])
if (IsUnderPostmaster == false) if (IsUnderPostmaster == false)
{ {
puts("\nPOSTGRES backend interactive interface"); puts("\nPOSTGRES backend interactive interface");
puts("$Revision: 1.51 $ $Date: 1997/11/02 15:25:45 $"); puts("$Revision: 1.52 $ $Date: 1997/11/07 20:51:54 $");
} }
/* ---------------- /* ----------------

View File

@ -7,7 +7,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/init/postinit.c,v 1.15 1997/11/07 06:38:46 thomas Exp $ * $Header: /cvsroot/pgsql/src/backend/utils/init/postinit.c,v 1.16 1997/11/07 20:51:58 momjian Exp $
* *
* NOTES * NOTES
* InitPostgres() is the function called from PostgresMain * InitPostgres() is the function called from PostgresMain
@ -310,9 +310,6 @@ InitCommunication()
{ {
SystemPortAddress address = atoi(postport); SystemPortAddress address = atoi(postport);
if (address == 0)
elog(FATAL, "InitCommunication: invalid POSTPORT");
if (MyBackendTag == -1) if (MyBackendTag == -1)
elog(FATAL, "InitCommunication: missing POSTID"); elog(FATAL, "InitCommunication: missing POSTID");

View File

@ -6,7 +6,7 @@
* *
* Copyright (c) 1994, Regents of the University of California * Copyright (c) 1994, Regents of the University of California
* *
* $Id: pqcomm.h,v 1.15 1997/09/12 22:26:13 momjian Exp $ * $Id: pqcomm.h,v 1.16 1997/11/07 20:52:06 momjian Exp $
* *
* NOTES * NOTES
* Some of this should move to libpq.h * Some of this should move to libpq.h
@ -19,6 +19,7 @@
#include <stdio.h> #include <stdio.h>
#include <sys/types.h> #include <sys/types.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <sys/un.h>
/* /*
@ -27,6 +28,10 @@
#define PATH_SIZE 64 #define PATH_SIZE 64
#define ARGV_SIZE 64 #define ARGV_SIZE 64
#define UNIXSOCK_PATH(sun,port) \
sprintf(sun.sun_path,"/tmp/.s.PGSQL.%d",port) + sizeof(sun.sun_family) + 1;
/* The various kinds of startup messages are for the various kinds of /* The various kinds of startup messages are for the various kinds of
user authentication systems. In the beginning, there was only user authentication systems. In the beginning, there was only
STARTUP_MSG and all connections were unauthenticated. Now, there are STARTUP_MSG and all connections were unauthenticated. Now, there are
@ -106,9 +111,10 @@ typedef struct Port
int sock; /* file descriptor */ int sock; /* file descriptor */
int mask; /* select mask */ int mask; /* select mask */
int nBytes; /* nBytes read in so far */ int nBytes; /* nBytes read in so far */
struct sockaddr_in laddr; /* local addr (us) */ /* local addr (us) */
struct sockaddr_in raddr; /* remote addr (them) */ union { struct sockaddr_in in; struct sockaddr_un un; } laddr;
/* remote addr (them) */
union { struct sockaddr_in in; struct sockaddr_un un; } raddr;
/* /*
* PacketBufId id; * PacketBufId id;
*//* id of packet buf currently in use */ *//* id of packet buf currently in use */

View File

@ -7,7 +7,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.42 1997/09/18 20:22:46 momjian Exp $ * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.43 1997/11/07 20:52:15 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -86,7 +86,7 @@ static PQconninfoOption PQconninfoOptions[] = {
{"dbname", "PGDATABASE", NULL, NULL, {"dbname", "PGDATABASE", NULL, NULL,
"Database-Name", "", 20}, "Database-Name", "", 20},
{"host", "PGHOST", DefaultHost, NULL, {"host", "PGHOST", NULL, NULL,
"Database-Host", "", 40}, "Database-Host", "", 40},
{"port", "PGPORT", DEF_PGPORT, NULL, {"port", "PGPORT", DEF_PGPORT, NULL,
@ -340,11 +340,8 @@ PQsetdb(const char *pghost, const char *pgport, const char *pgoptions, const cha
if (!pghost || pghost[0] == '\0') if (!pghost || pghost[0] == '\0')
{ {
if (!(tmp = getenv("PGHOST"))) conn->pghost = NULL;
{ if (tmp = getenv("PGHOST")) conn->pghost = strdup(tmp);
tmp = DefaultHost;
}
conn->pghost = strdup(tmp);
} }
else else
conn->pghost = strdup(pghost); conn->pghost = strdup(pghost);
@ -473,7 +470,7 @@ connectDB(PGconn *conn)
MsgType msgtype; MsgType msgtype;
int laddrlen = sizeof(struct sockaddr); int laddrlen = sizeof(struct sockaddr);
Port *port = conn->port; Port *port = conn->port;
int portno; int portno, family, len;
/* /*
* Initialize the startup packet. * Initialize the startup packet.
@ -501,7 +498,8 @@ connectDB(PGconn *conn)
port = (Port *) malloc(sizeof(Port)); port = (Port *) malloc(sizeof(Port));
MemSet((char *) port, 0, sizeof(Port)); MemSet((char *) port, 0, sizeof(Port));
if (!(hp = gethostbyname(conn->pghost)) || hp->h_addrtype != AF_INET) if (conn->pghost &&
(!(hp = gethostbyname(conn->pghost)) || hp->h_addrtype != AF_INET))
{ {
(void) sprintf(conn->errorMessage, (void) sprintf(conn->errorMessage,
"connectDB() -- unknown hostname: %s\n", "connectDB() -- unknown hostname: %s\n",
@ -509,29 +507,37 @@ connectDB(PGconn *conn)
goto connect_errReturn; goto connect_errReturn;
} }
MemSet((char *) &port->raddr, 0, sizeof(port->raddr)); MemSet((char *) &port->raddr, 0, sizeof(port->raddr));
memmove((char *) &(port->raddr.sin_addr),
(char *) hp->h_addr,
hp->h_length);
port->raddr.sin_family = AF_INET;
portno = atoi(conn->pgport); portno = atoi(conn->pgport);
port->raddr.sin_port = htons((unsigned short) (portno)); port->raddr.in.sin_family = family = conn->pghost ? AF_INET : AF_UNIX;
if (family == AF_INET)
{
memmove((char *) &(port->raddr.in.sin_addr),
(char *) hp->h_addr,
hp->h_length);
port->raddr.in.sin_port = htons((unsigned short) (portno));
len = sizeof(struct sockaddr_in);
}
else
{
len = UNIXSOCK_PATH(port->raddr.un,portno);
}
/* connect to the server */ /* connect to the server */
if ((port->sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) if ((port->sock = socket(family, SOCK_STREAM, 0)) < 0)
{ {
(void) sprintf(conn->errorMessage, (void) sprintf(conn->errorMessage,
"connectDB() -- socket() failed: errno=%d\n%s\n", "connectDB() -- socket() failed: errno=%d\n%s\n",
errno, strerror(errno)); errno, strerror(errno));
goto connect_errReturn; goto connect_errReturn;
} }
if (connect(port->sock, (struct sockaddr *) & port->raddr, if (connect(port->sock, (struct sockaddr *) &port->raddr, len) < 0)
sizeof(port->raddr)) < 0)
{ {
(void) sprintf(conn->errorMessage, (void) sprintf(conn->errorMessage,
"connectDB() failed: Is the postmaster running at '%s' on port '%s'?\n", "connectDB() failed: Is the postmaster running at '%s' on port '%s'?\n",
conn->pghost, conn->pgport); conn->pghost ? conn->pghost : "UNIX Socket",
conn->pgport);
goto connect_errReturn; goto connect_errReturn;
} }
if (family == AF_INET)
{ {
struct protoent *pe; struct protoent *pe;
int on = 1; int on = 1;
@ -773,19 +779,11 @@ packetSend(Port *port,
PacketLen len, PacketLen len,
bool nonBlocking) bool nonBlocking)
{ {
PacketLen totalLen; PacketLen doneLen = write(port->sock, buf, len);
int addrLen = sizeof(struct sockaddr_in); if (doneLen < len)
totalLen = len;
len = sendto(port->sock, (Addr) buf, totalLen, /* flags */ 0,
(struct sockaddr *) & (port->raddr), addrLen);
if (len < totalLen)
{ {
return (STATUS_ERROR); return (STATUS_ERROR);
} }
return (STATUS_OK); return (STATUS_OK);
} }