~ubuntu-branches/ubuntu/hardy/exim4/hardy-proposed

« back to all changes in this revision

Viewing changes to src/daemon.c

  • Committer: Bazaar Package Importer
  • Author(s): Marc Haber
  • Date: 2005-07-02 06:08:34 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050702060834-qk17pd52kb9nt3bj
Tags: 4.52-1
* new upstream version 4.51. (mh)
  * adapt 70_remove_exim-users_references
  * remove 37_gnutlsparams
  * adapt 36_pcre
  * adapt 31_eximmanpage
* fix package priorities to have them in sync with override again. (mh)
* Fix error in nb (Norwegian) translation.
  Thanks to Helge Hafting. (mh). Closes: #315775
* Standards-Version: 3.6.2, no changes needed. (mh)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Cambridge: exim/exim-src/src/daemon.c,v 1.13 2005/06/27 14:29:43 ph10 Exp $ */
 
2
 
1
3
/*************************************************
2
4
*     Exim - an Internet mail transport agent    *
3
5
*************************************************/
4
6
 
5
 
/* Copyright (c) University of Cambridge 1995 - 2004 */
 
7
/* Copyright (c) University of Cambridge 1995 - 2005 */
6
8
/* See the file NOTICE for conditions of use and distribution. */
7
9
 
8
10
/* Functions concerned with running Exim as a daemon */
37
39
static BOOL  accept_retry_select_failed;
38
40
 
39
41
static int   queue_run_count = 0;
40
 
static pid_t *queue_pid_slots;
41
 
static smtp_slot *smtp_slots;
 
42
static pid_t *queue_pid_slots = NULL;
 
43
static smtp_slot *smtp_slots = NULL;
42
44
 
43
45
static BOOL  write_pid = TRUE;
44
46
 
82
84
main_sigchld_handler(int sig)
83
85
{
84
86
sig = sig;    /* Keep picky compilers happy */
 
87
os_non_restarting_signal(SIGCHLD, SIG_DFL);
85
88
sigchld_seen = TRUE;
86
 
signal(SIGCHLD, SIG_DFL);
87
89
}
88
90
 
89
91
 
140
142
{
141
143
pid_t pid;
142
144
union sockaddr_46 interface_sockaddr;
143
 
SOCKLEN_T ifsize = sizeof(interface_sockaddr);
 
145
EXIM_SOCKLEN_T ifsize = sizeof(interface_sockaddr);
144
146
int dup_accept_socket = -1;
145
147
int max_for_this_host = 0;
 
148
int wfsize = 0;
 
149
int wfptr = 0;
 
150
int use_log_write_selector = log_write_selector;
 
151
uschar *whofrom = NULL;
 
152
 
146
153
void *reset_point = store_get(0);
147
154
 
148
155
/* Make the address available in ASCII representation, and also fish out
179
186
  goto ERROR_RETURN;
180
187
  }
181
188
 
 
189
/* Get the data for the local interface address. */
 
190
 
 
191
if (getsockname(accept_socket, (struct sockaddr *)(&interface_sockaddr),
 
192
     &ifsize) < 0)
 
193
  {
 
194
  log_write(0, LOG_MAIN|LOG_PANIC, "getsockname() failed: %s",
 
195
    strerror(errno));
 
196
  smtp_printf("421 Local problem: getsockname() failed; please try again later\r\n");
 
197
  goto ERROR_RETURN;
 
198
  }
 
199
 
 
200
interface_address = host_ntoa(-1, &interface_sockaddr, NULL, &interface_port);
 
201
DEBUG(D_interface) debug_printf("interface address=%s port=%d\n",
 
202
  interface_address, interface_port);
 
203
 
 
204
/* Build a string identifying the remote host and, if requested, the port and
 
205
the local interface data. This is for logging; at the end of this function the
 
206
memory is reclaimed. */
 
207
 
 
208
whofrom = string_append(whofrom, &wfsize, &wfptr, 3, "[", sender_host_address, "]");
 
209
 
 
210
if ((log_extra_selector & LX_incoming_port) != 0)
 
211
  whofrom = string_append(whofrom, &wfsize, &wfptr, 2, ":", string_sprintf("%d",
 
212
    sender_host_port));
 
213
 
 
214
if ((log_extra_selector & LX_incoming_interface) != 0)
 
215
  whofrom = string_append(whofrom, &wfsize, &wfptr, 4, " I=[",
 
216
    interface_address, "]:", string_sprintf("%d", interface_port));
 
217
 
 
218
whofrom[wfptr] = 0;    /* Terminate the newly-built string */
 
219
 
182
220
/* Check maximum number of connections. We do not check for reserved
183
221
connections or unacceptable hosts here. That is done in the subprocess because
184
222
it might take some time. */
191
229
    "please try again later.\r\n");
192
230
  log_write(L_connection_reject,
193
231
            LOG_MAIN, "Connection from %s refused: too many connections",
194
 
    sender_host_address);
 
