prevent static-threaded web server from closing the streaming socket
Costa Tsaousis (ktsaou) committed
Jan 10, 2018 at 21:10 UTC
9d079f6d5d84808645cee339b2dade90f26d8dba
5 files changed
+31
-14
src/rrdpush.c
+9
-4
@@ -1060,10 +1060,15 @@ int rrdpush_receiver_thread_spawn(RRDHOST *host, struct web_client *w, char *url
1060
error("Failed to create new STREAM receive thread for client.");
1061
1062
// prevent the caller from closing the streaming socket
1063
- if(w->ifd == w->ofd)
1064
- w->ifd = w->ofd = -1;
1065
- else
1066
- w->ifd = -1;
1063
+ if(web_server_mode == WEB_SERVER_MODE_STATIC_THREADED) {
1064
+ web_client_flag_set(w, WEB_CLIENT_FLAG_DONT_CLOSE_SOCKET);
1065
+ }
1066
+ else {
1067
+ if(w->ifd == w->ofd)
1068
+ w->ifd = w->ofd = -1;
1069
+ else
1070
+ w->ifd = -1;
1071
+ }
1072
1073
buffer_flush(w->response.data);
1074
return 200;
src/socket.c
+5
-2
@@ -1064,8 +1064,11 @@ inline void poll_close_fd(POLLINFO *pi) {
1064
pi->del_callback(pi);
1065
}
1066
1067
- // info("POLLFD: closing fd %d", pf->fd);
1068
- close(pf->fd);
1067
+ if(likely(!(pi->flags & POLLINFO_FLAG_DONT_CLOSE))) {
1068
+ if(close(pf->fd) == -1)
1069
+ error("Failed to close() poll_events() socket %d", pf->fd);
1070
+ }
1071
+
1072
pf->fd = -1;
1073
pf->events = 0;
1074
pf->revents = 0;
src/socket.h
+8
-3
@@ -58,9 +58,12 @@ extern int accept4(int sock, struct sockaddr *addr, socklen_t *addrlen, int flag
58
59
#define POLLINFO_FLAG_SERVER_SOCKET 0x00000001
60
#define POLLINFO_FLAG_CLIENT_SOCKET 0x00000002
61
+#define POLLINFO_FLAG_DONT_CLOSE 0x00000004
62
+
63
+typedef struct poll POLLJOB;
64
65
typedef struct pollinfo {
63
- struct poll *p; // the parent
66
+ POLLJOB *p; // the parent
67
size_t slot; // the slot id
68
69
int fd; // the file descriptor
@@ -85,7 +88,7 @@ typedef struct pollinfo {
88
struct pollinfo *next;
89
} POLLINFO;
90
88
-typedef struct poll {
91
+struct poll {
92
size_t slots;
93
size_t used;
94
size_t min;
@@ -100,7 +103,9 @@ typedef struct poll {
103
void (*del_callback)(POLLINFO *pi);
104
int (*rcv_callback)(POLLINFO *pi, short int *events);
105
int (*snd_callback)(POLLINFO *pi, short int *events);
103
-} POLLJOB;
106
+};
107
+
108
+#define pollinfo_from_slot(p, slot) (&((p)->inf[(slot)]))
109
110
extern int poll_default_snd_callback(POLLINFO *pi, short int *events);
111
extern int poll_default_rcv_callback(POLLINFO *pi, short int *events);
src/web_client.h
+3
-1
@@ -29,7 +29,9 @@ typedef enum web_client_flags {
29
WEB_CLIENT_FLAG_TRACKING_REQUIRED = 1 << 6, // if set, we need to send cookies
30
31
WEB_CLIENT_FLAG_TCP_CLIENT = 1 << 7, // if set, the client is using a TCP socket
32
- WEB_CLIENT_FLAG_UNIX_CLIENT = 1 << 8 // if set, the client is using a UNIX socket
32
+ WEB_CLIENT_FLAG_UNIX_CLIENT = 1 << 8, // if set, the client is using a UNIX socket
33
+
34
+ WEB_CLIENT_FLAG_DONT_CLOSE_SOCKET = 1 << 9, // don't close the socket when cleaning up (static-threaded web server)
35
} WEB_CLIENT_FLAGS;
36
37
//#ifdef HAVE_C___ATOMIC
src/web_server.c
+6
-4
@@ -954,8 +954,8 @@ static int web_server_file_read_callback(POLLINFO *pi, short int *events) {
954
ssize_t ret = unlikely(web_client_read_file(w));
955
956
if(likely(web_client_has_wait_send(w))) {
957
- POLLJOB *p = pi->p; // our POLLJOB
958
- POLLINFO *wpi = &p->inf[w->pollinfo_slot]; // POLLINFO of the client socket
957
+ POLLJOB *p = pi->p; // our POLLJOB
958
+ POLLINFO *wpi = pollinfo_from_slot(p, w->pollinfo_slot); // POLLINFO of the client socket
959
960
debug(D_WEB_CLIENT, "%llu: SIGNALING W TO SEND (iFD %d, oFD %d)", w->id, pi->fd, wpi->fd);
961
p->fds[wpi->slot].events |= POLLOUT;
@@ -1014,11 +1014,13 @@ static void web_server_del_callback(POLLINFO *pi) {
1014
1015
w->pollinfo_slot = 0;
1016
if(unlikely(w->pollinfo_filecopy_slot)) {
1017
- POLLJOB *p = pi->p; // our POLLJOB
1018
- POLLINFO *fpi = &p->inf[w->pollinfo_filecopy_slot]; // POLLINFO of the client socket
1017
+ POLLINFO *fpi = pollinfo_from_slot(pi->p, w->pollinfo_filecopy_slot); // POLLINFO of the client socket
1018
debug(D_WEB_CLIENT, "%llu: THE CLIENT WILL BE FRED BY READING FILE JOB ON FD %d", w->id, fpi->fd);
1019
}
1020
else {
1021
+ if(web_client_flag_check(w, WEB_CLIENT_FLAG_DONT_CLOSE_SOCKET))
1022
+ pi->flags |= POLLINFO_FLAG_DONT_CLOSE;
1023
+
1024
debug(D_WEB_CLIENT, "%llu: CLOSING CLIENT FD %d", w->id, pi->fd);
1025
web_client_release(w);
1026
}