Skip to content

Commit 92d1cc8

Browse files
committed
Issue psql connection warnings on connection start and via \c, per
observation by David Fetter.
1 parent 6b797c8 commit 92d1cc8

File tree

3 files changed

+113
-127
lines changed

3 files changed

+113
-127
lines changed

src/bin/psql/command.c

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2008, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.191 2008/06/26 01:35:45 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.192 2008/07/01 00:08:18 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "command.h"
@@ -29,6 +29,9 @@
2929
#include <sys/types.h> /* for umask() */
3030
#include <sys/stat.h> /* for stat() */
3131
#endif
32+
#ifdef USE_SSL
33+
#include <openssl/ssl.h>
34+
#endif
3235

3336
#include "portability/instr_time.h"
3437

@@ -57,6 +60,15 @@ static bool do_edit(const char *filename_arg, PQExpBuffer query_buf);
5760
static bool do_connect(char *dbname, char *user, char *host, char *port);
5861
static bool do_shell(const char *command);
5962

63+
#ifdef USE_SSL
64+
static void printSSLInfo(void);
65+
#endif
66+
67+
#ifdef WIN32
68+
static void checkWin32Codepage(void);
69+
#endif
70+
71+
6072

6173
/*----------
6274
* HandleSlashCmds:
@@ -1185,6 +1197,7 @@ do_connect(char *dbname, char *user, char *host, char *port)
11851197
* Replace the old connection with the new one, and update
11861198
* connection-dependent variables.
11871199
*/
1200+
connection_warnings();
11881201
PQsetNoticeProcessor(n_conn, NoticeProcessor, NULL);
11891202
pset.db = n_conn;
11901203
SyncVariables();
@@ -1212,6 +1225,100 @@ do_connect(char *dbname, char *user, char *host, char *port)
12121225
}
12131226

12141227

1228+
void
1229+
connection_warnings(void)
1230+
{
1231+
if (!pset.quiet && !pset.notty)
1232+
{
1233+
int client_ver = parse_version(PG_VERSION);
1234+
1235+
if (pset.sversion != client_ver)
1236+
{
1237+
const char *server_version;
1238+
char server_ver_str[16];
1239+
1240+
/* Try to get full text form, might include "devel" etc */
1241+
server_version = PQparameterStatus(pset.db, "server_version");
1242+
if (!server_version)
1243+
{
1244+
snprintf(server_ver_str, sizeof(server_ver_str),
1245+
"%d.%d.%d",
1246+
pset.sversion / 10000,
1247+
(pset.sversion / 100) % 100,
1248+
pset.sversion % 100);
1249+
server_version = server_ver_str;
1250+
}
1251+
1252+
printf(_("%s (%s, server %s)\n"),
1253+
pset.progname, PG_VERSION, server_version);
1254+
}
1255+
else
1256+
printf("%s (%s)\n", pset.progname, PG_VERSION);
1257+
1258+
if (pset.sversion / 100 != client_ver / 100)
1259+
printf(_("WARNING: %s version %d.%d, server version %d.%d.\n"
1260+
" Some psql features might not work.\n"),
1261+
pset.progname, client_ver / 10000, (client_ver / 100) % 100,
1262+
pset.sversion / 10000, (pset.sversion / 100) % 100);
1263+
1264+
#ifdef WIN32
1265+
checkWin32Codepage();
1266+
#endif
1267+
#ifdef USE_SSL
1268+
printSSLInfo();
1269+
#endif
1270+
}
1271+
}
1272+
1273+
1274+
/*
1275+
* printSSLInfo
1276+
*
1277+
* Prints information about the current SSL connection, if SSL is in use
1278+
*/
1279+
#ifdef USE_SSL
1280+
static void
1281+
printSSLInfo(void)
1282+
{
1283+
int sslbits = -1;
1284+
SSL *ssl;
1285+
1286+
ssl = PQgetssl(pset.db);
1287+
if (!ssl)
1288+
return; /* no SSL */
1289+
1290+
SSL_get_cipher_bits(ssl, &sslbits);
1291+
printf(_("SSL connection (cipher: %s, bits: %i)\n"),
1292+
SSL_get_cipher(ssl), sslbits);
1293+
}
1294+
#endif
1295+
1296+
1297+
/*
1298+
* checkWin32Codepage
1299+
*
1300+
* Prints a warning when win32 console codepage differs from Windows codepage
1301+
*/
1302+
#ifdef WIN32
1303+
static void
1304+
checkWin32Codepage(void)
1305+
{
1306+
unsigned int wincp,
1307+
concp;
1308+
1309+
wincp = GetACP();
1310+
concp = GetConsoleCP();
1311+
if (wincp != concp)
1312+
{
1313+
printf(_("WARNING: Console code page (%u) differs from Windows code page (%u)\n"
1314+
" 8-bit characters might not work correctly. See psql reference\n"
1315+
" page \"Notes for Windows users\" for details.\n"),
1316+
concp, wincp);
1317+
}
1318+
}
1319+
#endif
1320+
1321+
12151322
/*
12161323
* SyncVariables
12171324
*

src/bin/psql/command.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2008, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/command.h,v 1.30 2008/01/01 19:45:55 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/command.h,v 1.31 2008/07/01 00:08:18 momjian Exp $
77
*/
88
#ifndef COMMAND_H
99
#define COMMAND_H
@@ -34,6 +34,8 @@ extern bool do_pset(const char *param,
3434
printQueryOpt *popt,
3535
bool quiet);
3636

