summaryrefslogtreecommitdiff
path: root/src/utils/socket_stream.c
blob: 4748488ae458ea47b2ab8112f8ccad77fb01f8ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* -*-pgsql-c-*- */
/*
* pgpool: a language independent connection pool server for PostgreSQL
* written by Tatsuo Ishii
*
* Copyright (c) 2003-2019	PgPool Global Development Group
*
* Permission to use, copy, modify, and distribute this software and
* its documentation for any purpose and without fee is hereby
* granted, provided that the above copyright notice appear in all
* copies and that both that copyright notice and this permission
* notice appear in supporting documentation, and that the name of the
* author not be used in advertising or publicity pertaining to
* distribution of the software without specific, written prior
* permission. The author makes no representations about the
* suitability of this software for any purpose.  It is provided "as
* is" without express or implied warranty.
*
* pool_stream.c: stream I/O modules
*
*/

#include "config.h"

#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>

#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif

#include "pool.h"
#include "utils/socket_stream.h"
#ifndef POOL_PRIVATE
#include "utils/elog.h"
#else
#include "utils/fe_ports.h"
#endif


/*
 * set non-block flag
 */
void
socket_set_nonblock(int fd)
{
	int			var;

	/* set fd to none blocking */
	var = fcntl(fd, F_GETFL, 0);
	if (var == -1)
	{
		ereport(FATAL,
				(errmsg("unable to set options on socket"),
				 errdetail("fcntl system call failed with error \"%m\"")));

	}
	if (fcntl(fd, F_SETFL, var | O_NONBLOCK) == -1)
	{
		ereport(FATAL,
				(errmsg("unable to set options on socket"),
				 errdetail("fcntl system call failed with error \"%m\"")));
	}
}

/*
 * unset non-block flag
 */
void
socket_unset_nonblock(int fd)
{
	int			var;

	/* set fd to none blocking */
	var = fcntl(fd, F_GETFL, 0);
	if (var == -1)
	{
		ereport(FATAL,
				(errmsg("unable to set options on socket"),
				 errdetail("fcntl system call failed with error \"%m\"")));
	}
	if (fcntl(fd, F_SETFL, var & ~O_NONBLOCK) == -1)
	{
		ereport(FATAL,
				(errmsg("unable to set options on socket"),
				 errdetail("fcntl system call failed with error \"%m\"")));
	}
}

#ifdef DEBUG
/*
 * Debug aid
 */
static void
dump_buffer(char *buf, int len)
{
	if (!message_level_is_interesting(DEBUG5))
		return;

	while (--len)
	{
		ereport(DEBUG5,
				(errmsg("%02x", *buf++)));
	}
}
#endif
int
socket_write(int fd, void *buf, size_t len)
{
	int			bytes_send = 0;

	do
	{
		int			ret;

		ret = write(fd, buf + bytes_send, (len - bytes_send));
		if (ret <= 0)
		{
			if (errno == EINTR || errno == EAGAIN)
			{
				ereport(DEBUG5,
						(errmsg("write on socket failed with error :\"%m\""),
						 errdetail("retrying...")));
				continue;
			}
			ereport(LOG,
					(errmsg("write on socket failed"),
					 errdetail("%m")));
			return -1;
		}
		bytes_send += ret;
	} while (bytes_send < len);
	return bytes_send;
}

int
socket_read(int fd, void *buf, size_t len, int timeout)
{
	int			ret,
				read_len;

	read_len = 0;
	struct timeval timeoutval;
	fd_set		readmask;
	int			fds;

	while (read_len < len)
	{
		FD_ZERO(&readmask);
		FD_SET(fd, &readmask);

		timeoutval.tv_sec = timeout;
		timeoutval.tv_usec = 0;

		fds = select(fd + 1, &readmask, NULL, NULL, timeout ? &timeoutval : NULL);
		if (fds == -1)
		{
			if (errno == EAGAIN || errno == EINTR)
				continue;

			ereport(WARNING,
					(errmsg("system call select() failed"),
					 errdetail("%m")));
			return -1;
		}
		else if (fds == 0)
		{
			return -2;
		}
		ret = read(fd, buf + read_len, (len - read_len));
		if (ret < 0)
		{
			if (errno == EINTR || errno == EAGAIN)
			{
				ereport(DEBUG5,
						(errmsg("read from socket failed with error :\"%m\""),
						 errdetail("retrying...")));
				continue;
			}
			ereport(LOG,
					(errmsg("read from socket failed with error :\"%m\"")));
			return -1;
		}
		if (ret == 0)
		{
			ereport(LOG,
					(errmsg("read from socket failed, remote end closed the connection")));
			return 0;
		}
		read_len += ret;
	}
	return read_len;
}