From: Timo Sirainen Date: Tue, 14 Apr 2020 16:37:02 +0000 (+0300) Subject: lib-master: Use service name from master for log prefixes and for service:... X-Git-Tag: 2.3.11.2~188 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9acc0b6e64a561f8981784315f48a1bc97240740;p=thirdparty%2Fdovecot%2Fcore.git lib-master: Use service name from master for log prefixes and for service: category 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: category. --- diff --git a/src/lib-master/master-service-private.h b/src/lib-master/master-service-private.h index e822951517..d8a2da6bf3 100644 --- a/src/lib-master/master-service-private.h +++ b/src/lib-master/master-service-private.h @@ -24,6 +24,7 @@ struct master_service { struct ioloop *ioloop; char *name; + char *configured_name; char *getopt_str; enum master_service_flags flags; diff --git a/src/lib-master/master-service.c b/src/lib-master/master-service.c index 339c4f5b4f..9e072d2d1e 100644 --- a/src/lib-master/master-service.c +++ b/src/lib-master/master-service.c @@ -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); diff --git a/src/lib-master/master-service.h b/src/lib-master/master-service.h index 5a81b8b36e..92adbbf8d4 100644 --- a/src/lib-master/master-service.h +++ b/src/lib-master/master-service.h @@ -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,