7 static char *rcsid_gdb_ops_c = "$Header$";
30 /************************************************************************/
34 /* GDB - Asynchronous Operations and Their Synchronous
37 /* Author: Noah Mendelsohn
38 /* Copyright: 1986 MIT Project Athena
40 /* These routines provide a suite of asynchronous operations
43 /************************************************************************/
47 #include <netinet/in.h>
48 #include <sys/ioctl.h>
50 extern u_long htonl();
53 /************************************************************************/
55 /* send_object (send_object)
57 /* Synchronous form of start_sending_object. Returns either
58 /* OP_CANCELLED, or OP_RESULT(op).
60 /************************************************************************/
63 send_object(con, objp, type)
68 register OPERATION op;
72 op = create_operation();
73 start_sending_object(op, con, objp, type);
74 (void) complete_operation(op);
75 if (OP_STATUS(op) == OP_COMPLETE)
76 retval = OP_RESULT(op);
78 retval = OP_STATUS(op);
83 /************************************************************************/
85 /* start_send_object (g_snobj)
87 /* Start the asynchronous transmission of a gdb object.
88 /* Note that this routine must be passed the address of the object,
89 /* not the object itself.
91 /* The following three routines work together, and may be considered
92 /* as a single entity implementing the operation. The first merely
93 /* saves away its arguments and queues the operation on the designated
94 /* connection. These stay there until they percolate to the head of
95 /* the queue. The second is the initialization routine, which is
96 /* called by the connection maintenance logic when the operation
97 /* first reaches the head of the queue. This routine encodes
98 /* the supplied data for transmission, and then sends it. If the
99 /* transmission executes synchronously, then the third routine is
100 /* called immediately to clean up. If not, the third routine is
101 /* marked as the 'continuation' routine, which will cause its
102 /* invocation when the transmission completes.
104 /* The data is preceded by its length expressed as a long in
105 /* network byte order.
107 /************************************************************************/
110 char *objp; /* address of the object to */
112 int type; /* type code for the object */
114 char *flattened; /* address of first byte */
115 /* of flattened data */
116 int len; /* length of the flattened */
124 start_sending_object(op, con, objp, type)
130 struct obj_data *arg;
133 * Make sure the supplied connection is a legal one
135 GDB_CHECK_CON(con, "start_sending_object")
136 GDB_CHECK_OP(op, "start_sending_object")
138 arg = (struct obj_data *)db_alloc(sizeof(struct obj_data));
142 initialize_operation(op, g_isnobj, (char *)arg, (int (*)())NULL);
143 (void) queue_operation(con, CON_OUTPUT, op);
146 /*----------------------------------------------------------*/
150 /* Init routine for sending an object. This routine is
151 /* called by the connection management logic when the send
152 /* request percolates to the top of the queue. This routine
153 /* reformats the data into an appropriate form for transmission.
154 /* The format used is a length, represented as a long in
155 /* network byte order, followed by the data itself. The
156 /* continuation routine below is called, either synchronously
157 /* or asynchronously, once the transmission is complete.
159 /*----------------------------------------------------------*/
162 g_isnobj(op, hcon, arg)
164 HALF_CONNECTION hcon;
165 struct obj_data *arg;
168 * Find out the encoded length of the data
170 arg->len = FCN_PROPERTY(arg->type, CODED_LENGTH_PROPERTY)
174 * Allocate space and flatten (encode) the data
176 arg->flattened = db_alloc(arg->len+sizeof(long));
177 *(u_long *)arg->flattened = htonl((u_long)arg->len);
179 FCN_PROPERTY(arg->type, ENCODE_PROPERTY)
180 (arg->objp, hcon, arg->flattened+sizeof(long));
183 * Set up continuation routine in case it's needed after the return
185 op->fcn.cont = g_csnobj;
188 * Start sending the data, maybe even complete
190 if (gdb_send_data(hcon, arg->flattened, arg->len + sizeof(long)) ==
192 return g_csnobj(op, hcon, arg) ;/* this return is a little */
193 /* subtle. As continuation */
194 /* routines call each other */
195 /* synchronously, the last */
196 /* one determines whether we */
197 /* completed or are still */
198 /* running. That status */
199 /* percolates back through */
200 /* the entire call chain. */
211 /*----------------------------------------------------------*/
215 /* Continuation routine for sending an object. Since there is
216 /* only one transmission, started by the init routine, this is
217 /* called when that transmission is done, and it does all the
218 /* associated clean up.
220 /*----------------------------------------------------------*/
223 g_csnobj(op, hcon, arg)
225 HALF_CONNECTION hcon;
226 struct obj_data *arg;
228 op->result = OP_SUCCESS;
229 db_free((char *)arg->flattened, arg->len + sizeof(long));
230 /* free the sent data */
231 db_free((char *)arg, sizeof(struct obj_data)); /* free the state structure */
236 /************************************************************************/
238 /* receive_object (receive_object)
240 /* Synchronous form of start_receiving_object. Returns either
241 /* OP_CANCELLED, or OP_RESULT(op).
243 /************************************************************************/
246 receive_object(con, objp, type)
251 register OPERATION op;
254 op = create_operation();
255 start_receiving_object(op, con, objp, type);
256 (void) complete_operation(op);
257 if (OP_STATUS(op) == OP_COMPLETE)
258 retval = OP_RESULT(op);
260 retval = OP_STATUS(op);
261 delete_operation(op);
265 /************************************************************************/
267 /* start_receiving_object (g_rcobj)
269 /* Start the asynchronous receipt of a gdb object. Note that this
270 /* routine must be passed the address of the object, not the object
271 /* itself. In the case of structured objects, this routine may
272 /* allocate the necessary storage. The work to build the object is
273 /* done by the object's decode routine.
275 /* The following three routines work together, and may be considered
276 /* as a single entity implementing the operation. The first merely
277 /* saves away its arguments and queues the operation on the designated
278 /* connection. These stay there until they percolate to the head of
279 /* the queue. The second is the initialization routine, which is
280 /* called by the connection maintenance logic when the operation
281 /* first reaches the head of the queue. This routine initiates a read
282 /* for the length of the object, and sets up a continuation routine
283 /* to read the object itself. When the object itself has been read, it
284 /* is decoded and the operation completes.
286 /* The data is preceded by its length expressed as a long in
287 /* network byte order.
289 /* preempt_and_start_receiving_object (g_prcobj)
291 /* Similar to above, but may be called only from an active operation
292 /* (i.e. an init or continue routine) on an inbound half connection.
293 /* The receive effectively pre-empts the old operation, which wil
294 /* continue after the receive is done.
297 /************************************************************************/
300 char *objp; /* address of the object to */
302 int type; /* type code for the object */
304 char *flattened; /* address of first byte */
305 /* of flattened data */
306 int len; /* length of the flattened */
314 /*----------------------------------------------------------*/
316 /* start_receiving_object
318 /*----------------------------------------------------------*/
321 start_receiving_object(op, con, objp, type)
327 struct robj_data *arg;
330 * Make sure the supplied connection is a legal one
332 GDB_CHECK_CON(con, "start_receiving_object")
333 GDB_CHECK_OP(op, "start_receiving_object")
335 arg = (struct robj_data *)db_alloc(sizeof(struct robj_data));
339 initialize_operation(op, g_ircobj, (char *)arg, (int (*)())NULL);
340 (void) queue_operation(con, CON_INPUT, op);
343 /*----------------------------------------------------------*/
345 /* preempt_and_start_receiving_object
347 /*----------------------------------------------------------*/
350 preempt_and_start_receiving_object(op, oldop, objp, type)
356 struct robj_data *arg;
359 * Make sure the supplied connection is a legal one
361 GDB_CHECK_OP(op, "preempt_and_start_receiving_object")
362 GDB_CHECK_OP(oldop, "preempt_and_start_receiving_object")
364 arg = (struct robj_data *)db_alloc(sizeof(struct robj_data));
368 initialize_operation(op, g_ircobj, (char *)arg, (int (*)())NULL);
369 (void) g_preempt_me(oldop, op);
372 /*----------------------------------------------------------*/
376 /* Initialization routine for receiving an object.
377 /* Called when the receive operation percolates to the
378 /* top of the queue. First, we must receive the single
379 /* 'long' which carries the length of the rest of the data.
380 /* We do that now, either synchronously or asynchronously.
382 /*----------------------------------------------------------*/
385 g_ircobj(op, hcon, arg)
387 HALF_CONNECTION hcon;
388 struct robj_data *arg;
390 op->fcn.cont = g_c1rcobj;
391 if(gdb_receive_data(hcon, (char *)&(arg->len), sizeof(long)) == OP_COMPLETE) {
392 return g_c1rcobj(op, hcon, arg);/* this return is a little */
393 /* subtle. As continuation */
394 /* routines call each other */
395 /* synchronously, the last */
396 /* one determines whether we */
397 /* completed or are still */
398 /* running. That status */
399 /* percolates back through */
400 /* the entire call chain. */
406 /*----------------------------------------------------------*/
410 /* At this point, we have received the length. Now, allocate
411 /* the space for the rest of the data, and start receiving
414 /*----------------------------------------------------------*/
417 g_c1rcobj(op, hcon, arg)
419 HALF_CONNECTION hcon;
420 struct robj_data *arg;
423 extern u_long ntohl();
427 * Now we know the length of the encoded data, convert the length
428 * to local byte order, and allocate the space for the receive.
430 arg->len = (int) ntohl((u_long)arg->len);
432 arg->flattened = db_alloc(arg->len);
434 * Now start receiving the encoded object itself. If it all comes in
435 * synchronously, then just go on to the c2 routine to decode it and
436 * finish up. Else return OP_RUNNING, so the rest of the system
437 * can get some work done while we wait.
439 op->fcn.cont = g_c2rcobj;
440 if(gdb_receive_data(hcon, arg->flattened, arg->len ) == OP_COMPLETE) {
441 return g_c2rcobj(op, hcon, arg);
447 /*----------------------------------------------------------*/
451 /* At this point, all the data has been received. Decode
452 /* it into the place provided by the caller, free all
453 /* temporarily allocated memory, and return.
455 /*----------------------------------------------------------*/
458 g_c2rcobj(op, hcon, arg)
460 HALF_CONNECTION hcon;
461 struct robj_data *arg;
464 * Decode the received data into local representation.
466 FCN_PROPERTY(arg->type, DECODE_PROPERTY)
467 (arg->objp, hcon, arg->flattened);
468 op->result = OP_SUCCESS;
469 db_free(arg->flattened, arg->len); /* free the received data */
470 db_free((char *)arg, sizeof(struct robj_data)); /* free the state structure */
474 /************************************************************************/
476 /* complete_operation(complete_operation)
478 /* Wait for a given operation to complete, allowing everything
479 /* to progress in the meantime. Returns the last known status
480 /* of the operation, which in general will be OP_COMPLETE unless
481 /* errors were encountered (and this version of the code doesn't
482 /* do error handing right anyway!)
484 /* We do this by (1) calling gdb_progress to assure that all
485 /* possible progress has been made, which is always a good thing
486 /* to do when we get the chance and (2) looping on calls to
487 /* con_select, which will make all possible future progress,
488 /* but without burning cycles unnecessarily in the process.
490 /************************************************************************/
493 complete_operation(op)
496 (void) gdb_progress();
498 while(op->status != OP_COMPLETE && op->status != OP_CANCELLED)
499 (void) con_select(0, (fd_set *)NULL, (fd_set *)NULL,
500 (fd_set *)NULL, (struct timeval *)NULL);
507 /************************************************************************/
509 /* cancel_operation(cancel_operation)
511 /* Attempts to cancel an operation.
513 /************************************************************************/
519 register HALF_CONNECTION hcon = op->halfcon;
521 if (op->status != OP_RUNNING && op->status != OP_QUEUED)
525 GDB_GIVEUP("cancel_operation: operation is queued but half connection is unknown")
528 * If we're at the head of the queue and running, then we have to
529 * call the cancelation routine for this particular operation so
532 if (op->prev == (OPERATION)hcon) {
533 if (op->status == OP_RUNNING && op->cancel != NULL)
534 (*op->cancel)(op->halfcon, op->arg);
538 * Looks safe, now cancel it.
540 op->next->prev = op->prev; /* de-q it */
541 op->prev->next = op->next; /* " " " */
542 op->status = OP_CANCELLED;
548 /************************************************************************/
552 /* Start the asynchronous acquisition of a connection. This
553 /* results in the queuing of a GDB "OPERATION" to do the
554 /* requested listening.
556 /************************************************************************/
559 char *otherside; /* data returned from an */
561 int *otherlen; /* length of the otherside */
563 int *fdp; /* ptr to the fd of the */
572 gdb_start_listening(op, con, otherside, lenp, fdp)
579 struct lis_data *arg;
584 * Make sure the supplied connection is a legal one
586 GDB_CHECK_CON(con, "start_listening")
587 GDB_CHECK_OP(op, "start_listening")
589 arg = (struct lis_data *)db_alloc(sizeof(struct lis_data));
591 arg->otherside = otherside;
592 arg->otherlen = lenp;
594 initialize_operation(op, g_ilis, (char *)arg, (int (*)())NULL);
595 (void) queue_operation(con, CON_INPUT, op);
598 /*----------------------------------------------------------*/
602 /* Init routine for doing a listen.
604 /*----------------------------------------------------------*/
607 g_ilis(op, hcon, arg)
609 HALF_CONNECTION hcon;
610 struct lis_data *arg;
615 * Set up continuation routine in case it's needed after the return
617 op->fcn.cont = g_clis;
620 * Try doing the listen now, and then decide whether to go
621 * right on to the continuation routine or to let things hang
624 rc = gdb_start_a_listen(hcon, arg->otherside, arg->otherlen, arg->fdp);
625 if (rc==OP_COMPLETE) {
626 return g_clis(op, hcon, arg); /* this return is a little */
627 /* subtle. As continuation */
628 /* routines call each other */
629 /* synchronously, the last */
630 /* one determines whether we */
631 /* completed or are still */
632 /* running. That status */
633 /* percolates back through */
634 /* the entire call chain. */
642 /*----------------------------------------------------------*/
646 /* Continuation routine for accepting a connection.
648 /* At this point, the fd has been accepted and all
649 /* the necessary information given back to the caller.
651 /*----------------------------------------------------------*/
654 g_clis(op, hcon, arg)
656 HALF_CONNECTION hcon;
657 struct lis_data *arg;
659 op->result = OP_SUCCESS;
660 db_free((char *)arg, sizeof(struct lis_data));
661 /* free the state structure */
666 /************************************************************************/
668 /* start_accepting_client
670 /* Start the asynchronous acquisition of a client. This queueable
671 /* operation first tries to accept a connection. On this connection,
672 /* it reads a startup string from the client, and then completes.
674 /* The return values from this are not quite what you might expect.
675 /* In general, the operation will show complete, rather than cancelled,
676 /* if it gets as far as creating the new connection at all. If
677 /* subsequent activities result in errors from system calls, then
678 /* this operation will complete with a status of OP_COMPLETE and a
679 /* result of OP_CANCELLED. In this case, the applications IS given
680 /* a connection descriptor for the new connection, and that descriptor
681 /* has an errno value indicating why the failure occurred. The
682 /* caller must then sever this connection to free the descriptor.
684 /************************************************************************/
687 char *otherside; /* data returned from an */
689 int *otherlen; /* length of the otherside */
691 OPERATION listenop; /* used to listen for */
693 OPERATION receiveop; /* used when receiving */
694 /* tuple from the client */
695 CONNECTION con; /* the connection we're */
696 /* trying to create */
697 CONNECTION *conp; /* this is where the caller */
698 /* wants the connection */
700 TUPLE *tuplep; /* pointer to tuple we */
701 /* are going to receive */
702 /* from new client */
709 start_accepting_client(listencon, op, conp, otherside, lenp, tuplep)
710 CONNECTION listencon;
717 struct acc_data *arg;
722 * Make sure the supplied connection and operation are legal
724 GDB_CHECK_CON(listencon, "start_accepting_client")
725 GDB_CHECK_OP(op, "start_accepting_client")
727 arg = (struct acc_data *)db_alloc(sizeof(struct acc_data));
729 arg->otherside = otherside;
730 arg->otherlen = lenp;
732 *conp = NULL; /* in case we fail */
733 arg->listenop = create_operation();
734 arg->receiveop = create_operation();
735 arg->con = g_make_con();
736 arg->tuplep = tuplep;
737 *tuplep = NULL; /* in case we fail */
740 * Queue an operation ahead of us which will accept an fd and
741 * put it in arg->con->in. As a byproduct, pick up the from
742 * information that we return to the caller.
744 gdb_start_listening(arg->listenop, listencon,
746 arg->otherlen, &(arg->con->in.fd));
749 * Now queue us behind it. By the time we run our init routine,
750 * a connection should have been acquired.
752 initialize_operation(op, g_iacc, (char *)arg, (int (*)())NULL);
753 (void) queue_operation(listencon, CON_INPUT, op);
756 /*----------------------------------------------------------*/
760 /* Init routine for accepting a connection. By the
761 /* time this runs, the listen has been done, the
762 /* 'from' data put in position for the caller, and
763 /* the fd plugged into the connection descriptor.
764 /* If all went well, fill out the connection descriptor
765 /* and then requeue us on that to do the receive of
766 /* the requested tuple.
768 /*----------------------------------------------------------*/
771 g_iacc(op, hcon, arg)
773 HALF_CONNECTION hcon;
774 struct acc_data *arg;
776 register CONNECTION con = arg->con;
779 * Set up 2nd init routine for after we re-queue ourselves
781 op->fcn.cont = g_i2acc;
783 * See whether we successfully accepted a connection. If
784 * not, we just cancel ourselves. If so, fill out the
785 * connection descriptor and related data structures properly,
786 * then requeue ourselves on the new connection.
788 if (OP_STATUS(arg->listenop) != OP_COMPLETE ||
789 OP_RESULT(arg->listenop) != OP_SUCCESS ||
791 (void) sever_connection(con);
793 op->result = OP_CANCELLED;
798 * OK, we got an fd, but the connection and related structures
799 * aren't really set up straight, and the fd must be put
800 * into non-blocking mode. There really should be a common
801 * routine for this, since some of the logic exists in 2
804 con->status = CON_STARTING;
805 con->out.fd = con->in.fd;
806 g_ver_iprotocol(con); /* make sure we're at */
807 /* same level of protocol */
808 if (con->status == CON_UP) {
810 * We've successfully started the connection, now mark
811 * it for non-blocking I/O. Also, update the high water
812 * mark of fd's controlled by our system.
815 if(ioctl(con->in.fd, FIONBIO, (char *)&nb)== (-1)) {
816 g_stop_with_errno(con);
817 *arg->conp = con; /* give failed con to */
818 /* caller so he can find */
820 gdb_perror("gdb: ioctl for non-block failed");
822 op->result = OP_CANCELLED; /* we didn't really, but */
823 /* we want caller to look */
824 /* at the connection so he */
828 if (con->in.fd +1 > gdb_mfd)
829 gdb_mfd = con->in.fd + 1;
831 * Allocate a buffer, if necessary, and reset buffer pointers
832 * so first request will result in a long read into the buffer
834 g_allocate_connection_buffers(con);
837 *arg->conp = con; /* give failed con to */
838 /* caller so he can find */
841 op->result = OP_CANCELLED;
846 * Before we requeue ourselves on the new connection, queue
847 * up a receive for the expected tuple. Then we'll be
848 * sure that it's there by the time we run.
850 start_receiving_object(arg->receiveop, con, (char *)(arg->tuplep),
853 * Requeue ourselves behind the receive operation.
856 (void) requeue_operation(con, CON_INPUT, op);
862 /*----------------------------------------------------------*/
866 /* Second init routine for accepting a connection.
867 /* This one is run after the operation is requeued on
868 /* the new connection. By the time we run here, the
869 /* attempt to receive the tuple has already been made.
870 /* We just check on status and clean-up.
872 /*----------------------------------------------------------*/
875 g_i2acc(op, hcon, arg)
877 HALF_CONNECTION hcon;
878 struct acc_data *arg;
882 rc = OP_STATUS(arg->receiveop); /* if it completes, then */
884 *arg->conp = arg->con; /* give caller the new con */
885 if (rc != OP_COMPLETE)
886 (void) g_stop_connection(arg->con);
888 * Release all transient data structures.
895 /*----------------------------------------------------------*/
899 /* Free all data structures used by start_accepting_client.
901 /*----------------------------------------------------------*/
905 struct acc_data *arg;
907 delete_operation(arg->listenop);
908 delete_operation(arg->receiveop);
909 db_free((char *)arg, sizeof(struct acc_data));