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