Commit b05d3ae1 authored by Tom Lane's avatar Tom Lane

Error message editing in backend/libpq, backend/postmaster, backend/tcop.

Along the way, fix some logic problems in pgstat_initstats, notably the
bogus assumption that malloc returns zeroed memory.
parent 277dbb0c
<!-- <!--
$Header: /cvsroot/pgsql/doc/src/sgml/sources.sgml,v 2.9 2003/07/18 23:20:32 tgl Exp $ $Header: /cvsroot/pgsql/doc/src/sgml/sources.sgml,v 2.10 2003/07/22 19:00:07 tgl Exp $
--> -->
<chapter id="source"> <chapter id="source">
...@@ -234,6 +234,13 @@ less -x4 ...@@ -234,6 +234,13 @@ less -x4
primary error message text. primary error message text.
</para> </para>
</listitem> </listitem>
<listitem>
<para>
<function>errcode_for_socket_access</>() is a convenience function that
selects an appropriate SQLSTATE error identifier for a failure in a
socket-related system call.
</para>
</listitem>
</itemizedlist> </itemizedlist>
</para> </para>
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/commands/alter.c,v 1.2 2003/07/20 21:56:32 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/commands/alter.c,v 1.3 2003/07/22 19:00:07 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -29,41 +29,12 @@ ...@@ -29,41 +29,12 @@
#include "commands/user.h" #include "commands/user.h"
#include "miscadmin.h" #include "miscadmin.h"
#include "parser/parse_clause.h" #include "parser/parse_clause.h"
#include "tcop/utility.h"
#include "utils/acl.h" #include "utils/acl.h"
#include "utils/lsyscache.h" #include "utils/lsyscache.h"
#include "utils/syscache.h" #include "utils/syscache.h"
static void
CheckOwnership(RangeVar *rel, bool noCatalogs)
{
Oid relOid;
HeapTuple tuple;
relOid = RangeVarGetRelid(rel, false);
tuple = SearchSysCache(RELOID,
ObjectIdGetDatum(relOid),
0, 0, 0);
if (!HeapTupleIsValid(tuple)) /* should not happen */
elog(ERROR, "cache lookup failed for relation %u", relOid);
if (!pg_class_ownercheck(relOid, GetUserId()))
aclcheck_error(ACLCHECK_NOT_OWNER, rel->relname);
if (noCatalogs)
{
if (!allowSystemTableMods &&
IsSystemClass((Form_pg_class) GETSTRUCT(tuple)))
ereport(ERROR,
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
errmsg("relation \"%s\" is a system catalog",
rel->relname)));
}
ReleaseSysCache(tuple);
}
void void
ExecRenameStmt(RenameStmt *stmt) ExecRenameStmt(RenameStmt *stmt)
{ {
...@@ -111,7 +82,7 @@ ExecRenameStmt(RenameStmt *stmt) ...@@ -111,7 +82,7 @@ ExecRenameStmt(RenameStmt *stmt)
{ {
Oid relid; Oid relid;
CheckOwnership(stmt->relation, true); CheckRelationOwnership(stmt->relation, true);
relid = RangeVarGetRelid(stmt->relation, false); relid = RangeVarGetRelid(stmt->relation, false);
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.203 2003/07/21 17:04:58 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.204 2003/07/22 19:00:07 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -1923,7 +1923,7 @@ CopyReadBinaryAttribute(int column_no, FmgrInfo *flinfo, Oid typelem, ...@@ -1923,7 +1923,7 @@ CopyReadBinaryAttribute(int column_no, FmgrInfo *flinfo, Oid typelem,
/* Trouble if it didn't eat the whole buffer */ /* Trouble if it didn't eat the whole buffer */
if (attribute_buf.cursor != attribute_buf.len) if (attribute_buf.cursor != attribute_buf.len)
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_BAD_COPY_FILE_FORMAT), (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
errmsg("incorrect binary data format in field %d", errmsg("incorrect binary data format in field %d",
column_no))); column_no)));
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/libpq/auth.c,v 1.103 2003/06/25 01:19:47 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/libpq/auth.c,v 1.104 2003/07/22 19:00:10 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -109,20 +109,22 @@ pg_krb4_recvauth(Port *port) ...@@ -109,20 +109,22 @@ pg_krb4_recvauth(Port *port)
version); version);
if (status != KSUCCESS) if (status != KSUCCESS)
{ {
elog(LOG, "pg_krb4_recvauth: kerberos error: %s", ereport(LOG,
krb_err_txt[status]); (errmsg("kerberos error: %s", krb_err_txt[status])));
return STATUS_ERROR; return STATUS_ERROR;
} }
if (strncmp(version, PG_KRB4_VERSION, KRB_SENDAUTH_VLEN) != 0) if (strncmp(version, PG_KRB4_VERSION, KRB_SENDAUTH_VLEN) != 0)
{ {
elog(LOG, "pg_krb4_recvauth: protocol version \"%s\" != \"%s\"", ereport(LOG,
version, PG_KRB4_VERSION); (errmsg("kerberos protocol version \"%s\" != \"%s\"",
version, PG_KRB4_VERSION)));
return STATUS_ERROR; return STATUS_ERROR;
} }
if (strncmp(port->user_name, auth_data.pname, SM_DATABASE_USER) != 0) if (strncmp(port->user_name, auth_data.pname, SM_DATABASE_USER) != 0)
{ {
elog(LOG, "pg_krb4_recvauth: name \"%s\" != \"%s\"", ereport(LOG,
port->user_name, auth_data.pname); (errmsg("kerberos user name \"%s\" != \"%s\"",
port->user_name, auth_data.pname)));
return STATUS_ERROR; return STATUS_ERROR;
} }
return STATUS_OK; return STATUS_OK;
...@@ -133,7 +135,9 @@ pg_krb4_recvauth(Port *port) ...@@ -133,7 +135,9 @@ pg_krb4_recvauth(Port *port)
static int static int
pg_krb4_recvauth(Port *port) pg_krb4_recvauth(Port *port)
{ {
elog(LOG, "pg_krb4_recvauth: Kerberos not implemented on this server"); ereport(LOG,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("kerberos v4 not implemented on this server")));
return STATUS_ERROR; return STATUS_ERROR;
} }
#endif /* KRB4 */ #endif /* KRB4 */
...@@ -193,8 +197,9 @@ pg_krb5_init(void) ...@@ -193,8 +197,9 @@ pg_krb5_init(void)
retval = krb5_init_context(&pg_krb5_context); retval = krb5_init_context(&pg_krb5_context);
if (retval) if (retval)
{ {
elog(LOG, "pg_krb5_init: krb5_init_context returned Kerberos error %d", ereport(LOG,
retval); (errmsg("kerberos init returned error %d",
retval)));
com_err("postgres", retval, "while initializing krb5"); com_err("postgres", retval, "while initializing krb5");
return STATUS_ERROR; return STATUS_ERROR;
} }
...@@ -202,9 +207,10 @@ pg_krb5_init(void) ...@@ -202,9 +207,10 @@ pg_krb5_init(void)
retval = krb5_kt_resolve(pg_krb5_context, pg_krb_server_keyfile, &pg_krb5_keytab); retval = krb5_kt_resolve(pg_krb5_context, pg_krb_server_keyfile, &pg_krb5_keytab);
if (retval) if (retval)
{ {
elog(LOG, "pg_krb5_init: krb5_kt_resolve returned Kerberos error %d", ereport(LOG,
retval); (errmsg("kerberos keytab resolve returned error %d",
com_err("postgres", retval, "while resolving keytab file %s", retval)));
com_err("postgres", retval, "while resolving keytab file \"%s\"",
pg_krb_server_keyfile); pg_krb_server_keyfile);
krb5_free_context(pg_krb5_context); krb5_free_context(pg_krb5_context);
return STATUS_ERROR; return STATUS_ERROR;
...@@ -214,10 +220,11 @@ pg_krb5_init(void) ...@@ -214,10 +220,11 @@ pg_krb5_init(void)
KRB5_NT_SRV_HST, &pg_krb5_server); KRB5_NT_SRV_HST, &pg_krb5_server);
if (retval) if (retval)
{ {
elog(LOG, "pg_krb5_init: krb5_sname_to_principal returned Kerberos error %d", ereport(LOG,
retval); (errmsg("kerberos sname_to_principal(\"%s\") returned error %d",
PG_KRB_SRVNAM, retval)));
com_err("postgres", retval, com_err("postgres", retval,
"while getting server principal for service %s", "while getting server principal for service \"%s\"",
PG_KRB_SRVNAM); PG_KRB_SRVNAM);
krb5_kt_close(pg_krb5_context, pg_krb5_keytab); krb5_kt_close(pg_krb5_context, pg_krb5_keytab);
krb5_free_context(pg_krb5_context); krb5_free_context(pg_krb5_context);
...@@ -258,8 +265,9 @@ pg_krb5_recvauth(Port *port) ...@@ -258,8 +265,9 @@ pg_krb5_recvauth(Port *port)
pg_krb5_server, 0, pg_krb5_keytab, &ticket); pg_krb5_server, 0, pg_krb5_keytab, &ticket);
if (retval) if (retval)
{ {
elog(LOG, "pg_krb5_recvauth: krb5_recvauth returned Kerberos error %d", ereport(LOG,
retval); (errmsg("kerberos recvauth returned error %d",
retval)));
com_err("postgres", retval, "from krb5_recvauth"); com_err("postgres", retval, "from krb5_recvauth");
return STATUS_ERROR; return STATUS_ERROR;
} }
...@@ -282,8 +290,9 @@ pg_krb5_recvauth(Port *port) ...@@ -282,8 +290,9 @@ pg_krb5_recvauth(Port *port)
#endif #endif
if (retval) if (retval)
{ {
elog(LOG, "pg_krb5_recvauth: krb5_unparse_name returned Kerberos error %d", ereport(LOG,
retval); (errmsg("kerberos unparse_name returned error %d",
retval)));
com_err("postgres", retval, "while unparsing client name"); com_err("postgres", retval, "while unparsing client name");
krb5_free_ticket(pg_krb5_context, ticket); krb5_free_ticket(pg_krb5_context, ticket);
krb5_auth_con_free(pg_krb5_context, auth_context); krb5_auth_con_free(pg_krb5_context, auth_context);
...@@ -293,8 +302,9 @@ pg_krb5_recvauth(Port *port) ...@@ -293,8 +302,9 @@ pg_krb5_recvauth(Port *port)
kusername = pg_an_to_ln(kusername); kusername = pg_an_to_ln(kusername);
if (strncmp(port->user_name, kusername, SM_DATABASE_USER)) if (strncmp(port->user_name, kusername, SM_DATABASE_USER))
{ {
elog(LOG, "pg_krb5_recvauth: user name \"%s\" != krb5 name \"%s\"", ereport(LOG,
port->user_name, kusername); (errmsg("kerberos user name \"%s\" != \"%s\"",
port->user_name, kusername)));
ret = STATUS_ERROR; ret = STATUS_ERROR;
} }
else else
...@@ -312,7 +322,9 @@ pg_krb5_recvauth(Port *port) ...@@ -312,7 +322,9 @@ pg_krb5_recvauth(Port *port)
static int static int
pg_krb5_recvauth(Port *port) pg_krb5_recvauth(Port *port)
{ {
elog(LOG, "pg_krb5_recvauth: Kerberos not implemented on this server"); ereport(LOG,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("kerberos v5 not implemented on this server")));
return STATUS_ERROR; return STATUS_ERROR;
} }
#endif /* KRB5 */ #endif /* KRB5 */
...@@ -377,8 +389,10 @@ auth_failed(Port *port, int status) ...@@ -377,8 +389,10 @@ auth_failed(Port *port, int status)
#endif /* USE_PAM */ #endif /* USE_PAM */
} }
elog(FATAL, "%s authentication failed for user \"%s\"", ereport(FATAL,
authmethod, port->user_name); (errcode(ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION),
errmsg("%s authentication failed for user \"%s\"",
authmethod, port->user_name)));
/* doesn't return */ /* doesn't return */
} }
...@@ -399,7 +413,10 @@ ClientAuthentication(Port *port) ...@@ -399,7 +413,10 @@ ClientAuthentication(Port *port)
* an error message into the postmaster logfile if it failed. * an error message into the postmaster logfile if it failed.
*/ */
if (hba_getauthmethod(port) != STATUS_OK) if (hba_getauthmethod(port) != STATUS_OK)
elog(FATAL, "Missing or erroneous pg_hba.conf file, see postmaster log for details"); ereport(FATAL,
(errcode(ERRCODE_CONFIG_FILE_ERROR),
errmsg("missing or erroneous pg_hba.conf file"),
errhint("See postmaster log for details.")));
switch (port->auth_method) switch (port->auth_method)
{ {
...@@ -417,15 +434,16 @@ ClientAuthentication(Port *port) ...@@ -417,15 +434,16 @@ ClientAuthentication(Port *port)
{ {
char hostinfo[NI_MAXHOST]; char hostinfo[NI_MAXHOST];
getnameinfo( getnameinfo((struct sockaddr *) &port->raddr.addr,
(struct sockaddr *)&port->raddr.addr, port->raddr.salen,
port->raddr.salen, hostinfo, sizeof(hostinfo),
hostinfo, sizeof(hostinfo), NULL, 0,
NULL, 0, NI_NUMERICHOST); NI_NUMERICHOST);
elog(FATAL, ereport(FATAL,
"No pg_hba.conf entry for host %s, user %s, database %s", (errcode(ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION),
hostinfo, port->user_name, port->database_name); errmsg("no pg_hba.conf entry for host \"%s\", user \"%s\", database \"%s\"",
hostinfo, port->user_name, port->database_name)));
break; break;
} }
...@@ -433,10 +451,9 @@ ClientAuthentication(Port *port) ...@@ -433,10 +451,9 @@ ClientAuthentication(Port *port)
/* Kerberos 4 only seems to work with AF_INET. */ /* Kerberos 4 only seems to work with AF_INET. */
if (port->raddr.addr.ss_family != AF_INET if (port->raddr.addr.ss_family != AF_INET
|| port->laddr.addr.ss_family != AF_INET) || port->laddr.addr.ss_family != AF_INET)
{ ereport(FATAL,
elog(FATAL, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
"Unsupported protocol for Kerberos 4"); errmsg("kerberos 4 only supports IPv4 connections")));
}
sendAuthRequest(port, AUTH_REQ_KRB4); sendAuthRequest(port, AUTH_REQ_KRB4);
status = pg_krb4_recvauth(port); status = pg_krb4_recvauth(port);
break; break;
...@@ -466,7 +483,9 @@ ClientAuthentication(Port *port) ...@@ -466,7 +483,9 @@ ClientAuthentication(Port *port)
int on = 1; int on = 1;
if (setsockopt(port->sock, 0, LOCAL_CREDS, &on, sizeof(on)) < 0) if (setsockopt(port->sock, 0, LOCAL_CREDS, &on, sizeof(on)) < 0)
elog(FATAL, "pg_local_sendauth: can't do setsockopt: %m"); ereport(FATAL,
(errcode_for_socket_access(),
errmsg("failed to enable credential receipt: %m")));
} }
#endif #endif
if (port->raddr.addr.ss_family == AF_UNIX) if (port->raddr.addr.ss_family == AF_UNIX)
...@@ -552,12 +571,14 @@ pam_passwd_conv_proc(int num_msg, const struct pam_message ** msg, ...@@ -552,12 +571,14 @@ pam_passwd_conv_proc(int num_msg, const struct pam_message ** msg,
switch (msg[0]->msg_style) switch (msg[0]->msg_style)
{ {
case PAM_ERROR_MSG: case PAM_ERROR_MSG:
elog(LOG, "pam_passwd_conv_proc: Error from underlying PAM layer: '%s'", ereport(LOG,
msg[0]->msg); (errmsg("error from underlying PAM layer: %s",
msg[0]->msg)));
return PAM_CONV_ERR; return PAM_CONV_ERR;
default: default:
elog(LOG, "pam_passwd_conv_proc: Unexpected PAM conversation %d/'%s'", ereport(LOG,
msg[0]->msg_style, msg[0]->msg); (errmsg("unsupported PAM conversation %d/%s",
msg[0]->msg_style, msg[0]->msg)));
return PAM_CONV_ERR; return PAM_CONV_ERR;
} }
} }
...@@ -587,7 +608,8 @@ pam_passwd_conv_proc(int num_msg, const struct pam_message ** msg, ...@@ -587,7 +608,8 @@ pam_passwd_conv_proc(int num_msg, const struct pam_message ** msg,
if (strlen(passwd) == 0) if (strlen(passwd) == 0)
{ {
elog(LOG, "pam_passwd_conv_proc: no password"); ereport(LOG,
(errmsg("empty password returned by client")));
return PAM_CONV_ERR; return PAM_CONV_ERR;
} }
appdata_ptr = passwd; appdata_ptr = passwd;
...@@ -600,7 +622,9 @@ pam_passwd_conv_proc(int num_msg, const struct pam_message ** msg, ...@@ -600,7 +622,9 @@ pam_passwd_conv_proc(int num_msg, const struct pam_message ** msg,
*resp = calloc(num_msg, sizeof(struct pam_response)); *resp = calloc(num_msg, sizeof(struct pam_response));
if (!*resp) if (!*resp)
{ {
elog(LOG, "pam_passwd_conv_proc: Out of memory!"); ereport(LOG,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory")));
return PAM_CONV_ERR; return PAM_CONV_ERR;
} }
...@@ -644,8 +668,9 @@ CheckPAMAuth(Port *port, char *user, char *password) ...@@ -644,8 +668,9 @@ CheckPAMAuth(Port *port, char *user, char *password)
if (retval != PAM_SUCCESS) if (retval != PAM_SUCCESS)
{ {
elog(LOG, "CheckPAMAuth: Failed to create PAM authenticator: '%s'", ereport(LOG,
pam_strerror(pamh, retval)); (errmsg("Failed to create PAM authenticator: %s",
pam_strerror(pamh, retval))));
pam_passwd = NULL; /* Unset pam_passwd */ pam_passwd = NULL; /* Unset pam_passwd */
return STATUS_ERROR; return STATUS_ERROR;
} }
...@@ -654,8 +679,9 @@ CheckPAMAuth(Port *port, char *user, char *password) ...@@ -654,8 +679,9 @@ CheckPAMAuth(Port *port, char *user, char *password)
if (retval != PAM_SUCCESS) if (retval != PAM_SUCCESS)
{ {
elog(LOG, "CheckPAMAuth: pam_set_item(PAM_USER) failed: '%s'", ereport(LOG,
pam_strerror(pamh, retval)); (errmsg("pam_set_item(PAM_USER) failed: %s",
pam_strerror(pamh, retval))));
pam_passwd = NULL; /* Unset pam_passwd */ pam_passwd = NULL; /* Unset pam_passwd */
return STATUS_ERROR; return STATUS_ERROR;
} }
...@@ -664,8 +690,9 @@ CheckPAMAuth(Port *port, char *user, char *password) ...@@ -664,8 +690,9 @@ CheckPAMAuth(Port *port, char *user, char *password)
if (retval != PAM_SUCCESS) if (retval != PAM_SUCCESS)
{ {
elog(LOG, "CheckPAMAuth: pam_set_item(PAM_CONV) failed: '%s'", ereport(LOG,
pam_strerror(pamh, retval)); (errmsg("pam_set_item(PAM_CONV) failed: %s",
pam_strerror(pamh, retval))));
pam_passwd = NULL; /* Unset pam_passwd */ pam_passwd = NULL; /* Unset pam_passwd */
return STATUS_ERROR; return STATUS_ERROR;
} }
...@@ -674,8 +701,9 @@ CheckPAMAuth(Port *port, char *user, char *password) ...@@ -674,8 +701,9 @@ CheckPAMAuth(Port *port, char *user, char *password)
if (retval != PAM_SUCCESS) if (retval != PAM_SUCCESS)
{ {
elog(LOG, "CheckPAMAuth: pam_authenticate failed: '%s'", ereport(LOG,
pam_strerror(pamh, retval)); (errmsg("pam_authenticate failed: %s",
pam_strerror(pamh, retval))));
pam_passwd = NULL; /* Unset pam_passwd */ pam_passwd = NULL; /* Unset pam_passwd */
return STATUS_ERROR; return STATUS_ERROR;
} }
...@@ -684,8 +712,9 @@ CheckPAMAuth(Port *port, char *user, char *password) ...@@ -684,8 +712,9 @@ CheckPAMAuth(Port *port, char *user, char *password)
if (retval != PAM_SUCCESS) if (retval != PAM_SUCCESS)
{ {
elog(LOG, "CheckPAMAuth: pam_acct_mgmt failed: '%s'", ereport(LOG,
pam_strerror(pamh, retval)); (errmsg("pam_acct_mgmt failed: %s",
pam_strerror(pamh, retval))));
pam_passwd = NULL; /* Unset pam_passwd */ pam_passwd = NULL; /* Unset pam_passwd */
return STATUS_ERROR; return STATUS_ERROR;
} }
...@@ -694,8 +723,9 @@ CheckPAMAuth(Port *port, char *user, char *password) ...@@ -694,8 +723,9 @@ CheckPAMAuth(Port *port, char *user, char *password)
if (retval != PAM_SUCCESS) if (retval != PAM_SUCCESS)
{ {
elog(LOG, "CheckPAMAuth: Failed to release PAM authenticator: '%s'", ereport(LOG,
pam_strerror(pamh, retval)); (errmsg("failed to release PAM authenticator: %s",
pam_strerror(pamh, retval))));
} }
pam_passwd = NULL; /* Unset pam_passwd */ pam_passwd = NULL; /* Unset pam_passwd */
...@@ -730,7 +760,10 @@ recv_password_packet(Port *port) ...@@ -730,7 +760,10 @@ recv_password_packet(Port *port)
* the log. * the log.
*/ */
if (mtype != EOF) if (mtype != EOF)
elog(COMMERROR, "Expected password response, got %c", mtype); ereport(COMMERROR,
(errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("expected password response, got msg type %d",
mtype)));
return NULL; /* EOF or bad message type */ return NULL; /* EOF or bad message type */
} }
} }
...@@ -755,10 +788,13 @@ recv_password_packet(Port *port) ...@@ -755,10 +788,13 @@ recv_password_packet(Port *port)
* StringInfo is guaranteed to have an appended '\0'. * StringInfo is guaranteed to have an appended '\0'.
*/ */
if (strlen(buf.data) + 1 != buf.len) if (strlen(buf.data) + 1 != buf.len)
elog(COMMERROR, "bogus password packet size"); ereport(COMMERROR,
(errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("invalid password packet size")));
/* Do not echo password to logs, for security. */ /* Do not echo password to logs, for security. */
elog(DEBUG5, "received password packet"); ereport(DEBUG5,
(errmsg("received password packet")));
/* /*
* Return the received string. Note we do not attempt to do any * Return the received string. Note we do not attempt to do any
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/libpq/be-fsstubs.c,v 1.64 2003/05/27 17:49:46 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/libpq/be-fsstubs.c,v 1.65 2003/07/22 19:00:10 tgl Exp $
* *
* NOTES * NOTES
* This should be moved to a more appropriate place. It is here * This should be moved to a more appropriate place. It is here
...@@ -119,7 +119,9 @@ lo_close(PG_FUNCTION_ARGS) ...@@ -119,7 +119,9 @@ lo_close(PG_FUNCTION_ARGS)
if (fd < 0 || fd >= cookies_size || cookies[fd] == NULL) if (fd < 0 || fd >= cookies_size || cookies[fd] == NULL)
{ {
elog(ERROR, "lo_close: invalid large obj descriptor (%d)", fd); ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("invalid large-object descriptor: %d", fd)));
PG_RETURN_INT32(-1); PG_RETURN_INT32(-1);
} }
#if FSDB #if FSDB
...@@ -155,7 +157,9 @@ lo_read(int fd, char *buf, int len) ...@@ -155,7 +157,9 @@ lo_read(int fd, char *buf, int len)
if (fd < 0 || fd >= cookies_size || cookies[fd] == NULL) if (fd < 0 || fd >= cookies_size || cookies[fd] == NULL)
{ {
elog(ERROR, "lo_read: invalid large obj descriptor (%d)", fd); ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("invalid large-object descriptor: %d", fd)));
return -1; return -1;
} }
...@@ -177,7 +181,9 @@ lo_write(int fd, char *buf, int len) ...@@ -177,7 +181,9 @@ lo_write(int fd, char *buf, int len)
if (fd < 0 || fd >= cookies_size || cookies[fd] == NULL) if (fd < 0 || fd >= cookies_size || cookies[fd] == NULL)
{ {
elog(ERROR, "lo_write: invalid large obj descriptor (%d)", fd); ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("invalid large-object descriptor: %d", fd)));
return -1; return -1;
} }
...@@ -203,7 +209,9 @@ lo_lseek(PG_FUNCTION_ARGS) ...@@ -203,7 +209,9 @@ lo_lseek(PG_FUNCTION_ARGS)
if (fd < 0 || fd >= cookies_size || cookies[fd] == NULL) if (fd < 0 || fd >= cookies_size || cookies[fd] == NULL)
{ {
elog(ERROR, "lo_lseek: invalid large obj descriptor (%d)", fd); ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("invalid large-object descriptor: %d", fd)));
PG_RETURN_INT32(-1); PG_RETURN_INT32(-1);
} }
...@@ -258,7 +266,9 @@ lo_tell(PG_FUNCTION_ARGS) ...@@ -258,7 +266,9 @@ lo_tell(PG_FUNCTION_ARGS)
if (fd < 0 || fd >= cookies_size || cookies[fd] == NULL) if (fd < 0 || fd >= cookies_size || cookies[fd] == NULL)
{ {
elog(ERROR, "lo_tell: invalid large object descriptor (%d)", fd); ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("invalid large-object descriptor: %d", fd)));
PG_RETURN_INT32(-1); PG_RETURN_INT32(-1);
} }
...@@ -360,9 +370,10 @@ lo_import(PG_FUNCTION_ARGS) ...@@ -360,9 +370,10 @@ lo_import(PG_FUNCTION_ARGS)
#ifndef ALLOW_DANGEROUS_LO_FUNCTIONS #ifndef ALLOW_DANGEROUS_LO_FUNCTIONS
if (!superuser()) if (!superuser())
elog(ERROR, "You must have Postgres superuser privilege to use " ereport(ERROR,
"server-side lo_import().\n\tAnyone can use the " (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
"client-side lo_import() provided by libpq."); errmsg("must be superuser to use server-side lo_import()"),
errhint("Anyone can use the client-side lo_import() provided by libpq.")));
#endif #endif
/* /*
...@@ -375,16 +386,15 @@ lo_import(PG_FUNCTION_ARGS) ...@@ -375,16 +386,15 @@ lo_import(PG_FUNCTION_ARGS)
fnamebuf[nbytes] = '\0'; fnamebuf[nbytes] = '\0';
fd = PathNameOpenFile(fnamebuf, O_RDONLY | PG_BINARY, 0666); fd = PathNameOpenFile(fnamebuf, O_RDONLY | PG_BINARY, 0666);
if (fd < 0) if (fd < 0)
elog(ERROR, "lo_import: can't open unix file \"%s\": %m", ereport(ERROR,
fnamebuf); (errcode_for_file_access(),
errmsg("could not open server file \"%s\": %m",
fnamebuf)));
/* /*
* create an inversion "object" * create an inversion object
*/ */
lobj = inv_create(INV_READ | INV_WRITE); lobj = inv_create(INV_READ | INV_WRITE);
if (lobj == NULL)
elog(ERROR, "lo_import: can't create inv object for \"%s\"",
fnamebuf);
lobjOid = lobj->id; lobjOid = lobj->id;
/* /*
...@@ -393,11 +403,15 @@ lo_import(PG_FUNCTION_ARGS) ...@@ -393,11 +403,15 @@ lo_import(PG_FUNCTION_ARGS)
while ((nbytes = FileRead(fd, buf, BUFSIZE)) > 0) while ((nbytes = FileRead(fd, buf, BUFSIZE)) > 0)
{ {
tmp = inv_write(lobj, buf, nbytes); tmp = inv_write(lobj, buf, nbytes);
if (tmp != nbytes) Assert(tmp == nbytes);
elog(ERROR, "lo_import: error while reading \"%s\"",
fnamebuf);
} }
if (nbytes < 0)
ereport(ERROR,
(errcode_for_file_access(),
errmsg("could not read server file \"%s\": %m",
fnamebuf)));
FileClose(fd); FileClose(fd);
inv_close(lobj); inv_close(lobj);
...@@ -423,17 +437,16 @@ lo_export(PG_FUNCTION_ARGS) ...@@ -423,17 +437,16 @@ lo_export(PG_FUNCTION_ARGS)
#ifndef ALLOW_DANGEROUS_LO_FUNCTIONS #ifndef ALLOW_DANGEROUS_LO_FUNCTIONS
if (!superuser()) if (!superuser())
elog(ERROR, "You must have Postgres superuser privilege to use " ereport(ERROR,
"server-side lo_export().\n\tAnyone can use the " (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
"client-side lo_export() provided by libpq."); errmsg("must be superuser to use server-side lo_export()"),
errhint("Anyone can use the client-side lo_export() provided by libpq.")));
#endif #endif
/* /*
* open the inversion "object" * open the inversion object (no need to test for failure)
*/ */
lobj = inv_open(lobjId, INV_READ); lobj = inv_open(lobjId, INV_READ);
if (lobj == NULL)
elog(ERROR, "lo_export: can't open inv object %u", lobjId);
/* /*
* open the file to be written to * open the file to be written to
...@@ -451,8 +464,10 @@ lo_export(PG_FUNCTION_ARGS) ...@@ -451,8 +464,10 @@ lo_export(PG_FUNCTION_ARGS)
fd = PathNameOpenFile(fnamebuf, O_CREAT | O_WRONLY | O_TRUNC | PG_BINARY, 0666); fd = PathNameOpenFile(fnamebuf, O_CREAT | O_WRONLY | O_TRUNC | PG_BINARY, 0666);
umask(oumask); umask(oumask);
if (fd < 0) if (fd < 0)
elog(ERROR, "lo_export: can't open unix file \"%s\": %m", ereport(ERROR,
fnamebuf); (errcode_for_file_access(),
errmsg("could not create server file \"%s\": %m",
fnamebuf)));
/* /*
* read in from the inversion file and write to the Unix file * read in from the inversion file and write to the Unix file
...@@ -461,12 +476,14 @@ lo_export(PG_FUNCTION_ARGS) ...@@ -461,12 +476,14 @@ lo_export(PG_FUNCTION_ARGS)
{ {
tmp = FileWrite(fd, buf, nbytes); tmp = FileWrite(fd, buf, nbytes);
if (tmp != nbytes) if (tmp != nbytes)
elog(ERROR, "lo_export: error while writing \"%s\"", ereport(ERROR,
fnamebuf); (errcode_for_file_access(),
errmsg("could not write server file \"%s\": %m",
fnamebuf)));
} }
inv_close(lobj);
FileClose(fd); FileClose(fd);
inv_close(lobj);
PG_RETURN_INT32(1); PG_RETURN_INT32(1);
} }
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/libpq/be-secure.c,v 1.35 2003/07/01 13:49:47 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/libpq/be-secure.c,v 1.36 2003/07/22 19:00:10 tgl Exp $
* *
* Since the server static private key ($DataDir/server.key) * Since the server static private key ($DataDir/server.key)
* will normally be stored unencrypted so that the database * will normally be stored unencrypted so that the database
...@@ -277,12 +277,18 @@ secure_read(Port *port, void *ptr, size_t len) ...@@ -277,12 +277,18 @@ secure_read(Port *port, void *ptr, size_t len)
goto rloop; goto rloop;
case SSL_ERROR_SYSCALL: case SSL_ERROR_SYSCALL:
if (n == -1) if (n == -1)
elog(COMMERROR, "SSL SYSCALL error: %m"); ereport(COMMERROR,
(errcode_for_socket_access(),
errmsg("SSL SYSCALL error: %m")));
else else
elog(COMMERROR, "SSL SYSCALL error: EOF detected"); ereport(COMMERROR,
(errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("SSL SYSCALL error: EOF detected")));
break; break;
case SSL_ERROR_SSL: case SSL_ERROR_SSL:
elog(COMMERROR, "SSL error: %s", SSLerrmessage()); ereport(COMMERROR,
(errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("SSL error: %s", SSLerrmessage())));
/* fall through */ /* fall through */
case SSL_ERROR_ZERO_RETURN: case SSL_ERROR_ZERO_RETURN:
secure_close(port); secure_close(port);
...@@ -290,7 +296,9 @@ secure_read(Port *port, void *ptr, size_t len) ...@@ -290,7 +296,9 @@ secure_read(Port *port, void *ptr, size_t len)
n = -1; n = -1;
break; break;
default: default:
elog(COMMERROR, "Unknown SSL error code"); ereport(COMMERROR,
(errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("unrecognized SSL error code")));
break; break;
} }
} }
...@@ -317,15 +325,23 @@ secure_write(Port *port, void *ptr, size_t len) ...@@ -317,15 +325,23 @@ secure_write(Port *port, void *ptr, size_t len)
SSL_set_session_id_context(port->ssl, (void *) &SSL_context, SSL_set_session_id_context(port->ssl, (void *) &SSL_context,
sizeof(SSL_context)); sizeof(SSL_context));
if (SSL_renegotiate(port->ssl) <= 0) if (SSL_renegotiate(port->ssl) <= 0)
elog(COMMERROR, "SSL renegotiation failure"); ereport(COMMERROR,
(errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("SSL renegotiation failure")));
if (SSL_do_handshake(port->ssl) <= 0) if (SSL_do_handshake(port->ssl) <= 0)
elog(COMMERROR, "SSL renegotiation failure"); ereport(COMMERROR,
(errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("SSL renegotiation failure")));
if (port->ssl->state != SSL_ST_OK) if (port->ssl->state != SSL_ST_OK)
elog(COMMERROR, "SSL failed to send renegotiation request"); ereport(COMMERROR,
(errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("SSL failed to send renegotiation request")));
port->ssl->state |= SSL_ST_ACCEPT; port->ssl->state |= SSL_ST_ACCEPT;
SSL_do_handshake(port->ssl); SSL_do_handshake(port->ssl);
if (port->ssl->state != SSL_ST_OK) if (port->ssl->state != SSL_ST_OK)
elog(COMMERROR, "SSL renegotiation failure"); ereport(COMMERROR,
(errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("SSL renegotiation failure")));
port->count = 0; port->count = 0;
} }
...@@ -341,12 +357,18 @@ secure_write(Port *port, void *ptr, size_t len) ...@@ -341,12 +357,18 @@ secure_write(Port *port, void *ptr, size_t len)
goto wloop; goto wloop;
case SSL_ERROR_SYSCALL: case SSL_ERROR_SYSCALL:
if (n == -1) if (n == -1)
elog(COMMERROR, "SSL SYSCALL error: %m"); ereport(COMMERROR,
(errcode_for_socket_access(),
errmsg("SSL SYSCALL error: %m")));
else else
elog(COMMERROR, "SSL SYSCALL error: EOF detected"); ereport(COMMERROR,
(errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("SSL SYSCALL error: EOF detected")));
break; break;
case SSL_ERROR_SSL: case SSL_ERROR_SSL:
elog(COMMERROR, "SSL error: %s", SSLerrmessage()); ereport(COMMERROR,
(errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("SSL error: %s", SSLerrmessage())));
/* fall through */ /* fall through */
case SSL_ERROR_ZERO_RETURN: case SSL_ERROR_ZERO_RETURN:
secure_close(port); secure_close(port);
...@@ -354,7 +376,9 @@ secure_write(Port *port, void *ptr, size_t len) ...@@ -354,7 +376,9 @@ secure_write(Port *port, void *ptr, size_t len)
n = -1; n = -1;
break; break;
default: default:
elog(COMMERROR, "Unknown SSL error code"); ereport(COMMERROR,
(errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("unrecognized SSL error code")));
break; break;
} }
} }
...@@ -419,7 +443,7 @@ load_dh_file(int keylength) ...@@ -419,7 +443,7 @@ load_dh_file(int keylength)
(codes & DH_CHECK_P_NOT_SAFE_PRIME)) (codes & DH_CHECK_P_NOT_SAFE_PRIME))
{ {
elog(LOG, elog(LOG,
"DH error (%s): neither suitable generator or safe prime", "DH error (%s): neither suitable generator or safe prime",
fnbuf); fnbuf);
return NULL; return NULL;
} }
...@@ -690,7 +714,10 @@ open_server_SSL(Port *port) ...@@ -690,7 +714,10 @@ open_server_SSL(Port *port)
!SSL_set_fd(port->ssl, port->sock) || !SSL_set_fd(port->ssl, port->sock) ||
SSL_accept(port->ssl) <= 0) SSL_accept(port->ssl) <= 0)
{ {
elog(COMMERROR, "failed to initialize SSL connection: %s", SSLerrmessage()); ereport(COMMERROR,
(errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("failed to initialize SSL connection: %s",
SSLerrmessage())));
close_SSL(port); close_SSL(port);
return -1; return -1;
} }
...@@ -712,7 +739,7 @@ open_server_SSL(Port *port) ...@@ -712,7 +739,7 @@ open_server_SSL(Port *port)
NID_commonName, port->peer_cn, sizeof(port->peer_cn)); NID_commonName, port->peer_cn, sizeof(port->peer_cn));
port->peer_cn[sizeof(port->peer_cn) - 1] = '\0'; port->peer_cn[sizeof(port->peer_cn) - 1] = '\0';
} }
elog(DEBUG2, "secure connection from '%s'", port->peer_cn); elog(DEBUG2, "secure connection from \"%s\"", port->peer_cn);
/* set up debugging/info callback */ /* set up debugging/info callback */
SSL_CTX_set_info_callback(SSL_context, info_cb); SSL_CTX_set_info_callback(SSL_context, info_cb);
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Header: /cvsroot/pgsql/src/backend/libpq/crypt.c,v 1.53 2003/05/12 23:08:50 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/libpq/crypt.c,v 1.54 2003/07/22 19:00:10 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -58,8 +58,8 @@ md5_crypt_verify(const Port *port, const char *user, char *client_pass) ...@@ -58,8 +58,8 @@ md5_crypt_verify(const Port *port, const char *user, char *client_pass)
/* We can't do crypt with pg_shadow MD5 passwords */ /* We can't do crypt with pg_shadow MD5 passwords */
if (isMD5(shadow_pass) && port->auth_method == uaCrypt) if (isMD5(shadow_pass) && port->auth_method == uaCrypt)
{ {
elog(LOG, "Password is stored MD5 encrypted. " ereport(LOG,
"'crypt' auth method cannot be used."); (errmsg("cannot use CRYPT auth method because password is MD5-encrypted")));
return STATUS_ERROR; return STATUS_ERROR;
} }
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/libpq/hba.c,v 1.104 2003/06/15 16:21:39 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/libpq/hba.c,v 1.105 2003/07/22 19:00:10 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -124,7 +124,10 @@ next_token(FILE *fp, char *buf, const int bufsz) ...@@ -124,7 +124,10 @@ next_token(FILE *fp, char *buf, const int bufsz)
if (buf >= end_buf) if (buf >= end_buf)
{ {
elog(LOG, "Token too long in authentication file, skipping, %s", buf); ereport(LOG,
(errcode(ERRCODE_CONFIG_FILE_ERROR),
errmsg("authentication file token too long, skipping: \"%s\"",
buf)));
/* Discard remainder of line */ /* Discard remainder of line */
while ((c = getc(fp)) != EOF && c != '\n') while ((c = getc(fp)) != EOF && c != '\n')
; ;
...@@ -257,8 +260,10 @@ tokenize_inc_file(const char *inc_filename) ...@@ -257,8 +260,10 @@ tokenize_inc_file(const char *inc_filename)
inc_file = AllocateFile(inc_fullname, "r"); inc_file = AllocateFile(inc_fullname, "r");
if (!inc_file) if (!inc_file)
{ {
elog(LOG, "tokenize_inc_file: Unable to open secondary authentication file \"@%s\" as \"%s\": %m", ereport(LOG,
inc_filename, inc_fullname); (errcode_for_file_access(),
errmsg("could not open secondary authentication file \"@%s\" as \"%s\": %m",
inc_filename, inc_fullname)));
pfree(inc_fullname); pfree(inc_fullname);
/* return empty string, it matches nothing */ /* return empty string, it matches nothing */
...@@ -631,9 +636,7 @@ parse_hba(List *line, hbaPort *port, bool *found_p, bool *error_p) ...@@ -631,9 +636,7 @@ parse_hba(List *line, hbaPort *port, bool *found_p, bool *error_p)
/* Check if it has a CIDR suffix and if so isolate it */ /* Check if it has a CIDR suffix and if so isolate it */
cidr_slash = strchr(token,'/'); cidr_slash = strchr(token,'/');
if (cidr_slash) if (cidr_slash)
{
*cidr_slash = '\0'; *cidr_slash = '\0';
}
hints.ai_flags = AI_NUMERICHOST; hints.ai_flags = AI_NUMERICHOST;
hints.ai_family = PF_UNSPEC; hints.ai_family = PF_UNSPEC;
...@@ -646,13 +649,14 @@ parse_hba(List *line, hbaPort *port, bool *found_p, bool *error_p) ...@@ -646,13 +649,14 @@ parse_hba(List *line, hbaPort *port, bool *found_p, bool *error_p)
/* Get the IP address either way */ /* Get the IP address either way */
ret = getaddrinfo2(token, NULL, &hints, &file_ip_addr); ret = getaddrinfo2(token, NULL, &hints, &file_ip_addr);
if (ret) if (ret || !file_ip_addr)
{ {
elog(LOG, "getaddrinfo2() returned %d", ret); ereport(LOG,
(errcode(ERRCODE_CONFIG_FILE_ERROR),
errmsg("failed to interpret IP address \"%s\" in config file: %s",
token, gai_strerror(ret))));
if (cidr_slash) if (cidr_slash)
{
*cidr_slash = '/'; *cidr_slash = '/';
}
goto hba_syntax; goto hba_syntax;
} }
...@@ -669,9 +673,7 @@ parse_hba(List *line, hbaPort *port, bool *found_p, bool *error_p) ...@@ -669,9 +673,7 @@ parse_hba(List *line, hbaPort *port, bool *found_p, bool *error_p)
*cidr_slash = '/'; *cidr_slash = '/';
if (SockAddr_cidr_mask(&mask, cidr_slash + 1, if (SockAddr_cidr_mask(&mask, cidr_slash + 1,
file_ip_addr->ai_family) < 0) file_ip_addr->ai_family) < 0)
{ goto hba_syntax;
goto hba_syntax;
}
} }
else else
{ {
...@@ -682,16 +684,13 @@ parse_hba(List *line, hbaPort *port, bool *found_p, bool *error_p) ...@@ -682,16 +684,13 @@ parse_hba(List *line, hbaPort *port, bool *found_p, bool *error_p)
token = lfirst(line); token = lfirst(line);
ret = getaddrinfo2(token, NULL, &hints, &file_ip_mask); ret = getaddrinfo2(token, NULL, &hints, &file_ip_mask);
if (ret) if (ret || !file_ip_mask)
{
goto hba_syntax; goto hba_syntax;
}
mask = (struct sockaddr_storage *)file_ip_mask->ai_addr; mask = (struct sockaddr_storage *)file_ip_mask->ai_addr;
if(file_ip_addr->ai_family != mask->ss_family) if(file_ip_addr->ai_family != mask->ss_family)
{
goto hba_syntax; goto hba_syntax;
}
} }
/* Read the rest of the line. */ /* Read the rest of the line. */
...@@ -705,14 +704,11 @@ parse_hba(List *line, hbaPort *port, bool *found_p, bool *error_p) ...@@ -705,14 +704,11 @@ parse_hba(List *line, hbaPort *port, bool *found_p, bool *error_p)
/* Must meet network restrictions */ /* Must meet network restrictions */
if (!rangeSockAddr(&port->raddr.addr, if (!rangeSockAddr(&port->raddr.addr,
(struct sockaddr_storage *)file_ip_addr->ai_addr, mask)) (struct sockaddr_storage *)file_ip_addr->ai_addr, mask))
{
goto hba_freeaddr; goto hba_freeaddr;
}
freeaddrinfo2(hints.ai_family, file_ip_addr); freeaddrinfo2(hints.ai_family, file_ip_addr);
if (file_ip_mask) if (file_ip_mask)
{
freeaddrinfo2(hints.ai_family, file_ip_mask); freeaddrinfo2(hints.ai_family, file_ip_mask);
}
} }
else else
goto hba_syntax; goto hba_syntax;
...@@ -727,22 +723,24 @@ parse_hba(List *line, hbaPort *port, bool *found_p, bool *error_p) ...@@ -727,22 +723,24 @@ parse_hba(List *line, hbaPort *port, bool *found_p, bool *error_p)
return; return;
hba_syntax: hba_syntax:
elog(LOG, "parse_hba: invalid syntax in pg_hba.conf file at line %d, token \"%s\"", if (line)
line_number, ereport(LOG,
line ? (const char *) lfirst(line) : "(end of line)"); (errcode(ERRCODE_CONFIG_FILE_ERROR),
errmsg("invalid entry in pg_hba.conf file at line %d, token \"%s\"",
line_number, (const char *) lfirst(line))));
else
ereport(LOG,
(errcode(ERRCODE_CONFIG_FILE_ERROR),
errmsg("missing entry in pg_hba.conf file at end of line %d",
line_number)));
*error_p = true; *error_p = true;
hba_freeaddr: hba_freeaddr:
if (file_ip_addr) if (file_ip_addr)
{
freeaddrinfo2(hints.ai_family, file_ip_addr); freeaddrinfo2(hints.ai_family, file_ip_addr);
}
if (file_ip_mask) if (file_ip_mask)
{
freeaddrinfo2(hints.ai_family, file_ip_mask); freeaddrinfo2(hints.ai_family, file_ip_mask);
}
return;
} }
...@@ -790,7 +788,9 @@ group_openfile(void) ...@@ -790,7 +788,9 @@ group_openfile(void)
groupfile = AllocateFile(filename, "r"); groupfile = AllocateFile(filename, "r");
if (groupfile == NULL && errno != ENOENT) if (groupfile == NULL && errno != ENOENT)
elog(LOG, "could not open %s: %m", filename); ereport(LOG,
(errcode_for_file_access(),
errmsg("could not open \"%s\": %m", filename)));
pfree(filename); pfree(filename);
...@@ -812,7 +812,9 @@ user_openfile(void) ...@@ -812,7 +812,9 @@ user_openfile(void)
pwdfile = AllocateFile(filename, "r"); pwdfile = AllocateFile(filename, "r");
if (pwdfile == NULL && errno != ENOENT) if (pwdfile == NULL && errno != ENOENT)
elog(LOG, "could not open %s: %m", filename); ereport(LOG,
(errcode_for_file_access(),
errmsg("could not open \"%s\": %m", filename)));
pfree(filename); pfree(filename);
...@@ -929,9 +931,10 @@ load_hba(void) ...@@ -929,9 +931,10 @@ load_hba(void)
file = AllocateFile(conf_file, "r"); file = AllocateFile(conf_file, "r");
if (file == NULL) if (file == NULL)
elog(FATAL, ereport(FATAL,
"load_hba: Unable to open authentication config file \"%s\": %m", (errcode_for_file_access(),
conf_file); errmsg("could not open config file \"%s\": %m",
conf_file)));
hba_lines = tokenize_file(file); hba_lines = tokenize_file(file);
FreeFile(file); FreeFile(file);
...@@ -989,12 +992,18 @@ parse_ident_usermap(List *line, const char *usermap_name, const char *pg_user, ...@@ -989,12 +992,18 @@ parse_ident_usermap(List *line, const char *usermap_name, const char *pg_user,
return; return;
ident_syntax: ident_syntax:
elog(LOG, "parse_ident_usermap: invalid syntax in pg_ident.conf file at line %d, token \"%s\"", if (line)
line_number, ereport(LOG,
line ? (const char *) lfirst(line) : "(end of line)"); (errcode(ERRCODE_CONFIG_FILE_ERROR),
errmsg("invalid entry in pg_ident.conf file at line %d, token \"%s\"",
line_number, (const char *) lfirst(line))));
else
ereport(LOG,
(errcode(ERRCODE_CONFIG_FILE_ERROR),
errmsg("missing entry in pg_ident.conf file at end of line %d",
line_number)));
*error_p = true; *error_p = true;
return;
} }
...@@ -1021,10 +1030,9 @@ check_ident_usermap(const char *usermap_name, ...@@ -1021,10 +1030,9 @@ check_ident_usermap(const char *usermap_name,
if (usermap_name == NULL || usermap_name[0] == '\0') if (usermap_name == NULL || usermap_name[0] == '\0')
{ {
elog(LOG, "check_ident_usermap: hba configuration file does not " ereport(LOG,
"have the usermap field filled in in the entry that pertains " (errcode(ERRCODE_CONFIG_FILE_ERROR),
"to this connection. That field is essential for Ident-based " errmsg("cannot use IDENT authentication without usermap field")));
"authentication.");
found_entry = false; found_entry = false;
} }
else if (strcmp(usermap_name, "sameuser") == 0) else if (strcmp(usermap_name, "sameuser") == 0)
...@@ -1070,9 +1078,10 @@ load_ident(void) ...@@ -1070,9 +1078,10 @@ load_ident(void)
file = AllocateFile(map_file, "r"); file = AllocateFile(map_file, "r");
if (file == NULL) if (file == NULL)
{ {
/* The open of the map file failed. */ ereport(LOG,
elog(LOG, "load_ident: Unable to open usermap file \"%s\": %m", (errcode_for_file_access(),
map_file); errmsg("could not open usermap file \"%s\": %m",
map_file)));
} }
else else
{ {
...@@ -1192,18 +1201,22 @@ ident_inet(const SockAddr remote_addr, ...@@ -1192,18 +1201,22 @@ ident_inet(const SockAddr remote_addr,
char local_addr_s[NI_MAXHOST]; char local_addr_s[NI_MAXHOST];
char local_port[NI_MAXSERV]; char local_port[NI_MAXSERV];
char ident_port[NI_MAXSERV]; char ident_port[NI_MAXSERV];
char ident_query[80];
char ident_response[80 + IDENT_USERNAME_MAX];
struct addrinfo *ident_serv = NULL, *la = NULL, hints; struct addrinfo *ident_serv = NULL, *la = NULL, hints;
/* Might look a little weird to first convert it to text and /*
* then back to sockaddr, but it's protocol indepedant. */ * Might look a little weird to first convert it to text and
getnameinfo((struct sockaddr *)&remote_addr.addr, * then back to sockaddr, but it's protocol independent.
remote_addr.salen, remote_addr_s, sizeof(remote_addr_s), */
remote_port, sizeof(remote_port), getnameinfo((struct sockaddr *)&remote_addr.addr, remote_addr.salen,
NI_NUMERICHOST|NI_NUMERICSERV); remote_addr_s, sizeof(remote_addr_s),
getnameinfo((struct sockaddr *)&local_addr.addr, remote_port, sizeof(remote_port),
local_addr.salen, local_addr_s, sizeof(local_addr_s), NI_NUMERICHOST | NI_NUMERICSERV);
local_port, sizeof(local_port), getnameinfo((struct sockaddr *)&local_addr.addr, local_addr.salen,
NI_NUMERICHOST|NI_NUMERICSERV); local_addr_s, sizeof(local_addr_s),
local_port, sizeof(local_port),
NI_NUMERICHOST | NI_NUMERICSERV);
snprintf(ident_port, sizeof(ident_port), "%d", IDENT_PORT); snprintf(ident_port, sizeof(ident_port), "%d", IDENT_PORT);
hints.ai_flags = AI_NUMERICHOST; hints.ai_flags = AI_NUMERICHOST;
...@@ -1214,94 +1227,104 @@ ident_inet(const SockAddr remote_addr, ...@@ -1214,94 +1227,104 @@ ident_inet(const SockAddr remote_addr,
hints.ai_canonname = NULL; hints.ai_canonname = NULL;
hints.ai_addr = NULL; hints.ai_addr = NULL;
hints.ai_next = NULL; hints.ai_next = NULL;
getaddrinfo2(remote_addr_s, ident_port, &hints, &ident_serv); rc = getaddrinfo2(remote_addr_s, ident_port, &hints, &ident_serv);
getaddrinfo2(local_addr_s, NULL, &hints, &la); if (rc || !ident_serv)
return false; /* we don't expect this to happen */
hints.ai_flags = AI_NUMERICHOST;
hints.ai_family = local_addr.addr.ss_family;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = 0;
hints.ai_addrlen = 0;
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
hints.ai_next = NULL;
rc = getaddrinfo2(local_addr_s, NULL, &hints, &la);
if (rc || !la)
return false; /* we don't expect this to happen */
sock_fd = socket(ident_serv->ai_family, ident_serv->ai_socktype, sock_fd = socket(ident_serv->ai_family, ident_serv->ai_socktype,
ident_serv->ai_protocol); ident_serv->ai_protocol);
if (sock_fd < 0)
if (sock_fd == -1)
{ {
elog(LOG, "Failed to create socket on which to talk to Ident server: %m"); ereport(LOG,
(errcode_for_socket_access(),
errmsg("could not create socket for IDENT connection: %m")));
ident_return = false; ident_return = false;
goto ident_inet_done;
} }
else /*
* Bind to the address which the client originally contacted,
* otherwise the ident server won't be able to match up the right
* connection. This is necessary if the PostgreSQL server is
* running on an IP alias.
*/
rc = bind(sock_fd, la->ai_addr, la->ai_addrlen);
if (rc != 0)
{ {
/* ereport(LOG,
* Bind to the address which the client originally contacted, (errcode_for_socket_access(),
* otherwise the ident server won't be able to match up the right errmsg("could not bind to local address \"%s\": %m",
* connection. This is necessary if the PostgreSQL server is local_addr_s)));
* running on an IP alias. ident_return = false;
*/ goto ident_inet_done;
}
rc = bind(sock_fd, la->ai_addr, la->ai_addrlen); rc = connect(sock_fd, ident_serv->ai_addr,
if (rc == 0) ident_serv->ai_addrlen);
{ if (rc != 0)
rc = connect(sock_fd, ident_serv->ai_addr, {
ident_serv->ai_addrlen); ereport(LOG,
} (errcode_for_socket_access(),
if (rc != 0) errmsg("could not connect to IDENT server at address \"%s\", port %s): %m",
{ remote_addr_s, ident_port)));
int save_errno = errno; ident_return = false;
goto ident_inet_done;
}
elog(LOG, "Unable to connect to Ident server on the host which is " /* The query we send to the Ident server */
"trying to connect to Postgres " snprintf(ident_query, sizeof(ident_query), "%s,%s\r\n",
"(Address %s, Port %s): %s", remote_addr_s, remote_port, local_port);
ident_port, strerror(save_errno));
ident_return = false;
}
else
{
char ident_query[80];
/* The query we send to the Ident server */ /* loop in case send is interrupted */
snprintf(ident_query, sizeof(ident_query), "%s,%s\r\n", do
remote_port, local_port); {
/* loop in case send is interrupted */ rc = send(sock_fd, ident_query, strlen(ident_query), 0);
do } while (rc < 0 && errno == EINTR);
{
rc = send(sock_fd, ident_query, strlen(ident_query), 0);
} while (rc < 0 && errno == EINTR);
if (rc < 0)
{
int save_errno = errno;
elog(LOG, "Unable to send query to Ident server on the host which is " if (rc < 0)
"trying to connect to Postgres (Host %s, Port %s), " {
"even though we successfully connected to it: %s", ereport(LOG,
remote_addr_s, ident_port, strerror(save_errno)); (errcode_for_socket_access(),
ident_return = false; errmsg("could not send query to IDENT server at address \"%s\", port %s): %m",
} remote_addr_s, ident_port)));
else ident_return = false;
{ goto ident_inet_done;
char ident_response[80 + IDENT_USERNAME_MAX]; }
rc = recv(sock_fd, ident_response, do
sizeof(ident_response) - 1, 0); {
if (rc < 0) rc = recv(sock_fd, ident_response, sizeof(ident_response) - 1, 0);
{ } while (rc < 0 && errno == EINTR);
int save_errno = errno;
if (rc < 0)
elog(LOG, "Unable to receive response from Ident server " {
"on the host which is " ereport(LOG,
"trying to connect to Postgres (Host %s, Port %s), " (errcode_for_socket_access(),
"even though we successfully sent our query to it: %s", errmsg("could not receive response from IDENT server at address \"%s\", port %s): %m",
remote_addr_s, ident_port, remote_addr_s, ident_port)));
strerror(save_errno)); ident_return = false;
ident_return = false; goto ident_inet_done;
}
else
{
ident_response[rc] = '\0';
ident_return = interpret_ident_response(ident_response,
ident_user);
}
}
closesocket(sock_fd);
}
} }
freeaddrinfo2(hints.ai_family, la);
freeaddrinfo2(hints.ai_family, ident_serv); ident_response[rc] = '\0';
ident_return = interpret_ident_response(ident_response, ident_user);
ident_inet_done:
if (sock_fd >= 0)
closesocket(sock_fd);
freeaddrinfo2(remote_addr.addr.ss_family, ident_serv);
freeaddrinfo2(local_addr.addr.ss_family, la);
return ident_return; return ident_return;
} }
...@@ -1325,7 +1348,9 @@ ident_unix(int sock, char *ident_user) ...@@ -1325,7 +1348,9 @@ ident_unix(int sock, char *ident_user)
if (getpeereid(sock,&uid,&gid) != 0) if (getpeereid(sock,&uid,&gid) != 0)
{ {
/* We didn't get a valid credentials struct. */ /* We didn't get a valid credentials struct. */
elog(LOG, "ident_unix: error receiving credentials: %m"); ereport(LOG,
(errcode_for_socket_access(),
errmsg("could not receive credentials: %m")));
return false; return false;
} }
...@@ -1333,8 +1358,9 @@ ident_unix(int sock, char *ident_user) ...@@ -1333,8 +1358,9 @@ ident_unix(int sock, char *ident_user)
if (pass == NULL) if (pass == NULL)
{ {
elog(LOG, "ident_unix: unknown local user with uid %d", ereport(LOG,
(int) uid); (errmsg("local user with uid %d is not known to getpwuid",
(int) uid)));
return false; return false;
} }
...@@ -1353,7 +1379,9 @@ ident_unix(int sock, char *ident_user) ...@@ -1353,7 +1379,9 @@ ident_unix(int sock, char *ident_user)
so_len != sizeof(peercred)) so_len != sizeof(peercred))
{ {
/* We didn't get a valid credentials struct. */ /* We didn't get a valid credentials struct. */
elog(LOG, "ident_unix: error receiving credentials: %m"); ereport(LOG,
(errcode_for_socket_access(),
errmsg("could not receive credentials: %m")));
return false; return false;
} }
...@@ -1361,8 +1389,9 @@ ident_unix(int sock, char *ident_user) ...@@ -1361,8 +1389,9 @@ ident_unix(int sock, char *ident_user)
if (pass == NULL) if (pass == NULL)
{ {
elog(LOG, "ident_unix: unknown local user with uid %d", ereport(LOG,
(int) peercred.uid); (errmsg("local user with uid %d is not known to getpwuid",
(int) peercred.uid);
return false; return false;
} }
...@@ -1418,17 +1447,21 @@ ident_unix(int sock, char *ident_user) ...@@ -1418,17 +1447,21 @@ ident_unix(int sock, char *ident_user)
cmsg->cmsg_len < sizeof(cmsgmem) || cmsg->cmsg_len < sizeof(cmsgmem) ||
cmsg->cmsg_type != SCM_CREDS) cmsg->cmsg_type != SCM_CREDS)
{ {
elog(LOG, "ident_unix: error receiving credentials: %m"); ereport(LOG,
(errcode_for_socket_access(),
errmsg("could not receive credentials: %m")));
return false; return false;
} }
cred = (Cred *) CMSG_DATA(cmsg); cred = (Cred *) CMSG_DATA(cmsg);
pw = getpwuid(cred->cruid); pw = getpwuid(cred->cruid);
if (pw == NULL) if (pw == NULL)
{ {
elog(LOG, "ident_unix: unknown local user with uid %d", ereport(LOG,
(int) cred->cruid); (errmsg("local user with uid %d is not known to getpwuid",
(int) cred->cruid);
return false; return false;
} }
...@@ -1437,7 +1470,9 @@ ident_unix(int sock, char *ident_user) ...@@ -1437,7 +1470,9 @@ ident_unix(int sock, char *ident_user)
return true; return true;
#else #else
elog(LOG, "'ident' auth is not supported on local connections on this platform"); ereport(LOG,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("IDENT auth is not supported on local connections on this platform")));
return false; return false;
#endif #endif
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
* Unfortunately, COPY OUT was designed to commandeer the communication * Unfortunately, COPY OUT was designed to commandeer the communication
* channel (it just transfers data without wrapping it into messages). * channel (it just transfers data without wrapping it into messages).
* No other messages can be sent while COPY OUT is in progress; and if the * No other messages can be sent while COPY OUT is in progress; and if the
* copy is aborted by an elog(ERROR), we need to close out the copy so that * copy is aborted by an ereport(ERROR), we need to close out the copy so that
* the frontend gets back into sync. Therefore, these routines have to be * the frontend gets back into sync. Therefore, these routines have to be
* aware of COPY OUT state. (New COPY-OUT is message-based and does *not* * aware of COPY OUT state. (New COPY-OUT is message-based and does *not*
* set the DoingCopyOut flag.) * set the DoingCopyOut flag.)
...@@ -20,8 +20,8 @@ ...@@ -20,8 +20,8 @@
* to send. Instead, use the routines in pqformat.c to construct the message * to send. Instead, use the routines in pqformat.c to construct the message
* in a buffer and then emit it in one call to pq_putmessage. This ensures * in a buffer and then emit it in one call to pq_putmessage. This ensures
* that the channel will not be clogged by an incomplete message if execution * that the channel will not be clogged by an incomplete message if execution
* is aborted by elog(ERROR) partway through the message. The only non-libpq * is aborted by ereport(ERROR) partway through the message. The only
* code that should call pq_putbytes directly is old-style COPY OUT. * non-libpq code that should call pq_putbytes directly is old-style COPY OUT.
* *
* At one time, libpq was shared between frontend and backend, but now * At one time, libpq was shared between frontend and backend, but now
* the backend's "backend/libpq" is quite separate from "interfaces/libpq". * the backend's "backend/libpq" is quite separate from "interfaces/libpq".
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Header: /cvsroot/pgsql/src/backend/libpq/pqcomm.c,v 1.157 2003/06/12 07:36:51 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/libpq/pqcomm.c,v 1.158 2003/07/22 19:00:10 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -237,8 +237,9 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber, ...@@ -237,8 +237,9 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
ret = getaddrinfo2(hostName, service, &hint, &addrs); ret = getaddrinfo2(hostName, service, &hint, &addrs);
if (ret || addrs == NULL) if (ret || addrs == NULL)
{ {
elog(LOG, "server socket failure: getaddrinfo2(): %s", ereport(LOG,
gai_strerror(ret)); (errmsg("failed to translate hostname to address: %s",
gai_strerror(ret))));
freeaddrinfo2(hint.ai_family, addrs); freeaddrinfo2(hint.ai_family, addrs);
return STATUS_ERROR; return STATUS_ERROR;
} }
...@@ -269,8 +270,9 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber, ...@@ -269,8 +270,9 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
if ((fd = socket(addr->ai_family, addr->ai_socktype, if ((fd = socket(addr->ai_family, addr->ai_socktype,
addr->ai_protocol)) < 0) addr->ai_protocol)) < 0)
{ {
elog(LOG, "server socket failure: socket(): %s", ereport(LOG,
strerror(errno)); (errcode_for_socket_access(),
errmsg("failed to create socket: %m")));
continue; continue;
} }
...@@ -281,9 +283,9 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber, ...@@ -281,9 +283,9 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
if ((setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, if ((setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
(char *) &one, sizeof(one))) == -1) (char *) &one, sizeof(one))) == -1)
{ {
elog(LOG, "server socket failure: " ereport(LOG,
"setsockopt(SO_REUSEADDR): %s", (errcode_for_socket_access(),
strerror(errno)); errmsg("setsockopt(SO_REUSEADDR) failed: %m")));
closesocket(fd); closesocket(fd);
continue; continue;
} }
...@@ -295,9 +297,9 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber, ...@@ -295,9 +297,9 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY,
(char *)&one, sizeof(one)) == -1) (char *)&one, sizeof(one)) == -1)
{ {
elog(LOG, "server socket failure: " ereport(LOG,
"setsockopt(IPV6_V6ONLY): %s", (errcode_for_socket_access(),
strerror(errno)); errmsg("setsockopt(IPV6_V6ONLY) failed: %m")));
closesocket(fd); closesocket(fd);
continue; continue;
} }
...@@ -313,19 +315,16 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber, ...@@ -313,19 +315,16 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
err = bind(fd, addr->ai_addr, addr->ai_addrlen); err = bind(fd, addr->ai_addr, addr->ai_addrlen);
if (err < 0) if (err < 0)
{ {
elog(LOG, "server socket failure: bind(): %s\n" ereport(LOG,
"\tIs another postmaster already running on " (errcode_for_socket_access(),
"port %d?", strerror(errno), (int) portNumber); errmsg("failed to bind server socket: %m"),
if (addr->ai_family == AF_UNIX) (addr->ai_family == AF_UNIX) ?
{ errhint("Is another postmaster already running on port %d?"
elog(LOG, "\tIf not, remove socket node (%s) " " If not, remove socket node \"%s\" and retry.",
"and retry.", sock_path); (int) portNumber, sock_path) :
} errhint("Is another postmaster already running on port %d?"
else " If not, wait a few seconds and retry.",
{ (int) portNumber)));
elog(LOG, "\tIf not, wait a few seconds and "
"retry.");
}
closesocket(fd); closesocket(fd);
continue; continue;
} }
...@@ -354,8 +353,9 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber, ...@@ -354,8 +353,9 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
err = listen(fd, maxconn); err = listen(fd, maxconn);
if (err < 0) if (err < 0)
{ {
elog(LOG, "server socket failure: listen(): %s", ereport(LOG,
strerror(errno)); (errcode_for_socket_access(),
errmsg("failed to listen on server socket: %m")));
closesocket(fd); closesocket(fd);
continue; continue;
} }
...@@ -417,7 +417,7 @@ Setup_AF_UNIX(void) ...@@ -417,7 +417,7 @@ Setup_AF_UNIX(void)
if (Unix_socket_group[0] != '\0') if (Unix_socket_group[0] != '\0')
{ {
#ifdef WIN32 #ifdef WIN32
elog(FATAL, "Config value 'unix_socket_group' not supported on this platform"); elog(WARNING, "configuration item unix_socket_group is not supported on this platform");
#else #else
char *endptr; char *endptr;
unsigned long int val; unsigned long int val;
...@@ -435,16 +435,19 @@ Setup_AF_UNIX(void) ...@@ -435,16 +435,19 @@ Setup_AF_UNIX(void)
gr = getgrnam(Unix_socket_group); gr = getgrnam(Unix_socket_group);
if (!gr) if (!gr)
{ {
elog(LOG, "server socket failure: no such group '%s'", ereport(LOG,
Unix_socket_group); (errmsg("group \"%s\" does not exist",
Unix_socket_group)));
return STATUS_ERROR; return STATUS_ERROR;
} }
gid = gr->gr_gid; gid = gr->gr_gid;
} }
if (chown(sock_path, -1, gid) == -1) if (chown(sock_path, -1, gid) == -1)
{ {
elog(LOG, "server socket failure: could not set group of %s: %s", ereport(LOG,
sock_path, strerror(errno)); (errcode_for_file_access(),
errmsg("could not set group of \"%s\": %m",
sock_path)));
return STATUS_ERROR; return STATUS_ERROR;
} }
#endif #endif
...@@ -452,8 +455,10 @@ Setup_AF_UNIX(void) ...@@ -452,8 +455,10 @@ Setup_AF_UNIX(void)
if (chmod(sock_path, Unix_socket_permissions) == -1) if (chmod(sock_path, Unix_socket_permissions) == -1)
{ {
elog(LOG, "server socket failure: could not set permissions on %s: %s", ereport(LOG,
sock_path, strerror(errno)); (errcode_for_file_access(),
errmsg("could not set permissions of \"%s\": %m",
sock_path)));
return STATUS_ERROR; return STATUS_ERROR;
} }
return STATUS_OK; return STATUS_OK;
...@@ -475,13 +480,15 @@ Setup_AF_UNIX(void) ...@@ -475,13 +480,15 @@ Setup_AF_UNIX(void)
int int
StreamConnection(int server_fd, Port *port) StreamConnection(int server_fd, Port *port)
{ {
/* accept connection (and fill in the client (remote) address) */ /* accept connection and fill in the client (remote) address */
port->raddr.salen = sizeof(port->raddr.addr); port->raddr.salen = sizeof(port->raddr.addr);
if ((port->sock = accept(server_fd, if ((port->sock = accept(server_fd,
(struct sockaddr *) &port->raddr.addr, (struct sockaddr *) &port->raddr.addr,
&port->raddr.salen)) < 0) &port->raddr.salen)) < 0)
{ {
elog(LOG, "StreamConnection: accept() failed: %m"); ereport(LOG,
(errcode_for_socket_access(),
errmsg("could not accept new connection: %m")));
return STATUS_ERROR; return STATUS_ERROR;
} }
...@@ -496,30 +503,33 @@ StreamConnection(int server_fd, Port *port) ...@@ -496,30 +503,33 @@ StreamConnection(int server_fd, Port *port)
/* fill in the server (local) address */ /* fill in the server (local) address */
port->laddr.salen = sizeof(port->laddr.addr); port->laddr.salen = sizeof(port->laddr.addr);
if (getsockname(port->sock, (struct sockaddr *) & port->laddr.addr, if (getsockname(port->sock,
(struct sockaddr *) & port->laddr.addr,
&port->laddr.salen) < 0) &port->laddr.salen) < 0)
{ {
elog(LOG, "StreamConnection: getsockname() failed: %m"); elog(LOG, "getsockname() failed: %m");
return STATUS_ERROR; return STATUS_ERROR;
} }
/* select NODELAY and KEEPALIVE options if it's a TCP connection */ /* select NODELAY and KEEPALIVE options if it's a TCP connection */
if (!IS_AF_UNIX(port->laddr.addr.ss_family)) if (!IS_AF_UNIX(port->laddr.addr.ss_family))
{ {
int on = 1; int on;
#ifdef TCP_NODELAY #ifdef TCP_NODELAY
on = 1;
if (setsockopt(port->sock, IPPROTO_TCP, TCP_NODELAY, if (setsockopt(port->sock, IPPROTO_TCP, TCP_NODELAY,
(char *) &on, sizeof(on)) < 0) (char *) &on, sizeof(on)) < 0)
{ {
elog(LOG, "StreamConnection: setsockopt(TCP_NODELAY) failed: %m"); elog(LOG, "setsockopt(TCP_NODELAY) failed: %m");
return STATUS_ERROR; return STATUS_ERROR;
} }
#endif #endif
on = 1;
if (setsockopt(port->sock, SOL_SOCKET, SO_KEEPALIVE, if (setsockopt(port->sock, SOL_SOCKET, SO_KEEPALIVE,
(char *) &on, sizeof(on)) < 0) (char *) &on, sizeof(on)) < 0)
{ {
elog(LOG, "StreamConnection: setsockopt(SO_KEEPALIVE) failed: %m"); elog(LOG, "setsockopt(SO_KEEPALIVE) failed: %m");
return STATUS_ERROR; return STATUS_ERROR;
} }
} }
...@@ -622,11 +632,13 @@ pq_recvbuf(void) ...@@ -622,11 +632,13 @@ pq_recvbuf(void)
continue; /* Ok if interrupted */ continue; /* Ok if interrupted */
/* /*
* Careful: an elog() that tries to write to the client would * Careful: an ereport() that tries to write to the client would
* cause recursion to here, leading to stack overflow and core * cause recursion to here, leading to stack overflow and core
* dump! This message must go *only* to the postmaster log. * dump! This message must go *only* to the postmaster log.
*/ */
elog(COMMERROR, "pq_recvbuf: recv() failed: %m"); ereport(COMMERROR,
(errcode_for_socket_access(),
errmsg("could not receive data from client: %m")));
return EOF; return EOF;
} }
if (r == 0) if (r == 0)
...@@ -787,7 +799,9 @@ pq_getmessage(StringInfo s, int maxlen) ...@@ -787,7 +799,9 @@ pq_getmessage(StringInfo s, int maxlen)
/* Read message length word */ /* Read message length word */
if (pq_getbytes((char *) &len, 4) == EOF) if (pq_getbytes((char *) &len, 4) == EOF)
{ {
elog(COMMERROR, "unexpected EOF within message length word"); ereport(COMMERROR,
(errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("unexpected EOF within message length word")));
return EOF; return EOF;
} }
...@@ -797,7 +811,9 @@ pq_getmessage(StringInfo s, int maxlen) ...@@ -797,7 +811,9 @@ pq_getmessage(StringInfo s, int maxlen)
if (len < 0 || if (len < 0 ||
(maxlen > 0 && len > maxlen)) (maxlen > 0 && len > maxlen))
{ {
elog(COMMERROR, "invalid message length"); ereport(COMMERROR,
(errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("invalid message length")));
return EOF; return EOF;
} }
...@@ -809,7 +825,9 @@ pq_getmessage(StringInfo s, int maxlen) ...@@ -809,7 +825,9 @@ pq_getmessage(StringInfo s, int maxlen)
/* And grab the message */ /* And grab the message */
if (pq_getbytes(s->data, len) == EOF) if (pq_getbytes(s->data, len) == EOF)
{ {
elog(COMMERROR, "incomplete client message"); ereport(COMMERROR,
(errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("incomplete message from client")));
return EOF; return EOF;
} }
s->len = len; s->len = len;
...@@ -874,7 +892,7 @@ pq_flush(void) ...@@ -874,7 +892,7 @@ pq_flush(void)
continue; /* Ok if we were interrupted */ continue; /* Ok if we were interrupted */
/* /*
* Careful: an elog() that tries to write to the client would * Careful: an ereport() that tries to write to the client would
* cause recursion to here, leading to stack overflow and core * cause recursion to here, leading to stack overflow and core
* dump! This message must go *only* to the postmaster log. * dump! This message must go *only* to the postmaster log.
* *
...@@ -885,7 +903,9 @@ pq_flush(void) ...@@ -885,7 +903,9 @@ pq_flush(void)
if (errno != last_reported_send_errno) if (errno != last_reported_send_errno)
{ {
last_reported_send_errno = errno; last_reported_send_errno = errno;
elog(COMMERROR, "pq_flush: send() failed: %m"); ereport(COMMERROR,
(errcode_for_socket_access(),
errmsg("could not send data to client: %m")));
} }
/* /*
......
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Header: /cvsroot/pgsql/src/backend/libpq/pqformat.c,v 1.31 2003/05/09 21:19:49 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/libpq/pqformat.c,v 1.32 2003/07/22 19:00:10 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -231,7 +231,7 @@ pq_sendint(StringInfo buf, int i, int b) ...@@ -231,7 +231,7 @@ pq_sendint(StringInfo buf, int i, int b)
appendBinaryStringInfo(buf, (char *) &n32, 4); appendBinaryStringInfo(buf, (char *) &n32, 4);
break; break;
default: default:
elog(ERROR, "pq_sendint: unsupported size %d", b); elog(ERROR, "unsupported integer size %d", b);
break; break;
} }
} }
...@@ -440,7 +440,9 @@ int ...@@ -440,7 +440,9 @@ int
pq_getmsgbyte(StringInfo msg) pq_getmsgbyte(StringInfo msg)
{ {
if (msg->cursor >= msg->len) if (msg->cursor >= msg->len)
elog(ERROR, "pq_getmsgbyte: no data left in message"); ereport(ERROR,
(errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("no data left in message")));
return (unsigned char) msg->data[msg->cursor++]; return (unsigned char) msg->data[msg->cursor++];
} }
...@@ -473,7 +475,7 @@ pq_getmsgint(StringInfo msg, int b) ...@@ -473,7 +475,7 @@ pq_getmsgint(StringInfo msg, int b)
result = ntohl(n32); result = ntohl(n32);
break; break;
default: default:
elog(ERROR, "pq_getmsgint: unsupported size %d", b); elog(ERROR, "unsupported integer size %d", b);
result = 0; /* keep compiler quiet */ result = 0; /* keep compiler quiet */
break; break;
} }
...@@ -586,7 +588,9 @@ pq_getmsgbytes(StringInfo msg, int datalen) ...@@ -586,7 +588,9 @@ pq_getmsgbytes(StringInfo msg, int datalen)
const char *result; const char *result;
if (datalen < 0 || datalen > (msg->len - msg->cursor)) if (datalen < 0 || datalen > (msg->len - msg->cursor))
elog(ERROR, "pq_getmsgbytes: insufficient data left in message"); ereport(ERROR,
(errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("insufficient data left in message")));
result = &msg->data[msg->cursor]; result = &msg->data[msg->cursor];
msg->cursor += datalen; msg->cursor += datalen;
return result; return result;
...@@ -602,7 +606,9 @@ void ...@@ -602,7 +606,9 @@ void
pq_copymsgbytes(StringInfo msg, char *buf, int datalen) pq_copymsgbytes(StringInfo msg, char *buf, int datalen)
{ {
if (datalen < 0 || datalen > (msg->len - msg->cursor)) if (datalen < 0 || datalen > (msg->len - msg->cursor))
elog(ERROR, "pq_copymsgbytes: insufficient data left in message"); ereport(ERROR,
(errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("insufficient data left in message")));
memcpy(buf, &msg->data[msg->cursor], datalen); memcpy(buf, &msg->data[msg->cursor], datalen);
msg->cursor += datalen; msg->cursor += datalen;
} }
...@@ -621,7 +627,9 @@ pq_getmsgtext(StringInfo msg, int rawbytes, int *nbytes) ...@@ -621,7 +627,9 @@ pq_getmsgtext(StringInfo msg, int rawbytes, int *nbytes)
char *p; char *p;
if (rawbytes < 0 || rawbytes > (msg->len - msg->cursor)) if (rawbytes < 0 || rawbytes > (msg->len - msg->cursor))
elog(ERROR, "pq_getmsgtext: insufficient data left in message"); ereport(ERROR,
(errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("insufficient data left in message")));
str = &msg->data[msg->cursor]; str = &msg->data[msg->cursor];
msg->cursor += rawbytes; msg->cursor += rawbytes;
...@@ -661,7 +669,9 @@ pq_getmsgstring(StringInfo msg) ...@@ -661,7 +669,9 @@ pq_getmsgstring(StringInfo msg)
*/ */
slen = strlen(str); slen = strlen(str);
if (msg->cursor + slen >= msg->len) if (msg->cursor + slen >= msg->len)
elog(ERROR, "pq_getmsgstring: invalid string in message"); ereport(ERROR,
(errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("invalid string in message")));
msg->cursor += slen + 1; msg->cursor += slen + 1;
return (const char *) pg_client_to_server((unsigned char *) str, slen); return (const char *) pg_client_to_server((unsigned char *) str, slen);
...@@ -675,5 +685,7 @@ void ...@@ -675,5 +685,7 @@ void
pq_getmsgend(StringInfo msg) pq_getmsgend(StringInfo msg)
{ {
if (msg->cursor != msg->len) if (msg->cursor != msg->len)
elog(ERROR, "pq_getmsgend: invalid message format"); ereport(ERROR,
(errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("invalid message format")));
} }
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
* *
* Copyright (c) 2001-2003, PostgreSQL Global Development Group * Copyright (c) 2001-2003, PostgreSQL Global Development Group
* *
* $Header: /cvsroot/pgsql/src/backend/postmaster/pgstat.c,v 1.37 2003/06/12 07:36:51 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/postmaster/pgstat.c,v 1.38 2003/07/22 19:00:10 tgl Exp $
* ---------- * ----------
*/ */
#include "postgres.h" #include "postgres.h"
...@@ -81,13 +81,15 @@ static time_t last_pgstat_start_time; ...@@ -81,13 +81,15 @@ static time_t last_pgstat_start_time;
static long pgStatNumMessages = 0; static long pgStatNumMessages = 0;
static bool pgStatRunningInCollector = FALSE; static bool pgStatRunningInCollector = FALSE;
static int pgStatTabstatAlloc = 0; static int pgStatTabstatAlloc = 0;
static int pgStatTabstatUsed = 0; static int pgStatTabstatUsed = 0;
static PgStat_MsgTabstat **pgStatTabstatMessages = NULL; static PgStat_MsgTabstat **pgStatTabstatMessages = NULL;
#define TABSTAT_QUANTUM 4 /* we alloc this many at a time */
static int pgStatXactCommit = 0; static int pgStatXactCommit = 0;
static int pgStatXactRollback = 0; static int pgStatXactRollback = 0;
static TransactionId pgStatDBHashXact = InvalidTransactionId; static TransactionId pgStatDBHashXact = InvalidTransactionId;
static HTAB *pgStatDBHash = NULL; static HTAB *pgStatDBHash = NULL;
static HTAB *pgStatBeDead = NULL; static HTAB *pgStatBeDead = NULL;
...@@ -145,7 +147,7 @@ void ...@@ -145,7 +147,7 @@ void
pgstat_init(void) pgstat_init(void)
{ {
ACCEPT_TYPE_ARG3 alen; ACCEPT_TYPE_ARG3 alen;
struct addrinfo *addr, hints; struct addrinfo *addr = NULL, hints;
int ret; int ret;
/* /*
...@@ -190,15 +192,18 @@ pgstat_init(void) ...@@ -190,15 +192,18 @@ pgstat_init(void)
ret = getaddrinfo2("localhost", NULL, &hints, &addr); ret = getaddrinfo2("localhost", NULL, &hints, &addr);
if (ret || !addr) if (ret || !addr)
{ {
elog(LOG, "PGSTAT: getaddrinfo2() failed: %s", ereport(LOG,
gai_strerror(ret)); (errmsg("getaddrinfo2(\"localhost\") failed: %s",
gai_strerror(ret))));
goto startup_failed; goto startup_failed;
} }
if ((pgStatSock = socket(addr->ai_family, if ((pgStatSock = socket(addr->ai_family,
addr->ai_socktype, addr->ai_protocol)) < 0) addr->ai_socktype, addr->ai_protocol)) < 0)
{ {
elog(LOG, "PGSTAT: socket() failed: %m"); ereport(LOG,
(errcode_for_socket_access(),
errmsg("could not create socket for statistics: %m")));
goto startup_failed; goto startup_failed;
} }
...@@ -208,7 +213,9 @@ pgstat_init(void) ...@@ -208,7 +213,9 @@ pgstat_init(void)
*/ */
if (bind(pgStatSock, addr->ai_addr, addr->ai_addrlen) < 0) if (bind(pgStatSock, addr->ai_addr, addr->ai_addrlen) < 0)
{ {
elog(LOG, "PGSTAT: bind() failed: %m"); ereport(LOG,
(errcode_for_socket_access(),
errmsg("could not bind socket for statistics: %m")));
goto startup_failed; goto startup_failed;
} }
freeaddrinfo2(hints.ai_family, addr); freeaddrinfo2(hints.ai_family, addr);
...@@ -217,7 +224,9 @@ pgstat_init(void) ...@@ -217,7 +224,9 @@ pgstat_init(void)
alen = sizeof(pgStatAddr); alen = sizeof(pgStatAddr);
if (getsockname(pgStatSock, (struct sockaddr *)&pgStatAddr, &alen) < 0) if (getsockname(pgStatSock, (struct sockaddr *)&pgStatAddr, &alen) < 0)
{ {
elog(LOG, "PGSTAT: getsockname() failed: %m"); ereport(LOG,
(errcode_for_socket_access(),
errmsg("could not get address of socket for statistics: %m")));
goto startup_failed; goto startup_failed;
} }
...@@ -229,7 +238,9 @@ pgstat_init(void) ...@@ -229,7 +238,9 @@ pgstat_init(void)
*/ */
if (connect(pgStatSock, (struct sockaddr *) & pgStatAddr, alen) < 0) if (connect(pgStatSock, (struct sockaddr *) & pgStatAddr, alen) < 0)
{ {
elog(LOG, "PGSTAT: connect() failed: %m"); ereport(LOG,
(errcode_for_socket_access(),
errmsg("could not connect socket for statistics: %m")));
goto startup_failed; goto startup_failed;
} }
...@@ -241,7 +252,9 @@ pgstat_init(void) ...@@ -241,7 +252,9 @@ pgstat_init(void)
*/ */
if (FCNTL_NONBLOCK(pgStatSock) < 0) if (FCNTL_NONBLOCK(pgStatSock) < 0)
{ {
elog(LOG, "PGSTAT: fcntl() failed: %m"); ereport(LOG,
(errcode_for_socket_access(),
errmsg("could not set statistics socket to nonblock mode: %m")));
goto startup_failed; goto startup_failed;
} }
...@@ -250,7 +263,9 @@ pgstat_init(void) ...@@ -250,7 +263,9 @@ pgstat_init(void)
*/ */
if (pipe(pgStatPmPipe) < 0) if (pipe(pgStatPmPipe) < 0)
{ {
elog(LOG, "PGSTAT: pipe() failed: %m"); ereport(LOG,
(errcode_for_socket_access(),
errmsg("could not create pipe for statistics collector: %m")));
goto startup_failed; goto startup_failed;
} }
...@@ -258,9 +273,7 @@ pgstat_init(void) ...@@ -258,9 +273,7 @@ pgstat_init(void)
startup_failed: startup_failed:
if (addr) if (addr)
{
freeaddrinfo2(hints.ai_family, addr); freeaddrinfo2(hints.ai_family, addr);
}
if (pgStatSock >= 0) if (pgStatSock >= 0)
closesocket(pgStatSock); closesocket(pgStatSock);
...@@ -312,7 +325,8 @@ pgstat_start(void) ...@@ -312,7 +325,8 @@ pgstat_start(void)
*/ */
if (pgStatSock < 0) if (pgStatSock < 0)
{ {
elog(LOG, "PGSTAT: statistics collector startup skipped"); ereport(LOG,
(errmsg("statistics collector startup skipped")));
/* /*
* We can only get here if someone tries to manually turn * We can only get here if someone tries to manually turn
* pgstat_collect_startcollector on after it had been off. * pgstat_collect_startcollector on after it had been off.
...@@ -340,7 +354,8 @@ pgstat_start(void) ...@@ -340,7 +354,8 @@ pgstat_start(void)
/* Specific beos actions */ /* Specific beos actions */
beos_backend_startup_failed(); beos_backend_startup_failed();
#endif #endif
elog(LOG, "PGSTAT: fork() failed: %m"); ereport(LOG,
(errmsg("could not fork statistics buffer: %m")));
return; return;
case 0: case 0:
...@@ -744,7 +759,9 @@ pgstat_reset_counters(void) ...@@ -744,7 +759,9 @@ pgstat_reset_counters(void)
return; return;
if (!superuser()) if (!superuser())
elog(ERROR, "Only database superusers can reset statistic counters"); ereport(ERROR,
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
errmsg("must be superuser to reset statistics counters")));
pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_RESETCOUNTER); pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_RESETCOUNTER);
pgstat_send(&msg, sizeof(msg)); pgstat_send(&msg, sizeof(msg));
...@@ -802,32 +819,41 @@ pgstat_initstats(PgStat_Info *stats, Relation rel) ...@@ -802,32 +819,41 @@ pgstat_initstats(PgStat_Info *stats, Relation rel)
} }
/* /*
* On the first of all calls initialize the message buffers. * On the first of all calls create some message buffers.
*/ */
if (pgStatTabstatAlloc == 0) if (pgStatTabstatMessages == NULL)
{ {
pgStatTabstatAlloc = 4; PgStat_MsgTabstat *newMessages;
pgStatTabstatMessages = (PgStat_MsgTabstat **) PgStat_MsgTabstat **msgArray;
malloc(sizeof(PgStat_MsgTabstat *) * pgStatTabstatAlloc);
if (pgStatTabstatMessages == NULL) newMessages = (PgStat_MsgTabstat *)
malloc(sizeof(PgStat_MsgTabstat) * TABSTAT_QUANTUM);
if (newMessages == NULL)
{ {
elog(LOG, "PGSTATBE: malloc() failed"); ereport(LOG,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory")));
return; return;
} }
for (i = 0; i < pgStatTabstatAlloc; i++) msgArray = (PgStat_MsgTabstat **)
malloc(sizeof(PgStat_MsgTabstat *) * TABSTAT_QUANTUM);
if (msgArray == NULL)
{ {
pgStatTabstatMessages[i] = (PgStat_MsgTabstat *) free(newMessages);
malloc(sizeof(PgStat_MsgTabstat)); ereport(LOG,
if (pgStatTabstatMessages[i] == NULL) (errcode(ERRCODE_OUT_OF_MEMORY),
{ errmsg("out of memory")));
elog(LOG, "PGSTATBE: malloc() failed"); return;
return;
}
} }
MemSet(newMessages, 0, sizeof(PgStat_MsgTabstat) * TABSTAT_QUANTUM);
for (i = 0; i < TABSTAT_QUANTUM; i++)
msgArray[i] = newMessages++;
pgStatTabstatMessages = msgArray;
pgStatTabstatAlloc = TABSTAT_QUANTUM;
} }
/* /*
* Lookup the so far used table slots for this relation. * Search the already-used message slots for this relation.
*/ */
for (mb = 0; mb < pgStatTabstatUsed; mb++) for (mb = 0; mb < pgStatTabstatUsed; mb++)
{ {
...@@ -849,7 +875,6 @@ pgstat_initstats(PgStat_Info *stats, Relation rel) ...@@ -849,7 +875,6 @@ pgstat_initstats(PgStat_Info *stats, Relation rel)
*/ */
i = pgStatTabstatMessages[mb]->m_nentries++; i = pgStatTabstatMessages[mb]->m_nentries++;
useent = &pgStatTabstatMessages[mb]->m_entry[i]; useent = &pgStatTabstatMessages[mb]->m_entry[i];
memset(useent, 0, sizeof(PgStat_TableEntry));
useent->t_id = rel_id; useent->t_id = rel_id;
stats->tabentry = (void *) useent; stats->tabentry = (void *) useent;
return; return;
...@@ -860,27 +885,35 @@ pgstat_initstats(PgStat_Info *stats, Relation rel) ...@@ -860,27 +885,35 @@ pgstat_initstats(PgStat_Info *stats, Relation rel)
*/ */
if (pgStatTabstatUsed >= pgStatTabstatAlloc) if (pgStatTabstatUsed >= pgStatTabstatAlloc)
{ {
pgStatTabstatAlloc += 4; int newAlloc = pgStatTabstatAlloc + TABSTAT_QUANTUM;
pgStatTabstatMessages = (PgStat_MsgTabstat **) PgStat_MsgTabstat *newMessages;
realloc(pgStatTabstatMessages, PgStat_MsgTabstat **msgArray;
sizeof(PgStat_MsgTabstat *) * pgStatTabstatAlloc);
if (pgStatTabstatMessages == NULL) newMessages = (PgStat_MsgTabstat *)
malloc(sizeof(PgStat_MsgTabstat) * TABSTAT_QUANTUM);
if (newMessages == NULL)
{ {
pgStatTabstatAlloc -= 4; ereport(LOG,
elog(LOG, "PGSTATBE: malloc() failed"); (errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory")));
return; return;
} }
for (i = pgStatTabstatUsed; i < pgStatTabstatAlloc; i++) msgArray = (PgStat_MsgTabstat **)
realloc(pgStatTabstatMessages,
sizeof(PgStat_MsgTabstat *) * newAlloc);
if (msgArray == NULL)
{ {
pgStatTabstatMessages[i] = (PgStat_MsgTabstat *) free(newMessages);
malloc(sizeof(PgStat_MsgTabstat)); ereport(LOG,
if (pgStatTabstatMessages[i] == NULL) (errcode(ERRCODE_OUT_OF_MEMORY),
{ errmsg("out of memory")));
pgStatTabstatAlloc -= 4; return;
elog(LOG, "PGSTATBE: malloc() failed");
return;
}
} }
MemSet(newMessages, 0, sizeof(PgStat_MsgTabstat) * TABSTAT_QUANTUM);
for (i = 0; i < TABSTAT_QUANTUM; i++)
msgArray[pgStatTabstatAlloc + i] = newMessages++;
pgStatTabstatMessages = msgArray;
pgStatTabstatAlloc = newAlloc;
} }
/* /*
...@@ -889,10 +922,8 @@ pgstat_initstats(PgStat_Info *stats, Relation rel) ...@@ -889,10 +922,8 @@ pgstat_initstats(PgStat_Info *stats, Relation rel)
mb = pgStatTabstatUsed++; mb = pgStatTabstatUsed++;
pgStatTabstatMessages[mb]->m_nentries = 1; pgStatTabstatMessages[mb]->m_nentries = 1;
useent = &pgStatTabstatMessages[mb]->m_entry[0]; useent = &pgStatTabstatMessages[mb]->m_entry[0];
memset(useent, 0, sizeof(PgStat_TableEntry));
useent->t_id = rel_id; useent->t_id = rel_id;
stats->tabentry = (void *) useent; stats->tabentry = (void *) useent;
return;
} }
...@@ -1205,14 +1236,17 @@ pgstat_main(void) ...@@ -1205,14 +1236,17 @@ pgstat_main(void)
*/ */
if (pipe(pgStatPipe) < 0) if (pipe(pgStatPipe) < 0)
{ {
elog(LOG, "PGSTAT: pipe() failed: %m"); ereport(LOG,
(errcode_for_socket_access(),
errmsg("could not create pipe for statistics buffer: %m")));
exit(1); exit(1);
} }
switch (fork()) switch (fork())
{ {
case -1: case -1:
elog(LOG, "PGSTAT: fork() failed: %m"); ereport(LOG,
(errmsg("could not fork statistics collector: %m")));
exit(1); exit(1);
case 0: case 0:
...@@ -1266,7 +1300,10 @@ pgstat_main(void) ...@@ -1266,7 +1300,10 @@ pgstat_main(void)
&hash_ctl, HASH_ELEM | HASH_FUNCTION); &hash_ctl, HASH_ELEM | HASH_FUNCTION);
if (pgStatBeDead == NULL) if (pgStatBeDead == NULL)
{ {
elog(LOG, "PGSTAT: Creation of dead backend hash table failed"); /* assume the problem is out-of-memory */
ereport(LOG,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory in statistics collector --- abort")));
exit(1); exit(1);
} }
...@@ -1277,7 +1314,8 @@ pgstat_main(void) ...@@ -1277,7 +1314,8 @@ pgstat_main(void)
sizeof(PgStat_StatBeEntry) * MaxBackends); sizeof(PgStat_StatBeEntry) * MaxBackends);
if (pgStatBeTable == NULL) if (pgStatBeTable == NULL)
{ {
elog(LOG, "PGSTAT: Allocation of backend table failed"); ereport(LOG,
(errmsg("allocation of backend table failed")));
exit(1); exit(1);
} }
memset(pgStatBeTable, 0, sizeof(PgStat_StatBeEntry) * MaxBackends); memset(pgStatBeTable, 0, sizeof(PgStat_StatBeEntry) * MaxBackends);
...@@ -1341,7 +1379,9 @@ pgstat_main(void) ...@@ -1341,7 +1379,9 @@ pgstat_main(void)
{ {
if (errno == EINTR) if (errno == EINTR)
continue; continue;
elog(LOG, "PGSTAT: select() failed: %m"); ereport(LOG,
(errcode_for_socket_access(),
errmsg("select failed in statistics collector: %m")));
exit(1); exit(1);
} }
...@@ -1381,7 +1421,9 @@ pgstat_main(void) ...@@ -1381,7 +1421,9 @@ pgstat_main(void)
{ {
if (errno == EINTR) if (errno == EINTR)
continue; continue;
elog(LOG, "PGSTAT: read() failed: %m"); ereport(LOG,
(errcode_for_socket_access(),
errmsg("could not read from statistics pipe: %m")));
exit(1); exit(1);
} }
if (len == 0) /* EOF on the pipe! */ if (len == 0) /* EOF on the pipe! */
...@@ -1399,7 +1441,8 @@ pgstat_main(void) ...@@ -1399,7 +1441,8 @@ pgstat_main(void)
* sync with the buffer process somehow. Abort so * sync with the buffer process somehow. Abort so
* that we can restart both processes. * that we can restart both processes.
*/ */
elog(LOG, "PGSTAT: bogus message length"); ereport(LOG,
(errmsg("invalid statistics message length")));
exit(1); exit(1);
} }
} }
...@@ -1549,7 +1592,9 @@ pgstat_recvbuffer(void) ...@@ -1549,7 +1592,9 @@ pgstat_recvbuffer(void)
*/ */
if (FCNTL_NONBLOCK(writePipe) < 0) if (FCNTL_NONBLOCK(writePipe) < 0)
{ {
elog(LOG, "PGSTATBUFF: fcntl() failed: %m"); ereport(LOG,
(errcode_for_socket_access(),
errmsg("could not set statistics pipe to nonblock mode: %m")));
exit(1); exit(1);
} }
...@@ -1559,7 +1604,9 @@ pgstat_recvbuffer(void) ...@@ -1559,7 +1604,9 @@ pgstat_recvbuffer(void)
msgbuffer = (char *) malloc(PGSTAT_RECVBUFFERSZ); msgbuffer = (char *) malloc(PGSTAT_RECVBUFFERSZ);
if (msgbuffer == NULL) if (msgbuffer == NULL)
{ {
elog(LOG, "PGSTATBUFF: malloc() failed"); ereport(LOG,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory in statistics collector --- abort")));
exit(1); exit(1);
} }
...@@ -1586,7 +1633,8 @@ pgstat_recvbuffer(void) ...@@ -1586,7 +1633,8 @@ pgstat_recvbuffer(void)
{ {
if (!overflow) if (!overflow)
{ {
elog(LOG, "PGSTATBUFF: Warning - receive buffer full"); ereport(LOG,
(errmsg("statistics buffer is full")));
overflow = true; overflow = true;
} }
} }
...@@ -1617,7 +1665,9 @@ pgstat_recvbuffer(void) ...@@ -1617,7 +1665,9 @@ pgstat_recvbuffer(void)
{ {
if (errno == EINTR) if (errno == EINTR)
continue; continue;
elog(LOG, "PGSTATBUFF: select() failed: %m"); ereport(LOG,
(errcode_for_socket_access(),
errmsg("select failed in statistics buffer: %m")));
exit(1); exit(1);
} }
...@@ -1633,7 +1683,9 @@ pgstat_recvbuffer(void) ...@@ -1633,7 +1683,9 @@ pgstat_recvbuffer(void)
(struct sockaddr *) &fromaddr, &fromlen); (struct sockaddr *) &fromaddr, &fromlen);
if (len < 0) if (len < 0)
{ {
elog(LOG, "PGSTATBUFF: recvfrom() failed: %m"); ereport(LOG,
(errcode_for_socket_access(),
errmsg("failed to read statistics message: %m")));
exit(1); exit(1);
} }
...@@ -1706,7 +1758,9 @@ pgstat_recvbuffer(void) ...@@ -1706,7 +1758,9 @@ pgstat_recvbuffer(void)
{ {
if (errno == EINTR || errno == EAGAIN) if (errno == EINTR || errno == EAGAIN)
continue; /* not enough space in pipe */ continue; /* not enough space in pipe */
elog(LOG, "PGSTATBUFF: write() failed: %m"); ereport(LOG,
(errcode_for_socket_access(),
errmsg("failed to write statistics pipe: %m")));
exit(1); exit(1);
} }
/* NB: len < xfr is okay */ /* NB: len < xfr is okay */
...@@ -1759,7 +1813,8 @@ pgstat_add_backend(PgStat_MsgHdr *msg) ...@@ -1759,7 +1813,8 @@ pgstat_add_backend(PgStat_MsgHdr *msg)
*/ */
if (msg->m_backendid < 1 || msg->m_backendid > MaxBackends) if (msg->m_backendid < 1 || msg->m_backendid > MaxBackends)
{ {
elog(LOG, "PGSTAT: Invalid backend ID %d", msg->m_backendid); ereport(LOG,
(errmsg("invalid backend ID %d", msg->m_backendid)));
return -1; return -1;
} }
...@@ -1817,7 +1872,9 @@ pgstat_add_backend(PgStat_MsgHdr *msg) ...@@ -1817,7 +1872,9 @@ pgstat_add_backend(PgStat_MsgHdr *msg)
HASH_ENTER, &found); HASH_ENTER, &found);
if (dbentry == NULL) if (dbentry == NULL)
{ {
elog(LOG, "PGSTAT: DB hash table out of memory - abort"); ereport(LOG,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory in statistics collector --- abort")));
exit(1); exit(1);
} }
...@@ -1846,8 +1903,10 @@ pgstat_add_backend(PgStat_MsgHdr *msg) ...@@ -1846,8 +1903,10 @@ pgstat_add_backend(PgStat_MsgHdr *msg)
HASH_ELEM | HASH_FUNCTION); HASH_ELEM | HASH_FUNCTION);
if (dbentry->tables == NULL) if (dbentry->tables == NULL)
{ {
elog(LOG, "PGSTAT: failed to initialize hash table for " /* assume the problem is out-of-memory */
"new database entry"); ereport(LOG,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory in statistics collector --- abort")));
exit(1); exit(1);
} }
} }
...@@ -1895,10 +1954,13 @@ pgstat_sub_backend(int procpid) ...@@ -1895,10 +1954,13 @@ pgstat_sub_backend(int procpid)
*/ */
deadbe = (PgStat_StatBeDead *) hash_search(pgStatBeDead, deadbe = (PgStat_StatBeDead *) hash_search(pgStatBeDead,
(void *) &procpid, (void *) &procpid,
HASH_ENTER, &found); HASH_ENTER,
&found);
if (deadbe == NULL) if (deadbe == NULL)
{ {
elog(LOG, "PGSTAT: dead backend hash table out of memory"); ereport(LOG,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory in statistics collector --- abort")));
exit(1); exit(1);
} }
if (!found) if (!found)
...@@ -1945,8 +2007,10 @@ pgstat_write_statsfile(void) ...@@ -1945,8 +2007,10 @@ pgstat_write_statsfile(void)
fpout = fopen(pgStat_tmpfname, PG_BINARY_W); fpout = fopen(pgStat_tmpfname, PG_BINARY_W);
if (fpout == NULL) if (fpout == NULL)
{ {
elog(LOG, "PGSTAT: cannot open temp stats file %s: %m", ereport(LOG,
pgStat_tmpfname); (errcode_for_file_access(),
errmsg("cannot write temp statistics file \"%s\": %m",
pgStat_tmpfname)));
return; return;
} }
...@@ -1971,8 +2035,9 @@ pgstat_write_statsfile(void) ...@@ -1971,8 +2035,9 @@ pgstat_write_statsfile(void)
(void *) &(dbentry->databaseid), (void *) &(dbentry->databaseid),
HASH_REMOVE, NULL) == NULL) HASH_REMOVE, NULL) == NULL)
{ {
elog(LOG, "PGSTAT: database hash table corrupted " ereport(LOG,
"during cleanup - abort"); (errmsg("database hash table corrupted "
"during cleanup --- abort")));
exit(1); exit(1);
} }
} }
...@@ -2007,10 +2072,11 @@ pgstat_write_statsfile(void) ...@@ -2007,10 +2072,11 @@ pgstat_write_statsfile(void)
(void *) &(tabentry->tableid), (void *) &(tabentry->tableid),
HASH_REMOVE, NULL) == NULL) HASH_REMOVE, NULL) == NULL)
{ {
elog(LOG, "PGSTAT: tables hash table for " ereport(LOG,
"database %d corrupted during " (errmsg("tables hash table for "
"cleanup - abort", "database %u corrupted during "
dbentry->databaseid); "cleanup --- abort",
dbentry->databaseid)));
exit(1); exit(1);
} }
} }
...@@ -2049,20 +2115,24 @@ pgstat_write_statsfile(void) ...@@ -2049,20 +2115,24 @@ pgstat_write_statsfile(void)
/* /*
* No more output to be done. Close the temp file and replace the old * No more output to be done. Close the temp file and replace the old
* pgstat.stat with it's content. * pgstat.stat with it.
*/ */
fputc('E', fpout); fputc('E', fpout);
if (fclose(fpout) < 0) if (fclose(fpout) < 0)
{ {
elog(LOG, "PGSTAT: Error closing temp stats file %s: %m", ereport(LOG,
pgStat_tmpfname); (errcode_for_file_access(),
errmsg("could not write temp statistics file \"%s\": %m",
pgStat_tmpfname)));
} }
else else
{ {
if (rename(pgStat_tmpfname, pgStat_fname) < 0) if (rename(pgStat_tmpfname, pgStat_fname) < 0)
{ {
elog(LOG, "PGSTAT: Cannot rename temp stats file %s: %m", ereport(LOG,
pgStat_fname); (errcode_for_file_access(),
errmsg("could not rename temp statistics file \"%s\" to \"%s\": %m",
pgStat_tmpfname, pgStat_fname)));
} }
} }
...@@ -2082,8 +2152,9 @@ pgstat_write_statsfile(void) ...@@ -2082,8 +2152,9 @@ pgstat_write_statsfile(void)
(void *) &(deadbe->procpid), (void *) &(deadbe->procpid),
HASH_REMOVE, NULL) == NULL) HASH_REMOVE, NULL) == NULL)
{ {
elog(LOG, "PGSTAT: dead backend hash table corrupted " ereport(LOG,
"during cleanup - abort"); (errmsg("dead-backend hash table corrupted "
"during cleanup --- abort")));
exit(1); exit(1);
} }
} }
...@@ -2145,13 +2216,18 @@ pgstat_read_statsfile(HTAB **dbhash, Oid onlydb, ...@@ -2145,13 +2216,18 @@ pgstat_read_statsfile(HTAB **dbhash, Oid onlydb,
HASH_ELEM | HASH_FUNCTION | mcxt_flags); HASH_ELEM | HASH_FUNCTION | mcxt_flags);
if (*dbhash == NULL) if (*dbhash == NULL)
{ {
/* assume the problem is out-of-memory */
if (pgStatRunningInCollector) if (pgStatRunningInCollector)
{ {
elog(LOG, "PGSTAT: Creation of DB hash table failed"); ereport(LOG,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory in statistics collector --- abort")));
exit(1); exit(1);
} }
/* in backend, can do normal error */ /* in backend, can do normal error */
elog(ERROR, "PGSTAT: Creation of DB hash table failed"); ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory")));
} }
/* /*
...@@ -2187,18 +2263,10 @@ pgstat_read_statsfile(HTAB **dbhash, Oid onlydb, ...@@ -2187,18 +2263,10 @@ pgstat_read_statsfile(HTAB **dbhash, Oid onlydb,
case 'D': case 'D':
if (fread(&dbbuf, 1, sizeof(dbbuf), fpin) != sizeof(dbbuf)) if (fread(&dbbuf, 1, sizeof(dbbuf), fpin) != sizeof(dbbuf))
{ {
if (pgStatRunningInCollector) ereport(pgStatRunningInCollector ? LOG : WARNING,
{ (errmsg("corrupted pgstat.stat file")));
elog(LOG, "PGSTAT: corrupted pgstat.stat file"); fclose(fpin);
fclose(fpin); return;
return;
}
else
{
elog(WARNING, "PGSTAT: corrupted pgstat.stat file");
fclose(fpin);
return;
}
} }
/* /*
...@@ -2206,34 +2274,31 @@ pgstat_read_statsfile(HTAB **dbhash, Oid onlydb, ...@@ -2206,34 +2274,31 @@ pgstat_read_statsfile(HTAB **dbhash, Oid onlydb,
*/ */
dbentry = (PgStat_StatDBEntry *) hash_search(*dbhash, dbentry = (PgStat_StatDBEntry *) hash_search(*dbhash,
(void *) &dbbuf.databaseid, (void *) &dbbuf.databaseid,
HASH_ENTER, &found); HASH_ENTER,
&found);
if (dbentry == NULL) if (dbentry == NULL)
{ {
if (pgStatRunningInCollector) if (pgStatRunningInCollector)
{ {
elog(LOG, "PGSTAT: DB hash table out of memory"); ereport(LOG,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory in statistics collector --- abort")));
exit(1); exit(1);
} }
else else
{ {
fclose(fpin); fclose(fpin);
elog(ERROR, "PGSTAT: DB hash table out of memory"); ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory")));
} }
} }
if (found) if (found)
{ {
if (pgStatRunningInCollector) ereport(pgStatRunningInCollector ? LOG : WARNING,
{ (errmsg("corrupted pgstat.stat file")));
elog(LOG, "PGSTAT: corrupted pgstat.stat file"); fclose(fpin);
fclose(fpin); return;
return;
}
else
{
elog(WARNING, "PGSTAT: corrupted pgstat.stat file");
fclose(fpin);
return;
}
} }
memcpy(dbentry, &dbbuf, sizeof(PgStat_StatDBEntry)); memcpy(dbentry, &dbbuf, sizeof(PgStat_StatDBEntry));
...@@ -2247,7 +2312,6 @@ pgstat_read_statsfile(HTAB **dbhash, Oid onlydb, ...@@ -2247,7 +2312,6 @@ pgstat_read_statsfile(HTAB **dbhash, Oid onlydb,
if (onlydb != InvalidOid && onlydb != dbbuf.databaseid) if (onlydb != InvalidOid && onlydb != dbbuf.databaseid)
break; break;
memset(&hash_ctl, 0, sizeof(hash_ctl)); memset(&hash_ctl, 0, sizeof(hash_ctl));
hash_ctl.keysize = sizeof(Oid); hash_ctl.keysize = sizeof(Oid);
hash_ctl.entrysize = sizeof(PgStat_StatTabEntry); hash_ctl.entrysize = sizeof(PgStat_StatTabEntry);
...@@ -2259,18 +2323,19 @@ pgstat_read_statsfile(HTAB **dbhash, Oid onlydb, ...@@ -2259,18 +2323,19 @@ pgstat_read_statsfile(HTAB **dbhash, Oid onlydb,
HASH_ELEM | HASH_FUNCTION | mcxt_flags); HASH_ELEM | HASH_FUNCTION | mcxt_flags);
if (dbentry->tables == NULL) if (dbentry->tables == NULL)
{ {
/* assume the problem is out-of-memory */
if (pgStatRunningInCollector) if (pgStatRunningInCollector)
{ {
elog(LOG, "PGSTAT: failed to initialize " ereport(LOG,
"hash table for new database entry"); (errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory in statistics collector --- abort")));
exit(1); exit(1);
} }
else /* in backend, can do normal error */
{ fclose(fpin);
fclose(fpin); ereport(ERROR,
elog(ERROR, "PGSTAT: failed to initialize " (errcode(ERRCODE_OUT_OF_MEMORY),
"hash table for new database entry"); errmsg("out of memory")));
}
} }
/* /*
...@@ -2293,18 +2358,10 @@ pgstat_read_statsfile(HTAB **dbhash, Oid onlydb, ...@@ -2293,18 +2358,10 @@ pgstat_read_statsfile(HTAB **dbhash, Oid onlydb,
case 'T': case 'T':
if (fread(&tabbuf, 1, sizeof(tabbuf), fpin) != sizeof(tabbuf)) if (fread(&tabbuf, 1, sizeof(tabbuf), fpin) != sizeof(tabbuf))
{ {
if (pgStatRunningInCollector) ereport(pgStatRunningInCollector ? LOG : WARNING,
{ (errmsg("corrupted pgstat.stat file")));
elog(LOG, "PGSTAT: corrupted pgstat.stat file"); fclose(fpin);
fclose(fpin); return;
return;
}
else
{
elog(WARNING, "PGSTAT: corrupted pgstat.stat file");
fclose(fpin);
return;
}
} }
/* /*
...@@ -2320,30 +2377,24 @@ pgstat_read_statsfile(HTAB **dbhash, Oid onlydb, ...@@ -2320,30 +2377,24 @@ pgstat_read_statsfile(HTAB **dbhash, Oid onlydb,
{ {
if (pgStatRunningInCollector) if (pgStatRunningInCollector)
{ {
elog(LOG, "PGSTAT: Tab hash table out of memory"); ereport(LOG,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory in statistics collector --- abort")));
exit(1); exit(1);
} }
else /* in backend, can do normal error */
{ fclose(fpin);
fclose(fpin); ereport(ERROR,
elog(ERROR, "PGSTAT: Tab hash table out of memory"); (errcode(ERRCODE_OUT_OF_MEMORY),
} errmsg("out of memory")));
} }
if (found) if (found)
{ {
if (pgStatRunningInCollector) ereport(pgStatRunningInCollector ? LOG : WARNING,
{ (errmsg("corrupted pgstat.stat file")));
elog(LOG, "PGSTAT: corrupted pgstat.stat file"); fclose(fpin);
fclose(fpin); return;
return;
}
else
{
elog(WARNING, "PGSTAT: corrupted pgstat.stat file");
fclose(fpin);
return;
}
} }
memcpy(tabentry, &tabbuf, sizeof(tabbuf)); memcpy(tabentry, &tabbuf, sizeof(tabbuf));
...@@ -2361,18 +2412,10 @@ pgstat_read_statsfile(HTAB **dbhash, Oid onlydb, ...@@ -2361,18 +2412,10 @@ pgstat_read_statsfile(HTAB **dbhash, Oid onlydb,
if (fread(&maxbackends, 1, sizeof(maxbackends), fpin) != if (fread(&maxbackends, 1, sizeof(maxbackends), fpin) !=
sizeof(maxbackends)) sizeof(maxbackends))
{ {
if (pgStatRunningInCollector) ereport(pgStatRunningInCollector ? LOG : WARNING,
{ (errmsg("corrupted pgstat.stat file")));
elog(LOG, "PGSTAT: corrupted pgstat.stat file"); fclose(fpin);
fclose(fpin); return;
return;
}
else
{
elog(WARNING, "PGSTAT: corrupted pgstat.stat file");
fclose(fpin);
return;
}
} }
if (maxbackends == 0) if (maxbackends == 0)
{ {
...@@ -2415,18 +2458,10 @@ pgstat_read_statsfile(HTAB **dbhash, Oid onlydb, ...@@ -2415,18 +2458,10 @@ pgstat_read_statsfile(HTAB **dbhash, Oid onlydb,
sizeof(PgStat_StatBeEntry), fpin) != sizeof(PgStat_StatBeEntry), fpin) !=
sizeof(PgStat_StatBeEntry)) sizeof(PgStat_StatBeEntry))
{ {
if (pgStatRunningInCollector) ereport(pgStatRunningInCollector ? LOG : WARNING,
{ (errmsg("corrupted pgstat.stat file")));
elog(LOG, "PGSTAT: corrupted pgstat.stat file"); fclose(fpin);
fclose(fpin); return;
return;
}
else
{
elog(WARNING, "PGSTAT: corrupted pgstat.stat file");
fclose(fpin);
return;
}
} }
/* /*
...@@ -2456,18 +2491,10 @@ pgstat_read_statsfile(HTAB **dbhash, Oid onlydb, ...@@ -2456,18 +2491,10 @@ pgstat_read_statsfile(HTAB **dbhash, Oid onlydb,
return; return;
default: default:
if (pgStatRunningInCollector) ereport(pgStatRunningInCollector ? LOG : WARNING,
{ (errmsg("corrupted pgstat.stat file")));
elog(LOG, "PGSTAT: corrupted pgstat.stat file"); fclose(fpin);
fclose(fpin); return;
return;
}
else
{
elog(WARNING, "PGSTAT: corrupted pgstat.stat file");
fclose(fpin);
return;
}
} }
} }
...@@ -2579,8 +2606,9 @@ pgstat_recv_tabstat(PgStat_MsgTabstat *msg, int len) ...@@ -2579,8 +2606,9 @@ pgstat_recv_tabstat(PgStat_MsgTabstat *msg, int len)
HASH_ENTER, &found); HASH_ENTER, &found);
if (tabentry == NULL) if (tabentry == NULL)
{ {
elog(LOG, "PGSTAT: tables hash table out of memory for " ereport(LOG,
"database %d - abort", dbentry->databaseid); (errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory in statistics collector --- abort")));
exit(1); exit(1);
} }
...@@ -2759,8 +2787,10 @@ pgstat_recv_resetcounter(PgStat_MsgResetcounter *msg, int len) ...@@ -2759,8 +2787,10 @@ pgstat_recv_resetcounter(PgStat_MsgResetcounter *msg, int len)
HASH_ELEM | HASH_FUNCTION); HASH_ELEM | HASH_FUNCTION);
if (dbentry->tables == NULL) if (dbentry->tables == NULL)
{ {
elog(LOG, "PGSTAT: failed to reinitialize hash table for " /* assume the problem is out-of-memory */
"database entry"); ereport(LOG,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory in statistics collector --- abort")));
exit(1); exit(1);
} }
} }
...@@ -37,7 +37,7 @@ ...@@ -37,7 +37,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.333 2003/06/12 07:36:51 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.334 2003/07/22 19:00:10 tgl Exp $
* *
* NOTES * NOTES
* *
...@@ -327,10 +327,15 @@ checkDataDir(const char *checkdir) ...@@ -327,10 +327,15 @@ checkDataDir(const char *checkdir)
if (stat(checkdir, &stat_buf) == -1) if (stat(checkdir, &stat_buf) == -1)
{ {
if (errno == ENOENT) if (errno == ENOENT)
elog(FATAL, "data directory %s was not found", checkdir); ereport(FATAL,
(errcode_for_file_access(),
errmsg("data directory \"%s\" does not exist",
checkdir)));
else else
elog(FATAL, "could not read permissions of directory %s: %m", ereport(FATAL,
checkdir); (errcode_for_file_access(),
errmsg("could not read permissions of directory \"%s\": %m",
checkdir)));
} }
/* /*
...@@ -342,8 +347,11 @@ checkDataDir(const char *checkdir) ...@@ -342,8 +347,11 @@ checkDataDir(const char *checkdir)
*/ */
#if !defined(__CYGWIN__) && !defined(WIN32) #if !defined(__CYGWIN__) && !defined(WIN32)
if (stat_buf.st_mode & (S_IRWXG | S_IRWXO)) if (stat_buf.st_mode & (S_IRWXG | S_IRWXO))
elog(FATAL, "data directory %s has group or world access; permissions should be u=rwx (0700)", ereport(FATAL,
checkdir); (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("data directory \"%s\" has group or world access",
checkdir),
errdetail("permissions should be u=rwx (0700)")));
#endif #endif
/* Look for PG_VERSION before looking for pg_control */ /* Look for PG_VERSION before looking for pg_control */
...@@ -560,9 +568,15 @@ PostmasterMain(int argc, char *argv[]) ...@@ -560,9 +568,15 @@ PostmasterMain(int argc, char *argv[])
if (!value) if (!value)
{ {
if (opt == '-') if (opt == '-')
elog(ERROR, "--%s requires argument", optarg); ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("--%s requires a value",
optarg)));
else else
elog(ERROR, "-c %s requires argument", optarg); ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("-c %s requires a value",
optarg)));
} }
SetConfigOption(name, value, PGC_POSTMASTER, PGC_S_ARGV); SetConfigOption(name, value, PGC_POSTMASTER, PGC_S_ARGV);
...@@ -654,8 +668,9 @@ PostmasterMain(int argc, char *argv[]) ...@@ -654,8 +668,9 @@ PostmasterMain(int argc, char *argv[])
* On some systems our dynloader code needs the executable's pathname. * On some systems our dynloader code needs the executable's pathname.
*/ */
if (FindExec(pg_pathname, progname, "postgres") < 0) if (FindExec(pg_pathname, progname, "postgres") < 0)
elog(FATAL, "%s: could not locate executable, bailing out...", ereport(FATAL,
progname); (errmsg("%s: could not locate postgres executable",
progname)));
/* /*
* Initialize SSL library, if specified. * Initialize SSL library, if specified.
...@@ -830,7 +845,7 @@ PostmasterMain(int argc, char *argv[]) ...@@ -830,7 +845,7 @@ PostmasterMain(int argc, char *argv[])
/* /*
* Reset whereToSendOutput from Debug (its starting state) to None. * Reset whereToSendOutput from Debug (its starting state) to None.
* This prevents elog from sending log messages to stderr unless the * This prevents ereport from sending log messages to stderr unless the
* syslog/stderr switch permits. We don't do this until the * syslog/stderr switch permits. We don't do this until the
* postmaster is fully launched, since startup failures may as well be * postmaster is fully launched, since startup failures may as well be
* reported to stderr. * reported to stderr.
...@@ -1049,7 +1064,9 @@ ServerLoop(void) ...@@ -1049,7 +1064,9 @@ ServerLoop(void)
PG_SETMASK(&BlockSig); PG_SETMASK(&BlockSig);
if (errno == EINTR || errno == EWOULDBLOCK) if (errno == EINTR || errno == EWOULDBLOCK)
continue; continue;
elog(LOG, "ServerLoop: select failed: %m"); ereport(LOG,
(errcode_for_socket_access(),
errmsg("select failed in postmaster: %m")));
return STATUS_ERROR; return STATUS_ERROR;
} }
...@@ -1138,10 +1155,10 @@ initMasks(fd_set *rmask, fd_set *wmask) ...@@ -1138,10 +1155,10 @@ initMasks(fd_set *rmask, fd_set *wmask)
/* /*
* Read the startup packet and do something according to it. * Read the startup packet and do something according to it.
* *
* Returns STATUS_OK or STATUS_ERROR, or might call elog(FATAL) and * Returns STATUS_OK or STATUS_ERROR, or might call ereport(FATAL) and
* not return at all. * not return at all.
* *
* (Note that elog(FATAL) stuff is sent to the client, so only use it * (Note that ereport(FATAL) stuff is sent to the client, so only use it
* if that's what you want. Return STATUS_ERROR if you don't want to * if that's what you want. Return STATUS_ERROR if you don't want to
* send anything to the client, which would typically be appropriate * send anything to the client, which would typically be appropriate
* if we detect a communications failure.) * if we detect a communications failure.)
...@@ -1163,7 +1180,9 @@ ProcessStartupPacket(Port *port, bool SSLdone) ...@@ -1163,7 +1180,9 @@ ProcessStartupPacket(Port *port, bool SSLdone)
* so don't clutter the log with a complaint. * so don't clutter the log with a complaint.
*/ */
if (!SSLdone) if (!SSLdone)
elog(COMMERROR, "incomplete startup packet"); ereport(COMMERROR,
(errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("incomplete startup packet")));
return STATUS_ERROR; return STATUS_ERROR;
} }
...@@ -1173,7 +1192,9 @@ ProcessStartupPacket(Port *port, bool SSLdone) ...@@ -1173,7 +1192,9 @@ ProcessStartupPacket(Port *port, bool SSLdone)
if (len < (int32) sizeof(ProtocolVersion) || if (len < (int32) sizeof(ProtocolVersion) ||
len > MAX_STARTUP_PACKET_LENGTH) len > MAX_STARTUP_PACKET_LENGTH)
{ {
elog(COMMERROR, "invalid length of startup packet"); ereport(COMMERROR,
(errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("invalid length of startup packet")));
return STATUS_ERROR; return STATUS_ERROR;
} }
...@@ -1190,7 +1211,9 @@ ProcessStartupPacket(Port *port, bool SSLdone) ...@@ -1190,7 +1211,9 @@ ProcessStartupPacket(Port *port, bool SSLdone)
if (pq_getbytes(buf, len) == EOF) if (pq_getbytes(buf, len) == EOF)
{ {
elog(COMMERROR, "incomplete startup packet"); ereport(COMMERROR,
(errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("incomplete startup packet")));
return STATUS_ERROR; return STATUS_ERROR;
} }
...@@ -1221,7 +1244,9 @@ ProcessStartupPacket(Port *port, bool SSLdone) ...@@ -1221,7 +1244,9 @@ ProcessStartupPacket(Port *port, bool SSLdone)
#endif #endif
if (send(port->sock, &SSLok, 1, 0) != 1) if (send(port->sock, &SSLok, 1, 0) != 1)
{ {
elog(COMMERROR, "failed to send SSL negotiation response: %m"); ereport(COMMERROR,
(errcode_for_socket_access(),
errmsg("failed to send SSL negotiation response: %m")));
return STATUS_ERROR; /* close the connection */ return STATUS_ERROR; /* close the connection */
} }
...@@ -1237,7 +1262,7 @@ ProcessStartupPacket(Port *port, bool SSLdone) ...@@ -1237,7 +1262,7 @@ ProcessStartupPacket(Port *port, bool SSLdone)
/* Could add additional special packet types here */ /* Could add additional special packet types here */
/* /*
* Set FrontendProtocol now so that elog() knows what format to send * Set FrontendProtocol now so that ereport() knows what format to send
* if we fail during startup. * if we fail during startup.
*/ */
FrontendProtocol = proto; FrontendProtocol = proto;
...@@ -1248,11 +1273,13 @@ ProcessStartupPacket(Port *port, bool SSLdone) ...@@ -1248,11 +1273,13 @@ ProcessStartupPacket(Port *port, bool SSLdone)
PG_PROTOCOL_MAJOR(proto) > PG_PROTOCOL_MAJOR(PG_PROTOCOL_LATEST) || PG_PROTOCOL_MAJOR(proto) > PG_PROTOCOL_MAJOR(PG_PROTOCOL_LATEST) ||
(PG_PROTOCOL_MAJOR(proto) == PG_PROTOCOL_MAJOR(PG_PROTOCOL_LATEST) && (PG_PROTOCOL_MAJOR(proto) == PG_PROTOCOL_MAJOR(PG_PROTOCOL_LATEST) &&
PG_PROTOCOL_MINOR(proto) > PG_PROTOCOL_MINOR(PG_PROTOCOL_LATEST))) PG_PROTOCOL_MINOR(proto) > PG_PROTOCOL_MINOR(PG_PROTOCOL_LATEST)))
elog(FATAL, "unsupported frontend protocol %u.%u: server supports %u.0 to %u.%u", ereport(FATAL,
PG_PROTOCOL_MAJOR(proto), PG_PROTOCOL_MINOR(proto), (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
PG_PROTOCOL_MAJOR(PG_PROTOCOL_EARLIEST), errmsg("unsupported frontend protocol %u.%u: server supports %u.0 to %u.%u",
PG_PROTOCOL_MAJOR(PG_PROTOCOL_LATEST), PG_PROTOCOL_MAJOR(proto), PG_PROTOCOL_MINOR(proto),
PG_PROTOCOL_MINOR(PG_PROTOCOL_LATEST)); PG_PROTOCOL_MAJOR(PG_PROTOCOL_EARLIEST),
PG_PROTOCOL_MAJOR(PG_PROTOCOL_LATEST),
PG_PROTOCOL_MINOR(PG_PROTOCOL_LATEST))));
/* /*
* Now fetch parameters out of startup packet and save them into the * Now fetch parameters out of startup packet and save them into the
...@@ -1309,7 +1336,9 @@ ProcessStartupPacket(Port *port, bool SSLdone) ...@@ -1309,7 +1336,9 @@ ProcessStartupPacket(Port *port, bool SSLdone)
* given packet length, complain. * given packet length, complain.
*/ */
if (offset != len-1) if (offset != len-1)
elog(FATAL, "invalid startup packet layout: expected terminator as last byte"); ereport(FATAL,
(errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("invalid startup packet layout: expected terminator as last byte")));
} }
else else
{ {
...@@ -1335,7 +1364,9 @@ ProcessStartupPacket(Port *port, bool SSLdone) ...@@ -1335,7 +1364,9 @@ ProcessStartupPacket(Port *port, bool SSLdone)
/* Check a user name was given. */ /* Check a user name was given. */
if (port->user_name == NULL || port->user_name[0] == '\0') if (port->user_name == NULL || port->user_name[0] == '\0')
elog(FATAL, "no PostgreSQL user name specified in startup packet"); ereport(FATAL,
(errcode(ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION),
errmsg("no PostgreSQL user name specified in startup packet")));
/* The database defaults to the user name. */ /* The database defaults to the user name. */
if (port->database_name == NULL || port->database_name[0] == '\0') if (port->database_name == NULL || port->database_name[0] == '\0')
...@@ -1388,16 +1419,24 @@ ProcessStartupPacket(Port *port, bool SSLdone) ...@@ -1388,16 +1419,24 @@ ProcessStartupPacket(Port *port, bool SSLdone)
switch (cac) switch (cac)
{ {
case CAC_STARTUP: case CAC_STARTUP:
elog(FATAL, "The database system is starting up"); ereport(FATAL,
(errcode(ERRCODE_CANNOT_CONNECT_NOW),
errmsg("the database system is starting up")));
break; break;
case CAC_SHUTDOWN: case CAC_SHUTDOWN:
elog(FATAL, "The database system is shutting down"); ereport(FATAL,
(errcode(ERRCODE_CANNOT_CONNECT_NOW),
errmsg("the database system is shutting down")));
break; break;
case CAC_RECOVERY: case CAC_RECOVERY:
elog(FATAL, "The database system is in recovery mode"); ereport(FATAL,
(errcode(ERRCODE_CANNOT_CONNECT_NOW),
errmsg("the database system is in recovery mode")));
break; break;
case CAC_TOOMANY: case CAC_TOOMANY:
elog(FATAL, "Sorry, too many clients already"); ereport(FATAL,
(errcode(ERRCODE_TOO_MANY_CONNECTIONS),
errmsg("sorry, too many clients already")));
break; break;
case CAC_OK: case CAC_OK:
default: default:
...@@ -1427,7 +1466,8 @@ processCancelRequest(Port *port, void *pkt) ...@@ -1427,7 +1466,8 @@ processCancelRequest(Port *port, void *pkt)
if (backendPID == CheckPointPID) if (backendPID == CheckPointPID)
{ {
elog(DEBUG2, "processCancelRequest: CheckPointPID in cancel request for process %d", backendPID); elog(DEBUG2, "ignoring cancel request for checkpoint process %d",
backendPID);
return; return;
} }
else if (ExecBackend) else if (ExecBackend)
...@@ -1501,7 +1541,9 @@ ConnCreate(int serverFd) ...@@ -1501,7 +1541,9 @@ ConnCreate(int serverFd)
if (!(port = (Port *) calloc(1, sizeof(Port)))) if (!(port = (Port *) calloc(1, sizeof(Port))))
{ {
elog(LOG, "ConnCreate: malloc failed"); ereport(LOG,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory")));
SignalChildren(SIGQUIT); SignalChildren(SIGQUIT);
ExitPostmaster(1); ExitPostmaster(1);
} }
...@@ -1600,7 +1642,8 @@ SIGHUP_handler(SIGNAL_ARGS) ...@@ -1600,7 +1642,8 @@ SIGHUP_handler(SIGNAL_ARGS)
if (Shutdown <= SmartShutdown) if (Shutdown <= SmartShutdown)
{ {
elog(LOG, "Received SIGHUP, reloading configuration files"); ereport(LOG,
(errmsg("received SIGHUP, reloading configuration files")));
ProcessConfigFile(PGC_SIGHUP); ProcessConfigFile(PGC_SIGHUP);
#ifdef EXEC_BACKEND #ifdef EXEC_BACKEND
write_nondefault_variables(PGC_SIGHUP); write_nondefault_variables(PGC_SIGHUP);
...@@ -1627,7 +1670,7 @@ pmdie(SIGNAL_ARGS) ...@@ -1627,7 +1670,7 @@ pmdie(SIGNAL_ARGS)
PG_SETMASK(&BlockSig); PG_SETMASK(&BlockSig);
elog(DEBUG2, "pmdie %d", postgres_signal_arg); elog(DEBUG2, "postmaster received signal %d", postgres_signal_arg);
switch (postgres_signal_arg) switch (postgres_signal_arg)
{ {
...@@ -1641,7 +1684,8 @@ pmdie(SIGNAL_ARGS) ...@@ -1641,7 +1684,8 @@ pmdie(SIGNAL_ARGS)
if (Shutdown >= SmartShutdown) if (Shutdown >= SmartShutdown)
break; break;
Shutdown = SmartShutdown; Shutdown = SmartShutdown;
elog(LOG, "smart shutdown request"); ereport(LOG,
(errmsg("received smart shutdown request")));
if (DLGetHead(BackendList)) /* let reaper() handle this */ if (DLGetHead(BackendList)) /* let reaper() handle this */
break; break;
...@@ -1671,13 +1715,15 @@ pmdie(SIGNAL_ARGS) ...@@ -1671,13 +1715,15 @@ pmdie(SIGNAL_ARGS)
*/ */
if (Shutdown >= FastShutdown) if (Shutdown >= FastShutdown)
break; break;
elog(LOG, "fast shutdown request"); ereport(LOG,
(errmsg("received fast shutdown request")));
if (DLGetHead(BackendList)) /* let reaper() handle this */ if (DLGetHead(BackendList)) /* let reaper() handle this */
{ {
Shutdown = FastShutdown; Shutdown = FastShutdown;
if (!FatalError) if (!FatalError)
{ {
elog(LOG, "aborting any active transactions"); ereport(LOG,
(errmsg("aborting any active transactions")));
SignalChildren(SIGTERM); SignalChildren(SIGTERM);
} }
break; break;
...@@ -1713,7 +1759,8 @@ pmdie(SIGNAL_ARGS) ...@@ -1713,7 +1759,8 @@ pmdie(SIGNAL_ARGS)
* abort all children with SIGQUIT and exit without attempt to * abort all children with SIGQUIT and exit without attempt to
* properly shutdown data base system. * properly shutdown data base system.
*/ */
elog(LOG, "immediate shutdown request"); ereport(LOG,
(errmsg("received immediate shutdown request")));
if (ShutdownPID > 0) if (ShutdownPID > 0)
kill(ShutdownPID, SIGQUIT); kill(ShutdownPID, SIGQUIT);
if (StartupPID > 0) if (StartupPID > 0)
...@@ -1795,7 +1842,8 @@ reaper(SIGNAL_ARGS) ...@@ -1795,7 +1842,8 @@ reaper(SIGNAL_ARGS)
{ {
LogChildExit(LOG, gettext("startup process"), LogChildExit(LOG, gettext("startup process"),
pid, exitstatus); pid, exitstatus);
elog(LOG, "aborting startup due to startup process failure"); ereport(LOG,
(errmsg("aborting startup due to startup process failure")));
ExitPostmaster(1); ExitPostmaster(1);
} }
StartupPID = 0; StartupPID = 0;
...@@ -1849,7 +1897,8 @@ reaper(SIGNAL_ARGS) ...@@ -1849,7 +1897,8 @@ reaper(SIGNAL_ARGS)
*/ */
if (DLGetHead(BackendList) || StartupPID > 0 || ShutdownPID > 0) if (DLGetHead(BackendList) || StartupPID > 0 || ShutdownPID > 0)
goto reaper_done; goto reaper_done;
elog(LOG, "all server processes terminated; reinitializing shared memory and semaphores"); ereport(LOG,
(errmsg("all server processes terminated; reinitializing")));
shmem_exit(0); shmem_exit(0);
reset_shared(PostPortNumber); reset_shared(PostPortNumber);
...@@ -1936,7 +1985,8 @@ CleanupProc(int pid, ...@@ -1936,7 +1985,8 @@ CleanupProc(int pid,
(pid == CheckPointPID) ? gettext("checkpoint process") : (pid == CheckPointPID) ? gettext("checkpoint process") :
gettext("server process"), gettext("server process"),
pid, exitstatus); pid, exitstatus);
elog(LOG, "terminating any other active server processes"); ereport(LOG,
(errmsg("terminating any other active server processes")));
} }
curr = DLGetHead(BackendList); curr = DLGetHead(BackendList);
...@@ -1957,7 +2007,7 @@ CleanupProc(int pid, ...@@ -1957,7 +2007,7 @@ CleanupProc(int pid,
*/ */
if (!FatalError) if (!FatalError)
{ {
elog(DEBUG2, "CleanupProc: sending %s to process %d", elog(DEBUG2, "sending %s to process %d",
(SendStop ? "SIGSTOP" : "SIGQUIT"), (int) bp->pid); (SendStop ? "SIGSTOP" : "SIGQUIT"), (int) bp->pid);
kill(bp->pid, (SendStop ? SIGSTOP : SIGQUIT)); kill(bp->pid, (SendStop ? SIGSTOP : SIGQUIT));
} }
...@@ -1996,19 +2046,30 @@ CleanupProc(int pid, ...@@ -1996,19 +2046,30 @@ CleanupProc(int pid,
static void static void
LogChildExit(int lev, const char *procname, int pid, int exitstatus) LogChildExit(int lev, const char *procname, int pid, int exitstatus)
{ {
/*
* translator: the first %s in these messages is a noun phrase
* describing a child process, such as "server process"
*/
if (WIFEXITED(exitstatus)) if (WIFEXITED(exitstatus))
elog(lev, "%s (pid %d) exited with exit code %d", ereport(lev,
procname, pid, WEXITSTATUS(exitstatus)); /*
* translator: %s is a noun phrase describing a child process,
* such as "server process"
*/
(errmsg("%s (pid %d) exited with exit code %d",
procname, pid, WEXITSTATUS(exitstatus))));
else if (WIFSIGNALED(exitstatus)) else if (WIFSIGNALED(exitstatus))
elog(lev, "%s (pid %d) was terminated by signal %d", ereport(lev,
procname, pid, WTERMSIG(exitstatus)); /*
* translator: %s is a noun phrase describing a child process,
* such as "server process"
*/
(errmsg("%s (pid %d) was terminated by signal %d",
procname, pid, WTERMSIG(exitstatus))));
else else
elog(lev, "%s (pid %d) exited with unexpected status %d", ereport(lev,
procname, pid, exitstatus); /*
* translator: %s is a noun phrase describing a child process,
* such as "server process"
*/
(errmsg("%s (pid %d) exited with unexpected status %d",
procname, pid, exitstatus)));
} }
/* /*
...@@ -2029,7 +2090,7 @@ SignalChildren(int signal) ...@@ -2029,7 +2090,7 @@ SignalChildren(int signal)
if (bp->pid != MyProcPid) if (bp->pid != MyProcPid)
{ {
elog(DEBUG2, "SignalChildren: sending signal %d to process %d", elog(DEBUG2, "sending signal %d to process %d",
signal, (int) bp->pid); signal, (int) bp->pid);
kill(bp->pid, signal); kill(bp->pid, signal);
} }
...@@ -2067,7 +2128,9 @@ BackendStartup(Port *port) ...@@ -2067,7 +2128,9 @@ BackendStartup(Port *port)
bn = (Backend *) malloc(sizeof(Backend)); bn = (Backend *) malloc(sizeof(Backend));
if (!bn) if (!bn)
{ {
elog(LOG, "out of memory; connection startup aborted"); ereport(LOG,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory")));
return STATUS_ERROR; return STATUS_ERROR;
} }
...@@ -2118,7 +2181,8 @@ BackendStartup(Port *port) ...@@ -2118,7 +2181,8 @@ BackendStartup(Port *port)
status = BackendFork(port); status = BackendFork(port);
if (status != 0) if (status != 0)
elog(LOG, "connection startup failed"); ereport(LOG,
(errmsg("connection startup failed")));
proc_exit(status); proc_exit(status);
} }
...@@ -2132,15 +2196,16 @@ BackendStartup(Port *port) ...@@ -2132,15 +2196,16 @@ BackendStartup(Port *port)
beos_backend_startup_failed(); beos_backend_startup_failed();
#endif #endif
free(bn); free(bn);
elog(LOG, "connection startup failed (fork failure): %s", errno = save_errno;
strerror(save_errno)); ereport(LOG,
(errmsg("could not fork new process for connection: %m")));
report_fork_failure_to_client(port, save_errno); report_fork_failure_to_client(port, save_errno);
return STATUS_ERROR; return STATUS_ERROR;
} }
/* in parent, normal */ /* in parent, normal */
elog(DEBUG2, "BackendStartup: forked pid=%d socket=%d", (int) pid, elog(DEBUG2, "forked new backend, pid=%d socket=%d",
port->sock); (int) pid, port->sock);
/* /*
* Everything's been successful, it's safe to add this backend to our * Everything's been successful, it's safe to add this backend to our
...@@ -2166,9 +2231,9 @@ report_fork_failure_to_client(Port *port, int errnum) ...@@ -2166,9 +2231,9 @@ report_fork_failure_to_client(Port *port, int errnum)
{ {
char buffer[1000]; char buffer[1000];
/* Format the error message packet */ /* Format the error message packet (always V2 protocol) */
snprintf(buffer, sizeof(buffer), "E%s%s\n", snprintf(buffer, sizeof(buffer), "E%s%s\n",
gettext("Server process fork() failed: "), gettext("could not fork new process for connection: "),
strerror(errnum)); strerror(errnum));
/* Set port to non-blocking. Don't do send() if this fails */ /* Set port to non-blocking. Don't do send() if this fails */
...@@ -2262,12 +2327,12 @@ BackendFork(Port *port) ...@@ -2262,12 +2327,12 @@ BackendFork(Port *port)
MyProcPid = getpid(); MyProcPid = getpid();
/* /*
* Initialize libpq and enable reporting of elog errors to the client. * Initialize libpq and enable reporting of ereport errors to the client.
* Must do this now because authentication uses libpq to send * Must do this now because authentication uses libpq to send
* messages. * messages.
*/ */
pq_init(); /* initialize libpq to talk to client */ pq_init(); /* initialize libpq to talk to client */
whereToSendOutput = Remote; /* now safe to elog to client */ whereToSendOutput = Remote; /* now safe to ereport to client */
/* /*
* We arrange for a simple exit(0) if we receive SIGTERM or SIGQUIT * We arrange for a simple exit(0) if we receive SIGTERM or SIGQUIT
...@@ -2286,10 +2351,10 @@ BackendFork(Port *port) ...@@ -2286,10 +2351,10 @@ BackendFork(Port *port)
remote_host[0] = '\0'; remote_host[0] = '\0';
remote_port[0] = '\0'; remote_port[0] = '\0';
if (!getnameinfo((struct sockaddr *)&port->raddr.addr, if (!getnameinfo((struct sockaddr *)&port->raddr.addr,
port->raddr.salen, port->raddr.salen,
remote_host, sizeof(remote_host), remote_host, sizeof(remote_host),
remote_port, sizeof(remote_host), remote_port, sizeof(remote_host),
(log_hostname ? 0 : NI_NUMERICHOST) | NI_NUMERICSERV)) (log_hostname ? 0 : NI_NUMERICHOST) | NI_NUMERICSERV))
{ {
getnameinfo((struct sockaddr *)&port->raddr.addr, getnameinfo((struct sockaddr *)&port->raddr.addr,
port->raddr.salen, port->raddr.salen,
...@@ -2299,21 +2364,17 @@ BackendFork(Port *port) ...@@ -2299,21 +2364,17 @@ BackendFork(Port *port)
} }
if (Log_connections) if (Log_connections)
{ ereport(LOG,
elog(LOG, "connection received: host=%s port=%s", (errmsg("connection received: host=%s port=%s",
remote_host, remote_port); remote_host, remote_port)));
}
if (LogSourcePort) if (LogSourcePort)
{ {
/* modify remote_host for use in ps status */ /* modify remote_host for use in ps status */
int slen = strlen(remote_host) + 10; char tmphost[sizeof(remote_host) + 10];
char *str = palloc(slen);
snprintf(str, slen, "%s:%s", remote_host, remote_port); snprintf(tmphost, sizeof(tmphost), "%s:%s", remote_host, remote_port);
strncpy(remote_host, str, sizeof(remote_host)); StrNCpy(remote_host, tmphost, sizeof(remote_host));
remote_host[sizeof(remote_host) - 1] = '\0';
pfree(str);
} }
/* /*
...@@ -2332,7 +2393,7 @@ BackendFork(Port *port) ...@@ -2332,7 +2393,7 @@ BackendFork(Port *port)
* indefinitely. PreAuthDelay doesn't count against the time limit. * indefinitely. PreAuthDelay doesn't count against the time limit.
*/ */
if (!enable_sig_alarm(AuthenticationTimeout * 1000, false)) if (!enable_sig_alarm(AuthenticationTimeout * 1000, false))
elog(FATAL, "BackendFork: Unable to set timer for auth timeout"); elog(FATAL, "could not set timer for authorization timeout");
/* /*
* Receive the startup packet (which might turn out to be a cancel * Receive the startup packet (which might turn out to be a cancel
...@@ -2361,12 +2422,13 @@ BackendFork(Port *port) ...@@ -2361,12 +2422,13 @@ BackendFork(Port *port)
* SIGTERM/SIGQUIT again until backend startup is complete. * SIGTERM/SIGQUIT again until backend startup is complete.
*/ */
if (!disable_sig_alarm(false)) if (!disable_sig_alarm(false))
elog(FATAL, "BackendFork: Unable to disable timer for auth timeout"); elog(FATAL, "could not disable timer for authorization timeout");
PG_SETMASK(&BlockSig); PG_SETMASK(&BlockSig);
if (Log_connections) if (Log_connections)
elog(LOG, "connection authorized: user=%s database=%s", ereport(LOG,
port->user_name, port->database_name); (errmsg("connection authorized: user=%s database=%s",
port->user_name, port->database_name)));
/* /*
* Don't want backend to be able to see the postmaster random number * Don't want backend to be able to see the postmaster random number
...@@ -2519,9 +2581,10 @@ sigusr1_handler(SIGNAL_ARGS) ...@@ -2519,9 +2581,10 @@ sigusr1_handler(SIGNAL_ARGS)
int elapsed_secs = now - LastSignalledCheckpoint; int elapsed_secs = now - LastSignalledCheckpoint;
if (elapsed_secs < CheckPointWarning) if (elapsed_secs < CheckPointWarning)
elog(LOG, "Checkpoint segments are being created too frequently (%d secs)" ereport(LOG,
"\n\tConsider increasing CHECKPOINT_SEGMENTS", (errmsg("checkpoints are occurring too frequently (%d seconds apart)",
elapsed_secs); elapsed_secs),
errhint("Consider increasing CHECKPOINT_SEGMENTS.")));
} }
LastSignalledCheckpoint = now; LastSignalledCheckpoint = now;
} }
...@@ -2787,15 +2850,20 @@ SSDataBase(int xlop) ...@@ -2787,15 +2850,20 @@ SSDataBase(int xlop)
switch (xlop) switch (xlop)
{ {
case BS_XLOG_STARTUP: case BS_XLOG_STARTUP:
elog(LOG, "could not launch startup process (fork failure): %m"); ereport(LOG,
(errmsg("could not fork startup process: %m")));
break; break;
case BS_XLOG_CHECKPOINT: case BS_XLOG_CHECKPOINT:
elog(LOG, "could not launch checkpoint process (fork failure): %m"); ereport(LOG,
(errmsg("could not fork checkpoint process: %m")));
break; break;
case BS_XLOG_SHUTDOWN: case BS_XLOG_SHUTDOWN:
elog(LOG, "could not launch shutdown process (fork failure): %m"); ereport(LOG,
(errmsg("could not fork shutdown process: %m")));
break; break;
default: default:
ereport(LOG,
(errmsg("could not fork process: %m")));
break; break;
} }
...@@ -2817,7 +2885,9 @@ SSDataBase(int xlop) ...@@ -2817,7 +2885,9 @@ SSDataBase(int xlop)
{ {
if (!(bn = (Backend *) malloc(sizeof(Backend)))) if (!(bn = (Backend *) malloc(sizeof(Backend))))
{ {
elog(LOG, "CheckPointDataBase: malloc failed"); ereport(LOG,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory")));
ExitPostmaster(1); ExitPostmaster(1);
} }
...@@ -2857,7 +2927,7 @@ CreateOptsFile(int argc, char *argv[]) ...@@ -2857,7 +2927,7 @@ CreateOptsFile(int argc, char *argv[])
if ((fp = fopen(filename, "w")) == NULL) if ((fp = fopen(filename, "w")) == NULL)
{ {
postmaster_error("cannot create file %s: %s", postmaster_error("cannot create file \"%s\": %s",
filename, strerror(errno)); filename, strerror(errno));
return false; return false;
} }
...@@ -2880,7 +2950,7 @@ CreateOptsFile(int argc, char *argv[]) ...@@ -2880,7 +2950,7 @@ CreateOptsFile(int argc, char *argv[])
/* /*
* This should be used only for reporting "interactive" errors (ie, errors * This should be used only for reporting "interactive" errors (ie, errors
* during startup. Once the postmaster is launched, use elog. * during startup). Once the postmaster is launched, use ereport.
*/ */
void void
postmaster_error(const char *fmt,...) postmaster_error(const char *fmt,...)
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/tcop/dest.c,v 1.58 2003/05/08 18:16:36 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/tcop/dest.c,v 1.59 2003/07/22 19:00:11 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -104,7 +104,7 @@ CreateDestReceiver(CommandDest dest, Portal portal) ...@@ -104,7 +104,7 @@ CreateDestReceiver(CommandDest dest, Portal portal)
case Remote: case Remote:
case RemoteExecute: case RemoteExecute:
if (portal == NULL) if (portal == NULL)
elog(ERROR, "CreateDestReceiver: no portal specified"); elog(ERROR, "no portal specified for Remote receiver");
return printtup_create_DR(dest, portal); return printtup_create_DR(dest, portal);
case None: case None:
...@@ -118,10 +118,10 @@ CreateDestReceiver(CommandDest dest, Portal portal) ...@@ -118,10 +118,10 @@ CreateDestReceiver(CommandDest dest, Portal portal)
case Tuplestore: case Tuplestore:
if (portal == NULL) if (portal == NULL)
elog(ERROR, "CreateDestReceiver: no portal specified"); elog(ERROR, "no portal specified for Tuplestore receiver");
if (portal->holdStore == NULL || if (portal->holdStore == NULL ||
portal->holdContext == NULL) portal->holdContext == NULL)
elog(ERROR, "CreateDestReceiver: portal has no holdStore"); elog(ERROR, "portal has no holdStore");
return CreateTuplestoreDestReceiver(portal->holdStore, return CreateTuplestoreDestReceiver(portal->holdStore,
portal->holdContext); portal->holdContext);
} }
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/tcop/fastpath.c,v 1.64 2003/05/09 18:18:54 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/tcop/fastpath.c,v 1.65 2003/07/22 19:00:11 tgl Exp $
* *
* NOTES * NOTES
* This cruft is the server side of PQfn. * This cruft is the server side of PQfn.
...@@ -101,8 +101,10 @@ GetOldFunctionMessage(StringInfo buf) ...@@ -101,8 +101,10 @@ GetOldFunctionMessage(StringInfo buf)
if (argsize < -1) if (argsize < -1)
{ {
/* FATAL here since no hope of regaining message sync */ /* FATAL here since no hope of regaining message sync */
elog(FATAL, "HandleFunctionRequest: bogus argsize %d", ereport(FATAL,
argsize); (errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("invalid argument size %d in function call message",
argsize)));
} }
/* and arg contents */ /* and arg contents */
if (argsize > 0) if (argsize > 0)
...@@ -180,7 +182,9 @@ SendFunctionResult(Datum retval, bool isnull, Oid rettype, int16 format) ...@@ -180,7 +182,9 @@ SendFunctionResult(Datum retval, bool isnull, Oid rettype, int16 format)
pfree(outputbytes); pfree(outputbytes);
} }
else else
elog(ERROR, "Invalid format code %d", format); ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("unsupported format code: %d", format)));
} }
if (!newstyle) if (!newstyle)
...@@ -208,7 +212,7 @@ fetch_fp_info(Oid func_id, struct fp_info * fip) ...@@ -208,7 +212,7 @@ fetch_fp_info(Oid func_id, struct fp_info * fip)
* Since the validity of this structure is determined by whether the * Since the validity of this structure is determined by whether the
* funcid is OK, we clear the funcid here. It must not be set to the * funcid is OK, we clear the funcid here. It must not be set to the
* correct value until we are about to return with a good struct * correct value until we are about to return with a good struct
* fp_info, since we can be interrupted (i.e., with an elog(ERROR, * fp_info, since we can be interrupted (i.e., with an ereport(ERROR,
* ...)) at any time. [No longer really an issue since we don't save * ...)) at any time. [No longer really an issue since we don't save
* the struct fp_info across transactions anymore, but keep it * the struct fp_info across transactions anymore, but keep it
* anyway.] * anyway.]
...@@ -222,8 +226,9 @@ fetch_fp_info(Oid func_id, struct fp_info * fip) ...@@ -222,8 +226,9 @@ fetch_fp_info(Oid func_id, struct fp_info * fip)
ObjectIdGetDatum(func_id), ObjectIdGetDatum(func_id),
0, 0, 0); 0, 0, 0);
if (!HeapTupleIsValid(func_htp)) if (!HeapTupleIsValid(func_htp))
elog(ERROR, "fetch_fp_info: cache lookup for function %u failed", ereport(ERROR,
func_id); (errcode(ERRCODE_UNDEFINED_FUNCTION),
errmsg("function with OID %u does not exist", func_id)));
pp = (Form_pg_proc) GETSTRUCT(func_htp); pp = (Form_pg_proc) GETSTRUCT(func_htp);
fip->namespace = pp->pronamespace; fip->namespace = pp->pronamespace;
...@@ -254,8 +259,8 @@ fetch_fp_info(Oid func_id, struct fp_info * fip) ...@@ -254,8 +259,8 @@ fetch_fp_info(Oid func_id, struct fp_info * fip)
* RETURNS: * RETURNS:
* 0 if successful completion, EOF if frontend connection lost. * 0 if successful completion, EOF if frontend connection lost.
* *
* Note: All ordinary errors result in elog(ERROR,...). However, * Note: All ordinary errors result in ereport(ERROR,...). However,
* if we lose the frontend connection there is no one to elog to, * if we lose the frontend connection there is no one to ereport to,
* and no use in proceeding... * and no use in proceeding...
* *
* Note: palloc()s done here and in the called function do not need to be * Note: palloc()s done here and in the called function do not need to be
...@@ -282,19 +287,23 @@ HandleFunctionRequest(StringInfo msgBuf) ...@@ -282,19 +287,23 @@ HandleFunctionRequest(StringInfo msgBuf)
{ {
if (GetOldFunctionMessage(msgBuf)) if (GetOldFunctionMessage(msgBuf))
{ {
elog(COMMERROR, "unexpected EOF on client connection"); ereport(COMMERROR,
(errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("unexpected EOF on client connection")));
return EOF; return EOF;
} }
} }
/* /*
* Now that we've eaten the input message, check to see if we actually * Now that we've eaten the input message, check to see if we actually
* want to do the function call or not. It's now safe to elog(); we won't * want to do the function call or not. It's now safe to ereport();
* lose sync with the frontend. * we won't lose sync with the frontend.
*/ */
if (IsAbortedTransactionBlockState()) if (IsAbortedTransactionBlockState())
elog(ERROR, "current transaction is aborted, " ereport(ERROR,
"queries ignored until end of transaction block"); (errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION),
errmsg("current transaction is aborted, "
"queries ignored until end of transaction block")));
/* /*
* Begin parsing the buffer contents. * Begin parsing the buffer contents.
...@@ -404,14 +413,18 @@ parse_fcall_arguments(StringInfo msgBuf, struct fp_info *fip, ...@@ -404,14 +413,18 @@ parse_fcall_arguments(StringInfo msgBuf, struct fp_info *fip,
nargs = pq_getmsgint(msgBuf, 2); /* # of arguments */ nargs = pq_getmsgint(msgBuf, 2); /* # of arguments */
if (fip->flinfo.fn_nargs != nargs || nargs > FUNC_MAX_ARGS) if (fip->flinfo.fn_nargs != nargs || nargs > FUNC_MAX_ARGS)
elog(ERROR, "HandleFunctionRequest: actual arguments (%d) != registered arguments (%d)", ereport(ERROR,
nargs, fip->flinfo.fn_nargs); (errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("function call message contains %d arguments but function requires %d",
nargs, fip->flinfo.fn_nargs)));
fcinfo->nargs = nargs; fcinfo->nargs = nargs;
if (numAFormats > 1 && numAFormats != nargs) if (numAFormats > 1 && numAFormats != nargs)
elog(ERROR, "Function Call message has %d argument formats but %d arguments", ereport(ERROR,
numAFormats, nargs); (errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("function call message contains %d argument formats but %d arguments",
numAFormats, nargs)));
initStringInfo(&abuf); initStringInfo(&abuf);
...@@ -430,8 +443,10 @@ parse_fcall_arguments(StringInfo msgBuf, struct fp_info *fip, ...@@ -430,8 +443,10 @@ parse_fcall_arguments(StringInfo msgBuf, struct fp_info *fip,
continue; continue;
} }
if (argsize < 0) if (argsize < 0)
elog(ERROR, "HandleFunctionRequest: bogus argsize %d", ereport(ERROR,
argsize); (errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("invalid argument size %d in function call message",
argsize)));
/* Reset abuf to empty, and insert raw data into it */ /* Reset abuf to empty, and insert raw data into it */
abuf.len = 0; abuf.len = 0;
...@@ -489,11 +504,15 @@ parse_fcall_arguments(StringInfo msgBuf, struct fp_info *fip, ...@@ -489,11 +504,15 @@ parse_fcall_arguments(StringInfo msgBuf, struct fp_info *fip,
/* Trouble if it didn't eat the whole buffer */ /* Trouble if it didn't eat the whole buffer */
if (abuf.cursor != abuf.len) if (abuf.cursor != abuf.len)
elog(ERROR, "Improper binary format in function argument %d", ereport(ERROR,
i + 1); (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
errmsg("incorrect binary data format in function argument %d",
i + 1)));
} }
else else
elog(ERROR, "Invalid format code %d", aformat); ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("unsupported format code: %d", aformat)));
} }
/* Return result format code */ /* Return result format code */
...@@ -517,8 +536,10 @@ parse_fcall_arguments_20(StringInfo msgBuf, struct fp_info *fip, ...@@ -517,8 +536,10 @@ parse_fcall_arguments_20(StringInfo msgBuf, struct fp_info *fip,
nargs = pq_getmsgint(msgBuf, 4); /* # of arguments */ nargs = pq_getmsgint(msgBuf, 4); /* # of arguments */
if (fip->flinfo.fn_nargs != nargs || nargs > FUNC_MAX_ARGS) if (fip->flinfo.fn_nargs != nargs || nargs > FUNC_MAX_ARGS)
elog(ERROR, "HandleFunctionRequest: actual arguments (%d) != registered arguments (%d)", ereport(ERROR,
nargs, fip->flinfo.fn_nargs); (errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("function call message contains %d arguments but function requires %d",
nargs, fip->flinfo.fn_nargs)));
fcinfo->nargs = nargs; fcinfo->nargs = nargs;
...@@ -545,8 +566,10 @@ parse_fcall_arguments_20(StringInfo msgBuf, struct fp_info *fip, ...@@ -545,8 +566,10 @@ parse_fcall_arguments_20(StringInfo msgBuf, struct fp_info *fip,
continue; continue;
} }
if (argsize < 0) if (argsize < 0)
elog(ERROR, "HandleFunctionRequest: bogus argsize %d", ereport(ERROR,
argsize); (errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("invalid argument size %d in function call message",
argsize)));
/* Reset abuf to empty, and insert raw data into it */ /* Reset abuf to empty, and insert raw data into it */
abuf.len = 0; abuf.len = 0;
...@@ -566,8 +589,10 @@ parse_fcall_arguments_20(StringInfo msgBuf, struct fp_info *fip, ...@@ -566,8 +589,10 @@ parse_fcall_arguments_20(StringInfo msgBuf, struct fp_info *fip,
/* Trouble if it didn't eat the whole buffer */ /* Trouble if it didn't eat the whole buffer */
if (abuf.cursor != abuf.len) if (abuf.cursor != abuf.len)
elog(ERROR, "Improper binary format in function argument %d", ereport(ERROR,
i + 1); (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
errmsg("incorrect binary data format in function argument %d",
i + 1)));
} }
/* Desired result format is always binary in protocol 2.0 */ /* Desired result format is always binary in protocol 2.0 */
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.350 2003/07/09 06:47:34 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.351 2003/07/22 19:00:11 tgl Exp $
* *
* NOTES * NOTES
* this is the "main" module of the postgres backend and * this is the "main" module of the postgres backend and
...@@ -275,7 +275,9 @@ SocketBackend(StringInfo inBuf) ...@@ -275,7 +275,9 @@ SocketBackend(StringInfo inBuf)
if (qtype == EOF) /* frontend disconnected */ if (qtype == EOF) /* frontend disconnected */
{ {
elog(COMMERROR, "unexpected EOF on client connection"); ereport(COMMERROR,
(errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("unexpected EOF on client connection")));
return qtype; return qtype;
} }
...@@ -296,7 +298,9 @@ SocketBackend(StringInfo inBuf) ...@@ -296,7 +298,9 @@ SocketBackend(StringInfo inBuf)
/* old style without length word; convert */ /* old style without length word; convert */
if (pq_getstring(inBuf)) if (pq_getstring(inBuf))
{ {
elog(COMMERROR, "unexpected EOF on client connection"); ereport(COMMERROR,
(errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("unexpected EOF on client connection")));
return EOF; return EOF;
} }
} }
...@@ -321,7 +325,9 @@ SocketBackend(StringInfo inBuf) ...@@ -321,7 +325,9 @@ SocketBackend(StringInfo inBuf)
doing_extended_query_message = true; doing_extended_query_message = true;
/* these are only legal in protocol 3 */ /* these are only legal in protocol 3 */
if (PG_PROTOCOL_MAJOR(FrontendProtocol) < 3) if (PG_PROTOCOL_MAJOR(FrontendProtocol) < 3)
elog(FATAL, "Socket command type %c unknown", qtype); ereport(FATAL,
(errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("invalid frontend message type %d", qtype)));
break; break;
case 'S': /* sync */ case 'S': /* sync */
...@@ -331,7 +337,9 @@ SocketBackend(StringInfo inBuf) ...@@ -331,7 +337,9 @@ SocketBackend(StringInfo inBuf)
doing_extended_query_message = false; doing_extended_query_message = false;
/* only legal in protocol 3 */ /* only legal in protocol 3 */
if (PG_PROTOCOL_MAJOR(FrontendProtocol) < 3) if (PG_PROTOCOL_MAJOR(FrontendProtocol) < 3)
elog(FATAL, "Socket command type %c unknown", qtype); ereport(FATAL,
(errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("invalid frontend message type %d", qtype)));
break; break;
case 'd': /* copy data */ case 'd': /* copy data */
...@@ -340,7 +348,9 @@ SocketBackend(StringInfo inBuf) ...@@ -340,7 +348,9 @@ SocketBackend(StringInfo inBuf)
doing_extended_query_message = false; doing_extended_query_message = false;
/* these are only legal in protocol 3 */ /* these are only legal in protocol 3 */
if (PG_PROTOCOL_MAJOR(FrontendProtocol) < 3) if (PG_PROTOCOL_MAJOR(FrontendProtocol) < 3)
elog(FATAL, "Socket command type %c unknown", qtype); ereport(FATAL,
(errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("invalid frontend message type %d", qtype)));
break; break;
default: default:
...@@ -349,7 +359,9 @@ SocketBackend(StringInfo inBuf) ...@@ -349,7 +359,9 @@ SocketBackend(StringInfo inBuf)
* as fatal because we have probably lost message boundary sync, * as fatal because we have probably lost message boundary sync,
* and there's no good way to recover. * and there's no good way to recover.
*/ */
elog(FATAL, "Socket command type %c unknown", qtype); ereport(FATAL,
(errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("invalid frontend message type %d", qtype)));
break; break;
} }
...@@ -446,7 +458,8 @@ pg_parse_query(const char *query_string) ...@@ -446,7 +458,8 @@ pg_parse_query(const char *query_string)
List *raw_parsetree_list; List *raw_parsetree_list;
if (log_statement) if (log_statement)
elog(LOG, "query: %s", query_string); ereport(LOG,
(errmsg("query: %s", query_string)));
if (log_parser_stats) if (log_parser_stats)
ResetUsage(); ResetUsage();
...@@ -544,7 +557,8 @@ pg_rewrite_queries(List *querytree_list) ...@@ -544,7 +557,8 @@ pg_rewrite_queries(List *querytree_list)
new_list = (List *) copyObject(querytree_list); new_list = (List *) copyObject(querytree_list);
/* This checks both copyObject() and the equal() routines... */ /* This checks both copyObject() and the equal() routines... */
if (!equal(new_list, querytree_list)) if (!equal(new_list, querytree_list))
elog(WARNING, "pg_rewrite_queries: copyObject failed on parse tree"); ereport(WARNING,
(errmsg("copyObject failed to produce an equal parse tree")));
else else
querytree_list = new_list; querytree_list = new_list;
#endif #endif
...@@ -588,7 +602,8 @@ pg_plan_query(Query *querytree) ...@@ -588,7 +602,8 @@ pg_plan_query(Query *querytree)
#ifdef NOT_USED #ifdef NOT_USED
/* This checks both copyObject() and the equal() routines... */ /* This checks both copyObject() and the equal() routines... */
if (!equal(new_plan, plan)) if (!equal(new_plan, plan))
elog(WARNING, "pg_plan_query: copyObject failed on plan tree"); ereport(WARNING,
(errmsg("copyObject failed to produce an equal plan tree")));
else else
#endif #endif
plan = new_plan; plan = new_plan;
...@@ -774,8 +789,10 @@ exec_simple_query(const char *query_string) ...@@ -774,8 +789,10 @@ exec_simple_query(const char *query_string)
} }
if (!allowit) if (!allowit)
elog(ERROR, "current transaction is aborted, " ereport(ERROR,
"queries ignored until end of transaction block"); (errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION),
errmsg("current transaction is aborted, "
"queries ignored until end of transaction block")));
} }
/* Make sure we are in a transaction command */ /* Make sure we are in a transaction command */
...@@ -936,19 +953,21 @@ exec_simple_query(const char *query_string) ...@@ -936,19 +953,21 @@ exec_simple_query(const char *query_string)
* min duration. * min duration.
*/ */
if (usecs >= save_log_min_duration_statement * 1000) if (usecs >= save_log_min_duration_statement * 1000)
elog(LOG, "duration_statement: %ld.%06ld %s", ereport(LOG,
(long) (stop_t.tv_sec - start_t.tv_sec), (errmsg("duration_statement: %ld.%06ld %s",
(long) (stop_t.tv_usec - start_t.tv_usec), (long) (stop_t.tv_sec - start_t.tv_sec),
query_string); (long) (stop_t.tv_usec - start_t.tv_usec),
query_string)));
/* /*
* If the user is requesting logging of all durations, then log * If the user is requesting logging of all durations, then log
* that as well. * that as well.
*/ */
if (save_log_duration) if (save_log_duration)
elog(LOG, "duration: %ld.%06ld sec", ereport(LOG,
(long) (stop_t.tv_sec - start_t.tv_sec), (errmsg("duration: %ld.%06ld sec",
(long) (stop_t.tv_usec - start_t.tv_usec)); (long) (stop_t.tv_sec - start_t.tv_sec),
(long) (stop_t.tv_usec - start_t.tv_usec))));
} }
if (save_log_statement_stats) if (save_log_statement_stats)
...@@ -1050,7 +1069,9 @@ exec_parse_message(const char *query_string, /* string to execute */ ...@@ -1050,7 +1069,9 @@ exec_parse_message(const char *query_string, /* string to execute */
* to worry about multiple result tupdescs and things like that. * to worry about multiple result tupdescs and things like that.
*/ */
if (length(parsetree_list) > 1) if (length(parsetree_list) > 1)
elog(ERROR, "Cannot insert multiple commands into a prepared statement"); ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("cannot insert multiple commands into a prepared statement")));
if (parsetree_list != NIL) if (parsetree_list != NIL)
{ {
...@@ -1084,8 +1105,10 @@ exec_parse_message(const char *query_string, /* string to execute */ ...@@ -1084,8 +1105,10 @@ exec_parse_message(const char *query_string, /* string to execute */
} }
if (!allowit) if (!allowit)
elog(ERROR, "current transaction is aborted, " ereport(ERROR,
"queries ignored until end of transaction block"); (errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION),
errmsg("current transaction is aborted, "
"queries ignored until end of transaction block")));
} }
/* /*
...@@ -1110,8 +1133,10 @@ exec_parse_message(const char *query_string, /* string to execute */ ...@@ -1110,8 +1133,10 @@ exec_parse_message(const char *query_string, /* string to execute */
Oid ptype = paramTypes[i]; Oid ptype = paramTypes[i];
if (ptype == InvalidOid || ptype == UNKNOWNOID) if (ptype == InvalidOid || ptype == UNKNOWNOID)
elog(ERROR, "Could not determine datatype of parameter $%d", ereport(ERROR,
i + 1); (errcode(ERRCODE_INDETERMINATE_DATATYPE),
errmsg("could not determine datatype of parameter $%d",
i + 1)));
param_list = lappendo(param_list, ptype); param_list = lappendo(param_list, ptype);
} }
...@@ -1237,8 +1262,10 @@ exec_bind_message(StringInfo input_message) ...@@ -1237,8 +1262,10 @@ exec_bind_message(StringInfo input_message)
numParams = pq_getmsgint(input_message, 2); numParams = pq_getmsgint(input_message, 2);
if (numPFormats > 1 && numPFormats != numParams) if (numPFormats > 1 && numPFormats != numParams)
elog(ERROR, "BIND message has %d parameter formats but %d parameters", ereport(ERROR,
numPFormats, numParams); (errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("bind message has %d parameter formats but %d parameters",
numPFormats, numParams)));
/* Find prepared statement */ /* Find prepared statement */
if (stmt_name[0] != '\0') if (stmt_name[0] != '\0')
...@@ -1248,12 +1275,16 @@ exec_bind_message(StringInfo input_message) ...@@ -1248,12 +1275,16 @@ exec_bind_message(StringInfo input_message)
/* special-case the unnamed statement */ /* special-case the unnamed statement */
pstmt = unnamed_stmt_pstmt; pstmt = unnamed_stmt_pstmt;
if (!pstmt) if (!pstmt)
elog(ERROR, "Unnamed prepared statement does not exist"); ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_PSTATEMENT),
errmsg("unnamed prepared statement does not exist")));
} }
if (numParams != length(pstmt->argtype_list)) if (numParams != length(pstmt->argtype_list))
elog(ERROR, "Bind message supplies %d parameters, but prepared statement \"%s\" requires %d", ereport(ERROR,
numParams, stmt_name, length(pstmt->argtype_list)); (errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("bind message supplies %d parameters, but prepared statement \"%s\" requires %d",
numParams, stmt_name, length(pstmt->argtype_list))));
/* /*
* Create the portal. Allow silent replacement of an existing portal * Create the portal. Allow silent replacement of an existing portal
...@@ -1375,12 +1406,17 @@ exec_bind_message(StringInfo input_message) ...@@ -1375,12 +1406,17 @@ exec_bind_message(StringInfo input_message)
/* Trouble if it didn't eat the whole buffer */ /* Trouble if it didn't eat the whole buffer */
if (pbuf.cursor != pbuf.len) if (pbuf.cursor != pbuf.len)
elog(ERROR, "Improper binary format in BIND parameter %d", ereport(ERROR,
i + 1); (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
errmsg("incorrect binary data format in bind parameter %d",
i + 1)));
} }
else else
{ {
elog(ERROR, "Invalid format code %d", pformat); ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("unsupported format code: %d",
pformat)));
} }
/* Restore message buffer contents */ /* Restore message buffer contents */
...@@ -1453,7 +1489,9 @@ exec_execute_message(const char *portal_name, long max_rows) ...@@ -1453,7 +1489,9 @@ exec_execute_message(const char *portal_name, long max_rows)
portal = GetPortalByName(portal_name); portal = GetPortalByName(portal_name);
if (!PortalIsValid(portal)) if (!PortalIsValid(portal))
elog(ERROR, "Portal \"%s\" not found", portal_name); ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_CURSOR),
errmsg("portal \"%s\" does not exist", portal_name)));
/* /*
* If the original query was a null string, just return EmptyQueryResponse. * If the original query was a null string, just return EmptyQueryResponse.
...@@ -1517,8 +1555,10 @@ exec_execute_message(const char *portal_name, long max_rows) ...@@ -1517,8 +1555,10 @@ exec_execute_message(const char *portal_name, long max_rows)
if (IsAbortedTransactionBlockState()) if (IsAbortedTransactionBlockState())
{ {
if (!is_trans_exit) if (!is_trans_exit)
elog(ERROR, "current transaction is aborted, " ereport(ERROR,
"queries ignored until end of transaction block"); (errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION),
errmsg("current transaction is aborted, "
"queries ignored until end of transaction block")));
} }
/* Check for cancel signal before we start execution */ /* Check for cancel signal before we start execution */
...@@ -1591,7 +1631,9 @@ exec_describe_statement_message(const char *stmt_name) ...@@ -1591,7 +1631,9 @@ exec_describe_statement_message(const char *stmt_name)
/* special-case the unnamed statement */ /* special-case the unnamed statement */
pstmt = unnamed_stmt_pstmt; pstmt = unnamed_stmt_pstmt;
if (!pstmt) if (!pstmt)
elog(ERROR, "Unnamed prepared statement does not exist"); ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_PSTATEMENT),
errmsg("unnamed prepared statement does not exist")));
} }
if (whereToSendOutput != Remote) if (whereToSendOutput != Remote)
...@@ -1642,7 +1684,9 @@ exec_describe_portal_message(const char *portal_name) ...@@ -1642,7 +1684,9 @@ exec_describe_portal_message(const char *portal_name)
portal = GetPortalByName(portal_name); portal = GetPortalByName(portal_name);
if (!PortalIsValid(portal)) if (!PortalIsValid(portal))
elog(ERROR, "Portal \"%s\" not found", portal_name); ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_CURSOR),
errmsg("portal \"%s\" does not exist", portal_name)));
if (whereToSendOutput != Remote) if (whereToSendOutput != Remote)
return; /* can't actually do anything... */ return; /* can't actually do anything... */
...@@ -1724,13 +1768,19 @@ void ...@@ -1724,13 +1768,19 @@ void
quickdie(SIGNAL_ARGS) quickdie(SIGNAL_ARGS)
{ {
PG_SETMASK(&BlockSig); PG_SETMASK(&BlockSig);
elog(WARNING, "Message from PostgreSQL backend:" /*
"\n\tThe Postmaster has informed me that some other backend" * Ideally this should be ereport(FATAL), but then we'd not get control
"\n\tdied abnormally and possibly corrupted shared memory." * back (perhaps could fix by doing local sigsetjmp?)
"\n\tI have rolled back the current transaction and am" */
"\n\tgoing to terminate your database system connection and exit." ereport(WARNING,
"\n\tPlease reconnect to the database system and repeat your query."); (errcode(ERRCODE_CRASH_SHUTDOWN),
errmsg("terminating connection due to crash of another backend"),
errdetail("The postmaster has commanded this backend to roll back"
" the current transaction and exit, because another"
" backend exited abnormally and possibly corrupted"
" shared memory."),
errhint("In a moment you should be able to reconnect to the"
" database and repeat your query.")));
/* /*
* DO NOT proc_exit() -- we're here because shared memory may be * DO NOT proc_exit() -- we're here because shared memory may be
* corrupted, so we don't want to try to clean up our transaction. * corrupted, so we don't want to try to clean up our transaction.
...@@ -1741,7 +1791,6 @@ quickdie(SIGNAL_ARGS) ...@@ -1741,7 +1791,6 @@ quickdie(SIGNAL_ARGS)
* random backend. This is necessary precisely because we don't clean * random backend. This is necessary precisely because we don't clean
* up our shared memory state. * up our shared memory state.
*/ */
exit(1); exit(1);
} }
...@@ -1842,9 +1891,12 @@ StatementCancelHandler(SIGNAL_ARGS) ...@@ -1842,9 +1891,12 @@ StatementCancelHandler(SIGNAL_ARGS)
static void static void
FloatExceptionHandler(SIGNAL_ARGS) FloatExceptionHandler(SIGNAL_ARGS)
{ {
elog(ERROR, "floating point exception!" ereport(ERROR,
" The last floating point operation either exceeded legal ranges" (errcode(ERRCODE_FLOATING_POINT_EXCEPTION),
" or was a divide by zero"); errmsg("floating-point exception"),
errdetail("An invalid floating-point operation was signaled. "
"This probably means an out-of-range result or an "
"invalid operation, such as division by zero.")));
} }
/* SIGHUP: set flag to re-read config file at next convenient time */ /* SIGHUP: set flag to re-read config file at next convenient time */
...@@ -1875,14 +1927,18 @@ ProcessInterrupts(void) ...@@ -1875,14 +1927,18 @@ ProcessInterrupts(void)
QueryCancelPending = false; /* ProcDie trumps QueryCancel */ QueryCancelPending = false; /* ProcDie trumps QueryCancel */
ImmediateInterruptOK = false; /* not idle anymore */ ImmediateInterruptOK = false; /* not idle anymore */
DisableNotifyInterrupt(); DisableNotifyInterrupt();
elog(FATAL, "This connection has been terminated by the administrator."); ereport(FATAL,
(errcode(ERRCODE_ADMIN_SHUTDOWN),
errmsg("terminating connection due to administrator command")));
} }
if (QueryCancelPending) if (QueryCancelPending)
{ {
QueryCancelPending = false; QueryCancelPending = false;
ImmediateInterruptOK = false; /* not idle anymore */ ImmediateInterruptOK = false; /* not idle anymore */
DisableNotifyInterrupt(); DisableNotifyInterrupt();
elog(ERROR, "Query was canceled."); ereport(ERROR,
(errcode(ERRCODE_QUERY_CANCELED),
errmsg("canceling query due to user request")));
} }
/* If we get here, do nothing (probably, QueryCancelPending was reset) */ /* If we get here, do nothing (probably, QueryCancelPending was reset) */
} }
...@@ -2028,7 +2084,9 @@ PostgresMain(int argc, char *argv[], const char *username) ...@@ -2028,7 +2084,9 @@ PostgresMain(int argc, char *argv[], const char *username)
#ifdef USE_ASSERT_CHECKING #ifdef USE_ASSERT_CHECKING
SetConfigOption("debug_assertions", optarg, ctx, gucsource); SetConfigOption("debug_assertions", optarg, ctx, gucsource);
#else #else
elog(WARNING, "Assert checking is not compiled in"); ereport(WARNING,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("assert checking is not compiled in")));
#endif #endif
break; break;
...@@ -2313,9 +2371,15 @@ PostgresMain(int argc, char *argv[], const char *username) ...@@ -2313,9 +2371,15 @@ PostgresMain(int argc, char *argv[], const char *username)
if (!value) if (!value)
{ {
if (flag == '-') if (flag == '-')
elog(ERROR, "--%s requires argument", optarg); ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("--%s requires a value",
optarg)));
else else
elog(ERROR, "-c %s requires argument", optarg); ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("-c %s requires a value",
optarg)));
} }
SetConfigOption(name, value, ctx, gucsource); SetConfigOption(name, value, ctx, gucsource);
...@@ -2372,7 +2436,9 @@ PostgresMain(int argc, char *argv[], const char *username) ...@@ -2372,7 +2436,9 @@ PostgresMain(int argc, char *argv[], const char *username)
if (log_statement_stats && if (log_statement_stats &&
(log_parser_stats || log_planner_stats || log_executor_stats)) (log_parser_stats || log_planner_stats || log_executor_stats))
{ {
elog(WARNING, "Query statistics are disabled because parser, planner, or executor statistics are on."); ereport(WARNING,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("query-level statistics are disabled because parser, planner, or executor statistics are on")));
SetConfigOption("show_statement_stats", "false", ctx, gucsource); SetConfigOption("show_statement_stats", "false", ctx, gucsource);
} }
...@@ -2452,10 +2518,10 @@ PostgresMain(int argc, char *argv[], const char *username) ...@@ -2452,10 +2518,10 @@ PostgresMain(int argc, char *argv[], const char *username)
/* noninteractive case: nothing should be left after switches */ /* noninteractive case: nothing should be left after switches */
if (errs || argc != optind || dbname == NULL) if (errs || argc != optind || dbname == NULL)
{ {
elog(WARNING, "%s: invalid command line arguments\nTry -? for help.", ereport(FATAL,
argv[0]); (errcode(ERRCODE_SYNTAX_ERROR),
proc_exit(0); /* not 1, that causes system-wide errmsg("invalid backend command-line arguments"),
* restart... */ errhint("Try -? for help.")));
} }
BaseInit(); BaseInit();
#ifdef EXECBACKEND #ifdef EXECBACKEND
...@@ -2467,17 +2533,20 @@ PostgresMain(int argc, char *argv[], const char *username) ...@@ -2467,17 +2533,20 @@ PostgresMain(int argc, char *argv[], const char *username)
/* interactive case: database name can be last arg on command line */ /* interactive case: database name can be last arg on command line */
if (errs || argc - optind > 1) if (errs || argc - optind > 1)
{ {
elog(WARNING, "%s: invalid command line arguments\nTry -? for help.", ereport(FATAL,
argv[0]); (errcode(ERRCODE_SYNTAX_ERROR),
proc_exit(1); errmsg("%s: invalid command-line arguments",
argv[0]),
errhint("Try -? for help.")));
} }
else if (argc - optind == 1) else if (argc - optind == 1)
dbname = argv[optind]; dbname = argv[optind];
else if ((dbname = username) == NULL) else if ((dbname = username) == NULL)
{ {
elog(WARNING, "%s: user name undefined and no database specified", ereport(FATAL,
argv[0]); (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
proc_exit(1); errmsg("%s: no database nor user name specified",
argv[0])));
} }
/* /*
...@@ -2485,8 +2554,9 @@ PostgresMain(int argc, char *argv[], const char *username) ...@@ -2485,8 +2554,9 @@ PostgresMain(int argc, char *argv[], const char *username)
* pathname. (If under postmaster, this was done already.) * pathname. (If under postmaster, this was done already.)
*/ */
if (FindExec(pg_pathname, argv[0], "postgres") < 0) if (FindExec(pg_pathname, argv[0], "postgres") < 0)
elog(FATAL, "%s: could not locate executable, bailing out...", ereport(FATAL,
argv[0]); (errmsg("%s: could not locate postgres executable",
argv[0])));
/* /*
* Validate we have been given a reasonable-looking DataDir (if * Validate we have been given a reasonable-looking DataDir (if
...@@ -2556,7 +2626,7 @@ PostgresMain(int argc, char *argv[], const char *username) ...@@ -2556,7 +2626,7 @@ PostgresMain(int argc, char *argv[], const char *username)
if (!IsUnderPostmaster) if (!IsUnderPostmaster)
{ {
puts("\nPOSTGRES backend interactive interface "); puts("\nPOSTGRES backend interactive interface ");
puts("$Revision: 1.350 $ $Date: 2003/07/09 06:47:34 $\n"); puts("$Revision: 1.351 $ $Date: 2003/07/22 19:00:11 $\n");
} }
/* /*
...@@ -2594,7 +2664,7 @@ PostgresMain(int argc, char *argv[], const char *username) ...@@ -2594,7 +2664,7 @@ PostgresMain(int argc, char *argv[], const char *username)
* *
* Make sure we're not interrupted while cleaning up. Also forget * Make sure we're not interrupted while cleaning up. Also forget
* any pending QueryCancel request, since we're aborting anyway. * any pending QueryCancel request, since we're aborting anyway.
* Force InterruptHoldoffCount to a known state in case we elog'd * Force InterruptHoldoffCount to a known state in case we ereport'd
* from inside a holdoff section. * from inside a holdoff section.
*/ */
ImmediateInterruptOK = false; ImmediateInterruptOK = false;
...@@ -2648,7 +2718,7 @@ PostgresMain(int argc, char *argv[], const char *username) ...@@ -2648,7 +2718,7 @@ PostgresMain(int argc, char *argv[], const char *username)
RESUME_INTERRUPTS(); RESUME_INTERRUPTS();
} }
Warn_restart_ready = true; /* we can now handle elog(ERROR) */ Warn_restart_ready = true; /* we can now handle ereport(ERROR) */
PG_SETMASK(&UnBlockSig); PG_SETMASK(&UnBlockSig);
...@@ -2826,7 +2896,7 @@ PostgresMain(int argc, char *argv[], const char *username) ...@@ -2826,7 +2896,7 @@ PostgresMain(int argc, char *argv[], const char *username)
/* lost frontend connection during F message input */ /* lost frontend connection during F message input */
/* /*
* Reset whereToSendOutput to prevent elog from * Reset whereToSendOutput to prevent ereport from
* attempting to send any more messages to client. * attempting to send any more messages to client.
*/ */
if (whereToSendOutput == Remote) if (whereToSendOutput == Remote)
...@@ -2877,8 +2947,10 @@ PostgresMain(int argc, char *argv[], const char *username) ...@@ -2877,8 +2947,10 @@ PostgresMain(int argc, char *argv[], const char *username)
} }
break; break;
default: default:
elog(ERROR, "Invalid Close message subtype %d", ereport(ERROR,
close_type); (errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("invalid CLOSE message subtype %d",
close_type)));
break; break;
} }
...@@ -2905,8 +2977,10 @@ PostgresMain(int argc, char *argv[], const char *username) ...@@ -2905,8 +2977,10 @@ PostgresMain(int argc, char *argv[], const char *username)
exec_describe_portal_message(describe_target); exec_describe_portal_message(describe_target);
break; break;
default: default:
elog(ERROR, "Invalid Describe message subtype %d", ereport(ERROR,
describe_type); (errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("invalid DESCRIBE message subtype %d",
describe_type)));
break; break;
} }
} }
...@@ -2933,7 +3007,7 @@ PostgresMain(int argc, char *argv[], const char *username) ...@@ -2933,7 +3007,7 @@ PostgresMain(int argc, char *argv[], const char *username)
case EOF: case EOF:
/* /*
* Reset whereToSendOutput to prevent elog from attempting * Reset whereToSendOutput to prevent ereport from attempting
* to send any more messages to client. * to send any more messages to client.
*/ */
if (whereToSendOutput == Remote) if (whereToSendOutput == Remote)
...@@ -2959,7 +3033,10 @@ PostgresMain(int argc, char *argv[], const char *username) ...@@ -2959,7 +3033,10 @@ PostgresMain(int argc, char *argv[], const char *username)
break; break;
default: default:
elog(FATAL, "Socket command type %c unknown", firstchar); ereport(FATAL,
(errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("invalid frontend message type %d",
firstchar)));
} }
#ifdef MEMORY_CONTEXT_CHECKING #ifdef MEMORY_CONTEXT_CHECKING
...@@ -3088,7 +3165,9 @@ ShowUsage(const char *title) ...@@ -3088,7 +3165,9 @@ ShowUsage(const char *title)
if (str.data[str.len - 1] == '\n') if (str.data[str.len - 1] == '\n')
str.data[--str.len] = '\0'; str.data[--str.len] = '\0';
elog(LOG, "%s\n%s", title, str.data); ereport(LOG,
(errmsg_internal("%s", title),
errdetail("%s", str.data)));
pfree(str.data); pfree(str.data);
} }
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/tcop/pquery.c,v 1.66 2003/05/28 16:03:58 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/tcop/pquery.c,v 1.67 2003/07/22 19:00:12 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -341,8 +341,10 @@ PortalSetResultFormat(Portal portal, int nFormats, int16 *formats) ...@@ -341,8 +341,10 @@ PortalSetResultFormat(Portal portal, int nFormats, int16 *formats)
{ {
/* format specified for each column */ /* format specified for each column */
if (nFormats != natts) if (nFormats != natts)
elog(ERROR, "BIND message has %d result formats but query has %d columns", ereport(ERROR,
nFormats, natts); (errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("bind message has %d result formats but query has %d columns",
nFormats, natts)));
memcpy(portal->formats, formats, natts * sizeof(int16)); memcpy(portal->formats, formats, natts * sizeof(int16));
} else if (nFormats > 0) } else if (nFormats > 0)
{ {
...@@ -401,9 +403,13 @@ PortalRun(Portal portal, long count, ...@@ -401,9 +403,13 @@ PortalRun(Portal portal, long count,
* Check for improper portal use, and mark portal active. * Check for improper portal use, and mark portal active.
*/ */
if (portal->portalDone) if (portal->portalDone)
elog(ERROR, "Portal \"%s\" cannot be run anymore", portal->name); ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("portal \"%s\" cannot be run anymore", portal->name)));
if (portal->portalActive) if (portal->portalActive)
elog(ERROR, "Portal \"%s\" already active", portal->name); ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("portal \"%s\" already active", portal->name)));
portal->portalActive = true; portal->portalActive = true;
/* /*
...@@ -468,7 +474,8 @@ PortalRun(Portal portal, long count, ...@@ -468,7 +474,8 @@ PortalRun(Portal portal, long count,
break; break;
default: default:
elog(ERROR, "PortalRun: bogus portal strategy"); elog(ERROR, "unrecognized portal strategy: %d",
(int) portal->strategy);
result = false; /* keep compiler quiet */ result = false; /* keep compiler quiet */
break; break;
} }
...@@ -577,8 +584,10 @@ PortalRunSelect(Portal portal, ...@@ -577,8 +584,10 @@ PortalRunSelect(Portal portal,
else else
{ {
if (portal->cursorOptions & CURSOR_OPT_NO_SCROLL) if (portal->cursorOptions & CURSOR_OPT_NO_SCROLL)
elog(ERROR, "Cursor can only scan forward" ereport(ERROR,
"\n\tDeclare it with SCROLL option to enable backward scan"); (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("cursor can only scan forward"),
errhint("Declare it with SCROLL option to enable backward scan.")));
if (portal->atStart || count <= 0) if (portal->atStart || count <= 0)
direction = NoMovementScanDirection; direction = NoMovementScanDirection;
...@@ -900,9 +909,13 @@ PortalRunFetch(Portal portal, ...@@ -900,9 +909,13 @@ PortalRunFetch(Portal portal,
* Check for improper portal use, and mark portal active. * Check for improper portal use, and mark portal active.
*/ */
if (portal->portalDone) if (portal->portalDone)
elog(ERROR, "Portal \"%s\" cannot be run anymore", portal->name); ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("portal \"%s\" cannot be run anymore", portal->name)));
if (portal->portalActive) if (portal->portalActive)
elog(ERROR, "Portal \"%s\" already active", portal->name); ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("portal \"%s\" already active", portal->name)));
portal->portalActive = true; portal->portalActive = true;
/* /*
...@@ -922,7 +935,7 @@ PortalRunFetch(Portal portal, ...@@ -922,7 +935,7 @@ PortalRunFetch(Portal portal,
break; break;
default: default:
elog(ERROR, "PortalRunFetch: unsupported portal strategy"); elog(ERROR, "unsupported portal strategy");
result = 0; /* keep compiler quiet */ result = 0; /* keep compiler quiet */
break; break;
} }
...@@ -1053,7 +1066,7 @@ DoPortalRunFetch(Portal portal, ...@@ -1053,7 +1066,7 @@ DoPortalRunFetch(Portal portal,
} }
break; break;
default: default:
elog(ERROR, "PortalRunFetch: bogus direction"); elog(ERROR, "bogus direction");
break; break;
} }
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/tcop/utility.c,v 1.201 2003/06/27 14:45:30 petere Exp $ * $Header: /cvsroot/pgsql/src/backend/tcop/utility.c,v 1.202 2003/07/22 19:00:12 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -60,69 +60,90 @@ ...@@ -60,69 +60,90 @@
* Error-checking support for DROP commands * Error-checking support for DROP commands
*/ */
struct kindstrings struct msgstrings
{ {
char kind; char kind;
char *indef_article; int nonexistent_code;
char *name; const char *nonexistent_msg;
char *command; const char *nota_msg;
const char *drophint_msg;
}; };
static struct kindstrings kindstringarray[] = { static const struct msgstrings msgstringarray[] = {
{RELKIND_RELATION, "a", "table", "TABLE"}, { RELKIND_RELATION,
{RELKIND_SEQUENCE, "a", "sequence", "SEQUENCE"}, ERRCODE_UNDEFINED_TABLE,
{RELKIND_VIEW, "a", "view", "VIEW"}, gettext_noop("table \"%s\" does not exist"),
{RELKIND_INDEX, "an", "index", "INDEX"}, gettext_noop("\"%s\" is not a table"),
{RELKIND_COMPOSITE_TYPE, "a", "type", "TYPE"}, gettext_noop("Use DROP TABLE to remove a table.") },
{'\0', "a", "???", "???"} { RELKIND_SEQUENCE,
ERRCODE_UNDEFINED_TABLE,
gettext_noop("sequence \"%s\" does not exist"),
gettext_noop("\"%s\" is not a sequence"),
gettext_noop("Use DROP SEQUENCE to remove a sequence.") },
{ RELKIND_VIEW,
ERRCODE_UNDEFINED_TABLE,
gettext_noop("view \"%s\" does not exist"),
gettext_noop("\"%s\" is not a view"),
gettext_noop("Use DROP VIEW to remove a view.") },
{ RELKIND_INDEX,
ERRCODE_UNDEFINED_OBJECT,
gettext_noop("index \"%s\" does not exist"),
gettext_noop("\"%s\" is not an index"),
gettext_noop("Use DROP INDEX to remove an index.") },
{ RELKIND_COMPOSITE_TYPE,
ERRCODE_UNDEFINED_OBJECT,
gettext_noop("type \"%s\" does not exist"),
gettext_noop("\"%s\" is not a type"),
gettext_noop("Use DROP TYPE to remove a type.") },
{ '\0', 0, NULL, NULL, NULL }
}; };
static void static void
DropErrorMsg(char *relname, char wrongkind, char rightkind) DropErrorMsg(char *relname, char wrongkind, char rightkind)
{ {
struct kindstrings *rentry; const struct msgstrings *rentry;
struct kindstrings *wentry; const struct msgstrings *wentry;
for (rentry = kindstringarray; rentry->kind != '\0'; rentry++) for (rentry = msgstringarray; rentry->kind != '\0'; rentry++)
if (rentry->kind == rightkind) if (rentry->kind == rightkind)
break; break;
Assert(rentry->kind != '\0'); Assert(rentry->kind != '\0');
for (wentry = kindstringarray; wentry->kind != '\0'; wentry++) for (wentry = msgstringarray; wentry->kind != '\0'; wentry++)
if (wentry->kind == wrongkind) if (wentry->kind == wrongkind)
break; break;
/* wrongkind could be something we don't have in our table... */ /* wrongkind could be something we don't have in our table... */
if (wentry->kind != '\0')
elog(ERROR, "\"%s\" is not %s %s. Use DROP %s to remove %s %s", ereport(ERROR,
relname, rentry->indef_article, rentry->name, (errcode(ERRCODE_WRONG_OBJECT_TYPE),
wentry->command, wentry->indef_article, wentry->name); errmsg(rentry->nota_msg, relname),
else (wentry->kind != '\0') ? errhint(wentry->drophint_msg) : 0));
elog(ERROR, "\"%s\" is not %s %s",
relname, rentry->indef_article, rentry->name);
} }
static void static void
CheckDropPermissions(RangeVar *rel, char rightkind) CheckDropPermissions(RangeVar *rel, char rightkind)
{ {
struct kindstrings *rentry; const struct msgstrings *rentry;
Oid relOid; Oid relOid;
HeapTuple tuple; HeapTuple tuple;
Form_pg_class classform; Form_pg_class classform;
for (rentry = kindstringarray; rentry->kind != '\0'; rentry++) for (rentry = msgstringarray; rentry->kind != '\0'; rentry++)
if (rentry->kind == rightkind) if (rentry->kind == rightkind)
break; break;
Assert(rentry->kind != '\0'); Assert(rentry->kind != '\0');
relOid = RangeVarGetRelid(rel, true); relOid = RangeVarGetRelid(rel, true);
if (!OidIsValid(relOid)) if (!OidIsValid(relOid))
elog(ERROR, "%s \"%s\" does not exist", rentry->name, rel->relname); ereport(ERROR,
(errcode(rentry->nonexistent_code),
errmsg(rentry->nonexistent_msg, rel->relname)));
tuple = SearchSysCache(RELOID, tuple = SearchSysCache(RELOID,
ObjectIdGetDatum(relOid), ObjectIdGetDatum(relOid),
0, 0, 0); 0, 0, 0);
if (!HeapTupleIsValid(tuple)) if (!HeapTupleIsValid(tuple))
elog(ERROR, "%s \"%s\" does not exist", rentry->name, rel->relname); elog(ERROR, "cache lookup failed for relation %u", relOid);
classform = (Form_pg_class) GETSTRUCT(tuple); classform = (Form_pg_class) GETSTRUCT(tuple);
...@@ -135,14 +156,22 @@ CheckDropPermissions(RangeVar *rel, char rightkind) ...@@ -135,14 +156,22 @@ CheckDropPermissions(RangeVar *rel, char rightkind)
aclcheck_error(ACLCHECK_NOT_OWNER, rel->relname); aclcheck_error(ACLCHECK_NOT_OWNER, rel->relname);
if (!allowSystemTableMods && IsSystemClass(classform)) if (!allowSystemTableMods && IsSystemClass(classform))
elog(ERROR, "%s \"%s\" is a system %s", ereport(ERROR,
rentry->name, rel->relname, rentry->name); (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
errmsg("\"%s\" is a system catalog",
rel->relname)));
ReleaseSysCache(tuple); ReleaseSysCache(tuple);
} }
static void /*
CheckOwnership(RangeVar *rel, bool noCatalogs) * Verify user has ownership of specified relation, else ereport.
*
* If noCatalogs is true then we also deny access to system catalogs,
* except when allowSystemTableMods is true.
*/
void
CheckRelationOwnership(RangeVar *rel, bool noCatalogs)
{ {
Oid relOid; Oid relOid;
HeapTuple tuple; HeapTuple tuple;
...@@ -151,8 +180,8 @@ CheckOwnership(RangeVar *rel, bool noCatalogs) ...@@ -151,8 +180,8 @@ CheckOwnership(RangeVar *rel, bool noCatalogs)
tuple = SearchSysCache(RELOID, tuple = SearchSysCache(RELOID,
ObjectIdGetDatum(relOid), ObjectIdGetDatum(relOid),
0, 0, 0); 0, 0, 0);
if (!HeapTupleIsValid(tuple)) if (!HeapTupleIsValid(tuple)) /* should not happen */
elog(ERROR, "Relation \"%s\" does not exist", rel->relname); elog(ERROR, "cache lookup failed for relation %u", relOid);
if (!pg_class_ownercheck(relOid, GetUserId())) if (!pg_class_ownercheck(relOid, GetUserId()))
aclcheck_error(ACLCHECK_NOT_OWNER, rel->relname); aclcheck_error(ACLCHECK_NOT_OWNER, rel->relname);
...@@ -161,8 +190,10 @@ CheckOwnership(RangeVar *rel, bool noCatalogs) ...@@ -161,8 +190,10 @@ CheckOwnership(RangeVar *rel, bool noCatalogs)
{ {
if (!allowSystemTableMods && if (!allowSystemTableMods &&
IsSystemClass((Form_pg_class) GETSTRUCT(tuple))) IsSystemClass((Form_pg_class) GETSTRUCT(tuple)))
elog(ERROR, "relation \"%s\" is a system catalog", ereport(ERROR,
rel->relname); (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
errmsg("\"%s\" is a system catalog",
rel->relname)));
} }
ReleaseSysCache(tuple); ReleaseSysCache(tuple);
...@@ -222,10 +253,13 @@ check_xact_readonly(Node *parsetree) ...@@ -222,10 +253,13 @@ check_xact_readonly(Node *parsetree)
case T_DropUserStmt: case T_DropUserStmt:
case T_GrantStmt: case T_GrantStmt:
case T_TruncateStmt: case T_TruncateStmt:
elog(ERROR, "transaction is read-only"); ereport(ERROR,
(errcode(ERRCODE_READ_ONLY_SQL_TRANSACTION),
errmsg("transaction is read-only")));
break; break;
default: default:
/*nothing*/; /* do nothing */
break;
} }
} }
...@@ -407,7 +441,9 @@ ProcessUtility(Node *parsetree, ...@@ -407,7 +441,9 @@ ProcessUtility(Node *parsetree,
break; break;
default: default:
elog(ERROR, "invalid object type for DropStmt: %d", stmt->removeType); elog(ERROR, "unrecognized drop object type: %d",
(int) stmt->removeType);
break;
} }
/* /*
...@@ -551,7 +587,9 @@ ProcessUtility(Node *parsetree, ...@@ -551,7 +587,9 @@ ProcessUtility(Node *parsetree,
case 'U': /* ALTER OWNER */ case 'U': /* ALTER OWNER */
/* check that we are the superuser */ /* check that we are the superuser */
if (!superuser()) if (!superuser())
elog(ERROR, "ALTER TABLE: permission denied"); ereport(ERROR,
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
errmsg("permission denied")));
/* get_usesysid raises an error if no such user */ /* get_usesysid raises an error if no such user */
AlterTableOwner(relid, AlterTableOwner(relid,
get_usesysid(stmt->name)); get_usesysid(stmt->name));
...@@ -565,8 +603,8 @@ ProcessUtility(Node *parsetree, ...@@ -565,8 +603,8 @@ ProcessUtility(Node *parsetree,
false); false);
break; break;
default: /* oops */ default: /* oops */
elog(ERROR, "ProcessUtility: Invalid type for AlterTableStmt: %d", elog(ERROR, "unrecognized alter table type: %d",
stmt->subtype); (int) stmt->subtype);
break; break;
} }
} }
...@@ -611,14 +649,16 @@ ProcessUtility(Node *parsetree, ...@@ -611,14 +649,16 @@ ProcessUtility(Node *parsetree,
case 'U': /* OWNER TO */ case 'U': /* OWNER TO */
/* check that we are the superuser */ /* check that we are the superuser */
if (!superuser()) if (!superuser())
elog(ERROR, "ALTER DOMAIN: permission denied"); ereport(ERROR,
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
errmsg("permission denied")));
/* get_usesysid raises an error if no such user */ /* get_usesysid raises an error if no such user */
AlterTypeOwner(stmt->typename, AlterTypeOwner(stmt->typename,
get_usesysid(stmt->name)); get_usesysid(stmt->name));
break; break;
default: /* oops */ default: /* oops */
elog(ERROR, "ProcessUtility: Invalid type for AlterDomainStmt: %d", elog(ERROR, "unrecognized alter domain type: %d",
stmt->subtype); (int) stmt->subtype);
break; break;
} }
} }
...@@ -650,7 +690,9 @@ ProcessUtility(Node *parsetree, ...@@ -650,7 +690,9 @@ ProcessUtility(Node *parsetree,
DefineType(stmt->defnames, stmt->definition); DefineType(stmt->defnames, stmt->definition);
break; break;
default: default:
elog(ERROR, "invalid object type for DefineStmt: %d", stmt->kind); elog(ERROR, "unrecognized define stmt type: %d",
(int) stmt->kind);
break;
} }
} }
break; break;
...@@ -679,7 +721,7 @@ ProcessUtility(Node *parsetree, ...@@ -679,7 +721,7 @@ ProcessUtility(Node *parsetree,
{ {
IndexStmt *stmt = (IndexStmt *) parsetree; IndexStmt *stmt = (IndexStmt *) parsetree;
CheckOwnership(stmt->relation, true); CheckRelationOwnership(stmt->relation, true);
DefineIndex(stmt->relation, /* relation */ DefineIndex(stmt->relation, /* relation */
stmt->idxname, /* index name */ stmt->idxname, /* index name */
...@@ -865,7 +907,9 @@ ProcessUtility(Node *parsetree, ...@@ -865,7 +907,9 @@ ProcessUtility(Node *parsetree,
stmt->behavior); stmt->behavior);
break; break;
default: default:
elog(ERROR, "invalid object type for DropPropertyStmt: %d", stmt->removeType); elog(ERROR, "unrecognized object type: %d",
(int) stmt->removeType);
break;
} }
} }
break; break;
...@@ -926,7 +970,9 @@ ProcessUtility(Node *parsetree, ...@@ -926,7 +970,9 @@ ProcessUtility(Node *parsetree,
case T_CheckPointStmt: case T_CheckPointStmt:
if (!superuser()) if (!superuser())
elog(ERROR, "CHECKPOINT: permission denied"); ereport(ERROR,
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
errmsg("permission denied")));
CreateCheckPoint(false, false); CreateCheckPoint(false, false);
break; break;
...@@ -937,18 +983,20 @@ ProcessUtility(Node *parsetree, ...@@ -937,18 +983,20 @@ ProcessUtility(Node *parsetree,
switch (stmt->kind) switch (stmt->kind)
{ {
case OBJECT_INDEX: case OBJECT_INDEX:
CheckOwnership(stmt->relation, false); CheckRelationOwnership(stmt->relation, false);
ReindexIndex(stmt->relation, stmt->force); ReindexIndex(stmt->relation, stmt->force);
break; break;
case OBJECT_TABLE: case OBJECT_TABLE:
CheckOwnership(stmt->relation, false); CheckRelationOwnership(stmt->relation, false);
ReindexTable(stmt->relation, stmt->force); ReindexTable(stmt->relation, stmt->force);
break; break;
case OBJECT_DATABASE: case OBJECT_DATABASE:
ReindexDatabase(stmt->name, stmt->force, false); ReindexDatabase(stmt->name, stmt->force, false);
break; break;
default: default:
elog(ERROR, "invalid object type for ReindexStmt: %d", stmt->kind); elog(ERROR, "unrecognized object type: %d",
(int) stmt->kind);
break;
} }
break; break;
} }
...@@ -975,8 +1023,8 @@ ProcessUtility(Node *parsetree, ...@@ -975,8 +1023,8 @@ ProcessUtility(Node *parsetree,
break; break;
default: default:
elog(ERROR, "ProcessUtility: command #%d unsupported", elog(ERROR, "unrecognized node type: %d",
nodeTag(parsetree)); (int) nodeTag(parsetree));
break; break;
} }
} }
...@@ -1500,8 +1548,8 @@ CreateCommandTag(Node *parsetree) ...@@ -1500,8 +1548,8 @@ CreateCommandTag(Node *parsetree)
break; break;
default: default:
elog(LOG, "CreateCommandTag: unknown parse node type %d", elog(NOTICE, "unrecognized node type: %d",
nodeTag(parsetree)); (int) nodeTag(parsetree));
tag = "???"; tag = "???";
break; break;
} }
......
...@@ -37,7 +37,7 @@ ...@@ -37,7 +37,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/error/elog.c,v 1.113 2003/07/18 23:20:32 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/utils/error/elog.c,v 1.114 2003/07/22 19:00:12 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -572,6 +572,42 @@ errcode_for_file_access(void) ...@@ -572,6 +572,42 @@ errcode_for_file_access(void)
return 0; /* return value does not matter */ return 0; /* return value does not matter */
} }
/*
* errcode_for_socket_access --- add SQLSTATE error code to the current error
*
* The SQLSTATE code is chosen based on the saved errno value. We assume
* that the failing operation was some type of socket access.
*
* NOTE: the primary error message string should generally include %m
* when this is used.
*/
int
errcode_for_socket_access(void)
{
ErrorData *edata = &errordata[errordata_stack_depth];
/* we don't bother incrementing recursion_depth */
CHECK_STACK_DEPTH();
switch (edata->saved_errno)
{
/* Loss of connection */
case EPIPE:
#ifdef ECONNRESET
case ECONNRESET:
#endif
edata->sqlerrcode = ERRCODE_CONNECTION_FAILURE;
break;
/* All else is classified as internal errors */
default:
edata->sqlerrcode = ERRCODE_INTERNAL_ERROR;
break;
}
return 0; /* return value does not matter */
}
/* /*
* This macro handles expansion of a format string and associated parameters; * This macro handles expansion of a format string and associated parameters;
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Id: utility.h,v 1.18 2003/05/06 20:26:28 tgl Exp $ * $Id: utility.h,v 1.19 2003/07/22 19:00:12 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -25,4 +25,6 @@ extern TupleDesc UtilityTupleDescriptor(Node *parsetree); ...@@ -25,4 +25,6 @@ extern TupleDesc UtilityTupleDescriptor(Node *parsetree);
extern const char *CreateCommandTag(Node *parsetree); extern const char *CreateCommandTag(Node *parsetree);
extern void CheckRelationOwnership(RangeVar *rel, bool noCatalogs);
#endif /* UTILITY_H */ #endif /* UTILITY_H */
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Id: elog.h,v 1.53 2003/07/21 20:29:39 tgl Exp $ * $Id: elog.h,v 1.54 2003/07/22 19:00:12 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -155,6 +155,8 @@ ...@@ -155,6 +155,8 @@
#define ERRCODE_UNTERMINATED_C_STRING MAKE_SQLSTATE('2','2', '0','2','4') #define ERRCODE_UNTERMINATED_C_STRING MAKE_SQLSTATE('2','2', '0','2','4')
#define ERRCODE_ZERO_LENGTH_CHARACTER_STRING MAKE_SQLSTATE('2','2', '0','0','F') #define ERRCODE_ZERO_LENGTH_CHARACTER_STRING MAKE_SQLSTATE('2','2', '0','0','F')
#define ERRCODE_BAD_COPY_FILE_FORMAT MAKE_SQLSTATE('2','2', 'P','0','1') #define ERRCODE_BAD_COPY_FILE_FORMAT MAKE_SQLSTATE('2','2', 'P','0','1')
#define ERRCODE_INVALID_BINARY_REPRESENTATION MAKE_SQLSTATE('2','2', 'P','0','2')
#define ERRCODE_FLOATING_POINT_EXCEPTION MAKE_SQLSTATE('2','2', 'P','0','3')
/* Class 23 - Integrity Constraint Violation */ /* Class 23 - Integrity Constraint Violation */
#define ERRCODE_INTEGRITY_CONSTRAINT_VIOLATION MAKE_SQLSTATE('2','3', '0','0','0') #define ERRCODE_INTEGRITY_CONSTRAINT_VIOLATION MAKE_SQLSTATE('2','3', '0','0','0')
...@@ -178,6 +180,7 @@ ...@@ -178,6 +180,7 @@
#define ERRCODE_READ_ONLY_SQL_TRANSACTION MAKE_SQLSTATE('2','5', '0','0','6') #define ERRCODE_READ_ONLY_SQL_TRANSACTION MAKE_SQLSTATE('2','5', '0','0','6')
#define ERRCODE_SCHEMA_AND_DATA_STATEMENT_MIXING_NOT_SUPPORTED MAKE_SQLSTATE('2','5', '0','0','7') #define ERRCODE_SCHEMA_AND_DATA_STATEMENT_MIXING_NOT_SUPPORTED MAKE_SQLSTATE('2','5', '0','0','7')
#define ERRCODE_NO_ACTIVE_SQL_TRANSACTION MAKE_SQLSTATE('2','5', 'P','0','1') #define ERRCODE_NO_ACTIVE_SQL_TRANSACTION MAKE_SQLSTATE('2','5', 'P','0','1')
#define ERRCODE_IN_FAILED_SQL_TRANSACTION MAKE_SQLSTATE('2','5', 'P','0','2')
/* Class 26 - Invalid SQL Statement Name */ /* Class 26 - Invalid SQL Statement Name */
/* (we take this to mean prepared statements) */ /* (we take this to mean prepared statements) */
...@@ -244,6 +247,7 @@ ...@@ -244,6 +247,7 @@
#define ERRCODE_NAME_TOO_LONG MAKE_SQLSTATE('4','2', '6','2','2') #define ERRCODE_NAME_TOO_LONG MAKE_SQLSTATE('4','2', '6','2','2')
#define ERRCODE_RESERVED_NAME MAKE_SQLSTATE('4','2', '9','3','9') #define ERRCODE_RESERVED_NAME MAKE_SQLSTATE('4','2', '9','3','9')
#define ERRCODE_DATATYPE_MISMATCH MAKE_SQLSTATE('4','2', '8','0','4') #define ERRCODE_DATATYPE_MISMATCH MAKE_SQLSTATE('4','2', '8','0','4')
#define ERRCODE_INDETERMINATE_DATATYPE MAKE_SQLSTATE('4','2', 'P','1','8')
#define ERRCODE_WRONG_OBJECT_TYPE MAKE_SQLSTATE('4','2', '8','0','9') #define ERRCODE_WRONG_OBJECT_TYPE MAKE_SQLSTATE('4','2', '8','0','9')
/* /*
* Note: for ERRCODE purposes, we divide namable objects into these categories: * Note: for ERRCODE purposes, we divide namable objects into these categories:
...@@ -314,11 +318,17 @@ ...@@ -314,11 +318,17 @@
/* Class 57 - Operator Intervention (class borrowed from DB2) */ /* Class 57 - Operator Intervention (class borrowed from DB2) */
#define ERRCODE_OPERATOR_INTERVENTION MAKE_SQLSTATE('5','7', '0','0','0') #define ERRCODE_OPERATOR_INTERVENTION MAKE_SQLSTATE('5','7', '0','0','0')
#define ERRCODE_QUERY_CANCELED MAKE_SQLSTATE('5','7', '0','1','4') #define ERRCODE_QUERY_CANCELED MAKE_SQLSTATE('5','7', '0','1','4')
#define ERRCODE_ADMIN_SHUTDOWN MAKE_SQLSTATE('5','7', 'P','0','1')
#define ERRCODE_CRASH_SHUTDOWN MAKE_SQLSTATE('5','7', 'P','0','2')
#define ERRCODE_CANNOT_CONNECT_NOW MAKE_SQLSTATE('5','7', 'P','0','3')
/* Class 58 - System Error (class borrowed from DB2) */ /* Class 58 - System Error (class borrowed from DB2) */
/* (we define this as errors external to PostgreSQL itself) */ /* (we define this as errors external to PostgreSQL itself) */
#define ERRCODE_IO_ERROR MAKE_SQLSTATE('5','8', '0','3','0') #define ERRCODE_IO_ERROR MAKE_SQLSTATE('5','8', '0','3','0')
/* Class F0 - Configuration File Error (PostgreSQL-specific error class) */
#define ERRCODE_CONFIG_FILE_ERROR MAKE_SQLSTATE('F','0', '0','0','0')
/* Class XX - Internal Error (PostgreSQL-specific error class) */ /* Class XX - Internal Error (PostgreSQL-specific error class) */
/* (this is for "can't-happen" conditions and software bugs) */ /* (this is for "can't-happen" conditions and software bugs) */
#define ERRCODE_INTERNAL_ERROR MAKE_SQLSTATE('X','X', '0','0','0') #define ERRCODE_INTERNAL_ERROR MAKE_SQLSTATE('X','X', '0','0','0')
...@@ -361,6 +371,7 @@ extern void errfinish(int dummy, ...); ...@@ -361,6 +371,7 @@ extern void errfinish(int dummy, ...);
extern int errcode(int sqlerrcode); extern int errcode(int sqlerrcode);
extern int errcode_for_file_access(void); extern int errcode_for_file_access(void);
extern int errcode_for_socket_access(void);
extern int errmsg(const char *fmt, ...) extern int errmsg(const char *fmt, ...)
/* This extension allows gcc to check the format string for consistency with /* This extension allows gcc to check the format string for consistency with
......
...@@ -689,8 +689,8 @@ FETCH 1 FROM foo24; ...@@ -689,8 +689,8 @@ FETCH 1 FROM foo24;
(1 row) (1 row)
FETCH BACKWARD 1 FROM foo24; -- should fail FETCH BACKWARD 1 FROM foo24; -- should fail
ERROR: Cursor can only scan forward ERROR: cursor can only scan forward
Declare it with SCROLL option to enable backward scan HINT: Declare it with SCROLL option to enable backward scan.
END; END;
-- --
-- Cursors outside transaction blocks -- Cursors outside transaction blocks
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment