@@ -106,22 +106,22 @@ struct Client_t
106
106
char * bot;
107
107
char * message;
108
108
char * user;
109
- char * data;
109
+ char * data = NULL ;
110
110
111
111
Client_t (int fd, struct ev_loop *l_p) : fd(fd), l(l_p), requestValid(false )
112
112
{
113
113
strcpy (this ->magic , " deadbeef" );
114
114
ev_io_init (&this ->ev_r , client_read, this ->fd , EV_READ);
115
115
ev_io_init (&this ->ev_w , client_write, this ->fd , EV_WRITE);
116
- this ->data = NULL ;
117
- this ->ev_r .data = this ;
116
+ this ->ev_r .data = this ;
118
117
this ->ev_w .data = this ;
119
118
ev_io_start (this ->l , &this ->ev_r );
120
119
}
121
120
122
- ~Client_t ()
121
+ ~Client_t () // if child dies, this destructor is not called
123
122
{
124
- if (this ->data ) free (this ->data );
123
+ if (this ->data ) free (this ->data );
124
+ this ->data = NULL ;
125
125
if (ev_is_active (&this ->ev_r )) ev_io_stop (this ->l , &this ->ev_r );
126
126
if (ev_is_active (&this ->ev_w )) ev_io_stop (this ->l , &this ->ev_w );
127
127
close (this ->fd );
@@ -186,7 +186,8 @@ struct Client_t
186
186
}
187
187
188
188
Log (SERVERLOG, " evserver: send_data() could not send, errno: %s" , strerror (errno));
189
- return -1 ;
189
+ printf ( " evserver: send_data() could not send, errno: %s" , strerror (errno));
190
+ return -1 ;
190
191
}
191
192
192
193
if (r < (int )len)
@@ -209,7 +210,8 @@ struct Client_t
209
210
if (this ->ip .length () == 0 )
210
211
{
211
212
Log (SERVERLOG, " evserver: prepare_for_chat() could not get ip for client: %d\r\n " , this ->fd );
212
- return -1 ;
213
+ printf (" evserver: prepare_for_chat() could not get ip for client: %d\r\n " , this ->fd );
214
+ return -1 ;
213
215
}
214
216
215
217
return 1 ;
@@ -258,12 +260,14 @@ static int setnonblocking(int fd)
258
260
int flags = fcntl (fd, F_GETFL, 0 );
259
261
if (flags == -1 ) {
260
262
Log (SERVERLOG, " evserver: setnonblocking() fcntl(F_GETFL) failed, errno: %s\r\n " , strerror (errno));
261
- return -1 ;
263
+ printf ( " evserver: setnonblocking() fcntl(F_GETFL) failed, errno: %s\r\n " , strerror (errno));
264
+ return -1 ;
262
265
}
263
266
if (fcntl (fd, F_SETFL, flags | O_NONBLOCK) == -1 )
264
267
{
265
268
Log (SERVERLOG, " evserver: setnonblocking() fcntl(F_SETFL) failed, errno: %s\r\n " , strerror (errno));
266
- return -1 ;
269
+ printf ( " evserver: setnonblocking() fcntl(F_SETFL) failed, errno: %s\r\n " , strerror (errno));
270
+ return -1 ;
267
271
}
268
272
return 1 ;
269
273
}
@@ -277,7 +281,8 @@ int fork_child(ev_child *child_watcher = 0)
277
281
pid = fork ();
278
282
if (pid < 0 ) {
279
283
Log (SERVERLOG, " evserver: fork failed, errno %d\r\n " , errno);
280
- return -1 ;
284
+ printf ( " evserver: fork failed, errno %d\r\n " , errno);
285
+ return -1 ;
281
286
}
282
287
283
288
if (pid > 0 ) {
@@ -307,10 +312,21 @@ int fork_child(ev_child *child_watcher = 0)
307
312
}
308
313
309
314
static void evsrv_child_died (EV_P_ ev_child *w, int revents) {
310
- Log (SERVERLOG, " evserver: evsrv_child_died [pid: %d]\r\n " , w->pid );
311
- int r = fork_child (w);
312
- if (r < 0 ) Log (SERVERLOG, " evserver: could not re-spawn child after it died [pid: %d]\r\n " , w->pid );
313
- else if (r == 1 ) Log (SERVERLOG, " evserver child: re-spawned [pid: %d]\r\n " , getpid ());
315
+ Log (SERVERLOG, " evserver: evsrv_child_died [pid: %d]\r\n " , w->pid );
316
+ printf (" evserver: evsrv_child_died [pid: %d]\r\n " , w->pid );
317
+
318
+ int r = fork_child (w);
319
+ if (r < 0 )
320
+ {
321
+ Log (SERVERLOG, " evserver: could not re-spawn child after it died [pid: %d]\r\n " , w->pid );
322
+ printf (" evserver: could not re-spawn child after it died [pid: %d]\r\n " , w->pid );
323
+ }
324
+
325
+ else if (r == 1 )
326
+ {
327
+ Log (SERVERLOG, " evserver child: re-spawned [pid: %d]\r\n " , getpid ());
328
+ printf (" evserver child: re-spawned [pid: %d]\r\n " , getpid ());
329
+ }
314
330
}
315
331
#endif
316
332
@@ -453,6 +469,7 @@ int evsrv_init(const string &interfaceKind, int port, char* arg) {
453
469
ev_io_init (&ev_accept_r_g, evsrv_accept, srv_socket_g, EV_READ);
454
470
ev_io_start (l_g, &ev_accept_r_g);
455
471
Log (SERVERLOG, " evserver: running pid: %d\r\n " ,getpid ());
472
+ printf ( " evserver: running pid: %d\r\n " , getpid ());
456
473
457
474
return 1 ;
458
475
}
@@ -541,6 +558,12 @@ static void client_read(EV_P_ ev_io *w, int revents)
541
558
}
542
559
543
560
r = client->send_data ();
561
+ if (client->data )
562
+ {
563
+ free (client->data );
564
+ client->data = NULL ;
565
+ }
566
+
544
567
if (r < 0 ) {
545
568
Log (SERVERLOG, " evserver: could not sent data to client: %d\r\n " , client->fd );
546
569
delete client;
@@ -586,7 +609,8 @@ int evsrv_do_chat(Client_t *client)
586
609
}
587
610
#endif
588
611
if (!client->data ) client->data = (char *) malloc (outputsize);
589
-
612
+ if (!client->data ) printf (" Malloc failed for child data\r\n " );
613
+
590
614
RESTART_RETRY:
591
615
strcpy (ourMainInputBuffer,client->message );
592
616
struct tm ptm;
0 commit comments