232
    whofrom);
195
233
  goto ERROR_RETURN;
196
234
  }
197
235
 
210
248
    smtp_printf("421 Too much load; please try again later.\r\n");
211
249
    log_write(L_connection_reject,
212
250
              LOG_MAIN, "Connection from %s refused: load average = %.2f",
213
 
      sender_host_address, (double)load_average/1000.0);
 
251
      whofrom, (double)load_average/1000.0);
214
252
    goto ERROR_RETURN;
215
253
    }
216
254
  }
230
268
    {
231
269
    if (!expand_string_forcedfail)
232
270
      log_write(0, LOG_MAIN|LOG_PANIC, "expansion of smtp_accept_max_per_host "
233
 
        "failed for host %s: %s", sender_host_address, expand_string_message);
 
271
        "failed for %s: %s", whofrom, expand_string_message);
234
272
    }
235
273
  /* For speed, interpret a decimal number inline here */
236
274
  else
240
278
      max_for_this_host = max_for_this_host * 10 + *s++ - '0';
241
279
    if (*s != 0)
242
280
      log_write(0, LOG_MAIN|LOG_PANIC, "expansion of smtp_accept_max_per_host "
243
 
        "for host %s contains non-digit: %s", sender_host_address, expanded);
 
281
        "for %s contains non-digit: %s", whofrom, expanded);
244
282
    }
245
283
  }
246
284
 
283
321
      "from this IP address; please try again later.\r\n");
284
322
    log_write(L_connection_reject,
285
323
              LOG_MAIN, "Connection from %s refused: too many connections "
286
 
      "from that IP address", sender_host_address);
 
324
      "from that IP address", whofrom);
287
325
    goto ERROR_RETURN;
288
326
    }
289
327
  }
290
328
 
291
329
/* OK, the connection count checks have been passed. Before we can fork the
292
330
accepting process, we must first log the connection if requested. This logging
293
 
used to happen in the subprocess, but doing that that means that the value of
 
331
used to happen in the subprocess, but doing that means that the value of
294
332
smtp_accept_count can be out of step by the time it is logged. So we have to do
295
333
the logging here and accept the performance cost. Note that smtp_accept_count
296
334
hasn't yet been incremented to take account of this connection.
297
335
 
298
336
In order to minimize the cost (because this is going to happen for every
299
 
connection), do a selector test here. This saves ploughing through the
300
 
generalized logging code each time when the selector is false. */
 
337
connection), do a preliminary selector test here. This saves ploughing through
 
338
the generalized logging code each time when the selector is false. If the
 
339
selector is set, check whether the host is on the list for logging. If not,
 
340
arrange to unset the selector in the subprocess. */
301
341
 
302
342
if ((log_write_selector & L_smtp_connection) != 0)
303
343
  {
304
 
  if ((log_extra_selector & LX_incoming_port) == 0)
305
 
    log_write(0, LOG_MAIN, "SMTP connection from [%s] "
306
 
      "(TCP/IP connection count = %d)",
307
 
      sender_host_address, smtp_accept_count + 1);
 
344
  uschar *list = hosts_connection_nolog;
 
345
  if (list != NULL && verify_check_host(&list) == OK)
 
346
    use_log_write_selector &= ~L_smtp_connection;
308
347
  else
309
 
    log_write(0, LOG_MAIN, "SMTP connection from [%s]:%d "
310
 
      "(TCP/IP connection count = %d)",
311
 
      sender_host_address, sender_host_port, smtp_accept_count + 1);
312
 
  }
313
 
 
314
 
/* Get the data for the local interface address. We do this in the
315
 
main process so that we can easily give up if it fails. */
316
 
 
317
 
if (getsockname(accept_socket, (struct sockaddr *)(&interface_sockaddr),
318
 
     &ifsize) < 0)
319
 
  {
320
 
  log_write(0, LOG_MAIN|LOG_PANIC, "getsockname() failed: %s",
321
 
    strerror(errno));
322
 
  smtp_printf("421 Local problem: getsockname() failed; please try again later\r\n");
323
 
  goto ERROR_RETURN;
 
348
    log_write(L_smtp_connection, LOG_MAIN, "SMTP connection from %s "
 
349
      "(TCP/IP connection count = %d)", whofrom, smtp_accept_count + 1);
324
350
  }
325
351
 
326
352
/* Now we can fork the accepting process; do a lookup tidy, just in case any
335
361
  {
336
362
  int i;
337
363
  int queue_only_reason = 0;
 
364
  int old_pool = store_pool;
 
365
  int save_debug_selector = debug_selector;
338
366
  BOOL local_queue_only;
339
367
  #ifdef SA_NOCLDWAIT
340
368
  struct sigaction act;
341
369
  #endif
342
370
 
343
 
  /* Get the local interface address into some permanent store, and also set
344
 
  the port. */
345
 
 
346
 
  uschar *buffer = store_get_perm(sizeof(ip_address_item));
