6 static char *rcsid_gdb_fserv_c = "$Header$";
10 /************************************************************************
14 * GDB - Routines to implement forking servers.
16 * Author: Noah Mendelsohn
17 * Copyright: 1986 MIT Project Athena
18 * For copying and distribution information, please see
19 * the file <mit-copyright.h>.
21 ************************************************************************/
23 #include <mit-copyright.h>
25 #include <sys/types.h>
27 #include <sys/socket.h>
29 #include <sys/signal.h>
31 #include <sys/resource.h>
37 /************************************************************************
39 * create_forking_server (create_forking_server)
41 * Called by an application to turn itself into a forking model
42 * server. Returns from this routine occur only in the forked
43 * children. The parent lives in this routine forever, waiting
44 * for incoming connection requests and doing the appropriate
47 * Children are expected to do their own cleanup, but this routine
48 * does do the work of reaping the resulting zombie processes.
53 * service-id identifies the port to be used for
54 * listening. Same rules as for
55 * create_listening_connection.
57 * validate-rtn pointer to a function to be called to
58 * validate the incoming client. Should
59 * return TRUE if client is acceptable,
60 * else false. If this is NULL, all clients
66 * Children created by this routine inherit the global variables
67 * gdb_sockaddr_of_client, which is of type sockaddr_in and
68 * gdb_socklen, which is the returned length of the sockaddr.
69 * These are the Berkeley identifiers of the clients as accepted.
70 * Use of this interface is non-portable to other than Berkeley
73 * The client's request tuple may be found in gdb_client_tuple.
75 ************************************************************************/
79 create_forking_server(service, validate)
83 void start_accepting_client();
84 CONNECTION incoming; /* listen for incoming */
86 CONNECTION client = NULL; /* connection to client */
88 OPERATION listenop; /* used to asynchronously */
89 /* listen for a child */
90 /* connection request */
93 /*----------------------------------------------------------*/
95 /* Set up parent execution environment
97 /*----------------------------------------------------------*/
99 g_do_signals(); /* set up signal handling */
101 incoming = create_listening_connection(service);
102 if (incoming == NULL || connection_status(incoming) != CON_UP)
103 GDB_GIVEUP("create_forking_server: can't create listening connection")
105 /*----------------------------------------------------------*/
107 /* Repeatedly listen for incoming
109 /*----------------------------------------------------------*/
111 listenop = create_operation();
113 gdb_socklen = sizeof(gdb_sockaddr_of_client);
115 (void) start_accepting_client(incoming, listenop, &client,
116 gdb_sockaddr_of_client, &gdb_socklen,
118 if(complete_operation(listenop) != OP_COMPLETE ||
120 gdb_perror("GDB create_forking_server: failed to accept client");
121 reset_operation(listenop);
122 (void) sever_connection(client);
128 * Call the validate routine, if it fails,
131 if (validate != NULL && !(*validate)()) {
132 reset_operation(listenop);
133 start_replying_to_client(listenop, client,
135 (void) complete_operation(listenop);
136 reset_operation(listenop);
137 (void) sever_connection(client);
141 * Create the child for this client
143 if ((gdb_Debug & GDB_NOFORK) || fork() == 0) {
145 * This is the child, or we're in noforking
148 reset_operation(listenop);
149 start_replying_to_client(listenop, client,
150 GDB_ACCEPTED, "","");
151 if (complete_operation(listenop) != OP_COMPLETE)
153 return client; /* return to application */
157 * Still in the parent
159 (void) sever_connection(client);
160 reset_operation(listenop);
164 /************************************************************************/
168 /* Called on SIGCHILD to reap all dead children.
170 /************************************************************************/
185 while (waitpid(-1, &status, WNOHANG) >0);
187 while (wait3(&status, WNOHANG, (struct rusage *)0) >0);
191 /************************************************************************/
195 /* Set up signal handling for a forking server.
197 /************************************************************************/
203 struct sigaction act;
205 act.sa_handler = gdb_reaper;
206 sigemptyset(&act.sa_mask);
208 (void) sigaction(SIGCHLD, &act, (struct sigaction *)0);
210 (void) signal(SIGCHLD, gdb_reaper);