#include #include #include #include #include "libpq-fe.h" #include "libpq-int.h" #include "mb/pg_wchar.h" #include "pg_config_paths.h" #include "port/pg_bswap.h"">

PostgreSQL Source Code: src/interfaces/libpq/fe-misc.c File Reference (original) (raw)

#include "[postgres_fe.h](postgres%5F%5Ffe%5F8h%5Fsource.html)"
#include <signal.h>
#include <[time.h](time%5F8h%5Fsource.html)>
#include <[unistd.h](unistd%5F8h%5Fsource.html)>
#include <[sys/select.h](select%5F8h%5Fsource.html)>
#include <poll.h>
#include "[libpq-fe.h](libpq-fe%5F8h%5Fsource.html)"
#include "[libpq-int.h](libpq-int%5F8h%5Fsource.html)"
#include "[mb/pg_wchar.h](pg%5F%5Fwchar%5F8h%5Fsource.html)"
#include "pg_config_paths.h"
#include "[port/pg_bswap.h](pg%5F%5Fbswap%5F8h%5Fsource.html)"

Go to the source code of this file.

Functions
static int pqPutMsgBytes (const void *buf, size_t len, PGconn *conn)
static int pqSendSome (PGconn *conn, int len)
static int pqSocketCheck (PGconn *conn, int forRead, int forWrite, pg_usec_time_t end_time)
int PQlibVersion (void)
int pqGetc (char *result, PGconn *conn)
int pqPutc (char c, PGconn *conn)
static int pqGets_internal (PQExpBuffer buf, PGconn *conn, bool resetbuffer)
int pqGets (PQExpBuffer buf, PGconn *conn)
int pqGets_append (PQExpBuffer buf, PGconn *conn)
int pqPuts (const char *s, PGconn *conn)
int pqGetnchar (void *s, size_t len, PGconn *conn)
int pqSkipnchar (size_t len, PGconn *conn)
int pqPutnchar (const void *s, size_t len, PGconn *conn)
int pqGetInt (int *result, size_t bytes, PGconn *conn)
int pqPutInt (int value, size_t bytes, PGconn *conn)
int pqCheckOutBufferSpace (size_t bytes_needed, PGconn *conn)
int pqCheckInBufferSpace (size_t bytes_needed, PGconn *conn)
void pqParseDone (PGconn *conn, int newInStart)
int pqPutMsgStart (char msg_type, PGconn *conn)
int pqPutMsgEnd (PGconn *conn)
int pqReadData (PGconn *conn)
int pqFlush (PGconn *conn)
int pqWait (int forRead, int forWrite, PGconn *conn)
int pqWaitTimed (int forRead, int forWrite, PGconn *conn, pg_usec_time_t end_time)
int pqReadReady (PGconn *conn)
int pqWriteReady (PGconn *conn)
int PQsocketPoll (int sock, int forRead, int forWrite, pg_usec_time_t end_time)
pg_usec_time_t PQgetCurrentTimeUSec (void)
int PQmblen (const char *s, int encoding)
int PQmblenBounded (const char *s, int encoding)
int PQdsplen (const char *s, int encoding)
int PQenv2encoding (void)
void libpq_append_error (PQExpBuffer errorMessage, const char *fmt,...)
void libpq_append_conn_error (PGconn *conn, const char *fmt,...)

libpq_append_conn_error()

void libpq_append_conn_error ( PGconn * conn,
const char * fmt,
...
)

Definition at line 1378 of file fe-misc.c.

1379{

1380 int save_errno = errno;

1381 bool done;

1382 va_list args;

1383

1384 Assert(fmt[strlen(fmt) - 1] != '\n');

1385

1387 return;

1388

1389

1390 do

1391 {

1392 errno = save_errno;

1393 va_start(args, fmt);

1395 va_end(args);

1396 } while (!done);

1397

1399}

Assert(PointerIsAligned(start, uint64))

bool appendPQExpBufferVA(PQExpBuffer str, const char *fmt, va_list args)

void appendPQExpBufferChar(PQExpBuffer str, char ch)

#define PQExpBufferBroken(str)

PQExpBufferData errorMessage

