1997-03-18 21:15:39 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* pqcomm.c--
|
1997-09-07 07:04:48 +02:00
|
|
|
* Communication functions between the Frontend and the Backend
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
1998-09-01 05:29:17 +02:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/libpq/pqcomm.c,v 1.52 1998/09/01 03:22:50 momjian Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* INTERFACE ROUTINES
|
1997-09-07 07:04:48 +02:00
|
|
|
* pq_gettty - return the name of the tty in the given buffer
|
|
|
|
* pq_getport - return the PGPORT setting
|
|
|
|
* pq_close - close input / output connections
|
|
|
|
* pq_flush - flush pending output
|
|
|
|
* pq_getstr - get a null terminated string from connection
|
|
|
|
* pq_getnchar - get n characters from connection
|
|
|
|
* pq_getint - get an integer from connection
|
|
|
|
* pq_putstr - send a null terminated string to connection
|
|
|
|
* pq_putnchar - send n characters to connection
|
|
|
|
* pq_putint - send an integer to connection
|
1998-06-16 09:29:54 +02:00
|
|
|
* pq_putncharlen - send n characters to connection
|
|
|
|
* (also send an int header indicating
|
|
|
|
* the length)
|
1997-09-07 07:04:48 +02:00
|
|
|
* pq_getinaddr - initialize address from host and port number
|
|
|
|
* pq_getinserv - initialize address from host and service name
|
|
|
|
* pq_connect - create remote input / output connection
|
|
|
|
* pq_accept - accept remote input / output connection
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* NOTES
|
1997-09-07 07:04:48 +02:00
|
|
|
* These functions are used by both frontend applications and
|
|
|
|
* the postgres backend.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*/
|
1998-02-24 05:02:20 +01:00
|
|
|
#include "postgres.h"
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
#include <stdio.h>
|
1998-02-24 05:02:20 +01:00
|
|
|
#if defined(HAVE_STRING_H)
|
1998-02-26 05:46:47 +01:00
|
|
|
#include <string.h>
|
1998-02-24 05:02:20 +01:00
|
|
|
#else
|
1998-02-26 05:46:47 +01:00
|
|
|
#include <strings.h>
|
1998-02-24 05:02:20 +01:00
|
|
|
#endif
|
1996-12-26 23:08:34 +01:00
|
|
|
#include <signal.h>
|
1996-11-06 09:48:33 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
1997-09-07 07:04:48 +02:00
|
|
|
#include <unistd.h> /* for ttyname() */
|
1996-07-09 08:22:35 +02:00
|
|
|
#include <sys/types.h>
|
1997-11-21 19:12:58 +01:00
|
|
|
#include <sys/stat.h>
|
1996-07-09 08:22:35 +02:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <netinet/in.h>
|
1997-04-16 08:25:13 +02:00
|
|
|
#include <netinet/tcp.h>
|
1996-11-06 09:48:33 +01:00
|
|
|
#include <arpa/inet.h>
|
1998-08-25 23:32:10 +02:00
|
|
|
#include <sys/file.h>
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1996-10-31 11:17:09 +01:00
|
|
|
#if defined(linux)
|
1996-07-09 08:22:35 +02:00
|
|
|
#ifndef SOMAXCONN
|
1997-09-07 07:04:48 +02:00
|
|
|
#define SOMAXCONN 5 /* from Linux listen(2) man page */
|
|
|
|
#endif /* SOMAXCONN */
|
|
|
|
#endif /* linux */
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1998-02-24 05:02:20 +01:00
|
|
|
#include "miscadmin.h"
|
|
|
|
#include "libpq/pqsignal.h"
|
|
|
|
#include "libpq/auth.h"
|
|
|
|
#include "libpq/libpq.h" /* where the declarations go */
|
|
|
|
#include "storage/ipc.h"
|
1998-07-18 20:34:34 +02:00
|
|
|
#ifdef MULTIBYTE
|
1998-07-26 06:31:41 +02:00
|
|
|
#include "mb/pg_wchar.h"
|
1998-06-16 09:29:54 +02:00
|
|
|
#endif
|
1998-08-25 23:32:10 +02:00
|
|
|
#include "utils/trace.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* declarations
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
1997-09-08 04:41:22 +02:00
|
|
|
FILE *Pfout,
|
|
|
|
*Pfin;
|
|
|
|
FILE *Pfdebug; /* debugging libpq */
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
/* --------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* pq_init - open portal file descriptors
|
1996-07-09 08:22:35 +02:00
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
pq_init(int fd)
|
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
Pfin = fdopen(fd, "r");
|
|
|
|
Pfout = fdopen(dup(fd), "w");
|
|
|
|
if (!Pfin || !Pfout)
|
|
|
|
elog(FATAL, "pq_init: Couldn't initialize socket connection");
|
|
|
|
PQnotifies_init();
|
|
|
|
if (getenv("LIBPQ_DEBUG"))
|
|
|
|
Pfdebug = stderr;
|
|
|
|
else
|
|
|
|
Pfdebug = NULL;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* pq_getc(File* fin)
|
|
|
|
*
|
|
|
|
* get a character from the input file,
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* if Pfdebug is set, also echo the character fetched into Pfdebug
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* used for debugging libpq
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
static int
|
1997-09-08 23:56:23 +02:00
|
|
|
pq_getc(FILE *fin)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int c;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
c = getc(fin);
|
|
|
|
if (Pfdebug && c != EOF)
|
|
|
|
putc(c, Pfdebug);
|
|
|
|
return c;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* pq_gettty - return the name of the tty in the given buffer
|
1996-07-09 08:22:35 +02:00
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
pq_gettty(char *tp)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
|
|
|
strncpy(tp, ttyname(0), 19);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* pq_getport - return the PGPORT setting
|
1996-07-09 08:22:35 +02:00
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
pq_getport()
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
char *envport = getenv("PGPORT");
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (envport)
|
1998-09-01 05:29:17 +02:00
|
|
|
return atoi(envport);
|
|
|
|
return atoi(DEF_PGPORT);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* pq_close - close input / output connections
|
1996-07-09 08:22:35 +02:00
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
pq_close()
|
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
if (Pfin)
|
|
|
|
{
|
|
|
|
fclose(Pfin);
|
|
|
|
Pfin = NULL;
|
|
|
|
}
|
|
|
|
if (Pfout)
|
|
|
|
{
|
|
|
|
fclose(Pfout);
|
|
|
|
Pfout = NULL;
|
|
|
|
}
|
|
|
|
PQnotifies_init();
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* pq_flush - flush pending output
|
1996-07-09 08:22:35 +02:00
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
pq_flush()
|
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
if (Pfout)
|
|
|
|
fflush(Pfout);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* pq_getstr - get a null terminated string from connection
|
1996-07-09 08:22:35 +02:00
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
pq_getstr(char *s, int maxlen)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int c = '\0';
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-07-18 20:34:34 +02:00
|
|
|
#ifdef MULTIBYTE
|
1998-06-16 09:29:54 +02:00
|
|
|
unsigned char *p, *ps;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
ps = s;
|
|
|
|
len = maxlen;
|
|
|
|
#endif
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if (Pfin == (FILE *) NULL)
|
|
|
|
{
|
|
|
|
/* elog(DEBUG, "Input descriptor is null"); */
|
1998-09-01 05:29:17 +02:00
|
|
|
return EOF;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
while (maxlen-- && (c = pq_getc(Pfin)) != EOF && c)
|
|
|
|
*s++ = c;
|
|
|
|
*s = '\0';
|
|
|
|
|
1998-07-18 20:34:34 +02:00
|
|
|
#ifdef MULTIBYTE
|
1998-06-16 09:29:54 +02:00
|
|
|
p = pg_client_to_server(ps, len);
|
|
|
|
if (ps != p) { /* actual conversion has been done? */
|
|
|
|
strcpy(ps, p);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/* -----------------
|
|
|
|
* If EOF reached let caller know.
|
|
|
|
* (This will only happen if we hit EOF before the string
|
|
|
|
* delimiter is reached.)
|
|
|
|
* -----------------
|
|
|
|
*/
|
|
|
|
if (c == EOF)
|
1998-09-01 05:29:17 +02:00
|
|
|
return EOF;
|
|
|
|
return !EOF;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* USER FUNCTION - gets a newline-terminated string from the backend.
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* Chiefly here so that applications can use "COPY <rel> to stdout"
|
1997-09-07 07:04:48 +02:00
|
|
|
* and read the output string. Returns a null-terminated string in s.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* PQgetline reads up to maxlen-1 characters (like fgets(3)) but strips
|
|
|
|
* the terminating \n (like gets(3)).
|
|
|
|
*
|
|
|
|
* RETURNS:
|
1997-09-07 07:04:48 +02:00
|
|
|
* EOF if it is detected or invalid arguments are given
|
|
|
|
* 0 if EOL is reached (i.e., \n has been read)
|
|
|
|
* (this is required for backward-compatibility -- this
|
|
|
|
* routine used to always return EOF or 0, assuming that
|
|
|
|
* the line ended within maxlen bytes.)
|
|
|
|
* 1 in other cases
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
int
|
|
|
|
PQgetline(char *s, int maxlen)
|
|
|
|
{
|
|
|
|
if (!Pfin || !s || maxlen <= 1)
|
1998-09-01 05:29:17 +02:00
|
|
|
return EOF;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (fgets(s, maxlen - 1, Pfin) == NULL)
|
|
|
|
return feof(Pfin) ? EOF : 1;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (; *s; s++)
|
|
|
|
{
|
|
|
|
if (*s == '\n')
|
|
|
|
{
|
|
|
|
*s = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* USER FUNCTION - sends a string to the backend.
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* Chiefly here so that applications can use "COPY <rel> from stdin".
|
|
|
|
*
|
|
|
|
* RETURNS:
|
1997-09-07 07:04:48 +02:00
|
|
|
* 0 in all cases.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
PQputline(char *s)
|
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
if (Pfout)
|
|
|
|
{
|
|
|
|
fputs(s, Pfout);
|
|
|
|
fflush(Pfout);
|
|
|
|
}
|
1998-09-01 05:29:17 +02:00
|
|
|
return 0;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* pq_getnchar - get n characters from connection
|
1996-07-09 08:22:35 +02:00
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
pq_getnchar(char *s, int off, int maxlen)
|
|
|
|
{
|
1997-03-18 21:15:39 +01:00
|
|
|
return pqGetNBytes(s + off, maxlen, Pfin);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* pq_getint - get an integer from connection
|
|
|
|
* we receive an integer a byte at a type and reconstruct it so that
|
|
|
|
* machines with different ENDIAN representations can talk to each
|
|
|
|
* other
|
1996-07-09 08:22:35 +02:00
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
pq_getint(int b)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int n,
|
|
|
|
status = 1;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (!Pfin)
|
|
|
|
return EOF;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* mjl: Seems inconsisten w/ return value of pq_putint (void). Also,
|
|
|
|
* EOF is a valid return value for an int! XXX
|
|
|
|
*/
|
|
|
|
|
|
|
|
switch (b)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
case 1:
|
|
|
|
status = ((n = fgetc(Pfin)) == EOF);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
status = pqGetShort(&n, Pfin);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
status = pqGetLong(&n, Pfin);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "** Unsupported size %d\n", b);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (status)
|
|
|
|
{
|
|
|
|
sprintf(PQerrormsg,
|
|
|
|
"FATAL: pq_getint failed: errno=%d\n", errno);
|
|
|
|
fputs(PQerrormsg, stderr);
|
|
|
|
pqdebug("%s", PQerrormsg);
|
|
|
|
n = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* pq_putstr - send a null terminated string to connection
|
1996-07-09 08:22:35 +02:00
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
pq_putstr(char *s)
|
|
|
|
{
|
1998-07-18 20:34:34 +02:00
|
|
|
#ifdef MULTIBYTE
|
1998-06-16 09:29:54 +02:00
|
|
|
unsigned char *p;
|
|
|
|
|
|
|
|
p = pg_server_to_client(s, strlen(s));
|
|
|
|
if (pqPutString(p, Pfout))
|
|
|
|
#else
|
1997-09-07 07:04:48 +02:00
|
|
|
if (pqPutString(s, Pfout))
|
1998-06-16 09:29:54 +02:00
|
|
|
#endif
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
|
|
|
sprintf(PQerrormsg,
|
|
|
|
"FATAL: pq_putstr: fputs() failed: errno=%d\n", errno);
|
|
|
|
fputs(PQerrormsg, stderr);
|
|
|
|
pqdebug("%s", PQerrormsg);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* pq_putnchar - send n characters to connection
|
1996-07-09 08:22:35 +02:00
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
pq_putnchar(char *s, int n)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
|
|
|
if (pqPutNBytes(s, n, Pfout))
|
1997-03-18 21:15:39 +01:00
|
|
|
{
|
1997-08-13 00:55:25 +02:00
|
|
|
sprintf(PQerrormsg,
|
1997-09-07 07:04:48 +02:00
|
|
|
"FATAL: pq_putnchar: fputc() failed: errno=%d\n",
|
|
|
|
errno);
|
1996-07-09 08:22:35 +02:00
|
|
|
fputs(PQerrormsg, stderr);
|
|
|
|
pqdebug("%s", PQerrormsg);
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
/* --------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* pq_putint - send an integer to connection
|
|
|
|
* we chop an integer into bytes and send individual bytes
|
|
|
|
* machines with different ENDIAN representations can still talk to each
|
|
|
|
* other
|
1996-07-09 08:22:35 +02:00
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
pq_putint(int i, int b)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int status;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (!Pfout)
|
|
|
|
return;
|
|
|
|
|
|
|
|
status = 1;
|
|
|
|
switch (b)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
case 1:
|
|
|
|
status = (fputc(i, Pfout) == EOF);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
status = pqPutShort(i, Pfout);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
status = pqPutLong(i, Pfout);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "** Unsupported size %d\n", b);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (status)
|
|
|
|
{
|
1997-08-13 00:55:25 +02:00
|
|
|
sprintf(PQerrormsg,
|
1997-09-07 07:04:48 +02:00
|
|
|
"FATAL: pq_putint failed: errno=%d\n", errno);
|
1996-07-09 08:22:35 +02:00
|
|
|
fputs(PQerrormsg, stderr);
|
|
|
|
pqdebug("%s", PQerrormsg);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* pq_getinaddr - initialize address from host and port number
|
1996-07-09 08:22:35 +02:00
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
int
|
1997-09-07 07:04:48 +02:00
|
|
|
pq_getinaddr(struct sockaddr_in * sin,
|
|
|
|
char *host,
|
|
|
|
int port)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
struct hostent *hs;
|
|
|
|
|
1997-09-18 22:22:58 +02:00
|
|
|
MemSet((char *) sin, 0, sizeof(*sin));
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (host)
|
|
|
|
{
|
|
|
|
if (*host >= '0' && *host <= '9')
|
|
|
|
sin->sin_addr.s_addr = inet_addr(host);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!(hs = gethostbyname(host)))
|
|
|
|
{
|
|
|
|
perror(host);
|
1998-09-01 05:29:17 +02:00
|
|
|
return 1;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
if (hs->h_addrtype != AF_INET)
|
|
|
|
{
|
|
|
|
sprintf(PQerrormsg,
|
|
|
|
"FATAL: pq_getinaddr: %s not on Internet\n",
|
|
|
|
host);
|
|
|
|
fputs(PQerrormsg, stderr);
|
|
|
|
pqdebug("%s", PQerrormsg);
|
1998-09-01 05:29:17 +02:00
|
|
|
return 1;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
memmove((char *) &sin->sin_addr,
|
|
|
|
hs->h_addr,
|
|
|
|
hs->h_length);
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
sin->sin_family = AF_INET;
|
|
|
|
sin->sin_port = htons(port);
|
1998-09-01 05:29:17 +02:00
|
|
|
return 0;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* pq_getinserv - initialize address from host and servive name
|
1996-07-09 08:22:35 +02:00
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
int
|
1997-09-07 07:04:48 +02:00
|
|
|
pq_getinserv(struct sockaddr_in * sin, char *host, char *serv)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
struct servent *ss;
|
|
|
|
|
|
|
|
if (*serv >= '0' && *serv <= '9')
|
1998-09-01 05:29:17 +02:00
|
|
|
return pq_getinaddr(sin, host, atoi(serv));
|
1997-09-07 07:04:48 +02:00
|
|
|
if (!(ss = getservbyname(serv, NULL)))
|
|
|
|
{
|
|
|
|
sprintf(PQerrormsg,
|
|
|
|
"FATAL: pq_getinserv: unknown service: %s\n",
|
|
|
|
serv);
|
|
|
|
fputs(PQerrormsg, stderr);
|
|
|
|
pqdebug("%s", PQerrormsg);
|
1998-09-01 05:29:17 +02:00
|
|
|
return 1;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1998-09-01 05:29:17 +02:00
|
|
|
return pq_getinaddr(sin, host, ntohs(ss->s_port));
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Streams -- wrapper around Unix socket system calls
|
|
|
|
*
|
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* Stream functions are used for vanilla TCP connection protocol.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* StreamServerPort -- open a sock stream "listening" port.
|
|
|
|
*
|
|
|
|
* This initializes the Postmaster's connection
|
1997-09-07 07:04:48 +02:00
|
|
|
* accepting port.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* ASSUME: that this doesn't need to be non-blocking because
|
1997-09-07 07:04:48 +02:00
|
|
|
* the Postmaster uses select() to tell when the socket
|
|
|
|
* is ready.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* RETURNS: STATUS_OK or STATUS_ERROR
|
|
|
|
*/
|
1997-11-07 21:52:15 +01:00
|
|
|
|
1998-02-26 05:46:47 +01:00
|
|
|
static char sock_path[MAXPGPATH + 1] = "";
|
1997-11-07 21:52:15 +01:00
|
|
|
|
1997-12-16 16:58:14 +01:00
|
|
|
/* do_unlink()
|
|
|
|
* Shutdown routine for backend connection
|
|
|
|
* If a Unix socket is used for communication, explicitly close it.
|
|
|
|
*/
|
1998-03-02 06:42:15 +01:00
|
|
|
void
|
|
|
|
StreamDoUnlink()
|
1997-11-07 21:52:15 +01:00
|
|
|
{
|
1998-05-29 19:00:34 +02:00
|
|
|
Assert(sock_path[0]);
|
|
|
|
unlink(sock_path);
|
1997-11-07 21:52:15 +01:00
|
|
|
}
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
int
|
|
|
|
StreamServerPort(char *hostName, short portName, int *fdP)
|
|
|
|
{
|
1998-02-26 05:46:47 +01:00
|
|
|
SockAddr saddr;
|
1997-11-10 06:16:00 +01:00
|
|
|
int fd,
|
|
|
|
err,
|
|
|
|
family;
|
|
|
|
size_t len;
|
1997-09-08 04:41:22 +02:00
|
|
|
int one = 1;
|
1998-08-25 23:32:10 +02:00
|
|
|
int lock_fd;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1997-11-17 17:17:14 +01:00
|
|
|
family = ((hostName != NULL) ? AF_INET : AF_UNIX);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1997-11-07 21:52:15 +01:00
|
|
|
if ((fd = socket(family, SOCK_STREAM, 0)) < 0)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
|
|
|
sprintf(PQerrormsg,
|
|
|
|
"FATAL: StreamServerPort: socket() failed: errno=%d\n",
|
|
|
|
errno);
|
|
|
|
fputs(PQerrormsg, stderr);
|
|
|
|
pqdebug("%s", PQerrormsg);
|
1998-09-01 05:29:17 +02:00
|
|
|
return STATUS_ERROR;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
if ((setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one,
|
|
|
|
sizeof(one))) == -1)
|
|
|
|
{
|
|
|
|
sprintf(PQerrormsg,
|
|
|
|
"FATAL: StreamServerPort: setsockopt (SO_REUSEADDR) failed: errno=%d\n",
|
|
|
|
errno);
|
|
|
|
fputs(PQerrormsg, stderr);
|
|
|
|
pqdebug("%s", PQerrormsg);
|
1998-09-01 05:29:17 +02:00
|
|
|
return STATUS_ERROR;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1998-07-09 05:29:11 +02:00
|
|
|
MemSet((char *) &saddr, 0, sizeof(saddr));
|
1998-01-26 02:42:53 +01:00
|
|
|
saddr.sa.sa_family = family;
|
1997-11-07 21:52:15 +01:00
|
|
|
if (family == AF_UNIX)
|
1997-11-10 06:16:00 +01:00
|
|
|
{
|
|
|
|
len = UNIXSOCK_PATH(saddr.un, portName);
|
|
|
|
strcpy(sock_path, saddr.un.sun_path);
|
1998-08-25 23:32:10 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If the socket exists but nobody has an advisory lock on it
|
|
|
|
* we can safely delete the file.
|
|
|
|
*/
|
|
|
|
if ((lock_fd = open(sock_path, O_RDONLY|O_NONBLOCK, 0666)) >= 0) {
|
|
|
|
if (flock(lock_fd, LOCK_EX|LOCK_NB) == 0) {
|
|
|
|
TPRINTF(TRACE_VERBOSE, "flock on %s, deleting", sock_path);
|
|
|
|
unlink(sock_path);
|
|
|
|
} else {
|
|
|
|
TPRINTF(TRACE_VERBOSE, "flock failed for %s", sock_path);
|
|
|
|
}
|
|
|
|
close(lock_fd);
|
|
|
|
}
|
1997-11-10 06:16:00 +01:00
|
|
|
}
|
1997-11-07 21:52:15 +01:00
|
|
|
else
|
1997-11-10 06:16:00 +01:00
|
|
|
{
|
|
|
|
saddr.in.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
saddr.in.sin_port = htons(portName);
|
1998-02-26 05:46:47 +01:00
|
|
|
len = sizeof(struct sockaddr_in);
|
1997-11-10 06:16:00 +01:00
|
|
|
}
|
1998-01-26 02:42:53 +01:00
|
|
|
err = bind(fd, &saddr.sa, len);
|
1997-11-07 21:52:15 +01:00
|
|
|
if (err < 0)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1998-02-26 05:46:47 +01:00
|
|
|
sprintf(PQerrormsg,
|
|
|
|
"FATAL: StreamServerPort: bind() failed: errno=%d\n",
|
|
|
|
errno);
|
|
|
|
pqdebug("%s", PQerrormsg);
|
1998-08-25 23:32:10 +02:00
|
|
|
strcat(PQerrormsg,
|
|
|
|
"\tIs another postmaster already running on that port?\n");
|
1998-02-26 05:46:47 +01:00
|
|
|
if (family == AF_UNIX)
|
1998-08-25 23:32:10 +02:00
|
|
|
sprintf(PQerrormsg+strlen(PQerrormsg),
|
|
|
|
"\tIf not, remove socket node (%s) and retry.\n",
|
|
|
|
sock_path);
|
1998-02-26 05:46:47 +01:00
|
|
|
else
|
|
|
|
strcat(PQerrormsg, "\tIf not, wait a few seconds and retry.\n");
|
|
|
|
fputs(PQerrormsg, stderr);
|
1998-09-01 05:29:17 +02:00
|
|
|
return STATUS_ERROR;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
1998-08-25 23:32:10 +02:00
|
|
|
if (family == AF_UNIX) {
|
1998-06-27 06:53:49 +02:00
|
|
|
on_proc_exit(StreamDoUnlink, NULL);
|
1998-05-29 19:00:34 +02:00
|
|
|
|
1998-08-25 23:32:10 +02:00
|
|
|
/*
|
|
|
|
* Open the socket file and get an advisory lock on it.
|
|
|
|
* The lock_fd is left open to keep the lock.
|
|
|
|
*/
|
|
|
|
if ((lock_fd = open(sock_path, O_RDONLY|O_NONBLOCK, 0666)) >= 0) {
|
|
|
|
if (flock(lock_fd, LOCK_EX|LOCK_NB) != 0) {
|
|
|
|
TPRINTF(TRACE_VERBOSE, "flock error for %s", sock_path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
listen(fd, SOMAXCONN);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* MS: I took this code from Dillon's version. It makes the listening
|
|
|
|
* port non-blocking. That is not necessary (and may tickle kernel
|
|
|
|
* bugs).
|
|
|
|
*
|
|
|
|
* fcntl(fd, F_SETFD, 1); fcntl(fd, F_SETFL, FNDELAY);
|
|
|
|
*/
|
|
|
|
|
|
|
|
*fdP = fd;
|
1997-11-19 19:28:59 +01:00
|
|
|
if (family == AF_UNIX)
|
1998-02-26 05:46:47 +01:00
|
|
|
chmod(sock_path, 0777);
|
1998-09-01 05:29:17 +02:00
|
|
|
return STATUS_OK;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* StreamConnection -- create a new connection with client using
|
1997-09-07 07:04:48 +02:00
|
|
|
* server port.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* This one should be non-blocking.
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* RETURNS: STATUS_OK or STATUS_ERROR
|
|
|
|
*/
|
|
|
|
int
|
1997-09-08 23:56:23 +02:00
|
|
|
StreamConnection(int server_fd, Port *port)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-11-10 06:16:00 +01:00
|
|
|
int len,
|
|
|
|
addrlen;
|
1998-01-26 02:42:53 +01:00
|
|
|
int family = port->raddr.sa.sa_family;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* accept connection (and fill in the client (remote) address) */
|
1997-11-07 21:52:15 +01:00
|
|
|
len = family == AF_INET ?
|
1997-11-10 06:16:00 +01:00
|
|
|
sizeof(struct sockaddr_in) : sizeof(struct sockaddr_un);
|
1997-11-07 21:52:15 +01:00
|
|
|
addrlen = len;
|
1997-09-07 07:04:48 +02:00
|
|
|
if ((port->sock = accept(server_fd,
|
1997-11-10 06:16:00 +01:00
|
|
|
(struct sockaddr *) & port->raddr,
|
|
|
|
&addrlen)) < 0)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1998-01-07 22:07:04 +01:00
|
|
|
elog(ERROR, "postmaster: StreamConnection: accept: %m");
|
1998-09-01 05:29:17 +02:00
|
|
|
return STATUS_ERROR;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1997-11-10 06:16:00 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/* fill in the server (local) address */
|
1997-11-07 21:52:15 +01:00
|
|
|
addrlen = len;
|
1997-09-07 07:04:48 +02:00
|
|
|
if (getsockname(port->sock, (struct sockaddr *) & port->laddr,
|
|
|
|
&addrlen) < 0)
|
|
|
|
{
|
1998-01-07 22:07:04 +01:00
|
|
|
elog(ERROR, "postmaster: StreamConnection: getsockname: %m");
|
1998-09-01 05:29:17 +02:00
|
|
|
return STATUS_ERROR;
|
1997-04-16 08:25:13 +02:00
|
|
|
}
|
1997-11-07 21:52:15 +01:00
|
|
|
if (family == AF_INET)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
|
|
|
struct protoent *pe;
|
1997-09-08 04:41:22 +02:00
|
|
|
int on = 1;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
pe = getprotobyname("TCP");
|
|
|
|
if (pe == NULL)
|
|
|
|
{
|
1998-01-07 22:07:04 +01:00
|
|
|
elog(ERROR, "postmaster: getprotobyname failed");
|
1998-09-01 05:29:17 +02:00
|
|
|
return STATUS_ERROR;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
if (setsockopt(port->sock, pe->p_proto, TCP_NODELAY,
|
|
|
|
&on, sizeof(on)) < 0)
|
|
|
|
{
|
1998-01-07 22:07:04 +01:00
|
|
|
elog(ERROR, "postmaster: setsockopt failed");
|
1998-09-01 05:29:17 +02:00
|
|
|
return STATUS_ERROR;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1997-04-16 08:25:13 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* reset to non-blocking */
|
|
|
|
fcntl(port->sock, F_SETFL, 1);
|
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return STATUS_OK;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1996-07-09 08:22:35 +02:00
|
|
|
* StreamClose -- close a client/backend connection
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
StreamClose(int sock)
|
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
close(sock);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ---------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* StreamOpen -- From client, initiate a connection with the
|
|
|
|
* server (Postmaster).
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* RETURNS: STATUS_OK or STATUS_ERROR
|
|
|
|
*
|
|
|
|
* NOTE: connection is NOT established just because this
|
1997-09-07 07:04:48 +02:00
|
|
|
* routine exits. Local state is ok, but we haven't
|
|
|
|
* spoken to the postmaster yet.
|
1996-07-09 08:22:35 +02:00
|
|
|
* ---------------------------
|
|
|
|
*/
|
|
|
|
int
|
1997-09-08 23:56:23 +02:00
|
|
|
StreamOpen(char *hostName, short portName, Port *port)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-11-10 06:16:00 +01:00
|
|
|
int len,
|
|
|
|
err;
|
1997-09-07 07:04:48 +02:00
|
|
|
struct hostent *hp;
|
1997-09-08 04:41:22 +02:00
|
|
|
extern int errno;
|
1997-11-10 06:16:00 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/* set up the server (remote) address */
|
1997-11-07 21:52:15 +01:00
|
|
|
MemSet((char *) &port->raddr, 0, sizeof(port->raddr));
|
|
|
|
if (hostName)
|
1997-11-10 06:16:00 +01:00
|
|
|
{
|
|
|
|
if (!(hp = gethostbyname(hostName)) || hp->h_addrtype != AF_INET)
|
|
|
|
{
|
|
|
|
sprintf(PQerrormsg,
|
|
|
|
"FATAL: StreamOpen: unknown hostname: %s\n",
|
|
|
|
hostName);
|
|
|
|
fputs(PQerrormsg, stderr);
|
|
|
|
pqdebug("%s", PQerrormsg);
|
1998-09-01 05:29:17 +02:00
|
|
|
return STATUS_ERROR;
|
1997-11-10 06:16:00 +01:00
|
|
|
}
|
|
|
|
memmove((char *) &(port->raddr.in.sin_addr),
|
|
|
|
(char *) hp->h_addr,
|
|
|
|
hp->h_length);
|
|
|
|
port->raddr.in.sin_family = AF_INET;
|
|
|
|
port->raddr.in.sin_port = htons(portName);
|
|
|
|
len = sizeof(struct sockaddr_in);
|
|
|
|
}
|
1997-11-07 21:52:15 +01:00
|
|
|
else
|
1997-11-10 06:16:00 +01:00
|
|
|
{
|
|
|
|
port->raddr.un.sun_family = AF_UNIX;
|
|
|
|
len = UNIXSOCK_PATH(port->raddr.un, portName);
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
/* connect to the server */
|
1998-01-26 02:42:53 +01:00
|
|
|
if ((port->sock = socket(port->raddr.sa.sa_family, SOCK_STREAM, 0)) < 0)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
|
|
|
sprintf(PQerrormsg,
|
1997-11-10 06:16:00 +01:00
|
|
|
"FATAL: StreamOpen: socket() failed: errno=%d\n",
|
1997-09-07 07:04:48 +02:00
|
|
|
errno);
|
|
|
|
fputs(PQerrormsg, stderr);
|
|
|
|
pqdebug("%s", PQerrormsg);
|
1998-09-01 05:29:17 +02:00
|
|
|
return STATUS_ERROR;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1998-01-26 02:42:53 +01:00
|
|
|
err = connect(port->sock, &port->raddr.sa, len);
|
1997-11-07 21:52:15 +01:00
|
|
|
if (err < 0)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
|
|
|
sprintf(PQerrormsg,
|
1997-11-10 06:16:00 +01:00
|
|
|
"FATAL: StreamOpen: connect() failed: errno=%d\n",
|
1997-09-07 07:04:48 +02:00
|
|
|
errno);
|
|
|
|
fputs(PQerrormsg, stderr);
|
|
|
|
pqdebug("%s", PQerrormsg);
|
1998-09-01 05:29:17 +02:00
|
|
|
return STATUS_ERROR;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* fill in the client address */
|
1998-01-26 02:42:53 +01:00
|
|
|
if (getsockname(port->sock, &port->laddr.sa, &len) < 0)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
|
|
|
sprintf(PQerrormsg,
|
|
|
|
"FATAL: StreamOpen: getsockname() failed: errno=%d\n",
|
|
|
|
errno);
|
|
|
|
fputs(PQerrormsg, stderr);
|
|
|
|
pqdebug("%s", PQerrormsg);
|
1998-09-01 05:29:17 +02:00
|
|
|
return STATUS_ERROR;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return STATUS_OK;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1998-06-16 09:29:54 +02:00
|
|
|
|
1998-07-18 20:34:34 +02:00
|
|
|
#ifdef MULTIBYTE
|
1998-06-16 09:29:54 +02:00
|
|
|
void
|
|
|
|
pq_putncharlen(char *s, int n)
|
|
|
|
{
|
|
|
|
unsigned char *p;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
p = pg_server_to_client(s, n);
|
|
|
|
len = strlen(p);
|
|
|
|
pq_putint(len, sizeof(int));
|
|
|
|
pq_putnchar(p, len);
|
|
|
|
}
|
|
|
|
#endif
|