Postfix3.3.1
qmgr.c
[詳解]
1 /*++
2 /* NAME
3 /* qmgr 8
4 /* SUMMARY
5 /* Postfix queue manager
6 /* SYNOPSIS
7 /* \fBqmgr\fR [generic Postfix daemon options]
8 /* DESCRIPTION
9 /* The \fBqmgr\fR(8) daemon awaits the arrival of incoming mail
10 /* and arranges for its delivery via Postfix delivery processes.
11 /* The actual mail routing strategy is delegated to the
12 /* \fBtrivial-rewrite\fR(8) daemon.
13 /* This program expects to be run from the \fBmaster\fR(8) process
14 /* manager.
15 /*
16 /* Mail addressed to the local \fBdouble-bounce\fR address is
17 /* logged and discarded. This stops potential loops caused by
18 /* undeliverable bounce notifications.
19 /* MAIL QUEUES
20 /* .ad
21 /* .fi
22 /* The \fBqmgr\fR(8) daemon maintains the following queues:
23 /* .IP \fBincoming\fR
24 /* Inbound mail from the network, or mail picked up by the
25 /* local \fBpickup\fR(8) daemon from the \fBmaildrop\fR directory.
26 /* .IP \fBactive\fR
27 /* Messages that the queue manager has opened for delivery. Only
28 /* a limited number of messages is allowed to enter the \fBactive\fR
29 /* queue (leaky bucket strategy, for a fixed delivery rate).
30 /* .IP \fBdeferred\fR
31 /* Mail that could not be delivered upon the first attempt. The queue
32 /* manager implements exponential backoff by doubling the time between
33 /* delivery attempts.
34 /* .IP \fBcorrupt\fR
35 /* Unreadable or damaged queue files are moved here for inspection.
36 /* .IP \fBhold\fR
37 /* Messages that are kept "on hold" are kept here until someone
38 /* sets them free.
39 /* DELIVERY STATUS REPORTS
40 /* .ad
41 /* .fi
42 /* The \fBqmgr\fR(8) daemon keeps an eye on per-message delivery status
43 /* reports in the following directories. Each status report file has
44 /* the same name as the corresponding message file:
45 /* .IP \fBbounce\fR
46 /* Per-recipient status information about why mail is bounced.
47 /* These files are maintained by the \fBbounce\fR(8) daemon.
48 /* .IP \fBdefer\fR
49 /* Per-recipient status information about why mail is delayed.
50 /* These files are maintained by the \fBdefer\fR(8) daemon.
51 /* .IP \fBtrace\fR
52 /* Per-recipient status information as requested with the
53 /* Postfix "\fBsendmail -v\fR" or "\fBsendmail -bv\fR" command.
54 /* These files are maintained by the \fBtrace\fR(8) daemon.
55 /* .PP
56 /* The \fBqmgr\fR(8) daemon is responsible for asking the
57 /* \fBbounce\fR(8), \fBdefer\fR(8) or \fBtrace\fR(8) daemons to
58 /* send delivery reports.
59 /* STRATEGIES
60 /* .ad
61 /* .fi
62 /* The queue manager implements a variety of strategies for
63 /* either opening queue files (input) or for message delivery (output).
64 /* .IP "\fBleaky bucket\fR"
65 /* This strategy limits the number of messages in the \fBactive\fR queue
66 /* and prevents the queue manager from running out of memory under
67 /* heavy load.
68 /* .IP \fBfairness\fR
69 /* When the \fBactive\fR queue has room, the queue manager takes one
70 /* message from the \fBincoming\fR queue and one from the \fBdeferred\fR
71 /* queue. This prevents a large mail backlog from blocking the delivery
72 /* of new mail.
73 /* .IP "\fBslow start\fR"
74 /* This strategy eliminates "thundering herd" problems by slowly
75 /* adjusting the number of parallel deliveries to the same destination.
76 /* .IP "\fBround robin\fR"
77 /* The queue manager sorts delivery requests by destination.
78 /* Round-robin selection prevents one destination from dominating
79 /* deliveries to other destinations.
80 /* .IP "\fBexponential backoff\fR"
81 /* Mail that cannot be delivered upon the first attempt is deferred.
82 /* The time interval between delivery attempts is doubled after each
83 /* attempt.
84 /* .IP "\fBdestination status cache\fR"
85 /* The queue manager avoids unnecessary delivery attempts by
86 /* maintaining a short-term, in-memory list of unreachable destinations.
87 /* .IP "\fBpreemptive message scheduling\fR"
88 /* The queue manager attempts to minimize the average per-recipient delay
89 /* while still preserving the correct per-message delays, using
90 /* a sophisticated preemptive message scheduling.
91 /* TRIGGERS
92 /* .ad
93 /* .fi
94 /* On an idle system, the queue manager waits for the arrival of
95 /* trigger events, or it waits for a timer to go off. A trigger
96 /* is a one-byte message.
97 /* Depending on the message received, the queue manager performs
98 /* one of the following actions (the message is followed by the
99 /* symbolic constant used internally by the software):
100 /* .IP "\fBD (QMGR_REQ_SCAN_DEFERRED)\fR"
101 /* Start a deferred queue scan. If a deferred queue scan is already
102 /* in progress, that scan will be restarted as soon as it finishes.
103 /* .IP "\fBI (QMGR_REQ_SCAN_INCOMING)\fR"
104 /* Start an incoming queue scan. If an incoming queue scan is already
105 /* in progress, that scan will be restarted as soon as it finishes.
106 /* .IP "\fBA (QMGR_REQ_SCAN_ALL)\fR"
107 /* Ignore deferred queue file time stamps. The request affects
108 /* the next deferred queue scan.
109 /* .IP "\fBF (QMGR_REQ_FLUSH_DEAD)\fR"
110 /* Purge all information about dead transports and destinations.
111 /* .IP "\fBW (TRIGGER_REQ_WAKEUP)\fR"
112 /* Wakeup call, This is used by the master server to instantiate
113 /* servers that should not go away forever. The action is to start
114 /* an incoming queue scan.
115 /* .PP
116 /* The \fBqmgr\fR(8) daemon reads an entire buffer worth of triggers.
117 /* Multiple identical trigger requests are collapsed into one, and
118 /* trigger requests are sorted so that \fBA\fR and \fBF\fR precede
119 /* \fBD\fR and \fBI\fR. Thus, in order to force a deferred queue run,
120 /* one would request \fBA F D\fR; in order to notify the queue manager
121 /* of the arrival of new mail one would request \fBI\fR.
122 /* STANDARDS
123 /* RFC 3463 (Enhanced status codes)
124 /* RFC 3464 (Delivery status notifications)
125 /* SECURITY
126 /* .ad
127 /* .fi
128 /* The \fBqmgr\fR(8) daemon is not security sensitive. It reads
129 /* single-character messages from untrusted local users, and thus may
130 /* be susceptible to denial of service attacks. The \fBqmgr\fR(8) daemon
131 /* does not talk to the outside world, and it can be run at fixed low
132 /* privilege in a chrooted environment.
133 /* DIAGNOSTICS
134 /* Problems and transactions are logged to the syslog daemon.
135 /* Corrupted message files are saved to the \fBcorrupt\fR queue
136 /* for further inspection.
137 /*
138 /* Depending on the setting of the \fBnotify_classes\fR parameter,
139 /* the postmaster is notified of bounces and of other trouble.
140 /* BUGS
141 /* A single queue manager process has to compete for disk access with
142 /* multiple front-end processes such as \fBcleanup\fR(8). A sudden burst of
143 /* inbound mail can negatively impact outbound delivery rates.
144 /* CONFIGURATION PARAMETERS
145 /* .ad
146 /* .fi
147 /* Changes to \fBmain.cf\fR are not picked up automatically
148 /* as \fBqmgr\fR(8)
149 /* is a persistent process. Use the "\fBpostfix reload\fR" command after
150 /* a configuration change.
151 /*
152 /* The text below provides only a parameter summary. See
153 /* \fBpostconf\fR(5) for more details including examples.
154 /*
155 /* In the text below, \fItransport\fR is the first field in a
156 /* \fBmaster.cf\fR entry.
157 /* COMPATIBILITY CONTROLS
158 /* .ad
159 /* .fi
160 /* Available before Postfix version 2.5:
161 /* .IP "\fBallow_min_user (no)\fR"
162 /* Allow a sender or recipient address to have `-' as the first
163 /* character.
164 /* .PP
165 /* Available with Postfix version 2.7 and later:
166 /* .IP "\fBdefault_filter_nexthop (empty)\fR"
167 /* When a content_filter or FILTER request specifies no explicit
168 /* next-hop destination, use $default_filter_nexthop instead; when
169 /* that value is empty, use the domain in the recipient address.
170 /* ACTIVE QUEUE CONTROLS
171 /* .ad
172 /* .fi
173 /* .IP "\fBqmgr_clog_warn_time (300s)\fR"
174 /* The minimal delay between warnings that a specific destination is
175 /* clogging up the Postfix active queue.
176 /* .IP "\fBqmgr_message_active_limit (20000)\fR"
177 /* The maximal number of messages in the active queue.
178 /* .IP "\fBqmgr_message_recipient_limit (20000)\fR"
179 /* The maximal number of recipients held in memory by the Postfix
180 /* queue manager, and the maximal size of the short-term,
181 /* in-memory "dead" destination status cache.
182 /* .IP "\fBqmgr_message_recipient_minimum (10)\fR"
183 /* The minimal number of in-memory recipients for any message.
184 /* .IP "\fBdefault_recipient_limit (20000)\fR"
185 /* The default per-transport upper limit on the number of in-memory
186 /* recipients.
187 /* .IP "\fBtransport_recipient_limit ($default_recipient_limit)\fR"
188 /* A transport-specific override for the default_recipient_limit
189 /* parameter value, where \fItransport\fR is the master.cf name of
190 /* the message delivery transport.
191 /* .IP "\fBdefault_extra_recipient_limit (1000)\fR"
192 /* The default value for the extra per-transport limit imposed on the
193 /* number of in-memory recipients.
194 /* .IP "\fBtransport_extra_recipient_limit ($default_extra_recipient_limit)\fR"
195 /* A transport-specific override for the default_extra_recipient_limit
196 /* parameter value, where \fItransport\fR is the master.cf name of
197 /* the message delivery transport.
198 /* .PP
199 /* Available in Postfix version 2.4 and later:
200 /* .IP "\fBdefault_recipient_refill_limit (100)\fR"
201 /* The default per-transport limit on the number of recipients refilled at
202 /* once.
203 /* .IP "\fBtransport_recipient_refill_limit ($default_recipient_refill_limit)\fR"
204 /* A transport-specific override for the default_recipient_refill_limit
205 /* parameter value, where \fItransport\fR is the master.cf name of
206 /* the message delivery transport.
207 /* .IP "\fBdefault_recipient_refill_delay (5s)\fR"
208 /* The default per-transport maximum delay between recipients refills.
209 /* .IP "\fBtransport_recipient_refill_delay ($default_recipient_refill_delay)\fR"
210 /* A transport-specific override for the default_recipient_refill_delay
211 /* parameter value, where \fItransport\fR is the master.cf name of
212 /* the message delivery transport.
213 /* DELIVERY CONCURRENCY CONTROLS
214 /* .ad
215 /* .fi
216 /* .IP "\fBinitial_destination_concurrency (5)\fR"
217 /* The initial per-destination concurrency level for parallel delivery
218 /* to the same destination.
219 /* .IP "\fBdefault_destination_concurrency_limit (20)\fR"
220 /* The default maximal number of parallel deliveries to the same
221 /* destination.
222 /* .IP "\fBtransport_destination_concurrency_limit ($default_destination_concurrency_limit)\fR"
223 /* A transport-specific override for the
224 /* default_destination_concurrency_limit parameter value, where
225 /* \fItransport\fR is the master.cf name of the message delivery
226 /* transport.
227 /* .PP
228 /* Available in Postfix version 2.5 and later:
229 /* .IP "\fBtransport_initial_destination_concurrency ($initial_destination_concurrency)\fR"
230 /* A transport-specific override for the initial_destination_concurrency
231 /* parameter value, where \fItransport\fR is the master.cf name of
232 /* the message delivery transport.
233 /* .IP "\fBdefault_destination_concurrency_failed_cohort_limit (1)\fR"
234 /* How many pseudo-cohorts must suffer connection or handshake
235 /* failure before a specific destination is considered unavailable
236 /* (and further delivery is suspended).
237 /* .IP "\fBtransport_destination_concurrency_failed_cohort_limit ($default_destination_concurrency_failed_cohort_limit)\fR"
238 /* A transport-specific override for the
239 /* default_destination_concurrency_failed_cohort_limit parameter value,
240 /* where \fItransport\fR is the master.cf name of the message delivery
241 /* transport.
242 /* .IP "\fBdefault_destination_concurrency_negative_feedback (1)\fR"
243 /* The per-destination amount of delivery concurrency negative
244 /* feedback, after a delivery completes with a connection or handshake
245 /* failure.
246 /* .IP "\fBtransport_destination_concurrency_negative_feedback ($default_destination_concurrency_negative_feedback)\fR"
247 /* A transport-specific override for the
248 /* default_destination_concurrency_negative_feedback parameter value,
249 /* where \fItransport\fR is the master.cf name of the message delivery
250 /* transport.
251 /* .IP "\fBdefault_destination_concurrency_positive_feedback (1)\fR"
252 /* The per-destination amount of delivery concurrency positive
253 /* feedback, after a delivery completes without connection or handshake
254 /* failure.
255 /* .IP "\fBtransport_destination_concurrency_positive_feedback ($default_destination_concurrency_positive_feedback)\fR"
256 /* A transport-specific override for the
257 /* default_destination_concurrency_positive_feedback parameter value,
258 /* where \fItransport\fR is the master.cf name of the message delivery
259 /* transport.
260 /* .IP "\fBdestination_concurrency_feedback_debug (no)\fR"
261 /* Make the queue manager's feedback algorithm verbose for performance
262 /* analysis purposes.
263 /* RECIPIENT SCHEDULING CONTROLS
264 /* .ad
265 /* .fi
266 /* .IP "\fBdefault_destination_recipient_limit (50)\fR"
267 /* The default maximal number of recipients per message delivery.
268 /* .IP "\fBtransport_destination_recipient_limit ($default_destination_recipient_limit)\fR"
269 /* A transport-specific override for the
270 /* default_destination_recipient_limit parameter value, where
271 /* \fItransport\fR is the master.cf name of the message delivery
272 /* transport.
273 /* MESSAGE SCHEDULING CONTROLS
274 /* .ad
275 /* .fi
276 /* .IP "\fBdefault_delivery_slot_cost (5)\fR"
277 /* How often the Postfix queue manager's scheduler is allowed to
278 /* preempt delivery of one message with another.
279 /* .IP "\fBtransport_delivery_slot_cost ($default_delivery_slot_cost)\fR"
280 /* A transport-specific override for the default_delivery_slot_cost
281 /* parameter value, where \fItransport\fR is the master.cf name of
282 /* the message delivery transport.
283 /* .IP "\fBdefault_minimum_delivery_slots (3)\fR"
284 /* How many recipients a message must have in order to invoke the
285 /* Postfix queue manager's scheduling algorithm at all.
286 /* .IP "\fBtransport_minimum_delivery_slots ($default_minimum_delivery_slots)\fR"
287 /* A transport-specific override for the default_minimum_delivery_slots
288 /* parameter value, where \fItransport\fR is the master.cf name of
289 /* the message delivery transport.
290 /* .IP "\fBdefault_delivery_slot_discount (50)\fR"
291 /* The default value for transport-specific _delivery_slot_discount
292 /* settings.
293 /* .IP "\fBtransport_delivery_slot_discount ($default_delivery_slot_discount)\fR"
294 /* A transport-specific override for the default_delivery_slot_discount
295 /* parameter value, where \fItransport\fR is the master.cf name of
296 /* the message delivery transport.
297 /* .IP "\fBdefault_delivery_slot_loan (3)\fR"
298 /* The default value for transport-specific _delivery_slot_loan
299 /* settings.
300 /* .IP "\fBtransport_delivery_slot_loan ($default_delivery_slot_loan)\fR"
301 /* A transport-specific override for the default_delivery_slot_loan
302 /* parameter value, where \fItransport\fR is the master.cf name of
303 /* the message delivery transport.
304 /* OTHER RESOURCE AND RATE CONTROLS
305 /* .ad
306 /* .fi
307 /* .IP "\fBminimal_backoff_time (300s)\fR"
308 /* The minimal time between attempts to deliver a deferred message;
309 /* prior to Postfix 2.4 the default value was 1000s.
310 /* .IP "\fBmaximal_backoff_time (4000s)\fR"
311 /* The maximal time between attempts to deliver a deferred message.
312 /* .IP "\fBmaximal_queue_lifetime (5d)\fR"
313 /* Consider a message as undeliverable, when delivery fails with a
314 /* temporary error, and the time in the queue has reached the
315 /* maximal_queue_lifetime limit.
316 /* .IP "\fBqueue_run_delay (300s)\fR"
317 /* The time between deferred queue scans by the queue manager;
318 /* prior to Postfix 2.4 the default value was 1000s.
319 /* .IP "\fBtransport_retry_time (60s)\fR"
320 /* The time between attempts by the Postfix queue manager to contact
321 /* a malfunctioning message delivery transport.
322 /* .PP
323 /* Available in Postfix version 2.1 and later:
324 /* .IP "\fBbounce_queue_lifetime (5d)\fR"
325 /* Consider a bounce message as undeliverable, when delivery fails
326 /* with a temporary error, and the time in the queue has reached the
327 /* bounce_queue_lifetime limit.
328 /* .PP
329 /* Available in Postfix version 2.5 and later:
330 /* .IP "\fBdefault_destination_rate_delay (0s)\fR"
331 /* The default amount of delay that is inserted between individual
332 /* deliveries to the same destination; the resulting behavior depends
333 /* on the value of the corresponding per-destination recipient limit.
334 /* .IP "\fBtransport_destination_rate_delay ($default_destination_rate_delay)\fR"
335 /* A transport-specific override for the default_destination_rate_delay
336 /* parameter value, where \fItransport\fR is the master.cf name of
337 /* the message delivery transport.
338 /* .PP
339 /* Available in Postfix version 3.1 and later:
340 /* .IP "\fBdefault_transport_rate_delay (0s)\fR"
341 /* The default amount of delay that is inserted between individual
342 /* deliveries over the same message delivery transport, regardless of
343 /* destination.
344 /* .IP "\fBtransport_transport_rate_delay ($default_transport_rate_delay)\fR"
345 /* A transport-specific override for the default_transport_rate_delay
346 /* parameter value, where the initial \fItransport\fR in the parameter
347 /* name is the master.cf name of the message delivery transport.
348 /* SAFETY CONTROLS
349 /* .ad
350 /* .fi
351 /* .IP "\fBqmgr_daemon_timeout (1000s)\fR"
352 /* How much time a Postfix queue manager process may take to handle
353 /* a request before it is terminated by a built-in watchdog timer.
354 /* .IP "\fBqmgr_ipc_timeout (60s)\fR"
355 /* The time limit for the queue manager to send or receive information
356 /* over an internal communication channel.
357 /* .PP
358 /* Available in Postfix version 3.1 and later:
359 /* .IP "\fBaddress_verify_pending_request_limit (see 'postconf -d' output)\fR"
360 /* A safety limit that prevents address verification requests from
361 /* overwhelming the Postfix queue.
362 /* MISCELLANEOUS CONTROLS
363 /* .ad
364 /* .fi
365 /* .IP "\fBconfig_directory (see 'postconf -d' output)\fR"
366 /* The default location of the Postfix main.cf and master.cf
367 /* configuration files.
368 /* .IP "\fBdefer_transports (empty)\fR"
369 /* The names of message delivery transports that should not deliver mail
370 /* unless someone issues "\fBsendmail -q\fR" or equivalent.
371 /* .IP "\fBdelay_logging_resolution_limit (2)\fR"
372 /* The maximal number of digits after the decimal point when logging
373 /* sub-second delay values.
374 /* .IP "\fBhelpful_warnings (yes)\fR"
375 /* Log warnings about problematic configuration settings, and provide
376 /* helpful suggestions.
377 /* .IP "\fBprocess_id (read-only)\fR"
378 /* The process ID of a Postfix command or daemon process.
379 /* .IP "\fBprocess_name (read-only)\fR"
380 /* The process name of a Postfix command or daemon process.
381 /* .IP "\fBqueue_directory (see 'postconf -d' output)\fR"
382 /* The location of the Postfix top-level queue directory.
383 /* .IP "\fBsyslog_facility (mail)\fR"
384 /* The syslog facility of Postfix logging.
385 /* .IP "\fBsyslog_name (see 'postconf -d' output)\fR"
386 /* A prefix that is prepended to the process name in syslog
387 /* records, so that, for example, "smtpd" becomes "prefix/smtpd".
388 /* .PP
389 /* Available in Postfix version 3.0 and later:
390 /* .IP "\fBconfirm_delay_cleared (no)\fR"
391 /* After sending a "your message is delayed" notification, inform
392 /* the sender when the delay clears up.
393 /* .PP
394 /* Available in Postfix 3.3 and later:
395 /* .IP "\fBservice_name (read-only)\fR"
396 /* The master.cf service name of a Postfix daemon process.
397 /* FILES
398 /* /var/spool/postfix/incoming, incoming queue
399 /* /var/spool/postfix/active, active queue
400 /* /var/spool/postfix/deferred, deferred queue
401 /* /var/spool/postfix/bounce, non-delivery status
402 /* /var/spool/postfix/defer, non-delivery status
403 /* /var/spool/postfix/trace, delivery status
404 /* SEE ALSO
405 /* trivial-rewrite(8), address routing
406 /* bounce(8), delivery status reports
407 /* postconf(5), configuration parameters
408 /* master(5), generic daemon options
409 /* master(8), process manager
410 /* syslogd(8), system logging
411 /* README FILES
412 /* .ad
413 /* .fi
414 /* Use "\fBpostconf readme_directory\fR" or
415 /* "\fBpostconf html_directory\fR" to locate this information.
416 /* .na
417 /* .nf
418 /* SCHEDULER_README, scheduling algorithm
419 /* QSHAPE_README, Postfix queue analysis
420 /* LICENSE
421 /* .ad
422 /* .fi
423 /* The Secure Mailer license must be distributed with this software.
424 /* AUTHOR(S)
425 /* Wietse Venema
426 /* IBM T.J. Watson Research
427 /* P.O. Box 704
428 /* Yorktown Heights, NY 10598, USA
429 /*
430 /* Preemptive scheduler enhancements:
431 /* Patrik Rak
432 /* Modra 6
433 /* 155 00, Prague, Czech Republic
434 /*
435 /* Wietse Venema
436 /* Google, Inc.
437 /* 111 8th Avenue
438 /* New York, NY 10011, USA
439 /*--*/
440 
441 /* System library. */
442 
443 #include <sys_defs.h>
444 #include <stdlib.h>
445 #include <unistd.h>
446 #include <ctype.h>
447 
448 /* Utility library. */
449 
450 #include <msg.h>
451 #include <events.h>
452 #include <vstream.h>
453 #include <dict.h>
454 
455 /* Global library. */
456 
457 #include <mail_queue.h>
458 #include <recipient_list.h>
459 #include <mail_conf.h>
460 #include <mail_params.h>
461 #include <mail_version.h>
462 #include <mail_proto.h> /* QMGR_SCAN constants */
463 #include <mail_flow.h>
464 #include <flush_clnt.h>
465 
466 /* Master process interface */
467 
468 #include <master_proto.h>
469 #include <mail_server.h>
470 
471 /* Application-specific. */
472 
473 #include "qmgr.h"
474 
475  /*
476  * Tunables.
477  */
514 
515 static QMGR_SCAN *qmgr_scans[2];
516 
517 #define QMGR_SCAN_IDX_INCOMING 0
518 #define QMGR_SCAN_IDX_DEFERRED 1
519 #define QMGR_SCAN_IDX_COUNT (sizeof(qmgr_scans) / sizeof(qmgr_scans[0]))
520 
521 /* qmgr_deferred_run_event - queue manager heartbeat */
522 
523 static void qmgr_deferred_run_event(int unused_event, void *dummy)
524 {
525 
526  /*
527  * This routine runs when it is time for another deferred queue scan.
528  * Make sure this routine gets called again in the future.
529  */
531  event_request_timer(qmgr_deferred_run_event, dummy, var_queue_run_delay);
532 }
533 
534 /* qmgr_trigger_event - respond to external trigger(s) */
535 
536 static void qmgr_trigger_event(char *buf, ssize_t len,
537  char *unused_service, char **argv)
538 {
539  int incoming_flag = 0;
540  int deferred_flag = 0;
541  int i;
542 
543  /*
544  * Sanity check. This service takes no command-line arguments.
545  */
546  if (argv[0])
547  msg_fatal("unexpected command-line argument: %s", argv[0]);
548 
549  /*
550  * Collapse identical requests that have arrived since we looked last
551  * time. There is no client feedback so there is no need to process each
552  * request in order. And as long as we don't have conflicting requests we
553  * are free to sort them into the most suitable order.
554  */
555 #define QMGR_FLUSH_BEFORE (QMGR_FLUSH_ONCE | QMGR_FLUSH_DFXP)
556 
557  for (i = 0; i < len; i++) {
558  if (msg_verbose)
559  msg_info("request: %d (%c)",
560  buf[i], ISALNUM(buf[i]) ? buf[i] : '?');
561  switch (buf[i]) {
562  case TRIGGER_REQ_WAKEUP:
564  incoming_flag |= QMGR_SCAN_START;
565  break;
567  deferred_flag |= QMGR_SCAN_START;
568  break;
569  case QMGR_REQ_FLUSH_DEAD:
570  deferred_flag |= QMGR_FLUSH_BEFORE;
571  incoming_flag |= QMGR_FLUSH_BEFORE;
572  break;
573  case QMGR_REQ_SCAN_ALL:
574  deferred_flag |= QMGR_SCAN_ALL;
575  incoming_flag |= QMGR_SCAN_ALL;
576  break;
577  default:
578  if (msg_verbose)
579  msg_info("request ignored");
580  break;
581  }
582  }
583 
584  /*
585  * Process each request type at most once. Modifiers take effect upon the
586  * next queue run. If no queue run is in progress, and a queue scan is
587  * requested, the request takes effect immediately.
588  */
589  if (incoming_flag != 0)
590  qmgr_scan_request(qmgr_scans[QMGR_SCAN_IDX_INCOMING], incoming_flag);
591  if (deferred_flag != 0)
592  qmgr_scan_request(qmgr_scans[QMGR_SCAN_IDX_DEFERRED], deferred_flag);
593 }
594 
595 /* qmgr_loop - queue manager main loop */
596 
597 static int qmgr_loop(char *unused_name, char **unused_argv)
598 {
599  char *path;
600  ssize_t token_count;
601  int feed = 0;
602  int scan_idx; /* Priority order scan index */
603  static int first_scan_idx = QMGR_SCAN_IDX_INCOMING;
604  int last_scan_idx = QMGR_SCAN_IDX_COUNT - 1;
605  int delay;
606 
607  /*
608  * This routine runs as part of the event handling loop, after the event
609  * manager has delivered a timer or I/O event (including the completion
610  * of a connection to a delivery process), or after it has waited for a
611  * specified amount of time. The result value of qmgr_loop() specifies
612  * how long the event manager should wait for the next event.
613  */
614 #define DONT_WAIT 0
615 #define WAIT_FOR_EVENT (-1)
616 
617  /*
618  * Attempt to drain the active queue by allocating a suitable delivery
619  * process and by delivering mail via it. Delivery process allocation and
620  * mail delivery are asynchronous.
621  */
623 
624  /*
625  * Let some new blood into the active queue when the queue size is
626  * smaller than some configurable limit.
627  *
628  * We import one message per interrupt, to optimally tune the input count
629  * for the number of delivery agent protocol wait states, as explained in
630  * qmgr_transport.c.
631  */
632  delay = WAIT_FOR_EVENT;
633  for (scan_idx = 0; qmgr_message_count < var_qmgr_active_limit
634  && scan_idx < QMGR_SCAN_IDX_COUNT; ++scan_idx) {
635  last_scan_idx = (scan_idx + first_scan_idx) % QMGR_SCAN_IDX_COUNT;
636  if ((path = qmgr_scan_next(qmgr_scans[last_scan_idx])) != 0) {
637  delay = DONT_WAIT;
638  if ((feed = qmgr_active_feed(qmgr_scans[last_scan_idx], path)) != 0)
639  break;
640  }
641  }
642 
643  /*
644  * Round-robin the queue scans. When the active queue becomes full,
645  * prefer new mail over deferred mail.
646  */
648  first_scan_idx = (last_scan_idx + 1) % QMGR_SCAN_IDX_COUNT;
649  } else if (first_scan_idx != QMGR_SCAN_IDX_INCOMING) {
650  first_scan_idx = QMGR_SCAN_IDX_INCOMING;
651  }
652 
653  /*
654  * Global flow control. If enabled, slow down receiving processes that
655  * get ahead of the queue manager, but don't block them completely.
656  */
657  if (var_in_flow_delay > 0) {
658  token_count = mail_flow_count();
659  if (token_count < var_proc_limit) {
660  if (feed != 0 && last_scan_idx == QMGR_SCAN_IDX_INCOMING)
661  mail_flow_put(1);
662  else if (qmgr_scans[QMGR_SCAN_IDX_INCOMING]->handle == 0)
663  mail_flow_put(var_proc_limit - token_count);
664  } else if (token_count > var_proc_limit) {
665  mail_flow_get(token_count - var_proc_limit);
666  }
667  }
668  return (delay);
669 }
670 
671 /* pre_accept - see if tables have changed */
672 
673 static void pre_accept(char *unused_name, char **unused_argv)
674 {
675  const char *table;
676 
677  if ((table = dict_changed_name()) != 0) {
678  msg_info("table %s has changed -- restarting", table);
679  exit(0);
680  }
681 }
682 
683 /* qmgr_pre_init - pre-jail initialization */
684 
685 static void qmgr_pre_init(char *unused_name, char **unused_argv)
686 {
687  flush_init();
688 }
689 
690 /* qmgr_post_init - post-jail initialization */
691 
692 static void qmgr_post_init(char *name, char **unused_argv)
693 {
694 
695  /*
696  * Backwards compatibility.
697  */
698  if (strcmp(var_procname, "nqmgr") == 0) {
699  msg_warn("please update the %s/%s file; the new queue manager",
701  msg_warn("(old name: nqmgr) has become the standard queue manager (new name: qmgr)");
702  msg_warn("support for the name old name (nqmgr) will be removed from Postfix");
703  }
704 
705  /*
706  * Sanity check.
707  */
709  msg_warn("%s is smaller than %s - adjusting %s",
712  }
714  msg_warn("%s is larger than %s - adjusting %s",
717  }
718 
719  /*
720  * This routine runs after the skeleton code has entered the chroot jail.
721  * Prevent automatic process suicide after a limited number of client
722  * requests or after a limited amount of idle time. Move any left-over
723  * entries from the active queue to the incoming queue, and give them a
724  * time stamp into the future, in order to allow ongoing deliveries to
725  * finish first. Start scanning the incoming and deferred queues.
726  * Left-over active queue entries are moved to the incoming queue because
727  * the incoming queue has priority; moving left-overs to the deferred
728  * queue could cause anomalous delays when "postfix reload/start" are
729  * issued often. Override the IPC timeout (default 3600s) so that the
730  * queue manager can reset a broken IPC channel before the watchdog timer
731  * goes off.
732  */
734  var_use_limit = 0;
735  var_idle_limit = 0;
740  qmgr_deferred_run_event(0, (void *) 0);
741 }
742 
744 
745 /* main - the main program */
746 
747 int main(int argc, char **argv)
748 {
749  static const CONFIG_STR_TABLE str_table[] = {
754  0,
755  };
756  static const CONFIG_TIME_TABLE time_table[] = {
769  0,
770  };
771  static const CONFIG_INT_TABLE int_table[] = {
789  0,
790  };
791  static const CONFIG_BOOL_TABLE bool_table[] = {
795  0,
796  };
797 
798  /*
799  * Fingerprint executables and core dumps.
800  */
802 
803  /*
804  * Use the trigger service skeleton, because no-one else should be
805  * monitoring our service port while this process runs, and because we do
806  * not talk back to the client.
807  */
808  trigger_server_main(argc, argv, qmgr_trigger_event,
809  CA_MAIL_SERVER_INT_TABLE(int_table),
810  CA_MAIL_SERVER_STR_TABLE(str_table),
811  CA_MAIL_SERVER_BOOL_TABLE(bool_table),
812  CA_MAIL_SERVER_TIME_TABLE(time_table),
813  CA_MAIL_SERVER_PRE_INIT(qmgr_pre_init),
814  CA_MAIL_SERVER_POST_INIT(qmgr_post_init),
815  CA_MAIL_SERVER_LOOP(qmgr_loop),
816  CA_MAIL_SERVER_PRE_ACCEPT(pre_accept),
819  0);
820 }
#define VAR_DEST_CON_LIMIT
Definition: mail_params.h:843
int msg_verbose
Definition: msg.c:177
int qmgr_active_feed(QMGR_SCAN *, const char *)
Definition: qmgr_active.c:171
int var_qmgr_rcpt_limit
Definition: qmgr.c:416
#define VAR_DELIVERY_SLOT_DISCOUNT
Definition: mail_params.h:821
#define DEF_VRFY_PEND_LIMIT
Definition: mail_params.h:2784
int var_ipc_timeout
Definition: mail_params.c:255
int var_xport_rate_delay
Definition: qmgr.c:431
#define CA_MAIL_SERVER_WATCHDOG(v)
Definition: mail_server.h:73
#define DEF_INIT_DEST_CON
Definition: mail_params.h:840
#define VAR_QUEUE_RUN_DELAY
Definition: mail_params.h:742
#define DEF_XPORT_RETRY_TIME
Definition: mail_params.h:868
int var_qmgr_active_limit
Definition: qmgr.c:415
#define CA_MAIL_SERVER_BOOL_TABLE(v)
Definition: mail_server.h:58
char * var_procname
Definition: mail_params.c:252
#define QMGR_REQ_SCAN_DEFERRED
Definition: mail_proto.h:106
#define DEF_DELIVERY_SLOT_DISCOUNT
Definition: mail_params.h:823
int var_local_rcpt_lim
Definition: qmgr.c:423
#define VAR_DSN_QUEUE_TIME
Definition: mail_params.h:761
int var_xport_refill_delay
Definition: qmgr.c:489
#define DEF_QUEUE_RUN_DELAY
Definition: mail_params.h:743
#define CA_MAIL_SERVER_STR_TABLE(v)
Definition: mail_server.h:57
#define DEF_XPORT_REFILL_LIMIT
Definition: mail_params.h:800
#define MAIL_QUEUE_ACTIVE
Definition: mail_queue.h:31
ssize_t mail_flow_count(void)
Definition: mail_flow.c:134
int var_init_dest_concurrency
Definition: qmgr.c:417
#define VAR_DSN_DELAY_CLEARED
Definition: mail_params.h:769
#define VAR_DELIVERY_SLOT_LOAN
Definition: mail_params.h:816
#define DEF_QMGR_RCPT_LIMIT
Definition: mail_params.h:781
#define QMGR_SCAN_ALL
Definition: qmgr.h:398
int var_dest_rate_delay
Definition: qmgr.c:432
#define VAR_LOCAL_CON_LIMIT
Definition: mail_params.h:848
#define DEF_DEST_CON_LIMIT
Definition: mail_params.h:845
int var_conc_cohort_limit
Definition: qmgr.c:429
int var_conc_feedback_debug
Definition: qmgr.c:430
#define VAR_CONC_NEG_FDBACK
Definition: mail_params.h:3502
int var_transport_retry_time
Definition: qmgr.c:418
#define CA_MAIL_SERVER_LOOP(v)
Definition: mail_server.h:66
#define DEF_CONC_POS_FDBACK
Definition: mail_params.h:3499
#define VAR_MAX_BACKOFF_TIME
Definition: mail_params.h:749
#define VAR_DEST_RCPT_LIMIT
Definition: mail_params.h:855
char * var_config_dir
Definition: mail_params.c:241
int var_qmgr_clog_warn_time
Definition: qmgr.c:426
int var_qmgr_ipc_timeout
Definition: qmgr.c:435
int var_xport_refill_limit
Definition: qmgr.c:488
int var_idle_limit
Definition: mail_params.c:250
#define VAR_INIT_DEST_CON
Definition: mail_params.h:838
#define DEF_VERP_BOUNCE_OFF
Definition: mail_params.h:2597
#define DEF_LOCAL_CON_LIMIT
Definition: mail_params.h:849
NORETURN trigger_server_main(int, char **, TRIGGER_SERVER_FN,...)
#define QMGR_SCAN_IDX_INCOMING
Definition: qmgr.c:517
int var_min_backoff_time
Definition: qmgr.c:411
#define VAR_CONC_COHORT_LIM
Definition: mail_params.h:3510
int var_stack_rcpt_limit
Definition: qmgr.c:487
int var_dsn_delay_cleared
Definition: qmgr.c:436
int var_delivery_slot_discount
Definition: qmgr.c:492
void flush_init(void)
Definition: flush_clnt.c:104
#define DEF_XPORT_RCPT_LIMIT
Definition: mail_params.h:790
#define VAR_QMGR_IPC_TIMEOUT
Definition: mail_params.h:2019
#define DEF_QMGR_CLOG_WARN_TIME
Definition: mail_params.h:883
int var_proc_limit
Definition: mail_params.c:317
#define VAR_XPORT_REFILL_LIMIT
Definition: mail_params.h:798
ssize_t mail_flow_put(ssize_t len)
Definition: mail_flow.c:105
char * var_def_filter_nexthop
Definition: qmgr.c:433
#define ISALNUM(c)
Definition: sys_defs.h:1745
#define DEF_DELIVERY_SLOT_LOAN
Definition: mail_params.h:818
QMGR_SCAN * qmgr_scan_create(const char *)
Definition: qmgr_scan.c:176
#define DEF_LOCAL_RCPT_LIMIT
Definition: mail_params.h:861
#define CA_MAIL_SERVER_POST_INIT(v)
Definition: mail_server.h:65
#define CA_MAIL_SERVER_INT_TABLE(v)
Definition: mail_server.h:56
#define VAR_DELIVERY_SLOT_COST
Definition: mail_params.h:811
#define DEF_QMGR_ACT_LIMIT
Definition: mail_params.h:777
int var_min_delivery_slots
Definition: qmgr.c:493
#define VAR_QMGR_DAEMON_TIMEOUT
Definition: mail_params.h:2007
#define VAR_VRFY_PEND_LIMIT
Definition: mail_params.h:2783
#define VAR_QMGR_CLOG_WARN_TIME
Definition: mail_params.h:882
const char * dict_changed_name(void)
Definition: dict.c:583
void qmgr_active_drain(void)
Definition: qmgr_active.c:574
void qmgr_scan_request(QMGR_SCAN *, int)
Definition: qmgr_scan.c:112
int var_delivery_slot_cost
Definition: qmgr.c:490
#define DEF_DEFER_XPORTS
Definition: mail_params.h:875
ssize_t mail_flow_get(ssize_t len)
Definition: mail_flow.c:70
#define MASTER_CONF_FILE
Definition: mail_params.h:335
bool var_verp_bounce_off
Definition: qmgr.c:425
int var_qmgr_msg_rcpt_limit
Definition: qmgr.c:485
#define MAIL_QUEUE_INCOMING
Definition: mail_queue.h:30
void msg_warn(const char *fmt,...)
Definition: msg.c:215
int var_use_limit
Definition: mail_params.c:248
#define QMGR_SCAN_IDX_DEFERRED
Definition: qmgr.c:518
#define DEF_CONC_FDBACK_DEBUG
Definition: mail_params.h:3516
#define VAR_CONC_POS_FDBACK
Definition: mail_params.h:3497
#define DEF_XPORT_REFILL_DELAY
Definition: mail_params.h:805
#define MAIL_VERSION_STAMP_ALLOCATE
Definition: mail_version.h:67
int var_queue_run_delay
Definition: qmgr.c:410
#define QMGR_SCAN_IDX_COUNT
Definition: qmgr.c:519
#define CA_MAIL_SERVER_TIME_TABLE(v)
Definition: mail_server.h:59
char * var_defer_xports
Definition: qmgr.c:421
#define DEF_QMGR_IPC_TIMEOUT
Definition: mail_params.h:2020
#define DEF_MAX_BACKOFF_TIME
Definition: mail_params.h:750
#define DEF_DEST_RCPT_LIMIT
Definition: mail_params.h:857
#define DEF_MIN_BACKOFF_TIME
Definition: mail_params.h:746
#define WAIT_FOR_EVENT
#define VAR_XPORT_RATE_DELAY
Definition: mail_params.h:3524
int var_dest_rcpt_limit
Definition: qmgr.c:420
NORETURN msg_fatal(const char *fmt,...)
Definition: msg.c:249
#define VAR_DEFER_XPORTS
Definition: mail_params.h:874
#define DEF_CONC_NEG_FDBACK
Definition: mail_params.h:3504
#define VAR_QMGR_RCPT_LIMIT
Definition: mail_params.h:780
char * var_conc_pos_feedback
Definition: qmgr.c:427
int var_xport_rcpt_limit
Definition: qmgr.c:486
#define QMGR_REQ_SCAN_INCOMING
Definition: mail_proto.h:107
#define MAIL_QUEUE_DEFERRED
Definition: mail_queue.h:32
time_t event_time(void)
Definition: events.c:647
#define VAR_DEF_FILTER_NEXTHOP
Definition: mail_params.h:2483
int var_max_queue_time
Definition: qmgr.c:413
int var_in_flow_delay
Definition: mail_params.c:291
#define VAR_QMGR_ACT_LIMIT
Definition: mail_params.h:776
int var_qmgr_daemon_timeout
Definition: qmgr.c:434
#define DEF_STACK_RCPT_LIMIT
Definition: mail_params.h:795
#define DONT_WAIT
#define VAR_CONC_FDBACK_DEBUG
Definition: mail_params.h:3515
#define VAR_LOCAL_RCPT_LIMIT
Definition: mail_params.h:860
#define TRIGGER_REQ_WAKEUP
Definition: mail_proto.h:101
#define DEF_MIN_DELIVERY_SLOTS
Definition: mail_params.h:828
time_t event_request_timer(EVENT_NOTIFY_TIME_FN callback, void *context, int delay)
Definition: events.c:894
int main(int argc, char **argv)
Definition: qmgr.c:664
#define VAR_XPORT_RETRY_TIME
Definition: mail_params.h:867
int var_delivery_slot_loan
Definition: qmgr.c:491
#define DEF_DEF_FILTER_NEXTHOP
Definition: mail_params.h:2484
#define CA_MAIL_SERVER_SOLITARY
Definition: mail_server.h:69
int var_vrfy_pend_limit
Definition: qmgr.c:437
#define QMGR_REQ_SCAN_ALL
Definition: mail_proto.h:109
void qmgr_move(const char *, const char *, time_t)
Definition: qmgr_move.c:57
#define VAR_XPORT_RCPT_LIMIT
Definition: mail_params.h:788
#define VAR_XPORT_REFILL_DELAY
Definition: mail_params.h:803
#define VAR_QMGR_MSG_RCPT_LIMIT
Definition: mail_params.h:784
#define DEF_XPORT_RATE_DELAY
Definition: mail_params.h:3526
#define VAR_DEST_RATE_DELAY
Definition: mail_params.h:3519
#define QMGR_SCAN_START
Definition: qmgr.h:397
#define CA_MAIL_SERVER_PRE_ACCEPT(v)
Definition: mail_server.h:68
#define DEF_CONC_COHORT_LIM
Definition: mail_params.h:3512
#define QMGR_REQ_FLUSH_DEAD
Definition: mail_proto.h:108
#define DEF_DELIVERY_SLOT_COST
Definition: mail_params.h:813
int var_local_con_lim
Definition: qmgr.c:424
#define DEF_QMGR_MSG_RCPT_LIMIT
Definition: mail_params.h:785
#define DEF_DSN_QUEUE_TIME
Definition: mail_params.h:762
#define VAR_MIN_BACKOFF_TIME
Definition: mail_params.h:745
int var_dest_con_limit
Definition: qmgr.c:419
int var_dsn_queue_time
Definition: qmgr.c:414
#define QMGR_FLUSH_BEFORE
char * qmgr_scan_next(QMGR_SCAN *)
Definition: qmgr_scan.c:154
int qmgr_message_count
Definition: qmgr_message.c:150
#define VAR_STACK_RCPT_LIMIT
Definition: mail_params.h:793
#define VAR_MIN_DELIVERY_SLOTS
Definition: mail_params.h:826
#define VAR_VERP_BOUNCE_OFF
Definition: mail_params.h:2596
#define DEF_DEST_RATE_DELAY
Definition: mail_params.h:3521
#define DEF_MAX_QUEUE_TIME
Definition: mail_params.h:754
int var_max_backoff_time
Definition: qmgr.c:412
#define VAR_MAX_QUEUE_TIME
Definition: mail_params.h:753
char * var_conc_neg_feedback
Definition: qmgr.c:428
#define DEF_DSN_DELAY_CLEARED
Definition: mail_params.h:770
#define DEF_QMGR_DAEMON_TIMEOUT
Definition: mail_params.h:2008
#define CA_MAIL_SERVER_PRE_INIT(v)
Definition: mail_server.h:64
void msg_info(const char *fmt,...)
Definition: msg.c:199
MAIL_VERSION_STAMP_DECLARE
Definition: qmgr.c:660