References appendPQExpBufferChar(), appendPQExpBufferVA(), generate_unaccent_rules::args, Assert(), conn, pg_conn::errorMessage, libpq_gettext, and PQExpBufferBroken.

Referenced by pqReadData(), pqSocketCheck(), and pqWaitTimed().

libpq_append_error()

void libpq_append_error ( PQExpBuffer errorMessage,
const char * fmt,
...
)

Definition at line 1349 of file fe-misc.c.

1350{

1351 int save_errno = errno;

1352 bool done;

1353 va_list args;

1354

1355 Assert(fmt[strlen(fmt) - 1] != '\n');

1356

1358 return;

1359

1360

1361 do

1362 {

1363 errno = save_errno;

1364 va_start(args, fmt);

1366 va_end(args);

1367 } while (!done);

1368

1370}

References appendPQExpBufferChar(), appendPQExpBufferVA(), generate_unaccent_rules::args, Assert(), libpq_gettext, and PQExpBufferBroken.

Referenced by conninfo_add_defaults(), conninfo_array_parse(), conninfo_init(), conninfo_parse(), conninfo_storeval(), conninfo_uri_decode(), conninfo_uri_parse_options(), conninfo_uri_parse_params(), parseServiceFile(), parseServiceInfo(), pg_fe_getusername(), and read_attr_value().

pqCheckInBufferSpace()

int pqCheckInBufferSpace ( size_t bytes_needed,
PGconn * conn
)

Definition at line 351 of file fe-misc.c.

352{

354 char *newbuf;

355

356

357 if (bytes_needed <= (size_t) newsize)

358 return 0;

359

360

361

362

363

364

365

366

368

370 {

372 {

378 }

379 }

380 else

381 {

382

384 }

385

386

387 if (bytes_needed <= (size_t) newsize)

388 return 0;

389

390

391

392

393

394

395

396

397 do

398 {

399 newsize *= 2;

400 } while (newsize > 0 && bytes_needed > (size_t) newsize);

401

402 if (newsize > 0 && bytes_needed <= (size_t) newsize)

403 {

405 if (newbuf)

406 {

407

410 return 0;

411 }

412 }

413

415 do

416 {

417 newsize += 8192;

418 } while (newsize > 0 && bytes_needed > (size_t) newsize);

419

420 if (newsize > 0 && bytes_needed <= (size_t) newsize)

421 {

423 if (newbuf)

424 {

425

428 return 0;

429 }

430 }

431

432

434 "cannot allocate memory for input buffer\n");

435 return EOF;

436}

void appendPQExpBufferStr(PQExpBuffer str, const char *data)

References appendPQExpBufferStr(), conn, pg_conn::errorMessage, pg_conn::inBuffer, pg_conn::inBufSize, pg_conn::inCursor, pg_conn::inEnd, pg_conn::inStart, and realloc.

Referenced by getCopyDataMessage(), PQconnectPoll(), pqFunctionCall3(), pqParseInput3(), and pqReadData().

pqCheckOutBufferSpace()

int pqCheckOutBufferSpace ( size_t bytes_needed,
PGconn * conn
)

Definition at line 287 of file fe-misc.c.

288{

290 char *newbuf;

291

292

293 if (bytes_needed <= (size_t) newsize)

294 return 0;

295

296

297

298

299

300

301

302

303 do

304 {

305 newsize *= 2;

306 } while (newsize > 0 && bytes_needed > (size_t) newsize);

307

308 if (newsize > 0 && bytes_needed <= (size_t) newsize)

309 {

311 if (newbuf)

312 {

313

316 return 0;

317 }

318 }

319

321 do

322 {

323 newsize += 8192;

324 } while (newsize > 0 && bytes_needed > (size_t) newsize);

325

326 if (newsize > 0 && bytes_needed <= (size_t) newsize)

327 {

329 if (newbuf)

330 {

331

334 return 0;

335 }

336 }

337

338

340 "cannot allocate memory for output buffer\n");

341 return EOF;

342}

References appendPQExpBufferStr(), conn, pg_conn::errorMessage, pg_conn::outBuffer, pg_conn::outBufSize, and realloc.

