pgpool2.git / summary / log / commit / refs

commit    576ecb883a2f9a1e8ddb155c04e6d6da7d20f9bf
Author:   Tatsuo Ishii <ishii@postgresql.org>
Commit:   Muhammad Usama <m.usama@gmail.com>
Date:     Sun Dec 01 02:01:57 2013 +0000

    Add new config parameter "print_user".
    
    This adds current session user names to the log if true.  If there's
    no session, it does not add user names even if print_user is true.
    The default is false.


doc/pgpool-en.html | 11 ++- doc/pgpool-ja.html | 12 ++- src/config/pool_config.c | 13 +++ src/config/pool_config.l | 13 +++ src/include/pool.h | 12 +++ src/include/pool_config.h | 1 + src/tools/pgmd5/Makefile.am | 3 +- src/tools/pgmd5/Makefile.in | 2 +- src/utils/pool_error.c | 185 +++++++++++++++++++++++-------------- src/utils/pool_process_reporting.c | 5 + 10 files changed, 181 insertions(+), 76 deletions(-) diff --git a/doc/pgpool-en.html b/doc/pgpool-en.html index 7745b9e6b..7c4cd4ae5 100644 --- a/doc/pgpool-en.html +++ b/doc/pgpool-en.html @@ -7,7 +7,7 @@ </head> <!-- hhmts start --> -Last modified: Sun Oct 6 17:49:55 JST 2013 +Last modified: Sun Dec 1 10:46:44 JST 2013 <!-- hhmts end --> <body bgcolor="#ffffff"> @@ -733,6 +733,15 @@ local0.* /var/log/pgpool.log </p> </dd> +<dt><a name="PRINT_USER"></a>print_user</dt> + <dd> + <p>Add session user names to the logs when set to true. Default is false. + </p> + <p> + You need to reload pgpool.conf if you change print_user. + </p> + </dd> + <dt><a name="LOG_CONNECTIONS"></a>log_connections</dt> <dd> <p> diff --git a/doc/pgpool-ja.html b/doc/pgpool-ja.html index 6f564c514..2cabc97c2 100644 --- a/doc/pgpool-ja.html +++ b/doc/pgpool-ja.html @@ -8,7 +8,7 @@ <body> <!-- hhmts start --> -Last modified: Tue Aug 27 09:12:47 JST 2013 +Last modified: Sun Dec 1 10:46:32 JST 2013 <!-- hhmts end --> <body bgcolor="#ffffff"> @@ -762,6 +762,16 @@ local0.* /var/log/pgpool.log </p> </dd> +<dt><a name="PRINT_USER"></a>print_user</dt> + <dd> + <p> + trueならばpgpool-IIのログにセッションユーザ名を追加します。デフォルトはfalseです。 + </p> + <p> + このパラメータを変更した時には pgpool-II を再起動してください。 + </p> + </dd> + <dt><a name="LOG_CONNECTIONS"></a>log_connections</dt> <dd> <p> diff --git a/src/config/pool_config.c b/src/config/pool_config.c index f265ab194..210493256 100644 --- a/src/config/pool_config.c +++ b/src/config/pool_config.c @@ -1919,6 +1919,7 @@ int pool_init_config(void) pool_config->black_function_list = default_black_function_list; pool_config->num_black_function_list = sizeof(default_black_function_list)/sizeof(char *); pool_config->print_timestamp = 1; + pool_config->print_user = 0; pool_config->master_slave_mode = 0; pool_config->master_slave_sub_mode = "slony"; pool_config->delay_threshold = 0; @@ -2782,6 +2783,18 @@ int pool_get_config(char *confpath, POOL_CONFIG_CONTEXT context) } pool_config->print_timestamp = v; } + else if (!strcmp(key, "print_user") && CHECK_CONTEXT(INIT_CONFIG, context)) + { + int v = eval_logical(yytext); + + if (v < 0) + { + pool_error("pool_config: invalid value %s for %s", yytext, key); + fclose(fd); + return(-1); + } + pool_config->print_user = v; + } else if (!strcmp(key, "master_slave_mode") && CHECK_CONTEXT(INIT_CONFIG, context)) { diff --git a/src/config/pool_config.l b/src/config/pool_config.l index 2458b248f..00c09368e 100644 --- a/src/config/pool_config.l +++ b/src/config/pool_config.l @@ -194,6 +194,7 @@ int pool_init_config(void) pool_config->black_function_list = default_black_function_list; pool_config->num_black_function_list = sizeof(default_black_function_list)/sizeof(char *); pool_config->print_timestamp = 1; + pool_config->print_user = 0; pool_config->master_slave_mode = 0; pool_config->master_slave_sub_mode = "slony"; pool_config->delay_threshold = 0; @@ -1057,6 +1058,18 @@ int pool_get_config(char *confpath, POOL_CONFIG_CONTEXT context) } pool_config->print_timestamp = v; } + else if (!strcmp(key, "print_user") && CHECK_CONTEXT(INIT_CONFIG, context)) + { + int v = eval_logical(yytext); + + if (v < 0) + { + pool_error("pool_config: invalid value %s for %s", yytext, key); + fclose(fd); + return(-1); + } + pool_config->print_user = v; + } else if (!strcmp(key, "master_slave_mode") && CHECK_CONTEXT(INIT_CONFIG, context)) { diff --git a/src/include/pool.h b/src/include/pool.h index d0135552d..1ad29b49f 100644 --- a/src/include/pool.h +++ b/src/include/pool.h @@ -50,6 +50,18 @@ #define POOLMAXPATHLEN 8192 +/* + * Brought from PostgreSQL's pg_config_manual.h. + * + * Maximum length for identifiers (e.g. table names, column names, + * function names). Names actually are limited to one less byte than this, + * because the length must include a trailing zero byte. + * + * Please note that in version 2 protocol, maximum user name length is + * SM_USER, which is 32. + */ +#define NAMEDATALEN 64 + /* configuration file name */ #define POOL_CONF_FILE_NAME "pgpool.conf" diff --git a/src/include/pool_config.h b/src/include/pool_config.h index 8673c2c76..2e0b505b2 100644 --- a/src/include/pool_config.h +++ b/src/include/pool_config.h @@ -112,6 +112,7 @@ typedef struct { char **white_function_list; /* list of functions with no side effects */ char **black_function_list; /* list of functions with side effects */ int print_timestamp; /* if non 0, print time stamp to each log line */ + int print_user; /* if non 0, print user name to each log line */ int master_slave_mode; /* if non 0, operate in master/slave mode */ char *master_slave_sub_mode; /* either "slony" or "stream" */ unsigned long long int delay_threshold; /* If the standby server delays more than delay_threshold, diff --git a/src/tools/pgmd5/Makefile.am b/src/tools/pgmd5/Makefile.am index c676cfb0e..904cb5c48 100644 --- a/src/tools/pgmd5/Makefile.am +++ b/src/tools/pgmd5/Makefile.am @@ -12,5 +12,4 @@ pg_md5_SOURCES = pg_md5.c \ DEFS = @DEFS@ \ - -DDEFAULT_CONFIGDIR=\"$(sysconfdir)\" - + -DDEFAULT_CONFIGDIR=\"$(sysconfdir)\" -DPOOL_TOOLS diff --git a/src/tools/pgmd5/Makefile.in b/src/tools/pgmd5/Makefile.in index 30170eeef..2b49b9918 100644 --- a/src/tools/pgmd5/Makefile.in +++ b/src/tools/pgmd5/Makefile.in @@ -91,7 +91,7 @@ CPP = @CPP@ CPPFLAGS = @CPPFLAGS@ CYGPATH_W = @CYGPATH_W@ DEFS = @DEFS@ \ - -DDEFAULT_CONFIGDIR=\"$(sysconfdir)\" + -DDEFAULT_CONFIGDIR=\"$(sysconfdir)\" -DPOOL_TOOLS DEPDIR = @DEPDIR@ DLLTOOL = @DLLTOOL@ diff --git a/src/utils/pool_error.c b/src/utils/pool_error.c index d8410ceab..46c807574 100644 --- a/src/utils/pool_error.c +++ b/src/utils/pool_error.c @@ -5,7 +5,7 @@ * pgpool: a language independent connection pool server for PostgreSQL * written by Tatsuo Ishii * - * Copyright (c) 2003-2012 PgPool Global Development Group + * Copyright (c) 2003-2013 PgPool Global Development Group * * Permission to use, copy, modify, and distribute this software and * its documentation for any purpose and without fee is hereby @@ -30,12 +30,18 @@ #include "pool.h" #include "pool_config.h" +#ifndef POOL_TOOLS +#include "context/pool_session_context.h" +#endif #define MAXSTRFTIME 128 extern int debug; +#ifndef POOL_TOOLS static char *nowsec(void); +#endif +static char *optstring(int kind); void pool_error(const char *fmt,...) { @@ -52,33 +58,29 @@ void pool_error(const char *fmt,...) #endif POOL_SETMASK2(&BlockSig, &oldmask); - /* Write error message to syslog */ - if (pool_config->logsyslog == 1) { - va_start(ap, fmt); - vsyslog(pool_config->syslog_facility | LOG_ERR, fmt, ap); - va_end(ap); - POOL_SETMASK(&oldmask); - return; + /* Write message to syslog */ + if (pool_config->logsyslog == 1) + { + va_start(ap, fmt); + vsyslog(pool_config->syslog_facility | LOG_ERR, fmt, ap); + va_end(ap); + POOL_SETMASK(&oldmask); + return; } - if (pool_config->print_timestamp) #ifdef HAVE_ASPRINTF - len = asprintf(&fmt2, "%s ERROR: pid %d: %s\n", nowsec(), (int)getpid(), fmt); - else - len = asprintf(&fmt2, "ERROR: pid %d: %s\n", (int)getpid(), fmt); - - if (len >= 0 && fmt2) - { - va_start(ap, fmt); - vfprintf(stderr, fmt2, ap); - va_end(ap); - fflush(stderr); - free(fmt2); - } + len = asprintf(&fmt2, "%s %s\n", optstring(0), fmt); + + if (len >= 0 && fmt2) + { + va_start(ap, fmt); + vfprintf(stderr, fmt2, ap); + va_end(ap); + fflush(stderr); + free(fmt2); + } #else - fprintf(stderr, "%s ERROR: pid %d: ", nowsec(), (int)getpid()); - else - fprintf(stderr, "ERROR: pid %d: ", (int)getpid()); + fprintf(stderr, "%s %s", optstring(0)); va_start(ap, fmt); vfprintf(stderr, fmt, ap); @@ -116,33 +118,29 @@ void pool_debug(const char *fmt,...) POOL_SETMASK2(&BlockSig, &oldmask); - /* Write debug message to syslog */ - if (pool_config->logsyslog == 1) { - va_start(ap, fmt); - vsyslog(pool_config->syslog_facility | LOG_DEBUG, fmt, ap); - va_end(ap); - POOL_SETMASK(&oldmask); - return; + /* Write message to syslog */ + if (pool_config->logsyslog == 1) + { + va_start(ap, fmt); + vsyslog(pool_config->syslog_facility | LOG_DEBUG, fmt, ap); + va_end(ap); + POOL_SETMASK(&oldmask); + return; } - if (pool_config->print_timestamp) #ifdef HAVE_ASPRINTF - len = asprintf(&fmt2, "%s DEBUG: pid %d: %s\n", nowsec(), (int)getpid(), fmt); - else - len = asprintf(&fmt2, "DEBUG: pid %d: %s\n", (int)getpid(), fmt); - - if (len >= 0 && fmt2) - { - va_start(ap, fmt); - vfprintf(stderr, fmt2, ap); - va_end(ap); - fflush(stderr); - free(fmt2); - } + len = asprintf(&fmt2, "%s %s\n", optstring(1), fmt); + + if (len >= 0 && fmt2) + { + va_start(ap, fmt); + vfprintf(stderr, fmt2, ap); + va_end(ap); + fflush(stderr); + free(fmt2); + } #else - fprintf(stderr, "%s DEBUG: pid %d: ", nowsec(), (int)getpid()); - else - fprintf(stderr, "DEBUG: pid %d: ", (int)getpid()); + fprintf(stderr, "%s %s", optstring(1)); va_start(ap, fmt); vfprintf(stderr, fmt, ap); @@ -166,36 +164,31 @@ void pool_log(const char *fmt,...) #else int oldmask; #endif - POOL_SETMASK2(&BlockSig, &oldmask); - /* Write log message to syslog */ - if (pool_config->logsyslog == 1) { - va_start(ap, fmt); - vsyslog(pool_config->syslog_facility | LOG_NOTICE, fmt, ap); - va_end(ap); - POOL_SETMASK(&oldmask); - return; + /* Write message to syslog */ + if (pool_config->logsyslog == 1) + { + va_start(ap, fmt); + vsyslog(pool_config->syslog_facility | LOG_NOTICE, fmt, ap); + va_end(ap); + POOL_SETMASK(&oldmask); + return; } - if (pool_config->print_timestamp) #ifdef HAVE_ASPRINTF - len = asprintf(&fmt2, "%s LOG: pid %d: %s\n", nowsec(), (int)getpid(), fmt); - else - len = asprintf(&fmt2, "LOG: pid %d: %s\n", (int)getpid(), fmt); - - if (len >= 0 && fmt2) - { - va_start(ap, fmt); - vfprintf(stderr, fmt2, ap); - va_end(ap); - fflush(stderr); - free(fmt2); - } + len = asprintf(&fmt2, "%s %s\n", optstring(2), fmt); + + if (len >= 0 && fmt2) + { + va_start(ap, fmt); + vfprintf(stderr, fmt2, ap); + va_end(ap); + fflush(stderr); + free(fmt2); + } #else - fprintf(stderr, "%s LOG: pid %d: ", nowsec(), (int)getpid()); - else - fprintf(stderr, "LOG: pid %d: ", (int)getpid()); + fprintf(stderr, "%s %s", optstring(2)); va_start(ap, fmt); vfprintf(stderr, fmt, ap); @@ -206,6 +199,7 @@ void pool_log(const char *fmt,...) POOL_SETMASK(&oldmask); } +#ifndef POOL_TOOLS static char *nowsec(void) { static char strbuf[MAXSTRFTIME]; @@ -214,6 +208,7 @@ static char *nowsec(void) strftime(strbuf, MAXSTRFTIME, "%Y-%m-%d %H:%M:%S", localtime(&now)); return strbuf; } +#endif #ifndef HAVE_VSYSLOG void vsyslog (int priority, const char *format, va_list ap) @@ -240,3 +235,51 @@ void vsyslog (int priority, const char *format, va_list ap) free(msg); } #endif /* HAVE_VSYSLOG */ + +/* + * Create "ERROR" etc., timestamp, pid, user name string and return + * it. The returned string is in a static buff and subsequent calls + * will overwrite it. + */ +static char *optstring(int kind) +{ + static char *kindstr[] = {"ERROR:", "DEBUG:", "LOG: "}; + char timebuf[MAXSTRFTIME]; + time_t now; + static char optbuf[MAXSTRFTIME+7+8+NAMEDATALEN]; +#ifndef POOL_TOOLS + char username[NAMEDATALEN]; +#endif + char buf[128]; +#ifndef POOL_TOOLS + POOL_SESSION_CONTEXT *c; +#endif + + optbuf[0] = '\0'; + + if (pool_config->print_timestamp) + { + now = time(NULL); + strftime(timebuf, MAXSTRFTIME, "%Y-%m-%d %H:%M:%S ", localtime(&now)); + strcat(optbuf, timebuf); + } + + snprintf(buf, sizeof(buf), "%s pid: %d", kindstr[kind], (int)getpid()); + strcat(optbuf, buf); + +#ifndef POOL_TOOLS + if (pool_config->print_user) + { + if ((c = pool_get_session_context())) + if (MASTER_CONNECTION(c->backend) && MASTER_CONNECTION(c->backend)->sp && + MASTER_CONNECTION(c->backend)->sp->user) + { + strlcpy(username, MASTER_CONNECTION(c->backend)->sp->user, sizeof(username)); + strcat(optbuf, " user: "); + strcat(optbuf, username); + } + } +#endif + + return optbuf; +} diff --git a/src/utils/pool_process_reporting.c b/src/utils/pool_process_reporting.c index da8f19a1b..0d49f876c 100644 --- a/src/utils/pool_process_reporting.c +++ b/src/utils/pool_process_reporting.c @@ -272,6 +272,11 @@ POOL_REPORT_CONFIG* get_config(int *nrows) strncpy(status[i].desc, "if true print time stamp to each log line", POOLCONFIG_MAXDESCLEN); i++; + strncpy(status[i].name, "print_user", POOLCONFIG_MAXNAMELEN); + snprintf(status[i].value, POOLCONFIG_MAXVALLEN, "%d", pool_config->print_user); + strncpy(status[i].desc, "if true print user name to each log line", POOLCONFIG_MAXDESCLEN); + i++; + strncpy(status[i].name, "log_connections", POOLCONFIG_MAXNAMELEN); snprintf(status[i].value, POOLCONFIG_MAXVALLEN, "%d", pool_config->log_connections); strncpy(status[i].desc, "if true, print incoming connections to the log", POOLCONFIG_MAXDESCLEN); [parent: 5584a2470bf6]