347
 
  interface_address = host_ntoa(-1, &interface_sockaddr, buffer,
348
 
    &interface_port);
349
 
  DEBUG(D_interface) debug_printf("interface address=%s port=%d\n",
350
 
    interface_address, interface_port);
 
371
  /* May have been modified for the subprocess */
 
372
 
 
373
  log_write_selector = use_log_write_selector;
 
374
 
 
375
  /* Get the local interface address into permanent store */
 
376
 
 
377
  store_pool = POOL_PERM;
 
378
  interface_address = string_copy(interface_address);
 
379
  store_pool = old_pool;
 
380
 
 
381
  /* Check for a tls-on-connect port */
 
382
 
 
383
  if (host_is_tls_on_connect_port(interface_port)) tls_on_connect = TRUE;
351
384
 
352
385
  /* Expand smtp_active_hostname if required. We do not do this any earlier,
353
386
  because it may depend on the local interface address (indeed, that is most
386
419
  extensive comment before the reception loop in exim.c for a fuller
387
420
  explanation of this logic. */
388
421
 
389
 
  for (i = 0; i < listen_socket_count; i++) close(listen_sockets[i]);
 
422
  for (i = 0; i < listen_socket_count; i++) (void)close(listen_sockets[i]);
390
423
 
391
424
  #ifdef SA_NOCLDWAIT
392
425
  act.sa_handler = SIG_IGN;
400
433
  /* Attempt to get an id from the sending machine via the RFC 1413
401
434
  protocol. We do this in the sub-process in order not to hold up the
402
435
  main process if there is any delay. Then set up the fullhost information
403
 
  in case there is no HELO/EHLO. */
404
 
 
 
436
  in case there is no HELO/EHLO.
 
437
 
 
438
  If debugging is enabled only for the daemon, we must turn if off while
 
439
  finding the id, but turn it on again afterwards so that information about the
 
440
  incoming connection is output. */
 
441
 
 
442
  if (debug_daemon) debug_selector = 0;
405
443
  verify_get_ident(IDENT_PORT);
406
444
  host_build_sender_fullhost();
 
445
  debug_selector = save_debug_selector;
407
446
 
408
447
  DEBUG(D_any)
409
448
    debug_printf("Process %d is handling incoming connection from %s\n",
410
449
      (int)getpid(), sender_fullhost);
411
450
 
 
451
  /* Now disable debugging permanently if it's required only for the daemon
 
452
  process. */
 
453
 
 
454
  if (debug_daemon) debug_selector = 0;
 
455
 
412
456
  /* If there are too many child processes for immediate delivery,
413
457
  set the local_queue_only flag, which is initialized from the
414
458
  configured value and may therefore already be TRUE. Leave logging
452
496
      {
453
497
      BOOL ok = receive_msg(FALSE);
454
498
      search_tidyup();                    /* Close cached databases */
455
 
      if (!ok) _exit(EXIT_SUCCESS);       /* Connection was dropped */
 
499
      if (!ok)                            /* Connection was dropped */
 
500
        {
 
501
        mac_smtp_fflush();
 
502
        _exit(EXIT_SUCCESS);
 
503
        }
456
504
      if (message_id[0] == 0) continue;   /* No message was accepted */
457
505
      }
458
506
    else
555
603
 
556
604
      if ((dpid = fork()) == 0)
557
605
        {
558
 
        fclose(smtp_in);
559
 
        fclose(smtp_out);
 
606
        (void)fclose(smtp_in);
 
607
        (void)fclose(smtp_out);
560
608
 
561
609
        /* Don't ever molest the parent's SSL connection, but do clean up
562
610
        the data structures if necessary. */
643
691
      strerror(errno));
644
692
  smtp_out = NULL;
645
693
  }
646
 
else close(accept_socket);
 
694
else (void)close(accept_socket);
647
695
 
648
696
if (smtp_in != NULL)
649
697
  {
652
700
      strerror(errno));
653
701
  smtp_in = NULL;
654
702
  }
655
 
else close(dup_accept_socket);
 
703
else (void)close(dup_accept_socket);
656
704
 
657
705
/* Release any store used in this process, including the store used for holding
658
706
the incoming host address and an expanded active_hostname. */
728
776
 
729
777
 
730
778
 
 
779
/*************************************************
 
780
*         Handle terminating subprocesses        *
 
781
*************************************************/
 
782
 
 
783
/* Handle the termination of child processes. Theoretically, this need be done
 
784
only when sigchld_seen is TRUE, but rumour has it that some systems lose
 
785
SIGCHLD signals at busy times, so to be on the safe side, this function is
 
786
called each time round. It shouldn't be too expensive.
 
787
 
 
788
Arguments:  none
 
789
Returns:    nothing
 
790
*/
 
791
 
 
792
static void
 
793
handle_ending_processes(void)
 
