9c6a09aa16095ff72837799a37e0e3b3e93bb3d8Timo Sirainen#include <unistd.h> /* for getopt() opt* variables */
31a9637b38d37451b649c86301b2c12e53a7810eTimo Sirainen#include <stdio.h> /* for getopt() opt* variables in Solaris */
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen /* stdin/stdout already contains a client which we want to serve */
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen /* this process is currently running standalone without a master */
c6335901c67a4c9365319190a111a2168f3b06f5Timo Sirainen /* Log to configured log file instead of stderr. By default when
c6335901c67a4c9365319190a111a2168f3b06f5Timo Sirainen _FLAG_STANDALONE is set, logging is done to stderr. */
c6335901c67a4c9365319190a111a2168f3b06f5Timo Sirainen MASTER_SERVICE_FLAG_DONT_LOG_TO_STDERR = 0x04,
01230de017cd273de41143d88e9c18df1243ae8aTimo Sirainen /* Service is going to do multiple configuration lookups,
b7b9d4be2a1ff399026a5d6feeffd3a048f22be0Timo Sirainen keep the connection to config service open. Also opens the config
b7b9d4be2a1ff399026a5d6feeffd3a048f22be0Timo Sirainen socket before dropping privileges. */
1f9d1bedae25d86f26c239055c5487499dfeeb58Timo Sirainen /* Don't read settings, but use whatever is in environment */
047c00cd3f7f403672f81569413669238df8c15aTimo Sirainen MASTER_SERVICE_FLAG_NO_CONFIG_SETTINGS = 0x10,
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen /* Use MASTER_LOGIN_NOTIFY_FD to track login overflow state */
f37ecd72aad9b806aae83f71bacafdce32146945Timo Sirainen /* If master sends SIGINT, don't die even if we don't have clients */
e2bdca8201e4aa1cd31332ffbdd4c6eef9151d5eTimo Sirainen /* Show number of connections in process title
e2bdca8201e4aa1cd31332ffbdd4c6eef9151d5eTimo Sirainen (only if verbose_proctitle setting is enabled) */
32b78da5dfbbf6a06b3dbdc9278c60b55714f9bcTimo Sirainen /* SSL settings are always looked up when we have ssl listeners.
32b78da5dfbbf6a06b3dbdc9278c60b55714f9bcTimo Sirainen This flag enables looking up SSL settings even without ssl
32b78da5dfbbf6a06b3dbdc9278c60b55714f9bcTimo Sirainen listeners (i.e. the service does STARTTLS). */
8b5c520883aa37bb55646286d375fdbae294d710Timo Sirainen /* Don't initialize SSL context automatically. */
0679f8a70a8dda43b204ae35fc6a903818cc6584Timo Sirainen /* Don't create a data stack frame between master_service_init() and
0679f8a70a8dda43b204ae35fc6a903818cc6584Timo Sirainen master_service_init_finish(). By default this is done to make sure
0679f8a70a8dda43b204ae35fc6a903818cc6584Timo Sirainen initialization doesn't unnecessarily use up memory in data stack. */
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen MASTER_SERVICE_FLAG_NO_INIT_DATASTACK_FRAME = 0x800,
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen /* This process supports sending statistics to the stats process.
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen Connect to it at startup. */
b68b98e1545bad8af9cb58ef89e8d7f6e16577beAki Tuomi /* only set if ssl is TRUE */
541f258d86b2db26efd5670883966183b4fb6323Timo Sirainen /* fd of the new connection. */
541f258d86b2db26efd5670883966183b4fb6323Timo Sirainen /* fd of the socket listener. Same as fd for a FIFO. */
541f258d86b2db26efd5670883966183b4fb6323Timo Sirainen /* listener name as in configuration file, or "" if unnamed. */
541f258d86b2db26efd5670883966183b4fb6323Timo Sirainen /* Original client/server IP/port. Both of these may have been changed
541f258d86b2db26efd5670883966183b4fb6323Timo Sirainen by the haproxy protocol. */
541f258d86b2db26efd5670883966183b4fb6323Timo Sirainen /* The real client/server IP/port, unchanged by haproxy protocol. */
b68b98e1545bad8af9cb58ef89e8d7f6e16577beAki Tuomi /* filled if connection is proxied */
b68b98e1545bad8af9cb58ef89e8d7f6e16577beAki Tuomi /* This is a connection proxied wit HAproxy (or similar) */
541f258d86b2db26efd5670883966183b4fb6323Timo Sirainen /* This is a FIFO fd. Only a single "connection" is ever received from
541f258d86b2db26efd5670883966183b4fb6323Timo Sirainen a FIFO after the first writer sends something to it. */
541f258d86b2db26efd5670883966183b4fb6323Timo Sirainen /* Perform immediate SSL handshake for this connection. Currently this
541f258d86b2db26efd5670883966183b4fb6323Timo Sirainen needs to be performed explicitly by each service. */
541f258d86b2db26efd5670883966183b4fb6323Timo Sirainen /* Internal: master_service_client_connection_accept() has been
541f258d86b2db26efd5670883966183b4fb6323Timo Sirainen called for this connection. */
db693bf6fcae96d834567f1782257517b7207655Timo Sirainenmaster_service_connection_callback_t(struct master_service_connection *conn);
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainenconst char *master_service_getopt_string(void);
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen/* Start service initialization. */
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainenmaster_service_init(const char *name, enum master_service_flags flags,
a3fe8c0c54d87822f4b4f8f0d10caac611861b2bTimo Sirainen int *argc, char **argv[], const char *getopt_str);
578ef2538ccf42e2a48234c24a8b709397101d88Timo Sirainen/* Call getopt() and handle internal parameters. Return values are the same as
578ef2538ccf42e2a48234c24a8b709397101d88Timo Sirainen getopt()'s. */
578ef2538ccf42e2a48234c24a8b709397101d88Timo Sirainenint master_getopt(struct master_service *service);
9b0f6b90ff8d1d6efd718b0d7cbe01b2454e9fd6Timo Sirainen/* Returns TRUE if str is a valid getopt_str. Currently this only checks for
9b0f6b90ff8d1d6efd718b0d7cbe01b2454e9fd6Timo Sirainen duplicate args so they aren't accidentally added. */
9b0f6b90ff8d1d6efd718b0d7cbe01b2454e9fd6Timo Sirainenbool master_getopt_str_is_valid(const char *str);
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen/* Parser command line option. Returns TRUE if processed. */
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainenbool master_service_parse_option(struct master_service *service,
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen/* Finish service initialization. The caller should drop privileges
d6b3cfd855c0eebed68be50d3111de1b5a6afeb0Timo Sirainen before calling this. This also notifies the master that the service was
d6b3cfd855c0eebed68be50d3111de1b5a6afeb0Timo Sirainen successfully started and there shouldn't be any service throttling even if
d6b3cfd855c0eebed68be50d3111de1b5a6afeb0Timo Sirainen it crashes afterwards, so this should be called after all of the
d6b3cfd855c0eebed68be50d3111de1b5a6afeb0Timo Sirainen initialization code is finished. */
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainenvoid master_service_init_finish(struct master_service *service);
8a0a8c982a6ffc75a4b1c8717b6180a811655794Timo Sirainen/* import_environment is a space-separated list of environment keys or
8a0a8c982a6ffc75a4b1c8717b6180a811655794Timo Sirainen key=values. The key=values are immediately added to the environment.
8a0a8c982a6ffc75a4b1c8717b6180a811655794Timo Sirainen All the keys are added to DOVECOT_PRESERVE_ENVS environment so they're
8a0a8c982a6ffc75a4b1c8717b6180a811655794Timo Sirainen preserved by master_service_env_clean(). */
8a0a8c982a6ffc75a4b1c8717b6180a811655794Timo Sirainenvoid master_service_import_environment(const char *import_environment);
d5eb47a791ec56149fd711cd8e44efc8babeaae5Timo Sirainen/* Clean environment from everything except the ones listed in
d5eb47a791ec56149fd711cd8e44efc8babeaae5Timo Sirainen DOVECOT_PRESERVE_ENVS environment. */
719abeb2088987f213a33a7dd1fe78958beaef03Timo Sirainen/* Initialize logging. Only the first call changes the actual logging
719abeb2088987f213a33a7dd1fe78958beaef03Timo Sirainen functions. The following calls change the log prefix. */
e0740628f6ca05f4bc79a9d8a90b650f4d38d4d0Timo Sirainenvoid master_service_init_log(struct master_service *service,
e0740628f6ca05f4bc79a9d8a90b650f4d38d4d0Timo Sirainen const char *prefix);
f6f23086d0259d50cde3bd5d4180d67d820d293dTimo Sirainen/* Initialize stats client (if it's not already initialized). This is called
f6f23086d0259d50cde3bd5d4180d67d820d293dTimo Sirainen automatically if MASTER_SERVICE_FLAG_SEND_STATS is enabled. If
f6f23086d0259d50cde3bd5d4180d67d820d293dTimo Sirainen silent_notfound_errors is set, connect() errors aren't logged if they're
f6f23086d0259d50cde3bd5d4180d67d820d293dTimo Sirainen happening because the stats service isn't running. */
f6f23086d0259d50cde3bd5d4180d67d820d293dTimo Sirainenvoid master_service_init_stats_client(struct master_service *service,
3f603ef00e35fca21605afa0ad8d76e94fee2b96Timo Sirainen/* If set, die immediately when connection to master is lost.
3f603ef00e35fca21605afa0ad8d76e94fee2b96Timo Sirainen Normally all existing clients are handled first. */
3f603ef00e35fca21605afa0ad8d76e94fee2b96Timo Sirainenvoid master_service_set_die_with_master(struct master_service *service,
86791365b10f45982c88e70f2eb94fd6c3fea151Timo Sirainen/* Call the given when master connection dies and die_with_master is TRUE.
86791365b10f45982c88e70f2eb94fd6c3fea151Timo Sirainen The callback is expected to shut down the service somewhat soon or it's
86791365b10f45982c88e70f2eb94fd6c3fea151Timo Sirainen done forcibly. If NULL, the service is stopped immediately. */
86791365b10f45982c88e70f2eb94fd6c3fea151Timo Sirainenvoid master_service_set_die_callback(struct master_service *service,
86791365b10f45982c88e70f2eb94fd6c3fea151Timo Sirainen void (*callback)(void));
ccf50662cc02b5e703039a4ff7f91a4470e25b71Timo Sirainen/* "idle callback" is called when master thinks we're idling and asks us to
ccf50662cc02b5e703039a4ff7f91a4470e25b71Timo Sirainen die. We'll do it only if the idle callback returns TRUE. This callback isn't
ccf50662cc02b5e703039a4ff7f91a4470e25b71Timo Sirainen even called if the master service code knows that we're handling clients. */
ccf50662cc02b5e703039a4ff7f91a4470e25b71Timo Sirainenvoid master_service_set_idle_die_callback(struct master_service *service,
ccf50662cc02b5e703039a4ff7f91a4470e25b71Timo Sirainen bool (*callback)(void));
6fdfa4d4cf14d1d7764d7faa8258f112e39c8dbeTimo Sirainen/* Call the given callback when there are no available connections and master
6fdfa4d4cf14d1d7764d7faa8258f112e39c8dbeTimo Sirainen has indicated that it can't create any more processes to handle requests.
6fdfa4d4cf14d1d7764d7faa8258f112e39c8dbeTimo Sirainen The callback could decide to kill one of the existing connections. */
6fdfa4d4cf14d1d7764d7faa8258f112e39c8dbeTimo Sirainenvoid master_service_set_avail_overflow_callback(struct master_service *service,
6fdfa4d4cf14d1d7764d7faa8258f112e39c8dbeTimo Sirainen void (*callback)(void));
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen/* Set maximum number of clients we can handle. Default is given by master. */
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainenvoid master_service_set_client_limit(struct master_service *service,
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen/* Returns the maximum number of clients we can handle. */
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainenunsigned int master_service_get_client_limit(struct master_service *service);
1ffb2afe6d7e8860a2231a4827078cf2ef9c22cdTimo Sirainen/* Returns how many processes of this type can be created before reaching the
1ffb2afe6d7e8860a2231a4827078cf2ef9c22cdTimo Sirainenunsigned int master_service_get_process_limit(struct master_service *service);
d4845c4245638fd6f02dc0cb92c3465fae763cbbTimo Sirainen/* Returns service { process_min_avail } */
d4845c4245638fd6f02dc0cb92c3465fae763cbbTimo Sirainenunsigned int master_service_get_process_min_avail(struct master_service *service);
0161376aac025266d8654577c4b9ce371ffc87eaTimo Sirainen/* Returns the service's idle_kill timeout in seconds. Normally master handles
0161376aac025266d8654577c4b9ce371ffc87eaTimo Sirainen sending the kill request when the process has no clients, but some services
0161376aac025266d8654577c4b9ce371ffc87eaTimo Sirainen with permanent client connections may need to handle this themselves. */
0161376aac025266d8654577c4b9ce371ffc87eaTimo Sirainenunsigned int master_service_get_idle_kill_secs(struct master_service *service);
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen/* Set maximum number of client connections we will handle before shutting
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainenvoid master_service_set_service_count(struct master_service *service,
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen unsigned int count);
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen/* Returns the number of client connections we will handle before shutting
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen down. The value is decreased only after connection has been closed. */
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainenunsigned int master_service_get_service_count(struct master_service *service);
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen/* Return the number of listener sockets. */
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainenunsigned int master_service_get_socket_count(struct master_service *service);
7f1b897201d80c83c96b0d663f2a14c517d48f14Timo Sirainen/* Returns the name of the listener socket, or "" if none is specified. */
7f1b897201d80c83c96b0d663f2a14c517d48f14Timo Sirainenconst char *master_service_get_socket_name(struct master_service *service,
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen/* Returns configuration file path. */
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainenconst char *master_service_get_config_path(struct master_service *service);
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen/* Returns PACKAGE_VERSION or NULL if version_ignore=yes. This function is
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen useful mostly as parameter to module_dir_load(). */
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainenconst char *master_service_get_version_string(struct master_service *service);
b2d562f9c7fd13f9a16e9b3bcee904630b80b1feTimo Sirainen/* Returns name of the service, as given in name parameter to _init(). */
b2d562f9c7fd13f9a16e9b3bcee904630b80b1feTimo Sirainenconst char *master_service_get_name(struct master_service *service);
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen/* Start the service. Blocks until finished */
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainenvoid master_service_run(struct master_service *service,
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen master_service_connection_callback_t *callback)
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen/* Stop a running service. */
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainenvoid master_service_stop(struct master_service *service);
275385a2ecc58e41dc7df3ce3cd943caaa58c4d1Timo Sirainen/* Stop once we're done serving existing new connections, but don't accept
275385a2ecc58e41dc7df3ce3cd943caaa58c4d1Timo Sirainen any new ones. */
275385a2ecc58e41dc7df3ce3cd943caaa58c4d1Timo Sirainenvoid master_service_stop_new_connections(struct master_service *service);
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen/* Returns TRUE if we've received a SIGINT/SIGTERM and we've decided to stop. */
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainenbool master_service_is_killed(struct master_service *service);
57397188558fcd1a9e24dbbbd2952eac9c45c20dTimo Sirainen/* Returns TRUE if our master process is already stopped. This process may or
03baa1c4c51f7b08fb285e82b528fcb00ac09ebfTimo Sirainen may not be dying itself. Returns FALSE always if the process was started
03baa1c4c51f7b08fb285e82b528fcb00ac09ebfTimo Sirainen standalone. */
57397188558fcd1a9e24dbbbd2952eac9c45c20dTimo Sirainenbool master_service_is_master_stopped(struct master_service *service);
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen/* Send command to anvil process, if we have fd to it. */
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainenvoid master_service_anvil_send(struct master_service *service, const char *cmd);
db693bf6fcae96d834567f1782257517b7207655Timo Sirainen/* Call to accept the client connection. Otherwise the connection is closed. */
db693bf6fcae96d834567f1782257517b7207655Timo Sirainenvoid master_service_client_connection_accept(struct master_service_connection *conn);
21e6b4fd844fd074583b17f09e1f27b9835ee238Timo Sirainen/* Used to create "extra client connections" outside the common accept()
21e6b4fd844fd074583b17f09e1f27b9835ee238Timo Sirainenvoid master_service_client_connection_created(struct master_service *service);
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen/* Call whenever a client connection is destroyed. */
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainenvoid master_service_client_connection_destroyed(struct master_service *service);
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen/* Deinitialize the service. */
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainenvoid master_service_deinit(struct master_service **service);
f158d9a303bb15a6848ca276c9391c7ca52e452bTimo Sirainen/* Returns TRUE if line contains compatible service name and major version.
f158d9a303bb15a6848ca276c9391c7ca52e452bTimo Sirainen The line is expected to be in format:
f158d9a303bb15a6848ca276c9391c7ca52e452bTimo Sirainen VERSION <tab> service_name <tab> major version <tab> minor version */
f158d9a303bb15a6848ca276c9391c7ca52e452bTimo Sirainenbool version_string_verify(const char *line, const char *service_name,
6303f32ad4af9cb08794561e6324df1c6c5fb637Timo Sirainen/* Same as version_string_verify(), but return the minor version. */
6303f32ad4af9cb08794561e6324df1c6c5fb637Timo Sirainenbool version_string_verify_full(const char *line, const char *service_name,
6303f32ad4af9cb08794561e6324df1c6c5fb637Timo Sirainen unsigned int *minor_version_r);
754896551f0422cda5d78500b26700eec5343c5bAki Tuomi/* Returns TRUE if ssl module has been loaded */