@@ -51,6 +51,12 @@ int set_nonblocking(int fd)
51
51
return 1 ;
52
52
}
53
53
54
+ int proc_exists (pid_t pid)
55
+ {
56
+ if (pid==0 || pid==1 ) return 1 ;
57
+ return kill (pid, 0 ) != -1 ;
58
+ }
59
+
54
60
client_t * this_client;
55
61
56
62
int main (int argc, char * argv[])
@@ -119,13 +125,12 @@ int main(int argc, char* argv[])
119
125
if ( listen (listen_socket, 10 ) == -1 )
120
126
error_exit (MSG_START " cannot listen() on socket.\n " );
121
127
128
+ fprintf (stderr,MSG_START " listening on %s:%d\n " , inet_ntoa (addr_host.sin_addr ), host_port);
129
+
122
130
struct sockaddr_in addr_cli;
123
131
socklen_t addr_cli_len = sizeof (addr_cli);
124
132
int new_socket;
125
133
126
- // The server will wait on these sockets later...
127
-
128
-
129
134
// Set stdin and listen_socket to non-blocking
130
135
if (set_nonblocking (STDIN_FILENO) || set_nonblocking (listen_socket))
131
136
error_exit (MSG_START " cannot set_nonblocking().\n " );
@@ -200,24 +205,31 @@ int main(int argc, char* argv[])
200
205
if (write (clients[i]->pipefd [1 ], buf, retval)==-1 )
201
206
{
202
207
203
- if (!clients[i]->error ) print_client (clients[i], " lost buffer, failed to write pipe" );
204
- else clients[i]->error =1 ;
208
+ if (!clients[i]->error )
209
+ {
210
+ print_client (clients[i], " lost buffer, failed to write pipe." );
211
+ clients[i]->error =1 ;
212
+ }
205
213
// fprintf(stderr, MSG_START "errno is %d\n", errno); //usually 11
206
- int wpstatus;
207
- int wpresult = waitpid (clients[i]->pid , &wpstatus, WNOHANG);
208
- if (wpresult == -1 ) print_client (clients[i], " error while waitpid()!" );
209
- else if (wpresult == 0 )
214
+ // int wpstatus;
215
+ // int wpresult = waitpid(clients[i]->pid, &wpstatus, WNOHANG);
216
+ // fprintf(stderr, MSG_START "pid is %d\n",clients[i]->pid);
217
+ // perror("somethings wrong");
218
+ // if(wpresult == -1) print_client(clients[i], "error while waitpid()!");
219
+ // else if(wpresult == 0)
220
+ waitpid (clients[i]->pid , NULL , WNOHANG);
221
+ if (!proc_exists (clients[i]->pid ))
210
222
{
211
223
// Client exited!
212
224
print_client (clients[i], " closing client from main process." );
213
225
close (clients[i]->pipefd [1 ]);
214
226
close (clients[i]->socket );
215
227
delete clients[i];
216
228
clients.erase (clients.begin ()+i);
217
- print_client (clients[i], " okay ." );
229
+ print_client (clients[i], " done closing client from main process ." );
218
230
}
219
231
}
220
- else clients[i]->error =0 ;
232
+ else { if ( clients[i]->error ) print_client (clients[i], " pipe okay again. " ); clients[i]-> error =0 ; }
221
233
}
222
234
}
223
235
// TODO: at the end, server closes pipefd[1] for client
@@ -229,7 +241,7 @@ int main(int argc, char* argv[])
229
241
230
242
void print_client (client_t * client, const char * what)
231
243
{
232
- fprintf (stderr,MSG_START " (client %s:%d) %s\n " , inet_ntoa (client->addr .sin_addr ), client->addr .sin_port , what);
244
+ fprintf (stderr,MSG_START " (client %s:%d) %s\n " , inet_ntoa (client->addr .sin_addr ), client->addr .sin_port , what);
233
245
}
234
246
235
247
void client_cleanup ()
@@ -245,7 +257,7 @@ void client()
245
257
read (this_client->pipefd [0 ],buf,bufsizeall);
246
258
if (send (this_client->socket ,buf,bufsizeall,0 )==-1 )
247
259
{
248
- print_client (this_client, " closing ." );
260
+ print_client (this_client, " client process is exiting ." );
249
261
exit (0 );
250
262
}
251
263
}
0 commit comments