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