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
39 /* For copying and distribution information, please see
40 /* the file <mit-copyright.h>.
42 /* These routines provide a suite of asynchronous operations
45 /************************************************************************/
47 #include <mit-copyright.h>
50 #include <netinet/in.h>
51 #include <sys/ioctl.h>
53 extern u_long htonl();
56 /************************************************************************/
58 /* send_object (send_object)
60 /* Synchronous form of start_sending_object. Returns either
61 /* OP_CANCELLED, or OP_RESULT(op).
63 /************************************************************************/
66 send_object(con, objp, type)
71 register OPERATION op;
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);
81 retval = OP_STATUS(op);
86 /************************************************************************/
88 /* start_send_object (g_snobj)
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.
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.
107 /* The data is preceded by its length expressed as a long in
108 /* network byte order.
110 /************************************************************************/
113 char *objp; /* address of the object to */
115 int type; /* type code for the object */
117 char *flattened; /* address of first byte */
118 /* of flattened data */
119 int len; /* length of the flattened */
127 start_sending_object(op, con, objp, type)
133 struct obj_data *arg;
136 * Make sure the supplied connection is a legal one
138 GDB_CHECK_CON(con, "start_sending_object")
139 GDB_CHECK_OP(op, "start_sending_object")
141 arg = (struct obj_data *)db_alloc(sizeof(struct obj_data));
145 initialize_operation(op, g_isnobj, (char *)arg, (int (*)())NULL);
146 (void) queue_operation(con, CON_OUTPUT, op);
149 /*----------------------------------------------------------*/
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.
162 /*----------------------------------------------------------*/
165 g_isnobj(op, hcon, arg)
167 HALF_CONNECTION hcon;
168 struct obj_data *arg;
171 * Find out the encoded length of the data
173 arg->len = FCN_PROPERTY(arg->type, CODED_LENGTH_PROPERTY)
177 * Allocate space and flatten (encode) the data
179 arg->flattened = db_alloc(arg->len+sizeof(long));
180 *(u_long *)arg->flattened = htonl((u_long)arg->len);
182 FCN_PROPERTY(arg->type, ENCODE_PROPERTY)
183 (arg->objp, hcon, arg->flattened+sizeof(long));
186 * Set up continuation routine in case it's needed after the return
188 op->fcn.cont = g_csnobj;
191 * Start sending the data, maybe even complete
193 if (gdb_send_data(hcon, arg->flattened, arg->len + sizeof(long)) ==
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. */
214 /*----------------------------------------------------------*/
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.
223 /*----------------------------------------------------------*/
227 g_csnobj(op, hcon, arg)
229 HALF_CONNECTION hcon;
230 struct obj_data *arg;
232 op->result = OP_SUCCESS;
233 db_free((char *)arg->flattened, arg->len + sizeof(long));
234 /* free the sent data */
235 db_free((char *)arg, sizeof(struct obj_data)); /* free the state structure */
240 /************************************************************************/
242 /* receive_object (receive_object)
244 /* Synchronous form of start_receiving_object. Returns either
245 /* OP_CANCELLED, or OP_RESULT(op).
247 /************************************************************************/
250 receive_object(con, objp, type)
255 register OPERATION op;
258 op = create_operation();
259 start_receiving_object(op, con, objp, type);
260 (void) complete_operation(op);
261 if (OP_STATUS(op) == OP_COMPLETE)
262 retval = OP_RESULT(op);
264 retval = OP_STATUS(op);
265 delete_operation(op);
269 /************************************************************************/
271 /* start_receiving_object (g_rcobj)
273 /* Start the asynchronous receipt of a gdb object. Note that this
274 /* routine must be passed the address of the object, not the object
275 /* itself. In the case of structured objects, this routine may
276 /* allocate the necessary storage. The work to build the object is
277 /* done by the object's decode routine.
279 /* The following three routines work together, and may be considered
280 /* as a single entity implementing the operation. The first merely
281 /* saves away its arguments and queues the operation on the designated
282 /* connection. These stay there until they percolate to the head of
283 /* the queue. The second is the initialization routine, which is
284 /* called by the connection maintenance logic when the operation
285 /* first reaches the head of the queue. This routine initiates a read
286 /* for the length of the object, and sets up a continuation routine
287 /* to read the object itself. When the object itself has been read, it
288 /* is decoded and the operation completes.
290 /* The data is preceded by its length expressed as a long in
291 /* network byte order.
293 /* preempt_and_start_receiving_object (g_prcobj)
295 /* Similar to above, but may be called only from an active operation
296 /* (i.e. an init or continue routine) on an inbound half connection.
297 /* The receive effectively pre-empts the old operation, which wil
298 /* continue after the receive is done.
301 /************************************************************************/
304 char *objp; /* address of the object to */
306 int type; /* type code for the object */
308 char *flattened; /* address of first byte */
309 /* of flattened data */
310 int len; /* length of the flattened */
318 /*----------------------------------------------------------*/
320 /* start_receiving_object
322 /*----------------------------------------------------------*/
325 start_receiving_object(op, con, objp, type)
331 struct robj_data *arg;
334 * Make sure the supplied connection is a legal one
336 GDB_CHECK_CON(con, "start_receiving_object")
337 GDB_CHECK_OP(op, "start_receiving_object")
339 arg = (struct robj_data *)db_alloc(sizeof(struct robj_data));
343 initialize_operation(op, g_ircobj, (char *)arg, (int (*)())NULL);
344 (void) queue_operation(con, CON_INPUT, op);
347 /*----------------------------------------------------------*/
349 /* preempt_and_start_receiving_object
351 /*----------------------------------------------------------*/
354 preempt_and_start_receiving_object(op, oldop, objp, type)
360 struct robj_data *arg;
363 * Make sure the supplied connection is a legal one
365 GDB_CHECK_OP(op, "preempt_and_start_receiving_object")
366 GDB_CHECK_OP(oldop, "preempt_and_start_receiving_object")
368 arg = (struct robj_data *)db_alloc(sizeof(struct robj_data));
372 initialize_operation(op, g_ircobj, (char *)arg, (int (*)())NULL);
373 (void) g_preempt_me(oldop, op);
376 /*----------------------------------------------------------*/
380 /* Initialization routine for receiving an object.
381 /* Called when the receive operation percolates to the
382 /* top of the queue. First, we must receive the single
383 /* 'long' which carries the length of the rest of the data.
384 /* We do that now, either synchronously or asynchronously.
386 /*----------------------------------------------------------*/
389 g_ircobj(op, hcon, arg)
391 HALF_CONNECTION hcon;
392 struct robj_data *arg;
394 op->fcn.cont = g_c1rcobj;
395 if(gdb_receive_data(hcon, (char *)&(arg->len), sizeof(long)) == OP_COMPLETE) {
396 return g_c1rcobj(op, hcon, arg);/* this return is a little */
397 /* subtle. As continuation */
398 /* routines call each other */
399 /* synchronously, the last */
400 /* one determines whether we */
401 /* completed or are still */
402 /* running. That status */
403 /* percolates back through */
404 /* the entire call chain. */
410 /*----------------------------------------------------------*/
414 /* At this point, we have received the length. Now, allocate
415 /* the space for the rest of the data, and start receiving
418 /*----------------------------------------------------------*/
421 g_c1rcobj(op, hcon, arg)
423 HALF_CONNECTION hcon;
424 struct robj_data *arg;
427 extern u_long ntohl();
431 * Now we know the length of the encoded data, convert the length
432 * to local byte order, and allocate the space for the receive.
434 arg->len = (int) ntohl((u_long)arg->len);
435 if (arg->len > 65536)
438 arg->flattened = db_alloc(arg->len);
439 if (arg->flattened == NULL)
442 * Now start receiving the encoded object itself. If it all comes in
443 * synchronously, then just go on to the c2 routine to decode it and
444 * finish up. Else return OP_RUNNING, so the rest of the system
445 * can get some work done while we wait.
447 op->fcn.cont = g_c2rcobj;
448 if(gdb_receive_data(hcon, arg->flattened, arg->len ) == OP_COMPLETE) {
449 return g_c2rcobj(op, hcon, arg);
455 /*----------------------------------------------------------*/
459 /* At this point, all the data has been received. Decode
460 /* it into the place provided by the caller, free all
461 /* temporarily allocated memory, and return.
463 /*----------------------------------------------------------*/
466 g_c2rcobj(op, hcon, arg)
468 HALF_CONNECTION hcon;
469 struct robj_data *arg;
472 * Decode the received data into local representation.
474 FCN_PROPERTY(arg->type, DECODE_PROPERTY)
475 (arg->objp, hcon, arg->flattened);
476 op->result = OP_SUCCESS;
477 db_free(arg->flattened, arg->len); /* free the received data */
478 db_free((char *)arg, sizeof(struct robj_data)); /* free the state structure */
482 /************************************************************************/
484 /* complete_operation(complete_operation)
486 /* Wait for a given operation to complete, allowing everything
487 /* to progress in the meantime. Returns the last known status
488 /* of the operation, which in general will be OP_COMPLETE unless
489 /* errors were encountered (and this version of the code doesn't
490 /* do error handing right anyway!)
492 /* We do this by (1) calling gdb_progress to assure that all
493 /* possible progress has been made, which is always a good thing
494 /* to do when we get the chance and (2) looping on calls to
495 /* con_select, which will make all possible future progress,
496 /* but without burning cycles unnecessarily in the process.
498 /************************************************************************/
501 complete_operation(op)
504 (void) gdb_progress();
506 while(op->status != OP_COMPLETE && op->status != OP_CANCELLED)
507 (void) con_select(0, (fd_set *)NULL, (fd_set *)NULL,
508 (fd_set *)NULL, (struct timeval *)NULL);
515 /************************************************************************/
517 /* cancel_operation(cancel_operation)
519 /* Attempts to cancel an operation.
521 /************************************************************************/
527 register HALF_CONNECTION hcon = op->halfcon;
529 if (op->status != OP_RUNNING && op->status != OP_QUEUED)
533 GDB_GIVEUP("cancel_operation: operation is queued but half connection is unknown")
536 * If we're at the head of the queue and running, then we have to
537 * call the cancelation routine for this particular operation so
540 if (op->prev == (OPERATION)hcon) {
541 if (op->status == OP_RUNNING && op->cancel != NULL)
542 (*op->cancel)(op->halfcon, op->arg);
546 * Looks safe, now cancel it.
548 op->next->prev = op->prev; /* de-q it */
549 op->prev->next = op->next; /* " " " */
550 op->status = OP_CANCELLED;
556 /************************************************************************/
560 /* Start the asynchronous acquisition of a connection. This
561 /* results in the queuing of a GDB "OPERATION" to do the
562 /* requested listening.
564 /************************************************************************/
567 char *otherside; /* data returned from an */
569 int *otherlen; /* length of the otherside */
571 int *fdp; /* ptr to the fd of the */
580 gdb_start_listening(op, con, otherside, lenp, fdp)
587 struct lis_data *arg;
592 * Make sure the supplied connection is a legal one
594 GDB_CHECK_CON(con, "start_listening")
595 GDB_CHECK_OP(op, "start_listening")
597 arg = (struct lis_data *)db_alloc(sizeof(struct lis_data));
599 arg->otherside = otherside;
600 arg->otherlen = lenp;
602 initialize_operation(op, g_ilis, (char *)arg, (int (*)())NULL);
603 (void) queue_operation(con, CON_INPUT, op);
606 /*----------------------------------------------------------*/
610 /* Init routine for doing a listen.
612 /*----------------------------------------------------------*/
615 g_ilis(op, hcon, arg)
617 HALF_CONNECTION hcon;
618 struct lis_data *arg;
623 * Set up continuation routine in case it's needed after the return
625 op->fcn.cont = g_clis;
628 * Try doing the listen now, and then decide whether to go
629 * right on to the continuation routine or to let things hang
632 rc = gdb_start_a_listen(hcon, arg->otherside, arg->otherlen, arg->fdp);
633 if (rc==OP_COMPLETE) {
634 return g_clis(op, hcon, arg); /* this return is a little */
635 /* subtle. As continuation */
636 /* routines call each other */
637 /* synchronously, the last */
638 /* one determines whether we */
639 /* completed or are still */
640 /* running. That status */
641 /* percolates back through */
642 /* the entire call chain. */
650 /*----------------------------------------------------------*/
654 /* Continuation routine for accepting a connection.
656 /* At this point, the fd has been accepted and all
657 /* the necessary information given back to the caller.
659 /*----------------------------------------------------------*/
663 g_clis(op, hcon, arg)
665 HALF_CONNECTION hcon;
666 struct lis_data *arg;
668 op->result = OP_SUCCESS;
669 db_free((char *)arg, sizeof(struct lis_data));
670 /* free the state structure */
675 /************************************************************************/
677 /* start_accepting_client
679 /* Start the asynchronous acquisition of a client. This queueable
680 /* operation first tries to accept a connection. On this connection,
681 /* it reads a startup string from the client, and then completes.
683 /* The return values from this are not quite what you might expect.
684 /* In general, the operation will show complete, rather than cancelled,
685 /* if it gets as far as creating the new connection at all. If
686 /* subsequent activities result in errors from system calls, then
687 /* this operation will complete with a status of OP_COMPLETE and a
688 /* result of OP_CANCELLED. In this case, the applications IS given
689 /* a connection descriptor for the new connection, and that descriptor
690 /* has an errno value indicating why the failure occurred. The
691 /* caller must then sever this connection to free the descriptor.
693 /************************************************************************/
696 char *otherside; /* data returned from an */
698 int *otherlen; /* length of the otherside */
700 OPERATION listenop; /* used to listen for */
702 OPERATION receiveop; /* used when receiving */
703 /* tuple from the client */
704 CONNECTION con; /* the connection we're */
705 /* trying to create */
706 CONNECTION *conp; /* this is where the caller */
707 /* wants the connection */
709 TUPLE *tuplep; /* pointer to tuple we */
710 /* are going to receive */
711 /* from new client */
718 start_accepting_client(listencon, op, conp, otherside, lenp, tuplep)
719 CONNECTION listencon;
726 struct acc_data *arg;
731 * Make sure the supplied connection and operation are legal
733 GDB_CHECK_CON(listencon, "start_accepting_client")
734 GDB_CHECK_OP(op, "start_accepting_client")
736 arg = (struct acc_data *)db_alloc(sizeof(struct acc_data));
738 arg->otherside = otherside;
739 arg->otherlen = lenp;
741 *conp = NULL; /* in case we fail */
742 arg->listenop = create_operation();
743 arg->receiveop = create_operation();
744 arg->con = g_make_con();
745 arg->tuplep = tuplep;
746 *tuplep = NULL; /* in case we fail */
749 * Queue an operation ahead of us which will accept an fd and
750 * put it in arg->con->in. As a byproduct, pick up the from
751 * information that we return to the caller.
753 gdb_start_listening(arg->listenop, listencon,
755 arg->otherlen, &(arg->con->in.fd));
758 * Now queue us behind it. By the time we run our init routine,
759 * a connection should have been acquired.
761 initialize_operation(op, g_iacc, (char *)arg, (int (*)())NULL);
762 (void) queue_operation(listencon, CON_INPUT, op);
765 /*----------------------------------------------------------*/
769 /* Init routine for accepting a connection. By the
770 /* time this runs, the listen has been done, the
771 /* 'from' data put in position for the caller, and
772 /* the fd plugged into the connection descriptor.
773 /* If all went well, fill out the connection descriptor
774 /* and then requeue us on that to do the receive of
775 /* the requested tuple.
777 /*----------------------------------------------------------*/
781 g_iacc(op, hcon, arg)
783 HALF_CONNECTION hcon;
784 struct acc_data *arg;
786 register CONNECTION con = arg->con;
789 * Set up 2nd init routine for after we re-queue ourselves
791 op->fcn.cont = g_i2acc;
793 * See whether we successfully accepted a connection. If
794 * not, we just cancel ourselves. If so, fill out the
795 * connection descriptor and related data structures properly,
796 * then requeue ourselves on the new connection.
798 if (OP_STATUS(arg->listenop) != OP_COMPLETE ||
799 OP_RESULT(arg->listenop) != OP_SUCCESS ||
801 (void) sever_connection(con);
803 op->result = OP_CANCELLED;
808 * OK, we got an fd, but the connection and related structures
809 * aren't really set up straight, and the fd must be put
810 * into non-blocking mode. There really should be a common
811 * routine for this, since some of the logic exists in 2
814 con->status = CON_STARTING;
815 con->out.fd = con->in.fd;
816 g_ver_iprotocol(con); /* make sure we're at */
817 /* same level of protocol */
818 if (con->status == CON_UP) {
820 * We've successfully started the connection, now mark
821 * it for non-blocking I/O. Also, update the high water
822 * mark of fd's controlled by our system.
825 if(ioctl(con->in.fd, FIONBIO, (char *)&nb)== (-1)) {
826 g_stop_with_errno(con);
827 *arg->conp = con; /* give failed con to */
828 /* caller so he can find */
830 gdb_perror("gdb: ioctl for non-block failed");
832 op->result = OP_CANCELLED; /* we didn't really, but */
833 /* we want caller to look */
834 /* at the connection so he */
838 if (con->in.fd +1 > gdb_mfd)
839 gdb_mfd = con->in.fd + 1;
841 * Allocate a buffer, if necessary, and reset buffer pointers
842 * so first request will result in a long read into the buffer
844 g_allocate_connection_buffers(con);
847 *arg->conp = con; /* give failed con to */
848 /* caller so he can find */
851 op->result = OP_CANCELLED;
856 * Before we requeue ourselves on the new connection, queue
857 * up a receive for the expected tuple. Then we'll be
858 * sure that it's there by the time we run.
860 start_receiving_object(arg->receiveop, con, (char *)(arg->tuplep),
863 * Requeue ourselves behind the receive operation.
866 (void) requeue_operation(con, CON_INPUT, op);
872 /*----------------------------------------------------------*/
876 /* Second init routine for accepting a connection.
877 /* This one is run after the operation is requeued on
878 /* the new connection. By the time we run here, the
879 /* attempt to receive the tuple has already been made.
880 /* We just check on status and clean-up.
882 /*----------------------------------------------------------*/
886 g_i2acc(op, hcon, arg)
888 HALF_CONNECTION hcon;
889 struct acc_data *arg;
893 rc = OP_STATUS(arg->receiveop); /* if it completes, then */
895 *arg->conp = arg->con; /* give caller the new con */
896 if (rc != OP_COMPLETE)
897 (void) g_stop_connection(arg->con);
899 * Release all transient data structures.
906 /*----------------------------------------------------------*/
910 /* Free all data structures used by start_accepting_client.
912 /*----------------------------------------------------------*/
916 struct acc_data *arg;
918 delete_operation(arg->listenop);
919 delete_operation(arg->receiveop);
920 db_free((char *)arg, sizeof(struct acc_data));