Make application_name_add_host work also with unix sockets.
authorMarko Kreen <markokr@gmail.com>
Sat, 17 May 2014 15:53:04 +0000 (18:53 +0300)
committerMarko Kreen <markokr@gmail.com>
Sat, 17 May 2014 15:53:04 +0000 (18:53 +0300)
include/bouncer.h
src/client.c
src/util.c

index 14e6087e89606bd3d3732b1ed1884b3337aee2f7..75c06e47d6ab5f568347c4a169367bed1d1a6882 100644 (file)
@@ -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);
 
 /*
index 0fa8ad501f10ca11987912c0275d078ea251b693..275fd1aaa675fa5694c8452bd40610a1a4918c9d 100644 (file)
@@ -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 <addr>:<port>" */
-       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
index 1c761c31493973cf694681f825b826dc5ca6eee6..099c9c96c603c0c1f9570ab9b57807974aea3819 100644 (file)
@@ -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;
+}
+
+
+