794
{
 
795
int status;
 
796
pid_t pid;
 
797
 
 
798
while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
 
799
  {
 
800
  int i;
 
801
  DEBUG(D_any) debug_printf("child %d ended: status=0x%x\n", (int)pid,
 
802
    status);
 
803
 
 
804
  /* If it's a listening daemon for which we are keeping track of individual
 
805
  subprocesses, deal with an accepting process that has terminated. */
 
806
 
 
807
  if (smtp_slots != NULL)
 
808
    {
 
809
    for (i = 0; i < smtp_accept_max; i++)
 
810
      {
 
811
      if (smtp_slots[i].pid == pid)
 
812
        {
 
813
        if (smtp_slots[i].host_address != NULL)
 
814
          store_free(smtp_slots[i].host_address);
 
815
        smtp_slots[i] = empty_smtp_slot;
 
816
        if (--smtp_accept_count < 0) smtp_accept_count = 0;
 
817
        DEBUG(D_any) debug_printf("%d SMTP accept process%s now running\n",
 
818
          smtp_accept_count, (smtp_accept_count == 1)? "" : "es");
 
819
        break;
 
820
        }
 
821
      }
 
822
    if (i < smtp_accept_max) continue;  /* Found an accepting process */
 
823
    }
 
824
 
 
825
  /* If it wasn't an accepting process, see if it was a queue-runner
 
826
  process that we are tracking. */
 
827
 
 
828
  if (queue_pid_slots != NULL)
 
829
    {
 
830
    for (i = 0; i < queue_run_max; i++)
 
831
      {
 
832
      if (queue_pid_slots[i] == pid)
 
833
        {
 
834
        queue_pid_slots[i] = 0;
 
835
        if (--queue_run_count < 0) queue_run_count = 0;
 
836
        DEBUG(D_any) debug_printf("%d queue-runner process%s now running\n",
 
837
          queue_run_count, (queue_run_count == 1)? "" : "es");
 
838
        break;
 
839
        }
 
840
      }
 
841
    }
 
842
  }
 
843
}
 
844
 
731
845
 
732
846
 
733
847
/*************************************************
850
964
  order to perform an "open" on the kernel memory file). */
851
965
 
852
966
  #ifdef LOAD_AVG_NEEDS_ROOT
853
 
  if (queue_only_load >= 0 || smtp_load_reserve >= 0) (void)os_getloadavg();
 
967
  if (queue_only_load >= 0 || smtp_load_reserve >= 0 ||
 
968
       (deliver_queue_load_max >= 0 && deliver_drop_privilege))
 
969
    (void)os_getloadavg();
854
970
  #endif
855
971
 
856
972
  /* If -oX was used, disable the writing of a pid file unless -oP was
1074
1190
    }
1075
1191
  }
1076
1192
 
1077
 
/* We now close all open file descriptors that we know about, and disconnect
1078
 
from the controlling terminal, unless background_daemon is unset. This is
1079
 
always unset when debugging, but can also be forced. Most modern Unixes seem to
1080
 
have setsid() for getting rid of the controlling terminal. For any OS that
1081
 
doesn't, setsid() can be #defined as a no-op, or as something else. */
 
1193
/* The variable background_daemon is always false when debugging, but
 
1194
can also be forced false in order to keep a non-debugging daemon in the
 
1195
foreground. If background_daemon is true, close all open file descriptors that
 
1196
we know about, but then re-open stdin, stdout, and stderr to /dev/null.
 
1197
 
 
1198
This is protection against any called functions (in libraries, or in
 
1199
Perl, or whatever) that think they can write to stderr (or stdout). Before this
 
1200
was added, it was quite likely that an SMTP connection would use one of these
 
1201
file descriptors, in which case writing random stuff to it caused chaos.
 
1202
 
 
1203
Then disconnect from the controlling terminal, Most modern Unixes seem to have
 
1204
setsid() for getting rid of the controlling terminal. For any OS that doesn't,
 
1205
setsid() can be #defined as a no-op, or as something else. */
1082
1206
 
