@@ -106,22 +106,22 @@ struct Client_t
106106 char * bot;
107107 char * message;
108108 char * user;
109- char * data;
109+ char * data = NULL ;
110110
111111 Client_t (int fd, struct ev_loop *l_p) : fd(fd), l(l_p), requestValid(false )
112112 {
113113 strcpy (this ->magic , " deadbeef" );
114114 ev_io_init (&this ->ev_r , client_read, this ->fd , EV_READ);
115115 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 ;
118117 this ->ev_w .data = this ;
119118 ev_io_start (this ->l , &this ->ev_r );
120119 }
121120
122- ~Client_t ()
121+ ~Client_t () // if child dies, this destructor is not called
123122 {
124- if (this ->data ) free (this ->data );
123+ if (this ->data ) free (this ->data );
124+ this ->data = NULL ;
125125 if (ev_is_active (&this ->ev_r )) ev_io_stop (this ->l , &this ->ev_r );
126126 if (ev_is_active (&this ->ev_w )) ev_io_stop (this ->l , &this ->ev_w );
127127 close (this ->fd );
@@ -186,7 +186,8 @@ struct Client_t
186186 }
187187
188188 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 ;
190191 }
191192
192193 if (r < (int )len)
@@ -209,7 +210,8 @@ struct Client_t
209210 if (this ->ip .length () == 0 )
210211 {
211212 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 ;
213215 }
214216
215217 return 1 ;
@@ -258,12 +260,14 @@ static int setnonblocking(int fd)
258260 int flags = fcntl (fd, F_GETFL, 0 );
259261 if (flags == -1 ) {
260262 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 ;
262265 }
263266 if (fcntl (fd, F_SETFL, flags | O_NONBLOCK) == -1 )
264267 {
265268 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 ;
267271 }
268272 return 1 ;
269273}
@@ -277,7 +281,8 @@ int fork_child(ev_child *child_watcher = 0)
277281 pid = fork ();
278282 if (pid < 0 ) {
279283 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 ;
281286 }
282287
283288 if (pid > 0 ) {
@@ -307,10 +312,21 @@ int fork_child(ev_child *child_watcher = 0)
307312}
308313
309314static 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+ }
314330}
315331#endif
316332
@@ -453,6 +469,7 @@ int evsrv_init(const string &interfaceKind, int port, char* arg) {
453469 ev_io_init (&ev_accept_r_g, evsrv_accept, srv_socket_g, EV_READ);
454470 ev_io_start (l_g, &ev_accept_r_g);
455471 Log (SERVERLOG, " evserver: running pid: %d\r\n " ,getpid ());
472+ printf ( " evserver: running pid: %d\r\n " , getpid ());
456473
457474 return 1 ;
458475}
@@ -541,6 +558,12 @@ static void client_read(EV_P_ ev_io *w, int revents)
541558 }
542559
543560 r = client->send_data ();
561+ if (client->data )
562+ {
563+ free (client->data );
564+ client->data = NULL ;
565+ }
566+
544567 if (r < 0 ) {
545568 Log (SERVERLOG, " evserver: could not sent data to client: %d\r\n " , client->fd );
546569 delete client;
@@ -586,7 +609,8 @@ int evsrv_do_chat(Client_t *client)
586609 }
587610#endif
588611 if (!client->data ) client->data = (char *) malloc (outputsize);
589-
612+ if (!client->data ) printf (" Malloc failed for child data\r\n " );
613+
590614RESTART_RETRY:
591615 strcpy (ourMainInputBuffer,client->message );
592616 struct tm ptm;
0 commit comments