]>
Commit | Line | Data |
---|---|---|
5580185e | 1 | /* |
097e9b93 | 2 | * $Source$ |
3 | * $Header$ | |
5580185e | 4 | */ |
5 | ||
6 | #ifndef lint | |
7 | static char *rcsid_gdb_ops_c = "$Header$"; | |
d9f43d51 | 8 | #endif |
5580185e | 9 | |
10 | ||
097e9b93 | 11 | /************************************************************************ |
12 | * | |
13 | * gdb_ops.c | |
14 | * | |
15 | * GDB - Asynchronous Operations and Their Synchronous | |
16 | * Counterparts | |
17 | * | |
18 | * Author: Noah Mendelsohn | |
19 | * Copyright: 1986 MIT Project Athena | |
20 | * For copying and distribution information, please see | |
21 | * the file <mit-copyright.h>. | |
22 | * | |
23 | * These routines provide a suite of asynchronous operations | |
24 | * on connections. | |
25 | * | |
26 | ************************************************************************/ | |
5580185e | 27 | |
0a5ff702 | 28 | #include <mit-copyright.h> |
5580185e | 29 | #include <stdio.h> |
30 | #include "gdb.h" | |
31 | #include <netinet/in.h> | |
32 | #include <sys/ioctl.h> | |
097e9b93 | 33 | #ifdef SOLARIS |
34 | #include <sys/filio.h> | |
35 | #endif | |
097e9b93 | 36 | |
37 | /************************************************************************ | |
38 | * | |
39 | * send_object (send_object) | |
40 | * | |
41 | * Synchronous form of start_sending_object. Returns either | |
42 | * OP_CANCELLED, or OP_RESULT(op). | |
43 | * | |
44 | ************************************************************************/ | |
5580185e | 45 | |
46 | int | |
47 | send_object(con, objp, type) | |
48 | CONNECTION con; | |
49 | char *objp; | |
50 | int type; | |
51 | { | |
52 | register OPERATION op; | |
53 | register int retval; | |
54 | ||
55 | ||
56 | op = create_operation(); | |
57 | start_sending_object(op, con, objp, type); | |
58 | (void) complete_operation(op); | |
59 | if (OP_STATUS(op) == OP_COMPLETE) | |
60 | retval = OP_RESULT(op); | |
61 | else | |
62 | retval = OP_STATUS(op); | |
63 | delete_operation(op); | |
64 | return retval; | |
65 | } | |
097e9b93 | 66 | |
67 | ||
5580185e | 68 | /************************************************************************/ |
69 | /* | |
70 | /* start_send_object (g_snobj) | |
71 | /* | |
72 | /* Start the asynchronous transmission of a gdb object. | |
73 | /* Note that this routine must be passed the address of the object, | |
74 | /* not the object itself. | |
75 | /* | |
76 | /* The following three routines work together, and may be considered | |
77 | /* as a single entity implementing the operation. The first merely | |
78 | /* saves away its arguments and queues the operation on the designated | |
79 | /* connection. These stay there until they percolate to the head of | |
80 | /* the queue. The second is the initialization routine, which is | |
81 | /* called by the connection maintenance logic when the operation | |
82 | /* first reaches the head of the queue. This routine encodes | |
83 | /* the supplied data for transmission, and then sends it. If the | |
84 | /* transmission executes synchronously, then the third routine is | |
85 | /* called immediately to clean up. If not, the third routine is | |
86 | /* marked as the 'continuation' routine, which will cause its | |
87 | /* invocation when the transmission completes. | |
88 | /* | |
d9f43d51 | 89 | /* The data is preceded by its length expressed as a 32-bit number in |
5580185e | 90 | /* network byte order. |
91 | /* | |
92 | /************************************************************************/ | |
93 | ||
94 | struct obj_data { | |
95 | char *objp; /* address of the object to */ | |
96 | /* be sent */ | |
97 | int type; /* type code for the object */ | |
98 | /* to be sent*/ | |
99 | char *flattened; /* address of first byte */ | |
100 | /* of flattened data */ | |
101 | int len; /* length of the flattened */ | |
102 | /* data */ | |
103 | }; | |
104 | ||
105 | int g_isnobj(); | |
106 | int g_csnobj(); | |
107 | ||
108 | int | |
109 | start_sending_object(op, con, objp, type) | |
110 | OPERATION op; | |
111 | CONNECTION con; | |
112 | char *objp; | |
113 | int type; | |
114 | { | |
115 | struct obj_data *arg; | |
116 | ||
117 | /* | |
118 | * Make sure the supplied connection is a legal one | |
119 | */ | |
120 | GDB_CHECK_CON(con, "start_sending_object") | |
121 | GDB_CHECK_OP(op, "start_sending_object") | |
122 | ||
123 | arg = (struct obj_data *)db_alloc(sizeof(struct obj_data)); | |
124 | ||
125 | arg->objp = objp; | |
126 | arg->type = type; | |
127 | initialize_operation(op, g_isnobj, (char *)arg, (int (*)())NULL); | |
128 | (void) queue_operation(con, CON_OUTPUT, op); | |
129 | } | |
130 | ||
131 | /*----------------------------------------------------------*/ | |
132 | /* | |
133 | /* g_isnobj | |
134 | /* | |
135 | /* Init routine for sending an object. This routine is | |
136 | /* called by the connection management logic when the send | |
137 | /* request percolates to the top of the queue. This routine | |
138 | /* reformats the data into an appropriate form for transmission. | |
d9f43d51 | 139 | /* The format used is a length, represented as a 32-bit # in |
5580185e | 140 | /* network byte order, followed by the data itself. The |
141 | /* continuation routine below is called, either synchronously | |
142 | /* or asynchronously, once the transmission is complete. | |
143 | /* | |
144 | /*----------------------------------------------------------*/ | |
145 | ||
146 | int | |
147 | g_isnobj(op, hcon, arg) | |
148 | OPERATION op; | |
149 | HALF_CONNECTION hcon; | |
150 | struct obj_data *arg; | |
151 | { | |
152 | /* | |
153 | * Find out the encoded length of the data | |
154 | */ | |
155 | arg->len = FCN_PROPERTY(arg->type, CODED_LENGTH_PROPERTY) | |
156 | (arg->objp, hcon); | |
157 | ||
158 | /* | |
159 | * Allocate space and flatten (encode) the data | |
160 | */ | |
d9f43d51 | 161 | arg->flattened = db_alloc(arg->len+sizeof(int32)); |
162 | *(uint32 *)arg->flattened = htonl((uint32)arg->len); | |
5580185e | 163 | |
164 | FCN_PROPERTY(arg->type, ENCODE_PROPERTY) | |
d9f43d51 | 165 | (arg->objp, hcon, arg->flattened+sizeof(int32)); |
5580185e | 166 | |
167 | /* | |
168 | * Set up continuation routine in case it's needed after the return | |
169 | */ | |
170 | op->fcn.cont = g_csnobj; | |
171 | ||
172 | /* | |
173 | * Start sending the data, maybe even complete | |
174 | */ | |
d9f43d51 | 175 | if (gdb_send_data(hcon, arg->flattened, arg->len + sizeof(int32)) == |
5580185e | 176 | OP_COMPLETE) { |
177 | return g_csnobj(op, hcon, arg) ;/* this return is a little */ | |
178 | /* subtle. As continuation */ | |
179 | /* routines call each other */ | |
180 | /* synchronously, the last */ | |
181 | /* one determines whether we */ | |
182 | /* completed or are still */ | |
183 | /* running. That status */ | |
184 | /* percolates back through */ | |
185 | /* the entire call chain. */ | |
186 | } else { | |
187 | return OP_RUNNING; | |
188 | } | |
189 | } | |
190 | ||
191 | ||
192 | ||
193 | ||
194 | ||
195 | ||
196 | /*----------------------------------------------------------*/ | |
197 | /* | |
198 | /* g_csnobj | |
199 | /* | |
200 | /* Continuation routine for sending an object. Since there is | |
201 | /* only one transmission, started by the init routine, this is | |
202 | /* called when that transmission is done, and it does all the | |
203 | /* associated clean up. | |
204 | /* | |
205 | /*----------------------------------------------------------*/ | |
206 | ||
24582af9 | 207 | /*ARGSUSED*/ |
5580185e | 208 | int |
209 | g_csnobj(op, hcon, arg) | |
210 | OPERATION op; | |
211 | HALF_CONNECTION hcon; | |
212 | struct obj_data *arg; | |
213 | { | |
214 | op->result = OP_SUCCESS; | |
d9f43d51 | 215 | db_free((char *)arg->flattened, arg->len + sizeof(int32)); |
5580185e | 216 | /* free the sent data */ |
217 | db_free((char *)arg, sizeof(struct obj_data)); /* free the state structure */ | |
218 | return OP_COMPLETE; | |
219 | } | |
220 | ||
097e9b93 | 221 | |
5580185e | 222 | /************************************************************************/ |
223 | /* | |
224 | /* receive_object (receive_object) | |
225 | /* | |
226 | /* Synchronous form of start_receiving_object. Returns either | |
227 | /* OP_CANCELLED, or OP_RESULT(op). | |
228 | /* | |
229 | /************************************************************************/ | |
230 | ||
231 | int | |
232 | receive_object(con, objp, type) | |
233 | CONNECTION con; | |
234 | char *objp; | |
235 | int type; | |
236 | { | |
237 | register OPERATION op; | |
238 | register int retval; | |
239 | ||
240 | op = create_operation(); | |
241 | start_receiving_object(op, con, objp, type); | |
242 | (void) complete_operation(op); | |
243 | if (OP_STATUS(op) == OP_COMPLETE) | |
244 | retval = OP_RESULT(op); | |
245 | else | |
246 | retval = OP_STATUS(op); | |
247 | delete_operation(op); | |
248 | return retval; | |
249 | } | |
097e9b93 | 250 | |
251 | ||
5580185e | 252 | /************************************************************************/ |
253 | /* | |
254 | /* start_receiving_object (g_rcobj) | |
255 | /* | |
256 | /* Start the asynchronous receipt of a gdb object. Note that this | |
257 | /* routine must be passed the address of the object, not the object | |
258 | /* itself. In the case of structured objects, this routine may | |
259 | /* allocate the necessary storage. The work to build the object is | |
260 | /* done by the object's decode routine. | |
261 | /* | |
262 | /* The following three routines work together, and may be considered | |
263 | /* as a single entity implementing the operation. The first merely | |
264 | /* saves away its arguments and queues the operation on the designated | |
265 | /* connection. These stay there until they percolate to the head of | |
266 | /* the queue. The second is the initialization routine, which is | |
267 | /* called by the connection maintenance logic when the operation | |
268 | /* first reaches the head of the queue. This routine initiates a read | |
269 | /* for the length of the object, and sets up a continuation routine | |
270 | /* to read the object itself. When the object itself has been read, it | |
271 | /* is decoded and the operation completes. | |
272 | /* | |
d9f43d51 | 273 | /* The data is preceded by its length expressed as a 32-bit number in |
5580185e | 274 | /* network byte order. |
275 | /* | |
276 | /* preempt_and_start_receiving_object (g_prcobj) | |
277 | /* | |
278 | /* Similar to above, but may be called only from an active operation | |
279 | /* (i.e. an init or continue routine) on an inbound half connection. | |
280 | /* The receive effectively pre-empts the old operation, which wil | |
281 | /* continue after the receive is done. | |
282 | /* | |
283 | /* | |
284 | /************************************************************************/ | |
285 | ||
286 | struct robj_data { | |
287 | char *objp; /* address of the object to */ | |
288 | /* be received */ | |
289 | int type; /* type code for the object */ | |
290 | /* to be received */ | |
291 | char *flattened; /* address of first byte */ | |
292 | /* of flattened data */ | |
293 | int len; /* length of the flattened */ | |
294 | /* data */ | |
295 | }; | |
296 | ||
297 | int g_ircobj(); | |
298 | int g_c1rcobj(); | |
299 | int g_c2rcobj(); | |
300 | ||
301 | /*----------------------------------------------------------*/ | |
302 | /* | |
303 | /* start_receiving_object | |
304 | /* | |
305 | /*----------------------------------------------------------*/ | |
306 | ||
307 | int | |
308 | start_receiving_object(op, con, objp, type) | |
309 | OPERATION op; | |
310 | CONNECTION con; | |
311 | char *objp; | |
312 | int type; | |
313 | { | |
314 | struct robj_data *arg; | |
315 | ||
316 | /* | |
317 | * Make sure the supplied connection is a legal one | |
318 | */ | |
319 | GDB_CHECK_CON(con, "start_receiving_object") | |
320 | GDB_CHECK_OP(op, "start_receiving_object") | |
321 | ||
322 | arg = (struct robj_data *)db_alloc(sizeof(struct robj_data)); | |
323 | ||
324 | arg->objp = objp; | |
325 | arg->type = type; | |
326 | initialize_operation(op, g_ircobj, (char *)arg, (int (*)())NULL); | |
327 | (void) queue_operation(con, CON_INPUT, op); | |
328 | } | |
329 | ||
330 | /*----------------------------------------------------------*/ | |
331 | /* | |
332 | /* preempt_and_start_receiving_object | |
333 | /* | |
334 | /*----------------------------------------------------------*/ | |
335 | ||
336 | int | |
337 | preempt_and_start_receiving_object(op, oldop, objp, type) | |
338 | OPERATION op; | |
339 | OPERATION oldop; | |
340 | char *objp; | |
341 | int type; | |
342 | { | |
343 | struct robj_data *arg; | |
344 | ||
345 | /* | |
346 | * Make sure the supplied connection is a legal one | |
347 | */ | |
348 | GDB_CHECK_OP(op, "preempt_and_start_receiving_object") | |
349 | GDB_CHECK_OP(oldop, "preempt_and_start_receiving_object") | |
350 | ||
351 | arg = (struct robj_data *)db_alloc(sizeof(struct robj_data)); | |
352 | ||
353 | arg->objp = objp; | |
354 | arg->type = type; | |
355 | initialize_operation(op, g_ircobj, (char *)arg, (int (*)())NULL); | |
356 | (void) g_preempt_me(oldop, op); | |
357 | } | |
358 | ||
359 | /*----------------------------------------------------------*/ | |
360 | /* | |
361 | /* g_ircobj | |
362 | /* | |
363 | /* Initialization routine for receiving an object. | |
364 | /* Called when the receive operation percolates to the | |
365 | /* top of the queue. First, we must receive the single | |
d9f43d51 | 366 | /* 32-bit # which carries the length of the rest of the data. |
5580185e | 367 | /* We do that now, either synchronously or asynchronously. |
368 | /* | |
369 | /*----------------------------------------------------------*/ | |
370 | ||
371 | int | |
372 | g_ircobj(op, hcon, arg) | |
373 | OPERATION op; | |
374 | HALF_CONNECTION hcon; | |
375 | struct robj_data *arg; | |
376 | { | |
377 | op->fcn.cont = g_c1rcobj; | |
d9f43d51 | 378 | if(gdb_receive_data(hcon, (char *)&(arg->len), sizeof(int32)) == OP_COMPLETE) { |
5580185e | 379 | return g_c1rcobj(op, hcon, arg);/* this return is a little */ |
380 | /* subtle. As continuation */ | |
381 | /* routines call each other */ | |
382 | /* synchronously, the last */ | |
383 | /* one determines whether we */ | |
384 | /* completed or are still */ | |
385 | /* running. That status */ | |
386 | /* percolates back through */ | |
387 | /* the entire call chain. */ | |
388 | } else { | |
389 | return OP_RUNNING; | |
390 | } | |
391 | } | |
392 | ||
393 | /*----------------------------------------------------------*/ | |
394 | /* | |
395 | /* g_c1rcobj | |
396 | /* | |
397 | /* At this point, we have received the length. Now, allocate | |
398 | /* the space for the rest of the data, and start receiving | |
399 | /* it. | |
400 | /* | |
401 | /*----------------------------------------------------------*/ | |
402 | ||
403 | int | |
404 | g_c1rcobj(op, hcon, arg) | |
405 | OPERATION op; | |
406 | HALF_CONNECTION hcon; | |
407 | struct robj_data *arg; | |
408 | { | |
5580185e | 409 | /* |
410 | * Now we know the length of the encoded data, convert the length | |
411 | * to local byte order, and allocate the space for the receive. | |
412 | */ | |
d9f43d51 | 413 | arg->len = (int) ntohl((uint32)arg->len); |
b1fb7b0c | 414 | if (arg->len > 65536) |
415 | return OP_CANCELLED; | |
5580185e | 416 | |
417 | arg->flattened = db_alloc(arg->len); | |
b1fb7b0c | 418 | if (arg->flattened == NULL) |
419 | return OP_CANCELLED; | |
5580185e | 420 | /* |
421 | * Now start receiving the encoded object itself. If it all comes in | |
422 | * synchronously, then just go on to the c2 routine to decode it and | |
423 | * finish up. Else return OP_RUNNING, so the rest of the system | |
424 | * can get some work done while we wait. | |
425 | */ | |
426 | op->fcn.cont = g_c2rcobj; | |
427 | if(gdb_receive_data(hcon, arg->flattened, arg->len ) == OP_COMPLETE) { | |
428 | return g_c2rcobj(op, hcon, arg); | |
429 | } else { | |
430 | return OP_RUNNING; | |
431 | } | |
432 | } | |
433 | ||
434 | /*----------------------------------------------------------*/ | |
435 | /* | |
436 | /* g_c2rcobj | |
437 | /* | |
438 | /* At this point, all the data has been received. Decode | |
439 | /* it into the place provided by the caller, free all | |
440 | /* temporarily allocated memory, and return. | |
441 | /* | |
442 | /*----------------------------------------------------------*/ | |
443 | ||
444 | int | |
445 | g_c2rcobj(op, hcon, arg) | |
446 | OPERATION op; | |
447 | HALF_CONNECTION hcon; | |
448 | struct robj_data *arg; | |
449 | { | |
450 | /* | |
451 | * Decode the received data into local representation. | |
452 | */ | |
453 | FCN_PROPERTY(arg->type, DECODE_PROPERTY) | |
454 | (arg->objp, hcon, arg->flattened); | |
455 | op->result = OP_SUCCESS; | |
456 | db_free(arg->flattened, arg->len); /* free the received data */ | |
457 | db_free((char *)arg, sizeof(struct robj_data)); /* free the state structure */ | |
458 | return OP_COMPLETE; | |
459 | } | |
097e9b93 | 460 | |
461 | ||
5580185e | 462 | /************************************************************************/ |
463 | /* | |
464 | /* complete_operation(complete_operation) | |
465 | /* | |
466 | /* Wait for a given operation to complete, allowing everything | |
467 | /* to progress in the meantime. Returns the last known status | |
468 | /* of the operation, which in general will be OP_COMPLETE unless | |
469 | /* errors were encountered (and this version of the code doesn't | |
470 | /* do error handing right anyway!) | |
471 | /* | |
472 | /* We do this by (1) calling gdb_progress to assure that all | |
473 | /* possible progress has been made, which is always a good thing | |
474 | /* to do when we get the chance and (2) looping on calls to | |
475 | /* con_select, which will make all possible future progress, | |
476 | /* but without burning cycles unnecessarily in the process. | |
477 | /* | |
478 | /************************************************************************/ | |
479 | ||
480 | int | |
481 | complete_operation(op) | |
482 | OPERATION op; | |
483 | { | |
484 | (void) gdb_progress(); | |
485 | ||
486 | while(op->status != OP_COMPLETE && op->status != OP_CANCELLED) | |
487 | (void) con_select(0, (fd_set *)NULL, (fd_set *)NULL, | |
488 | (fd_set *)NULL, (struct timeval *)NULL); | |
489 | ||
490 | return op->status; | |
491 | ||
492 | } | |
493 | ||
097e9b93 | 494 | |
5580185e | 495 | /************************************************************************/ |
496 | /* | |
497 | /* cancel_operation(cancel_operation) | |
498 | /* | |
499 | /* Attempts to cancel an operation. | |
500 | /* | |
501 | /************************************************************************/ | |
502 | ||
503 | int | |
504 | cancel_operation(op) | |
505 | OPERATION op; | |
506 | { | |
507 | register HALF_CONNECTION hcon = op->halfcon; | |
508 | ||
509 | if (op->status != OP_RUNNING && op->status != OP_QUEUED) | |
510 | return op->status; | |
511 | ||
512 | if (hcon == NULL) | |
513 | GDB_GIVEUP("cancel_operation: operation is queued but half connection is unknown") | |
514 | ||
515 | /* | |
516 | * If we're at the head of the queue and running, then we have to | |
517 | * call the cancelation routine for this particular operation so | |
518 | * it can clean up. | |
519 | */ | |
520 | if (op->prev == (OPERATION)hcon) { | |
521 | if (op->status == OP_RUNNING && op->cancel != NULL) | |
522 | (*op->cancel)(op->halfcon, op->arg); | |
523 | } | |
524 | ||
525 | /* | |
526 | * Looks safe, now cancel it. | |
527 | */ | |
528 | op->next->prev = op->prev; /* de-q it */ | |
529 | op->prev->next = op->next; /* " " " */ | |
530 | op->status = OP_CANCELLED; | |
531 | op->halfcon = NULL; | |
532 | ||
533 | return OP_CANCELLED; | |
534 | } | |
097e9b93 | 535 | |
536 | ||
5580185e | 537 | /************************************************************************/ |
538 | /* | |
539 | /* start_listening | |
540 | /* | |
541 | /* Start the asynchronous acquisition of a connection. This | |
542 | /* results in the queuing of a GDB "OPERATION" to do the | |
543 | /* requested listening. | |
544 | /* | |
545 | /************************************************************************/ | |
546 | ||
547 | struct lis_data { | |
548 | char *otherside; /* data returned from an */ | |
549 | /* accept */ | |
550 | int *otherlen; /* length of the otherside */ | |
551 | /* field */ | |
552 | int *fdp; /* ptr to the fd of the */ | |
553 | /* newly accepted */ | |
554 | /* connection */ | |
555 | }; | |
556 | ||
557 | int g_ilis(); | |
558 | int g_clis(); | |
559 | ||
b1fb7b0c | 560 | void |
5580185e | 561 | gdb_start_listening(op, con, otherside, lenp, fdp) |
562 | OPERATION op; | |
563 | CONNECTION con; | |
564 | char *otherside; | |
565 | int *lenp; | |
566 | int *fdp; | |
567 | { | |
568 | struct lis_data *arg; | |
569 | ||
570 | GDB_INIT_CHECK | |
571 | ||
572 | /* | |
573 | * Make sure the supplied connection is a legal one | |
574 | */ | |
575 | GDB_CHECK_CON(con, "start_listening") | |
576 | GDB_CHECK_OP(op, "start_listening") | |
577 | ||
578 | arg = (struct lis_data *)db_alloc(sizeof(struct lis_data)); | |
579 | ||
580 | arg->otherside = otherside; | |
581 | arg->otherlen = lenp; | |
582 | arg->fdp = fdp; | |
583 | initialize_operation(op, g_ilis, (char *)arg, (int (*)())NULL); | |
584 | (void) queue_operation(con, CON_INPUT, op); | |
585 | } | |
586 | ||
587 | /*----------------------------------------------------------*/ | |
588 | /* | |
589 | /* g_ilis | |
590 | /* | |
591 | /* Init routine for doing a listen. | |
592 | /* | |
593 | /*----------------------------------------------------------*/ | |
594 | ||
595 | int | |
596 | g_ilis(op, hcon, arg) | |
597 | OPERATION op; | |
598 | HALF_CONNECTION hcon; | |
599 | struct lis_data *arg; | |
600 | { | |
601 | int rc; | |
602 | ||
603 | /* | |
604 | * Set up continuation routine in case it's needed after the return | |
605 | */ | |
606 | op->fcn.cont = g_clis; | |
607 | ||
608 | /* | |
609 | * Try doing the listen now, and then decide whether to go | |
610 | * right on to the continuation routine or to let things hang | |
611 | * for the moment. | |
612 | */ | |
613 | rc = gdb_start_a_listen(hcon, arg->otherside, arg->otherlen, arg->fdp); | |
614 | if (rc==OP_COMPLETE) { | |
615 | return g_clis(op, hcon, arg); /* this return is a little */ | |
616 | /* subtle. As continuation */ | |
617 | /* routines call each other */ | |
618 | /* synchronously, the last */ | |
619 | /* one determines whether we */ | |
620 | /* completed or are still */ | |
621 | /* running. That status */ | |
622 | /* percolates back through */ | |
623 | /* the entire call chain. */ | |
624 | } else { | |
625 | return OP_RUNNING; | |
626 | } | |
627 | } | |
628 | ||
629 | ||
630 | ||
631 | /*----------------------------------------------------------*/ | |
632 | /* | |
633 | /* g_clis | |
634 | /* | |
635 | /* Continuation routine for accepting a connection. | |
636 | /* | |
637 | /* At this point, the fd has been accepted and all | |
638 | /* the necessary information given back to the caller. | |
639 | /* | |
640 | /*----------------------------------------------------------*/ | |
641 | ||
24582af9 | 642 | /*ARGSUSED*/ |
5580185e | 643 | int |
644 | g_clis(op, hcon, arg) | |
645 | OPERATION op; | |
646 | HALF_CONNECTION hcon; | |
647 | struct lis_data *arg; | |
648 | { | |
649 | op->result = OP_SUCCESS; | |
650 | db_free((char *)arg, sizeof(struct lis_data)); | |
651 | /* free the state structure */ | |
652 | return OP_COMPLETE; | |
653 | } | |
654 | ||
097e9b93 | 655 | |
5580185e | 656 | /************************************************************************/ |
657 | /* | |
658 | /* start_accepting_client | |
659 | /* | |
660 | /* Start the asynchronous acquisition of a client. This queueable | |
661 | /* operation first tries to accept a connection. On this connection, | |
662 | /* it reads a startup string from the client, and then completes. | |
663 | /* | |
664 | /* The return values from this are not quite what you might expect. | |
665 | /* In general, the operation will show complete, rather than cancelled, | |
666 | /* if it gets as far as creating the new connection at all. If | |
667 | /* subsequent activities result in errors from system calls, then | |
668 | /* this operation will complete with a status of OP_COMPLETE and a | |
669 | /* result of OP_CANCELLED. In this case, the applications IS given | |
670 | /* a connection descriptor for the new connection, and that descriptor | |
671 | /* has an errno value indicating why the failure occurred. The | |
672 | /* caller must then sever this connection to free the descriptor. | |
673 | /* | |
674 | /************************************************************************/ | |
675 | ||
676 | struct acc_data { | |
677 | char *otherside; /* data returned from an */ | |
678 | /* accept */ | |
679 | int *otherlen; /* length of the otherside */ | |
680 | /* field */ | |
681 | OPERATION listenop; /* used to listen for */ | |
682 | /* the fd */ | |
683 | OPERATION receiveop; /* used when receiving */ | |
684 | /* tuple from the client */ | |
685 | CONNECTION con; /* the connection we're */ | |
686 | /* trying to create */ | |
687 | CONNECTION *conp; /* this is where the caller */ | |
688 | /* wants the connection */ | |
689 | /* returned */ | |
690 | TUPLE *tuplep; /* pointer to tuple we */ | |
691 | /* are going to receive */ | |
692 | /* from new client */ | |
693 | }; | |
694 | ||
695 | int g_iacc(); | |
696 | int g_i2acc(); | |
697 | ||
b1fb7b0c | 698 | void |
5580185e | 699 | start_accepting_client(listencon, op, conp, otherside, lenp, tuplep) |
700 | CONNECTION listencon; | |
701 | OPERATION op; | |
702 | CONNECTION *conp; | |
703 | char *otherside; | |
704 | int *lenp; | |
705 | TUPLE *tuplep; | |
706 | { | |
707 | struct acc_data *arg; | |
708 | ||
709 | GDB_INIT_CHECK | |
710 | ||
711 | /* | |
712 | * Make sure the supplied connection and operation are legal | |
713 | */ | |
714 | GDB_CHECK_CON(listencon, "start_accepting_client") | |
715 | GDB_CHECK_OP(op, "start_accepting_client") | |
716 | ||
717 | arg = (struct acc_data *)db_alloc(sizeof(struct acc_data)); | |
718 | ||
719 | arg->otherside = otherside; | |
720 | arg->otherlen = lenp; | |
721 | arg->conp = conp; | |
722 | *conp = NULL; /* in case we fail */ | |
723 | arg->listenop = create_operation(); | |
724 | arg->receiveop = create_operation(); | |
725 | arg->con = g_make_con(); | |
726 | arg->tuplep = tuplep; | |
727 | *tuplep = NULL; /* in case we fail */ | |
728 | ||
729 | /* | |
730 | * Queue an operation ahead of us which will accept an fd and | |
731 | * put it in arg->con->in. As a byproduct, pick up the from | |
732 | * information that we return to the caller. | |
733 | */ | |
734 | gdb_start_listening(arg->listenop, listencon, | |
735 | arg->otherside, | |
736 | arg->otherlen, &(arg->con->in.fd)); | |
737 | ||
738 | /* | |
739 | * Now queue us behind it. By the time we run our init routine, | |
740 | * a connection should have been acquired. | |
741 | */ | |
742 | initialize_operation(op, g_iacc, (char *)arg, (int (*)())NULL); | |
743 | (void) queue_operation(listencon, CON_INPUT, op); | |
744 | } | |
745 | ||
746 | /*----------------------------------------------------------*/ | |
747 | /* | |
748 | /* g_iacc | |
749 | /* | |
750 | /* Init routine for accepting a connection. By the | |
751 | /* time this runs, the listen has been done, the | |
752 | /* 'from' data put in position for the caller, and | |
753 | /* the fd plugged into the connection descriptor. | |
754 | /* If all went well, fill out the connection descriptor | |
755 | /* and then requeue us on that to do the receive of | |
756 | /* the requested tuple. | |
757 | /* | |
758 | /*----------------------------------------------------------*/ | |
759 | ||
24582af9 | 760 | /*ARGSUSED*/ |
5580185e | 761 | int |
762 | g_iacc(op, hcon, arg) | |
763 | OPERATION op; | |
764 | HALF_CONNECTION hcon; | |
765 | struct acc_data *arg; | |
766 | { | |
767 | register CONNECTION con = arg->con; | |
768 | ||
769 | /* | |
770 | * Set up 2nd init routine for after we re-queue ourselves | |
771 | */ | |
772 | op->fcn.cont = g_i2acc; | |
773 | /* | |
774 | * See whether we successfully accepted a connection. If | |
775 | * not, we just cancel ourselves. If so, fill out the | |
776 | * connection descriptor and related data structures properly, | |
777 | * then requeue ourselves on the new connection. | |
778 | */ | |
779 | if (OP_STATUS(arg->listenop) != OP_COMPLETE || | |
780 | OP_RESULT(arg->listenop) != OP_SUCCESS || | |
781 | con->in.fd <=0) { | |
782 | (void) sever_connection(con); | |
783 | g_clnup_accept(arg); | |
784 | op->result = OP_CANCELLED; | |
785 | return OP_CANCELLED; | |
786 | } | |
787 | ||
788 | /* | |
789 | * OK, we got an fd, but the connection and related structures | |
790 | * aren't really set up straight, and the fd must be put | |
791 | * into non-blocking mode. There really should be a common | |
792 | * routine for this, since some of the logic exists in 2 | |
793 | * or 3 places. | |
794 | */ | |
795 | con->status = CON_STARTING; | |
796 | con->out.fd = con->in.fd; | |
797 | g_ver_iprotocol(con); /* make sure we're at */ | |
798 | /* same level of protocol */ | |
799 | if (con->status == CON_UP) { | |
800 | /* | |
801 | * We've successfully started the connection, now mark | |
802 | * it for non-blocking I/O. Also, update the high water | |
803 | * mark of fd's controlled by our system. | |
804 | */ | |
805 | int nb = 1; | |
806 | if(ioctl(con->in.fd, FIONBIO, (char *)&nb)== (-1)) { | |
807 | g_stop_with_errno(con); | |
808 | *arg->conp = con; /* give failed con to */ | |
809 | /* caller so he can find */ | |
810 | /* errno */ | |
811 | gdb_perror("gdb: ioctl for non-block failed"); | |
812 | g_clnup_accept(arg); | |
813 | op->result = OP_CANCELLED; /* we didn't really, but */ | |
814 | /* we want caller to look */ | |
815 | /* at the connection so he */ | |
816 | /* can find errno*/ | |
817 | return OP_COMPLETE; | |
818 | } | |
819 | if (con->in.fd +1 > gdb_mfd) | |
820 | gdb_mfd = con->in.fd + 1; | |
821 | /* | |
822 | * Allocate a buffer, if necessary, and reset buffer pointers | |
823 | * so first request will result in a long read into the buffer | |
824 | */ | |
825 | g_allocate_connection_buffers(con); | |
826 | ||
827 | } else { | |
828 | *arg->conp = con; /* give failed con to */ | |
829 | /* caller so he can find */ | |
830 | /* errno */ | |
831 | g_clnup_accept(arg); | |
832 | op->result = OP_CANCELLED; | |
833 | return OP_COMPLETE; | |
834 | } | |
835 | ||
836 | /* | |
837 | * Before we requeue ourselves on the new connection, queue | |
838 | * up a receive for the expected tuple. Then we'll be | |
839 | * sure that it's there by the time we run. | |
840 | */ | |
841 | start_receiving_object(arg->receiveop, con, (char *)(arg->tuplep), | |
842 | TUPLE_T); | |
843 | /* | |
844 | * Requeue ourselves behind the receive operation. | |
845 | */ | |
846 | ||
847 | (void) requeue_operation(con, CON_INPUT, op); | |
848 | return OP_REQUEUED; | |
849 | } | |
850 | ||
851 | ||
852 | ||
853 | /*----------------------------------------------------------*/ | |
854 | /* | |
855 | /* g_i2acc | |
856 | /* | |
857 | /* Second init routine for accepting a connection. | |
858 | /* This one is run after the operation is requeued on | |
859 | /* the new connection. By the time we run here, the | |
860 | /* attempt to receive the tuple has already been made. | |
861 | /* We just check on status and clean-up. | |
862 | /* | |
863 | /*----------------------------------------------------------*/ | |
864 | ||
24582af9 | 865 | /*ARGSUSED*/ |
5580185e | 866 | int |
867 | g_i2acc(op, hcon, arg) | |
868 | OPERATION op; | |
869 | HALF_CONNECTION hcon; | |
870 | struct acc_data *arg; | |
871 | { | |
872 | int rc; | |
873 | ||
874 | rc = OP_STATUS(arg->receiveop); /* if it completes, then */ | |
875 | /* so do we! */ | |
876 | *arg->conp = arg->con; /* give caller the new con */ | |
877 | if (rc != OP_COMPLETE) | |
878 | (void) g_stop_connection(arg->con); | |
879 | /* | |
880 | * Release all transient data structures. | |
881 | */ | |
882 | g_clnup_accept(arg); | |
883 | ||
884 | return OP_COMPLETE; | |
885 | } | |
886 | ||
887 | /*----------------------------------------------------------*/ | |
888 | /* | |
889 | /* g_clnup_accept | |
890 | /* | |
891 | /* Free all data structures used by start_accepting_client. | |
892 | /* | |
893 | /*----------------------------------------------------------*/ | |
894 | ||
895 | int | |
896 | g_clnup_accept(arg) | |
897 | struct acc_data *arg; | |
898 | { | |
899 | delete_operation(arg->listenop); | |
900 | delete_operation(arg->receiveop); | |
901 | db_free((char *)arg, sizeof(struct acc_data)); | |
902 | } |