Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
postgres-lambda-diff
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Jakob Huber
postgres-lambda-diff
Commits
329b38ee
Commit
329b38ee
authored
28 years ago
by
Bryan Henderson
Browse files
Options
Downloads
Patches
Plain Diff
Add PGUSER environment variable for client to specify Postgres username.
parent
ca5f6dba
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/interfaces/libpq/fe-connect.c
+76
-33
76 additions, 33 deletions
src/interfaces/libpq/fe-connect.c
src/interfaces/libpq/libpq-fe.h
+2
-1
2 additions, 1 deletion
src/interfaces/libpq/libpq-fe.h
with
78 additions
and
34 deletions
src/interfaces/libpq/fe-connect.c
+
76
−
33
View file @
329b38ee
...
@@ -7,7 +7,7 @@
...
@@ -7,7 +7,7 @@
*
*
*
*
* IDENTIFICATION
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.
8
1996/
08/19 13:38:42 scrappy
Exp $
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.
9
1996/
10/10 08:20:09 bryanh
Exp $
*
*
*-------------------------------------------------------------------------
*-------------------------------------------------------------------------
*/
*/
...
@@ -21,8 +21,9 @@
...
@@ -21,8 +21,9 @@
#include
<string.h>
#include
<string.h>
#include
<netdb.h>
#include
<netdb.h>
#include
<errno.h>
#include
<errno.h>
#include
"libpq/pqcomm.h"
/* for decls of MsgType, PacketBuf, StartupInfo */
#include
<signal.h>
#include
"fe-auth.h"
#include
<libpq/pqcomm.h>
/* for decls of MsgType, PacketBuf, StartupInfo */
#include
<fe-auth.h>
#include
"libpq-fe.h"
#include
"libpq-fe.h"
#if defined(PORTNAME_ultrix4) || defined(PORTNAME_next)
#if defined(PORTNAME_ultrix4) || defined(PORTNAME_next)
...
@@ -56,19 +57,45 @@ static void closePGconn(PGconn *conn);
...
@@ -56,19 +57,45 @@ static void closePGconn(PGconn *conn);
/* ----------------
/* ----------------
* PQsetdb
* PQsetdb
*
*
* establishes a connectin to a postgres backend through the postmaster
* establishes a connecti
o
n to a postgres backend through the postmaster
* at the specified host and port.
* at the specified host and port.
*
*
* returns a PGconn* which is needed for all subsequent libpq calls
* returns a PGconn* which is needed for all subsequent libpq calls
* if the status field of the connection returned is CONNECTION_BAD,
* if the status field of the connection returned is CONNECTION_BAD,
* then some fields may be null'ed out instead of having valid values
* then some fields may be null'ed out instead of having valid values
*
* Uses these environment variables:
*
* PGHOST identifies host to which to connect if <pghost> argument
* is NULL or a null string.
*
* PGPORT identifies TCP port to which to connect if <pgport> argument
* is NULL or a null string.
*
* PGTTY identifies tty to which to send messages if <pgtty> argument
* is NULL or a null string.
*
* PGOPTIONS identifies connection options if <pgoptions> argument is
* NULL or a null string.
*
* PGUSER Postgres username to associate with the connection.
*
* PGDATABASE name of database to which to connect if <pgdatabase>
* argument is NULL or a null string
*
* None of the above need be defined. There are defaults for all of them.
*
* ----------------
* ----------------
*/
*/
PGconn
*
PGconn
*
PQsetdb
(
const
char
*
pghost
,
const
char
*
pgport
,
const
char
*
pgoptions
,
const
char
*
pgtty
,
const
char
*
dbName
)
PQsetdb
(
const
char
*
pghost
,
const
char
*
pgport
,
const
char
*
pgoptions
,
const
char
*
pgtty
,
const
char
*
dbName
)
{
{
PGconn
*
conn
;
PGconn
*
conn
;
const
char
*
tmp
;
char
*
tmp
;
char
errorMessage
[
ERROR_MSG_LENGTH
];
/* An error message from some service we call. */
bool
error
;
/* We encountered an error that prevents successful completion */
conn
=
(
PGconn
*
)
malloc
(
sizeof
(
PGconn
));
conn
=
(
PGconn
*
)
malloc
(
sizeof
(
PGconn
));
...
@@ -113,33 +140,45 @@ PQsetdb(const char *pghost, const char* pgport, const char* pgoptions, const cha
...
@@ -113,33 +140,45 @@ PQsetdb(const char *pghost, const char* pgport, const char* pgoptions, const cha
conn
->
pgoptions
=
strdup
(
tmp
);
conn
->
pgoptions
=
strdup
(
tmp
);
}
else
}
else
conn
->
pgoptions
=
strdup
(
pgoptions
);
conn
->
pgoptions
=
strdup
(
pgoptions
);
if
(((
tmp
=
dbName
)
&&
(
dbName
[
0
]
!=
'\0'
))
||
((
tmp
=
getenv
(
"PGDATABASE"
))))
{
if
(
tmp
=
getenv
(
"PGUSER"
))
{
conn
->
dbName
=
strdup
(
tmp
);
error
=
FALSE
;
conn
->
pguser
=
strdup
(
tmp
);
}
else
{
}
else
{
char
errorMessage
[
ERROR_MSG_LENGTH
];
tmp
=
fe_getauthname
(
errorMessage
);
if
((
tmp
=
fe_getauthname
(
errorMessage
))
!=
0
)
{
if
(
tmp
==
0
)
{
conn
->
dbName
=
strdup
(
tmp
);
error
=
TRUE
;
free
((
char
*
)
tmp
);
}
else
{
sprintf
(
conn
->
errorMessage
,
sprintf
(
conn
->
errorMessage
,
"FATAL: PQsetdb: Unable to determine a database name!
\n
"
);
"FATAL: PQsetdb: Unable to determine a Postgres username!
\n
"
);
conn
->
dbName
=
NULL
;
}
else
{
return
conn
;
error
=
FALSE
;
conn
->
pguser
=
tmp
;
}
}
}
}
conn
->
status
=
connectDB
(
conn
);
if
(
conn
->
status
==
CONNECTION_OK
)
{
if
(
!
error
)
{
PGresult
*
res
;
if
(((
tmp
=
(
char
*
)
dbName
)
&&
(
dbName
[
0
]
!=
'\0'
))
||
/* Send a blank query to make sure everything works; in particular, that
((
tmp
=
getenv
(
"PGDATABASE"
))))
{
the database exists.
conn
->
dbName
=
strdup
(
tmp
);
*/
}
else
conn
->
dbName
=
conn
->
pguser
;
res
=
PQexec
(
conn
,
" "
);
}
else
conn
->
dbName
=
NULL
;
if
(
res
==
NULL
||
res
->
resultStatus
!=
PGRES_EMPTY_QUERY
)
{
/* PQexec has put error message in conn->errorMessage */
if
(
error
)
conn
->
status
=
CONNECTION_BAD
;
closePGconn
(
conn
);
else
{
conn
->
status
=
connectDB
(
conn
);
/* Puts message in conn->errorMessage */
if
(
conn
->
status
==
CONNECTION_OK
)
{
PGresult
*
res
;
/* Send a blank query to make sure everything works;
in particular, that the database exists.
*/
res
=
PQexec
(
conn
,
" "
);
if
(
res
==
NULL
||
res
->
resultStatus
!=
PGRES_EMPTY_QUERY
)
{
/* PQexec has put error message in conn->errorMessage */
closePGconn
(
conn
);
}
PQclear
(
res
);
}
}
PQclear
(
res
);
}
}
}
}
return
conn
;
return
conn
;
...
@@ -165,7 +204,6 @@ connectDB(PGconn *conn)
...
@@ -165,7 +204,6 @@ connectDB(PGconn *conn)
Port
*
port
=
conn
->
port
;
Port
*
port
=
conn
->
port
;
int
portno
;
int
portno
;
char
*
user
;
/*
/*
//
//
// Initialize the startup packet.
// Initialize the startup packet.
...
@@ -175,11 +213,7 @@ connectDB(PGconn *conn)
...
@@ -175,11 +213,7 @@ connectDB(PGconn *conn)
//
//
//
//
*/
*/
user
=
fe_getauthname
(
conn
->
errorMessage
);
strncpy
(
startup
.
user
,
conn
->
pguser
,
sizeof
(
startup
.
user
));
if
(
!
user
)
goto
connect_errReturn
;
strncpy
(
startup
.
user
,
user
,
sizeof
(
startup
.
user
));
free
(
user
);
strncpy
(
startup
.
database
,
conn
->
dbName
,
sizeof
(
startup
.
database
));
strncpy
(
startup
.
database
,
conn
->
dbName
,
sizeof
(
startup
.
database
));
strncpy
(
startup
.
tty
,
conn
->
pgtty
,
sizeof
(
startup
.
tty
));
strncpy
(
startup
.
tty
,
conn
->
pgtty
,
sizeof
(
startup
.
tty
));
if
(
conn
->
pgoptions
)
{
if
(
conn
->
pgoptions
)
{
...
@@ -292,6 +326,7 @@ freePGconn(PGconn *conn)
...
@@ -292,6 +326,7 @@ freePGconn(PGconn *conn)
if
(
conn
->
pgoptions
)
free
(
conn
->
pgoptions
);
if
(
conn
->
pgoptions
)
free
(
conn
->
pgoptions
);
if
(
conn
->
pgport
)
free
(
conn
->
pgport
);
if
(
conn
->
pgport
)
free
(
conn
->
pgport
);
if
(
conn
->
dbName
)
free
(
conn
->
dbName
);
if
(
conn
->
dbName
)
free
(
conn
->
dbName
);
if
(
conn
->
pguser
)
free
(
conn
->
pguser
);
if
(
conn
->
notifyList
)
DLFreeList
(
conn
->
notifyList
);
if
(
conn
->
notifyList
)
DLFreeList
(
conn
->
notifyList
);
free
(
conn
);
free
(
conn
);
}
}
...
@@ -303,8 +338,16 @@ freePGconn(PGconn *conn)
...
@@ -303,8 +338,16 @@ freePGconn(PGconn *conn)
static
void
static
void
closePGconn
(
PGconn
*
conn
)
closePGconn
(
PGconn
*
conn
)
{
{
const
struct
sigaction
ignore_action
=
{
SIG_IGN
,
0
,
0
,
NULL
};
struct
sigaction
oldaction
;
/* If connection is already gone, that's cool. No reason for kernel
to kill us when we try to write to it. So ignore SIGPIPE signals.
*/
sigaction
(
SIGPIPE
,
(
struct
sigaction
*
)
&
ignore_action
,
&
oldaction
);
fputs
(
"X
\0
"
,
conn
->
Pfout
);
fputs
(
"X
\0
"
,
conn
->
Pfout
);
fflush
(
conn
->
Pfout
);
fflush
(
conn
->
Pfout
);
sigaction
(
SIGPIPE
,
&
oldaction
,
NULL
);
if
(
conn
->
Pfout
)
fclose
(
conn
->
Pfout
);
if
(
conn
->
Pfout
)
fclose
(
conn
->
Pfout
);
if
(
conn
->
Pfin
)
fclose
(
conn
->
Pfin
);
if
(
conn
->
Pfin
)
fclose
(
conn
->
Pfin
);
if
(
conn
->
Pfdebug
)
fclose
(
conn
->
Pfdebug
);
if
(
conn
->
Pfdebug
)
fclose
(
conn
->
Pfdebug
);
...
...
This diff is collapsed.
Click to expand it.
src/interfaces/libpq/libpq-fe.h
+
2
−
1
View file @
329b38ee
...
@@ -6,7 +6,7 @@
...
@@ -6,7 +6,7 @@
*
*
* Copyright (c) 1994, Regents of the University of California
* Copyright (c) 1994, Regents of the University of California
*
*
* $Id: libpq-fe.h,v 1.
7
1996/0
8
/1
3
0
1:34:29 scrappy
Exp $
* $Id: libpq-fe.h,v 1.
8
1996/
1
0/1
0
0
8:20:11 bryanh
Exp $
*
*
*-------------------------------------------------------------------------
*-------------------------------------------------------------------------
*/
*/
...
@@ -112,6 +112,7 @@ typedef struct pg_conn{
...
@@ -112,6 +112,7 @@ typedef struct pg_conn{
void
*
port
;
/* really a Port* */
void
*
port
;
/* really a Port* */
int
asyncNotifyWaiting
;
int
asyncNotifyWaiting
;
Dllist
*
notifyList
;
Dllist
*
notifyList
;
char
*
pguser
;
/* Postgres username of user who is connected */
}
PGconn
;
}
PGconn
;
#define CMDSTATUS_LEN 40
#define CMDSTATUS_LEN 40
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment