]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-master: Use service name from master for log prefixes and for service:<name>...
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Tue, 14 Apr 2020 16:37:02 +0000 (19:37 +0300)
committertimo.sirainen <timo.sirainen@open-xchange.com>
Fri, 24 Apr 2020 10:42:00 +0000 (10:42 +0000)
This makes it easier to differentiate between different services using the
same executable. Especially auth vs auth-worker events which might
otherwise be the same except for the service:<name> category.

src/lib-master/master-service-private.h
src/lib-master/master-service.c
src/lib-master/master-service.h

index e8229515178052b02db3c1bc60533ad28455a240..d8a2da6bf368380673ccb314581bd49a856c8ad5 100644 (file)
@@ -24,6 +24,7 @@ struct master_service {
        struct ioloop *ioloop;
 
        char *name;
+       char *configured_name;
        char *getopt_str;
        enum master_service_flags flags;
 
index 339c4f5b4f25699b0989ef49bfae690442cf439d..9e072d2d1e8f1ff08e83a110a5c8040a42802f0d 100644 (file)
@@ -202,7 +202,7 @@ master_service_init(const char *name, enum master_service_flags flags,
        struct master_service *service;
        data_stack_frame_t datastack_frame_id = 0;
        unsigned int count;
-       const char *value;
+       const char *service_configured_name, *value;
 
        i_assert(name != NULL);
 
@@ -227,9 +227,22 @@ master_service_init(const char *name, enum master_service_flags flags,
        /* NOTE: we start rooted, so keep the code minimal until
           restrict_access_by_env() is called */
        lib_init();
+       /* Get the service name from environment. This usually differs from the
+          service name parameter if the executable is used for multiple
+          services. For example "auth" vs "auth-worker". It can also be a
+          service with slightly different settings, like "lmtp" vs
+          "lmtp-no-quota". We don't want to use the configured name as the
+          service's primary name, because that could break some lookups (e.g.
+          auth would suddenly see service=lmtp-no-quota. However, this can be
+          very useful in events to differentiate e.g. auth master and
+          auth-worker events which might otherwise look very similar. It's
+          also useful in log prefixes. */
+       service_configured_name = getenv(MASTER_SERVICE_ENV);
+       if (service_configured_name == NULL)
+               service_configured_name = name;
        /* Set a logging prefix temporarily. This will be ignored once the log
           is properly initialized */
-       i_set_failure_prefix("%s(init): ", name);
+       i_set_failure_prefix("%s(init): ", service_configured_name);
 
        /* make sure all the data stack allocations during init will be freed
           before we get to ioloop. the corresponding t_pop() is in
@@ -247,10 +260,17 @@ master_service_init(const char *name, enum master_service_flags flags,
 
        process_title_init(*argc, argv);
 
+       /* process_title_init() might destroy all environments.
+          Need to look this up again. */
+       service_configured_name = getenv(MASTER_SERVICE_ENV);
+       if (service_configured_name == NULL)
+               service_configured_name = name;
+
        service = i_new(struct master_service, 1);
        service->argc = *argc;
        service->argv = *argv;
        service->name = i_strdup(name);
+       service->configured_name = i_strdup(service_configured_name);
        /* keep getopt_str first in case it contains "+" */
        service->getopt_str = *getopt_str == '\0' ?
                i_strdup(master_service_getopt_string()) :
@@ -298,13 +318,15 @@ master_service_init(const char *name, enum master_service_flags flags,
           we want to log */
        if (getenv("LOG_SERVICE") != NULL)
                i_set_failure_internal();
-       if (getenv("USER") != NULL)
-               i_set_failure_prefix("%s(%s): ", name, getenv("USER"));
-       else
-               i_set_failure_prefix("%s: ", name);
+       if (getenv("USER") != NULL) {
+               i_set_failure_prefix("%s(%s): ", service->configured_name,
+                                    getenv("USER"));
+       } else {
+               i_set_failure_prefix("%s: ", service->configured_name);
+       }
 
        master_service_category_name =
-               i_strdup_printf("service:%s", service->name);
+               i_strdup_printf("service:%s", service->configured_name);
        master_service_category.name = master_service_category_name;
        event_register_callback(master_service_event_callback);
 
@@ -804,6 +826,11 @@ const char *master_service_get_name(struct master_service *service)
        return service->name;
 }
 
+const char *master_service_get_configured_name(struct master_service *service)
+{
+       return service->configured_name;
+}
+
 void master_service_run(struct master_service *service,
                        master_service_connection_callback_t *callback)
 {
@@ -1071,6 +1098,7 @@ void master_service_deinit(struct master_service **_service)
                i_free(service->listeners[i].name);
        i_free(service->listeners);
        i_free(service->getopt_str);
+       i_free(service->configured_name);
        i_free(service->name);
        i_free(service->config_path);
        i_free(service);
index 5a81b8b36e1f3d22663e11e5237b670abb4197a8..92adbbf8d4f31f83839089f869396a88eb967a92 100644 (file)
@@ -193,6 +193,10 @@ const char *master_service_get_config_path(struct master_service *service);
 const char *master_service_get_version_string(struct master_service *service);
 /* Returns name of the service, as given in name parameter to _init(). */
 const char *master_service_get_name(struct master_service *service);
+/* Returns name of the service, as given in the configuration file. For example
+   service name=auth, but configured_name=auth-worker. This is preferred in
+   e.g. log prefixes. */
+const char *master_service_get_configured_name(struct master_service *service);
 
 /* Start the service. Blocks until finished */
 void master_service_run(struct master_service *service,