Clean up callers of AllocateFile and BasicOpenFile to ensure that

a reasonable error message (including the kernel errno message)
is reported on any file open failure.
This commit is contained in:
Tom Lane 2000-08-27 21:50:18 +00:00
parent 5ba9d8c2d4
commit 4c8d2f7f7b
4 changed files with 34 additions and 38 deletions

View File

@ -6,7 +6,7 @@
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* 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/commands/user.c,v 1.66 2000/08/03 16:34:01 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/commands/user.c,v 1.67 2000/08/27 21:50:17 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -72,7 +72,7 @@ write_password_file(Relation rel)
fp = AllocateFile(tempname, "w"); fp = AllocateFile(tempname, "w");
umask(oumask); umask(oumask);
if (fp == NULL) if (fp == NULL)
elog(ERROR, "%s: %m", tempname); elog(ERROR, "write_password_file: unable to write %s: %m", tempname);
/* read table */ /* read table */
scan = heap_beginscan(rel, false, SnapshotSelf, 0, NULL); scan = heap_beginscan(rel, false, SnapshotSelf, 0, NULL);
@ -156,7 +156,7 @@ write_password_file(Relation rel)
filename = crypt_getpwdreloadfilename(); filename = crypt_getpwdreloadfilename();
flagfd = BasicOpenFile(filename, O_WRONLY | O_CREAT, 0600); flagfd = BasicOpenFile(filename, O_WRONLY | O_CREAT, 0600);
if (flagfd < 0) if (flagfd < 0)
elog(NOTICE, "%s: %m", filename); elog(NOTICE, "write_password_file: unable to write %s: %m", filename);
else else
close(flagfd); close(flagfd);
pfree((void *) filename); pfree((void *) filename);

View File