Referenced by PQputCopyData(), pqPutMsgBytes(), and pqPutMsgStart().

PQdsplen()

int PQdsplen ( const char * s,
int encoding
)

PQenv2encoding()

int PQenv2encoding ( void )

pqFlush()

Definition at line 968 of file fe-misc.c.

969{

971 {

974

976 }

977

978 return 0;

979}

static int pqSendSome(PGconn *conn, int len)

References conn, pg_conn::outCount, pg_conn::Pfdebug, and pqSendSome().

Referenced by pg_SASL_init(), PQconnectPoll(), PQconsumeInput(), pqEndcopy3(), PQexitPipelineMode(), PQflush(), pqFunctionCall3(), PQgetResult(), pqPacketSend(), pqPipelineFlush(), pqPipelineSyncInternal(), PQputCopyData(), PQputCopyEnd(), PQsendCancelRequest(), PQsendQueryInternal(), PQsetnonblocking(), and sendTerminateConn().

pqGetc()

int pqGetc ( char * result,
PGconn * conn
)

PQgetCurrentTimeUSec()

pqGetInt()

int pqGetInt ( int * result,
size_t bytes,
PGconn * conn
)

Definition at line 216 of file fe-misc.c.

217{

220

221 switch (bytes)

222 {

223 case 2:

225 return EOF;

229 break;

230 case 4:

232 return EOF;

236 break;

237 default:

239 "integer of size %lu not supported by pqGetInt",

240 (unsigned long) bytes);

241 return EOF;

242 }

243

244 return 0;

245}

void pqInternalNotice(const PGNoticeHooks *hooks, const char *fmt,...)

PGNoticeHooks noticeHooks

References conn, pg_conn::inBuffer, pg_conn::inCursor, pg_conn::inEnd, pg_conn::noticeHooks, pg_ntoh16, pg_ntoh32, and pqInternalNotice().

Referenced by getAnotherTuple(), getBackendKeyData(), getCopyDataMessage(), getCopyStart(), getNotify(), getParamDescriptions(), getRowDescriptions(), PQconnectPoll(), pqFunctionCall3(), pqGetNegotiateProtocolVersion3(), and pqParseInput3().

pqGetnchar()

int pqGetnchar ( void * s,
size_t len,
PGconn * conn
)

pqGets()

Definition at line 136 of file fe-misc.c.

137{

139}

static int pqGets_internal(PQExpBuffer buf, PGconn *conn, bool resetbuffer)

References buf, conn, and pqGets_internal().

Referenced by getNotify(), getParameterStatus(), getRowDescriptions(), pg_SASL_init(), pqGetErrorNotice3(), pqGetNegotiateProtocolVersion3(), and pqParseInput3().

pqGets_append()

pqGets_internal()

static int pqGets_internal ( PQExpBuffer buf, PGconn * conn, bool resetbuffer ) static

PQlibVersion()

int PQlibVersion ( void )

Definition at line 63 of file fe-misc.c.

64{

65 return PG_VERSION_NUM;

66}

PQmblen()

int PQmblen ( const char * s,
int encoding
)

PQmblenBounded()

int PQmblenBounded ( const char * s,
int encoding
)

Definition at line 1240 of file fe-misc.c.

1241{

1243}

size_t strnlen(const char *str, size_t maxlen)

References encoding, pg_encoding_mblen(), and strnlen().

Referenced by command_no_begin(), dequote_downcase_identifier(), do_field(), patternToSQLRegex(), quote_if_needed(), reportErrorPosition(), skip_white_space(), splitTableColumnsSpec(), strip_quotes(), and strtokx().

pqParseDone()

void pqParseDone ( PGconn * conn,
int newInStart
)

Definition at line 443 of file fe-misc.c.

444{

445

448

449

451}

void pqTraceOutputMessage(PGconn *conn, const char *message, bool toServer)

References conn, pg_conn::inBuffer, pg_conn::inStart, pg_conn::Pfdebug, and pqTraceOutputMessage().

Referenced by getCopyDataMessage(), PQconnectPoll(), pqFunctionCall3(), pqGetCopyData3(), and pqParseInput3().

