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

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

32

33#include <signal.h>

35

36#ifdef WIN32

37#include "win32.h"

38#else

42#endif

43

44#ifdef HAVE_POLL_H

45#include <poll.h>

46#endif

47

51#include "pg_config_paths.h"

53

58

59

60

61

62int

64{

65 return PG_VERSION_NUM;

66}

67

68

69

70

71

72

73

74

75

76int

78{

80 return EOF;

81

83

84 return 0;

85}

86

87

88

89

90

91int

93{

95 return EOF;

96

97 return 0;

98}

99

100

101

102

103

104

105

106

107

108static int

110{

111

115 int slen;

116

117 while (inCursor < inEnd && inBuffer[inCursor])

118 inCursor++;

119

120 if (inCursor >= inEnd)

121 return EOF;

122

124

125 if (resetbuffer)

127

129

131

132 return 0;

133}

134

135int

137{

139}

140

141int

143{

145}

146

147

148

149

150

151int

153{

155 return EOF;

156

157 return 0;

158}

159

160

161

162

163

164int

166{

168 return EOF;

169

171

172

174

175 return 0;

176}

177

178

179

180

181

182

183

184

185

186int

188{

190 return EOF;

191

193

194 return 0;

195}

196

197

198

199

200

201int

203{

205 return EOF;

206

207 return 0;

208}

209

210

211

212

213

214

215int

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}

246

247

248

249

250

251

252int

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}

279

280

281

282

283

284

285

286int

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}

343

344

345

346

347

348

349

350int

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}

437

438

439

440

441

442void

444{

445

448

449

451}

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472int

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}

502

503

504

505

506

507

508static int

510{

511

513 return EOF;

514

517

518 return 0;

519}

520

521

522

523

524

525

526

527

528

529

530

531int

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}

567

568

569

570

571

572

573

574

575

576

577

578

579int

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}

776

777

778

779

780

781

782

783

784

785

786

787

788

789

790

791

792

793

794

795

796

797

798static int

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}

958

959

960

961

962

963

964

965

966

967int

969{

971 {

974

976 }

977

978 return 0;

979}

980

981

982

983

984

985

986

987

988

989

990

991

992int

994{

996}

997

998

999

1000

1001

1002

1003

1004

1005

1006

1007

1008int

1010{

1011 int result;

1012

1014

1015 if (result < 0)

1016 return -1;

1017

1018 if (result == 0)

1019 {

1021 return 1;

1022 }

1023

1024 return 0;

1025}

1026

1027

1028

1029

1030

1031int

1033{

1035}

1036

1037

1038

1039

1040

1041int

1043{

1045}

1046

1047

1048

1049

1050

1051

1052

1053

1054

1055

1056static int

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}

1101

1102

1103

1104

1105

1106

1107

1108

1109

1110

1111

1112

1113

1114int

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}

1201

1202

1203

1204

1205

1206

1207

1210{

1211 struct timeval tval;

1212

1214 return (pg_usec_time_t) tval.tv_sec * 1000000 + tval.tv_usec;

1215}

1216

1217

1218

1219

1220

1221

1222

1223

1224

1225

1226

1227

1228int

1230{

1232}

1233

1234

1235

1236

1237

1238

1239int

1241{

1243}

1244

1245

1246

1247

1248

1249int

1251{

1253}

1254

1255

1256

1257

1258int

1260{

1261 char *str;

1263

1264 str = getenv("PGCLIENTENCODING");

1265 if (str && *str != '\0')

1266 {

1270 }

1272}

1273

1274

1275#ifdef ENABLE_NLS

1276

1277static void

1278libpq_binddomain(void)

1279{

1280

1281

1282

1283

1284

1285

1286

1287 static volatile bool already_bound = false;

1289

1290 if (!already_bound)

1291 {

1292

1293#ifdef WIN32

1294 int save_errno = GetLastError();

1295#else

1296 int save_errno = errno;

1297#endif

1298

1300

1301 if (!already_bound)

1302 {

1303 const char *ldir;

1304

1305

1306

1307

1308

1309 ldir = getenv("PGLOCALEDIR");

1310 if (!ldir)

1311 ldir = LOCALEDIR;

1313 already_bound = true;

1314 }

1315

1317

1318#ifdef WIN32

1319 SetLastError(save_errno);

1320#else

1321 errno = save_errno;

1322#endif

1323 }

1324}

1325

1326char *

1328{

1329 libpq_binddomain();

1331}

1332

1333char *

1334libpq_ngettext(const char *msgid, const char *msgid_plural, unsigned long n)

1335{

1336 libpq_binddomain();

1338}

1339

1340#endif

1341

1342

1343

1344

1345

1346

1347

1348void

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}

1371

1372

1373

1374

1375

1376

1377void

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}

Datum now(PG_FUNCTION_ARGS)

#define PG_TEXTDOMAIN(domain)

#define dngettext(d, s, p, n)

void pqDropConnection(PGconn *conn, bool flushInput)

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

int pqPutc(char c, PGconn *conn)

int pqReadData(PGconn *conn)

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

int pqCheckOutBufferSpace(size_t bytes_needed, PGconn *conn)

int pqFlush(PGconn *conn)

void pqParseDone(PGconn *conn, int newInStart)

int pqReadReady(PGconn *conn)

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

int pqPutMsgStart(char msg_type, PGconn *conn)

int pqSkipnchar(size_t len, PGconn *conn)

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

int pqGetc(char *result, PGconn *conn)

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

int PQmblen(const char *s, int encoding)

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

int pqGets(PQExpBuffer buf, PGconn *conn)

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

int PQdsplen(const char *s, int encoding)

int pqCheckInBufferSpace(size_t bytes_needed, PGconn *conn)

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

static int pqSendSome(PGconn *conn, int len)

pg_usec_time_t PQgetCurrentTimeUSec(void)

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

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

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

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

int PQmblenBounded(const char *s, int encoding)

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

int pqWaitTimed(int forRead, int forWrite, PGconn *conn, pg_usec_time_t end_time)

int pqGets_append(PQExpBuffer buf, PGconn *conn)

int pqWriteReady(PGconn *conn)

int pqPutMsgEnd(PGconn *conn)

bool pgtls_read_pending(PGconn *conn)

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

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

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

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

Assert(PointerIsAligned(start, uint64))

#define libpq_ngettext(s, p, n)

#define pqIsnonblocking(conn)

#define pg_char_to_encoding

#define PG_STRERROR_R_BUFLEN

#define ALL_CONNECTION_FAILURE_ERRNOS

size_t strnlen(const char *str, size_t maxlen)

void resetPQExpBuffer(PQExpBuffer str)

void appendBinaryPQExpBuffer(PQExpBuffer str, const char *data, size_t datalen)

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

void appendPQExpBufferChar(PQExpBuffer str, char ch)

void appendPQExpBufferStr(PQExpBuffer str, const char *data)

#define PQExpBufferBroken(str)

int pthread_mutex_unlock(pthread_mutex_t *mp)

int pthread_mutex_lock(pthread_mutex_t *mp)

#define PTHREAD_MUTEX_INITIALIZER

PQExpBufferData errorMessage

PGNoticeHooks noticeHooks

int pg_encoding_dsplen(int encoding, const char *mbstr)

int pg_encoding_mblen(int encoding, const char *mbstr)

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

int gettimeofday(struct timeval *tp, void *tzp)