Skip to content

Commit c17fa36

Browse files
committed
Add Unix domain socket support, from Goran Thyni, goran@bildbasen.se
1 parent d3cf6f9 commit c17fa36

File tree

9 files changed

+187
-136
lines changed

9 files changed

+187
-136
lines changed

src/backend/libpq/hba.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/libpq/hba.c,v 1.22 1997/09/08 21:43:45 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/libpq/hba.c,v 1.23 1997/11/07 20:51:27 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -855,8 +855,8 @@ authident(const char DataDir[],
855855

856856
/* The username returned by ident */
857857

858-
ident(port.raddr.sin_addr, port.laddr.sin_addr,
859-
port.raddr.sin_port, port.laddr.sin_port,
858+
ident(port.raddr.in.sin_addr, port.laddr.in.sin_addr,
859+
port.raddr.in.sin_port, port.laddr.in.sin_port,
860860
&ident_failed, ident_username);
861861

862862
if (ident_failed)
@@ -906,10 +906,13 @@ hba_recvauth(const Port *port, const char database[], const char user[],
906906
*/
907907
int retvalue;
908908

909+
/* UNIX socket always OK, for now */
910+
if(port->raddr.in.sin_family == AF_UNIX)
911+
return STATUS_OK;
909912
/* Our eventual return value */
913+
910914

911-
912-
find_hba_entry(DataDir, port->raddr.sin_addr, database,
915+
find_hba_entry(DataDir, port->raddr.in.sin_addr, database,
913916
&host_ok, &userauth, usermap_name,
914917
false /* don't find password entries of type
915918
'password' */ );

src/backend/libpq/password.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ verify_password(char *user, char *password, Port *port,
2626
*test_pw;
2727
char salt[3];
2828

29-
find_hba_entry(DataDir, port->raddr.sin_addr, database,
29+
find_hba_entry(DataDir, port->raddr.in.sin_addr, database,
3030
&host_ok, &userauth, pw_file_name, true);
3131

3232
if (!host_ok)

src/backend/libpq/pqcomm.c

Lines changed: 70 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/libpq/pqcomm.c,v 1.23 1997/09/18 20:20:39 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/libpq/pqcomm.c,v 1.24 1997/11/07 20:51:34 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -571,20 +571,27 @@ pq_async_notify()
571571
*
572572
* RETURNS: STATUS_OK or STATUS_ERROR
573573
*/
574+
575+
static char sock_path[100] = "";
576+
577+
static void do_unlink()
578+
{
579+
if (sock_path[0]) unlink(sock_path);
580+
}
581+
574582
int
575583
StreamServerPort(char *hostName, short portName, int *fdP)
576584
{
577585
struct sockaddr_in sin;
578-
int fd;
586+
struct sockaddr_un sun;
587+
int fd, err, family;
579588
int one = 1;
580589

581-
582-
if (!hostName)
583-
hostName = "localhost";
590+
family = hostName != NULL ? AF_INET : AF_UNIX;
584591

585592
MemSet((char *) &sin, 0, sizeof sin);
586593

587-
if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
594+
if ((fd = socket(family, SOCK_STREAM, 0)) < 0)
588595
{
589596
sprintf(PQerrormsg,
590597
"FATAL: StreamServerPort: socket() failed: errno=%d\n",
@@ -593,7 +600,7 @@ StreamServerPort(char *hostName, short portName, int *fdP)
593600
pqdebug("%s", PQerrormsg);
594601
return (STATUS_ERROR);
595602
}
596-
603+
if (family == AF_UNIX) on_exitpg(do_unlink, (caddr_t) 0);
597604
if ((setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one,
598605
sizeof(one))) == -1)
599606
{
@@ -604,11 +611,24 @@ StreamServerPort(char *hostName, short portName, int *fdP)
604611
pqdebug("%s", PQerrormsg);
605612
return (STATUS_ERROR);
606613
}
607-
608-
sin.sin_family = AF_INET;
609-
sin.sin_port = htons(portName);
610-
611-
if (bind(fd, (struct sockaddr *) & sin, sizeof sin) < 0)
614+
if (family == AF_UNIX)
615+
{
616+
size_t len;
617+
bzero(&sun, sizeof(sun));
618+
sun.sun_family = family;
619+
len = UNIXSOCK_PATH(sun,portName);
620+
strcpy(sock_path, sun.sun_path);
621+
err = bind(fd, (struct sockaddr *) &sun, len);
622+
}
623+
else
624+
{
625+
bzero(&sin, sizeof(sin));
626+
sin.sin_family = family;
627+
sin.sin_addr.s_addr = htonl(INADDR_ANY);
628+
sin.sin_port = htons(portName);
629+
err = bind(fd, (struct sockaddr *) &sin, sizeof sin);
630+
}
631+
if (err < 0)
612632
{
613633
sprintf(PQerrormsg,
614634
"FATAL: StreamServerPort: bind() failed: errno=%d\n",
@@ -645,26 +665,30 @@ StreamServerPort(char *hostName, short portName, int *fdP)
645665
int
646666
StreamConnection(int server_fd, Port *port)
647667
{
648-
int addrlen;
668+
int len, addrlen;
669+
int family = port->raddr.in.sin_family;
649670

650671
/* accept connection (and fill in the client (remote) address) */
651-
addrlen = sizeof(struct sockaddr_in);
672+
len = family == AF_INET ?
673+
sizeof(struct sockaddr_in) : sizeof(struct sockaddr_un);
674+
addrlen = len;
652675
if ((port->sock = accept(server_fd,
653-
(struct sockaddr *) & port->raddr,
654-
&addrlen)) < 0)
676+
(struct sockaddr *) & port->raddr,
677+
&addrlen)) < 0)
655678
{
656679
elog(WARN, "postmaster: StreamConnection: accept: %m");
657680
return (STATUS_ERROR);
658681
}
659-
682+
660683
/* fill in the server (local) address */
661-
addrlen = sizeof(struct sockaddr_in);
684+
addrlen = len;
662685
if (getsockname(port->sock, (struct sockaddr *) & port->laddr,
663686
&addrlen) < 0)
664687
{
665688
elog(WARN, "postmaster: StreamConnection: getsockname: %m");
666689
return (STATUS_ERROR);
667690
}
691+
if (family == AF_INET)
668692
{
669693
struct protoent *pe;
670694
int on = 1;
@@ -714,45 +738,50 @@ StreamClose(int sock)
714738
int
715739
StreamOpen(char *hostName, short portName, Port *port)
716740
{
741+
int len, err;
717742
struct hostent *hp;
718-
int laddrlen = sizeof(struct sockaddr_in);
719743
extern int errno;
720-
721-
if (!hostName)
722-
hostName = "localhost";
723-
744+
724745
/* set up the server (remote) address */
725-
if (!(hp = gethostbyname(hostName)) || hp->h_addrtype != AF_INET)
726-
{
746+
MemSet((char *) &port->raddr, 0, sizeof(port->raddr));
747+
if (hostName)
748+
{
749+
if (!(hp = gethostbyname(hostName)) || hp->h_addrtype != AF_INET)
750+
{
727751
sprintf(PQerrormsg,
728-
"FATAL: StreamOpen: unknown hostname: %s\n",
729-
hostName);
752+
"FATAL: StreamOpen: unknown hostname: %s\n",
753+
hostName);
730754
fputs(PQerrormsg, stderr);
731755
pqdebug("%s", PQerrormsg);
732756
return (STATUS_ERROR);
733-
}
734-
MemSet((char *) &port->raddr, 0, sizeof(port->raddr));
735-
memmove((char *) &(port->raddr.sin_addr),
736-
(char *) hp->h_addr,
737-
hp->h_length);
738-
port->raddr.sin_family = AF_INET;
739-
port->raddr.sin_port = htons(portName);
740-
757+
}
758+
memmove((char *) &(port->raddr.in.sin_addr),
759+
(char *) hp->h_addr,
760+
hp->h_length);
761+
port->raddr.in.sin_family = AF_INET;
762+
port->raddr.in.sin_port = htons(portName);
763+
len = sizeof(struct sockaddr_in);
764+
}
765+
else
766+
{
767+
port->raddr.un.sun_family = AF_UNIX;
768+
len = UNIXSOCK_PATH(port->raddr.un,portName);
769+
}
741770
/* connect to the server */
742-
if ((port->sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
771+
if ((port->sock=socket(port->raddr.in.sin_family, SOCK_STREAM, 0)) < 0)
743772
{
744773
sprintf(PQerrormsg,
745-
"FATAL: StreamOpen: socket() failed: errno=%d\n",
774+
"FATAL: StreamOpen: socket() failed: errno=%d\n",
746775
errno);
747776
fputs(PQerrormsg, stderr);
748777
pqdebug("%s", PQerrormsg);
749778
return (STATUS_ERROR);
750779
}
751-
if (connect(port->sock, (struct sockaddr *) & port->raddr,
752-
sizeof(port->raddr)) < 0)
780+
err = connect(port->sock, (struct sockaddr*) &port->raddr, len);
781+
if (err < 0)
753782
{
754783
sprintf(PQerrormsg,
755-
"FATAL: StreamOpen: connect() failed: errno=%d\n",
784+
"FATAL: StreamOpen: connect() failed: errno=%d\n",
756785
errno);
757786
fputs(PQerrormsg, stderr);
758787
pqdebug("%s", PQerrormsg);
@@ -761,7 +790,7 @@ StreamOpen(char *hostName, short portName, Port *port)
761790

762791
/* fill in the client address */
763792
if (getsockname(port->sock, (struct sockaddr *) & port->laddr,
764-
&laddrlen) < 0)
793+
&len) < 0)
765794
{
766795
sprintf(PQerrormsg,
767796
"FATAL: StreamOpen: getsockname() failed: errno=%d\n",

src/backend/libpq/pqpacket.c

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/libpq/Attic/pqpacket.c,v 1.8 1997/09/08 21:43:52 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/libpq/Attic/pqpacket.c,v 1.9 1997/11/07 20:51:36 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -85,7 +85,7 @@ PacketReceive(Port *port, /* receive port */
8585

8686
/*
8787
* Assume port->nBytes is zero unless we were interrupted during
88-
* non-blocking I/O. This first recvfrom() is to get the hdr
88+
* non-blocking I/O. This first recv() is to get the hdr
8989
* information so we know how many bytes to read. Life would be very
9090
* complicated if we read too much data (buffering).
9191
*/
@@ -98,8 +98,7 @@ PacketReceive(Port *port, /* receive port */
9898
else
9999
{
100100
/* peeking into the incoming message */
101-
cc = recvfrom(port->sock, (char *) &(buf->len), hdrLen, flag,
102-
(struct sockaddr *) & (port->raddr), &addrLen);
101+
cc = recv(port->sock, (char *) &(buf->len), hdrLen, flag);
103102
if (cc < hdrLen)
104103
{
105104
/* if cc is negative, the system call failed */
@@ -179,8 +178,7 @@ PacketReceive(Port *port, /* receive port */
179178
*/
180179
while (packetLen)
181180
{
182-
cc = recvfrom(port->sock, tmp, packetLen, 0,
183-
(struct sockaddr *) & (port->raddr), &addrLen);
181+
cc = read(port->sock, tmp, packetLen);
184182
if (cc < 0)
185183
return (STATUS_ERROR);
186184

@@ -224,18 +222,13 @@ PacketSend(Port *port,
224222
PacketLen len,
225223
bool nonBlocking)
226224
{
227-
PacketLen totalLen;
228-
int addrLen = sizeof(struct sockaddr_in);
225+
PacketLen doneLen;
229226

230227
Assert(!nonBlocking);
231228
Assert(buf);
232229

233-
totalLen = len;
234-
235-
len = sendto(port->sock, (Addr) buf, totalLen, /* flags */ 0,
236-
(struct sockaddr *) & (port->raddr), addrLen);
237-
238-
if (len < totalLen)
230+
doneLen = write(port->sock, buf, len);
231+
if (doneLen < len)
239232
{
240233
sprintf(PQerrormsg,
241234
"FATAL: PacketSend: couldn't send complete packet: errno=%d\n",

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy