1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
|
/*
* Copyright (c) 1999-2002 Sendmail, Inc. and its suppliers.
* All rights reserved.
*
* By using this file, you agree to the terms and conditions set
* forth in the LICENSE file which can be found at the top level of
* the sendmail distribution.
*
*/
#include <sm/gen.h>
SM_RCSID("@(#)$Id: listener.c,v 8.85.2.1 2002/08/09 22:13:36 gshapiro Exp $")
/*
** listener.c -- threaded network listener
*/
#include "libmilter.h"
#include <sm/errstring.h>
# if NETINET || NETINET6
# include <arpa/inet.h>
# endif /* NETINET || NETINET6 */
static smutex_t L_Mutex;
static int L_family;
static SOCKADDR_LEN_T L_socksize;
static socket_t listenfd = INVALID_SOCKET;
static socket_t mi_milteropen __P((char *, int, char *));
/*
** MI_OPENSOCKET -- create the socket where this filter and the MTA will meet
**
** Parameters:
** conn -- connection description
** backlog -- listen backlog
** dbg -- debug level
** smfi -- filter structure to use
**
** Return value:
** MI_SUCCESS/MI_FAILURE
*/
int
mi_opensocket(conn, backlog, dbg, smfi)
char *conn;
int backlog;
int dbg;
smfiDesc_ptr smfi;
{
if (smfi == NULL || conn == NULL)
return MI_FAILURE;
if (ValidSocket(listenfd))
return MI_SUCCESS;
if (dbg > 0)
{
smi_log(SMI_LOG_DEBUG,
"%s: Opening listen socket on conn %s",
smfi->xxfi_name, conn);
}
(void) smutex_init(&L_Mutex);
(void) smutex_lock(&L_Mutex);
listenfd = mi_milteropen(conn, backlog, smfi->xxfi_name);
if (!ValidSocket(listenfd))
{
smi_log(SMI_LOG_FATAL,
"%s: Unable to create listening socket on conn %s",
smfi->xxfi_name, conn);
(void) smutex_unlock(&L_Mutex);
return MI_FAILURE;
}
return MI_SUCCESS;
}
/*
** MI_MILTEROPEN -- setup socket to listen on
**
** Parameters:
** conn -- connection description
** backlog -- listen backlog
** name -- name for logging
**
** Returns:
** socket upon success, error code otherwise.
**
** Side effect:
** sets sockpath if UNIX socket.
*/
#if NETUNIX
static char *sockpath = NULL;
#endif /* NETUNIX */
static socket_t
mi_milteropen(conn, backlog, name)
char *conn;
int backlog;
char *name;
{
socket_t sock;
int sockopt = 1;
int fdflags;
size_t len = 0;
char *p;
char *colon;
char *at;
SOCKADDR addr;
if (conn == NULL || conn[0] == '\0')
{
smi_log(SMI_LOG_ERR, "%s: empty or missing socket information",
name);
return INVALID_SOCKET;
}
(void) memset(&addr, '\0', sizeof addr);
/* protocol:filename or protocol:port@host */
p = conn;
colon = strchr(p, ':');
if (colon != NULL)
{
*colon = '\0';
if (*p == '\0')
{
#if NETUNIX
/* default to AF_UNIX */
addr.sa.sa_family = AF_UNIX;
L_socksize = sizeof (struct sockaddr_un);
#else /* NETUNIX */
# if NETINET
/* default to AF_INET */
addr.sa.sa_family = AF_INET;
L_socksize = sizeof addr.sin;
# else /* NETINET */
# if NETINET6
/* default to AF_INET6 */
addr.sa.sa_family = AF_INET6;
L_socksize = sizeof addr.sin6;
# else /* NETINET6 */
/* no protocols available */
smi_log(SMI_LOG_ERR,
"%s: no valid socket protocols available",
name);
return INVALID_SOCKET;
# endif /* NETINET6 */
# endif /* NETINET */
#endif /* NETUNIX */
}
#if NETUNIX
else if (strcasecmp(p, "unix") == 0 ||
strcasecmp(p, "local") == 0)
{
addr.sa.sa_family = AF_UNIX;
L_socksize = sizeof (struct sockaddr_un);
}
#endif /* NETUNIX */
#if NETINET
else if (strcasecmp(p, "inet") == 0)
{
addr.sa.sa_family = AF_INET;
L_socksize = sizeof addr.sin;
}
#endif /* NETINET */
#if NETINET6
else if (strcasecmp(p, "inet6") == 0)
{
addr.sa.sa_family = AF_INET6;
L_socksize = sizeof addr.sin6;
}
#endif /* NETINET6 */
else
{
smi_log(SMI_LOG_ERR, "%s: unknown socket type %s",
name, p);
return INVALID_SOCKET;
}
*colon++ = ':';
}
else
{
colon = p;
#if NETUNIX
/* default to AF_UNIX */
addr.sa.sa_family = AF_UNIX;
L_socksize = sizeof (struct sockaddr_un);
#else /* NETUNIX */
# if NETINET
/* default to AF_INET */
addr.sa.sa_family = AF_INET;
L_socksize = sizeof addr.sin;
# else /* NETINET */
# if NETINET6
/* default to AF_INET6 */
addr.sa.sa_family = AF_INET6;
L_socksize = sizeof addr.sin6;
# else /* NETINET6 */
smi_log(SMI_LOG_ERR, "%s: unknown socket type %s",
name, p);
return INVALID_SOCKET;
# endif /* NETINET6 */
# endif /* NETINET */
#endif /* NETUNIX */
}
#if NETUNIX
if (addr.sa.sa_family == AF_UNIX)
{
# if 0
long sff = SFF_SAFEDIRPATH|SFF_OPENASROOT|SFF_NOLINK|SFF_CREAT|SFF_MUSTOWN;
# endif /* 0 */
at = colon;
len = strlen(colon) + 1;
if (len >= sizeof addr.sunix.sun_path)
{
errno = EINVAL;
smi_log(SMI_LOG_ERR, "%s: UNIX socket name %s too long",
name, colon);
return INVALID_SOCKET;
}
(void) sm_strlcpy(addr.sunix.sun_path, colon,
sizeof addr.sunix.sun_path);
# if 0
errno = safefile(colon, RunAsUid, RunAsGid, RunAsUserName, sff,
S_IRUSR|S_IWUSR, NULL);
/* if not safe, don't create */
if (errno != 0)
{
smi_log(SMI_LOG_ERR,
"%s: UNIX socket name %s unsafe",
name, colon);
return INVALID_SOCKET;
}
# endif /* 0 */
}
#endif /* NETUNIX */
#if NETINET || NETINET6
if (
# if NETINET
addr.sa.sa_family == AF_INET
# endif /* NETINET */
# if NETINET && NETINET6
||
# endif /* NETINET && NETINET6 */
# if NETINET6
addr.sa.sa_family == AF_INET6
# endif /* NETINET6 */
)
{
unsigned short port;
/* Parse port@host */
at = strchr(colon, '@');
if (at == NULL)
{
switch (addr.sa.sa_family)
{
# if NETINET
case AF_INET:
addr.sin.sin_addr.s_addr = INADDR_ANY;
break;
# endif /* NETINET */
# if NETINET6
case AF_INET6:
addr.sin6.sin6_addr = in6addr_any;
break;
# endif /* NETINET6 */
}
}
else
*at = '\0';
if (isascii(*colon) && isdigit(*colon))
port = htons((unsigned short) atoi(colon));
else
{
# ifdef NO_GETSERVBYNAME
smi_log(SMI_LOG_ERR, "%s: invalid port number %s",
name, colon);
return INVALID_SOCKET;
# else /* NO_GETSERVBYNAME */
register struct servent *sp;
sp = getservbyname(colon, "tcp");
if (sp == NULL)
{
smi_log(SMI_LOG_ERR,
"%s: unknown port name %s",
name, colon);
return INVALID_SOCKET;
}
port = sp->s_port;
# endif /* NO_GETSERVBYNAME */
}
if (at != NULL)
{
*at++ = '@';
if (*at == '[')
{
char *end;
end = strchr(at, ']');
if (end != NULL)
{
bool found = false;
# if NETINET
unsigned long hid = INADDR_NONE;
# endif /* NETINET */
# if NETINET6
struct sockaddr_in6 hid6;
# endif /* NETINET6 */
*end = '\0';
# if NETINET
if (addr.sa.sa_family == AF_INET &&
(hid = inet_addr(&at[1])) != INADDR_NONE)
{
addr.sin.sin_addr.s_addr = hid;
addr.sin.sin_port = port;
found = true;
}
# endif /* NETINET */
# if NETINET6
(void) memset(&hid6, '\0', sizeof hid6);
if (addr.sa.sa_family == AF_INET6 &&
mi_inet_pton(AF_INET6, &at[1],
&hid6.sin6_addr) == 1)
{
addr.sin6.sin6_addr = hid6.sin6_addr;
addr.sin6.sin6_port = port;
found = true;
}
# endif /* NETINET6 */
*end = ']';
if (!found)
{
smi_log(SMI_LOG_ERR,
"%s: Invalid numeric domain spec \"%s\"",
name, at);
return INVALID_SOCKET;
}
}
else
{
smi_log(SMI_LOG_ERR,
"%s: Invalid numeric domain spec \"%s\"",
name, at);
return INVALID_SOCKET;
}
}
else
{
struct hostent *hp = NULL;
hp = mi_gethostbyname(at, addr.sa.sa_family);
if (hp == NULL)
{
smi_log(SMI_LOG_ERR,
"%s: Unknown host name %s",
name, at);
return INVALID_SOCKET;
}
addr.sa.sa_family = hp->h_addrtype;
switch (hp->h_addrtype)
{
# if NETINET
case AF_INET:
memmove(&addr.sin.sin_addr,
hp->h_addr,
INADDRSZ);
addr.sin.sin_port = port;
break;
# endif /* NETINET */
# if NETINET6
case AF_INET6:
memmove(&addr.sin6.sin6_addr,
hp->h_addr,
IN6ADDRSZ);
addr.sin6.sin6_port = port;
break;
# endif /* NETINET6 */
default:
smi_log(SMI_LOG_ERR,
"%s: Unknown protocol for %s (%d)",
name, at, hp->h_addrtype);
return INVALID_SOCKET;
}
# if NETINET6
freehostent(hp);
# endif /* NETINET6 */
}
}
else
{
switch (addr.sa.sa_family)
{
# if NETINET
case AF_INET:
addr.sin.sin_port = port;
break;
# endif /* NETINET */
# if NETINET6
case AF_INET6:
addr.sin6.sin6_port = port;
break;
# endif /* NETINET6 */
}
}
}
#endif /* NETINET || NETINET6 */
sock = socket(addr.sa.sa_family, SOCK_STREAM, 0);
if (!ValidSocket(sock))
{
smi_log(SMI_LOG_ERR,
"%s: Unable to create new socket: %s",
name, sm_errstring(errno));
return INVALID_SOCKET;
}
if ((fdflags = fcntl(sock, F_GETFD, 0)) == -1 ||
fcntl(sock, F_SETFD, fdflags | FD_CLOEXEC) == -1)
{
smi_log(SMI_LOG_ERR,
"%s: Unable to set close-on-exec: %s", name,
sm_errstring(errno));
(void) closesocket(sock);
return INVALID_SOCKET;
}
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *) &sockopt,
sizeof(sockopt)) == -1)
{
smi_log(SMI_LOG_ERR,
"%s: Unable to setsockopt: %s", name,
sm_errstring(errno));
(void) closesocket(sock);
return INVALID_SOCKET;
}
if (bind(sock, &addr.sa, L_socksize) < 0)
{
smi_log(SMI_LOG_ERR,
"%s: Unable to bind to port %s: %s",
name, conn, sm_errstring(errno));
(void) closesocket(sock);
return INVALID_SOCKET;
}
if (listen(sock, backlog) < 0)
{
smi_log(SMI_LOG_ERR,
"%s: listen call failed: %s", name,
sm_errstring(errno));
(void) closesocket(sock);
return INVALID_SOCKET;
}
#if NETUNIX
if (addr.sa.sa_family == AF_UNIX && len > 0)
{
/*
** Set global variable sockpath so the UNIX socket can be
** unlink()ed at exit.
*/
sockpath = (char *) malloc(len);
if (sockpath != NULL)
(void) sm_strlcpy(sockpath, colon, len);
else
{
smi_log(SMI_LOG_ERR,
"%s: can't malloc(%d) for sockpath: %s",
name, len, sm_errstring(errno));
(void) closesocket(sock);
return INVALID_SOCKET;
}
}
#endif /* NETUNIX */
L_family = addr.sa.sa_family;
return sock;
}
/*
** MI_THREAD_HANDLE_WRAPPER -- small wrapper to handle session
**
** Parameters:
** arg -- argument to pass to mi_handle_session()
**
** Returns:
** results from mi_handle_session()
*/
void *
mi_thread_handle_wrapper(arg)
void *arg;
{
return (void *) mi_handle_session(arg);
}
/*
** MI_CLOSENER -- close listen socket
**
** NOTE: It is assumed that this function is called from a
** function that has a mutex lock (currently mi_stop_milters()).
**
** Parameters:
** none.
**
** Returns:
** none.
*/
void
mi_closener()
{
(void) smutex_lock(&L_Mutex);
if (ValidSocket(listenfd))
{
#if NETUNIX
bool removable;
struct stat sockinfo;
struct stat fileinfo;
removable = sockpath != NULL &&
#if _FFR_MILTER_ROOT_UNSAFE
geteuid() != 0 &&
#endif /* _FFR_MILTER_ROOT_UNSAFE */
fstat(listenfd, &sockinfo) == 0 &&
(S_ISFIFO(sockinfo.st_mode)
# ifdef S_ISSOCK
|| S_ISSOCK(sockinfo.st_mode)
# endif /* S_ISSOCK */
);
#endif /* NETUNIX */
(void) closesocket(listenfd);
listenfd = INVALID_SOCKET;
#if NETUNIX
/* XXX sleep() some time before doing this? */
if (sockpath != NULL)
{
if (removable &&
stat(sockpath, &fileinfo) == 0 &&
((fileinfo.st_dev == sockinfo.st_dev &&
fileinfo.st_ino == sockinfo.st_ino)
# ifdef S_ISSOCK
|| S_ISSOCK(fileinfo.st_mode)
# endif /* S_ISSOCK */
)
&&
(S_ISFIFO(fileinfo.st_mode)
# ifdef S_ISSOCK
|| S_ISSOCK(fileinfo.st_mode)
# endif /* S_ISSOCK */
))
(void) unlink(sockpath);
free(sockpath);
sockpath = NULL;
}
#endif /* NETUNIX */
}
(void) smutex_unlock(&L_Mutex);
}
/*
** MI_LISTENER -- Generic listener harness
**
** Open up listen port
** Wait for connections
**
** Parameters:
** conn -- connection description
** dbg -- debug level
** smfi -- filter structure to use
** timeout -- timeout for reads/writes
** backlog -- listen queue backlog size
**
** Returns:
** MI_SUCCESS -- Exited normally
** (session finished or we were told to exit)
** MI_FAILURE -- Network initialization failed.
*/
#if BROKEN_PTHREAD_SLEEP
/*
** Solaris 2.6, perhaps others, gets an internal threads library panic
** when sleep() is used:
**
** thread_create() failed, returned 11 (EINVAL)
** co_enable, thr_create() returned error = 24
** libthread panic: co_enable failed (PID: 17793 LWP 1)
** stacktrace:
** ef526b10
** ef52646c
** ef534cbc
** 156a4
** 14644
** 1413c
** 135e0
** 0
*/
# define MI_SLEEP(s) \
{ \
int rs = 0; \
struct timeval st; \
\
st.tv_sec = (s); \
st.tv_usec = 0; \
if (st.tv_sec > 0) \
{ \
for (;;) \
{ \
rs = select(0, NULL, NULL, NULL, &st); \
if (rs < 0 && errno == EINTR) \
continue; \
if (rs != 0) \
{ \
smi_log(SMI_LOG_ERR, \
"MI_SLEEP(): select() returned non-zero result %d, errno = %d", \
rs, errno); \
} \
} \
} \
}
#else /* BROKEN_PTHREAD_SLEEP */
# define MI_SLEEP(s) sleep((s))
#endif /* BROKEN_PTHREAD_SLEEP */
int
mi_listener(conn, dbg, smfi, timeout, backlog)
char *conn;
int dbg;
smfiDesc_ptr smfi;
time_t timeout;
int backlog;
{
socket_t connfd = INVALID_SOCKET;
int sockopt = 1;
int r;
int ret = MI_SUCCESS;
int mcnt = 0; /* error count for malloc() failures */
int tcnt = 0; /* error count for thread_create() failures */
int acnt = 0; /* error count for accept() failures */
int scnt = 0; /* error count for select() failures */
int save_errno = 0;
sthread_t thread_id;
_SOCK_ADDR cliaddr;
SOCKADDR_LEN_T clilen;
SMFICTX_PTR ctx;
fd_set readset, excset;
struct timeval chktime;
if (mi_opensocket(conn, backlog, dbg, smfi) == MI_FAILURE)
return MI_FAILURE;
clilen = L_socksize;
if (listenfd >= FD_SETSIZE)
{
smi_log(SMI_LOG_ERR, "%s: fd %d is larger than FD_SETSIZE %d",
smfi->xxfi_name, listenfd, FD_SETSIZE);
(void) smutex_unlock(&L_Mutex);
return MI_FAILURE;
}
(void) smutex_unlock(&L_Mutex);
while (mi_stop() == MILTER_CONT)
{
(void) smutex_lock(&L_Mutex);
if (!ValidSocket(listenfd))
{
(void) smutex_unlock(&L_Mutex);
break;
}
/* select on interface ports */
FD_ZERO(&readset);
FD_ZERO(&excset);
FD_SET((unsigned int) listenfd, &readset);
FD_SET((unsigned int) listenfd, &excset);
chktime.tv_sec = MI_CHK_TIME;
chktime.tv_usec = 0;
r = select(listenfd + 1, &readset, NULL, &excset, &chktime);
if (r == 0) /* timeout */
{
(void) smutex_unlock(&L_Mutex);
continue; /* just check mi_stop() */
}
if (r < 0)
{
save_errno = errno;
(void) smutex_unlock(&L_Mutex);
if (save_errno == EINTR)
continue;
scnt++;
smi_log(SMI_LOG_ERR,
"%s: select() failed (%s), %s",
smfi->xxfi_name, sm_errstring(save_errno),
scnt >= MAX_FAILS_S ? "abort" : "try again");
MI_SLEEP(scnt);
if (scnt >= MAX_FAILS_S)
{
ret = MI_FAILURE;
break;
}
continue;
}
if (!FD_ISSET(listenfd, &readset))
{
/* some error: just stop for now... */
ret = MI_FAILURE;
(void) smutex_unlock(&L_Mutex);
smi_log(SMI_LOG_ERR,
"%s: select() returned exception for socket, abort",
smfi->xxfi_name);
break;
}
scnt = 0; /* reset error counter for select() */
memset(&cliaddr, '\0', sizeof cliaddr);
connfd = accept(listenfd, (struct sockaddr *) &cliaddr,
&clilen);
save_errno = errno;
(void) smutex_unlock(&L_Mutex);
/*
** If remote side closes before
** accept() finishes, sockaddr
** might not be fully filled in.
*/
if (ValidSocket(connfd) &&
(clilen == 0 ||
# ifdef BSD4_4_SOCKADDR
cliaddr.sa.sa_len == 0 ||
# endif /* BSD4_4_SOCKADDR */
cliaddr.sa.sa_family != L_family))
{
(void) closesocket(connfd);
connfd = INVALID_SOCKET;
save_errno = EINVAL;
}
if (!ValidSocket(connfd))
{
if (save_errno == EINTR)
continue;
acnt++;
smi_log(SMI_LOG_ERR,
"%s: accept() returned invalid socket (%s), %s",
smfi->xxfi_name, sm_errstring(save_errno),
acnt >= MAX_FAILS_A ? "abort" : "try again");
MI_SLEEP(acnt);
if (acnt >= MAX_FAILS_A)
{
ret = MI_FAILURE;
break;
}
continue;
}
acnt = 0; /* reset error counter for accept() */
if (setsockopt(connfd, SOL_SOCKET, SO_KEEPALIVE,
(void *) &sockopt, sizeof sockopt) < 0)
{
smi_log(SMI_LOG_WARN, "%s: setsockopt() failed (%s)",
smfi->xxfi_name, sm_errstring(errno));
/* XXX: continue? */
}
if ((ctx = (SMFICTX_PTR) malloc(sizeof *ctx)) == NULL)
{
(void) closesocket(connfd);
mcnt++;
smi_log(SMI_LOG_ERR, "%s: malloc(ctx) failed (%s), %s",
smfi->xxfi_name, sm_errstring(save_errno),
mcnt >= MAX_FAILS_M ? "abort" : "try again");
MI_SLEEP(mcnt);
if (mcnt >= MAX_FAILS_M)
{
ret = MI_FAILURE;
break;
}
continue;
}
mcnt = 0; /* reset error counter for malloc() */
memset(ctx, '\0', sizeof *ctx);
ctx->ctx_sd = connfd;
ctx->ctx_dbg = dbg;
ctx->ctx_timeout = timeout;
ctx->ctx_smfi = smfi;
#if 0
if (smfi->xxfi_eoh == NULL)
if (smfi->xxfi_eom == NULL)
if (smfi->xxfi_abort == NULL)
if (smfi->xxfi_close == NULL)
#endif /* 0 */
if (smfi->xxfi_connect == NULL)
ctx->ctx_pflags |= SMFIP_NOCONNECT;
if (smfi->xxfi_helo == NULL)
ctx->ctx_pflags |= SMFIP_NOHELO;
if (smfi->xxfi_envfrom == NULL)
ctx->ctx_pflags |= SMFIP_NOMAIL;
if (smfi->xxfi_envrcpt == NULL)
ctx->ctx_pflags |= SMFIP_NORCPT;
if (smfi->xxfi_header == NULL)
ctx->ctx_pflags |= SMFIP_NOHDRS;
if (smfi->xxfi_eoh == NULL)
ctx->ctx_pflags |= SMFIP_NOEOH;
if (smfi->xxfi_body == NULL)
ctx->ctx_pflags |= SMFIP_NOBODY;
if ((r = thread_create(&thread_id,
mi_thread_handle_wrapper,
(void *) ctx)) != 0)
{
tcnt++;
smi_log(SMI_LOG_ERR,
"%s: thread_create() failed: %d, %s",
smfi->xxfi_name, r,
tcnt >= MAX_FAILS_T ? "abort" : "try again");
MI_SLEEP(tcnt);
(void) closesocket(connfd);
free(ctx);
if (tcnt >= MAX_FAILS_T)
{
ret = MI_FAILURE;
break;
}
continue;
}
tcnt = 0;
}
if (ret != MI_SUCCESS)
mi_stop_milters(MILTER_ABRT);
else
mi_closener();
(void) smutex_destroy(&L_Mutex);
return ret;
}
|