main.c revision 4403bc18add3e8a1392c9af8354e2c8430c47aa0
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek/* Copyright (C) 2002 Timo Sirainen */
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozekstatic const char *configfile = SYSCONFDIR "/" PACKAGE ".conf";
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozekint validate_str(const char *str, size_t max_len)
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek for (i = 0; i < max_len; i++) {
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek /* remove all environment, we don't need them */
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek /* set the failure log */
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek env_put(t_strconcat("IMAP_LOGFILE=", set_log_path, NULL));
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek env_put(t_strconcat("IMAP_LOGSTAMP=", set_log_timestamp, NULL));
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozekstatic void settings_reload(void)
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek i_warning("SIGHUP received - reloading configuration");
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek /* restart auth and login processes */
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozekstatic const char *get_exit_status_message(enum fatal_exit_status status)
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek return "Can't open log file";
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek return "Can't write to log file";
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek return "Internal logging error";
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek return "Out of memory";
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek return "exec() failed";
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozekstatic void timeout_handler(void *context __attr_unused__,
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek /* get the type and remove from hash */
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek /* write errors to syslog */
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek process_type_name = process_names[process_type];
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek if (pid == -1 && errno != EINTR && errno != ECHILD)
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozekstatic struct ip_addr *resolve_ip(const char *name)
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek /* IPv4 any */
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek /* IPv6 any */
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek /* Return the first IP if there happens to be multiple. */
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek ret = net_gethostbyname(name, &ip, &ips_count);
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozekstatic void open_fds(void)
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek if (imaps_ip == NULL && set_imaps_listen == NULL)
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek i_fatal("listen(%d) failed: %m", set_imap_port);
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek imaps_fd = set_ssl_disable || set_imaps_port == 0 ? dup(null_fd) :
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek i_fatal("listen(%d) failed: %m", set_imaps_port);
static void open_logfile(void)
static void main_init(void)
open_logfile();
ssl_init();
static void main_deinit(void)
if (lib_signal_kill != 0)
ssl_deinit();
closelog();
static void daemonize(void)
if (pid < 0)
if (pid != 0)
_exit(0);
if (setsid() < 0)
static void print_help(void)
lib_init();
print_help();
open_fds();
if (!foreground)
daemonize();
main_init();
main_deinit();
lib_deinit();