1083
1207
if (background_daemon)
1084
1208
  {
1085
 
  log_close_all();  /* Just in case anything was logged earlier */
1086
 
  search_tidyup();  /* Just in case any were used in reading the config. */
1087
 
  close(0);         /* Get rid of stdin/stdout/stderr */
1088
 
  close(1);
1089
 
  close(2);
 
1209
  log_close_all();    /* Just in case anything was logged earlier */
 
1210
  search_tidyup();    /* Just in case any were used in reading the config. */
 
1211
  (void)close(0);           /* Get rid of stdin/stdout/stderr */
 
1212
  (void)close(1);
 
1213
  (void)close(2);
 
1214
  exim_nullstd();     /* Connect stdin/stdout/stderr to /dev/null */
1090
1215
  log_stderr = NULL;  /* So no attempt to copy paniclog output */
1091
1216
 
1092
1217
  /* If the parent process of this one has pid == 1, we are re-initializing the
1093
 
  daemon as the result of a SIGHUP. In this case, there is no need to do any
1094
 
  forking, because the controlling terminal has long gone. Otherwise, fork,
1095
 
  in case current process is a process group leader (see 'man setsid' for an
1096
 
  explanation). */
 
1218
  daemon as the result of a SIGHUP. In this case, there is no need to do
 
1219
  anything, because the controlling terminal has long gone. Otherwise, fork, in
 
1220
  case current process is a process group leader (see 'man setsid' for an
 
1221
  explanation) before calling setsid(). */
1097
1222
 
1098
1223
  if (getppid() != 1)
1099
1224
    {
1124
1249
    {
1125
1250
    BOOL wildcard;
1126
1251
    ip_address_item *ipa2;
1127
 
    int retries = 9;
1128
1252
    int af;
1129
1253
 
1130
1254
    if (Ustrchr(ipa->address, ':') != NULL)
1196
1320
        {
1197
1321
        DEBUG(D_any) debug_printf("wildcard IPv4 bind() failed after IPv6 "
1198
1322
          "listen() success; EADDRINUSE ignored\n");
1199
 
        close(listen_sockets[sk]);
 
1323
        (void)close(listen_sockets[sk]);
1200
1324
        goto SKIP_SOCKET;
1201
1325
        }
1202
1326
      msg = US strerror(errno);
1203
1327
      addr = wildcard? ((af == AF_INET6)? US"(any IPv6)" : US"(any IPv4)") :
1204
1328
        ipa->address;
1205
 
      if (retries-- <= 0)
 
1329
      if (daemon_startup_retries <= 0)
1206
1330
        log_write(0, LOG_MAIN|LOG_PANIC_DIE,
1207
1331
          "socket bind() to port %d for address %s failed: %s: "
1208
1332
          "daemon abandoned", ipa->port, addr, msg);
1209
1333
      log_write(0, LOG_MAIN, "socket bind() to port %d for address %s "
1210
 
        "failed: %s: waiting before trying again", ipa->port, addr, msg);
1211
 
      sleep(30);
 
1334
        "failed: %s: waiting %s before trying again (%d more %s)",
 
1335
        ipa->port, addr, msg, readconf_printtime(daemon_startup_sleep),
 
1336
        daemon_startup_retries, (daemon_startup_retries > 1)? "tries" : "try");
 
1337
      daemon_startup_retries--;
 
1338
      sleep(daemon_startup_sleep);
1212
1339
      }
1213
1340
 
1214
1341
    DEBUG(D_any)
1239
1366
 
1240
1367
    DEBUG(D_any) debug_printf("wildcard IPv4 listen() failed after IPv6 "
1241
1368
      "listen() success; EADDRINUSE ignored\n");
1242
 
    close(listen_sockets[sk]);
 
1369
    (void)close(listen_sockets[sk]);
1243
1370
 
1244
1371
    /* Come here if there has been a problem with the socket which we
1245
1372
    are going to ignore. We remove the address from the chain, and back up the
1290
1417
  f = Ufopen(pid_file_path, "wb");
1291
1418
  if (f != NULL)
1292
1419
    {
1293
 
    fprintf(f, "%d\n", (int)getpid());
1294
 
    fchmod(fileno(f), 0644);
1295
 
    fclose(f);
 
1420
    (void)fprintf(f, "%d\n", (int)getpid());
 
1421
    (void)fchmod(fileno(f), 0644);
 
1422
    (void)fclose(f);
1296
1423
    DEBUG(D_any) debug_printf("pid written to %s\n", pid_file_path);
1297
1424
    }
1298
1425
  else
1329
1456
/* Set up the handler for termination of child processes. */
1330
1457
 
1331
1458
sigchld_seen = FALSE;
1332
 
signal(SIGCHLD, main_sigchld_handler);
 
1459
os_non_restarting_signal(SIGCHLD, main_sigchld_handler);
1333
1460
 
1334
1461
/* If we are to run the queue periodically, pretend the alarm has just gone
1335
1462
off. This will cause the first queue-runner to get kicked off straight away. */
1341
1468
 
1342
1469
if (daemon_listen)
1343
1470
  {
1344
 
  int i;
 
1471
  int i, j;
 
1472
  int smtp_ports = 0;
 
1473
  int smtps_ports = 0;
1345
1474
  ip_address_item *ipa;
1346
1475
  uschar *p = big_buffer;
1347
1476
  uschar *qinfo = (queue_interval > 0)?
1349
1478
    :
1350
1479
    US"no queue runs";
1351
1480
 
1352
 
  #ifdef SUPPORT_TLS
1353
 
  uschar *sinfo = tls_on_connect ?
1354
 
      US"listening for SMTPS on" : US"listening for SMTP on";
1355
 
  #else
1356
 
  uschar *sinfo = US"listening for SMTP on";
1357
 
  #endif
1358
 
 
1359
1481
  /* Build a list of listening addresses in big_buffer, but limit it to 10
1360
 
  items. The style is for backwards compatibility. */
1361
 
 
1362
 
  for (i = 0, ipa = addresses; i < 10 && ipa != NULL; i++, ipa = ipa->next)
1363
 
     {
1364
 
     if (ipa->address[0] == ':' && ipa->address[1] == 0)
 
1482
  items. The style is for backwards compatibility.
 
1483
 
 
1484
  It is now possible to have some ports listening for SMTPS (the old,
 
1485
  deprecated protocol that starts TLS without using STARTTLS), and others
 
1486
  listening for standard SMTP. Keep their listings separate. */
 
1487
 
 
1488
  for (j = 0; j < 2; j++)
 
1489
    {
 
1490
    for (i = 0, ipa = addresses; i < 10 && ipa != NULL; i++, ipa = ipa->next)
1365
1491
       {
1366
 
       if (ipa->next != NULL && ipa->next->address[0] == 0 &&
1367
 
           ipa->next->port == ipa->port)
 
1492
       /* First time round, look for SMTP ports; second time round, look for
 
1493
       SMTPS ports. For the first one of each, insert leading text. */
 
1494
 
 
1495
       if (host_is_tls_on_connect_port(ipa->port) == (j > 0))
1368
1496
         {
1369
 
         (void)sprintf(CS p, " port %d (IPv6 and IPv4)", ipa->port);
1370
 
         ipa = ipa->next;
 
1497
         if (j == 0)
 
1498
           {
 
1499
           if (smtp_ports++ == 0)
 
1500
             {
 
1501
             memcpy(p, "SMTP on", 8);
 
1502
             p += 7;
 
1503
             }
 
1504
           }
 
1505
         else
 
1506
           {
 
1507
           if (smtps_ports++ == 0)
 
1508
             {
 
1509
             (void)sprintf(CS p, "%sSMTPS on",
 
1510
               (smtp_ports == 0)? "":" and for ");
 
1511
             while (*p != 0) p++;
 
1512
             }
 
1513
           }
 
1514
 
 
1515
         /* Now the information about the port (and sometimes interface) */
 
1516
 
 
1517
         if (ipa->address[0] == ':' && ipa->address[1] == 0)
 
1518
           {
 
1519
           if (ipa->next != NULL && ipa->next->address[0] == 0 &&
 
1520
               ipa->next->port == ipa->port)
 
1521
             {
 
1522
             (void)sprintf(CS p, " port %d (IPv6 and IPv4)", ipa->port);
 
1523
             ipa = ipa->next;
 
1524
             }
 
1525
           else if (ipa->v6_include_v4)
 
1526
             (void)sprintf(CS p, " port %d (IPv6 with IPv4)", ipa->port);
 
1527
           else
 
1528
             (void)sprintf(CS p, " port %d (IPv6)", ipa->port);
 
1529
           }
 
1530
         else if (ipa->address[0] == 0)
 
1531
           (void)sprintf(CS p, " port %d (IPv4)", ipa->port);
 
1532
         else
 
1533
           (void)sprintf(CS p, " [%s]:%d", ipa->address, ipa->port);
 
1534
         while (*p != 0) p++;
1371
1535
         }
1372
 
       else if (ipa->v6_include_v4)
1373
 
         (void)sprintf(CS p, " port %d (IPv6 with IPv4)", ipa->port);
1374
 
       else
1375
 
         (void)sprintf(CS p, " port %d (IPv6)", ipa->port);
1376
1536
       }
1377
 
     else if (ipa->address[0] == 0)
1378
 
       (void)sprintf(CS p, " port %d (IPv4)", ipa->port);
1379
 
     else
1380
 
       (void)sprintf(CS p, " [%s]:%d", ipa->address, ipa->port);
1381
 
     while (*p != 0) p++;
1382
 
     }
1383
1537
 
1384
 
  if (ipa != NULL) memcpy(p, " ...", 5);
 
1538
    if (ipa != NULL)
 
1539
      {
 
1540
      memcpy(p, " ...", 5);
 
1541
      p += 4;
 
1542
      }
 
1543
    }
1385
1544
 
1386
1545
  log_write(0, LOG_MAIN,
1387
 
    "exim %s daemon started: pid=%d, %s, %s%s",
1388
 
    version_string, getpid(), qinfo, sinfo, big_buffer);
1389
 
  set_process_info("daemon: %s, %s%s", qinfo, sinfo, big_buffer);
 
1546
    "exim %s daemon started: pid=%d, %s, listening for %s",
 
1547
    version_string, getpid(), qinfo, big_buffer);
 
1548
  set_process_info("daemon: %s, listening for %s", qinfo, big_buffer);
1390
1549
  }
