From: Marko Kreen Date: Sat, 17 May 2014 15:53:04 +0000 (+0300) Subject: Make application_name_add_host work also with unix sockets. X-Git-Tag: pgbouncer_1_6_rc1~12 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5d2b23bb46285ca7099a6605d5e47af49cb20148;p=pgbouncer Make application_name_add_host work also with unix sockets. --- diff --git a/include/bouncer.h b/include/bouncer.h index 14e6087..75c06e4 100644 --- a/include/bouncer.h +++ b/include/bouncer.h @@ -168,6 +168,7 @@ void pga_copy(PgAddr *a, const struct sockaddr *sa); bool pga_pton(PgAddr *a, const char *s, int port); const char *pga_ntop(const PgAddr *a, char *dst, int dstlen); const char *pga_str(const PgAddr *a, char *dst, int dstlen); +const char *pga_details(const PgAddr *a, char *dst, int dstlen); int pga_cmp_addr(const PgAddr *a, const PgAddr *b); /* diff --git a/src/client.c b/src/client.c index 0fa8ad5..275fd1a 100644 --- a/src/client.c +++ b/src/client.c @@ -323,6 +323,27 @@ bool handle_auth_response(PgSocket *client, PktHdr *pkt) { return true; } +static void set_appname(PgSocket *client, const char *app_name) +{ + char buf[400], abuf[300]; + const char *details; + + if (cf_application_name_add_host) { + /* give app a name */ + if (!app_name) + app_name = "app"; + + /* add details */ + details = pga_details(&client->remote_addr, abuf, sizeof(abuf)); + snprintf(buf, sizeof(buf), "%s - %s", app_name, details); + app_name = buf; + } + if (app_name) { + slog_debug(client, "using application_name: %s", app_name); + varcache_set(&client->vars, "application_name", app_name); + } +} + static bool decide_startup_pool(PgSocket *client, PktHdr *pkt) { const char *username = NULL, *dbname = NULL; @@ -344,16 +365,8 @@ static bool decide_startup_pool(PgSocket *client, PktHdr *pkt) } else if (strcmp(key, "user") == 0) { slog_debug(client, "got var: %s=%s", key, val); username = val; - } else if (cf_application_name_add_host && - strcmp(key, "application_name") == 0) { - int port = pga_port(&client->remote_addr); - static char ipbuf[PGADDR_BUF], buf[1024]; - const char *ip; - - ip = pga_ntop(&client->remote_addr, ipbuf, sizeof(ipbuf)); - snprintf(buf, sizeof(buf), "%s (%s:%d)", val, ip, port); - slog_debug(client,"using application name %s",buf); - varcache_set(&client->vars, key, buf); + } else if (strcmp(key, "application_name") == 0) { + set_appname(client, val); appname_found = true; } else if (varcache_set(&client->vars, key, val)) { slog_debug(client, "got var: %s=%s", key, val); @@ -374,17 +387,9 @@ static bool decide_startup_pool(PgSocket *client, PktHdr *pkt) if (!dbname || !dbname[0]) dbname = username; - /* default application_name to "client at :" */ - if (!appname_found && cf_application_name_add_host) { - int port = pga_port(&client->remote_addr); - static char ipbuf[PGADDR_BUF], buf[1024]; - const char *ip; - - ip = pga_ntop(&client->remote_addr, ipbuf, sizeof(ipbuf)); - snprintf(buf, sizeof(buf), "client at %s:%d", ip, port); - slog_debug(client,"using default application name %s",buf); - varcache_set(&client->vars, key, buf); - } + /* create application_name if requested */ + if (!appname_found) + set_appname(client, NULL); /* check if limit allows, don't limit admin db nb: new incoming conn will be attached to PgSocket, thus diff --git a/src/util.c b/src/util.c index 1c761c3..099c9c9 100644 --- a/src/util.c +++ b/src/util.c @@ -406,7 +406,38 @@ const char *pga_str(const PgAddr *a, char *dst, int dstlen) pga_ntop(a, buf, sizeof(buf)); if (pga_family(a) == AF_INET6) snprintf(dst, dstlen, "[%s]:%d", buf, pga_port(a)); + else if (pga_family(a) == AF_UNIX && a->scred.pid) + snprintf(dst, dstlen, "%s:%d$%u", buf, pga_port(a), a->scred.pid); else snprintf(dst, dstlen, "%s:%d", buf, pga_port(a)); return dst; } + +static const char *cached_hostname(void) +{ + static char cache[HOST_NAME_MAX + 1]; + int err; + + if (cache[0] == 0) { + err = gethostname(cache, sizeof(cache)); + if (err != 0) + strlcpy(cache, "somehost", sizeof(cache)); + } + return cache; +} + +const char *pga_details(const PgAddr *a, char *dst, int dstlen) +{ + char buf[PGADDR_BUF]; + pga_ntop(a, buf, sizeof(buf)); + if (pga_family(a) == AF_INET6) + snprintf(dst, dstlen, "[%s]:%d", buf, pga_port(a)); + else if (pga_family(a) == AF_UNIX && a->scred.pid) + snprintf(dst, dstlen, "%s(%u@%s):%d", buf, a->scred.pid, cached_hostname(), pga_port(a)); + else + snprintf(dst, dstlen, "%s:%d", buf, pga_port(a)); + return dst; +} + + +