master-service.c revision d9515a2eaa94c8287188c38fc28028727671e729
/* Copyright (c) 2005-2012 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "lib-signals.h"
#include "ioloop.h"
#include "abspath.h"
#include "array.h"
#include "strescape.h"
#include "env-util.h"
#include "home-expand.h"
#include "process-title.h"
#include "restrict-access.h"
#include "fd-close-on-exec.h"
#include "settings-parser.h"
#include "syslog-util.h"
#include "master-instance.h"
#include "master-login.h"
#include "master-service-private.h"
#include "master-service-settings.h"
#include <stdlib.h>
#include <unistd.h>
#include <syslog.h>
#define MASTER_CONFIG_FILE_ENV "CONFIG_FILE"
/* getenv(MASTER_DOVECOT_VERSION_ENV) provides master's version number */
#define MASTER_DOVECOT_VERSION_ENV "DOVECOT_VERSION"
/* when we're full of connections, how often to check if login state has
changed. we normally notice it immediately because of a signal, so this is
just a fallback against race conditions. */
#define MASTER_SERVICE_STATE_CHECK_MSECS 1000
/* If die callback hasn't managed to stop the service for this many seconds,
force it. */
struct master_service *master_service;
const char *master_service_getopt_string(void)
{
return "c:i:ko:OL";
}
{
/* SIGINT comes either from master process or from keyboard. we don't
want to log it in either case.*/
i_warning("Killed with signal %d (by pid=%s uid=%s code=%s)",
/* never die when idling */
return;
/* SIGINT came from master. die only if we're not handling
any clients currently. */
return;
!service->idle_die_callback())
return;
}
}
static void
{
}
{
i_fatal("Dovecot version mismatch: "
"(if you don't care, set version_ignore=yes)",
}
}
struct master_service *
{
struct master_service *service;
const char *value;
unsigned int count;
#ifdef DEBUG
(flags & MASTER_SERVICE_FLAG_STANDALONE) == 0) {
int count;
}
#endif
if ((flags & MASTER_SERVICE_FLAG_STANDALONE) == 0) {
/* make sure we can dump core, at least until
privileges are dropped. (i'm not really sure why this
is needed, because doing the same just before exec
doesn't help, and exec shouldn't affect this with
non-setuid/gid binaries..) */
}
/* NOTE: we start rooted, so keep the code minimal until
restrict_access_by_env() is called */
lib_init();
/* Set a logging prefix temporarily. This will be ignored once the log
is properly initialized */
/* ignore these signals as early as possible */
/* keep getopt_str first in case it contains "+" */
}
if ((flags & MASTER_SERVICE_FLAG_STANDALONE) == 0) {
} else {
}
}
(flags & MASTER_SERVICE_FLAG_USE_SSL_SETTINGS) != 0;
/* set up some kind of logging until we know exactly how and where
we want to log */
} else {
}
if ((flags & MASTER_SERVICE_FLAG_STANDALONE) == 0) {
/* initialize master_status structure */
/* set the default limit */
count == 0)
/* seve the process limit */
count > 0)
/* set the default service count */
count > 0)
/* set the idle kill timeout */
} else {
}
return service;
}
{
int c;
service->getopt_str)) > 0) {
break;
}
return c;
}
const char *prefix)
{
const char *path;
return;
}
/* logging via log service */
return;
}
return;
}
/* error logging goes to file or stderr */
}
/* something gets logged to syslog */
int facility;
&facility))
/* set error handlers back to file */
}
}
if (*path != '\0')
}
if (*path != '\0')
}
}
bool set)
{
}
void (*callback)(void))
{
}
bool (*callback)(void))
{
}
{
struct master_instance_list *list;
const struct master_instance *inst;
const char *instance_path, *path;
/* note that we don't have any settings yet. we're just finding out
which dovecot.conf we even want to read! so we must use the
hardcoded state_dir path. */
}
}
{
const char *path;
switch (opt) {
case 'c':
break;
case 'i':
break;
case 'k':
break;
case 'o':
break;
case 'O':
break;
case 'L':
break;
default:
return FALSE;
}
return TRUE;
}
{
else {
service);
service->die_callback();
}
}
}
{
/* status fd is a write-only pipe, so if we're here it means the
master wants us to die (or died itself). don't die until all
service connections are finished. */
/* the log fd may also be closed already, don't die when trying to
log later */
}
{
/* set default signal handlers */
}
i_fatal("Must be started by dovecot master process");
/* start listening errors for status fd, it means master died */
}
/* we already have a connection to be served */
}
}
void master_service_env_clean(void)
{
env_clean();
else T_BEGIN {
} T_END;
}
unsigned int client_limit)
{
unsigned int used;
}
{
return service->total_available_count;
}
{
return service->process_limit;
}
{
return service->idle_kill_secs;
}
unsigned int count)
{
unsigned int used;
}
}
{
return service->service_count_left;
}
{
return service->socket_count;
}
void (*callback)(void))
{
}
{
return service->config_path;
}
{
return service->version_string;
}
{
}
{
}
{
}
{
unsigned int current_count;
return;
/* make sure we stop after servicing current connections */
if (current_count == 0)
else {
/* notify master that we're not accepting any more
connections */
}
}
{
}
{
return;
if (ret < 0) {
/* anvil process was probably recreated, don't bother
logging an error about losing connection to it */
return;
}
i_error("write(anvil) failed: %m");
} else if (ret == 0)
i_error("write(anvil) failed: EOF");
else {
}
}
{
}
{
}
{
/* we can listen again */
} else {
}
if (service->service_count_left == 0) {
/* we've finished handling all clients, and
a) master has closed the connection
b) there are no listeners (std-client?) */
} else {
}
}
enum master_login_state state)
{
switch (state) {
return;
/* some processes should now be able to handle new connections,
although we can't. but there may be race conditions, so
make sure that we'll check again soon if the state has
changed to "full" without our knowledge. */
service);
return;
case MASTER_LOGIN_STATE_FULL:
/* make sure we're listening for more connections */
return;
}
}
{
int ret;
if (ret < 0)
i_error("lseek(login notify fd) failed: %m");
else
}
{
i_error("close(master config fd) failed: %m");
}
}
{
}
lib_deinit();
}
static void master_service_listen(struct master_service_listener *l)
{
struct master_service_connection conn;
/* we are full. stop listening for now, unless overflow
callback destroys one of the existing connections */
if (service->call_avail_overflow &&
return;
}
}
int orig_errno = errno;
return;
/* it's not a socket. should be a fifo. */
/* BSDI fails accept(fifo) with EINVAL. */
} else {
errno = orig_errno;
i_error("net_accept() failed: %m");
/* try again later after one of the existing
connections has died */
return;
}
/* use the "listener" as the connection fd and stop the
listener. */
l->fd = -1;
}
i_error("close(service connection) failed: %m");
}
/* reading FIFOs stays open forever, don't count them
as real clients */
}
}
{
unsigned int i;
if (service->socket_count == 0)
return;
for (i = 0; i < service->socket_count; i++) {
l->fd = MASTER_LISTEN_FD_FIRST + i;
}
}
{
unsigned int i;
return;
for (i = 0; i < service->socket_count; i++) {
}
}
}
{
unsigned int i;
for (i = 0; i < service->socket_count; i++) {
}
}
}
{
unsigned int i;
for (i = 0; i < service->socket_count; i++) {
}
}
}
{
unsigned int i;
/* close via listeners. some fds might be pipes that are
currently handled as clients. we don't want to close them. */
for (i = 0; i < service->socket_count; i++) {
i_error("close(listener %d) failed: %m",
}
}
}
} else {
for (i = 0; i < service->socket_count; i++) {
int fd = MASTER_LISTEN_FD_FIRST + i;
}
}
}
{
return TRUE;
if (!service->initial_status_sent)
return TRUE;
return FALSE;
}
{
bool important_update;
used_count));
} T_END;
/* a) closed, b) updating to same state */
return;
}
!important_update) {
/* don't spam master */
else {
service);
}
return;
}
sizeof(service->master_status));
/* success */
/* delayed important update sent successfully */
}
} else if (ret >= 0) {
/* shouldn't happen? */
/* failure */
i_error("write(master_status_fd) failed: %m");
} else if (important_update) {
/* reader is busy, but it's important to get this notification
through. send it when possible. */
}
}
}
unsigned major_version)
{
bool ret;
return FALSE;
line += 8;
return FALSE;
T_BEGIN {
} T_END;
return ret;
}