Postfix3.3.1
qmgr.c
[詳解]
1 /*++
2 /* NAME
3 /* qmgr 8
4 /* SUMMARY
5 /* old 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) agent 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 /* TRIGGERS
88 /* .ad
89 /* .fi
90 /* On an idle system, the queue manager waits for the arrival of
91 /* trigger events, or it waits for a timer to go off. A trigger
92 /* is a one-byte message.
93 /* Depending on the message received, the queue manager performs
94 /* one of the following actions (the message is followed by the
95 /* symbolic constant used internally by the software):
96 /* .IP "\fBD (QMGR_REQ_SCAN_DEFERRED)\fR"
97 /* Start a deferred queue scan. If a deferred queue scan is already
98 /* in progress, that scan will be restarted as soon as it finishes.
99 /* .IP "\fBI (QMGR_REQ_SCAN_INCOMING)\fR"
100 /* Start an incoming queue scan. If an incoming queue scan is already
101 /* in progress, that scan will be restarted as soon as it finishes.
102 /* .IP "\fBA (QMGR_REQ_SCAN_ALL)\fR"
103 /* Ignore deferred queue file time stamps. The request affects
104 /* the next deferred queue scan.
105 /* .IP "\fBF (QMGR_REQ_FLUSH_DEAD)\fR"
106 /* Purge all information about dead transports and destinations.
107 /* .IP "\fBW (TRIGGER_REQ_WAKEUP)\fR"
108 /* Wakeup call, This is used by the master server to instantiate
109 /* servers that should not go away forever. The action is to start
110 /* an incoming queue scan.
111 /* .PP
112 /* The \fBqmgr\fR(8) daemon reads an entire buffer worth of triggers.
113 /* Multiple identical trigger requests are collapsed into one, and
114 /* trigger requests are sorted so that \fBA\fR and \fBF\fR precede
115 /* \fBD\fR and \fBI\fR. Thus, in order to force a deferred queue run,
116 /* one would request \fBA F D\fR; in order to notify the queue manager
117 /* of the arrival of new mail one would request \fBI\fR.
118 /* STANDARDS
119 /* RFC 3463 (Enhanced status codes)
120 /* RFC 3464 (Delivery status notifications)
121 /* SECURITY
122 /* .ad
123 /* .fi
124 /* The \fBqmgr\fR(8) daemon is not security sensitive. It reads
125 /* single-character messages from untrusted local users, and thus may
126 /* be susceptible to denial of service attacks. The \fBqmgr\fR(8) daemon
127 /* does not talk to the outside world, and it can be run at fixed low
128 /* privilege in a chrooted environment.
129 /* DIAGNOSTICS
130 /* Problems and transactions are logged to the \fBsyslog\fR(8) daemon.
131 /* Corrupted message files are saved to the \fBcorrupt\fR queue
132 /* for further inspection.
133 /*
134 /* Depending on the setting of the \fBnotify_classes\fR parameter,
135 /* the postmaster is notified of bounces and of other trouble.
136 /* BUGS
137 /* A single queue manager process has to compete for disk access with
138 /* multiple front-end processes such as \fBcleanup\fR(8). A sudden burst of
139 /* inbound mail can negatively impact outbound delivery rates.
140 /* CONFIGURATION PARAMETERS
141 /* .ad
142 /* .fi
143 /* Changes to \fBmain.cf\fR are not picked up automatically,
144 /* as \fBqmgr\fR(8)
145 /* is a persistent process. Use the command "\fBpostfix reload\fR" after
146 /* a configuration change.
147 /*
148 /* The text below provides only a parameter summary. See
149 /* \fBpostconf\fR(5) for more details including examples.
150 /*
151 /* In the text below, \fItransport\fR is the first field in a
152 /* \fBmaster.cf\fR entry.
153 /* COMPATIBILITY CONTROLS
154 /* .ad
155 /* .fi
156 /* Available before Postfix version 2.5:
157 /* .IP "\fBallow_min_user (no)\fR"
158 /* Allow a sender or recipient address to have `-' as the first
159 /* character.
160 /* .PP
161 /* Available with Postfix version 2.7 and later:
162 /* .IP "\fBdefault_filter_nexthop (empty)\fR"
163 /* When a content_filter or FILTER request specifies no explicit
164 /* next-hop destination, use $default_filter_nexthop instead; when
165 /* that value is empty, use the domain in the recipient address.
166 /* ACTIVE QUEUE CONTROLS
167 /* .ad
168 /* .fi
169 /* .IP "\fBqmgr_clog_warn_time (300s)\fR"
170 /* The minimal delay between warnings that a specific destination is
171 /* clogging up the Postfix active queue.
172 /* .IP "\fBqmgr_message_active_limit (20000)\fR"
173 /* The maximal number of messages in the active queue.
174 /* .IP "\fBqmgr_message_recipient_limit (20000)\fR"
175 /* The maximal number of recipients held in memory by the Postfix
176 /* queue manager, and the maximal size of the short-term,
177 /* in-memory "dead" destination status cache.
178 /* DELIVERY CONCURRENCY CONTROLS
179 /* .ad
180 /* .fi
181 /* .IP "\fBqmgr_fudge_factor (100)\fR"
182 /* Obsolete feature: the percentage of delivery resources that a busy
183 /* mail system will use up for delivery of a large mailing list
184 /* message.
185 /* .IP "\fBinitial_destination_concurrency (5)\fR"
186 /* The initial per-destination concurrency level for parallel delivery
187 /* to the same destination.
188 /* .IP "\fBdefault_destination_concurrency_limit (20)\fR"
189 /* The default maximal number of parallel deliveries to the same
190 /* destination.
191 /* .IP "\fBtransport_destination_concurrency_limit ($default_destination_concurrency_limit)\fR"
192 /* A transport-specific override for the
193 /* default_destination_concurrency_limit parameter value, where
194 /* \fItransport\fR is the master.cf name of the message delivery
195 /* transport.
196 /* .PP
197 /* Available in Postfix version 2.5 and later:
198 /* .IP "\fBtransport_initial_destination_concurrency ($initial_destination_concurrency)\fR"
199 /* A transport-specific override for the initial_destination_concurrency
200 /* parameter value, where \fItransport\fR is the master.cf name of
201 /* the message delivery transport.
202 /* .IP "\fBdefault_destination_concurrency_failed_cohort_limit (1)\fR"
203 /* How many pseudo-cohorts must suffer connection or handshake
204 /* failure before a specific destination is considered unavailable
205 /* (and further delivery is suspended).
206 /* .IP "\fBtransport_destination_concurrency_failed_cohort_limit ($default_destination_concurrency_failed_cohort_limit)\fR"
207 /* A transport-specific override for the
208 /* default_destination_concurrency_failed_cohort_limit parameter value,
209 /* where \fItransport\fR is the master.cf name of the message delivery
210 /* transport.
211 /* .IP "\fBdefault_destination_concurrency_negative_feedback (1)\fR"
212 /* The per-destination amount of delivery concurrency negative
213 /* feedback, after a delivery completes with a connection or handshake
214 /* failure.
215 /* .IP "\fBtransport_destination_concurrency_negative_feedback ($default_destination_concurrency_negative_feedback)\fR"
216 /* A transport-specific override for the
217 /* default_destination_concurrency_negative_feedback parameter value,
218 /* where \fItransport\fR is the master.cf name of the message delivery
219 /* transport.
220 /* .IP "\fBdefault_destination_concurrency_positive_feedback (1)\fR"
221 /* The per-destination amount of delivery concurrency positive
222 /* feedback, after a delivery completes without connection or handshake
223 /* failure.
224 /* .IP "\fBtransport_destination_concurrency_positive_feedback ($default_destination_concurrency_positive_feedback)\fR"
225 /* A transport-specific override for the
226 /* default_destination_concurrency_positive_feedback parameter value,
227 /* where \fItransport\fR is the master.cf name of the message delivery
228 /* transport.
229 /* .IP "\fBdestination_concurrency_feedback_debug (no)\fR"
230 /* Make the queue manager's feedback algorithm verbose for performance
231 /* analysis purposes.
232 /* RECIPIENT SCHEDULING CONTROLS
233 /* .ad
234 /* .fi
235 /* .IP "\fBdefault_destination_recipient_limit (50)\fR"
236 /* The default maximal number of recipients per message delivery.
237 /* .IP "\fBtransport_destination_recipient_limit ($default_destination_recipient_limit)\fR"
238 /* A transport-specific override for the
239 /* default_destination_recipient_limit parameter value, where
240 /* \fItransport\fR is the master.cf name of the message delivery
241 /* transport.
242 /* OTHER RESOURCE AND RATE CONTROLS
243 /* .ad
244 /* .fi
245 /* .IP "\fBminimal_backoff_time (300s)\fR"
246 /* The minimal time between attempts to deliver a deferred message;
247 /* prior to Postfix 2.4 the default value was 1000s.
248 /* .IP "\fBmaximal_backoff_time (4000s)\fR"
249 /* The maximal time between attempts to deliver a deferred message.
250 /* .IP "\fBmaximal_queue_lifetime (5d)\fR"
251 /* Consider a message as undeliverable, when delivery fails with a
252 /* temporary error, and the time in the queue has reached the
253 /* maximal_queue_lifetime limit.
254 /* .IP "\fBqueue_run_delay (300s)\fR"
255 /* The time between deferred queue scans by the queue manager;
256 /* prior to Postfix 2.4 the default value was 1000s.
257 /* .IP "\fBtransport_retry_time (60s)\fR"
258 /* The time between attempts by the Postfix queue manager to contact
259 /* a malfunctioning message delivery transport.
260 /* .PP
261 /* Available in Postfix version 2.1 and later:
262 /* .IP "\fBbounce_queue_lifetime (5d)\fR"
263 /* Consider a bounce message as undeliverable, when delivery fails
264 /* with a temporary error, and the time in the queue has reached the
265 /* bounce_queue_lifetime limit.
266 /* .PP
267 /* Available in Postfix version 2.5 and later:
268 /* .IP "\fBdefault_destination_rate_delay (0s)\fR"
269 /* The default amount of delay that is inserted between individual
270 /* deliveries to the same destination; the resulting behavior depends
271 /* on the value of the corresponding per-destination recipient limit.
272 /* .IP "\fBtransport_destination_rate_delay ($default_destination_rate_delay)\fR"
273 /* A transport-specific override for the default_destination_rate_delay
274 /* parameter value, where \fItransport\fR is the master.cf name of
275 /* the message delivery transport.
276 /* .PP
277 /* Available in Postfix version 3.1 and later:
278 /* .IP "\fBdefault_transport_rate_delay (0s)\fR"
279 /* The default amount of delay that is inserted between individual
280 /* deliveries over the same message delivery transport, regardless of
281 /* destination.
282 /* .IP "\fBtransport_transport_rate_delay ($default_transport_rate_delay)\fR"
283 /* A transport-specific override for the default_transport_rate_delay
284 /* parameter value, where the initial \fItransport\fR in the parameter
285 /* name is the master.cf name of the message delivery transport.
286 /* SAFETY CONTROLS
287 /* .ad
288 /* .fi
289 /* .IP "\fBqmgr_daemon_timeout (1000s)\fR"
290 /* How much time a Postfix queue manager process may take to handle
291 /* a request before it is terminated by a built-in watchdog timer.
292 /* .IP "\fBqmgr_ipc_timeout (60s)\fR"
293 /* The time limit for the queue manager to send or receive information
294 /* over an internal communication channel.
295 /* .PP
296 /* Available in Postfix version 3.1 and later:
297 /* .IP "\fBaddress_verify_pending_request_limit (see 'postconf -d' output)\fR"
298 /* A safety limit that prevents address verification requests from
299 /* overwhelming the Postfix queue.
300 /* MISCELLANEOUS CONTROLS
301 /* .ad
302 /* .fi
303 /* .IP "\fBconfig_directory (see 'postconf -d' output)\fR"
304 /* The default location of the Postfix main.cf and master.cf
305 /* configuration files.
306 /* .IP "\fBdefer_transports (empty)\fR"
307 /* The names of message delivery transports that should not deliver mail
308 /* unless someone issues "\fBsendmail -q\fR" or equivalent.
309 /* .IP "\fBdelay_logging_resolution_limit (2)\fR"
310 /* The maximal number of digits after the decimal point when logging
311 /* sub-second delay values.
312 /* .IP "\fBhelpful_warnings (yes)\fR"
313 /* Log warnings about problematic configuration settings, and provide
314 /* helpful suggestions.
315 /* .IP "\fBprocess_id (read-only)\fR"
316 /* The process ID of a Postfix command or daemon process.
317 /* .IP "\fBprocess_name (read-only)\fR"
318 /* The process name of a Postfix command or daemon process.
319 /* .IP "\fBqueue_directory (see 'postconf -d' output)\fR"
320 /* The location of the Postfix top-level queue directory.
321 /* .IP "\fBsyslog_facility (mail)\fR"
322 /* The syslog facility of Postfix logging.
323 /* .IP "\fBsyslog_name (see 'postconf -d' output)\fR"
324 /* A prefix that is prepended to the process name in syslog
325 /* records, so that, for example, "smtpd" becomes "prefix/smtpd".
326 /* .PP
327 /* Available in Postfix version 3.0 and later:
328 /* .IP "\fBconfirm_delay_cleared (no)\fR"
329 /* After sending a "your message is delayed" notification, inform
330 /* the sender when the delay clears up.
331 /* .PP
332 /* Available in Postfix 3.3 and later:
333 /* .IP "\fBservice_name (read-only)\fR"
334 /* The master.cf service name of a Postfix daemon process.
335 /* FILES
336 /* /var/spool/postfix/incoming, incoming queue
337 /* /var/spool/postfix/active, active queue
338 /* /var/spool/postfix/deferred, deferred queue
339 /* /var/spool/postfix/bounce, non-delivery status
340 /* /var/spool/postfix/defer, non-delivery status
341 /* /var/spool/postfix/trace, delivery status
342 /* SEE ALSO
343 /* trivial-rewrite(8), address routing
344 /* bounce(8), delivery status reports
345 /* postconf(5), configuration parameters
346 /* master(5), generic daemon options
347 /* master(8), process manager
348 /* syslogd(8), system logging
349 /* README FILES
350 /* .ad
351 /* .fi
352 /* Use "\fBpostconf readme_directory\fR" or
353 /* "\fBpostconf html_directory\fR" to locate this information.
354 /* .na
355 /* .nf
356 /* QSHAPE_README, Postfix queue analysis
357 /* LICENSE
358 /* .ad
359 /* .fi
360 /* The Secure Mailer license must be distributed with this software.
361 /* AUTHOR(S)
362 /* Wietse Venema
363 /* IBM T.J. Watson Research
364 /* P.O. Box 704
365 /* Yorktown Heights, NY 10598, USA
366 /*
367 /* Wietse Venema
368 /* Google, Inc.
369 /* 111 8th Avenue
370 /* New York, NY 10011, USA
371 /*--*/
372 
373 /* System library. */
374 
375 #include <sys_defs.h>
376 #include <stdlib.h>
377 #include <unistd.h>
378 #include <ctype.h>
379 
380 /* Utility library. */
381 
382 #include <msg.h>
383 #include <events.h>
384 #include <vstream.h>
385 #include <dict.h>
386 
387 /* Global library. */
388 
389 #include <mail_queue.h>
390 #include <recipient_list.h>
391 #include <mail_conf.h>
392 #include <mail_params.h>
393 #include <mail_version.h>
394 #include <mail_proto.h> /* QMGR_SCAN constants */
395 #include <mail_flow.h>
396 #include <flush_clnt.h>
397 
398 /* Master process interface */
399 
400 #include <master_proto.h>
401 #include <mail_server.h>
402 
403 /* Application-specific. */
404 
405 #include "qmgr.h"
406 
407  /*
408  * Tunables.
409  */
423 int var_local_rcpt_lim; /* XXX */
424 int var_local_con_lim; /* XXX */
438 
439 static QMGR_SCAN *qmgr_scans[2];
440 
441 #define QMGR_SCAN_IDX_INCOMING 0
442 #define QMGR_SCAN_IDX_DEFERRED 1
443 #define QMGR_SCAN_IDX_COUNT (sizeof(qmgr_scans) / sizeof(qmgr_scans[0]))
444 
445 /* qmgr_deferred_run_event - queue manager heartbeat */
446 
447 static void qmgr_deferred_run_event(int unused_event, void *dummy)
448 {
449 
450  /*
451  * This routine runs when it is time for another deferred queue scan.
452  * Make sure this routine gets called again in the future.
453  */
455  event_request_timer(qmgr_deferred_run_event, dummy, var_queue_run_delay);
456 }
457 
458 /* qmgr_trigger_event - respond to external trigger(s) */
459 
460 static void qmgr_trigger_event(char *buf, ssize_t len,
461  char *unused_service, char **argv)
462 {
463  int incoming_flag = 0;
464  int deferred_flag = 0;
465  int i;
466 
467  /*
468  * Sanity check. This service takes no command-line arguments.
469  */
470  if (argv[0])
471  msg_fatal("unexpected command-line argument: %s", argv[0]);
472 
473  /*
474  * Collapse identical requests that have arrived since we looked last
475  * time. There is no client feedback so there is no need to process each
476  * request in order. And as long as we don't have conflicting requests we
477  * are free to sort them into the most suitable order.
478  */
479 #define QMGR_FLUSH_BEFORE (QMGR_FLUSH_ONCE | QMGR_FLUSH_DFXP)
480 
481  for (i = 0; i < len; i++) {
482  if (msg_verbose)
483  msg_info("request: %d (%c)",
484  buf[i], ISALNUM(buf[i]) ? buf[i] : '?');
485  switch (buf[i]) {
486  case TRIGGER_REQ_WAKEUP:
488  incoming_flag |= QMGR_SCAN_START;
489  break;
491  deferred_flag |= QMGR_SCAN_START;
492  break;
493  case QMGR_REQ_FLUSH_DEAD:
494  deferred_flag |= QMGR_FLUSH_BEFORE;
495  incoming_flag |= QMGR_FLUSH_BEFORE;
496  break;
497  case QMGR_REQ_SCAN_ALL:
498  deferred_flag |= QMGR_SCAN_ALL;
499  incoming_flag |= QMGR_SCAN_ALL;
500  break;
501  default:
502  if (msg_verbose)
503  msg_info("request ignored");
504  break;
505  }
506  }
507 
508  /*
509  * Process each request type at most once. Modifiers take effect upon the
510  * next queue run. If no queue run is in progress, and a queue scan is
511  * requested, the request takes effect immediately.
512  */
513  if (incoming_flag != 0)
514  qmgr_scan_request(qmgr_scans[QMGR_SCAN_IDX_INCOMING], incoming_flag);
515  if (deferred_flag != 0)
516  qmgr_scan_request(qmgr_scans[QMGR_SCAN_IDX_DEFERRED], deferred_flag);
517 }
518 
519 /* qmgr_loop - queue manager main loop */
520 
521 static int qmgr_loop(char *unused_name, char **unused_argv)
522 {
523  char *path;
524  ssize_t token_count;
525  int feed = 0;
526  int scan_idx; /* Priority order scan index */
527  static int first_scan_idx = QMGR_SCAN_IDX_INCOMING;
528  int last_scan_idx = QMGR_SCAN_IDX_COUNT - 1;
529  int delay;
530 
531  /*
532  * This routine runs as part of the event handling loop, after the event
533  * manager has delivered a timer or I/O event (including the completion
534  * of a connection to a delivery process), or after it has waited for a
535  * specified amount of time. The result value of qmgr_loop() specifies
536  * how long the event manager should wait for the next event.
537  */
538 #define DONT_WAIT 0
539 #define WAIT_FOR_EVENT (-1)
540 
541  /*
542  * Attempt to drain the active queue by allocating a suitable delivery
543  * process and by delivering mail via it. Delivery process allocation and
544  * mail delivery are asynchronous.
545  */
547 
548  /*
549  * Let some new blood into the active queue when the queue size is
550  * smaller than some configurable limit, and when the number of in-core
551  * recipients does not exceed some configurable limit.
552  *
553  * We import one message per interrupt, to optimally tune the input count
554  * for the number of delivery agent protocol wait states, as explained in
555  * qmgr_transport.c.
556  */
557  delay = WAIT_FOR_EVENT;
558  for (scan_idx = 0; qmgr_message_count < var_qmgr_active_limit
560  && scan_idx < QMGR_SCAN_IDX_COUNT; ++scan_idx) {
561  last_scan_idx = (scan_idx + first_scan_idx) % QMGR_SCAN_IDX_COUNT;
562  if ((path = qmgr_scan_next(qmgr_scans[last_scan_idx])) != 0) {
563  delay = DONT_WAIT;
564  if ((feed = qmgr_active_feed(qmgr_scans[last_scan_idx], path)) != 0)
565  break;
566  }
567  }
568 
569  /*
570  * Round-robin the queue scans. When the active queue becomes full,
571  * prefer new mail over deferred mail.
572  */
575  first_scan_idx = (last_scan_idx + 1) % QMGR_SCAN_IDX_COUNT;
576  } else if (first_scan_idx != QMGR_SCAN_IDX_INCOMING) {
577  first_scan_idx = QMGR_SCAN_IDX_INCOMING;
578  }
579 
580  /*
581  * Global flow control. If enabled, slow down receiving processes that
582  * get ahead of the queue manager, but don't block them completely.
583  */
584  if (var_in_flow_delay > 0) {
585  token_count = mail_flow_count();
586  if (token_count < var_proc_limit) {
587  if (feed != 0 && last_scan_idx == QMGR_SCAN_IDX_INCOMING)
588  mail_flow_put(1);
589  else if (qmgr_scans[QMGR_SCAN_IDX_INCOMING]->handle == 0)
590  mail_flow_put(var_proc_limit - token_count);
591  } else if (token_count > var_proc_limit) {
592  mail_flow_get(token_count - var_proc_limit);
593  }
594  }
595  return (delay);
596 }
597 
598 /* pre_accept - see if tables have changed */
599 
600 static void pre_accept(char *unused_name, char **unused_argv)
601 {
602  const char *table;
603 
604  if ((table = dict_changed_name()) != 0) {
605  msg_info("table %s has changed -- restarting", table);
606  exit(0);
607  }
608 }
609 
610 /* qmgr_pre_init - pre-jail initialization */
611 
612 static void qmgr_pre_init(char *unused_name, char **unused_argv)
613 {
614  flush_init();
615 }
616 
617 /* qmgr_post_init - post-jail initialization */
618 
619 static void qmgr_post_init(char *unused_name, char **unused_argv)
620 {
621 
622  /*
623  * Sanity check.
624  */
626  msg_warn("%s is smaller than %s - adjusting %s",
629  }
631  msg_warn("%s is larger than %s - adjusting %s",
634  }
635 
636  /*
637  * This routine runs after the skeleton code has entered the chroot jail.
638  * Prevent automatic process suicide after a limited number of client
639  * requests or after a limited amount of idle time. Move any left-over
640  * entries from the active queue to the incoming queue, and give them a
641  * time stamp into the future, in order to allow ongoing deliveries to
642  * finish first. Start scanning the incoming and deferred queues.
643  * Left-over active queue entries are moved to the incoming queue because
644  * the incoming queue has priority; moving left-overs to the deferred
645  * queue could cause anomalous delays when "postfix reload/start" are
646  * issued often. Override the IPC timeout (default 3600s) so that the
647  * queue manager can reset a broken IPC channel before the watchdog timer
648  * goes off.
649  */
651  var_use_limit = 0;
652  var_idle_limit = 0;
657  qmgr_deferred_run_event(0, (void *) 0);
658 }
659 
661 
662 /* main - the main program */
663 
664 int main(int argc, char **argv)
665 {
666  static const CONFIG_STR_TABLE str_table[] = {
671  0,
672  };
673  static const CONFIG_TIME_TABLE time_table[] = {
685  0,
686  };
687  static const CONFIG_INT_TABLE int_table[] = {
698  0,
699  };
700  static const CONFIG_BOOL_TABLE bool_table[] = {
704  0,
705  };
706 
707  /*
708  * Fingerprint executables and core dumps.
709  */
711 
712  /*
713  * Use the trigger service skeleton, because no-one else should be
714  * monitoring our service port while this process runs, and because we do
715  * not talk back to the client.
716  */
717  trigger_server_main(argc, argv, qmgr_trigger_event,
718  CA_MAIL_SERVER_INT_TABLE(int_table),
719  CA_MAIL_SERVER_STR_TABLE(str_table),
720  CA_MAIL_SERVER_BOOL_TABLE(bool_table),
721  CA_MAIL_SERVER_TIME_TABLE(time_table),
722  CA_MAIL_SERVER_PRE_INIT(qmgr_pre_init),
723  CA_MAIL_SERVER_POST_INIT(qmgr_post_init),
724  CA_MAIL_SERVER_LOOP(qmgr_loop),
725  CA_MAIL_SERVER_PRE_ACCEPT(pre_accept),
728  0);
729 }
#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 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
#define QMGR_REQ_SCAN_DEFERRED
Definition: mail_proto.h:106
int var_local_rcpt_lim
Definition: qmgr.c:423
#define DONT_WAIT
#define VAR_DSN_QUEUE_TIME
Definition: mail_params.h:761
#define DEF_QUEUE_RUN_DELAY
Definition: mail_params.h:743
#define CA_MAIL_SERVER_STR_TABLE(v)
Definition: mail_server.h:57
#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 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
#define QMGR_SCAN_IDX_INCOMING
Definition: qmgr.c:441
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
int var_qmgr_clog_warn_time
Definition: qmgr.c:426
int var_qmgr_ipc_timeout
Definition: qmgr.c:435
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 DEF_QMGR_FUDGE
Definition: mail_params.h:832
int var_min_backoff_time
Definition: qmgr.c:411
#define VAR_CONC_COHORT_LIM
Definition: mail_params.h:3510
int var_dsn_delay_cleared
Definition: qmgr.c:436
void flush_init(void)
Definition: flush_clnt.c:104
#define VAR_QMGR_IPC_TIMEOUT
Definition: mail_params.h:2019
#define VAR_QMGR_FUDGE
Definition: mail_params.h:831
#define DEF_QMGR_CLOG_WARN_TIME
Definition: mail_params.h:883
int var_proc_limit
Definition: mail_params.c:317
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
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 DEF_QMGR_ACT_LIMIT
Definition: mail_params.h:777
#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
int var_qmgr_fudge
Definition: qmgr.c:422
void qmgr_scan_request(QMGR_SCAN *, int)
Definition: qmgr_scan.c:112
#define DEF_DEFER_XPORTS
Definition: mail_params.h:875
ssize_t mail_flow_get(ssize_t len)
Definition: mail_flow.c:70
#define QMGR_SCAN_IDX_COUNT
Definition: qmgr.c:443
bool var_verp_bounce_off
Definition: qmgr.c:425
#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 DEF_CONC_FDBACK_DEBUG
Definition: mail_params.h:3516
#define VAR_CONC_POS_FDBACK
Definition: mail_params.h:3497
#define MAIL_VERSION_STAMP_ALLOCATE
Definition: mail_version.h:67
int var_queue_run_delay
Definition: qmgr.c:410
#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 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
#define QMGR_FLUSH_BEFORE
#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 WAIT_FOR_EVENT
int qmgr_recipient_count
Definition: qmgr_message.c:151
#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
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
#define DEF_DEF_FILTER_NEXTHOP
Definition: mail_params.h:2484
#define CA_MAIL_SERVER_SOLITARY
Definition: mail_server.h:69
#define QMGR_SCAN_IDX_DEFERRED
Definition: qmgr.c:442
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 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
int var_local_con_lim
Definition: qmgr.c:424
#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
char * qmgr_scan_next(QMGR_SCAN *)
Definition: qmgr_scan.c:154
int qmgr_message_count
Definition: qmgr_message.c:150
#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