pqPutc()

int pqPutc ( char c,
PGconn * conn
)

pqPutInt()

int pqPutInt ( int value,
size_t bytes,
PGconn * conn
)

Definition at line 253 of file fe-misc.c.

254{

257

258 switch (bytes)

259 {

260 case 2:

263 return EOF;

264 break;

265 case 4:

268 return EOF;

269 break;

270 default:

272 "integer of size %lu not supported by pqPutInt",

273 (unsigned long) bytes);

274 return EOF;

275 }

276

277 return 0;

278}

References conn, pg_conn::noticeHooks, pg_hton16, pg_hton32, pqInternalNotice(), pqPutMsgBytes(), and value.

Referenced by pg_SASL_init(), pqFunctionCall3(), PQsendPrepare(), and PQsendQueryGuts().

pqPutMsgBytes()

static int pqPutMsgBytes ( const void * buf, size_t len, PGconn * conn ) static

pqPutMsgEnd()

int pqPutMsgEnd ( PGconn * conn )

Definition at line 532 of file fe-misc.c.

533{

534

536 {

538

541 }

542

543

545 {

548 else

551 }

552

553

555

557 {

559

561 return EOF;

562

563 }

564

565 return 0;

566}

void pqTraceOutputNoTypeByteMessage(PGconn *conn, const char *message)

References conn, pg_conn::outBuffer, pg_conn::outCount, pg_conn::outMsgEnd, pg_conn::outMsgStart, pg_conn::Pfdebug, pg_hton32, pqSendSome(), pqTraceOutputMessage(), and pqTraceOutputNoTypeByteMessage().

Referenced by pg_SASL_init(), pqEndcopy3(), pqFunctionCall3(), pqPacketSend(), pqPipelineSyncInternal(), PQputCopyData(), PQputCopyEnd(), PQsendCancelRequest(), PQsendFlushRequest(), PQsendPrepare(), PQsendQueryGuts(), PQsendQueryInternal(), PQsendTypedCommand(), and sendTerminateConn().

pqPutMsgStart()

int pqPutMsgStart ( char msg_type,
PGconn * conn
)

Definition at line 473 of file fe-misc.c.

474{

475 int lenPos;

476 int endPos;

477

478

479 if (msg_type)

481 else

483

484

485 lenPos = endPos;

486

487 endPos += 4;

488

489

491 return EOF;

492

493 if (msg_type)

495

498

499

500 return 0;

501}

References conn, pg_conn::outBuffer, pg_conn::outCount, pg_conn::outMsgEnd, pg_conn::outMsgStart, and pqCheckOutBufferSpace().

Referenced by pg_SASL_init(), pqEndcopy3(), pqFunctionCall3(), pqPacketSend(), pqPipelineSyncInternal(), PQputCopyData(), PQputCopyEnd(), PQsendCancelRequest(), PQsendFlushRequest(), PQsendPrepare(), PQsendQueryGuts(), PQsendQueryInternal(), PQsendTypedCommand(), and sendTerminateConn().

pqPutnchar()

int pqPutnchar ( const void * s,
size_t len,
PGconn * conn
)

pqPuts()

int pqPuts ( const char * s,
PGconn * conn
)

pqReadData()

int pqReadData ( PGconn * conn )

Definition at line 580 of file fe-misc.c.

