]> andersk Git - moira.git/blame - gdb/gdb_fserv.c
lint
[moira.git] / gdb / gdb_fserv.c
CommitLineData
5580185e 1/*
5580185e 2 * $Header$
3 */
4
5#ifndef lint
6static char *rcsid_gdb_fserv_c = "$Header$";
7#endif lint
8
9
10
24582af9 11/************************************************************************
12 *
13 * gdb_fserv.c
14 *
15 * GDB - Routines to implement forking servers.
16 *
17 * Author: Noah Mendelsohn
18 * Copyright: 1986 MIT Project Athena
19 * For copying and distribution information, please see
20 * the file <mit-copyright.h>.
21 *
22 ************************************************************************/
5580185e 23
0a5ff702 24#include <mit-copyright.h>
5580185e 25#include <stdio.h>
26#include <sys/types.h>
27#include <sys/uio.h>
28#include <sys/socket.h>
29#include <sys/wait.h>
30#include <sys/signal.h>
31#include "gdb.h"
32#include <sys/resource.h>
33\f
24582af9 34/************************************************************************
35 *
36 * create_forking_server (create_forking_server)
37 *
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
42 * forking.
43 *
44 * Children are expected to do their own cleanup, but this routine
45 * does do the work of reaping the resulting zombie processes.
46 *
47 * ARGUMENTS:
48 * ----------
49 *
50 * service-id identifies the port to be used for
51 * listening. Same rules as for
52 * create_listening_connection.
53 *
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
58 * are accepted.
59 *
60 * GLOBAL VARIABLES
61 * ----------------
62 *
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
68 * systems.
69 *
70 * The client's request tuple may be found in gdb_client_tuple.
71 *
72 ************************************************************************/
5580185e 73
74
75CONNECTION
76create_forking_server(service, validate)
77char *service;
78int (*validate)();
79{
24582af9 80 void start_accepting_client();
5580185e 81 CONNECTION incoming; /* listen for incoming */
82 /* children here */
83 CONNECTION client = NULL; /* connection to client */
84 /* is created here */
85 OPERATION listenop; /* used to asynchronously */
86 /* listen for a child */
87 /* connection request */
88 GDB_INIT_CHECK
89
90 /*----------------------------------------------------------*/
91 /*
92 /* Set up parent execution environment
93 /*
94 /*----------------------------------------------------------*/
95
96 g_do_signals(); /* set up signal handling */
97
98 incoming = create_listening_connection(service);
99 if (incoming == NULL)
100 GDB_GIVEUP("create_forking_server: can't create listening connection")
101
102 /*----------------------------------------------------------*/
103 /*
104 /* Repeatedly listen for incoming
105 /*
106 /*----------------------------------------------------------*/
107
108 listenop = create_operation();
109 while (TRUE) {
110 gdb_socklen = sizeof(gdb_sockaddr_of_client);
111 client = NULL;
112 (void) start_accepting_client(incoming, listenop, &client,
113 gdb_sockaddr_of_client, &gdb_socklen,
114 &gdb_client_tuple);
115 if(complete_operation(listenop) != OP_COMPLETE ||
116 client == NULL)
117 GDB_GIVEUP("create_forking_server: failed to accept client")
118
119
120 /*
121 * Call the validate routine, if it fails,
122 * refuse the client.
123 */
124 if (validate != NULL && !(*validate)()) {
125 reset_operation(listenop);
126 start_replying_to_client(listenop, client,
127 GDB_REFUSED, "","");
128 (void) complete_operation(listenop);
129 reset_operation(listenop);
130 (void) sever_connection(client);
131 continue;
132 }
133 /*
134 * Create the child for this client
135 */
136 if ((gdb_Debug & GDB_NOFORK) || fork() == 0) {
137 /*
138 * This is the child, or we're in noforking
139 * debug mode.
140 */
141 reset_operation(listenop);
142 start_replying_to_client(listenop, client,
143 GDB_ACCEPTED, "","");
144 if (complete_operation(listenop) != OP_COMPLETE)
145 exit(8);
146 return client; /* return to application */
147 /* program */
148 }
149 /*
150 * Still in the parent
151 */
152 (void) sever_connection(client);
153 reset_operation(listenop);
154 }
155}
156\f
157/************************************************************************/
158/*
159/* gdb_reaper
160/*
161/* Called on SIGCHILD to reap all dead children.
162/*
163/************************************************************************/
164int
165gdb_reaper()
166{
167 union wait status;
168 extern char *sys_siglist[];
169
170 while (wait3(&status, WNOHANG, (struct rusage *)0) >0) {
171#ifdef notdef
172 if (WIFEXITED(status)) {
173 if (status.w_retcode)
174 printf("exited with code %d\n",
175 status.w_retcode);
176 }
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));
181 }
182#endif notdef
183 }
184}
185\f
186/************************************************************************/
187/*
188/* g_do_signals
189/*
190/* Set up signal handling for a forking server.
191/*
192/************************************************************************/
193
194int
195g_do_signals()
196{
197 (void) signal(SIGCHLD, gdb_reaper);
198}
This page took 0.088041 seconds and 5 git commands to generate.