main.c revision 647afe054117804e3e156b46ada4c5bec0ac4c38
286N/A/* Copyright (C) 2002 Timo Sirainen */
286N/A
286N/A#include "common.h"
286N/A#include "ioloop.h"
286N/A#include "lib-signals.h"
286N/A#include "restrict-access.h"
286N/A
286N/A#include <stdlib.h>
286N/A#include <syslog.h>
286N/A
286N/AIOLoop ioloop;
286N/Astatic char log_prefix[64];
286N/A
286N/Astatic void sig_quit(int signo __attr_unused__)
286N/A{
286N/A io_loop_stop(ioloop);
286N/A}
286N/A
286N/Astatic void main_init(int use_syslog)
286N/A{
286N/A Client *client;
286N/A MailStorage *storage;
286N/A const char *logfile, *mail, *tag;
286N/A
570N/A lib_init_signals(sig_quit);
286N/A
286N/A i_snprintf(log_prefix, sizeof(log_prefix), "imap(%s)", getenv("USER"));
286N/A
286N/A logfile = getenv("IMAP_LOGFILE");
286N/A if (logfile != NULL) {
286N/A /* log failures into specified log file */
286N/A i_set_failure_file(logfile, log_prefix);
286N/A i_set_failure_timestamp_format(getenv("IMAP_LOGSTAMP"));
286N/A } else if (use_syslog) {
286N/A /* prefix with imapd(user) */
286N/A openlog(log_prefix, 0, LOG_MAIL);
286N/A
286N/A i_set_panic_handler(i_syslog_panic_handler);
286N/A i_set_fatal_handler(i_syslog_fatal_handler);
286N/A i_set_error_handler(i_syslog_error_handler);
286N/A i_set_warning_handler(i_syslog_warning_handler);
286N/A }
286N/A
286N/A /* do the chrooting etc. */
286N/A restrict_access_by_env();
286N/A
286N/A mail_storage_register_all();
286N/A clients_init();
286N/A
286N/A mail = getenv("MAIL");
286N/A if (mail == NULL) {
286N/A /* support also maildir-specific environment */
286N/A mail = getenv("MAILDIR");
286N/A if (mail != NULL)
286N/A mail = t_strconcat("maildir:", mail, NULL);
286N/A }
286N/A
286N/A storage = mail_storage_create_with_data(mail, getenv("USER"));
286N/A if (storage == NULL) {
286N/A /* failed */
286N/A if (mail != NULL && *mail != '\0')
286N/A i_fatal("Failed to create storage with data: %s", mail);
286N/A else {
286N/A const char *home;
286N/A
286N/A home = getenv("HOME");
286N/A if (home == NULL) home = "not set";
286N/A
286N/A i_fatal("MAIL environment missing and "
286N/A "autodetection failed (home %s)", home);
286N/A }
286N/A } else {
286N/A client = client_create(0, 1, FALSE, storage);
286N/A
286N/A tag = getenv("LOGIN_TAG");
286N/A if (tag == NULL || *tag == '\0')
286N/A tag = "*";
286N/A
286N/A client_send_line(client, t_strconcat(tag, " OK Logged in.",
286N/A NULL));
286N/A }
286N/A}
286N/A
286N/Astatic void main_deinit(void)
286N/A{
286N/A /* warn about being killed because of some signal, except SIGINT (^C)
286N/A which is too common at least while testing :) */
286N/A if (lib_signal_kill != 0 && lib_signal_kill != 2)
286N/A i_warning("Killed with signal %d", lib_signal_kill);
286N/A
286N/A clients_deinit();
286N/A
286N/A closelog();
286N/A}
286N/A
286N/Aint main(int argc, char *argv[])
286N/A{
286N/A /* NOTE: we start rooted, so keep the code minimal until
286N/A restrict_access_by_env() is called */
286N/A lib_init();
286N/A ioloop = io_loop_create(system_pool);
286N/A
286N/A main_init(argc == 2 && strcmp(argv[1], "-s") == 0);
286N/A io_loop_run(ioloop);
286N/A main_deinit();
286N/A
286N/A io_loop_destroy(ioloop);
286N/A lib_deinit();
286N/A
286N/A return 0;
286N/A}
286N/A