main.c revision 2ac5f36aa7c2e7a07ba8815d43a6d7483f62e74c
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen/* Copyright (c) 2011-2016 Dovecot authors, see the included COPYING file */
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen#include "lib.h"
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen#include "restrict-access.h"
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen#include "master-service.h"
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen#include "master-service-settings.h"
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen#include "ipc-group.h"
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen#include "ipc-connection.h"
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen#include "client.h"
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen
faec0abfd648c647030027e86de2ce8911df683bTimo Sirainenstatic bool ipc_socket_is_client(const char *name)
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen{
2ac5f36aa7c2e7a07ba8815d43a6d7483f62e74cTimo Sirainen size_t len;
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen
faec0abfd648c647030027e86de2ce8911df683bTimo Sirainen if (strcmp(name, "ipc") == 0)
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen return TRUE;
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen
faec0abfd648c647030027e86de2ce8911df683bTimo Sirainen len = strlen(name);
faec0abfd648c647030027e86de2ce8911df683bTimo Sirainen if (len > 7 && strcmp(name + len - 7, "-client") == 0)
faec0abfd648c647030027e86de2ce8911df683bTimo Sirainen return TRUE;
faec0abfd648c647030027e86de2ce8911df683bTimo Sirainen return FALSE;
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen}
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainenstatic void client_connected(struct master_service_connection *conn)
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen{
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen master_service_client_connection_accept(conn);
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen
faec0abfd648c647030027e86de2ce8911df683bTimo Sirainen if (ipc_socket_is_client(conn->name))
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen (void)client_create(conn->fd);
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen else
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen (void)ipc_connection_create(conn->listen_fd, conn->fd);
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen}
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainenint main(int argc, char *argv[])
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen{
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen const enum master_service_flags service_flags =
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen MASTER_SERVICE_FLAG_UPDATE_PROCTITLE;
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen const char *error;
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen master_service = master_service_init("ipc", service_flags,
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen &argc, &argv, "");
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen if (master_getopt(master_service) > 0)
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen return FATAL_DEFAULT;
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen if (master_service_settings_read_simple(master_service,
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen NULL, &error) < 0)
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen i_fatal("Error reading configuration: %s", error);
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen master_service_init_log(master_service, "ipc: ");
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen restrict_access_by_env(NULL, FALSE);
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen restrict_access_allow_coredumps(TRUE);
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen ipc_groups_init();
d6b3cfd855c0eebed68be50d3111de1b5a6afeb0Timo Sirainen master_service_init_finish(master_service);
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen master_service_run(master_service, client_connected);
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen clients_destroy_all();
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen ipc_groups_deinit();
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen master_service_deinit(&master_service);
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen return 0;
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen}