Mercurial > dovecot > original-hg > dovecot-1.2
annotate src/master/main.c @ 805:5ac361acb316 HEAD
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
cleanups and a couple of minor bugfixes.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Wed, 18 Dec 2002 17:15:41 +0200 |
parents | 86224ff16bf6 |
children | 35abd7a5d381 |
rev | line source |
---|---|
0 | 1 /* Copyright (C) 2002 Timo Sirainen */ |
2 | |
3 #include "common.h" | |
4 #include "ioloop.h" | |
5 #include "lib-signals.h" | |
6 #include "network.h" | |
697
7814b29d0862
Created env_put() and env_clean() for a bit easier handling of environment
Timo Sirainen <tss@iki.fi>
parents:
635
diff
changeset
|
7 #include "env-util.h" |
727
8dd8ebe6bcac
We use close-on-exec flag now to make sure that master process closes the
Timo Sirainen <tss@iki.fi>
parents:
697
diff
changeset
|
8 #include "fd-close-on-exec.h" |
0 | 9 |
10 #include "auth-process.h" | |
11 #include "login-process.h" | |
615
0d852af6842e
Master process generates DH/RSA parameters now and stores them into file
Timo Sirainen <tss@iki.fi>
parents:
610
diff
changeset
|
12 #include "ssl-init.h" |
0 | 13 |
279
49f4b0be0d87
Print usage with invalid parameters.
Timo Sirainen <tss@iki.fi>
parents:
50
diff
changeset
|
14 #include <stdio.h> |
0 | 15 #include <stdlib.h> |
16 #include <unistd.h> | |
17 #include <fcntl.h> | |
18 #include <syslog.h> | |
19 #include <sys/stat.h> | |
20 #include <sys/wait.h> | |
21 | |
22 const char *process_names[PROCESS_TYPE_MAX] = { | |
23 "unknown", | |
24 "auth", | |
25 "login", | |
615
0d852af6842e
Master process generates DH/RSA parameters now and stores them into file
Timo Sirainen <tss@iki.fi>
parents:
610
diff
changeset
|
26 "imap", |
0d852af6842e
Master process generates DH/RSA parameters now and stores them into file
Timo Sirainen <tss@iki.fi>
parents:
610
diff
changeset
|
27 "ssl-param" |
0 | 28 }; |
29 | |
635
90a65c017bf0
SIGHUP reloads now settings. Logged in clients are left untouched, but
Timo Sirainen <tss@iki.fi>
parents:
615
diff
changeset
|
30 static const char *configfile = SYSCONFDIR "/" PACKAGE ".conf"; |
0 | 31 static IOLoop ioloop; |
635
90a65c017bf0
SIGHUP reloads now settings. Logged in clients are left untouched, but
Timo Sirainen <tss@iki.fi>
parents:
615
diff
changeset
|
32 static Timeout to; |
0 | 33 |
34 HashTable *pids; | |
35 int null_fd, imap_fd, imaps_fd; | |
36 | |
37 int validate_str(const char *str, int max_len) | |
38 { | |
39 int i; | |
40 | |
41 for (i = 0; i < max_len; i++) { | |
42 if (str[i] == '\0') | |
43 return TRUE; | |
44 } | |
45 | |
46 return FALSE; | |
47 } | |
48 | |
49 void clean_child_process(void) | |
50 { | |
51 /* remove all environment, we don't need them */ | |
697
7814b29d0862
Created env_put() and env_clean() for a bit easier handling of environment
Timo Sirainen <tss@iki.fi>
parents:
635
diff
changeset
|
52 env_clean(); |
0 | 53 |
54 /* set the failure log */ | |
697
7814b29d0862
Created env_put() and env_clean() for a bit easier handling of environment
Timo Sirainen <tss@iki.fi>
parents:
635
diff
changeset
|
55 if (set_log_path != NULL) |
7814b29d0862
Created env_put() and env_clean() for a bit easier handling of environment
Timo Sirainen <tss@iki.fi>
parents:
635
diff
changeset
|
56 env_put(t_strconcat("IMAP_LOGFILE=", set_log_path, NULL)); |
801
86224ff16bf6
Drop root privileges earlier. Close syslog more later in imap-master when
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
57 else |
86224ff16bf6
Drop root privileges earlier. Close syslog more later in imap-master when
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
58 env_put("IMAP_USE_SYSLOG=1"); |
86224ff16bf6
Drop root privileges earlier. Close syslog more later in imap-master when
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
59 |
697
7814b29d0862
Created env_put() and env_clean() for a bit easier handling of environment
Timo Sirainen <tss@iki.fi>
parents:
635
diff
changeset
|
60 if (set_log_timestamp != NULL) |
7814b29d0862
Created env_put() and env_clean() for a bit easier handling of environment
Timo Sirainen <tss@iki.fi>
parents:
635
diff
changeset
|
61 env_put(t_strconcat("IMAP_LOGSTAMP=", set_log_timestamp, NULL)); |
0 | 62 } |
63 | |
64 static void sig_quit(int signo __attr_unused__) | |
65 { | |
66 io_loop_stop(ioloop); | |
67 } | |
68 | |
635
90a65c017bf0
SIGHUP reloads now settings. Logged in clients are left untouched, but
Timo Sirainen <tss@iki.fi>
parents:
615
diff
changeset
|
69 static void settings_reload(void) |
90a65c017bf0
SIGHUP reloads now settings. Logged in clients are left untouched, but
Timo Sirainen <tss@iki.fi>
parents:
615
diff
changeset
|
70 { |
90a65c017bf0
SIGHUP reloads now settings. Logged in clients are left untouched, but
Timo Sirainen <tss@iki.fi>
parents:
615
diff
changeset
|
71 i_warning("SIGHUP received - reloading configuration"); |
90a65c017bf0
SIGHUP reloads now settings. Logged in clients are left untouched, but
Timo Sirainen <tss@iki.fi>
parents:
615
diff
changeset
|
72 |
90a65c017bf0
SIGHUP reloads now settings. Logged in clients are left untouched, but
Timo Sirainen <tss@iki.fi>
parents:
615
diff
changeset
|
73 settings_read(configfile); |
90a65c017bf0
SIGHUP reloads now settings. Logged in clients are left untouched, but
Timo Sirainen <tss@iki.fi>
parents:
615
diff
changeset
|
74 |
90a65c017bf0
SIGHUP reloads now settings. Logged in clients are left untouched, but
Timo Sirainen <tss@iki.fi>
parents:
615
diff
changeset
|
75 /* restart auth and login processes */ |
90a65c017bf0
SIGHUP reloads now settings. Logged in clients are left untouched, but
Timo Sirainen <tss@iki.fi>
parents:
615
diff
changeset
|
76 login_processes_destroy_all(); |
90a65c017bf0
SIGHUP reloads now settings. Logged in clients are left untouched, but
Timo Sirainen <tss@iki.fi>
parents:
615
diff
changeset
|
77 auth_processes_destroy_all(); |
90a65c017bf0
SIGHUP reloads now settings. Logged in clients are left untouched, but
Timo Sirainen <tss@iki.fi>
parents:
615
diff
changeset
|
78 } |
90a65c017bf0
SIGHUP reloads now settings. Logged in clients are left untouched, but
Timo Sirainen <tss@iki.fi>
parents:
615
diff
changeset
|
79 |
801
86224ff16bf6
Drop root privileges earlier. Close syslog more later in imap-master when
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
80 static const char *get_exit_status_message(FatalExitStatus status) |
86224ff16bf6
Drop root privileges earlier. Close syslog more later in imap-master when
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
81 { |
86224ff16bf6
Drop root privileges earlier. Close syslog more later in imap-master when
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
82 switch (status) { |
86224ff16bf6
Drop root privileges earlier. Close syslog more later in imap-master when
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
83 case FATAL_LOGOPEN: |
86224ff16bf6
Drop root privileges earlier. Close syslog more later in imap-master when
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
84 return "Can't open log file"; |
86224ff16bf6
Drop root privileges earlier. Close syslog more later in imap-master when
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
85 case FATAL_LOGWRITE: |
86224ff16bf6
Drop root privileges earlier. Close syslog more later in imap-master when
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
86 return "Can't write to log file"; |
86224ff16bf6
Drop root privileges earlier. Close syslog more later in imap-master when
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
87 case FATAL_OUTOFMEM: |
86224ff16bf6
Drop root privileges earlier. Close syslog more later in imap-master when
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
88 return "Out of memory"; |
86224ff16bf6
Drop root privileges earlier. Close syslog more later in imap-master when
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
89 case FATAL_EXEC: |
86224ff16bf6
Drop root privileges earlier. Close syslog more later in imap-master when
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
90 return "exec() failed"; |
86224ff16bf6
Drop root privileges earlier. Close syslog more later in imap-master when
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
91 |
86224ff16bf6
Drop root privileges earlier. Close syslog more later in imap-master when
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
92 case FATAL_DEFAULT: |
86224ff16bf6
Drop root privileges earlier. Close syslog more later in imap-master when
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
93 return NULL; |
86224ff16bf6
Drop root privileges earlier. Close syslog more later in imap-master when
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
94 } |
86224ff16bf6
Drop root privileges earlier. Close syslog more later in imap-master when
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
95 |
86224ff16bf6
Drop root privileges earlier. Close syslog more later in imap-master when
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
96 return NULL; |
86224ff16bf6
Drop root privileges earlier. Close syslog more later in imap-master when
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
97 } |
86224ff16bf6
Drop root privileges earlier. Close syslog more later in imap-master when
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
98 |
635
90a65c017bf0
SIGHUP reloads now settings. Logged in clients are left untouched, but
Timo Sirainen <tss@iki.fi>
parents:
615
diff
changeset
|
99 static void timeout_handler(void *context __attr_unused__, |
90a65c017bf0
SIGHUP reloads now settings. Logged in clients are left untouched, but
Timo Sirainen <tss@iki.fi>
parents:
615
diff
changeset
|
100 Timeout timeout __attr_unused__) |
0 | 101 { |
801
86224ff16bf6
Drop root privileges earlier. Close syslog more later in imap-master when
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
102 const char *process_type_name, *msg; |
0 | 103 pid_t pid; |
104 int status, process_type; | |
105 | |
635
90a65c017bf0
SIGHUP reloads now settings. Logged in clients are left untouched, but
Timo Sirainen <tss@iki.fi>
parents:
615
diff
changeset
|
106 if (lib_signal_hup != 0) { |
90a65c017bf0
SIGHUP reloads now settings. Logged in clients are left untouched, but
Timo Sirainen <tss@iki.fi>
parents:
615
diff
changeset
|
107 settings_reload(); |
90a65c017bf0
SIGHUP reloads now settings. Logged in clients are left untouched, but
Timo Sirainen <tss@iki.fi>
parents:
615
diff
changeset
|
108 lib_signal_hup = 0; |
90a65c017bf0
SIGHUP reloads now settings. Logged in clients are left untouched, but
Timo Sirainen <tss@iki.fi>
parents:
615
diff
changeset
|
109 } |
90a65c017bf0
SIGHUP reloads now settings. Logged in clients are left untouched, but
Timo Sirainen <tss@iki.fi>
parents:
615
diff
changeset
|
110 |
0 | 111 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) { |
112 /* get the type and remove from hash */ | |
113 process_type = PID_GET_PROCESS_TYPE(pid); | |
114 PID_REMOVE_PROCESS_TYPE(pid); | |
115 | |
116 if (process_type == PROCESS_TYPE_IMAP) | |
117 imap_process_destroyed(pid); | |
615
0d852af6842e
Master process generates DH/RSA parameters now and stores them into file
Timo Sirainen <tss@iki.fi>
parents:
610
diff
changeset
|
118 if (process_type == PROCESS_TYPE_SSL_PARAM) |
0d852af6842e
Master process generates DH/RSA parameters now and stores them into file
Timo Sirainen <tss@iki.fi>
parents:
610
diff
changeset
|
119 ssl_parameter_process_destroyed(pid); |
0 | 120 |
121 /* write errors to syslog */ | |
122 process_type_name = process_names[process_type]; | |
123 if (WIFEXITED(status)) { | |
124 status = WEXITSTATUS(status); | |
125 if (status != 0) { | |
615
0d852af6842e
Master process generates DH/RSA parameters now and stores them into file
Timo Sirainen <tss@iki.fi>
parents:
610
diff
changeset
|
126 login_process_abormal_exit(pid); |
801
86224ff16bf6
Drop root privileges earlier. Close syslog more later in imap-master when
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
127 msg = get_exit_status_message(status); |
86224ff16bf6
Drop root privileges earlier. Close syslog more later in imap-master when
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
128 if (msg != NULL) |
86224ff16bf6
Drop root privileges earlier. Close syslog more later in imap-master when
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
129 msg = t_strconcat(" (", msg, ")", NULL); |
86224ff16bf6
Drop root privileges earlier. Close syslog more later in imap-master when
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
130 i_error("child %d (%s) returned error %d%s", |
86224ff16bf6
Drop root privileges earlier. Close syslog more later in imap-master when
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
131 (int)pid, process_type_name, |
86224ff16bf6
Drop root privileges earlier. Close syslog more later in imap-master when
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
132 status, msg); |
0 | 133 } |
134 } else if (WIFSIGNALED(status)) { | |
615
0d852af6842e
Master process generates DH/RSA parameters now and stores them into file
Timo Sirainen <tss@iki.fi>
parents:
610
diff
changeset
|
135 login_process_abormal_exit(pid); |
0 | 136 i_error("child %d (%s) killed with signal %d", |
137 (int)pid, process_type_name, WTERMSIG(status)); | |
138 } | |
139 } | |
140 | |
141 if (pid == -1 && errno != EINTR && errno != ECHILD) | |
142 i_warning("waitpid() failed: %m"); | |
143 } | |
144 | |
145 static IPADDR *resolve_ip(const char *name) | |
146 { | |
147 IPADDR *ip; | |
148 int ret, ips_count; | |
149 | |
36 | 150 if (name == NULL || *name == '\0') |
780
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
740
diff
changeset
|
151 return NULL; /* defaults to "*" or "::" */ |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
740
diff
changeset
|
152 |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
740
diff
changeset
|
153 if (strcmp(name, "*") == 0) { |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
740
diff
changeset
|
154 /* IPv4 any */ |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
740
diff
changeset
|
155 ip = t_new(IPADDR, 1); |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
740
diff
changeset
|
156 net_get_ip_any4(ip); |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
740
diff
changeset
|
157 return ip; |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
740
diff
changeset
|
158 } |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
740
diff
changeset
|
159 |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
740
diff
changeset
|
160 if (strcmp(name, "::") == 0) { |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
740
diff
changeset
|
161 /* IPv6 any */ |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
740
diff
changeset
|
162 ip = t_new(IPADDR, 1); |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
740
diff
changeset
|
163 net_get_ip_any6(ip); |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
740
diff
changeset
|
164 return ip; |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
740
diff
changeset
|
165 } |
0 | 166 |
805
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
801
diff
changeset
|
167 /* Return the first IP if there happens to be multiple. */ |
0 | 168 ret = net_gethostbyname(name, &ip, &ips_count); |
805
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
801
diff
changeset
|
169 if (ret != 0) { |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
801
diff
changeset
|
170 i_fatal("Can't resolve address %s: %s", |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
801
diff
changeset
|
171 name, net_gethosterror(ret)); |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
801
diff
changeset
|
172 } |
0 | 173 |
174 if (ips_count < 1) | |
36 | 175 i_fatal("No IPs for address: %s", name); |
0 | 176 |
177 return ip; | |
178 } | |
179 | |
180 static void open_fds(void) | |
181 { | |
182 IPADDR *imap_ip, *imaps_ip; | |
183 | |
184 imap_ip = resolve_ip(set_imap_listen); | |
185 imaps_ip = resolve_ip(set_imaps_listen); | |
186 | |
187 if (imaps_ip == NULL && set_imaps_listen == NULL) | |
188 imaps_ip = imap_ip; | |
189 | |
190 null_fd = open("/dev/null", O_RDONLY); | |
191 if (null_fd == -1) | |
192 i_fatal("Can't open /dev/null: %m"); | |
727
8dd8ebe6bcac
We use close-on-exec flag now to make sure that master process closes the
Timo Sirainen <tss@iki.fi>
parents:
697
diff
changeset
|
193 fd_close_on_exec(null_fd, TRUE); |
0 | 194 |
195 imap_fd = set_imap_port == 0 ? dup(null_fd) : | |
196 net_listen(imap_ip, &set_imap_port); | |
635
90a65c017bf0
SIGHUP reloads now settings. Logged in clients are left untouched, but
Timo Sirainen <tss@iki.fi>
parents:
615
diff
changeset
|
197 if (imap_fd == -1) |
468 | 198 i_fatal("listen(%d) failed: %m", set_imap_port); |
727
8dd8ebe6bcac
We use close-on-exec flag now to make sure that master process closes the
Timo Sirainen <tss@iki.fi>
parents:
697
diff
changeset
|
199 fd_close_on_exec(imap_fd, TRUE); |
0 | 200 |
575
ba6f89be8237
Never open imaps port if we're building with SSL disabled.
Timo Sirainen <tss@iki.fi>
parents:
468
diff
changeset
|
201 #ifdef HAVE_SSL |
740
3eae69eb6628
If ssl_disabled = yes, we shouldn't listen in imaps port.
Timo Sirainen <tss@iki.fi>
parents:
727
diff
changeset
|
202 imaps_fd = set_ssl_disable || set_imaps_port == 0 ? dup(null_fd) : |
0 | 203 net_listen(imaps_ip, &set_imaps_port); |
575
ba6f89be8237
Never open imaps port if we're building with SSL disabled.
Timo Sirainen <tss@iki.fi>
parents:
468
diff
changeset
|
204 #else |
ba6f89be8237
Never open imaps port if we're building with SSL disabled.
Timo Sirainen <tss@iki.fi>
parents:
468
diff
changeset
|
205 imaps_fd = dup(null_fd); |
ba6f89be8237
Never open imaps port if we're building with SSL disabled.
Timo Sirainen <tss@iki.fi>
parents:
468
diff
changeset
|
206 #endif |
635
90a65c017bf0
SIGHUP reloads now settings. Logged in clients are left untouched, but
Timo Sirainen <tss@iki.fi>
parents:
615
diff
changeset
|
207 if (imaps_fd == -1) |
0 | 208 i_fatal("listen(%d) failed: %m", set_imaps_port); |
727
8dd8ebe6bcac
We use close-on-exec flag now to make sure that master process closes the
Timo Sirainen <tss@iki.fi>
parents:
697
diff
changeset
|
209 fd_close_on_exec(imaps_fd, TRUE); |
0 | 210 } |
211 | |
801
86224ff16bf6
Drop root privileges earlier. Close syslog more later in imap-master when
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
212 static void open_logfile(void) |
86224ff16bf6
Drop root privileges earlier. Close syslog more later in imap-master when
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
213 { |
86224ff16bf6
Drop root privileges earlier. Close syslog more later in imap-master when
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
214 if (set_log_path == NULL) |
86224ff16bf6
Drop root privileges earlier. Close syslog more later in imap-master when
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
215 i_set_failure_syslog("imap-master", LOG_NDELAY, LOG_MAIL); |
86224ff16bf6
Drop root privileges earlier. Close syslog more later in imap-master when
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
216 else { |
86224ff16bf6
Drop root privileges earlier. Close syslog more later in imap-master when
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
217 /* log to file or stderr */ |
86224ff16bf6
Drop root privileges earlier. Close syslog more later in imap-master when
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
218 i_set_failure_file(set_log_path, "imap-master"); |
86224ff16bf6
Drop root privileges earlier. Close syslog more later in imap-master when
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
219 i_set_failure_timestamp_format(set_log_timestamp); |
86224ff16bf6
Drop root privileges earlier. Close syslog more later in imap-master when
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
220 } |
86224ff16bf6
Drop root privileges earlier. Close syslog more later in imap-master when
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
221 } |
86224ff16bf6
Drop root privileges earlier. Close syslog more later in imap-master when
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
222 |
50
d493b9cc265e
Introduced uoff_t which is the unsigned-equilevant of off_t. This was needed
Timo Sirainen <tss@iki.fi>
parents:
37
diff
changeset
|
223 static void main_init(void) |
0 | 224 { |
225 /* deny file access from everyone else except owner */ | |
226 (void)umask(0077); | |
227 | |
801
86224ff16bf6
Drop root privileges earlier. Close syslog more later in imap-master when
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
228 open_logfile(); |
0 | 229 |
801
86224ff16bf6
Drop root privileges earlier. Close syslog more later in imap-master when
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
230 lib_init_signals(sig_quit); |
0 | 231 |
232 pids = hash_create(default_pool, 128, NULL, NULL); | |
635
90a65c017bf0
SIGHUP reloads now settings. Logged in clients are left untouched, but
Timo Sirainen <tss@iki.fi>
parents:
615
diff
changeset
|
233 to = timeout_add(100, timeout_handler, NULL); |
0 | 234 |
615
0d852af6842e
Master process generates DH/RSA parameters now and stores them into file
Timo Sirainen <tss@iki.fi>
parents:
610
diff
changeset
|
235 ssl_init(); |
0 | 236 auth_processes_init(); |
237 login_processes_init(); | |
238 } | |
239 | |
240 static void main_deinit(void) | |
241 { | |
242 if (lib_signal_kill != 0) | |
243 i_warning("Killed with signal %d", lib_signal_kill); | |
244 | |
245 login_processes_deinit(); | |
246 auth_processes_deinit(); | |
615
0d852af6842e
Master process generates DH/RSA parameters now and stores them into file
Timo Sirainen <tss@iki.fi>
parents:
610
diff
changeset
|
247 ssl_deinit(); |
0 | 248 |
635
90a65c017bf0
SIGHUP reloads now settings. Logged in clients are left untouched, but
Timo Sirainen <tss@iki.fi>
parents:
615
diff
changeset
|
249 timeout_remove(to); |
0 | 250 |
805
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
801
diff
changeset
|
251 if (close(null_fd) < 0) |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
801
diff
changeset
|
252 i_error("close(null_fd) failed: %m"); |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
801
diff
changeset
|
253 if (close(imap_fd) < 0) |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
801
diff
changeset
|
254 i_error("close(imap_fd) failed: %m"); |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
801
diff
changeset
|
255 if (close(imaps_fd) < 0) |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
801
diff
changeset
|
256 i_error("close(imaps_fd) failed: %m"); |
0 | 257 |
258 hash_destroy(pids); | |
259 closelog(); | |
260 } | |
261 | |
262 static void daemonize(void) | |
263 { | |
264 pid_t pid; | |
265 | |
266 pid = fork(); | |
267 if (pid < 0) | |
268 i_fatal("fork() failed: %m"); | |
269 | |
270 if (pid != 0) | |
271 _exit(0); | |
610 | 272 |
801
86224ff16bf6
Drop root privileges earlier. Close syslog more later in imap-master when
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
273 if (setsid() < 0) |
86224ff16bf6
Drop root privileges earlier. Close syslog more later in imap-master when
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
274 i_fatal("setsid() failed: %m"); |
0 | 275 } |
276 | |
279
49f4b0be0d87
Print usage with invalid parameters.
Timo Sirainen <tss@iki.fi>
parents:
50
diff
changeset
|
277 static void print_help(void) |
49f4b0be0d87
Print usage with invalid parameters.
Timo Sirainen <tss@iki.fi>
parents:
50
diff
changeset
|
278 { |
49f4b0be0d87
Print usage with invalid parameters.
Timo Sirainen <tss@iki.fi>
parents:
50
diff
changeset
|
279 printf("Usage: imap-master [-F] [-c <config file>]\n"); |
49f4b0be0d87
Print usage with invalid parameters.
Timo Sirainen <tss@iki.fi>
parents:
50
diff
changeset
|
280 } |
49f4b0be0d87
Print usage with invalid parameters.
Timo Sirainen <tss@iki.fi>
parents:
50
diff
changeset
|
281 |
0 | 282 int main(int argc, char *argv[]) |
283 { | |
284 /* parse arguments */ | |
285 int foreground = FALSE; | |
286 int i; | |
287 | |
288 lib_init(); | |
289 | |
290 for (i = 1; i < argc; i++) { | |
291 if (strcmp(argv[i], "-F") == 0) { | |
292 /* foreground */ | |
293 foreground = TRUE; | |
294 } else if (strcmp(argv[i], "-c") == 0) { | |
295 /* config file */ | |
296 i++; | |
297 if (i == argc) i_fatal("Missing config file argument"); | |
298 configfile = argv[i]; | |
299 } else { | |
279
49f4b0be0d87
Print usage with invalid parameters.
Timo Sirainen <tss@iki.fi>
parents:
50
diff
changeset
|
300 print_help(); |
0 | 301 i_fatal("Unknown argument: %s", argv[1]); |
302 } | |
303 } | |
304 | |
305 /* read and verify settings before forking */ | |
635
90a65c017bf0
SIGHUP reloads now settings. Logged in clients are left untouched, but
Timo Sirainen <tss@iki.fi>
parents:
615
diff
changeset
|
306 settings_init(); |
0 | 307 settings_read(configfile); |
308 open_fds(); | |
309 | |
310 if (!foreground) | |
311 daemonize(); | |
312 | |
389
60040a9d243f
ioloop_create() takes now pool-parameter. io_buffer_create_mmaped() takes
Timo Sirainen <tss@iki.fi>
parents:
279
diff
changeset
|
313 ioloop = io_loop_create(system_pool); |
0 | 314 |
35
b420373f88f6
Added log_path and log_timestamp settings to config file. Removed -l command
Timo Sirainen <tss@iki.fi>
parents:
10
diff
changeset
|
315 main_init(); |
0 | 316 io_loop_run(ioloop); |
317 main_deinit(); | |
318 | |
319 io_loop_destroy(ioloop); | |
320 lib_deinit(); | |
321 | |
322 return 0; | |
323 } |