581{

582 int someread = 0;

583 int nread;

584

586 {

588 return -1;

589 }

590

591

593 {

595 {

601 }

602 }

603 else

604 {

605

607 }

608

609

610

611

612

613

614

615

616

618 {

620 {

621

622

623

625 return -1;

626 }

627 }

628

629

630retry3:

633 if (nread < 0)

634 {

636 {

638 goto retry3;

639

640

641#ifdef EAGAIN

643 return someread;

644#endif

645#if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))

647 return someread;

648#endif

649

650

652 goto definitelyFailed;

653

654 default:

655

656 return -1;

657 }

658 }

659 if (nread > 0)

660 {

662

663

664

665

666

667

668

669

670

671

672

673

674

677 {

678 someread = 1;

679 goto retry3;

680 }

681 return 1;

682 }

683

684 if (someread)

685 return 1;

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703#ifdef USE_SSL

705 return 0;

706#endif

707

709 {

710 case 0:

711

712 return 0;

713 case 1:

714

715 break;

716 default:

717

718 goto definitelyEOF;

719 }

720

721

722

723

724

725retry4:

728 if (nread < 0)

729 {

731 {

733 goto retry4;

734

735

736#ifdef EAGAIN

738 return 0;

739#endif

740#if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))

742 return 0;

743#endif

744

745

747 goto definitelyFailed;

748

749 default:

750

751 return -1;

752 }

753 }

754 if (nread > 0)

755 {

757 return 1;

758 }

759

760

761

762

763

764definitelyEOF:

766 "\tThis probably means the server terminated abnormally\n"

767 "\tbefore or while processing the request.");

768

769

770definitelyFailed:

771

774 return -1;

775}

void pqDropConnection(PGconn *conn, bool flushInput)

int pqReadReady(PGconn *conn)

int pqCheckInBufferSpace(size_t bytes_needed, PGconn *conn)

void libpq_append_conn_error(PGconn *conn, const char *fmt,...)

ssize_t pqsecure_read(PGconn *conn, void *ptr, size_t len)

#define ALL_CONNECTION_FAILURE_ERRNOS

References ALL_CONNECTION_FAILURE_ERRNOS, conn, CONNECTION_BAD, EAGAIN, EINTR, EWOULDBLOCK, pg_conn::inBuffer, pg_conn::inBufSize, pg_conn::inCursor, pg_conn::inEnd, pg_conn::inStart, libpq_append_conn_error(), PGINVALID_SOCKET, pqCheckInBufferSpace(), pqDropConnection(), pqReadReady(), pqsecure_read(), pg_conn::sock, SOCK_ERRNO, pg_conn::ssl_in_use, and pg_conn::status.

Referenced by PQcancelPoll(), PQconnectPoll(), PQconsumeInput(), pqFunctionCall3(), pqGetCopyData3(), pqGetline3(), PQgetResult(), and pqSendSome().

pqReadReady()

int pqReadReady ( PGconn * conn )

pqSendSome()

static int pqSendSome ( PGconn * conn, int len ) static

Definition at line 799 of file fe-misc.c.

800{

803 int result = 0;

804

805

806

807

808

809

810

811

812

813

815 {

816

818

820 {

822 return -1;

823 }

824 return 0;

825 }

826

828 {

830

831

833

835 return 0;

836 }

837

838

839 while (len > 0)

840 {

841 int sent;

842

843#ifndef WIN32

845#else

846

847

848

849

850

851

853#endif

854

855 if (sent < 0)

856 {

857

859 {

860#ifdef EAGAIN

862 break;

863#endif

864#if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))

866 break;

867#endif

869 continue;

870

871 default:

872

874

875

877 {

879 return -1;

880 }

881

882

883

884

885

886

887

888

890 return 0;

891 else

892 return -1;

893 }

894 }

895 else

896 {

897 ptr += sent;

898 len -= sent;

900 }

901

902 if (len > 0)

903 {

904

905

906

907

908

909

910

911

912

913

914

915

916

917

918

919

920

921

922

923

924

925

926

927

928

929

930

932 {

933 result = -1;

934 break;

935 }

936

938 {

939 result = 1;

940 break;

941 }

942

944 {

945 result = -1;

946 break;

947 }

948 }

949 }

950

951

955

956 return result;

957}

int pqReadData(PGconn *conn)

int pqWait(int forRead, int forWrite, PGconn *conn)

ssize_t pqsecure_write(PGconn *conn, const void *ptr, size_t len)

#define pqIsnonblocking(conn)

References conn, EAGAIN, EINTR, EWOULDBLOCK, len, libpq_gettext, Min, pg_conn::outBuffer, pg_conn::outCount, PGINVALID_SOCKET, pqIsnonblocking, pqReadData(), pqsecure_write(), pqWait(), remaining, pg_conn::sock, SOCK_ERRNO, pg_conn::write_err_msg, and pg_conn::write_failed.

