| 1 |
/* $Revision: 1.39 $ |
| 2 |
** |
| 3 |
** I/O channel (and buffer) processing. |
| 4 |
*/ |
| 5 |
#include <stdio.h> |
| 6 |
#include <sys/types.h> |
| 7 |
#include "configdata.h" |
| 8 |
#include "clibrary.h" |
| 9 |
#include "innd.h" |
| 10 |
|
| 11 |
/* Divisor of the BUFFER size. If the amount free at the beginning of the |
| 12 |
buffer is bigger than the quotient, then it is compacted in the |
| 13 |
readloop */ |
| 14 |
#define COMP_THRESHOLD 10 |
| 15 |
|
| 16 |
STATIC FDSET RCHANmask; |
| 17 |
STATIC FDSET SCHANmask; |
| 18 |
STATIC FDSET WCHANmask; |
| 19 |
STATIC int SCHANcount; |
| 20 |
STATIC int CHANlastfd; |
| 21 |
STATIC int CHANlastsleepfd; |
| 22 |
STATIC int CHANccfd; |
| 23 |
STATIC int CHANtablesize; |
| 24 |
STATIC CHANNEL *CHANtable; |
| 25 |
STATIC CHANNEL *CHANcc; |
| 26 |
STATIC CHANNEL CHANnull = { CTfree, CSerror, -1 }; |
| 27 |
|
| 28 |
#define PRIORITISE_REMCONN |
| 29 |
#ifdef PRIORITISE_REMCONN |
| 30 |
STATIC int CHANrcfd; |
| 31 |
STATIC CHANNEL *CHANrc; |
| 32 |
#endif /* PRIORITISE_REMCONN */ |
| 33 |
|
| 34 |
#include "timer.h" |
| 35 |
|
| 36 |
|
| 37 |
/* |
| 38 |
** Set a buffer's contents, ignoring anything that might have |
| 39 |
** been there. |
| 40 |
*/ |
| 41 |
void |
| 42 |
BUFFset(bp, p, length) |
| 43 |
register BUFFER *bp; |
| 44 |
register char *p; |
| 45 |
register int length; |
| 46 |
{ |
| 47 |
register char *dest; |
| 48 |
|
| 49 |
if ((bp->Left = length) != 0) { |
| 50 |
/* Need more space? */ |
| 51 |
if (bp->Size < length) { |
| 52 |
bp->Size = length; |
| 53 |
RENEW(bp->Data, char, bp->Size); |
| 54 |
} |
| 55 |
|
| 56 |
/* Try to test for non-overlapping copies. */ |
| 57 |
if (length > MEMCPY_THRESHOLD |
| 58 |
&& (p < bp->Data || p >= &bp->Data[length])) |
| 59 |
(void)memcpy((POINTER)bp->Data, (POINTER)p, (SIZE_T)length); |
| 60 |
else { |
| 61 |
for (dest = bp->Data, length++; --length > 0; ) |
| 62 |
*dest++ = *p++; |
| 63 |
} |
| 64 |
} |
| 65 |
bp->Used = 0; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
/* |
| 70 |
** Swap the contents of two buffers. |
| 71 |
*/ |
| 72 |
void |
| 73 |
BUFFswap(b1, b2) |
| 74 |
register BUFFER *b1; |
| 75 |
register BUFFER *b2; |
| 76 |
{ |
| 77 |
BUFFER b; |
| 78 |
|
| 79 |
b = *b1; |
| 80 |
*b1 = *b2; |
| 81 |
*b2 = b; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
/* |
| 86 |
** Initialize all the I/O channels. |
| 87 |
*/ |
| 88 |
void |
| 89 |
CHANsetup(i) |
| 90 |
register int i; |
| 91 |
{ |
| 92 |
register CHANNEL *cp; |
| 93 |
|
| 94 |
FD_ZERO(&RCHANmask); |
| 95 |
FD_ZERO(&SCHANmask); |
| 96 |
FD_ZERO(&WCHANmask); |
| 97 |
if (CHANtable) |
| 98 |
DISPOSE(CHANtable); |
| 99 |
CHANtablesize = i; |
| 100 |
CHANtable = NEW(CHANNEL, CHANtablesize); |
| 101 |
(void)memset((POINTER)CHANtable, 0, |
| 102 |
(SIZE_T)(CHANtablesize * sizeof *CHANtable)); |
| 103 |
CHANnull.NextLog = CHANNEL_INACTIVE_TIME; |
| 104 |
CHANnull.Address.s_addr = MyAddress.s_addr; |
| 105 |
CHANnull.RequirePathHostForPath = NULL; |
| 106 |
CHANnull.IdentForPath = NULL; |
| 107 |
for (cp = CHANtable; --i >= 0; cp++) |
| 108 |
*cp = CHANnull; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
/* |
| 113 |
** Create a channel from a descriptor. |
| 114 |
*/ |
| 115 |
CHANNEL * |
| 116 |
CHANcreate(fd, Type, State, Reader, WriteDone) |
| 117 |
int fd; |
| 118 |
CHANNELTYPE Type; |
| 119 |
CHANNELSTATE State; |
| 120 |
FUNCPTR Reader; |
| 121 |
FUNCPTR WriteDone; |
| 122 |
{ |
| 123 |
register CHANNEL *cp; |
| 124 |
BUFFER in; |
| 125 |
BUFFER out; |
| 126 |
|
| 127 |
cp = &CHANtable[fd]; |
| 128 |
|
| 129 |
/* Don't overwrite the buffers with CHANnull. */ |
| 130 |
in = cp->In; |
| 131 |
if (in.Size == 0) { |
| 132 |
in.Size = START_BUFF_SIZE; |
| 133 |
in.Data = NEW(char, in.Size); |
| 134 |
} |
| 135 |
in.Used = 0; |
| 136 |
in.Left = in.Size; |
| 137 |
out = cp->Out; |
| 138 |
if (out.Size == 0) { |
| 139 |
out.Size = SMBUF; |
| 140 |
out.Data = NEW(char, out.Size); |
| 141 |
} |
| 142 |
out.Used = 0; |
| 143 |
out.Left = 0; |
| 144 |
|
| 145 |
/* Set up the channel's info. */ |
| 146 |
*cp = CHANnull; |
| 147 |
cp->fd = fd; |
| 148 |
cp->Type = Type; |
| 149 |
cp->State = State; |
| 150 |
cp->Streaming = FALSE ; |
| 151 |
cp->Reader = Reader; |
| 152 |
cp->WriteDone = WriteDone; |
| 153 |
cp->Started = cp->LastActive = Now.time; |
| 154 |
cp->In = in; |
| 155 |
cp->Out = out; |
| 156 |
cp->Tracing = Tracing; |
| 157 |
cp->Sendid.Size=0; |
| 158 |
cp->Rest=0; |
| 159 |
cp->SaveUsed=0; |
| 160 |
cp->Lastch=0; |
| 161 |
|
| 162 |
/* Make the descriptor close-on-exec and non-blocking. */ |
| 163 |
CloseOnExec(fd, TRUE); |
| 164 |
if (SetNonBlocking(fd, TRUE) < 0 |
| 165 |
#if defined(ENOTSOCK) |
| 166 |
&& errno != ENOTSOCK |
| 167 |
#endif /* defined(ENOTSOCK) */ |
| 168 |
#if defined(ENOTTY) |
| 169 |
&& errno != ENOTTY |
| 170 |
#endif /* defined(ENOTTY) */ |
| 171 |
) |
| 172 |
syslog(L_ERROR, "%s cant nonblock %d %m", LogName, fd); |
| 173 |
|
| 174 |
/* Note control channel, for efficiency. */ |
| 175 |
if (Type == CTcontrol) { |
| 176 |
CHANcc = cp; |
| 177 |
CHANccfd = fd; |
| 178 |
} |
| 179 |
#ifdef PRIORITISE_REMCONN |
| 180 |
/* Note remconn channel, for efficiency */ |
| 181 |
if (Type == CTremconn) { |
| 182 |
CHANrc = cp; |
| 183 |
CHANrcfd = fd; |
| 184 |
} |
| 185 |
#endif /* PRIORITISE_REMCONN */ |
| 186 |
return cp; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
/* |
| 191 |
** Start tracing a channel. |
| 192 |
*/ |
| 193 |
void |
| 194 |
CHANtracing(cp, Flag) |
| 195 |
register CHANNEL *cp; |
| 196 |
BOOL Flag; |
| 197 |
{ |
| 198 |
char *p; |
| 199 |
|
| 200 |
p = CHANname(cp); |
| 201 |
syslog(L_NOTICE, "%s trace %s", p, Flag ? "on" : "off"); |
| 202 |
cp->Tracing = Flag; |
| 203 |
if (Flag) { |
| 204 |
syslog(L_NOTICE, "%s trace badwrites %d blockwrites %d badreads %d", |
| 205 |
p, cp->BadWrites, cp->BlockedWrites, cp->BadReads); |
| 206 |
syslog(L_NOTICE, "%s trace address %s lastactive %ld nextlog %ld", |
| 207 |
p, inet_ntoa(cp->Address), cp->LastActive, cp->NextLog); |
| 208 |
if (FD_ISSET(cp->fd, &SCHANmask)) |
| 209 |
syslog(L_NOTICE, "%s trace sleeping %ld %p", |
| 210 |
p, (long)cp->Waketime, cp->Waker); |
| 211 |
if (FD_ISSET(cp->fd, &RCHANmask)) |
| 212 |
syslog(L_NOTICE, "%s trace reading %ld %s", |
| 213 |
p, cp->In.Used, MaxLength(cp->In.Data, cp->In.Data)); |
| 214 |
if (FD_ISSET(cp->fd, &WCHANmask)) |
| 215 |
syslog(L_NOTICE, "%s trace writing %ld %s", |
| 216 |
p, cp->Out.Left, MaxLength(cp->Out.Data, cp->Out.Data)); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
/* |
| 222 |
** Close a channel. |
| 223 |
*/ |
| 224 |
void |
| 225 |
CHANclose(cp, name) |
| 226 |
register CHANNEL *cp; |
| 227 |
char *name; |
| 228 |
{ |
| 229 |
if (cp->Type == CTfree) |
| 230 |
syslog(L_ERROR, "%s internal closing free channel %d", name, cp->fd); |
| 231 |
else { |
| 232 |
if (cp->Type == CTnntp) |
| 233 |
syslog(L_NOTICE, |
| 234 |
"%s closed seconds %ld accepted %ld refused %ld rejected %ld", |
| 235 |
name, (long)(Now.time - cp->Started), |
| 236 |
cp->Received, cp->Refused, cp->Rejected); |
| 237 |
else if (cp->Type == CTreject) |
| 238 |
syslog(L_NOTICE, "%s %ld", name, cp->Rejected); |
| 239 |
else if (cp->Out.Left) |
| 240 |
syslog(L_NOTICE, "%s closed lost %ld", name, cp->Out.Left); |
| 241 |
else |
| 242 |
syslog(L_NOTICE, "%s closed", name); |
| 243 |
WCHANremove(cp); |
| 244 |
RCHANremove(cp); |
| 245 |
SCHANremove(cp); |
| 246 |
if (cp->Argument != NULL) |
| 247 |
/* Set to NULL below. */ |
| 248 |
DISPOSE(cp->Argument); |
| 249 |
if (cp->fd >= 0 && close(cp->fd) < 0) |
| 250 |
syslog(L_ERROR, "%s cant close %s %m", LogName, name); |
| 251 |
} |
| 252 |
|
| 253 |
/* Mark it unused. */ |
| 254 |
cp->Type = CTfree; |
| 255 |
cp->State = CSerror; |
| 256 |
cp->fd = -1; |
| 257 |
cp->Argument = NULL; |
| 258 |
|
| 259 |
/* Free the buffers if they got big. */ |
| 260 |
if (cp->In.Size > BIG_BUFFER) { |
| 261 |
cp->In.Size = 0; |
| 262 |
DISPOSE(cp->In.Data); |
| 263 |
} |
| 264 |
if (cp->Out.Size > BIG_BUFFER) { |
| 265 |
cp->Out.Size = 0; |
| 266 |
DISPOSE(cp->Out.Data); |
| 267 |
} |
| 268 |
if (cp->Sendid.Size) { |
| 269 |
cp->Sendid.Size = 0; |
| 270 |
DISPOSE(cp->Sendid.Data); |
| 271 |
} |
| 272 |
if (cp->RequirePathHostForPath) { |
| 273 |
DISPOSE(cp->RequirePathHostForPath); |
| 274 |
cp->RequirePathHostForPath = NULL; |
| 275 |
} |
| 276 |
if (cp->IdentForPath) { |
| 277 |
DISPOSE(cp->IdentForPath); |
| 278 |
cp->IdentForPath = NULL; |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
/* |
| 284 |
** Return a printable name for the channel. |
| 285 |
*/ |
| 286 |
char * |
| 287 |
CHANname(cp) |
| 288 |
register CHANNEL *cp; |
| 289 |
{ |
| 290 |
static char buff[SMBUF]; |
| 291 |
register int i; |
| 292 |
register SITE *sp; |
| 293 |
STRING p; |
| 294 |
PID_T pid; |
| 295 |
|
| 296 |
switch (cp->Type) { |
| 297 |
default: |
| 298 |
(void)sprintf(buff, "?%d(#%d@%ld)?", cp->Type, cp->fd, cp - CHANtable); |
| 299 |
break; |
| 300 |
case CTany: |
| 301 |
(void)sprintf(buff, "any:%d", cp->fd); |
| 302 |
break; |
| 303 |
case CTfree: |
| 304 |
(void)sprintf(buff, "free:%d", cp->fd); |
| 305 |
break; |
| 306 |
case CTremconn: |
| 307 |
(void)sprintf(buff, "remconn:%d", cp->fd); |
| 308 |
break; |
| 309 |
case CTreject: |
| 310 |
(void)sprintf(buff, "%s rejected", RChostname(cp)); |
| 311 |
break; |
| 312 |
case CTnntp: |
| 313 |
(void)sprintf(buff, "%s:%d", |
| 314 |
cp->Address.s_addr == 0 ? "localhost" : RChostname(cp), |
| 315 |
cp->fd); |
| 316 |
break; |
| 317 |
case CTlocalconn: |
| 318 |
(void)sprintf(buff, "localconn:%d", cp->fd); |
| 319 |
break; |
| 320 |
case CTcontrol: |
| 321 |
(void)sprintf(buff, "control:%d", cp->fd); |
| 322 |
break; |
| 323 |
case CTexploder: |
| 324 |
case CTfile: |
| 325 |
case CTprocess: |
| 326 |
/* Find the site that has this channel. */ |
| 327 |
for (p = "?", i = nSites, sp = Sites, pid = 0; --i >= 0; sp++) |
| 328 |
if (sp->Channel == cp) { |
| 329 |
p = sp->Name; |
| 330 |
if (cp->Type != CTfile) |
| 331 |
pid = sp->pid; |
| 332 |
break; |
| 333 |
} |
| 334 |
if (pid == 0) |
| 335 |
(void)sprintf(buff, "%s:%d:%s", |
| 336 |
MaxLength(p, p), cp->fd, |
| 337 |
cp->Type == CTfile ? "file" : "proc"); |
| 338 |
else |
| 339 |
(void)sprintf(buff, "%s:%d:%s:%ld", |
| 340 |
MaxLength(p, p), cp->fd, |
| 341 |
cp->Type == CTfile ? "file" : "proc", (long)pid); |
| 342 |
break; |
| 343 |
} |
| 344 |
return buff; |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
/* |
| 349 |
** Return the channel for a specified descriptor. |
| 350 |
*/ |
| 351 |
CHANNEL * |
| 352 |
CHANfromdescriptor(fd) |
| 353 |
int fd; |
| 354 |
{ |
| 355 |
if (fd <0 || fd > CHANtablesize) |
| 356 |
return NULL; |
| 357 |
return &CHANtable[fd]; |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
/* |
| 362 |
** Iterate over all channels of a specified type. |
| 363 |
*/ |
| 364 |
CHANNEL * |
| 365 |
CHANiter(ip, Type) |
| 366 |
int *ip; |
| 367 |
CHANNELTYPE Type; |
| 368 |
{ |
| 369 |
register CHANNEL *cp; |
| 370 |
register int i; |
| 371 |
|
| 372 |
if ((i = *ip) >= 0 && i < CHANtablesize) { |
| 373 |
do { |
| 374 |
cp = &CHANtable[i]; |
| 375 |
if (cp->Type == CTfree && cp->fd == -1) |
| 376 |
continue; |
| 377 |
if (Type == CTany || cp->Type == Type) { |
| 378 |
*ip = ++i; |
| 379 |
return cp; |
| 380 |
} |
| 381 |
} while (++i < CHANtablesize); |
| 382 |
} |
| 383 |
return NULL; |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
/* |
| 388 |
** Mark a channel as an active reader. |
| 389 |
*/ |
| 390 |
void |
| 391 |
RCHANadd(cp) |
| 392 |
register CHANNEL *cp; |
| 393 |
{ |
| 394 |
FD_SET(cp->fd, &RCHANmask); |
| 395 |
if (cp->fd > CHANlastfd) |
| 396 |
CHANlastfd = cp->fd; |
| 397 |
|
| 398 |
/* Start reading at the beginning of the buffer. */ |
| 399 |
cp->In.Used = 0; |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
/* |
| 404 |
** Remove a channel from the set of readers. |
| 405 |
*/ |
| 406 |
void |
| 407 |
RCHANremove(cp) |
| 408 |
register CHANNEL *cp; |
| 409 |
{ |
| 410 |
if (FD_ISSET(cp->fd, &RCHANmask)) { |
| 411 |
FD_CLR(cp->fd, &RCHANmask); |
| 412 |
if (cp->fd == CHANlastfd) { |
| 413 |
/* This was the highest descriptor, get a new highest. */ |
| 414 |
while (!FD_ISSET(CHANlastfd, &RCHANmask) |
| 415 |
&& !FD_ISSET(CHANlastfd, &WCHANmask) |
| 416 |
&& CHANlastfd > 1) |
| 417 |
CHANlastfd--; |
| 418 |
} |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
/* |
| 424 |
** Put a channel to sleep, call a function when it wakes. |
| 425 |
** Note that the Argument must be NULL or allocated memory! |
| 426 |
*/ |
| 427 |
void |
| 428 |
SCHANadd(cp, Waketime, Event, Waker, Argument) |
| 429 |
register CHANNEL *cp; |
| 430 |
time_t Waketime; |
| 431 |
POINTER Event; |
| 432 |
FUNCPTR Waker; |
| 433 |
POINTER Argument; |
| 434 |
{ |
| 435 |
if (!FD_ISSET(cp->fd, &SCHANmask)) { |
| 436 |
SCHANcount++; |
| 437 |
FD_SET(cp->fd, &SCHANmask); |
| 438 |
} |
| 439 |
if (cp->fd > CHANlastsleepfd) |
| 440 |
CHANlastsleepfd = cp->fd; |
| 441 |
cp->Waketime = Waketime; |
| 442 |
cp->Waker = Waker; |
| 443 |
if (cp->Argument != Argument) { |
| 444 |
DISPOSE(cp->Argument); |
| 445 |
cp->Argument = Argument; |
| 446 |
} |
| 447 |
cp->Event = Event; |
| 448 |
} |
| 449 |
|
| 450 |
|
| 451 |
/* |
| 452 |
** Take a channel off the sleep list. |
| 453 |
*/ |
| 454 |
void |
| 455 |
SCHANremove(cp) |
| 456 |
register CHANNEL *cp; |
| 457 |
{ |
| 458 |
if (FD_ISSET(cp->fd, &SCHANmask)) { |
| 459 |
FD_CLR(cp->fd, &SCHANmask); |
| 460 |
SCHANcount--; |
| 461 |
cp->Waketime = 0; |
| 462 |
if (cp->fd == CHANlastsleepfd) { |
| 463 |
/* This was the highest descriptor, get a new highest. */ |
| 464 |
while (!FD_ISSET(CHANlastsleepfd, &SCHANmask) |
| 465 |
&& CHANlastsleepfd > 1) |
| 466 |
CHANlastsleepfd--; |
| 467 |
} |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
/* |
| 473 |
** Is a channel on the sleep list? |
| 474 |
*/ |
| 475 |
BOOL |
| 476 |
CHANsleeping(cp) |
| 477 |
CHANNEL *cp; |
| 478 |
{ |
| 479 |
return FD_ISSET(cp->fd, &SCHANmask); |
| 480 |
} |
| 481 |
|
| 482 |
|
| 483 |
/* |
| 484 |
** Wake up channels waiting for a specific event. |
| 485 |
*/ |
| 486 |
void |
| 487 |
SCHANwakeup(Event) |
| 488 |
register POINTER Event; |
| 489 |
{ |
| 490 |
register CHANNEL *cp; |
| 491 |
register int i; |
| 492 |
|
| 493 |
for (cp = CHANtable, i = CHANtablesize; --i >= 0; cp++) |
| 494 |
if (cp->Type != CTfree && cp->Event == Event && CHANsleeping(cp)) |
| 495 |
cp->Waketime = 0; |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
/* |
| 500 |
** Mark a channel as an active writer. Don't reset the Out->Left field |
| 501 |
** since we could have buffered I/O already in there. |
| 502 |
*/ |
| 503 |
void |
| 504 |
WCHANadd(cp) |
| 505 |
register CHANNEL *cp; |
| 506 |
{ |
| 507 |
if (cp->Out.Left > 0) { |
| 508 |
FD_SET(cp->fd, &WCHANmask); |
| 509 |
if (cp->fd > CHANlastfd) |
| 510 |
CHANlastfd = cp->fd; |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
/* |
| 516 |
** Remove a channel from the set of writers. |
| 517 |
*/ |
| 518 |
void |
| 519 |
WCHANremove(cp) |
| 520 |
register CHANNEL *cp; |
| 521 |
{ |
| 522 |
if (FD_ISSET(cp->fd, &WCHANmask)) { |
| 523 |
FD_CLR(cp->fd, &WCHANmask); |
| 524 |
if (cp->Out.Left <= 0) { |
| 525 |
/* No data left -- reset used so we don't grow the buffer. */ |
| 526 |
cp->Out.Used = 0; |
| 527 |
cp->Out.Left = 0; |
| 528 |
} |
| 529 |
if (cp->fd == CHANlastfd) { |
| 530 |
/* This was the highest descriptor, get a new highest. */ |
| 531 |
while (!FD_ISSET(CHANlastfd, &RCHANmask) |
| 532 |
&& !FD_ISSET(CHANlastfd, &WCHANmask) |
| 533 |
&& CHANlastfd > 1) |
| 534 |
CHANlastfd--; |
| 535 |
} |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
|
| 540 |
/* |
| 541 |
** Set a channel to start off with the contents of an existing channel. |
| 542 |
*/ |
| 543 |
void |
| 544 |
WCHANsetfrombuffer(cp, bp) |
| 545 |
CHANNEL *cp; |
| 546 |
BUFFER *bp; |
| 547 |
{ |
| 548 |
WCHANset(cp, &bp->Data[bp->Used], bp->Left); |
| 549 |
} |
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
/* |
| 554 |
** Read in text data, return the amount we read. |
| 555 |
*/ |
| 556 |
int |
| 557 |
CHANreadtext(cp) |
| 558 |
register CHANNEL *cp; |
| 559 |
{ |
| 560 |
register int i; |
| 561 |
register BUFFER *bp; |
| 562 |
char *p; |
| 563 |
int oerrno; |
| 564 |
|
| 565 |
/* Grow buffer if we're getting close to current limit. */ |
| 566 |
bp = &cp->In; |
| 567 |
if (bp->Left <= LOW_WATER) { |
| 568 |
i = GROW_AMOUNT(bp->Size); |
| 569 |
bp->Size += i; |
| 570 |
bp->Left += i; |
| 571 |
RENEW(bp->Data, char, bp->Size); |
| 572 |
} |
| 573 |
|
| 574 |
/* Read in whatever is there, up to some reasonable limit. */ |
| 575 |
/* |
| 576 |
* XXX We really want to limit the amount of time it takes to |
| 577 |
* process the incoming data for this channel. But there's |
| 578 |
* no easy way of doing that, so we restrict the data size instead. |
| 579 |
* If the data is part of a single large article, then reading |
| 580 |
* and processing many kilobytes at a time costs very little. |
| 581 |
* If the data is a long list of CHECK commands from a streaming |
| 582 |
* feed, then every line of data will require a history lookup, and |
| 583 |
* we probably don't want to do more than about 10 of those per |
| 584 |
* channel on each cycle of the main select() loop (otherwise we |
| 585 |
* might take too long before giving other channels a turn). 10 |
| 586 |
* lines of CHECK commands suggests a limit of about 1 kilobyte of |
| 587 |
* data, or less. BUFSIZ is often about 1 kilobyte, and is |
| 588 |
* attractive for other reasons, so let's use that as our size limit. |
| 589 |
*/ |
| 590 |
bp->Left = bp->Size - bp->Used; |
| 591 |
i = read(cp->fd, &bp->Data[bp->Used], |
| 592 |
(bp->Left - 1 > BUFSIZ ? BUFSIZ : bp->Left - 1)); |
| 593 |
if (i < 0) { |
| 594 |
#ifdef POLL_BUG |
| 595 |
/* return of -2 indicates EAGAIN, for SUNOS5.4 poll() bug workaround */ |
| 596 |
if (errno == EAGAIN) { |
| 597 |
return -2; |
| 598 |
} |
| 599 |
#endif |
| 600 |
oerrno = errno; |
| 601 |
p = CHANname(cp); |
| 602 |
errno = oerrno; |
| 603 |
syslog(L_ERROR, "%s cant read %m", p); |
| 604 |
return -1; |
| 605 |
} |
| 606 |
if (i == 0) { |
| 607 |
p = CHANname(cp); |
| 608 |
syslog(L_NOTICE, "%s readclose", p); |
| 609 |
return 0; |
| 610 |
} |
| 611 |
|
| 612 |
bp->Used += i; |
| 613 |
bp->Left -= i; |
| 614 |
return i; |
| 615 |
} |
| 616 |
|
| 617 |
|
| 618 |
/* |
| 619 |
** If I/O backs up a lot, we can get EMSGSIZE on some systems. If that |
| 620 |
** happens we want to do the I/O in chunks. We assume stdio's BUFSIZ is |
| 621 |
** a good chunk value. |
| 622 |
*/ |
| 623 |
STATIC int |
| 624 |
largewrite(fd, p, length) |
| 625 |
register int fd; |
| 626 |
register char *p; |
| 627 |
register int length; |
| 628 |
{ |
| 629 |
register int i; |
| 630 |
register char *save; |
| 631 |
|
| 632 |
do { |
| 633 |
/* Try the standard case -- write it all. */ |
| 634 |
i = write(fd, (POINTER)p, (SIZE_T)length); |
| 635 |
if (i > 0 || (i < 0 && errno != EMSGSIZE && errno != EINTR)) |
| 636 |
return i; |
| 637 |
} while (i < 0 && errno == EINTR); |
| 638 |
|
| 639 |
/* Write it in pieces. */ |
| 640 |
for (save = p, i = 0; length; p += i, length -= i) { |
| 641 |
i = write(fd, (POINTER)p, (SIZE_T)(length > BUFSIZ ? BUFSIZ : length)); |
| 642 |
if (i <= 0) |
| 643 |
break; |
| 644 |
} |
| 645 |
|
| 646 |
/* Return error, or partial results if we got something. */ |
| 647 |
return p == save ? i : p - save; |
| 648 |
} |
| 649 |
|
| 650 |
|
| 651 |
/* |
| 652 |
** Try to flush out the buffer. Use this only on file channels! |
| 653 |
*/ |
| 654 |
BOOL |
| 655 |
WCHANflush(cp) |
| 656 |
register CHANNEL *cp; |
| 657 |
{ |
| 658 |
register BUFFER *bp; |
| 659 |
register int i; |
| 660 |
|
| 661 |
/* Write it. */ |
| 662 |
for (bp = &cp->Out; bp->Left > 0; bp->Left -= i, bp->Used += i) { |
| 663 |
i = largewrite(cp->fd, &bp->Data[bp->Used], bp->Left); |
| 664 |
if (i < 0) { |
| 665 |
syslog(L_ERROR, "%s cant flush count %ld %m", |
| 666 |
CHANname(cp), bp->Left); |
| 667 |
return FALSE; |
| 668 |
} |
| 669 |
if (i == 0) { |
| 670 |
syslog(L_ERROR, "%s cant flush count %ld", |
| 671 |
CHANname(cp), bp->Left); |
| 672 |
return FALSE; |
| 673 |
} |
| 674 |
} |
| 675 |
WCHANremove(cp); |
| 676 |
return TRUE; |
| 677 |
} |
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
/* |
| 682 |
** Wakeup routine called after a write channel was put to sleep. |
| 683 |
*/ |
| 684 |
STATIC FUNCTYPE |
| 685 |
CHANwakeup(cp) |
| 686 |
CHANNEL *cp; |
| 687 |
{ |
| 688 |
syslog(L_NOTICE, "%s wakeup", CHANname(cp)); |
| 689 |
WCHANadd(cp); |
| 690 |
} |
| 691 |
|
| 692 |
|
| 693 |
/* |
| 694 |
** Attempting to write would block; stop output or give up. |
| 695 |
*/ |
| 696 |
STATIC void |
| 697 |
CHANwritesleep(cp, p) |
| 698 |
register CHANNEL *cp; |
| 699 |
char *p; |
| 700 |
{ |
| 701 |
int i; |
| 702 |
|
| 703 |
if ((i = ++(cp->BlockedWrites)) > BAD_IO_COUNT) |
| 704 |
switch (cp->Type) { |
| 705 |
default: |
| 706 |
break; |
| 707 |
case CTreject: |
| 708 |
case CTnntp: |
| 709 |
case CTfile: |
| 710 |
case CTexploder: |
| 711 |
case CTprocess: |
| 712 |
syslog(L_ERROR, "%s blocked closing", p); |
| 713 |
SITEchanclose(cp); |
| 714 |
CHANclose(cp, p); |
| 715 |
return; |
| 716 |
} |
| 717 |
i *= BLOCK_BACKOFF; |
| 718 |
syslog(L_ERROR, "%s blocked sleeping %d", p, i); |
| 719 |
SCHANadd(cp, (time_t)(Now.time + i), (POINTER)NULL, |
| 720 |
CHANwakeup, (POINTER)NULL); |
| 721 |
} |
| 722 |
|
| 723 |
|
| 724 |
#if defined(INND_FIND_BAD_FDS) |
| 725 |
/* |
| 726 |
** We got an unknown error in select. Find out the culprit. |
| 727 |
** Not really ready for production use yet, and it's expensive, too. |
| 728 |
*/ |
| 729 |
STATIC void |
| 730 |
CHANdiagnose() |
| 731 |
{ |
| 732 |
FDSET Test; |
| 733 |
int i; |
| 734 |
struct timeval t; |
| 735 |
|
| 736 |
FD_ZERO(&Test); |
| 737 |
for (i = CHANlastfd; i >= 0; i--) { |
| 738 |
if (FD_ISSET(i, &RCHANmask)) { |
| 739 |
FD_SET(i, &Test); |
| 740 |
t.tv_sec = 0; |
| 741 |
t.tv_usec = 0; |
| 742 |
if (select(i + 1, &Test, (FDSET *)NULL, (FDSET *)NULL, &t) < 0 |
| 743 |
&& errno != EINTR) { |
| 744 |
syslog(L_ERROR, "%s Bad Read File %d", LogName, i); |
| 745 |
FD_CLR(i, &RCHANmask); |
| 746 |
/* Probably do something about the file descriptor here; call |
| 747 |
* CHANclose on it? */ |
| 748 |
} |
| 749 |
FD_CLR(i, &Test); |
| 750 |
} |
| 751 |
if (FD_ISSET(i, &WCHANmask)) { |
| 752 |
FD_SET(i, &Test); |
| 753 |
t.tv_sec = 0; |
| 754 |
t.tv_usec = 0; |
| 755 |
if (select(i + 1, (FDSET *)NULL, &Test, (FDSET *)NULL, &t) < 0 |
| 756 |
&& errno != EINTR) { |
| 757 |
syslog(L_ERROR, "%s Bad Write File %d", LogName, i); |
| 758 |
FD_CLR(i, &WCHANmask); |
| 759 |
/* Probably do something about the file descriptor here; call |
| 760 |
* CHANclose on it? */ |
| 761 |
} |
| 762 |
FD_CLR(i, &Test); |
| 763 |
} |
| 764 |
} |
| 765 |
} |
| 766 |
#endif /* defined(INND_FIND_BAD_FDS) */ |
| 767 |
|
| 768 |
|
| 769 |
/* |
| 770 |
** Main I/O loop. Wait for data, call the channel's handler when there is |
| 771 |
** something to read or when the queued write is finished. In order to |
| 772 |
** be fair (i.e., don't always give descriptor n priority over n+1), we |
| 773 |
** remember where we last had something and pick up from there. |
| 774 |
** |
| 775 |
** Yes, the main code has really wandered over to the side a lot. |
| 776 |
*/ |
| 777 |
void |
| 778 |
CHANreadloop() |
| 779 |
{ |
| 780 |
static char EXITING[] = "INND exiting because of signal\n"; |
| 781 |
static int fd; |
| 782 |
register int i; |
| 783 |
register int startpoint; |
| 784 |
register int count; |
| 785 |
register int lastfd; |
| 786 |
int oerrno; |
| 787 |
register CHANNEL *cp; |
| 788 |
register BUFFER *bp; |
| 789 |
FDSET MyRead; |
| 790 |
FDSET MyWrite; |
| 791 |
struct timeval MyTime; |
| 792 |
long silence; |
| 793 |
char *p; |
| 794 |
time_t LastUpdate; |
| 795 |
|
| 796 |
#if defined(TIMER) |
| 797 |
TMRinit(); |
| 798 |
#endif /* defined(TIMER) */ |
| 799 |
|
| 800 |
LastUpdate = GetTimeInfo(&Now) < 0 ? 0 : Now.time; |
| 801 |
for ( ; ; ) { |
| 802 |
/* See if any processes died. */ |
| 803 |
PROCscan(); |
| 804 |
|
| 805 |
/* Wait for data, note the time. */ |
| 806 |
MyRead = RCHANmask; |
| 807 |
MyWrite = WCHANmask; |
| 808 |
MyTime = TimeOut; |
| 809 |
#if defined(TIMER) |
| 810 |
i = TMRmainloophook(); |
| 811 |
if (i) |
| 812 |
MyTime.tv_sec = i; |
| 813 |
TMRstart(TMR_IDLE); |
| 814 |
#endif /* defined(TIMER) */ |
| 815 |
count = select(CHANlastfd + 1, &MyRead, &MyWrite, (FDSET *)NULL, |
| 816 |
&MyTime); |
| 817 |
#if defined(TIMER) |
| 818 |
TMRstop(TMR_IDLE); |
| 819 |
#endif /* defined(TIMER) */ |
| 820 |
if (GotTerminate) { |
| 821 |
(void)write(2, EXITING, STRLEN(EXITING)); |
| 822 |
CleanupAndExit(0, (char *)NULL); |
| 823 |
} |
| 824 |
if (count < 0) { |
| 825 |
if (errno != EINTR) { |
| 826 |
syslog(L_ERROR, "%s cant select %m", LogName); |
| 827 |
#if defined(INND_FIND_BAD_FDS) |
| 828 |
CHANdiagnose(); |
| 829 |
#endif /* defined(INND_FIND_BAD_FDS) */ |
| 830 |
} |
| 831 |
continue; |
| 832 |
} |
| 833 |
|
| 834 |
/* Update the "reasonably accurate" time. */ |
| 835 |
if (GetTimeInfo(&Now) < 0) |
| 836 |
syslog(L_ERROR, "%s cant gettimeinfo %m", LogName); |
| 837 |
if (Now.time > LastUpdate + TimeOut.tv_sec) { |
| 838 |
HISsync(); |
| 839 |
if (ICDactivedirty) { |
| 840 |
ICDwriteactive(); |
| 841 |
ICDactivedirty = 0; |
| 842 |
} |
| 843 |
LastUpdate = Now.time; |
| 844 |
} |
| 845 |
|
| 846 |
if (count == 0) { |
| 847 |
/* No channels active, so flush and skip if nobody's |
| 848 |
* sleeping. */ |
| 849 |
if (Mode == OMrunning) |
| 850 |
ICDwrite(); |
| 851 |
if (SCHANcount == 0) |
| 852 |
continue; |
| 853 |
} |
| 854 |
|
| 855 |
/* Try the control channel first. */ |
| 856 |
if (FD_ISSET(CHANccfd, &RCHANmask) && FD_ISSET(CHANccfd, &MyRead)) { |
| 857 |
count--; |
| 858 |
if (count > 3) count = 3; /* might be more requests */ |
| 859 |
(*CHANcc->Reader)(CHANcc); |
| 860 |
FD_CLR(CHANccfd, &MyRead); |
| 861 |
} |
| 862 |
|
| 863 |
#ifdef PRIORITISE_REMCONN |
| 864 |
/* Try the remconn channel next. */ |
| 865 |
if (FD_ISSET(CHANrcfd, &RCHANmask) && FD_ISSET(CHANrcfd, &MyRead)) { |
| 866 |
count--; |
| 867 |
if (count > 3) count = 3; /* might be more requests */ |
| 868 |
(*CHANrc->Reader)(CHANrc); |
| 869 |
FD_CLR(CHANrcfd, &MyRead); |
| 870 |
} |
| 871 |
#endif /* PRIORITISE_REMCONN */ |
| 872 |
|
| 873 |
/* Loop through all active channels. Somebody could have closed |
| 874 |
* closed a channel so we double-check the global mask before |
| 875 |
* looking at what select returned. The code here is written so |
| 876 |
* that a channel could be reading and writing and sleeping at the |
| 877 |
* same time, even though that's not possible. (Just as well, |
| 878 |
* since in SysVr4 the count would be wrong.) */ |
| 879 |
lastfd = CHANlastfd; |
| 880 |
if (lastfd < CHANlastsleepfd) |
| 881 |
lastfd = CHANlastsleepfd; |
| 882 |
if (fd > lastfd) |
| 883 |
fd = 0; |
| 884 |
startpoint = fd; |
| 885 |
do { |
| 886 |
cp = &CHANtable[fd]; |
| 887 |
|
| 888 |
/* Anything to read? */ |
| 889 |
if (FD_ISSET(fd, &RCHANmask) && FD_ISSET(fd, &MyRead)) { |
| 890 |
count--; |
| 891 |
if (cp->Type == CTfree) { |
| 892 |
syslog(L_ERROR, "%s %d free but was in RMASK", |
| 893 |
CHANname(cp), fd); |
| 894 |
/* Don't call RCHANremove since cp->fd will be -1. */ |
| 895 |
FD_CLR(fd, &RCHANmask); |
| 896 |
(void)close(fd); |
| 897 |
} |
| 898 |
else { |
| 899 |
cp->LastActive = Now.time; |
| 900 |
(*cp->Reader)(cp); |
| 901 |
} |
| 902 |
} |
| 903 |
|
| 904 |
|
| 905 |
#define MAX(a,b) ((a) > (b) ? (a) : (b)) |
| 906 |
|
| 907 |
/* Check and see if the buffer is grossly overallocated and shrink |
| 908 |
if needed */ |
| 909 |
if (cp->In.Size > (START_BUFF_SIZE * 4)) { |
| 910 |
if (cp->In.Used) { |
| 911 |
if ((cp->In.Size / cp->In.Used) > 10) { |
| 912 |
cp->In.Size = MAX(cp->In.Used * 2, START_BUFF_SIZE); |
| 913 |
cp->In.Data = RENEW(cp->In.Data, char, cp->In.Size); |
| 914 |
cp->In.Left = cp->In.Size - cp->In.Used; |
| 915 |
} |
| 916 |
} else { |
| 917 |
cp->In.Data = RENEW(cp->In.Data, char, START_BUFF_SIZE); |
| 918 |
cp->In.Size = cp->In.Left = START_BUFF_SIZE; |
| 919 |
} |
| 920 |
} |
| 921 |
|
| 922 |
/* Possibly recheck for dead children so we don't get SIGPIPE |
| 923 |
* on readerless channels. */ |
| 924 |
if (PROCneedscan) |
| 925 |
PROCscan(); |
| 926 |
|
| 927 |
/* Ready to write? */ |
| 928 |
if (FD_ISSET(fd, &WCHANmask) && FD_ISSET(fd, &MyWrite)) { |
| 929 |
count--; |
| 930 |
if (cp->Type == CTfree) { |
| 931 |
syslog(L_ERROR, "%s %d free but was in WMASK", |
| 932 |
CHANname(cp), fd); |
| 933 |
/* Don't call WCHANremove since cp->fd will be -1. */ |
| 934 |
FD_CLR(fd, &WCHANmask); |
| 935 |
(void)close(fd); |
| 936 |
} |
| 937 |
else { |
| 938 |
bp = &cp->Out; |
| 939 |
if (bp->Left) { |
| 940 |
cp->LastActive = Now.time; |
| 941 |
i = largewrite(fd, &bp->Data[bp->Used], bp->Left); |
| 942 |
if (i <= 0) { |
| 943 |
oerrno = errno; |
| 944 |
p = CHANname(cp); |
| 945 |
errno = oerrno; |
| 946 |
if (i < 0) |
| 947 |
syslog(L_ERROR, "%s cant write %m", p); |
| 948 |
else |
| 949 |
syslog(L_ERROR, "%s cant write", p); |
| 950 |
cp->BadWrites++; |
| 951 |
if (i < 0 && oerrno == EPIPE) { |
| 952 |
SITEchanclose(cp); |
| 953 |
CHANclose(cp, p); |
| 954 |
} |
| 955 |
else if (i < 0 && oerrno == EWOULDBLOCK) { |
| 956 |
WCHANremove(cp); |
| 957 |
CHANwritesleep(cp, p); |
| 958 |
} |
| 959 |
else if (cp->BadWrites >= BAD_IO_COUNT) { |
| 960 |
syslog(L_ERROR, "%s sleeping", p); |
| 961 |
WCHANremove(cp); |
| 962 |
SCHANadd(cp, |
| 963 |
(time_t)(Now.time + PAUSE_RETRY_TIME), |
| 964 |
(POINTER)NULL, CHANwakeup, (POINTER)NULL); |
| 965 |
} |
| 966 |
} |
| 967 |
else { |
| 968 |
cp->BadWrites = 0; |
| 969 |
cp->BlockedWrites = 0; |
| 970 |
bp->Left -= i; |
| 971 |
bp->Used += i; |
| 972 |
if (bp->Left <= 0) { |
| 973 |
WCHANremove(cp); |
| 974 |
(*cp->WriteDone)(cp); |
| 975 |
} else if (bp->Used > (bp->Size/COMP_THRESHOLD)) { |
| 976 |
/* compact the buffer, shoving the |
| 977 |
data back to the beginning. |
| 978 |
<[email protected]> */ |
| 979 |
BUFFset(bp, &bp->Data[bp->Used], bp->Left); |
| 980 |
} |
| 981 |
} |
| 982 |
} |
| 983 |
else |
| 984 |
/* Should not be possible. */ |
| 985 |
WCHANremove(cp); |
| 986 |
} |
| 987 |
} |
| 988 |
|
| 989 |
/* Coming off a sleep? */ |
| 990 |
if (FD_ISSET(fd, &SCHANmask) && cp->Waketime <= Now.time) { |
| 991 |
if (cp->Type == CTfree) { |
| 992 |
syslog(L_ERROR,"%s ERROR s-select free %d",CHANname(cp),fd); |
| 993 |
FD_CLR(fd, &SCHANmask); |
| 994 |
(void) close(fd); |
| 995 |
} else { |
| 996 |
cp->LastActive = Now.time; |
| 997 |
SCHANremove(cp); |
| 998 |
(*cp->Waker)(cp); |
| 999 |
} |
| 1000 |
} |
| 1001 |
|
| 1002 |
/* Toss CTreject channel early if it's inactive. */ |
| 1003 |
if (cp->Type == CTreject |
| 1004 |
&& cp->LastActive + REJECT_TIMEOUT < Now.time) { |
| 1005 |
p = CHANname(cp); |
| 1006 |
syslog(L_NOTICE, "%s timeout reject", p); |
| 1007 |
CHANclose(cp, p); |
| 1008 |
} |
| 1009 |
|
| 1010 |
/* Has this channel been inactive very long? */ |
| 1011 |
if (cp->Type == CTnntp |
| 1012 |
&& cp->LastActive + cp->NextLog < Now.time) { |
| 1013 |
p = CHANname(cp); |
| 1014 |
silence = Now.time - cp->LastActive; |
| 1015 |
cp->NextLog += CHANNEL_INACTIVE_TIME; |
| 1016 |
syslog(L_NOTICE, "%s inactive %ld", p, silence / 60L); |
| 1017 |
if (silence > PEER_TIMEOUT) { |
| 1018 |
syslog(L_NOTICE, "%s timeout", p); |
| 1019 |
CHANclose(cp, p); |
| 1020 |
} |
| 1021 |
} |
| 1022 |
|
| 1023 |
/* Bump pointer, modulo the table size. */ |
| 1024 |
if (fd >= lastfd) |
| 1025 |
fd = 0; |
| 1026 |
else |
| 1027 |
fd++; |
| 1028 |
|
| 1029 |
/* If there is nothing to do, break out. */ |
| 1030 |
if (count == 0 && SCHANcount == 0) |
| 1031 |
break; |
| 1032 |
|
| 1033 |
} while (fd != startpoint); |
| 1034 |
} |
| 1035 |
} |