@ -9,11 +9,12 @@
* Dec 17, 1997 - Todd A. Brandys * Dec 17, 1997 - Todd A. Brandys
* Orignal Version Completed. * Orignal Version Completed.
* *
* $Id: crypt.c,v 1.28 2000/07/12 22:58:59 petere Exp $ * $Id: crypt.c,v 1.29 2000/08/27 21:50:18 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
#include <errno.h>
#include <unistd.h> #include <unistd.h>
#include "postgres.h" #include "postgres.h"
@ -32,11 +33,10 @@ int pwd_cache_count = 0;
/*-------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------*/
char * char *
crypt_getpwdfilename() crypt_getpwdfilename(void)
{ {
static char *pfnam = NULL;
int bufsize; int bufsize;
char *pfnam;
bufsize = strlen(DataDir) + 8 + strlen(CRYPT_PWD_FILE) + 1; bufsize = strlen(DataDir) + 8 + strlen(CRYPT_PWD_FILE) + 1;
pfnam = (char *) palloc(bufsize); pfnam = (char *) palloc(bufsize);
@ -48,12 +48,11 @@ crypt_getpwdfilename()
/*-------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------*/
char * char *
crypt_getpwdreloadfilename() crypt_getpwdreloadfilename(void)
{ {
static char *rpfnam = NULL;
char *pwdfilename; char *pwdfilename;
int bufsize; int bufsize;
char *rpfnam;
pwdfilename = crypt_getpwdfilename(); pwdfilename = crypt_getpwdfilename();
bufsize = strlen(pwdfilename) + strlen(CRYPT_PWD_RELOAD_SUFX) + 1; bufsize = strlen(pwdfilename) + strlen(CRYPT_PWD_RELOAD_SUFX) + 1;
@ -65,9 +64,8 @@ crypt_getpwdreloadfilename()
/*-------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------*/
static static FILE *
FILE * crypt_openpwdfile(void)
crypt_openpwdfile()
{ {
char *filename; char *filename;
FILE *pwdfile; FILE *pwdfile;
@ -75,13 +73,16 @@ crypt_openpwdfile()
filename = crypt_getpwdfilename(); filename = crypt_getpwdfilename();
pwdfile = AllocateFile(filename, PG_BINARY_R); pwdfile = AllocateFile(filename, PG_BINARY_R);
if (pwdfile == NULL)
fprintf(stderr, "Couldn't read %s: %s\n",
filename, strerror(errno));
return pwdfile; return pwdfile;
} }
/*-------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------*/
static static int
int
compar_user(const void *user_a, const void *user_b) compar_user(const void *user_a, const void *user_b)
{ {
@ -115,9 +116,8 @@ compar_user(const void *user_a, const void *user_b)
/*-------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------*/
static static void
void crypt_loadpwdfile(void)
crypt_loadpwdfile()
{ {
char *filename; char *filename;
@ -176,8 +176,7 @@ crypt_loadpwdfile()
/*-------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------*/
static static void
void
crypt_parsepwdentry(char *buffer, char **pwd, char **valdate) crypt_parsepwdentry(char *buffer, char **pwd, char **valdate)
{ {
@ -212,11 +211,9 @@ crypt_parsepwdentry(char *buffer, char **pwd, char **valdate)
/*-------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------*/
static static int
int
crypt_getloginfo(const char *user, char **passwd, char **valuntil) crypt_getloginfo(const char *user, char **passwd, char **valuntil)
{ {
char *pwd, char *pwd,
*valdate; *valdate;
void *fakeout; void *fakeout;

View File

@ -5,7 +5,7 @@
* wherein you authenticate a user by seeing what IP address the system * wherein you authenticate a user by seeing what IP address the system
* says he comes from and possibly using ident). * says he comes from and possibly using ident).
* *
* $Id: hba.c,v 1.53 2000/07/08 03:04:39 tgl Exp $ * $Id: hba.c,v 1.54 2000/08/27 21:50:18 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -427,10 +427,8 @@ find_hba_entry(hbaPort *port, bool *hba_ok_p)
/* The open of the config file failed. */ /* The open of the config file failed. */
snprintf(PQerrormsg, PQERRORMSG_LENGTH, snprintf(PQerrormsg, PQERRORMSG_LENGTH,
"find_hba_entry: Host-based authentication config file " "find_hba_entry: Unable to open authentication config file \"%s\": %s\n",
"does not exist or permissions are not setup correctly! " conf_file, strerror(errno));
"Unable to open file \"%s\".\n",
conf_file);
fputs(PQerrormsg, stderr); fputs(PQerrormsg, stderr);
pqdebug("%s", PQerrormsg); pqdebug("%s", PQerrormsg);
} }
@ -812,16 +810,13 @@ verify_against_usermap(const char *pguser,
{ {
/* The open of the map file failed. */ /* The open of the map file failed. */
*checks_out_p = false;
snprintf(PQerrormsg, PQERRORMSG_LENGTH, snprintf(PQerrormsg, PQERRORMSG_LENGTH,
"verify_against_usermap: usermap file for Ident-based " "verify_against_usermap: Unable to open usermap file \"%s\": %s\n",
"authentication " map_file, strerror(errno));
"does not exist or permissions are not setup correctly! "
"Unable to open file \"%s\".\n",
map_file);
fputs(PQerrormsg, stderr); fputs(PQerrormsg, stderr);
pqdebug("%s", PQerrormsg); pqdebug("%s", PQerrormsg);
*checks_out_p = false;
} }
else else
{ {
@ -981,7 +976,10 @@ GetCharSetByHost(char *TableName, int host, const char *DataDir)
snprintf(map_file, bufsize, "%s/%s", DataDir, CHARSET_FILE); snprintf(map_file, bufsize, "%s/%s", DataDir, CHARSET_FILE);
file = AllocateFile(map_file, PG_BINARY_R); file = AllocateFile(map_file, PG_BINARY_R);
if (file == NULL) if (file == NULL)
{
/* XXX should we log a complaint? */
return; return;
}
while (!eof) while (!eof)
{ {
c = getc(file); c = getc(file);

View File

@ -2,10 +2,11 @@
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Id: password.c,v 1.31 2000/07/08 03:04:40 tgl Exp $ * $Id: password.c,v 1.32 2000/08/27 21:50:18 tgl Exp $
* *
*/ */
#include <errno.h>
#include <unistd.h> #include <unistd.h>
#include "postgres.h" #include "postgres.h"
@ -36,8 +37,8 @@ verify_password(const Port *port, const char *user, const char *password)
if (!pw_file) if (!pw_file)
{ {
snprintf(PQerrormsg, PQERRORMSG_LENGTH, snprintf(PQerrormsg, PQERRORMSG_LENGTH,
"verify_password: couldn't open password file '%s'\n", "verify_password: Unable to open password file \"%s\": %s\n",
pw_file_fullname); pw_file_fullname, strerror(errno));
fputs(PQerrormsg, stderr); fputs(PQerrormsg, stderr);
pqdebug("%s", PQerrormsg); pqdebug("%s", PQerrormsg);