Referenced by pqFlush(), and pqPutMsgEnd().

pqSkipnchar()

int pqSkipnchar ( size_t len,
PGconn * conn
)

pqSocketCheck()

static int pqSocketCheck ( PGconn * conn, int forRead, int forWrite, pg_usec_time_t end_time ) static

Definition at line 1057 of file fe-misc.c.

1058{

1059 int result;

1061

1063 return -1;

1064

1067 else

1068 {

1071 {

1073 return -1;

1074 }

1075

1076#ifdef USE_SSL

1077

1079 {

1080

1081 return 1;

1082 }

1083#endif

1084 }

1085

1086

1087 do

1090

1091 if (result < 0)

1092 {

1094

1097 }

1098

1099 return result;

1100}

int PQsocketPoll(int sock, int forRead, int forWrite, pg_usec_time_t end_time)

bool pgtls_read_pending(PGconn *conn)

#define PG_STRERROR_R_BUFLEN

References pg_conn::altsock, conn, EINTR, end_time, libpq_append_conn_error(), PG_STRERROR_R_BUFLEN, PGINVALID_SOCKET, pgtls_read_pending(), PQsocketPoll(), pg_conn::sock, SOCK_ERRNO, SOCK_STRERROR, and pg_conn::ssl_in_use.

Referenced by pqReadReady(), pqWaitTimed(), and pqWriteReady().

PQsocketPoll()

int PQsocketPoll ( int sock,
int forRead,
int forWrite,
pg_usec_time_t end_time
)

Definition at line 1115 of file fe-misc.c.

1116{

1117

1118#ifdef HAVE_POLL

1119 struct pollfd input_fd;

1120 int timeout_ms;

1121

1122 if (!forRead && !forWrite)

1123 return 0;

1124

1125 input_fd.fd = sock;

1126 input_fd.events = POLLERR;

1127 input_fd.revents = 0;

1128

1129 if (forRead)

1130 input_fd.events |= POLLIN;

1131 if (forWrite)

1132 input_fd.events |= POLLOUT;

1133

1134

1136 timeout_ms = -1;

1138 timeout_ms = 0;

1139 else

1140 {

1142

1145 else

1146 timeout_ms = 0;

1147 }

1148

1149 return poll(&input_fd, 1, timeout_ms);

1150#else

1151

1152 fd_set input_mask;

1153 fd_set output_mask;

1154 fd_set except_mask;

1155 struct timeval timeout;

1156 struct timeval *ptr_timeout;

1157

1158 if (!forRead && !forWrite)

1159 return 0;

1160

1161 FD_ZERO(&input_mask);

1162 FD_ZERO(&output_mask);

1163 FD_ZERO(&except_mask);

1164 if (forRead)

1165 FD_SET(sock, &input_mask);

1166

1167 if (forWrite)

1168 FD_SET(sock, &output_mask);

1169 FD_SET(sock, &except_mask);

1170

1171

1173 ptr_timeout = NULL;

1175 {

1176 timeout.tv_sec = 0;

1177 timeout.tv_usec = 0;

1178 ptr_timeout = &timeout;

1179 }

1180 else

1181 {

1183

1185 {

1186 timeout.tv_sec = (end_time - now) / 1000000;

1187 timeout.tv_usec = (end_time - now) % 1000000;

1188 }

1189 else

1190 {

1191 timeout.tv_sec = 0;

1192 timeout.tv_usec = 0;

1193 }

1194 ptr_timeout = &timeout;

1195 }

1196

1197 return select(sock + 1, &input_mask, &output_mask,

1198 &except_mask, ptr_timeout);

1199#endif

1200}

Datum now(PG_FUNCTION_ARGS)

pg_usec_time_t PQgetCurrentTimeUSec(void)

#define select(n, r, w, e, timeout)

References end_time, now(), PQgetCurrentTimeUSec(), and select.

Referenced by pqSocketCheck(), timer_expired(), and wait_until_connected().

pqWait()

int pqWait ( int forRead,
int forWrite,
PGconn * conn
)

pqWaitTimed()

pqWriteReady()

int pqWriteReady ( PGconn * conn )