37+
extern void connection_warnings(void);
38+
3739
extern void SyncVariables(void);
3840

3941
extern void UnsyncVariables(void);

src/bin/psql/startup.c

Lines changed: 2 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@
33
*
44
* Copyright (c) 2000-2008, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/startup.c,v 1.148 2008/05/16 17:17:00 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/startup.c,v 1.149 2008/07/01 00:08:18 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99

1010
#include <sys/types.h>
11-
#ifdef USE_SSL
12-
#include <openssl/ssl.h>
13-
#endif
1411

1512
#ifndef WIN32
1613
#include <unistd.h>
@@ -78,22 +75,13 @@ struct adhoc_opts
7875
bool single_txn;
7976
};
8077

81-
static int parse_version(const char *versionString);
8278
static void parse_psql_options(int argc, char *argv[],
8379
struct adhoc_opts * options);
8480
static void process_psqlrc(char *argv0);
8581
static void process_psqlrc_file(char *filename);
8682
static void showVersion(void);
8783
static void EstablishVariableSpace(void);
8884

89-
#ifdef USE_SSL
90-
static void printSSLInfo(void);
91-
#endif
92-
93-
#ifdef WIN32
94-
static void checkWin32Codepage(void);
95-
#endif
96-
9785
/*
9886
*
9987
* main
@@ -296,49 +284,9 @@ main(int argc, char *argv[])
296284
if (!options.no_psqlrc)
297285
process_psqlrc(argv[0]);
298286

287+
connection_warnings();
299288
if (!pset.quiet && !pset.notty)
300-
{
301-
int client_ver = parse_version(PG_VERSION);
302-
303-
if (pset.sversion != client_ver)
304-
{
305-
const char *server_version;
306-
char server_ver_str[16];
307-
308-
/* Try to get full text form, might include "devel" etc */
309-
server_version = PQparameterStatus(pset.db, "server_version");
310-
if (!server_version)
311-
{
312-
snprintf(server_ver_str, sizeof(server_ver_str),
313-
"%d.%d.%d",
314-
pset.sversion / 10000,
315-
(pset.sversion / 100) % 100,
316-
pset.sversion % 100);
317-
server_version = server_ver_str;
318-
}
319-
320-
printf(_("%s (%s, server %s)\n"),
321-
pset.progname, PG_VERSION, server_version);
322-
}
323-
else
324-
printf("%s (%s)\n", pset.progname, PG_VERSION);
325-
326-
if (pset.sversion / 100 != client_ver / 100)
327-
printf(_("WARNING: %s version %d.%d, server version %d.%d.\n"
328-
" Some psql features might not work.\n"),
329-
pset.progname, client_ver / 10000, (client_ver / 100) % 100,
330-
pset.sversion / 10000, (pset.sversion / 100) % 100);
331-
332-
#ifdef WIN32
333-
checkWin32Codepage();
334-
#endif
335-
#ifdef USE_SSL
336-
printSSLInfo();
337-
#endif
338-
339289
printf(_("Type \"help\" for help.\n\n"));
340-
}
341-
342290
if (!pset.notty)
343291
initializeInput(options.no_readline ? 0 : 1);
344292
if (options.action_string) /* -f - was used */
@@ -357,29 +305,6 @@ main(int argc, char *argv[])
357305
}
358306

359307

360-
/*
361-
* Convert a version string into a number.
362-
*/
363-
static int
364-
parse_version(const char *versionString)
365-
{
366-
int cnt;
367-
int vmaj,
368-
vmin,
369-
vrev;
370-
371-
cnt = sscanf(versionString, "%d.%d.%d", &vmaj, &vmin, &vrev);
372-
373-
if (cnt < 2)
374-
return -1;
375-
376-
if (cnt == 2)
377-
vrev = 0;
378-
379-
return (100 * vmaj + vmin) * 100 + vrev;
380-
}
381-
382-
383308
/*
384309
* Parse command line options
385310
*/
@@ -683,54 +608,6 @@ showVersion(void)
683608

684609

685610

686-
/*
687-
* printSSLInfo
688-
*
689-
* Prints information about the current SSL connection, if SSL is in use
690-
*/
691-
#ifdef USE_SSL
692-
static void
693-
printSSLInfo(void)
694-
{
695-
int sslbits = -1;
696-
SSL *ssl;
697-
698-
ssl = PQgetssl(pset.db);
699-
if (!ssl)
700-
return; /* no SSL */
701-
702-
SSL_get_cipher_bits(ssl, &sslbits);
703-
printf(_("SSL connection (cipher: %s, bits: %i)\n"),
704-
SSL_get_cipher(ssl), sslbits);
705-
}
706-
#endif
707-
708-
709-
/*
710-
* checkWin32Codepage
711-
*
712-
* Prints a warning when win32 console codepage differs from Windows codepage
713-
*/
714-
#ifdef WIN32
715-
static void
716-
checkWin32Codepage(void)
717-
{
718-
unsigned int wincp,
719-
concp;
720-
721-
wincp = GetACP();
722-
concp = GetConsoleCP();
723-
if (wincp != concp)
724-
{
725-
printf(_("WARNING: Console code page (%u) differs from Windows code page (%u)\n"
726-
" 8-bit characters might not work correctly. See psql reference\n"
727-
" page \"Notes for Windows users\" for details.\n"),
728-
concp, wincp);
729-
}
730-
}
731-
#endif
732-
733-
734611
/*
735612
* Assign hooks for psql variables.
736613
*

0 commit comments

Comments
 (0)