LCOV - code coverage report
Current view: top level - src/interfaces/libpq - fe-misc.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 268 386 69.4 %
Date: 2019-11-13 23:06:49 Functions: 29 33 87.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  *   FILE
       4             :  *      fe-misc.c
       5             :  *
       6             :  *   DESCRIPTION
       7             :  *       miscellaneous useful functions
       8             :  *
       9             :  * The communication routines here are analogous to the ones in
      10             :  * backend/libpq/pqcomm.c and backend/libpq/pqformat.c, but operate
      11             :  * in the considerably different environment of the frontend libpq.
      12             :  * In particular, we work with a bare nonblock-mode socket, rather than
      13             :  * a stdio stream, so that we can avoid unwanted blocking of the application.
      14             :  *
      15             :  * XXX: MOVE DEBUG PRINTOUT TO HIGHER LEVEL.  As is, block and restart
      16             :  * will cause repeat printouts.
      17             :  *
      18             :  * We must speak the same transmitted data representations as the backend
      19             :  * routines.
      20             :  *
      21             :  *
      22             :  * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
      23             :  * Portions Copyright (c) 1994, Regents of the University of California
      24             :  *
      25             :  * IDENTIFICATION
      26             :  *    src/interfaces/libpq/fe-misc.c
      27             :  *
      28             :  *-------------------------------------------------------------------------
      29             :  */
      30             : 
      31             : #include "postgres_fe.h"
      32             : 
      33             : #include <signal.h>
      34             : #include <time.h>
      35             : 
      36             : #ifdef WIN32
      37             : #include "win32.h"
      38             : #else
      39             : #include <unistd.h>
      40             : #include <sys/time.h>
      41             : #endif
      42             : 
      43             : #ifdef HAVE_POLL_H
      44             : #include <poll.h>
      45             : #endif
      46             : #ifdef HAVE_SYS_SELECT_H
      47             : #include <sys/select.h>
      48             : #endif
      49             : 
      50             : #include "libpq-fe.h"
      51             : #include "libpq-int.h"
      52             : #include "mb/pg_wchar.h"
      53             : #include "pg_config_paths.h"
      54             : #include "port/pg_bswap.h"
      55             : 
      56             : static int  pqPutMsgBytes(const void *buf, size_t len, PGconn *conn);
      57             : static int  pqSendSome(PGconn *conn, int len);
      58             : static int  pqSocketCheck(PGconn *conn, int forRead, int forWrite,
      59             :                           time_t end_time);
      60             : static int  pqSocketPoll(int sock, int forRead, int forWrite, time_t end_time);
      61             : 
      62             : /*
      63             :  * PQlibVersion: return the libpq version number
      64             :  */
      65             : int
      66           0 : PQlibVersion(void)
      67             : {
      68           0 :     return PG_VERSION_NUM;
      69             : }
      70             : 
      71             : /*
      72             :  * fputnbytes: print exactly N bytes to a file
      73             :  *
      74             :  * We avoid using %.*s here because it can misbehave if the data
      75             :  * is not valid in what libc thinks is the prevailing encoding.
      76             :  */
      77             : static void
      78           0 : fputnbytes(FILE *f, const char *str, size_t n)
      79             : {
      80           0 :     while (n-- > 0)
      81           0 :         fputc(*str++, f);
      82           0 : }
      83             : 
      84             : 
      85             : /*
      86             :  * pqGetc: get 1 character from the connection
      87             :  *
      88             :  *  All these routines return 0 on success, EOF on error.
      89             :  *  Note that for the Get routines, EOF only means there is not enough
      90             :  *  data in the buffer, not that there is necessarily a hard error.
      91             :  */
      92             : int
      93     7057272 : pqGetc(char *result, PGconn *conn)
      94             : {
      95     7057272 :     if (conn->inCursor >= conn->inEnd)
      96     1657968 :         return EOF;
      97             : 
      98     5399304 :     *result = conn->inBuffer[conn->inCursor++];
      99             : 
     100     5399304 :     if (conn->Pfdebug)
     101           0 :         fprintf(conn->Pfdebug, "From backend> %c\n", *result);
     102             : 
     103     5399312 :     return 0;
     104             : }
     105             : 
     106             : 
     107             : /*
     108             :  * pqPutc: write 1 char to the current message
     109             :  */
     110             : int
     111       26000 : pqPutc(char c, PGconn *conn)
     112             : {
     113       26000 :     if (pqPutMsgBytes(&c, 1, conn))
     114           0 :         return EOF;
     115             : 
     116       26002 :     if (conn->Pfdebug)
     117           0 :         fprintf(conn->Pfdebug, "To backend> %c\n", c);
     118             : 
     119       26002 :     return 0;
     120             : }
     121             : 
     122             : 
     123             : /*
     124             :  * pqGets[_append]:
     125             :  * get a null-terminated string from the connection,
     126             :  * and store it in an expansible PQExpBuffer.
     127             :  * If we run out of memory, all of the string is still read,
     128             :  * but the excess characters are silently discarded.
     129             :  */
     130             : static int
     131     1302488 : pqGets_internal(PQExpBuffer buf, PGconn *conn, bool resetbuffer)
     132             : {
     133             :     /* Copy conn data to locals for faster search loop */
     134     1302488 :     char       *inBuffer = conn->inBuffer;
     135     1302488 :     int         inCursor = conn->inCursor;
     136     1302488 :     int         inEnd = conn->inEnd;
     137             :     int         slen;
     138             : 
     139    17101738 :     while (inCursor < inEnd && inBuffer[inCursor])
     140    14496762 :         inCursor++;
     141             : 
     142     1302488 :     if (inCursor >= inEnd)
     143           0 :         return EOF;
     144             : 
     145     1302488 :     slen = inCursor - conn->inCursor;
     146             : 
     147     1302488 :     if (resetbuffer)
     148     1302488 :         resetPQExpBuffer(buf);
     149             : 
     150     1302488 :     appendBinaryPQExpBuffer(buf, inBuffer + conn->inCursor, slen);
     151             : 
     152     1302486 :     conn->inCursor = ++inCursor;
     153             : 
     154     1302486 :     if (conn->Pfdebug)
     155           0 :         fprintf(conn->Pfdebug, "From backend> \"%s\"\n",
     156             :                 buf->data);
     157             : 
     158     1302486 :     return 0;
     159             : }
     160             : 
     161             : int
     162     1302488 : pqGets(PQExpBuffer buf, PGconn *conn)
     163             : {
     164     1302488 :     return pqGets_internal(buf, conn, true);
     165             : }
     166             : 
     167             : int
     168           0 : pqGets_append(PQExpBuffer buf, PGconn *conn)
     169             : {
     170           0 :     return pqGets_internal(buf, conn, false);
     171             : }
     172             : 
     173             : 
     174             : /*
     175             :  * pqPuts: write a null-terminated string to the current message
     176             :  */
     177             : int
     178      397786 : pqPuts(const char *s, PGconn *conn)
     179             : {
     180      397786 :     if (pqPutMsgBytes(s, strlen(s) + 1, conn))
     181           0 :         return EOF;
     182             : 
     183      397788 :     if (conn->Pfdebug)
     184           0 :         fprintf(conn->Pfdebug, "To backend> \"%s\"\n", s);
     185             : 
     186      397788 :     return 0;
     187             : }
     188             : 
     189             : /*
     190             :  * pqGetnchar:
     191             :  *  get a string of exactly len bytes in buffer s, no null termination
     192             :  */
     193             : int
     194         492 : pqGetnchar(char *s, size_t len, PGconn *conn)
     195             : {
     196         492 :     if (len > (size_t) (conn->inEnd - conn->inCursor))
     197           0 :         return EOF;
     198             : 
     199         492 :     memcpy(s, conn->inBuffer + conn->inCursor, len);
     200             :     /* no terminating null */
     201             : 
     202         492 :     conn->inCursor += len;
     203             : 
     204         492 :     if (conn->Pfdebug)
     205             :     {
     206           0 :         fprintf(conn->Pfdebug, "From backend (%lu)> ", (unsigned long) len);
     207           0 :         fputnbytes(conn->Pfdebug, s, len);
     208           0 :         fprintf(conn->Pfdebug, "\n");
     209             :     }
     210             : 
     211         492 :     return 0;
     212             : }
     213             : 
     214             : /*
     215             :  * pqSkipnchar:
     216             :  *  skip over len bytes in input buffer.
     217             :  *
     218             :  * Note: this is primarily useful for its debug output, which should
     219             :  * be exactly the same as for pqGetnchar.  We assume the data in question
     220             :  * will actually be used, but just isn't getting copied anywhere as yet.
     221             :  */
     222             : int
     223     8625332 : pqSkipnchar(size_t len, PGconn *conn)
     224             : {
     225     8625332 :     if (len > (size_t) (conn->inEnd - conn->inCursor))
     226           0 :         return EOF;
     227             : 
     228     8625332 :     if (conn->Pfdebug)
     229             :     {
     230           0 :         fprintf(conn->Pfdebug, "From backend (%lu)> ", (unsigned long) len);
     231           0 :         fputnbytes(conn->Pfdebug, conn->inBuffer + conn->inCursor, len);
     232           0 :         fprintf(conn->Pfdebug, "\n");
     233             :     }
     234             : 
     235     8625332 :     conn->inCursor += len;
     236             : 
     237     8625332 :     return 0;
     238             : }
     239             : 
     240             : /*
     241             :  * pqPutnchar:
     242             :  *  write exactly len bytes to the current message
     243             :  */
     244             : int
     245      516288 : pqPutnchar(const char *s, size_t len, PGconn *conn)
     246             : {
     247      516288 :     if (pqPutMsgBytes(s, len, conn))
     248           0 :         return EOF;
     249             : 
     250      516294 :     if (conn->Pfdebug)
     251             :     {
     252           0 :         fprintf(conn->Pfdebug, "To backend> ");
     253           0 :         fputnbytes(conn->Pfdebug, s, len);
     254           0 :         fprintf(conn->Pfdebug, "\n");
     255             :     }
     256             : 
     257      516294 :     return 0;
     258             : }
     259             : 
     260             : /*
     261             :  * pqGetInt
     262             :  *  read a 2 or 4 byte integer and convert from network byte order
     263             :  *  to local byte order
     264             :  */
     265             : int
     266    19849720 : pqGetInt(int *result, size_t bytes, PGconn *conn)
     267             : {
     268             :     uint16      tmp2;
     269             :     uint32      tmp4;
     270             : 
     271    19849720 :     switch (bytes)
     272             :     {
     273             :         case 2:
     274     3531978 :             if (conn->inCursor + 2 > conn->inEnd)
     275           0 :                 return EOF;
     276     3531978 :             memcpy(&tmp2, conn->inBuffer + conn->inCursor, 2);
     277     3531978 :             conn->inCursor += 2;
     278     3531978 :             *result = (int) pg_ntoh16(tmp2);
     279     3531978 :             break;
     280             :         case 4:
     281    16317742 :             if (conn->inCursor + 4 > conn->inEnd)
     282        1164 :                 return EOF;
     283    16316578 :             memcpy(&tmp4, conn->inBuffer + conn->inCursor, 4);
     284    16316578 :             conn->inCursor += 4;
     285    16316578 :             *result = (int) pg_ntoh32(tmp4);
     286    16316578 :             break;
     287             :         default:
     288           0 :             pqInternalNotice(&conn->noticeHooks,
     289             :                              "integer of size %lu not supported by pqGetInt",
     290             :                              (unsigned long) bytes);
     291           0 :             return EOF;
     292             :     }
     293             : 
     294    19848556 :     if (conn->Pfdebug)
     295           0 :         fprintf(conn->Pfdebug, "From backend (#%lu)> %d\n", (unsigned long) bytes, *result);
     296             : 
     297    19848556 :     return 0;
     298             : }
     299             : 
     300             : /*
     301             :  * pqPutInt
     302             :  * write an integer of 2 or 4 bytes, converting from host byte order
     303             :  * to network byte order.
     304             :  */
     305             : int
     306      184288 : pqPutInt(int value, size_t bytes, PGconn *conn)
     307             : {
     308             :     uint16      tmp2;
     309             :     uint32      tmp4;
     310             : 
     311      184288 :     switch (bytes)
     312             :     {
     313             :         case 2:
     314      123564 :             tmp2 = pg_hton16((uint16) value);
     315      123564 :             if (pqPutMsgBytes((const char *) &tmp2, 2, conn))
     316           4 :                 return EOF;
     317      123572 :             break;
     318             :         case 4:
     319       60724 :             tmp4 = pg_hton32((uint32) value);
     320       60724 :             if (pqPutMsgBytes((const char *) &tmp4, 4, conn))
     321           0 :                 return EOF;
     322       60724 :             break;
     323             :         default:
     324           0 :             pqInternalNotice(&conn->noticeHooks,
     325             :                              "integer of size %lu not supported by pqPutInt",
     326             :                              (unsigned long) bytes);
     327           0 :             return EOF;
     328             :     }
     329             : 
     330      184296 :     if (conn->Pfdebug)
     331           0 :         fprintf(conn->Pfdebug, "To backend (%lu#)> %d\n", (unsigned long) bytes, value);
     332             : 
     333      184292 :     return 0;
     334             : }
     335             : 
     336             : /*
     337             :  * Make sure conn's output buffer can hold bytes_needed bytes (caller must
     338             :  * include already-stored data into the value!)
     339             :  *
     340             :  * Returns 0 on success, EOF if failed to enlarge buffer
     341             :  */
     342             : int
     343     2013888 : pqCheckOutBufferSpace(size_t bytes_needed, PGconn *conn)
     344             : {
     345     2013888 :     int         newsize = conn->outBufSize;
     346             :     char       *newbuf;
     347             : 
     348             :     /* Quick exit if we have enough space */
     349     2013888 :     if (bytes_needed <= (size_t) newsize)
     350     2013882 :         return 0;
     351             : 
     352             :     /*
     353             :      * If we need to enlarge the buffer, we first try to double it in size; if
     354             :      * that doesn't work, enlarge in multiples of 8K.  This avoids thrashing
     355             :      * the malloc pool by repeated small enlargements.
     356             :      *
     357             :      * Note: tests for newsize > 0 are to catch integer overflow.
     358             :      */
     359             :     do
     360             :     {
     361           6 :         newsize *= 2;
     362           6 :     } while (newsize > 0 && bytes_needed > (size_t) newsize);
     363             : 
     364           6 :     if (newsize > 0 && bytes_needed <= (size_t) newsize)
     365             :     {
     366           6 :         newbuf = realloc(conn->outBuffer, newsize);
     367           6 :         if (newbuf)
     368             :         {
     369             :             /* realloc succeeded */
     370           6 :             conn->outBuffer = newbuf;
     371           6 :             conn->outBufSize = newsize;
     372           6 :             return 0;
     373             :         }
     374             :     }
     375             : 
     376           0 :     newsize = conn->outBufSize;
     377             :     do
     378             :     {
     379           0 :         newsize += 8192;
     380           0 :     } while (newsize > 0 && bytes_needed > (size_t) newsize);
     381             : 
     382           0 :     if (newsize > 0 && bytes_needed <= (size_t) newsize)
     383             :     {
     384           0 :         newbuf = realloc(conn->outBuffer, newsize);
     385           0 :         if (newbuf)
     386             :         {
     387             :             /* realloc succeeded */
     388           0 :             conn->outBuffer = newbuf;
     389           0 :             conn->outBufSize = newsize;
     390           0 :             return 0;
     391             :         }
     392             :     }
     393             : 
     394             :     /* realloc failed. Probably out of memory */
     395           0 :     printfPQExpBuffer(&conn->errorMessage,
     396             :                       "cannot allocate memory for output buffer\n");
     397           0 :     return EOF;
     398             : }
     399             : 
     400             : /*
     401             :  * Make sure conn's input buffer can hold bytes_needed bytes (caller must
     402             :  * include already-stored data into the value!)
     403             :  *
     404             :  * Returns 0 on success, EOF if failed to enlarge buffer
     405             :  */
     406             : int
     407       93396 : pqCheckInBufferSpace(size_t bytes_needed, PGconn *conn)
     408             : {
     409       93396 :     int         newsize = conn->inBufSize;
     410             :     char       *newbuf;
     411             : 
     412             :     /* Quick exit if we have enough space */
     413       93396 :     if (bytes_needed <= (size_t) newsize)
     414       36722 :         return 0;
     415             : 
     416             :     /*
     417             :      * Before concluding that we need to enlarge the buffer, left-justify
     418             :      * whatever is in it and recheck.  The caller's value of bytes_needed
     419             :      * includes any data to the left of inStart, but we can delete that in
     420             :      * preference to enlarging the buffer.  It's slightly ugly to have this
     421             :      * function do this, but it's better than making callers worry about it.
     422             :      */
     423       56674 :     bytes_needed -= conn->inStart;
     424             : 
     425       56674 :     if (conn->inStart < conn->inEnd)
     426             :     {
     427       56674 :         if (conn->inStart > 0)
     428             :         {
     429       56522 :             memmove(conn->inBuffer, conn->inBuffer + conn->inStart,
     430       56522 :                     conn->inEnd - conn->inStart);
     431       56522 :             conn->inEnd -= conn->inStart;
     432       56522 :             conn->inCursor -= conn->inStart;
     433       56522 :             conn->inStart = 0;
     434             :         }
     435             :     }
     436             :     else
     437             :     {
     438             :         /* buffer is logically empty, reset it */
     439           0 :         conn->inStart = conn->inCursor = conn->inEnd = 0;
     440             :     }
     441             : 
     442             :     /* Recheck whether we have enough space */
     443       56674 :     if (bytes_needed <= (size_t) newsize)
     444       56226 :         return 0;
     445             : 
     446             :     /*
     447             :      * If we need to enlarge the buffer, we first try to double it in size; if
     448             :      * that doesn't work, enlarge in multiples of 8K.  This avoids thrashing
     449             :      * the malloc pool by repeated small enlargements.
     450             :      *
     451             :      * Note: tests for newsize > 0 are to catch integer overflow.
     452             :      */
     453             :     do
     454             :     {
     455         836 :         newsize *= 2;
     456         836 :     } while (newsize > 0 && bytes_needed > (size_t) newsize);
     457             : 
     458         448 :     if (newsize > 0 && bytes_needed <= (size_t) newsize)
     459             :     {
     460         448 :         newbuf = realloc(conn->inBuffer, newsize);
     461         448 :         if (newbuf)
     462             :         {
     463             :             /* realloc succeeded */
     464         448 :             conn->inBuffer = newbuf;
     465         448 :             conn->inBufSize = newsize;
     466         448 :             return 0;
     467             :         }
     468             :     }
     469             : 
     470           0 :     newsize = conn->inBufSize;
     471             :     do
     472             :     {
     473           0 :         newsize += 8192;
     474           0 :     } while (newsize > 0 && bytes_needed > (size_t) newsize);
     475             : 
     476           0 :     if (newsize > 0 && bytes_needed <= (size_t) newsize)
     477             :     {
     478           0 :         newbuf = realloc(conn->inBuffer, newsize);
     479           0 :         if (newbuf)
     480             :         {
     481             :             /* realloc succeeded */
     482           0 :             conn->inBuffer = newbuf;
     483           0 :             conn->inBufSize = newsize;
     484           0 :             return 0;
     485             :         }
     486             :     }
     487             : 
     488             :     /* realloc failed. Probably out of memory */
     489           0 :     printfPQExpBuffer(&conn->errorMessage,
     490             :                       "cannot allocate memory for input buffer\n");
     491           0 :     return EOF;
     492             : }
     493             : 
     494             : /*
     495             :  * pqPutMsgStart: begin construction of a message to the server
     496             :  *
     497             :  * msg_type is the message type byte, or 0 for a message without type byte
     498             :  * (only startup messages have no type byte)
     499             :  *
     500             :  * force_len forces the message to have a length word; otherwise, we add
     501             :  * a length word if protocol 3.
     502             :  *
     503             :  * Returns 0 on success, EOF on error
     504             :  *
     505             :  * The idea here is that we construct the message in conn->outBuffer,
     506             :  * beginning just past any data already in outBuffer (ie, at
     507             :  * outBuffer+outCount).  We enlarge the buffer as needed to hold the message.
     508             :  * When the message is complete, we fill in the length word (if needed) and
     509             :  * then advance outCount past the message, making it eligible to send.
     510             :  *
     511             :  * The state variable conn->outMsgStart points to the incomplete message's
     512             :  * length word: it is either outCount or outCount+1 depending on whether
     513             :  * there is a type byte.  If we are sending a message without length word
     514             :  * (pre protocol 3.0 only), then outMsgStart is -1.  The state variable
     515             :  * conn->outMsgEnd is the end of the data collected so far.
     516             :  */
     517             : int
     518      889522 : pqPutMsgStart(char msg_type, bool force_len, PGconn *conn)
     519             : {
     520             :     int         lenPos;
     521             :     int         endPos;
     522             : 
     523             :     /* allow room for message type byte */
     524      889522 :     if (msg_type)
     525      882000 :         endPos = conn->outCount + 1;
     526             :     else
     527        7522 :         endPos = conn->outCount;
     528             : 
     529             :     /* do we want a length word? */
     530      889522 :     if (force_len || PG_PROTOCOL_MAJOR(conn->pversion) >= 3)
     531             :     {
     532      889522 :         lenPos = endPos;
     533             :         /* allow room for message length */
     534      889522 :         endPos += 4;
     535             :     }
     536             :     else
     537           0 :         lenPos = -1;
     538             : 
     539             :     /* make sure there is room for message header */
     540      889522 :     if (pqCheckOutBufferSpace(endPos, conn))
     541           0 :         return EOF;
     542             :     /* okay, save the message type byte if any */
     543      889518 :     if (msg_type)
     544      882004 :         conn->outBuffer[conn->outCount] = msg_type;
     545             :     /* set up the message pointers */
     546      889518 :     conn->outMsgStart = lenPos;
     547      889518 :     conn->outMsgEnd = endPos;
     548             :     /* length word, if needed, will be filled in by pqPutMsgEnd */
     549             : 
     550      889518 :     if (conn->Pfdebug)
     551           0 :         fprintf(conn->Pfdebug, "To backend> Msg %c\n",
     552             :                 msg_type ? msg_type : ' ');
     553             : 
     554      889526 :     return 0;
     555             : }
     556             : 
     557             : /*
     558             :  * pqPutMsgBytes: add bytes to a partially-constructed message
     559             :  *
     560             :  * Returns 0 on success, EOF on error
     561             :  */
     562             : static int
     563     1124364 : pqPutMsgBytes(const void *buf, size_t len, PGconn *conn)
     564             : {
     565             :     /* make sure there is room for it */
     566     1124364 :     if (pqCheckOutBufferSpace(conn->outMsgEnd + len, conn))
     567           0 :         return EOF;
     568             :     /* okay, save the data */
     569     1124366 :     memcpy(conn->outBuffer + conn->outMsgEnd, buf, len);
     570     1124366 :     conn->outMsgEnd += len;
     571             :     /* no Pfdebug call here, caller should do it */
     572     1124366 :     return 0;
     573             : }
     574             : 
     575             : /*
     576             :  * pqPutMsgEnd: finish constructing a message and possibly send it
     577             :  *
     578             :  * Returns 0 on success, EOF on error
     579             :  *
     580             :  * We don't actually send anything here unless we've accumulated at least
     581             :  * 8K worth of data (the typical size of a pipe buffer on Unix systems).
     582             :  * This avoids sending small partial packets.  The caller must use pqFlush
     583             :  * when it's important to flush all the data out to the server.
     584             :  */
     585             : int
     586      889524 : pqPutMsgEnd(PGconn *conn)
     587             : {
     588      889524 :     if (conn->Pfdebug)
     589           0 :         fprintf(conn->Pfdebug, "To backend> Msg complete, length %u\n",
     590           0 :                 conn->outMsgEnd - conn->outCount);
     591             : 
     592             :     /* Fill in length word if needed */
     593      889528 :     if (conn->outMsgStart >= 0)
     594             :     {
     595      889528 :         uint32      msgLen = conn->outMsgEnd - conn->outMsgStart;
     596             : 
     597      889528 :         msgLen = pg_hton32(msgLen);
     598      889528 :         memcpy(conn->outBuffer + conn->outMsgStart, &msgLen, 4);
     599             :     }
     600             : 
     601             :     /* Make message eligible to send */
     602      889528 :     conn->outCount = conn->outMsgEnd;
     603             : 
     604      889528 :     if (conn->outCount >= 8192)
     605             :     {
     606        1614 :         int         toSend = conn->outCount - (conn->outCount % 8192);
     607             : 
     608        1614 :         if (pqSendSome(conn, toSend) < 0)
     609           0 :             return EOF;
     610             :         /* in nonblock mode, don't complain if unable to send it all */
     611             :     }
     612             : 
     613      889528 :     return 0;
     614             : }
     615             : 
     616             : /* ----------
     617             :  * pqReadData: read more data, if any is available
     618             :  * Possible return values:
     619             :  *   1: successfully loaded at least one more byte
     620             :  *   0: no data is presently available, but no error detected
     621             :  *  -1: error detected (including EOF = connection closure);
     622             :  *      conn->errorMessage set
     623             :  * NOTE: callers must not assume that pointers or indexes into conn->inBuffer
     624             :  * remain valid across this call!
     625             :  * ----------
     626             :  */
     627             : int
     628      634380 : pqReadData(PGconn *conn)
     629             : {
     630      634380 :     int         someread = 0;
     631             :     int         nread;
     632             : 
     633      634380 :     if (conn->sock == PGINVALID_SOCKET)
     634             :     {
     635           0 :         printfPQExpBuffer(&conn->errorMessage,
     636           0 :                           libpq_gettext("connection not open\n"));
     637           0 :         return -1;
     638             :     }
     639             : 
     640             :     /* Left-justify any data in the buffer to make room */
     641      634380 :     if (conn->inStart < conn->inEnd)
     642             :     {
     643       91238 :         if (conn->inStart > 0)
     644             :         {
     645       22558 :             memmove(conn->inBuffer, conn->inBuffer + conn->inStart,
     646       22558 :                     conn->inEnd - conn->inStart);
     647       22558 :             conn->inEnd -= conn->inStart;
     648       22558 :             conn->inCursor -= conn->inStart;
     649       22558 :             conn->inStart = 0;
     650             :         }
     651             :     }
     652             :     else
     653             :     {
     654             :         /* buffer is logically empty, reset it */
     655      543142 :         conn->inStart = conn->inCursor = conn->inEnd = 0;
     656             :     }
     657             : 
     658             :     /*
     659             :      * If the buffer is fairly full, enlarge it. We need to be able to enlarge
     660             :      * the buffer in case a single message exceeds the initial buffer size. We
     661             :      * enlarge before filling the buffer entirely so as to avoid asking the
     662             :      * kernel for a partial packet. The magic constant here should be large
     663             :      * enough for a TCP packet or Unix pipe bufferload.  8K is the usual pipe
     664             :      * buffer size, so...
     665             :      */
     666      634380 :     if (conn->inBufSize - conn->inEnd < 8192)
     667             :     {
     668           0 :         if (pqCheckInBufferSpace(conn->inEnd + (size_t) 8192, conn))
     669             :         {
     670             :             /*
     671             :              * We don't insist that the enlarge worked, but we need some room
     672             :              */
     673           0 :             if (conn->inBufSize - conn->inEnd < 100)
     674           0 :                 return -1;      /* errorMessage already set */
     675             :         }
     676             :     }
     677             : 
     678             :     /* OK, try to read some data */
     679             : retry3:
     680      645152 :     nread = pqsecure_read(conn, conn->inBuffer + conn->inEnd,
     681      645152 :                           conn->inBufSize - conn->inEnd);
     682      645154 :     if (nread < 0)
     683             :     {
     684      199570 :         if (SOCK_ERRNO == EINTR)
     685           0 :             goto retry3;
     686             :         /* Some systems return EAGAIN/EWOULDBLOCK for no data */
     687             : #ifdef EAGAIN
     688      199570 :         if (SOCK_ERRNO == EAGAIN)
     689      199566 :             return someread;
     690             : #endif
     691             : #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
     692             :         if (SOCK_ERRNO == EWOULDBLOCK)
     693             :             return someread;
     694             : #endif
     695             :         /* We might get ECONNRESET here if using TCP and backend died */
     696             : #ifdef ECONNRESET
     697           4 :         if (SOCK_ERRNO == ECONNRESET)
     698           4 :             goto definitelyFailed;
     699             : #endif
     700             :         /* pqsecure_read set the error message for us */
     701           0 :         return -1;
     702             :     }
     703      445584 :     if (nread > 0)
     704             :     {
     705      445446 :         conn->inEnd += nread;
     706             : 
     707             :         /*
     708             :          * Hack to deal with the fact that some kernels will only give us back
     709             :          * 1 packet per recv() call, even if we asked for more and there is
     710             :          * more available.  If it looks like we are reading a long message,
     711             :          * loop back to recv() again immediately, until we run out of data or
     712             :          * buffer space.  Without this, the block-and-restart behavior of
     713             :          * libpq's higher levels leads to O(N^2) performance on long messages.
     714             :          *
     715             :          * Since we left-justified the data above, conn->inEnd gives the
     716             :          * amount of data already read in the current message.  We consider
     717             :          * the message "long" once we have acquired 32k ...
     718             :          */
     719      504008 :         if (conn->inEnd > 32768 &&
     720       58562 :             (conn->inBufSize - conn->inEnd) >= 8192)
     721             :         {
     722       10772 :             someread = 1;
     723       10772 :             goto retry3;
     724             :         }
     725      434674 :         return 1;
     726             :     }
     727             : 
     728         138 :     if (someread)
     729           0 :         return 1;               /* got a zero read after successful tries */
     730             : 
     731             :     /*
     732             :      * A return value of 0 could mean just that no data is now available, or
     733             :      * it could mean EOF --- that is, the server has closed the connection.
     734             :      * Since we have the socket in nonblock mode, the only way to tell the
     735             :      * difference is to see if select() is saying that the file is ready.
     736             :      * Grumble.  Fortunately, we don't expect this path to be taken much,
     737             :      * since in normal practice we should not be trying to read data unless
     738             :      * the file selected for reading already.
     739             :      *
     740             :      * In SSL mode it's even worse: SSL_read() could say WANT_READ and then
     741             :      * data could arrive before we make the pqReadReady() test, but the second
     742             :      * SSL_read() could still say WANT_READ because the data received was not
     743             :      * a complete SSL record.  So we must play dumb and assume there is more
     744             :      * data, relying on the SSL layer to detect true EOF.
     745             :      */
     746             : 
     747             : #ifdef USE_SSL
     748         138 :     if (conn->ssl_in_use)
     749          54 :         return 0;
     750             : #endif
     751             : 
     752          84 :     switch (pqReadReady(conn))
     753             :     {
     754             :         case 0:
     755             :             /* definitely no data available */
     756           0 :             return 0;
     757             :         case 1:
     758             :             /* ready for read */
     759          84 :             break;
     760             :         default:
     761             :             /* we override pqReadReady's message with something more useful */
     762           0 :             goto definitelyEOF;
     763             :     }
     764             : 
     765             :     /*
     766             :      * Still not sure that it's EOF, because some data could have just
     767             :      * arrived.
     768             :      */
     769             : retry4:
     770          84 :     nread = pqsecure_read(conn, conn->inBuffer + conn->inEnd,
     771          84 :                           conn->inBufSize - conn->inEnd);
     772          84 :     if (nread < 0)
     773             :     {
     774           0 :         if (SOCK_ERRNO == EINTR)
     775           0 :             goto retry4;
     776             :         /* Some systems return EAGAIN/EWOULDBLOCK for no data */
     777             : #ifdef EAGAIN
     778           0 :         if (SOCK_ERRNO == EAGAIN)
     779           0 :             return 0;
     780             : #endif
     781             : #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
     782             :         if (SOCK_ERRNO == EWOULDBLOCK)
     783             :             return 0;
     784             : #endif
     785             :         /* We might get ECONNRESET here if using TCP and backend died */
     786             : #ifdef ECONNRESET
     787           0 :         if (SOCK_ERRNO == ECONNRESET)
     788           0 :             goto definitelyFailed;
     789             : #endif
     790             :         /* pqsecure_read set the error message for us */
     791           0 :         return -1;
     792             :     }
     793          84 :     if (nread > 0)
     794             :     {
     795           0 :         conn->inEnd += nread;
     796           0 :         return 1;
     797             :     }
     798             : 
     799             :     /*
     800             :      * OK, we are getting a zero read even though select() says ready. This
     801             :      * means the connection has been closed.  Cope.
     802             :      */
     803             : definitelyEOF:
     804          84 :     printfPQExpBuffer(&conn->errorMessage,
     805          84 :                       libpq_gettext(
     806             :                                     "server closed the connection unexpectedly\n"
     807             :                                     "\tThis probably means the server terminated abnormally\n"
     808             :                                     "\tbefore or while processing the request.\n"));
     809             : 
     810             :     /* Come here if lower-level code already set a suitable errorMessage */
     811             : definitelyFailed:
     812             :     /* Do *not* drop any already-read data; caller still wants it */
     813          88 :     pqDropConnection(conn, false);
     814          88 :     conn->status = CONNECTION_BAD;   /* No more connection to backend */
     815          88 :     return -1;
     816             : }
     817             : 
     818             : /*
     819             :  * pqSendSome: send data waiting in the output buffer.
     820             :  *
     821             :  * len is how much to try to send (typically equal to outCount, but may
     822             :  * be less).
     823             :  *
     824             :  * Return 0 on success, -1 on failure and 1 when not all data could be sent
     825             :  * because the socket would block and the connection is non-blocking.
     826             :  *
     827             :  * Upon write failure, conn->write_failed is set and the error message is
     828             :  * saved in conn->write_err_msg, but we clear the output buffer and return
     829             :  * zero anyway; this is because callers should soldier on until it's possible
     830             :  * to read from the server and check for an error message.  write_err_msg
     831             :  * should be reported only when we are unable to obtain a server error first.
     832             :  * (Thus, a -1 result is returned only for an internal *read* failure.)
     833             :  */
     834             : static int
     835      331776 : pqSendSome(PGconn *conn, int len)
     836             : {
     837      331776 :     char       *ptr = conn->outBuffer;
     838      331776 :     int         remaining = conn->outCount;
     839      331776 :     int         result = 0;
     840             : 
     841             :     /*
     842             :      * If we already had a write failure, we will never again try to send data
     843             :      * on that connection.  Even if the kernel would let us, we've probably
     844             :      * lost message boundary sync with the server.  conn->write_failed
     845             :      * therefore persists until the connection is reset, and we just discard
     846             :      * all data presented to be written.
     847             :      */
     848      331776 :     if (conn->write_failed)
     849             :     {
     850             :         /* conn->write_err_msg should be set up already */
     851           0 :         conn->outCount = 0;
     852           0 :         return 0;
     853             :     }
     854             : 
     855      331776 :     if (conn->sock == PGINVALID_SOCKET)
     856             :     {
     857           0 :         printfPQExpBuffer(&conn->errorMessage,
     858           0 :                           libpq_gettext("connection not open\n"));
     859           0 :         conn->write_failed = true;
     860             :         /* Transfer error message to conn->write_err_msg, if possible */
     861             :         /* (strdup failure is OK, we'll cope later) */
     862           0 :         conn->write_err_msg = strdup(conn->errorMessage.data);
     863           0 :         resetPQExpBuffer(&conn->errorMessage);
     864             :         /* Discard queued data; no chance it'll ever be sent */
     865           0 :         conn->outCount = 0;
     866           0 :         return 0;
     867             :     }
     868             : 
     869             :     /* while there's still data to send */
     870      995368 :     while (len > 0)
     871             :     {
     872             :         int         sent;
     873             : 
     874             : #ifndef WIN32
     875      331812 :         sent = pqsecure_write(conn, ptr, len);
     876             : #else
     877             : 
     878             :         /*
     879             :          * Windows can fail on large sends, per KB article Q201213. The
     880             :          * failure-point appears to be different in different versions of
     881             :          * Windows, but 64k should always be safe.
     882             :          */
     883             :         sent = pqsecure_write(conn, ptr, Min(len, 65536));
     884             : #endif
     885             : 
     886      331820 :         if (sent < 0)
     887             :         {
     888             :             /* Anything except EAGAIN/EWOULDBLOCK/EINTR is trouble */
     889          40 :             switch (SOCK_ERRNO)
     890             :             {
     891             : #ifdef EAGAIN
     892             :                 case EAGAIN:
     893          36 :                     break;
     894             : #endif
     895             : #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
     896             :                 case EWOULDBLOCK:
     897             :                     break;
     898             : #endif
     899             :                 case EINTR:
     900           0 :                     continue;
     901             : 
     902             :                 default:
     903             :                     /* pqsecure_write set the error message for us */
     904           4 :                     conn->write_failed = true;
     905             : 
     906             :                     /*
     907             :                      * Transfer error message to conn->write_err_msg, if
     908             :                      * possible (strdup failure is OK, we'll cope later).
     909             :                      *
     910             :                      * Note: this assumes that pqsecure_write and its children
     911             :                      * will overwrite not append to conn->errorMessage.  If
     912             :                      * that's ever changed, we could remember the length of
     913             :                      * conn->errorMessage at entry to this routine, and then
     914             :                      * save and delete just what was appended.
     915             :                      */
     916           4 :                     conn->write_err_msg = strdup(conn->errorMessage.data);
     917           4 :                     resetPQExpBuffer(&conn->errorMessage);
     918             : 
     919             :                     /* Discard queued data; no chance it'll ever be sent */
     920           4 :                     conn->outCount = 0;
     921           4 :                     return 0;
     922             :             }
     923             :         }
     924             :         else
     925             :         {
     926      331780 :             ptr += sent;
     927      331780 :             len -= sent;
     928      331780 :             remaining -= sent;
     929             :         }
     930             : 
     931      331816 :         if (len > 0)
     932             :         {
     933             :             /*
     934             :              * We didn't send it all, wait till we can send more.
     935             :              *
     936             :              * There are scenarios in which we can't send data because the
     937             :              * communications channel is full, but we cannot expect the server
     938             :              * to clear the channel eventually because it's blocked trying to
     939             :              * send data to us.  (This can happen when we are sending a large
     940             :              * amount of COPY data, and the server has generated lots of
     941             :              * NOTICE responses.)  To avoid a deadlock situation, we must be
     942             :              * prepared to accept and buffer incoming data before we try
     943             :              * again.  Furthermore, it is possible that such incoming data
     944             :              * might not arrive until after we've gone to sleep.  Therefore,
     945             :              * we wait for either read ready or write ready.
     946             :              *
     947             :              * In non-blocking mode, we don't wait here directly, but return 1
     948             :              * to indicate that data is still pending.  The caller should wait
     949             :              * for both read and write ready conditions, and call
     950             :              * PQconsumeInput() on read ready, but just in case it doesn't, we
     951             :              * call pqReadData() ourselves before returning.  That's not
     952             :              * enough if the data has not arrived yet, but it's the best we
     953             :              * can do, and works pretty well in practice.  (The documentation
     954             :              * used to say that you only need to wait for write-ready, so
     955             :              * there are still plenty of applications like that out there.)
     956             :              *
     957             :              * Note that errors here don't result in write_failed becoming
     958             :              * set.
     959             :              */
     960          36 :             if (pqReadData(conn) < 0)
     961             :             {
     962           0 :                 result = -1;    /* error message already set up */
     963           0 :                 break;
     964             :             }
     965             : 
     966          36 :             if (pqIsnonblocking(conn))
     967             :             {
     968           0 :                 result = 1;
     969           0 :                 break;
     970             :             }
     971             : 
     972          36 :             if (pqWait(true, true, conn))
     973             :             {
     974           0 :                 result = -1;
     975           0 :                 break;
     976             :             }
     977             :         }
     978             :     }
     979             : 
     980             :     /* shift the remaining contents of the buffer */
     981      331780 :     if (remaining > 0)
     982        1606 :         memmove(conn->outBuffer, ptr, remaining);
     983      331780 :     conn->outCount = remaining;
     984             : 
     985      331780 :     return result;
     986             : }
     987             : 
     988             : 
     989             : /*
     990             :  * pqFlush: send any data waiting in the output buffer
     991             :  *
     992             :  * Return 0 on success, -1 on failure and 1 when not all data could be sent
     993             :  * because the socket would block and the connection is non-blocking.
     994             :  * (See pqSendSome comments about how failure should be handled.)
     995             :  */
     996             : int
     997      642286 : pqFlush(PGconn *conn)
     998             : {
     999      642286 :     if (conn->Pfdebug)
    1000           0 :         fflush(conn->Pfdebug);
    1001             : 
    1002      642296 :     if (conn->outCount > 0)
    1003      330168 :         return pqSendSome(conn, conn->outCount);
    1004             : 
    1005      312128 :     return 0;
    1006             : }
    1007             : 
    1008             : 
    1009             : /*
    1010             :  * pqWait: wait until we can read or write the connection socket
    1011             :  *
    1012             :  * JAB: If SSL enabled and used and forRead, buffered bytes short-circuit the
    1013             :  * call to select().
    1014             :  *
    1015             :  * We also stop waiting and return if the kernel flags an exception condition
    1016             :  * on the socket.  The actual error condition will be detected and reported
    1017             :  * when the caller tries to read or write the socket.
    1018             :  */
    1019             : int
    1020      382990 : pqWait(int forRead, int forWrite, PGconn *conn)
    1021             : {
    1022      382990 :     return pqWaitTimed(forRead, forWrite, conn, (time_t) -1);
    1023             : }
    1024             : 
    1025             : /*
    1026             :  * pqWaitTimed: wait, but not past finish_time.
    1027             :  *
    1028             :  * finish_time = ((time_t) -1) disables the wait limit.
    1029             :  *
    1030             :  * Returns -1 on failure, 0 if the socket is readable/writable, 1 if it timed out.
    1031             :  */
    1032             : int
    1033      398162 : pqWaitTimed(int forRead, int forWrite, PGconn *conn, time_t finish_time)
    1034             : {
    1035             :     int         result;
    1036             : 
    1037      398162 :     result = pqSocketCheck(conn, forRead, forWrite, finish_time);
    1038             : 
    1039      398160 :     if (result < 0)
    1040           0 :         return -1;              /* errorMessage is already set */
    1041             : 
    1042      398160 :     if (result == 0)
    1043             :     {
    1044           0 :         printfPQExpBuffer(&conn->errorMessage,
    1045           0 :                           libpq_gettext("timeout expired\n"));
    1046           0 :         return 1;
    1047             :     }
    1048             : 
    1049      398160 :     return 0;
    1050             : }
    1051             : 
    1052             : /*
    1053             :  * pqReadReady: is select() saying the file is ready to read?
    1054             :  * Returns -1 on failure, 0 if not ready, 1 if ready.
    1055             :  */
    1056             : int
    1057          84 : pqReadReady(PGconn *conn)
    1058             : {
    1059          84 :     return pqSocketCheck(conn, 1, 0, (time_t) 0);
    1060             : }
    1061             : 
    1062             : /*
    1063             :  * pqWriteReady: is select() saying the file is ready to write?
    1064             :  * Returns -1 on failure, 0 if not ready, 1 if ready.
    1065             :  */
    1066             : int
    1067           0 : pqWriteReady(PGconn *conn)
    1068             : {
    1069           0 :     return pqSocketCheck(conn, 0, 1, (time_t) 0);
    1070             : }
    1071             : 
    1072             : /*
    1073             :  * Checks a socket, using poll or select, for data to be read, written,
    1074             :  * or both.  Returns >0 if one or more conditions are met, 0 if it timed
    1075             :  * out, -1 if an error occurred.
    1076             :  *
    1077             :  * If SSL is in use, the SSL buffer is checked prior to checking the socket
    1078             :  * for read data directly.
    1079             :  */
    1080             : static int
    1081      398246 : pqSocketCheck(PGconn *conn, int forRead, int forWrite, time_t end_time)
    1082             : {
    1083             :     int         result;
    1084             : 
    1085      398246 :     if (!conn)
    1086           0 :         return -1;
    1087      398246 :     if (conn->sock == PGINVALID_SOCKET)
    1088             :     {
    1089           0 :         printfPQExpBuffer(&conn->errorMessage,
    1090           0 :                           libpq_gettext("invalid socket\n"));
    1091           0 :         return -1;
    1092             :     }
    1093             : 
    1094             : #ifdef USE_SSL
    1095             :     /* Check for SSL library buffering read bytes */
    1096      398246 :     if (forRead && conn->ssl_in_use && pgtls_read_pending(conn))
    1097             :     {
    1098             :         /* short-circuit the select */
    1099           0 :         return 1;
    1100             :     }
    1101             : #endif
    1102             : 
    1103             :     /* We will retry as long as we get EINTR */
    1104             :     do
    1105      398246 :         result = pqSocketPoll(conn->sock, forRead, forWrite, end_time);
    1106      398244 :     while (result < 0 && SOCK_ERRNO == EINTR);
    1107             : 
    1108      398244 :     if (result < 0)
    1109             :     {
    1110             :         char        sebuf[PG_STRERROR_R_BUFLEN];
    1111             : 
    1112           0 :         printfPQExpBuffer(&conn->errorMessage,
    1113           0 :                           libpq_gettext("select() failed: %s\n"),
    1114           0 :                           SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
    1115             :     }
    1116             : 
    1117      398244 :     return result;
    1118             : }
    1119             : 
    1120             : 
    1121             : /*
    1122             :  * Check a file descriptor for read and/or write data, possibly waiting.
    1123             :  * If neither forRead nor forWrite are set, immediately return a timeout
    1124             :  * condition (without waiting).  Return >0 if condition is met, 0
    1125             :  * if a timeout occurred, -1 if an error or interrupt occurred.
    1126             :  *
    1127             :  * Timeout is infinite if end_time is -1.  Timeout is immediate (no blocking)
    1128             :  * if end_time is 0 (or indeed, any time before now).
    1129             :  */
    1130             : static int
    1131      398244 : pqSocketPoll(int sock, int forRead, int forWrite, time_t end_time)
    1132             : {
    1133             :     /* We use poll(2) if available, otherwise select(2) */
    1134             : #ifdef HAVE_POLL
    1135             :     struct pollfd input_fd;
    1136             :     int         timeout_ms;
    1137             : 
    1138      398244 :     if (!forRead && !forWrite)
    1139           0 :         return 0;
    1140             : 
    1141      398244 :     input_fd.fd = sock;
    1142      398244 :     input_fd.events = POLLERR;
    1143      398244 :     input_fd.revents = 0;
    1144             : 
    1145      398244 :     if (forRead)
    1146      390608 :         input_fd.events |= POLLIN;
    1147      398244 :     if (forWrite)
    1148        7672 :         input_fd.events |= POLLOUT;
    1149             : 
    1150             :     /* Compute appropriate timeout interval */
    1151      398244 :     if (end_time == ((time_t) -1))
    1152      398132 :         timeout_ms = -1;
    1153             :     else
    1154             :     {
    1155         112 :         time_t      now = time(NULL);
    1156             : 
    1157         112 :         if (end_time > now)
    1158          28 :             timeout_ms = (end_time - now) * 1000;
    1159             :         else
    1160          84 :             timeout_ms = 0;
    1161             :     }
    1162             : 
    1163      398244 :     return poll(&input_fd, 1, timeout_ms);
    1164             : #else                           /* !HAVE_POLL */
    1165             : 
    1166             :     fd_set      input_mask;
    1167             :     fd_set      output_mask;
    1168             :     fd_set      except_mask;
    1169             :     struct timeval timeout;
    1170             :     struct timeval *ptr_timeout;
    1171             : 
    1172             :     if (!forRead && !forWrite)
    1173             :         return 0;
    1174             : 
    1175             :     FD_ZERO(&input_mask);
    1176             :     FD_ZERO(&output_mask);
    1177             :     FD_ZERO(&except_mask);
    1178             :     if (forRead)
    1179             :         FD_SET(sock, &input_mask);
    1180             : 
    1181             :     if (forWrite)
    1182             :         FD_SET(sock, &output_mask);
    1183             :     FD_SET(sock, &except_mask);
    1184             : 
    1185             :     /* Compute appropriate timeout interval */
    1186             :     if (end_time == ((time_t) -1))
    1187             :         ptr_timeout = NULL;
    1188             :     else
    1189             :     {
    1190             :         time_t      now = time(NULL);
    1191             : 
    1192             :         if (end_time > now)
    1193             :             timeout.tv_sec = end_time - now;
    1194             :         else
    1195             :             timeout.tv_sec = 0;
    1196             :         timeout.tv_usec = 0;
    1197             :         ptr_timeout = &timeout;
    1198             :     }
    1199             : 
    1200             :     return select(sock + 1, &input_mask, &output_mask,
    1201             :                   &except_mask, ptr_timeout);
    1202             : #endif                          /* HAVE_POLL */
    1203             : }
    1204             : 
    1205             : 
    1206             : /*
    1207             :  * A couple of "miscellaneous" multibyte related functions. They used
    1208             :  * to be in fe-print.c but that file is doomed.
    1209             :  */
    1210             : 
    1211             : /*
    1212             :  * returns the byte length of the character beginning at s, using the
    1213             :  * specified encoding.
    1214             :  */
    1215             : int
    1216    26730244 : PQmblen(const char *s, int encoding)
    1217             : {
    1218    26730244 :     return pg_encoding_mblen(encoding, s);
    1219             : }
    1220             : 
    1221             : /*
    1222             :  * returns the display length of the character beginning at s, using the
    1223             :  * specified encoding.
    1224             :  */
    1225             : int
    1226    26701358 : PQdsplen(const char *s, int encoding)
    1227             : {
    1228    26701358 :     return pg_encoding_dsplen(encoding, s);
    1229             : }
    1230             : 
    1231             : /*
    1232             :  * Get encoding id from environment variable PGCLIENTENCODING.
    1233             :  */
    1234             : int
    1235        4178 : PQenv2encoding(void)
    1236             : {
    1237             :     char       *str;
    1238        4178 :     int         encoding = PG_SQL_ASCII;
    1239             : 
    1240        4178 :     str = getenv("PGCLIENTENCODING");
    1241        4178 :     if (str && *str != '\0')
    1242             :     {
    1243          70 :         encoding = pg_char_to_encoding(str);
    1244          70 :         if (encoding < 0)
    1245           0 :             encoding = PG_SQL_ASCII;
    1246             :     }
    1247        4178 :     return encoding;
    1248             : }
    1249             : 
    1250             : 
    1251             : #ifdef ENABLE_NLS
    1252             : 
    1253             : static void
    1254       13142 : libpq_binddomain()
    1255             : {
    1256             :     static bool already_bound = false;
    1257             : 
    1258       13142 :     if (!already_bound)
    1259             :     {
    1260             :         /* bindtextdomain() does not preserve errno */
    1261             : #ifdef WIN32
    1262             :         int         save_errno = GetLastError();
    1263             : #else
    1264        1240 :         int         save_errno = errno;
    1265             : #endif
    1266             :         const char *ldir;
    1267             : 
    1268        1240 :         already_bound = true;
    1269             :         /* No relocatable lookup here because the binary could be anywhere */
    1270        1240 :         ldir = getenv("PGLOCALEDIR");
    1271        1240 :         if (!ldir)
    1272           4 :             ldir = LOCALEDIR;
    1273        1240 :         bindtextdomain(PG_TEXTDOMAIN("libpq"), ldir);
    1274             : #ifdef WIN32
    1275             :         SetLastError(save_errno);
    1276             : #else
    1277        1240 :         errno = save_errno;
    1278             : #endif
    1279             :     }
    1280       13142 : }
    1281             : 
    1282             : char *
    1283       13136 : libpq_gettext(const char *msgid)
    1284             : {
    1285       13136 :     libpq_binddomain();
    1286       13136 :     return dgettext(PG_TEXTDOMAIN("libpq"), msgid);
    1287             : }
    1288             : 
    1289             : char *
    1290           6 : libpq_ngettext(const char *msgid, const char *msgid_plural, unsigned long n)
    1291             : {
    1292           6 :     libpq_binddomain();
    1293           6 :     return dngettext(PG_TEXTDOMAIN("libpq"), msgid, msgid_plural, n);
    1294             : }
    1295             : 
    1296             : #endif                          /* ENABLE_NLS */

Generated by: LCOV version 1.13