1391
1550
 
1392
1551
else
1399
1558
  }
1400
1559
 
1401
1560
 
1402
 
/* Close the log so it can be renamed and moved. This process doesn't write
1403
 
to the log again, unless it is about to die or exec and in the latter case
1404
 
it closes the log first. */
 
1561
/* Close the log so it can be renamed and moved. In the few cases below where
 
1562
this long-running process writes to the log (always exceptional conditions), it
 
1563
closes the log afterwards, for the same reason. */
1405
1564
 
1406
1565
log_close_all();
1407
1566
 
1421
1580
  struct sockaddr_in accepted;
1422
1581
  #endif
1423
1582
 
1424
 
  SOCKLEN_T len = sizeof(accepted);
1425
 
  int status;
 
1583
  EXIM_SOCKLEN_T len = sizeof(accepted);
1426
1584
  pid_t pid;
1427
1585
 
1428
1586
  /* This code is placed first in the loop, so that it gets obeyed at the
1443
1601
      if ((pid = fork()) == 0)
1444
1602
        {
1445
1603
        int sk;
 
1604
 
1446
1605
        DEBUG(D_any) debug_printf("Starting queue-runner: pid %d\n",
1447
1606
          (int)getpid());
1448
1607
 
 
1608
        /* Disable debugging if it's required only for the daemon process. We
 
1609
        leave the above message, because it ties up with the "child ended"
 
1610
        debugging messages. */
 
