master-service.c revision 3eeaad7297f63f643ebb3fb2a0f7a03b20ffc120
2454dfa32c93c20a8522c6ed42fe057baaac9f9aStephan Bosch/* Copyright (c) 2005-2010 Dovecot authors, see the included COPYING file */
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#include "lib.h"
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#include "lib-signals.h"
dfa2201c6ac8ddb2d2798dee15662cfe774e644eMartti Rannanjärvi#include "ioloop.h"
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#include "array.h"
472369cba85d9f7c995dda60e7cd01d78b4a960aTimo Sirainen#include "env-util.h"
e28fa207d1a097fa6e4a867f74ee0761472ef1ceTimo Sirainen#include "home-expand.h"
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainen#include "process-title.h"
bd4e36a8cd7257cca7d1434c49a1e343ed7c5100Timo Sirainen#include "restrict-access.h"
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen#include "fd-close-on-exec.h"
b1f37113a5760bee842c5a7678bb5fa6f5bd8b60Timo Sirainen#include "settings-parser.h"
1c1cecd3dfaf71b0c9499b044023e631841e88aaTimo Sirainen#include "syslog-util.h"
94d8e51119003d2bc5a100c663f90141f297385dTimo Sirainen#include "master-login.h"
9393445a6dabd17ce62ebfc12fd73545b0065824Timo Sirainen#include "master-service-private.h"
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainen#include "master-service-settings.h"
e28fa207d1a097fa6e4a867f74ee0761472ef1ceTimo Sirainen
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainen#include <stdlib.h>
ef50336eefcb9ba99f73c6af37420eaf8857a39bTimo Sirainen#include <unistd.h>
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen#include <sys/stat.h>
13d98ffa534f2e7d04a832c9d0153fc9c568b878Timo Sirainen#include <syslog.h>
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#define DEFAULT_CONFIG_FILE_PATH SYSCONFDIR"/dovecot.conf"
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen/* getenv(MASTER_CONFIG_FILE_ENV) provides path to configuration file/socket */
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen#define MASTER_CONFIG_FILE_ENV "CONFIG_FILE"
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen/* getenv(MASTER_DOVECOT_VERSION_ENV) provides master's version number */
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#define MASTER_DOVECOT_VERSION_ENV "DOVECOT_VERSION"
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainen
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainen/* when we're full of connections, how often to check if login state has
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainen changed. we normally notice it immediately because of a signal, so this is
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainen just a fallback against race conditions. */
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainen#define MASTER_SERVICE_STATE_CHECK_MSECS 1000
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainen
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainen/* If die callback hasn't managed to stop the service for this many seconds,
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainen force it. */
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen#define MASTER_SERVICE_DIE_TIMEOUT_MSECS (30*1000)
220e21750948941dc6e33b8f11b552fa21d7f81eTimo Sirainen
4ee00532a265bdfb38539d811fcd12d51210ac35Timo Sirainenstruct master_service *master_service;
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainen
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainenstatic void master_service_io_listeners_close(struct master_service *service);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenstatic void master_service_refresh_login_state(struct master_service *service);
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainenconst char *master_service_getopt_string(void)
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen{
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen return "c:ko:Os:L";
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen}
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainenstatic void sig_die(const siginfo_t *si, void *context)
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen{
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen struct master_service *service = context;
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen /* SIGINT comes either from master process or from keyboard. we don't
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen want to log it in either case.*/
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen if (si->si_signo != SIGINT) {
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen i_warning("Killed with signal %d (by pid=%s uid=%s code=%s)",
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen si->si_signo, dec2str(si->si_pid),
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen dec2str(si->si_uid),
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen lib_signal_code_to_str(si->si_signo, si->si_code));
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen } else if ((service->flags & MASTER_SERVICE_FLAG_NO_IDLE_DIE) != 0) {
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen /* never die when idling */
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen return;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen } else if ((service->flags & MASTER_SERVICE_FLAG_STANDALONE) == 0) {
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen /* SIGINT came from master. die only if we're not handling
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen any clients currently. */
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen if (service->master_status.available_count !=
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen service->total_available_count)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen return;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen }
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen service->killed = TRUE;
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen io_loop_stop(service->ioloop);
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen}
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenstatic void
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainensig_state_changed(const siginfo_t *si ATTR_UNUSED, void *context)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen{
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen struct master_service *service = context;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen master_service_refresh_login_state(service);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen}
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenstatic void master_service_verify_version_string(struct master_service *service)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen{
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen if (service->version_string != NULL &&
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen strcmp(service->version_string, PACKAGE_VERSION) != 0) {
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen i_fatal("Dovecot version mismatch: "
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen "Master is v%s, %s is v"PACKAGE_VERSION" "
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen "(if you don't care, set version_ignore=yes)",
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen service->version_string, service->name);
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen }
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen}
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainenstruct master_service *
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainenmaster_service_init(const char *name, enum master_service_flags flags,
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen int *argc, char **argv[], const char *getopt_str)
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen{
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen struct master_service *service;
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen const char *str;
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen i_assert(name != NULL);
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen#ifdef DEBUG
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen if (getenv("GDB") == NULL &&
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen (flags & MASTER_SERVICE_FLAG_STANDALONE) == 0) {
db8b0a3f74a20528d66a3c4be7df920e5c4554c2Timo Sirainen int count;
db8b0a3f74a20528d66a3c4be7df920e5c4554c2Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen str = getenv("SOCKET_COUNT");
ac2defed599a97c4a71a9e90ba185929dfe59226Josef 'Jeff' Sipek count = str == NULL ? 0 : atoi(str);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen fd_debug_verify_leaks(MASTER_LISTEN_FD_FIRST + count, 1024);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen }
db8b0a3f74a20528d66a3c4be7df920e5c4554c2Timo Sirainen#endif
db8b0a3f74a20528d66a3c4be7df920e5c4554c2Timo Sirainen if ((flags & MASTER_SERVICE_FLAG_STANDALONE) == 0) {
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen /* make sure we can dump core, at least until
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen privileges are dropped. (i'm not really sure why this
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen is needed, because doing the same just before exec
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen doesn't help, and exec shouldn't affect this with
ac2defed599a97c4a71a9e90ba185929dfe59226Josef 'Jeff' Sipek non-setuid/gid binaries..) */
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen restrict_access_allow_coredumps(TRUE);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen }
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen /* NOTE: we start rooted, so keep the code minimal until
ac2defed599a97c4a71a9e90ba185929dfe59226Josef 'Jeff' Sipek restrict_access_by_env() is called */
63e207529879438e9f4412d97cdc34bdc82a3702Timo Sirainen lib_init();
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen /* Set a logging prefix temporarily. This will be ignored once the log
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen is properly initialized */
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen i_set_failure_prefix(t_strdup_printf("%s(init): ", name));
ac2defed599a97c4a71a9e90ba185929dfe59226Josef 'Jeff' Sipek
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen /* ignore these signals as early as possible */
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen lib_signals_ignore(SIGPIPE, TRUE);
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen lib_signals_ignore(SIGALRM, FALSE);
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen if (getenv(MASTER_UID_ENV) == NULL)
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen flags |= MASTER_SERVICE_FLAG_STANDALONE;
ac2defed599a97c4a71a9e90ba185929dfe59226Josef 'Jeff' Sipek
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen process_title_init(argv);
ac2defed599a97c4a71a9e90ba185929dfe59226Josef 'Jeff' Sipek
1701e3f91107051b1704721bf1dc1e32491faaf9Timo Sirainen service = i_new(struct master_service, 1);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen service->argc = *argc;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen service->argv = *argv;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen service->name = i_strdup(name);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen /* keep getopt_str first in case it contains "+" */
76b91bac787101e6b0075122ab6478dd98c8a884Timo Sirainen service->getopt_str = getopt_str == NULL ?
76b91bac787101e6b0075122ab6478dd98c8a884Timo Sirainen i_strdup(master_service_getopt_string()) :
76b91bac787101e6b0075122ab6478dd98c8a884Timo Sirainen i_strconcat(getopt_str, master_service_getopt_string(), NULL);
3fe67ec75ccae1230bb9eb9f16affc48377f6441Timo Sirainen service->flags = flags;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen service->ioloop = io_loop_create();
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen service->service_count_left = (unsigned int)-1;
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainen service->config_fd = -1;
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainen
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainen service->config_path = getenv(MASTER_CONFIG_FILE_ENV);
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainen if (service->config_path == NULL) {
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainen service->config_path = DEFAULT_CONFIG_FILE_PATH;
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen service->config_path_is_default = TRUE;
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen }
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainen if ((flags & MASTER_SERVICE_FLAG_STANDALONE) == 0) {
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainen service->version_string = getenv(MASTER_DOVECOT_VERSION_ENV);
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainen service->socket_count = 1;
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainen } else {
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen service->version_string = PACKAGE_VERSION;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen }
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen str = getenv("SOCKET_COUNT");
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen if (str != NULL)
fa780a18c41881036af582f7a3473d6399e9d34dTimo Sirainen service->socket_count = atoi(str);
fa780a18c41881036af582f7a3473d6399e9d34dTimo Sirainen str = getenv("SSL_SOCKET_COUNT");
5214b67a7dabab87da74e04bb8b227f94b95bce4Timo Sirainen if (str != NULL)
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen service->ssl_socket_count = atoi(str);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen /* set up some kind of logging until we know exactly how and where
27586e4785d56aeb76e1fd96af8db799688dc64aTimo Sirainen we want to log */
360123b1b41b7aa8af6c4a91c39046be646cd349Timo Sirainen if (getenv("LOG_SERVICE") != NULL)
12b4dbf933ee54f7b96968ba150095baa985fdafTimo Sirainen i_set_failure_internal();
12b4dbf933ee54f7b96968ba150095baa985fdafTimo Sirainen if (getenv("USER") != NULL) {
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen i_set_failure_prefix(t_strdup_printf("%s(%s): ",
5214b67a7dabab87da74e04bb8b227f94b95bce4Timo Sirainen name, getenv("USER")));
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen } else {
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen i_set_failure_prefix(t_strdup_printf("%s: ", name));
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen }
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen master_service_verify_version_string(service);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen return service;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen}
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
f0d09be40bd0c4423873128ae2f88a4020075dc4Timo Sirainenint master_getopt(struct master_service *service)
f0d09be40bd0c4423873128ae2f88a4020075dc4Timo Sirainen{
1c1cecd3dfaf71b0c9499b044023e631841e88aaTimo Sirainen int c;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
917498e6f84969d2b93410c1e479735abe8e0ed7Timo Sirainen while ((c = getopt(service->argc, service->argv,
917498e6f84969d2b93410c1e479735abe8e0ed7Timo Sirainen service->getopt_str)) > 0) {
917498e6f84969d2b93410c1e479735abe8e0ed7Timo Sirainen if (!master_service_parse_option(service, c, optarg))
917498e6f84969d2b93410c1e479735abe8e0ed7Timo Sirainen break;
917498e6f84969d2b93410c1e479735abe8e0ed7Timo Sirainen }
917498e6f84969d2b93410c1e479735abe8e0ed7Timo Sirainen return c;
917498e6f84969d2b93410c1e479735abe8e0ed7Timo Sirainen}
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainenvoid master_service_init_log(struct master_service *service,
e5acc283bf030b0b5c79ca4e52d315c516a299faPascal Volk const char *prefix)
e5acc283bf030b0b5c79ca4e52d315c516a299faPascal Volk{
e5acc283bf030b0b5c79ca4e52d315c516a299faPascal Volk const char *path;
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen
e5acc283bf030b0b5c79ca4e52d315c516a299faPascal Volk if ((service->flags & MASTER_SERVICE_FLAG_STANDALONE) != 0 &&
e5acc283bf030b0b5c79ca4e52d315c516a299faPascal Volk (service->flags & MASTER_SERVICE_FLAG_DONT_LOG_TO_STDERR) == 0) {
e5acc283bf030b0b5c79ca4e52d315c516a299faPascal Volk i_set_failure_file("/dev/stderr", "");
7920a47321690c932ffd4d286cd16b4048d22d41Timo Sirainen return;
7920a47321690c932ffd4d286cd16b4048d22d41Timo Sirainen }
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
db8b0a3f74a20528d66a3c4be7df920e5c4554c2Timo Sirainen if (getenv("LOG_SERVICE") != NULL && !service->log_directly) {
db8b0a3f74a20528d66a3c4be7df920e5c4554c2Timo Sirainen /* logging via log service */
db8b0a3f74a20528d66a3c4be7df920e5c4554c2Timo Sirainen i_set_failure_internal();
db8b0a3f74a20528d66a3c4be7df920e5c4554c2Timo Sirainen i_set_failure_prefix(prefix);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen return;
5666a3d6a7ea89362b8d9e8b39b15424cd9d6388Timo Sirainen }
1701e3f91107051b1704721bf1dc1e32491faaf9Timo Sirainen
1701e3f91107051b1704721bf1dc1e32491faaf9Timo Sirainen if (service->set == NULL) {
1701e3f91107051b1704721bf1dc1e32491faaf9Timo Sirainen i_set_failure_file("/dev/stderr", prefix);
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen return;
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen }
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen if (strcmp(service->set->log_path, "syslog") != 0) {
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen /* error logging goes to file or stderr */
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen path = home_expand(service->set->log_path);
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen i_set_failure_file(path, prefix);
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen }
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen if (strcmp(service->set->log_path, "syslog") == 0 ||
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen strcmp(service->set->info_log_path, "syslog") == 0 ||
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen strcmp(service->set->debug_log_path, "syslog") == 0) {
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen /* something gets logged to syslog */
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen int facility;
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen if (!syslog_facility_find(service->set->syslog_facility,
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen &facility))
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen facility = LOG_MAIL;
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen i_set_failure_syslog("dovecot", LOG_NDELAY, facility);
b365bd121cdc87f63e1dd47c5085a27091118e00Timo Sirainen i_set_failure_prefix(prefix);
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen if (strcmp(service->set->log_path, "syslog") != 0) {
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen /* set error handlers back to file */
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen i_set_fatal_handler(NULL);
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen i_set_error_handler(NULL);
02e61e13a8360a9d3ec92c5fa5ae60c0f0181b71Timo Sirainen }
02e61e13a8360a9d3ec92c5fa5ae60c0f0181b71Timo Sirainen }
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen if (*service->set->info_log_path != '\0' &&
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen strcmp(service->set->info_log_path, "syslog") != 0) {
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen path = home_expand(service->set->info_log_path);
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen if (*path != '\0')
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen i_set_info_file(path);
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen }
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen if (*service->set->debug_log_path != '\0' &&
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen strcmp(service->set->debug_log_path, "syslog") != 0) {
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen path = home_expand(service->set->debug_log_path);
5c597df6aa8d81de4053c6986fab7739f3b44b20Timo Sirainen if (*path != '\0')
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen i_set_debug_file(path);
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen }
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen i_set_failure_timestamp_format(service->set->log_timestamp);
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen}
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainenvoid master_service_set_die_with_master(struct master_service *service,
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen bool set)
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen{
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen service->die_with_master = set;
33525312d3f45995686aa0b538dea1cd6eb936e2Timo Sirainen}
33525312d3f45995686aa0b538dea1cd6eb936e2Timo Sirainen
efe78d3ba24fc866af1c79b9223dc0809ba26cadStephan Boschvoid master_service_set_die_callback(struct master_service *service,
33525312d3f45995686aa0b538dea1cd6eb936e2Timo Sirainen void (*callback)(void))
33525312d3f45995686aa0b538dea1cd6eb936e2Timo Sirainen{
fa780a18c41881036af582f7a3473d6399e9d34dTimo Sirainen service->die_callback = callback;
33525312d3f45995686aa0b538dea1cd6eb936e2Timo Sirainen}
33525312d3f45995686aa0b538dea1cd6eb936e2Timo Sirainen
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainenbool master_service_parse_option(struct master_service *service,
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen int opt, const char *arg)
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen{
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen switch (opt) {
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen case 'c':
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen service->config_path = arg;
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen service->config_path_is_default = FALSE;
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen break;
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen case 'k':
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen service->keep_environment = TRUE;
33525312d3f45995686aa0b538dea1cd6eb936e2Timo Sirainen break;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen case 'o':
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen if (!array_is_created(&service->config_overrides))
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen i_array_init(&service->config_overrides, 16);
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen array_append(&service->config_overrides, &arg, 1);
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen break;
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen case 'O':
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen service->flags |= MASTER_SERVICE_FLAG_NO_CONFIG_SETTINGS;
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen break;
02e61e13a8360a9d3ec92c5fa5ae60c0f0181b71Timo Sirainen case 'L':
02e61e13a8360a9d3ec92c5fa5ae60c0f0181b71Timo Sirainen service->log_directly = TRUE;
c58c12049c883b281c088d47a2a7278c21c390e1Timo Sirainen break;
c58c12049c883b281c088d47a2a7278c21c390e1Timo Sirainen default:
c58c12049c883b281c088d47a2a7278c21c390e1Timo Sirainen return FALSE;
c58c12049c883b281c088d47a2a7278c21c390e1Timo Sirainen }
c58c12049c883b281c088d47a2a7278c21c390e1Timo Sirainen return TRUE;
c58c12049c883b281c088d47a2a7278c21c390e1Timo Sirainen}
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainenstatic void master_service_error(struct master_service *service)
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen{
1c1cecd3dfaf71b0c9499b044023e631841e88aaTimo Sirainen master_service_stop_new_connections(service);
1c1cecd3dfaf71b0c9499b044023e631841e88aaTimo Sirainen if (service->master_status.available_count ==
1c1cecd3dfaf71b0c9499b044023e631841e88aaTimo Sirainen service->total_available_count || service->die_with_master) {
1c1cecd3dfaf71b0c9499b044023e631841e88aaTimo Sirainen if (service->die_callback == NULL)
1c1cecd3dfaf71b0c9499b044023e631841e88aaTimo Sirainen master_service_stop(service);
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen else {
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen service->to_die =
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen timeout_add(MASTER_SERVICE_DIE_TIMEOUT_MSECS,
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen master_service_stop,
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen service);
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen service->die_callback();
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen }
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen }
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen}
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen
b365bd121cdc87f63e1dd47c5085a27091118e00Timo Sirainenstatic void master_status_error(void *context)
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen{
b365bd121cdc87f63e1dd47c5085a27091118e00Timo Sirainen struct master_service *service = context;
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen /* status fd is a write-only pipe, so if we're here it means the
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen master wants us to die (or died itself). don't die until all
b365bd121cdc87f63e1dd47c5085a27091118e00Timo Sirainen service connections are finished. */
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen io_remove(&service->io_status_error);
b365bd121cdc87f63e1dd47c5085a27091118e00Timo Sirainen
360123b1b41b7aa8af6c4a91c39046be646cd349Timo Sirainen /* the log fd may also be closed already, don't die when trying to
360123b1b41b7aa8af6c4a91c39046be646cd349Timo Sirainen log later */
360123b1b41b7aa8af6c4a91c39046be646cd349Timo Sirainen i_set_failure_ignore_errors(TRUE);
360123b1b41b7aa8af6c4a91c39046be646cd349Timo Sirainen
b365bd121cdc87f63e1dd47c5085a27091118e00Timo Sirainen master_service_error(service);
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen}
b365bd121cdc87f63e1dd47c5085a27091118e00Timo Sirainen
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainenvoid master_service_init_finish(struct master_service *service)
b365bd121cdc87f63e1dd47c5085a27091118e00Timo Sirainen{
5494a6bc149da8f02fd25c0434a9d612ac33f659Timo Sirainen struct stat st;
b365bd121cdc87f63e1dd47c5085a27091118e00Timo Sirainen const char *value;
fa780a18c41881036af582f7a3473d6399e9d34dTimo Sirainen unsigned int count;
fa780a18c41881036af582f7a3473d6399e9d34dTimo Sirainen
12b4dbf933ee54f7b96968ba150095baa985fdafTimo Sirainen i_assert(service->total_available_count == 0);
12b4dbf933ee54f7b96968ba150095baa985fdafTimo Sirainen i_assert(service->service_count_left == (unsigned int)-1);
12b4dbf933ee54f7b96968ba150095baa985fdafTimo Sirainen
12b4dbf933ee54f7b96968ba150095baa985fdafTimo Sirainen /* set default signal handlers */
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen lib_signals_init();
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen lib_signals_set_handler(SIGINT, TRUE, sig_die, service);
02e61e13a8360a9d3ec92c5fa5ae60c0f0181b71Timo Sirainen lib_signals_set_handler(SIGTERM, TRUE, sig_die, service);
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen if ((service->flags & MASTER_SERVICE_FLAG_TRACK_LOGIN_STATE) != 0) {
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen lib_signals_set_handler(SIGUSR1, TRUE,
02e61e13a8360a9d3ec92c5fa5ae60c0f0181b71Timo Sirainen sig_state_changed, service);
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen }
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen if ((service->flags & MASTER_SERVICE_FLAG_STANDALONE) == 0) {
b365bd121cdc87f63e1dd47c5085a27091118e00Timo Sirainen if (fstat(MASTER_STATUS_FD, &st) < 0 || !S_ISFIFO(st.st_mode))
b365bd121cdc87f63e1dd47c5085a27091118e00Timo Sirainen i_fatal("Must be started by dovecot master process");
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen /* initialize master_status structure */
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen value = getenv(MASTER_UID_ENV);
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen if (value == NULL ||
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen str_to_uint(value, &service->master_status.uid) < 0)
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen i_fatal(MASTER_UID_ENV" missing");
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen service->master_status.pid = getpid();
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen /* set the default limit */
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen value = getenv(MASTER_CLIENT_LIMIT_ENV);
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen if (value == NULL || str_to_uint(value, &count) < 0 ||
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen count == 0)
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen i_fatal(MASTER_CLIENT_LIMIT_ENV" missing");
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen master_service_set_client_limit(service, count);
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen /* set the default service count */
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen value = getenv(MASTER_SERVICE_COUNT_ENV);
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen if (value != NULL && str_to_uint(value, &count) == 0 &&
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen count > 0)
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen master_service_set_service_count(service, count);
94d8e51119003d2bc5a100c663f90141f297385dTimo Sirainen
1433bf361ddb0bba8878c8ada5726d0284edad57Timo Sirainen /* start listening errors for status fd, it means master died */
1433bf361ddb0bba8878c8ada5726d0284edad57Timo Sirainen service->io_status_error = io_add(MASTER_STATUS_FD, IO_ERROR,
1433bf361ddb0bba8878c8ada5726d0284edad57Timo Sirainen master_status_error, service);
1433bf361ddb0bba8878c8ada5726d0284edad57Timo Sirainen } else {
94d8e51119003d2bc5a100c663f90141f297385dTimo Sirainen master_service_set_client_limit(service, 1);
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen master_service_set_service_count(service, 1);
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen }
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen master_service_io_listeners_add(service);
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen if ((service->flags & MASTER_SERVICE_FLAG_STD_CLIENT) != 0) {
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen /* we already have a connection to be served */
94d8e51119003d2bc5a100c663f90141f297385dTimo Sirainen service->master_status.available_count--;
4a514fb20e04df397842cde11cc9ea92abfe9728Timo Sirainen }
94d8e51119003d2bc5a100c663f90141f297385dTimo Sirainen master_status_update(service);
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen}
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainenvoid master_service_env_clean(bool preserve_home)
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen{
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen static const char *preserve_envs[] = {
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen "HOME", /* keep as the first element */
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen "USER",
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen "TZ",
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen#ifdef DEBUG
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen "GDB",
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen#endif
d9a7e950a9cd21f2b4a90ec7759fca9e8fcc7995Timo Sirainen NULL
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen };
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen ARRAY_TYPE(const_string) envs;
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen const char *value, *const *envp;
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen unsigned int i;
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen t_array_init(&envs, N_ELEMENTS(preserve_envs));
2ac5f36aa7c2e7a07ba8815d43a6d7483f62e74cTimo Sirainen i = preserve_home ? 0 : 1;
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen for (; preserve_envs[i] != NULL; i++) {
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen const char *key = preserve_envs[i];
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen value = getenv(key);
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen if (value != NULL) {
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen value = t_strconcat(key, "=", value, NULL);
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen array_append(&envs, &value, 1);
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen }
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen }
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen /* Note that if the original environment was set with env_put(), the
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen environment strings will be invalid after env_clean(). That's why
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen we t_strconcat() them above. */
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen env_clean();
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen array_foreach(&envs, envp)
d3e5a14ea363264dcc7640ca7226249d0c27a793Timo Sirainen env_put(*envp);
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen}
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainenvoid master_service_set_client_limit(struct master_service *service,
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen unsigned int client_limit)
2ac5f36aa7c2e7a07ba8815d43a6d7483f62e74cTimo Sirainen{
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen unsigned int used;
d9a7e950a9cd21f2b4a90ec7759fca9e8fcc7995Timo Sirainen
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen i_assert(service->master_status.available_count ==
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen service->total_available_count);
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen used = service->total_available_count -
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen service->master_status.available_count;
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen i_assert(client_limit >= used);
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen service->total_available_count = client_limit;
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen service->master_status.available_count = client_limit - used;
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen}
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainenunsigned int master_service_get_client_limit(struct master_service *service)
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen{
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen return service->total_available_count;
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen}
6f970b9a0dadb80e120d017c75c637b5a3879dacTimo Sirainen
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainenvoid master_service_set_service_count(struct master_service *service,
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen unsigned int count)
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen{
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen unsigned int used;
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen used = service->total_available_count -
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen service->master_status.available_count;
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen i_assert(count >= used);
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen if (service->total_available_count > count) {
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen service->total_available_count = count;
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen service->master_status.available_count = count - used;
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen }
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen service->service_count_left = count;
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen}
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainenunsigned int master_service_get_service_count(struct master_service *service)
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen{
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen return service->service_count_left;
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen}
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainenunsigned int master_service_get_socket_count(struct master_service *service)
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen{
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen return service->socket_count;
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen}
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainenvoid master_service_set_avail_overflow_callback(struct master_service *service,
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen void (*callback)(void))
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen{
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen service->avail_overflow_callback = callback;
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen}
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainenconst char *master_service_get_config_path(struct master_service *service)
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen{
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen return service->config_path;
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen}
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainenconst char *master_service_get_version_string(struct master_service *service)
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen{
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen return service->version_string;
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen}
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainenconst char *master_service_get_name(struct master_service *service)
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen{
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen return service->name;
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen}
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainenvoid master_service_run(struct master_service *service,
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen master_service_connection_callback_t *callback)
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen{
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen service->callback = callback;
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen io_loop_run(service->ioloop);
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen service->callback = NULL;
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen}
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainenvoid master_service_stop(struct master_service *service)
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen{
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen io_loop_stop(service->ioloop);
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen}
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainenvoid master_service_stop_new_connections(struct master_service *service)
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen{
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen unsigned int current_count;
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen if (service->stopping)
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen return;
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen service->stopping = TRUE;
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen master_service_io_listeners_remove(service);
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen master_service_io_listeners_close(service);
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen /* make sure we stop after servicing current connections */
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen current_count = service->total_available_count -
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen service->master_status.available_count;
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen service->service_count_left = current_count;
2ac5f36aa7c2e7a07ba8815d43a6d7483f62e74cTimo Sirainen service->total_available_count = current_count;
636f017be100bce67d66fd3ae1544a47681efd33Timo Sirainen
636f017be100bce67d66fd3ae1544a47681efd33Timo Sirainen if (current_count == 0)
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen master_service_stop(service);
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen else {
e58f291e777b25c1286965b80e142ada6fdacb03Timo Sirainen /* notify master that we're not accepting any more
e58f291e777b25c1286965b80e142ada6fdacb03Timo Sirainen connections */
636f017be100bce67d66fd3ae1544a47681efd33Timo Sirainen service->master_status.available_count = 0;
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen master_status_update(service);
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen }
494a5de15db3b2806ab31d5ecc3e1c306ae14d06Timo Sirainen if (service->login != NULL)
e58f291e777b25c1286965b80e142ada6fdacb03Timo Sirainen master_login_stop(service->login);
e58f291e777b25c1286965b80e142ada6fdacb03Timo Sirainen}
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen
636f017be100bce67d66fd3ae1544a47681efd33Timo Sirainenbool master_service_is_killed(struct master_service *service)
636f017be100bce67d66fd3ae1544a47681efd33Timo Sirainen{
636f017be100bce67d66fd3ae1544a47681efd33Timo Sirainen return service->killed;
9d4c027e7de01ab948d6221bc27c9b45d32d1ea5Timo Sirainen}
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainenvoid master_service_anvil_send(struct master_service *service, const char *cmd)
636f017be100bce67d66fd3ae1544a47681efd33Timo Sirainen{
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen ssize_t ret;
08ed4ab71fd2a4e800d9025a736f0f46b771ea90Timo Sirainen
08ed4ab71fd2a4e800d9025a736f0f46b771ea90Timo Sirainen if ((service->flags & MASTER_SERVICE_FLAG_STANDALONE) != 0)
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen return;
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen ret = write(MASTER_ANVIL_FD, cmd, strlen(cmd));
1c1cecd3dfaf71b0c9499b044023e631841e88aaTimo Sirainen if (ret < 0) {
1c1cecd3dfaf71b0c9499b044023e631841e88aaTimo Sirainen if (errno == EPIPE) {
1c1cecd3dfaf71b0c9499b044023e631841e88aaTimo Sirainen /* anvil process was probably recreated, don't bother
1c1cecd3dfaf71b0c9499b044023e631841e88aaTimo Sirainen logging an error about losing connection to it */
1c1cecd3dfaf71b0c9499b044023e631841e88aaTimo Sirainen return;
1c1cecd3dfaf71b0c9499b044023e631841e88aaTimo Sirainen }
1c1cecd3dfaf71b0c9499b044023e631841e88aaTimo Sirainen i_error("write(anvil) failed: %m");
636f017be100bce67d66fd3ae1544a47681efd33Timo Sirainen } else if (ret == 0)
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen i_error("write(anvil) failed: EOF");
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen else {
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen i_assert((size_t)ret == strlen(cmd));
3e0bae44b65f5c46989fcef3d1e07203f496327eTimo Sirainen }
0bd259973f98837cf0e41fdee3e2a578e51ad09eTimo Sirainen}
0bd259973f98837cf0e41fdee3e2a578e51ad09eTimo Sirainen
e4cb3bfcd42f1f2c9e676ece6f7f53803f5c6a16Timo Sirainenvoid master_service_client_connection_accept(struct master_service_connection *conn)
e4cb3bfcd42f1f2c9e676ece6f7f53803f5c6a16Timo Sirainen{
e4cb3bfcd42f1f2c9e676ece6f7f53803f5c6a16Timo Sirainen conn->accepted = TRUE;
e4cb3bfcd42f1f2c9e676ece6f7f53803f5c6a16Timo Sirainen}
6f970b9a0dadb80e120d017c75c637b5a3879dacTimo Sirainen
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainenvoid master_service_client_connection_destroyed(struct master_service *service)
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen{
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen /* we can listen again */
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen master_service_io_listeners_add(service);
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen i_assert(service->total_available_count > 0);
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen i_assert(service->service_count_left > 0);
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen if (service->service_count_left == service->total_available_count) {
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen service->total_available_count--;
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen service->service_count_left--;
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen } else {
636f017be100bce67d66fd3ae1544a47681efd33Timo Sirainen if (service->service_count_left != (unsigned int)-1)
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen service->service_count_left--;
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen i_assert(service->master_status.available_count <
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen service->total_available_count);
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen service->master_status.available_count++;
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen }
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen if (service->service_count_left == 0) {
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen i_assert(service->master_status.available_count ==
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen service->total_available_count);
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen master_service_stop(service);
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen } else if ((service->io_status_error == NULL ||
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen service->listeners == NULL) &&
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen service->master_status.available_count ==
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen service->total_available_count) {
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen /* we've finished handling all clients, and
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen a) master has closed the connection
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen b) there are no listeners (std-client?) */
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen master_service_stop(service);
d3e5a14ea363264dcc7640ca7226249d0c27a793Timo Sirainen } else {
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen master_status_update(service);
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen }
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen}
2ac5f36aa7c2e7a07ba8815d43a6d7483f62e74cTimo Sirainen
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainenstatic void master_service_set_login_state(struct master_service *service,
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen enum master_login_state state)
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen{
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen if (service->to_overflow_state != NULL)
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen timeout_remove(&service->to_overflow_state);
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen
8f70c97f7ab7b7e1683ed5cfcd96721a899c2520Timo Sirainen switch (state) {
8f70c97f7ab7b7e1683ed5cfcd96721a899c2520Timo Sirainen case MASTER_LOGIN_STATE_NONFULL:
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen service->call_avail_overflow = FALSE;
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen if (service->master_status.available_count > 0)
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen return;
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen /* some processes should now be able to handle new connections,
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen although we can't. but there may be race conditions, so
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen make sure that we'll check again soon if the state has
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen changed to "full" without our knowledge. */
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen service->to_overflow_state =
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen timeout_add(MASTER_SERVICE_STATE_CHECK_MSECS,
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen master_service_refresh_login_state,
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen service);
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen return;
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen case MASTER_LOGIN_STATE_FULL:
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen /* make sure we're listening for more connections */
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen service->call_avail_overflow = TRUE;
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen master_service_io_listeners_add(service);
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen return;
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen }
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen i_error("Invalid master login state: %d", state);
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen}
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainenstatic void master_service_refresh_login_state(struct master_service *service)
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen{
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen int ret;
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen ret = lseek(MASTER_LOGIN_NOTIFY_FD, 0, SEEK_CUR);
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen if (ret < 0)
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen i_error("lseek(login notify fd) failed: %m");
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen else
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen master_service_set_login_state(service, ret);
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen}
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainenvoid master_service_close_config_fd(struct master_service *service)
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen{
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen if (service->config_fd != -1) {
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen if (close(service->config_fd) < 0)
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen i_error("close(master config fd) failed: %m");
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen service->config_fd = -1;
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen }
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen}
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainenvoid master_service_deinit(struct master_service **_service)
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen{
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen struct master_service *service = *_service;
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen *_service = NULL;
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen master_service_io_listeners_remove(service);
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen master_service_close_config_fd(service);
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen if (service->to_die != NULL)
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen timeout_remove(&service->to_die);
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen if (service->to_overflow_state != NULL)
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen timeout_remove(&service->to_overflow_state);
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen if (service->to_status != NULL)
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen timeout_remove(&service->to_status);
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen if (service->io_status_error != NULL)
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen io_remove(&service->io_status_error);
1f4399a277b861419b82758ab0462e90c00a4c41Timo Sirainen if (service->io_status_write != NULL)
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen io_remove(&service->io_status_write);
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen if (array_is_created(&service->config_overrides))
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen array_free(&service->config_overrides);
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen if (service->set_parser != NULL) {
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen settings_parser_deinit(&service->set_parser);
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen pool_unref(&service->set_pool);
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen }
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen lib_signals_deinit();
2ac5f36aa7c2e7a07ba8815d43a6d7483f62e74cTimo Sirainen io_loop_destroy(&service->ioloop);
636f017be100bce67d66fd3ae1544a47681efd33Timo Sirainen
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen i_free(service->listeners);
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen i_free(service->getopt_str);
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen i_free(service->name);
9b00ecffbe74fd864d0d72e6112ec53b86f619baTimo Sirainen i_free(service);
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen
9b00ecffbe74fd864d0d72e6112ec53b86f619baTimo Sirainen lib_deinit();
9b00ecffbe74fd864d0d72e6112ec53b86f619baTimo Sirainen}
9b00ecffbe74fd864d0d72e6112ec53b86f619baTimo Sirainen
9b00ecffbe74fd864d0d72e6112ec53b86f619baTimo Sirainenstatic void master_service_listen(struct master_service_listener *l)
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen{
3e0bae44b65f5c46989fcef3d1e07203f496327eTimo Sirainen struct master_service *service = l->service;
3e0bae44b65f5c46989fcef3d1e07203f496327eTimo Sirainen struct master_service_connection conn;
0bd259973f98837cf0e41fdee3e2a578e51ad09eTimo Sirainen
0bd259973f98837cf0e41fdee3e2a578e51ad09eTimo Sirainen if (service->master_status.available_count == 0) {
e4cb3bfcd42f1f2c9e676ece6f7f53803f5c6a16Timo Sirainen /* we are full. stop listening for now, unless overflow
e4cb3bfcd42f1f2c9e676ece6f7f53803f5c6a16Timo Sirainen callback destroys one of the existing connections */
e4cb3bfcd42f1f2c9e676ece6f7f53803f5c6a16Timo Sirainen if (service->call_avail_overflow &&
e4cb3bfcd42f1f2c9e676ece6f7f53803f5c6a16Timo Sirainen service->avail_overflow_callback != NULL)
636f017be100bce67d66fd3ae1544a47681efd33Timo Sirainen service->avail_overflow_callback();
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen if (service->master_status.available_count == 0) {
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen master_service_io_listeners_remove(service);
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen return;
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen }
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen }
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen
1c1cecd3dfaf71b0c9499b044023e631841e88aaTimo Sirainen memset(&conn, 0, sizeof(conn));
636f017be100bce67d66fd3ae1544a47681efd33Timo Sirainen conn.listen_fd = l->fd;
636f017be100bce67d66fd3ae1544a47681efd33Timo Sirainen conn.fd = net_accept(l->fd, &conn.remote_ip, &conn.remote_port);
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen if (conn.fd < 0) {
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen struct stat st;
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen int orig_errno = errno;
636f017be100bce67d66fd3ae1544a47681efd33Timo Sirainen
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen if (conn.fd == -1)
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen return;
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen if (errno == ENOTSOCK) {
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen /* it's not a socket. should be a fifo. */
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen } else if (errno == EINVAL &&
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen (fstat(l->fd, &st) == 0 && S_ISFIFO(st.st_mode))) {
6f970b9a0dadb80e120d017c75c637b5a3879dacTimo Sirainen /* BSDI fails accept(fifo) with EINVAL. */
6f970b9a0dadb80e120d017c75c637b5a3879dacTimo Sirainen } else {
6f970b9a0dadb80e120d017c75c637b5a3879dacTimo Sirainen errno = orig_errno;
6f970b9a0dadb80e120d017c75c637b5a3879dacTimo Sirainen i_error("net_accept() failed: %m");
6f970b9a0dadb80e120d017c75c637b5a3879dacTimo Sirainen master_service_error(service);
6f970b9a0dadb80e120d017c75c637b5a3879dacTimo Sirainen return;
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen }
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen /* use the "listener" as the connection fd and stop the
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen listener. */
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen conn.fd = l->fd;
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen conn.listen_fd = l->fd;
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen conn.fifo = TRUE;
e7d0bea63a08b08c47c4b5c187d2cb7127859657Timo Sirainen
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen io_remove(&l->io);
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen l->fd = -1;
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen }
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen conn.ssl = l->ssl;
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen net_set_nonblock(conn.fd, TRUE);
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen i_assert(service->master_status.available_count > 0);
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen service->master_status.available_count--;
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen master_status_update(service);
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen service->callback(&conn);
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen if (!conn.accepted) {
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen if (close(conn.fd) < 0)
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen i_error("close(service connection) failed: %m");
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen master_service_client_connection_destroyed(service);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen }
1701e3f91107051b1704721bf1dc1e32491faaf9Timo Sirainen}
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenstatic void io_listeners_init(struct master_service *service)
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen{
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen unsigned int i;
8ca217bf3aa23c7922d0d4aa44fcd2320416d61cMartti Rannanjärvi
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen if (service->socket_count == 0)
678d0463849ba777106eb7875f27db07a5d8e3dfTimo Sirainen return;
bd4e36a8cd7257cca7d1434c49a1e343ed7c5100Timo Sirainen
bd4e36a8cd7257cca7d1434c49a1e343ed7c5100Timo Sirainen service->listeners =
bd4e36a8cd7257cca7d1434c49a1e343ed7c5100Timo Sirainen i_new(struct master_service_listener, service->socket_count);
bd4e36a8cd7257cca7d1434c49a1e343ed7c5100Timo Sirainen
cb933f0a570a9cef5c975eadb818aa6b1002a269Timo Sirainen for (i = 0; i < service->socket_count; i++) {
cb933f0a570a9cef5c975eadb818aa6b1002a269Timo Sirainen struct master_service_listener *l = &service->listeners[i];
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainen
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainen l->service = service;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen l->fd = MASTER_LISTEN_FD_FIRST + i;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen if (i >= service->socket_count - service->ssl_socket_count)
68a4946b12583b88fa802e52ebee45cd96056772Timo Sirainen l->ssl = TRUE;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen }
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen}
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenvoid master_service_io_listeners_add(struct master_service *service)
ae2b61a8c6318e56dabd44de17e227c95985aedaTimo Sirainen{
ae2b61a8c6318e56dabd44de17e227c95985aedaTimo Sirainen unsigned int i;
ae2b61a8c6318e56dabd44de17e227c95985aedaTimo Sirainen
e36574dadcac802d6780fa94ee45951e75594c96Timo Sirainen if (service->stopping)
ae2b61a8c6318e56dabd44de17e227c95985aedaTimo Sirainen return;
ae2b61a8c6318e56dabd44de17e227c95985aedaTimo Sirainen
68a4946b12583b88fa802e52ebee45cd96056772Timo Sirainen if (service->listeners == NULL)
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainen io_listeners_init(service);
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainen
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainen for (i = 0; i < service->socket_count; i++) {
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainen struct master_service_listener *l = &service->listeners[i];
68a4946b12583b88fa802e52ebee45cd96056772Timo Sirainen
68a4946b12583b88fa802e52ebee45cd96056772Timo Sirainen if (l->io == NULL && l->fd != -1) {
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainen l->io = io_add(MASTER_LISTEN_FD_FIRST + i, IO_READ,
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainen master_service_listen, l);
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainen }
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainen }
6dd77763f5451269ace733579cf58f2f3b18bca4Timo Sirainen}
6dd77763f5451269ace733579cf58f2f3b18bca4Timo Sirainen
6dd77763f5451269ace733579cf58f2f3b18bca4Timo Sirainenvoid master_service_io_listeners_remove(struct master_service *service)
6dd77763f5451269ace733579cf58f2f3b18bca4Timo Sirainen{
6dd77763f5451269ace733579cf58f2f3b18bca4Timo Sirainen unsigned int i;
6dd77763f5451269ace733579cf58f2f3b18bca4Timo Sirainen
6dd77763f5451269ace733579cf58f2f3b18bca4Timo Sirainen if (service->listeners != NULL) {
6dd77763f5451269ace733579cf58f2f3b18bca4Timo Sirainen for (i = 0; i < service->socket_count; i++) {
6dd77763f5451269ace733579cf58f2f3b18bca4Timo Sirainen if (service->listeners[i].io != NULL)
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen io_remove(&service->listeners[i].io);
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen }
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen }
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen}
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainenstatic void master_service_io_listeners_close(struct master_service *service)
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen{
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen unsigned int i;
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen if (service->listeners != NULL) {
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen /* close via listeners. some fds might be pipes that are
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen currently handled as clients. we don't want to close them. */
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen for (i = 0; i < service->socket_count; i++) {
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen if (service->listeners[i].fd != -1) {
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen if (close(service->listeners[i].fd) < 0) {
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen i_error("close(listener %d) failed: %m",
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen service->listeners[i].fd);
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen }
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen }
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen }
4b1781e4c64be52e25b5994e5242dbe696cc7d29Timo Sirainen } else {
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen for (i = 0; i < service->socket_count; i++) {
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen int fd = MASTER_LISTEN_FD_FIRST + i;
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen if (close(fd) < 0)
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen i_error("close(listener %d) failed: %m", fd);
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen }
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen }
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen}
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainenstatic bool master_status_update_is_important(struct master_service *service)
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen{
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen if (service->master_status.available_count == 0)
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen return TRUE;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen if (!service->initial_status_sent)
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen return TRUE;
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen return FALSE;
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen}
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainenvoid master_status_update(struct master_service *service)
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen{
15bfe73fb3988bb80e6afe6a60b9a715c7207600Timo Sirainen ssize_t ret;
4809537f0c5a2e1cee9559ec842cc869884d2cb7Timo Sirainen bool important_update;
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen important_update = master_status_update_is_important(service);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen if (service->master_status.pid == 0 ||
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen service->master_status.available_count ==
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen service->last_sent_status_avail_count) {
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen /* a) closed, b) updating to same state */
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen if (service->to_status != NULL)
8709b2fe6ec2b5ca1d90a63490f8371472062efdTimo Sirainen timeout_remove(&service->to_status);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen if (service->io_status_write != NULL)
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen io_remove(&service->io_status_write);
949fa97a4ab5c62e4db73c3973e35ae3b73a2b23Timo Sirainen return;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen }
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen if (ioloop_time == service->last_sent_status_time &&
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen !important_update) {
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen /* don't spam master */
e353d03d3643f380dc7e6dc29a512dec86b8a3deTimo Sirainen if (service->to_status != NULL)
e353d03d3643f380dc7e6dc29a512dec86b8a3deTimo Sirainen timeout_reset(service->to_status);
e353d03d3643f380dc7e6dc29a512dec86b8a3deTimo Sirainen else {
e353d03d3643f380dc7e6dc29a512dec86b8a3deTimo Sirainen service->to_status =
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen timeout_add(1000, master_status_update,
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen service);
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen }
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen if (service->io_status_write != NULL)
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen io_remove(&service->io_status_write);
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen return;
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen }
d02d34e138e32b4266f5a403d6c51d7803bf322fTimo Sirainen
a7e1fd461a52bc597b7c0319c613dfbb6d3dc914Timo Sirainen if (service->to_status != NULL)
d02d34e138e32b4266f5a403d6c51d7803bf322fTimo Sirainen timeout_remove(&service->to_status);
d02d34e138e32b4266f5a403d6c51d7803bf322fTimo Sirainen
efe78d3ba24fc866af1c79b9223dc0809ba26cadStephan Bosch ret = write(MASTER_STATUS_FD, &service->master_status,
47d5cc09738defd0020c797866e2a25a344aa65aTimo Sirainen sizeof(service->master_status));
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen if (ret == sizeof(service->master_status)) {
47d5cc09738defd0020c797866e2a25a344aa65aTimo Sirainen /* success */
47d5cc09738defd0020c797866e2a25a344aa65aTimo Sirainen if (service->io_status_write != NULL) {
47d5cc09738defd0020c797866e2a25a344aa65aTimo Sirainen /* delayed important update sent successfully */
47d5cc09738defd0020c797866e2a25a344aa65aTimo Sirainen io_remove(&service->io_status_write);
47d5cc09738defd0020c797866e2a25a344aa65aTimo Sirainen }
47d5cc09738defd0020c797866e2a25a344aa65aTimo Sirainen service->last_sent_status_time = ioloop_time;
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen service->last_sent_status_avail_count =
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen service->master_status.available_count;
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen service->initial_status_sent = TRUE;
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen } else if (ret >= 0) {
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen /* shouldn't happen? */
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen i_error("write(master_status_fd) returned %d", (int)ret);
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen service->master_status.pid = 0;
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen } else if (errno != EAGAIN) {
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen /* failure */
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen if (errno != EPIPE)
758d8b46f9e8fd87cf58bb4912cddf6bf28918cfTimo Sirainen i_error("write(master_status_fd) failed: %m");
758d8b46f9e8fd87cf58bb4912cddf6bf28918cfTimo Sirainen service->master_status.pid = 0;
b7c7a04bc5edb8eebea3837ff624441d9fa3721cTimo Sirainen } else if (important_update) {
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen /* reader is busy, but it's important to get this notification
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen through. send it when possible. */
c905571984fe8ccdb6f2a266b813f19b0643a66cTimo Sirainen if (service->io_status_write == NULL) {
c905571984fe8ccdb6f2a266b813f19b0643a66cTimo Sirainen service->io_status_write =
c905571984fe8ccdb6f2a266b813f19b0643a66cTimo Sirainen io_add(MASTER_STATUS_FD, IO_WRITE,
c905571984fe8ccdb6f2a266b813f19b0643a66cTimo Sirainen master_status_update, service);
d02d34e138e32b4266f5a403d6c51d7803bf322fTimo Sirainen }
d02d34e138e32b4266f5a403d6c51d7803bf322fTimo Sirainen }
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen}
e200d1ba38eeebfb0b9e60150d93753ec6d823c8Timo Sirainen
e200d1ba38eeebfb0b9e60150d93753ec6d823c8Timo Sirainenbool version_string_verify(const char *line, const char *service_name,
e200d1ba38eeebfb0b9e60150d93753ec6d823c8Timo Sirainen unsigned major_version)
d02d34e138e32b4266f5a403d6c51d7803bf322fTimo Sirainen{
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen unsigned int service_name_len = strlen(service_name);
d06c46087e9e6e66bbbbb9df1d5b33154349515cTimo Sirainen bool ret;
d06c46087e9e6e66bbbbb9df1d5b33154349515cTimo Sirainen
d06c46087e9e6e66bbbbb9df1d5b33154349515cTimo Sirainen if (strncmp(line, "VERSION\t", 8) != 0)
d06c46087e9e6e66bbbbb9df1d5b33154349515cTimo Sirainen return FALSE;
d06c46087e9e6e66bbbbb9df1d5b33154349515cTimo Sirainen line += 8;
d06c46087e9e6e66bbbbb9df1d5b33154349515cTimo Sirainen
d06c46087e9e6e66bbbbb9df1d5b33154349515cTimo Sirainen if (strncmp(line, service_name, service_name_len) != 0 ||
d06c46087e9e6e66bbbbb9df1d5b33154349515cTimo Sirainen line[service_name_len] != '\t')
d06c46087e9e6e66bbbbb9df1d5b33154349515cTimo Sirainen return FALSE;
47d5cc09738defd0020c797866e2a25a344aa65aTimo Sirainen line += service_name_len + 1;
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen T_BEGIN {
47d5cc09738defd0020c797866e2a25a344aa65aTimo Sirainen ret = str_uint_equals(t_strcut(line, '\t'), major_version);
47d5cc09738defd0020c797866e2a25a344aa65aTimo Sirainen } T_END;
47d5cc09738defd0020c797866e2a25a344aa65aTimo Sirainen return ret;
47d5cc09738defd0020c797866e2a25a344aa65aTimo Sirainen}
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainen