6 static char *rcsid_gdb_fserv_c = "$Header$";
11 /************************************************************************
15 * GDB - Routines to implement forking servers.
17 * Author: Noah Mendelsohn
18 * Copyright: 1986 MIT Project Athena
19 * For copying and distribution information, please see
20 * the file <mit-copyright.h>.
22 ************************************************************************/
24 #include <mit-copyright.h>
26 #include <sys/types.h>
28 #include <sys/socket.h>
30 #include <sys/signal.h>
32 #include <sys/resource.h>
34 /************************************************************************
36 * create_forking_server (create_forking_server)
38 * Called by an application to turn itself into a forking model
39 * server. Returns from this routine occur only in the forked
40 * children. The parent lives in this routine forever, waiting
41 * for incoming connection requests and doing the appropriate
44 * Children are expected to do their own cleanup, but this routine
45 * does do the work of reaping the resulting zombie processes.
50 * service-id identifies the port to be used for
51 * listening. Same rules as for
52 * create_listening_connection.
54 * validate-rtn pointer to a function to be called to
55 * validate the incoming client. Should
56 * return TRUE if client is acceptable,
57 * else false. If this is NULL, all clients
63 * Children created by this routine inherit the global variables
64 * gdb_sockaddr_of_client, which is of type sockaddr_in and
65 * gdb_socklen, which is the returned length of the sockaddr.
66 * These are the Berkeley identifiers of the clients as accepted.
67 * Use of this interface is non-portable to other than Berkeley
70 * The client's request tuple may be found in gdb_client_tuple.
72 ************************************************************************/
76 create_forking_server(service, validate)
80 void start_accepting_client();
81 CONNECTION incoming; /* listen for incoming */
83 CONNECTION client = NULL; /* connection to client */
85 OPERATION listenop; /* used to asynchronously */
86 /* listen for a child */
87 /* connection request */
90 /*----------------------------------------------------------*/
92 /* Set up parent execution environment
94 /*----------------------------------------------------------*/
96 g_do_signals(); /* set up signal handling */
98 incoming = create_listening_connection(service);
100 GDB_GIVEUP("create_forking_server: can't create listening connection")
102 /*----------------------------------------------------------*/
104 /* Repeatedly listen for incoming
106 /*----------------------------------------------------------*/
108 listenop = create_operation();
110 gdb_socklen = sizeof(gdb_sockaddr_of_client);
112 (void) start_accepting_client(incoming, listenop, &client,
113 gdb_sockaddr_of_client, &gdb_socklen,
115 if(complete_operation(listenop) != OP_COMPLETE ||
117 GDB_GIVEUP("create_forking_server: failed to accept client")
121 * Call the validate routine, if it fails,
124 if (validate != NULL && !(*validate)()) {
125 reset_operation(listenop);
126 start_replying_to_client(listenop, client,
128 (void) complete_operation(listenop);
129 reset_operation(listenop);
130 (void) sever_connection(client);
134 * Create the child for this client
136 if ((gdb_Debug & GDB_NOFORK) || fork() == 0) {
138 * This is the child, or we're in noforking
141 reset_operation(listenop);
142 start_replying_to_client(listenop, client,
143 GDB_ACCEPTED, "","");
144 if (complete_operation(listenop) != OP_COMPLETE)
146 return client; /* return to application */
150 * Still in the parent
152 (void) sever_connection(client);
153 reset_operation(listenop);
157 /************************************************************************/
161 /* Called on SIGCHILD to reap all dead children.
163 /************************************************************************/
168 extern char *sys_siglist[];
170 while (wait3(&status, WNOHANG, (struct rusage *)0) >0) {
172 if (WIFEXITED(status)) {
173 if (status.w_retcode)
174 printf("exited with code %d\n",
177 if (WIFSIGNALED(status)) {
178 printf("exited on %s signal%s\n",
179 sys_siglist[status.w_termsig],
180 (status.w_coredump?"; core dumped":0));
186 /************************************************************************/
190 /* Set up signal handling for a forking server.
192 /************************************************************************/
197 (void) signal(SIGCHLD, gdb_reaper);