1611
 
 
1612
        if (debug_daemon) debug_selector = 0;
 
1613
 
1449
1614
        /* Close any open listening sockets in the child */
1450
1615
 
1451
 
        for (sk = 0; sk < listen_socket_count; sk++) close(listen_sockets[sk]);
 
1616
        for (sk = 0; sk < listen_socket_count; sk++)
 
1617
          (void)close(listen_sockets[sk]);
1452
1618
 
1453
1619
        /* Reset SIGHUP and SIGCHLD in the child in both cases. */
1454
1620
 
1487
1653
        {
1488
1654
        log_write(0, LOG_MAIN|LOG_PANIC, "daemon: fork of queue-runner "
1489
1655
          "process failed: %s", strerror(errno));
 
1656
        log_close_all();
1490
1657
        }
1491
1658
      else
1492
1659
        {
1523
1690
 
1524
1691
  if (daemon_listen)
1525
1692
    {
1526
 
    int sk, lcount;
 
1693
    int sk, lcount, select_errno;
1527
1694
    int max_socket = 0;
1528
1695
    BOOL select_failed = FALSE;
1529
1696
    fd_set select_listen;
1537
1704
 
1538
1705
    DEBUG(D_any) debug_printf("Listening...\n");
1539
1706
 
1540
 
    if ((lcount = select(max_socket + 1, (SELECT_ARG2_TYPE *)&select_listen,
1541
 
         NULL, NULL, NULL)) < 0)
 
1707
    /* In rare cases we may have had a SIGCHLD signal in the time between
 
1708
    setting the handler (below) and getting back here. If so, pretend that the
 
1709
    select() was interrupted so that we reap the child. This might still leave
 
1710
    a small window when a SIGCHLD could get lost. However, since we use SIGCHLD
 
1711
    only to do the reaping more quickly, it shouldn't result in anything other
 
1712
    than a delay until something else causes a wake-up. */
 
1713
 
 
1714
    if (sigchld_seen)
 
1715
      {
 
1716
      lcount = -1;
 
1717
      errno = EINTR;
 
1718
      }
 
1719
    else
 
1720
      {
 
1721
      lcount = select(max_socket + 1, (SELECT_ARG2_TYPE *)&select_listen,
 
1722
        NULL, NULL, NULL);
 
1723
      }
 
1724
 
 
1725
    if (lcount < 0)
1542
1726
      {
1543
1727
      select_failed = TRUE;
1544
1728
      lcount = 1;
1545
1729
      }
1546
1730
 
 
1731
    /* Clean up any subprocesses that may have terminated. We need to do this
 
1732
    here so that smtp_accept_max_per_host works when a connection to that host
 
1733
    has completed, and we are about to accept a new one. When this code was
 
1734
    later in the sequence, a new connection could be rejected, even though an
 
1735
    old one had just finished. Preserve the errno from any select() failure for
 
1736
    the use of the common select/accept error processing below. */
 
1737
 
 
1738
    select_errno = errno;
 
1739
    handle_ending_processes();
 
1740
    errno = select_errno;
 
1741
 
1547
1742
    /* Loop for all the sockets that are currently ready to go. If select
1548
 
    actually failed, we have set the count to 1 and a flag, so as to use the
1549
 
    common error code for select/accept below. */
 
1743
    actually failed, we have set the count to 1 and select_failed=TRUE, so as
 
1744
    to use the common error code for select/accept below. */
1550
1745
 
1551
1746
    while (lcount-- > 0)
1552
1747
      {
1594
1789
              accept_retry_select_failed? "select" : "accept",
1595
1790
              (accept_retry_count == 1)? "" : "s",
1596
1791
              strerror(accept_retry_errno));
 
1792
            log_close_all();
1597
1793
            accept_retry_count = 0;
1598
1794
            accept_retry_errno = errno;
1599
1795
            accept_retry_select_failed = select_failed;
1611
1807
            accept_retry_select_failed? "select" : "accept",
1612
1808
            (accept_retry_count == 1)? "" : "s",
1613
1809
            strerror(accept_retry_errno));
 
1810
          log_close_all();
1614
1811
          accept_retry_count = 0;
1615
1812
          }
