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
|
/* -*-pgsql-c-*- */
/*
* $Header: /cvsroot/pgpool/pgpool/pool_stream.c,v 1.6 2006/02/05 01:59:27 devrim Exp $
*
* pgpool: a language independent connection pool server for PostgreSQL
* written by Tatsuo Ishii
*
* Copyright (c) 2003-2006 PgPool Global Development Group
*
* Permission to use, copy, modify, and distribute this software and
* its documentation for any purpose and without fee is hereby
* granted, provided that the above copyright notice appear in all
* copies and that both that copyright notice and this permission
* notice appear in supporting documentation, and that the name of the
* author not be used in advertising or publicity pertaining to
* distribution of the software without specific, written prior
* permission. The author makes no representations about the
* suitability of this software for any purpose. It is provided "as
* is" without express or implied warranty.
*
* pool_stream.c: stream I/O modules
*
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include "pool.h"
#define READBUFSZ 1024
#define WRITEBUFSZ 8192
static int mystrlen(char *str, int upper, int *flag);
static int mystrlinelen(char *str, int upper, int *flag);
static int save_pending_data(POOL_CONNECTION *cp, void *data, int len);
static int consume_pending_data(POOL_CONNECTION *cp, void *data, int len);
/*
* open read/write file descriptors.
* returns POOL_CONNECTION on success otherwise NULL.
*/
POOL_CONNECTION *pool_open(int fd)
{
POOL_CONNECTION *cp;
cp = (POOL_CONNECTION *)malloc(sizeof(POOL_CONNECTION));
if (cp == NULL)
{
pool_error("pool_open: malloc failed: %s", strerror(errno));
return NULL;
}
memset(cp, 0, sizeof(*cp));
/* initialize write buffer */
cp->wbuf = malloc(WRITEBUFSZ);
if (cp->wbuf == NULL)
{
pool_error("pool_open: malloc failed");
return NULL;
}
cp->wbufsz = WRITEBUFSZ;
cp->wbufpo = 0;
/* initialize pending data buffer */
cp->hp = malloc(READBUFSZ);
if (cp->hp == NULL)
{
pool_error("pool_open: malloc failed");
return NULL;
}
cp->bufsz = READBUFSZ;
cp->po = 0;
cp->len = 0;
cp->sbuf = NULL;
cp->sbufsz = 0;
cp->buf2 = NULL;
cp->sbufsz = 0;
cp->fd = fd;
return cp;
}
/*
* close read/write file descriptors.
*/
void pool_close(POOL_CONNECTION *cp)
{
close(cp->fd);
free(cp->wbuf);
free(cp->hp);
if (cp->sbuf)
free(cp->sbuf);
if (cp->buf2)
free(cp->buf2);
pool_discard_params(&cp->params);
free(cp);
}
/*
* read len bytes from cp
* returns 0 on success otherwise -1.
*/
int pool_read(POOL_CONNECTION *cp, void *buf, int len)
{
static char readbuf[READBUFSZ];
int consume_size;
int readlen;
int notimeout;
consume_size = consume_pending_data(cp, buf, len);
len -= consume_size;
buf += consume_size;
/* if this is the secondary backend, then set timeout */
notimeout = !cp->issecondary_backend;
while (len > 0)
{
if (pool_check_fd(cp, notimeout))
{
if (cp->issecondary_backend)
{
pool_log("pool_read: secondary data is not ready. abort this session");
exit(1);
}
else
{
pool_error("pool_read: pool_check_fd failed (%s)", strerror(errno));
return -1;
}
}
readlen = read(cp->fd, readbuf, READBUFSZ);
if (readlen == -1)
{
if (errno == EINTR || errno == EAGAIN)
{
pool_debug("pool_read: retrying due to %s", strerror(errno));
continue;
}
pool_error("pool_read: read failed (%s)", strerror(errno));
if (cp->isbackend)
{
/* fatal error, notice to parent and exit */
notice_backend_error(!cp->issecondary_backend);
exit(1);
}
else
{
return -1;
}
}
else if (readlen == 0)
{
if (cp->isbackend)
{
pool_error("pool_read2: EOF encountered with backend");
return -1;
#ifdef NOT_USED
/* fatal error, notice to parent and exit */
notice_backend_error(!cp->issecondary_backend);
exit(1);
#endif
}
else
{
/*
* if backend offers authentication method, frontend could close connection
*/
return -1;
}
}
if (len < readlen)
{
/* overrun. we need to save remaining data to pending buffer */
if (save_pending_data(cp, readbuf+len, readlen-len))
return -1;
memmove(buf, readbuf, len);
break;
}
memmove(buf, readbuf, readlen);
buf += readlen;
len -= readlen;
}
return 0;
}
/*
* read exactly len bytes from cp
* returns buffer address on success otherwise NULL.
*/
char *pool_read2(POOL_CONNECTION *cp, int len)
{
char *buf;
int req_size;
int alloc_size;
int consume_size;
int readlen;
int notimeout;
req_size = cp->len + len;
if (req_size > cp->bufsz2)
{
alloc_size = ((req_size+1)/READBUFSZ+1)*READBUFSZ;
cp->buf2 = realloc(cp->buf2, alloc_size);
if (cp->buf2 == NULL)
{
pool_error("pool_read2: failed to realloc");
exit(1);
}
cp->bufsz2 = alloc_size;
}
buf = cp->buf2;
consume_size = consume_pending_data(cp, buf, len);
len -= consume_size;
buf += consume_size;
/* if this is the secondary backend, then set timeout */
notimeout = !cp->issecondary_backend;
#ifdef NOT_USED
pool_debug("notimeout: %d isbackend: %d issecondary_backend: %d", notimeout,
cp->isbackend, cp->issecondary_backend);
#endif
while (len > 0)
{
if (pool_check_fd(cp, notimeout))
{
if (cp->issecondary_backend)
{
pool_log("pool_read2: secondary data is not ready. abort this session");
exit(1);
}
else
{
pool_error("pool_read2: pool_check_fd failed (%s)", strerror(errno));
return NULL;
}
}
readlen = read(cp->fd, buf, len);
if (readlen == -1)
{
if (errno == EINTR || errno == EAGAIN)
{
pool_debug("pool_read2: retrying due to %s", strerror(errno));
continue;
}
pool_error("pool_read2: read failed (%s)", strerror(errno));
if (cp->isbackend)
{
/* fatal error, notice to parent and exit */
notice_backend_error(!cp->issecondary_backend);
exit(1);
}
else
{
return NULL;
}
}
else if (readlen == 0)
{
if (cp->isbackend)
{
pool_error("pool_read2: EOF encountered with backend");
return NULL;
#ifdef NOT_USED
/* fatal error, notice to parent and exit */
notice_backend_error(!cp->issecondary_backend);
exit(1);
#endif
}
else
{
/*
* if backend offers authentication method, frontend could close connection
*/
return NULL;
}
}
buf += readlen;
len -= readlen;
}
return cp->buf2;
}
/*
* write len bytes to cp the write buffer.
* returns 0 on success otherwise -1.
*/
int pool_write(POOL_CONNECTION *cp, void *buf, int len)
{
int reqlen;
if (len < 0)
{
pool_error("pool_write: invalid request size: %d", len);
return -1;
}
if (cp->no_forward)
return 0;
/* check buffer size */
reqlen = cp->wbufpo + len;
if (reqlen > cp->wbufsz)
{
char *p;
reqlen = (reqlen/WRITEBUFSZ+1)*WRITEBUFSZ;
p = realloc(cp->wbuf, reqlen);
if (p == NULL)
{
pool_error("pool_write: realloc failed");
return -1;
}
cp->wbuf = p;
cp->wbufsz = reqlen;
}
memcpy(cp->wbuf+cp->wbufpo, buf, len);
cp->wbufpo += len;
return 0;
}
/*
* flush write buffer
*/
int pool_flush_it(POOL_CONNECTION *cp)
{
int sts;
int wlen;
int offset;
wlen = cp->wbufpo;
if (wlen == 0)
{
return 0;
}
offset = 0;
for (;;)
{
errno = 0;
#ifdef NOT_USED
if (!cp->isbackend)
{
fd_set writemask;
fd_set exceptmask;
FD_ZERO(&writemask);
FD_ZERO(&exceptmask);
FD_SET(cp->fd, &writemask);
FD_SET(cp->fd, &exceptmask);
sts = select(cp->fd+1, NULL, &writemask, &exceptmask, NULL);
if (sts == -1)
{
if (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK)
continue;
pool_error("pool_flush_it: select() failed. reason: %s", strerror(errno));
return -1;
}
else if (sts == 0)
{
continue;
}
else if (FD_ISSET(cp->fd, &exceptmask))
{
pool_log("pool_flush_it: exception occured");
return -1;
}
}
#endif
sts = write(cp->fd, cp->wbuf + offset, wlen);
if (sts > 0)
{
wlen -= sts;
if (wlen == 0)
{
/* write completed */
break;
}
else if (wlen < 0)
{
pool_error("pool_flush_it: invalid write size %d", sts);
return -1;
}
else
{
/* need to write remaining data */
offset += sts;
continue;
}
}
else if (errno == EAGAIN || errno == EINTR)
{
continue;
}
else
{
pool_error("pool_flush_it: write failed (%s) offset: %d wlen: %d",
strerror(errno), offset, wlen);
return -1;
}
}
cp->wbufpo = 0;
return 0;
}
/*
* flush write buffer and degenerate/failover if error occurs
*/
int pool_flush(POOL_CONNECTION *cp)
{
if (pool_flush_it(cp) == -1)
{
if (cp->isbackend)
{
notice_backend_error(!cp->issecondary_backend);
exit(1);
}
else
{
return -1;
}
}
return 0;
}
/*
* combo of pool_write and pool_flush
*/
int pool_write_and_flush(POOL_CONNECTION *cp, void *buf, int len)
{
if (pool_write(cp, buf, len))
return -1;
return pool_flush(cp);
}
/*
* read a string until EOF or NULL is encountered.
* if line is not 0, read until new line is encountered.
*/
char *pool_read_string(POOL_CONNECTION *cp, int *len, int line)
{
int readp;
int readsize;
int readlen;
int strlength;
int flag;
int consume_size;
int notimeout;
#ifdef DEBUG
static char pbuf[READBUFSZ];
#endif
*len = 0;
readp = 0;
/* initialize read buffer */
if (cp->sbufsz == 0)
{
cp->sbuf = malloc(READBUFSZ);
if (cp->sbuf == NULL)
{
pool_error("pool_read_string: malloc failed");
return NULL;
}
cp->sbufsz = READBUFSZ;
*cp->sbuf = '\0';
}
/* any pending data? */
if (cp->len)
{
if (line)
strlength = mystrlinelen(cp->hp+cp->po, cp->len, &flag);
else
strlength = mystrlen(cp->hp+cp->po, cp->len, &flag);
/* buffer is too small? */
if ((strlength + 1) > cp->sbufsz)
{
cp->sbufsz = ((strlength+1)/READBUFSZ+1)*READBUFSZ;
cp->sbuf = realloc(cp->sbuf, cp->sbufsz);
if (cp->sbuf == NULL)
{
pool_error("pool_read_string: realloc failed");
return NULL;
}
}
/* consume pending and save to read string buffer */
consume_size = consume_pending_data(cp, cp->sbuf, strlength);
*len = strlength;
/* is the string null terminated? */
if (consume_size == strlength && !flag)
{
/* not null or line terminated.
* we need to read more since we have not encountered NULL or new line yet
*/
readsize = cp->sbufsz - strlength;
readp = strlength;
}
else
{
pool_debug("pool_read_string: read all from pending data. po:%d len:%d",
cp->po, cp->len);
return cp->sbuf;
}
} else
{
readsize = cp->sbufsz;
}
/* if this is the secondary backend, then set timeout */
notimeout = !cp->issecondary_backend;
for (;;)
{
if (pool_check_fd(cp, notimeout))
{
if (cp->issecondary_backend)
{
pool_log("pool_read_string: secondary data is not ready. abort this session");
exit(1);
}
else
{
pool_error("pool_read_string: pool_check_fd failed (%s)", strerror(errno));
return NULL;
}
}
readlen = read(cp->fd, cp->sbuf+readp, readsize);
if (readlen == -1)
{
pool_error("pool_read_string: read() failed. reason:%s", strerror(errno));
if (cp->isbackend)
{
notice_backend_error(!cp->issecondary_backend);
exit(1);
}
else
{
return NULL;
}
}
else if (readlen == 0) /* EOF detected */
{
/*
* just returns an error, not trigger failover or degeneration
*/
pool_error("pool_read_string: read () EOF detected");
return NULL;
}
/* check overrun */
if (line)
strlength = mystrlinelen(cp->sbuf+readp, readlen, &flag);
else
strlength = mystrlen(cp->sbuf+readp, readlen, &flag);
if (strlength < readlen)
{
save_pending_data(cp, cp->sbuf+readp+strlength, readlen-strlength);
*len += strlength;
pool_debug("pool_read_string: total result %d with pending data po:%d len:%d", *len, cp->po, cp->len);
return cp->sbuf;
}
*len += readlen;
/* encountered null or newline? */
if (flag)
{
/* ok we have read all data */
pool_debug("pool_read_string: total result %d ", *len);
break;
}
readp += readlen;
readsize = READBUFSZ;
if ((*len+readsize) > cp->sbufsz)
{
cp->sbufsz += READBUFSZ;
cp->sbuf = realloc(cp->sbuf, cp->sbufsz);
if (cp->sbuf == NULL)
{
pool_error("pool_read_string: realloc failed");
return NULL;
}
}
}
return cp->sbuf;
}
/*
* returns the byte length of str, including \0, no more than upper.
* if encountered \0, flag is set to non 0.
* example:
* mystrlen("abc", 2) returns 2
* mystrlen("abc", 3) returns 3
* mystrlen("abc", 4) returns 4
* mystrlen("abc", 5) returns 4
*/
static int mystrlen(char *str, int upper, int *flag)
{
int len;
*flag = 0;
for (len = 0;len < upper; len++, str++)
{
if (!*str)
{
len++;
*flag = 1;
break;
}
}
return len;
}
/*
* returns the byte length of str terminated by \n or \0 (including \n or \0), no more than upper.
* if encountered \0 or \n, flag is set to non 0.
* example:
* mystrlinelen("abc", 2) returns 2
* mystrlinelen("abc", 3) returns 3
* mystrlinelen("abc", 4) returns 4
* mystrlinelen("abc", 5) returns 4
* mystrlinelen("abcd\nefg", 4) returns 4
* mystrlinelen("abcd\nefg", 5) returns 5
* mystrlinelen("abcd\nefg", 6) returns 5
*/
static int mystrlinelen(char *str, int upper, int *flag)
{
int len;
*flag = 0;
for (len = 0;len < upper; len++, str++)
{
if (!*str || *str == '\n')
{
len++;
*flag = 1;
break;
}
}
return len;
}
/*
* save pending data
*/
static int save_pending_data(POOL_CONNECTION *cp, void *data, int len)
{
int reqlen;
size_t realloc_size;
char *p;
/* to be safe */
if (cp->len == 0)
cp->po = 0;
reqlen = cp->po + cp->len + len;
/* pending buffer is enough? */
if (reqlen > cp->bufsz)
{
/* too small, enlarge it */
realloc_size = (reqlen/READBUFSZ+1)*READBUFSZ;
p = realloc(cp->hp, realloc_size);
if (p == NULL)
{
pool_error("save_pending_data: realloc failed");
return -1;
}
cp->bufsz = realloc_size;
cp->hp = p;
}
memmove(cp->hp + cp->po + cp->len, data, len);
cp->len += len;
return 0;
}
/*
* consume pending data. returns actually consumed data length.
*/
static int consume_pending_data(POOL_CONNECTION *cp, void *data, int len)
{
int consume_size;
if (cp->len <= 0)
return 0;
consume_size = Min(len, cp->len);
memmove(data, cp->hp + cp->po, consume_size);
cp->len -= consume_size;
if (cp->len <= 0)
cp->po = 0;
else
cp->po += consume_size;
return consume_size;
}
|