* Read one message from the specified pipe (fd), blocking if necessary * until one is available, and return it as a malloc'd string. * On EOF, return NULL. * * A "message" on the channel is just a null-terminated string. */
| 1664 | * A "message" on the channel is just a null-terminated string. |
| 1665 | */ |
| 1666 | static char * |
| 1667 | readMessageFromPipe(int fd) |
| 1668 | { |
| 1669 | char *msg; |
| 1670 | int msgsize, |
| 1671 | bufsize; |
| 1672 | int ret; |
| 1673 | |
| 1674 | /* |
| 1675 | * In theory, if we let piperead() read multiple bytes, it might give us |
| 1676 | * back fragments of multiple messages. (That can't actually occur, since |
| 1677 | * neither leader nor workers send more than one message without waiting |
| 1678 | * for a reply, but we don't wish to assume that here.) For simplicity, |
| 1679 | * read a byte at a time until we get the terminating '\0'. This method |
| 1680 | * is a bit inefficient, but since this is only used for relatively short |
| 1681 | * command and status strings, it shouldn't matter. |
| 1682 | */ |
| 1683 | bufsize = 64; /* could be any number */ |
| 1684 | msg = (char *) pg_malloc(bufsize); |
| 1685 | msgsize = 0; |
| 1686 | for (;;) |
| 1687 | { |
| 1688 | Assert(msgsize < bufsize); |
| 1689 | ret = piperead(fd, msg + msgsize, 1); |
| 1690 | if (ret <= 0) |
| 1691 | break; /* error or connection closure */ |
| 1692 | |
| 1693 | Assert(ret == 1); |
| 1694 | |
| 1695 | if (msg[msgsize] == '\0') |
| 1696 | return msg; /* collected whole message */ |
| 1697 | |
| 1698 | msgsize++; |
| 1699 | if (msgsize == bufsize) /* enlarge buffer if needed */ |
| 1700 | { |
| 1701 | bufsize += 16; /* could be any number */ |
| 1702 | msg = (char *) pg_realloc(msg, bufsize); |
| 1703 | } |
| 1704 | } |
| 1705 | |
| 1706 | /* Other end has closed the connection */ |
| 1707 | pg_free(msg); |
| 1708 | return NULL; |
| 1709 | } |
| 1710 | |
| 1711 | #ifdef WIN32 |
| 1712 |
no test coverage detected