1616
1813
        }
1636
1833
    tv.tv_sec = queue_interval;
1637
1834
    tv.tv_usec = 0;
1638
1835
    select(0, NULL, NULL, NULL, &tv);
1639
 
    }
1640
 
 
1641
 
  /* Handle the termination of a child process. Theoretically, this need
1642
 
  be done only when sigchld_seen is TRUE, but rumour has it that some systems
1643
 
  lose SIGCHLD signals at busy times, so to be on the safe side, just
1644
 
  do it each time round. It shouldn't be too expensive. */
1645
 
 
1646
 
  while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
1647
 
    {
1648
 
    int i;
1649
 
    DEBUG(D_any) debug_printf("child %d ended: status=0x%x\n", (int)pid,
1650
 
      status);
1651
 
 
1652
 
    /* If it's a listening daemon, deal with an accepting process that has
1653
 
    terminated. */
1654
 
 
1655
 
    if (daemon_listen)
1656
 
      {
1657
 
      for (i = 0; i < smtp_accept_max; i++)
1658
 
        {
1659
 
        if (smtp_slots[i].pid == pid)
1660
 
          {
1661
 
          if (smtp_slots[i].host_address != NULL)
1662
 
            store_free(smtp_slots[i].host_address);
1663
 
          smtp_slots[i] = empty_smtp_slot;
1664
 
          if (--smtp_accept_count < 0) smtp_accept_count = 0;
1665
 
          DEBUG(D_any) debug_printf("%d SMTP accept process%s now running\n",
1666
 
            smtp_accept_count, (smtp_accept_count == 1)? "" : "es");
1667
 
          break;
1668
 
          }
1669
 
        }
1670
 
      if (i < smtp_accept_max) continue;  /* Found an accepting process */
1671
 
      }
1672
 
 
1673
 
    /* If it wasn't an accepting process, see if it was a queue-runner
1674
 
    process, if we are keeping track of them. */
1675
 
 
1676
 
    if (queue_interval > 0)
1677
 
      {
1678
 
      for (i = 0; i < queue_run_max; i++)
1679
 
        {
1680
 
        if (queue_pid_slots[i] == pid)
1681
 
          {
1682
 
          queue_pid_slots[i] = 0;
1683
 
          if (--queue_run_count < 0) queue_run_count = 0;
1684
 
          DEBUG(D_any) debug_printf("%d queue-runner process%s now running\n",
1685
 
            queue_run_count, (queue_run_count == 1)? "" : "es");
1686
 
          break;
1687
 
          }
1688
 
        }
1689
 
      }
 
1836
    handle_ending_processes();
1690
1837
    }
1691
1838
 
1692
1839
  /* Re-enable the SIGCHLD handler if it has been run. It can't do it
1695
1842
  if (sigchld_seen)
1696
1843
    {
1697
1844
    sigchld_seen = FALSE;
1698
 
    signal(SIGCHLD, main_sigchld_handler);
 
1845
    os_non_restarting_signal(SIGCHLD, main_sigchld_handler);
1699
1846
    }
1700
1847
 
1701
1848
  /* Handle being woken by SIGHUP. We know at this point that the result
1703
1850
  closing the listening sockets so that they can be reused. Cancel any pending
1704
1851
  alarm in case it is just about to go off, and set SIGHUP to be ignored so
1705
1852
  that another HUP in quick succession doesn't clobber the new daemon before it
1706
 
  gets going. All log files get closed by the close-on-exec flag. */
 
1853
  gets going. All log files get closed by the close-on-exec flag; however, if
 
1854
  the exec fails, we need to close the logs. */
1707
1855
 
1708
1856
  if (sighup_seen)
1709
1857
    {
1710
1858
    int sk;
1711
1859
    log_write(0, LOG_MAIN, "pid %d: SIGHUP received: re-exec daemon",
1712
1860
      getpid());
1713
 
    for (sk = 0; sk < listen_socket_count; sk++) close(listen_sockets[sk]);
 
1861
    for (sk = 0; sk < listen_socket_count; sk++)
 
1862
      (void)close(listen_sockets[sk]);
1714
1863
    alarm(0);
1715
1864
    signal(SIGHUP, SIG_IGN);
1716
1865
    sighup_argv[0] = exim_path;
1718
1867
    execv(CS exim_path, (char *const *)sighup_argv);
1719
1868
    log_write(0, LOG_MAIN|LOG_PANIC_DIE, "pid %d: exec of %s failed: %s",
1720
1869
      getpid(), exim_path, strerror(errno));
 
1870
    log_close_all();
1721
1871
    }
1722
1872
 
1723
1873
  }   /* End of main loop */