7 static char *rcsid_gdb_ops_c = "$Header$";
11 /************************************************************************
15 * GDB - Asynchronous Operations and Their Synchronous
18 * Author: Noah Mendelsohn
19 * Copyright: 1986 MIT Project Athena
20 * For copying and distribution information, please see
21 * the file <mit-copyright.h>.
23 * These routines provide a suite of asynchronous operations
26 ************************************************************************/
28 #include <mit-copyright.h>
31 #include <netinet/in.h>
32 #include <sys/ioctl.h>
34 #include <sys/filio.h>
37 extern u_long htonl();
41 /************************************************************************
43 * send_object (send_object)
45 * Synchronous form of start_sending_object. Returns either
46 * OP_CANCELLED, or OP_RESULT(op).
48 ************************************************************************/
51 send_object(con, objp, type)
56 register OPERATION op;
60 op = create_operation();
61 start_sending_object(op, con, objp, type);
62 (void) complete_operation(op);
63 if (OP_STATUS(op) == OP_COMPLETE)
64 retval = OP_RESULT(op);
66 retval = OP_STATUS(op);
72 /************************************************************************/
74 /* start_send_object (g_snobj)
76 /* Start the asynchronous transmission of a gdb object.
77 /* Note that this routine must be passed the address of the object,
78 /* not the object itself.
80 /* The following three routines work together, and may be considered
81 /* as a single entity implementing the operation. The first merely
82 /* saves away its arguments and queues the operation on the designated
83 /* connection. These stay there until they percolate to the head of
84 /* the queue. The second is the initialization routine, which is
85 /* called by the connection maintenance logic when the operation
86 /* first reaches the head of the queue. This routine encodes
87 /* the supplied data for transmission, and then sends it. If the
88 /* transmission executes synchronously, then the third routine is
89 /* called immediately to clean up. If not, the third routine is
90 /* marked as the 'continuation' routine, which will cause its
91 /* invocation when the transmission completes.
93 /* The data is preceded by its length expressed as a long in
94 /* network byte order.
96 /************************************************************************/
99 char *objp; /* address of the object to */
101 int type; /* type code for the object */
103 char *flattened; /* address of first byte */
104 /* of flattened data */
105 int len; /* length of the flattened */
113 start_sending_object(op, con, objp, type)
119 struct obj_data *arg;
122 * Make sure the supplied connection is a legal one
124 GDB_CHECK_CON(con, "start_sending_object")
125 GDB_CHECK_OP(op, "start_sending_object")
127 arg = (struct obj_data *)db_alloc(sizeof(struct obj_data));
131 initialize_operation(op, g_isnobj, (char *)arg, (int (*)())NULL);
132 (void) queue_operation(con, CON_OUTPUT, op);
135 /*----------------------------------------------------------*/
139 /* Init routine for sending an object. This routine is
140 /* called by the connection management logic when the send
141 /* request percolates to the top of the queue. This routine
142 /* reformats the data into an appropriate form for transmission.
143 /* The format used is a length, represented as a long in
144 /* network byte order, followed by the data itself. The
145 /* continuation routine below is called, either synchronously
146 /* or asynchronously, once the transmission is complete.
148 /*----------------------------------------------------------*/
151 g_isnobj(op, hcon, arg)
153 HALF_CONNECTION hcon;
154 struct obj_data *arg;
157 * Find out the encoded length of the data
159 arg->len = FCN_PROPERTY(arg->type, CODED_LENGTH_PROPERTY)
163 * Allocate space and flatten (encode) the data
165 arg->flattened = db_alloc(arg->len+sizeof(long));
166 *(u_long *)arg->flattened = htonl((u_long)arg->len);
168 FCN_PROPERTY(arg->type, ENCODE_PROPERTY)
169 (arg->objp, hcon, arg->flattened+sizeof(long));
172 * Set up continuation routine in case it's needed after the return
174 op->fcn.cont = g_csnobj;
177 * Start sending the data, maybe even complete
179 if (gdb_send_data(hcon, arg->flattened, arg->len + sizeof(long)) ==
181 return g_csnobj(op, hcon, arg) ;/* this return is a little */
182 /* subtle. As continuation */
183 /* routines call each other */
184 /* synchronously, the last */
185 /* one determines whether we */
186 /* completed or are still */
187 /* running. That status */
188 /* percolates back through */
189 /* the entire call chain. */
200 /*----------------------------------------------------------*/
204 /* Continuation routine for sending an object. Since there is
205 /* only one transmission, started by the init routine, this is
206 /* called when that transmission is done, and it does all the
207 /* associated clean up.
209 /*----------------------------------------------------------*/
213 g_csnobj(op, hcon, arg)
215 HALF_CONNECTION hcon;
216 struct obj_data *arg;
218 op->result = OP_SUCCESS;
219 db_free((char *)arg->flattened, arg->len + sizeof(long));
220 /* free the sent data */
221 db_free((char *)arg, sizeof(struct obj_data)); /* free the state structure */
226 /************************************************************************/
228 /* receive_object (receive_object)
230 /* Synchronous form of start_receiving_object. Returns either
231 /* OP_CANCELLED, or OP_RESULT(op).
233 /************************************************************************/
236 receive_object(con, objp, type)
241 register OPERATION op;
244 op = create_operation();
245 start_receiving_object(op, con, objp, type);
246 (void) complete_operation(op);
247 if (OP_STATUS(op) == OP_COMPLETE)
248 retval = OP_RESULT(op);
250 retval = OP_STATUS(op);
251 delete_operation(op);
256 /************************************************************************/
258 /* start_receiving_object (g_rcobj)
260 /* Start the asynchronous receipt of a gdb object. Note that this
261 /* routine must be passed the address of the object, not the object
262 /* itself. In the case of structured objects, this routine may
263 /* allocate the necessary storage. The work to build the object is
264 /* done by the object's decode routine.
266 /* The following three routines work together, and may be considered
267 /* as a single entity implementing the operation. The first merely
268 /* saves away its arguments and queues the operation on the designated
269 /* connection. These stay there until they percolate to the head of
270 /* the queue. The second is the initialization routine, which is
271 /* called by the connection maintenance logic when the operation
272 /* first reaches the head of the queue. This routine initiates a read
273 /* for the length of the object, and sets up a continuation routine
274 /* to read the object itself. When the object itself has been read, it
275 /* is decoded and the operation completes.
277 /* The data is preceded by its length expressed as a long in
278 /* network byte order.
280 /* preempt_and_start_receiving_object (g_prcobj)
282 /* Similar to above, but may be called only from an active operation
283 /* (i.e. an init or continue routine) on an inbound half connection.
284 /* The receive effectively pre-empts the old operation, which wil
285 /* continue after the receive is done.
288 /************************************************************************/
291 char *objp; /* address of the object to */
293 int type; /* type code for the object */
295 char *flattened; /* address of first byte */
296 /* of flattened data */
297 int len; /* length of the flattened */
305 /*----------------------------------------------------------*/
307 /* start_receiving_object
309 /*----------------------------------------------------------*/
312 start_receiving_object(op, con, objp, type)
318 struct robj_data *arg;
321 * Make sure the supplied connection is a legal one
323 GDB_CHECK_CON(con, "start_receiving_object")
324 GDB_CHECK_OP(op, "start_receiving_object")
326 arg = (struct robj_data *)db_alloc(sizeof(struct robj_data));
330 initialize_operation(op, g_ircobj, (char *)arg, (int (*)())NULL);
331 (void) queue_operation(con, CON_INPUT, op);
334 /*----------------------------------------------------------*/
336 /* preempt_and_start_receiving_object
338 /*----------------------------------------------------------*/
341 preempt_and_start_receiving_object(op, oldop, objp, type)
347 struct robj_data *arg;
350 * Make sure the supplied connection is a legal one
352 GDB_CHECK_OP(op, "preempt_and_start_receiving_object")
353 GDB_CHECK_OP(oldop, "preempt_and_start_receiving_object")
355 arg = (struct robj_data *)db_alloc(sizeof(struct robj_data));
359 initialize_operation(op, g_ircobj, (char *)arg, (int (*)())NULL);
360 (void) g_preempt_me(oldop, op);
363 /*----------------------------------------------------------*/
367 /* Initialization routine for receiving an object.
368 /* Called when the receive operation percolates to the
369 /* top of the queue. First, we must receive the single
370 /* 'long' which carries the length of the rest of the data.
371 /* We do that now, either synchronously or asynchronously.
373 /*----------------------------------------------------------*/
376 g_ircobj(op, hcon, arg)
378 HALF_CONNECTION hcon;
379 struct robj_data *arg;
381 op->fcn.cont = g_c1rcobj;
382 if(gdb_receive_data(hcon, (char *)&(arg->len), sizeof(long)) == OP_COMPLETE) {
383 return g_c1rcobj(op, hcon, arg);/* this return is a little */
384 /* subtle. As continuation */
385 /* routines call each other */
386 /* synchronously, the last */
387 /* one determines whether we */
388 /* completed or are still */
389 /* running. That status */
390 /* percolates back through */
391 /* the entire call chain. */
397 /*----------------------------------------------------------*/
401 /* At this point, we have received the length. Now, allocate
402 /* the space for the rest of the data, and start receiving
405 /*----------------------------------------------------------*/
408 g_c1rcobj(op, hcon, arg)
410 HALF_CONNECTION hcon;
411 struct robj_data *arg;
414 extern u_long ntohl();
418 * Now we know the length of the encoded data, convert the length
419 * to local byte order, and allocate the space for the receive.
421 arg->len = (int) ntohl((u_long)arg->len);
422 if (arg->len > 65536)
425 arg->flattened = db_alloc(arg->len);
426 if (arg->flattened == NULL)
429 * Now start receiving the encoded object itself. If it all comes in
430 * synchronously, then just go on to the c2 routine to decode it and
431 * finish up. Else return OP_RUNNING, so the rest of the system
432 * can get some work done while we wait.
434 op->fcn.cont = g_c2rcobj;
435 if(gdb_receive_data(hcon, arg->flattened, arg->len ) == OP_COMPLETE) {
436 return g_c2rcobj(op, hcon, arg);
442 /*----------------------------------------------------------*/
446 /* At this point, all the data has been received. Decode
447 /* it into the place provided by the caller, free all
448 /* temporarily allocated memory, and return.
450 /*----------------------------------------------------------*/
453 g_c2rcobj(op, hcon, arg)
455 HALF_CONNECTION hcon;
456 struct robj_data *arg;
459 * Decode the received data into local representation.
461 FCN_PROPERTY(arg->type, DECODE_PROPERTY)
462 (arg->objp, hcon, arg->flattened);
463 op->result = OP_SUCCESS;
464 db_free(arg->flattened, arg->len); /* free the received data */
465 db_free((char *)arg, sizeof(struct robj_data)); /* free the state structure */
470 /************************************************************************/
472 /* complete_operation(complete_operation)
474 /* Wait for a given operation to complete, allowing everything
475 /* to progress in the meantime. Returns the last known status
476 /* of the operation, which in general will be OP_COMPLETE unless
477 /* errors were encountered (and this version of the code doesn't
478 /* do error handing right anyway!)
480 /* We do this by (1) calling gdb_progress to assure that all
481 /* possible progress has been made, which is always a good thing
482 /* to do when we get the chance and (2) looping on calls to
483 /* con_select, which will make all possible future progress,
484 /* but without burning cycles unnecessarily in the process.
486 /************************************************************************/
489 complete_operation(op)
492 (void) gdb_progress();
494 while(op->status != OP_COMPLETE && op->status != OP_CANCELLED)
495 (void) con_select(0, (fd_set *)NULL, (fd_set *)NULL,
496 (fd_set *)NULL, (struct timeval *)NULL);
503 /************************************************************************/
505 /* cancel_operation(cancel_operation)
507 /* Attempts to cancel an operation.
509 /************************************************************************/
515 register HALF_CONNECTION hcon = op->halfcon;
517 if (op->status != OP_RUNNING && op->status != OP_QUEUED)
521 GDB_GIVEUP("cancel_operation: operation is queued but half connection is unknown")
524 * If we're at the head of the queue and running, then we have to
525 * call the cancelation routine for this particular operation so
528 if (op->prev == (OPERATION)hcon) {
529 if (op->status == OP_RUNNING && op->cancel != NULL)
530 (*op->cancel)(op->halfcon, op->arg);
534 * Looks safe, now cancel it.
536 op->next->prev = op->prev; /* de-q it */
537 op->prev->next = op->next; /* " " " */
538 op->status = OP_CANCELLED;
545 /************************************************************************/
549 /* Start the asynchronous acquisition of a connection. This
550 /* results in the queuing of a GDB "OPERATION" to do the
551 /* requested listening.
553 /************************************************************************/
556 char *otherside; /* data returned from an */
558 int *otherlen; /* length of the otherside */
560 int *fdp; /* ptr to the fd of the */
569 gdb_start_listening(op, con, otherside, lenp, fdp)
576 struct lis_data *arg;
581 * Make sure the supplied connection is a legal one
583 GDB_CHECK_CON(con, "start_listening")
584 GDB_CHECK_OP(op, "start_listening")
586 arg = (struct lis_data *)db_alloc(sizeof(struct lis_data));
588 arg->otherside = otherside;
589 arg->otherlen = lenp;
591 initialize_operation(op, g_ilis, (char *)arg, (int (*)())NULL);
592 (void) queue_operation(con, CON_INPUT, op);
595 /*----------------------------------------------------------*/
599 /* Init routine for doing a listen.
601 /*----------------------------------------------------------*/
604 g_ilis(op, hcon, arg)
606 HALF_CONNECTION hcon;
607 struct lis_data *arg;
612 * Set up continuation routine in case it's needed after the return
614 op->fcn.cont = g_clis;
617 * Try doing the listen now, and then decide whether to go
618 * right on to the continuation routine or to let things hang
621 rc = gdb_start_a_listen(hcon, arg->otherside, arg->otherlen, arg->fdp);
622 if (rc==OP_COMPLETE) {
623 return g_clis(op, hcon, arg); /* this return is a little */
624 /* subtle. As continuation */
625 /* routines call each other */
626 /* synchronously, the last */
627 /* one determines whether we */
628 /* completed or are still */
629 /* running. That status */
630 /* percolates back through */
631 /* the entire call chain. */
639 /*----------------------------------------------------------*/
643 /* Continuation routine for accepting a connection.
645 /* At this point, the fd has been accepted and all
646 /* the necessary information given back to the caller.
648 /*----------------------------------------------------------*/
652 g_clis(op, hcon, arg)
654 HALF_CONNECTION hcon;
655 struct lis_data *arg;
657 op->result = OP_SUCCESS;
658 db_free((char *)arg, sizeof(struct lis_data));
659 /* free the state structure */
664 /************************************************************************/
666 /* start_accepting_client
668 /* Start the asynchronous acquisition of a client. This queueable
669 /* operation first tries to accept a connection. On this connection,
670 /* it reads a startup string from the client, and then completes.
672 /* The return values from this are not quite what you might expect.
673 /* In general, the operation will show complete, rather than cancelled,
674 /* if it gets as far as creating the new connection at all. If
675 /* subsequent activities result in errors from system calls, then
676 /* this operation will complete with a status of OP_COMPLETE and a
677 /* result of OP_CANCELLED. In this case, the applications IS given
678 /* a connection descriptor for the new connection, and that descriptor
679 /* has an errno value indicating why the failure occurred. The
680 /* caller must then sever this connection to free the descriptor.
682 /************************************************************************/
685 char *otherside; /* data returned from an */
687 int *otherlen; /* length of the otherside */
689 OPERATION listenop; /* used to listen for */
691 OPERATION receiveop; /* used when receiving */
692 /* tuple from the client */
693 CONNECTION con; /* the connection we're */
694 /* trying to create */
695 CONNECTION *conp; /* this is where the caller */
696 /* wants the connection */
698 TUPLE *tuplep; /* pointer to tuple we */
699 /* are going to receive */
700 /* from new client */
707 start_accepting_client(listencon, op, conp, otherside, lenp, tuplep)
708 CONNECTION listencon;
715 struct acc_data *arg;
720 * Make sure the supplied connection and operation are legal
722 GDB_CHECK_CON(listencon, "start_accepting_client")
723 GDB_CHECK_OP(op, "start_accepting_client")
725 arg = (struct acc_data *)db_alloc(sizeof(struct acc_data));
727 arg->otherside = otherside;
728 arg->otherlen = lenp;
730 *conp = NULL; /* in case we fail */
731 arg->listenop = create_operation();
732 arg->receiveop = create_operation();
733 arg->con = g_make_con();
734 arg->tuplep = tuplep;
735 *tuplep = NULL; /* in case we fail */
738 * Queue an operation ahead of us which will accept an fd and
739 * put it in arg->con->in. As a byproduct, pick up the from
740 * information that we return to the caller.
742 gdb_start_listening(arg->listenop, listencon,
744 arg->otherlen, &(arg->con->in.fd));
747 * Now queue us behind it. By the time we run our init routine,
748 * a connection should have been acquired.
750 initialize_operation(op, g_iacc, (char *)arg, (int (*)())NULL);
751 (void) queue_operation(listencon, CON_INPUT, op);
754 /*----------------------------------------------------------*/
758 /* Init routine for accepting a connection. By the
759 /* time this runs, the listen has been done, the
760 /* 'from' data put in position for the caller, and
761 /* the fd plugged into the connection descriptor.
762 /* If all went well, fill out the connection descriptor
763 /* and then requeue us on that to do the receive of
764 /* the requested tuple.
766 /*----------------------------------------------------------*/
770 g_iacc(op, hcon, arg)
772 HALF_CONNECTION hcon;
773 struct acc_data *arg;
775 register CONNECTION con = arg->con;
778 * Set up 2nd init routine for after we re-queue ourselves
780 op->fcn.cont = g_i2acc;
782 * See whether we successfully accepted a connection. If
783 * not, we just cancel ourselves. If so, fill out the
784 * connection descriptor and related data structures properly,
785 * then requeue ourselves on the new connection.
787 if (OP_STATUS(arg->listenop) != OP_COMPLETE ||
788 OP_RESULT(arg->listenop) != OP_SUCCESS ||
790 (void) sever_connection(con);
792 op->result = OP_CANCELLED;
797 * OK, we got an fd, but the connection and related structures
798 * aren't really set up straight, and the fd must be put
799 * into non-blocking mode. There really should be a common
800 * routine for this, since some of the logic exists in 2
803 con->status = CON_STARTING;
804 con->out.fd = con->in.fd;
805 g_ver_iprotocol(con); /* make sure we're at */
806 /* same level of protocol */
807 if (con->status == CON_UP) {
809 * We've successfully started the connection, now mark
810 * it for non-blocking I/O. Also, update the high water
811 * mark of fd's controlled by our system.
814 if(ioctl(con->in.fd, FIONBIO, (char *)&nb)== (-1)) {
815 g_stop_with_errno(con);
816 *arg->conp = con; /* give failed con to */
817 /* caller so he can find */
819 gdb_perror("gdb: ioctl for non-block failed");
821 op->result = OP_CANCELLED; /* we didn't really, but */
822 /* we want caller to look */
823 /* at the connection so he */
827 if (con->in.fd +1 > gdb_mfd)
828 gdb_mfd = con->in.fd + 1;
830 * Allocate a buffer, if necessary, and reset buffer pointers
831 * so first request will result in a long read into the buffer
833 g_allocate_connection_buffers(con);
836 *arg->conp = con; /* give failed con to */
837 /* caller so he can find */
840 op->result = OP_CANCELLED;
845 * Before we requeue ourselves on the new connection, queue
846 * up a receive for the expected tuple. Then we'll be
847 * sure that it's there by the time we run.
849 start_receiving_object(arg->receiveop, con, (char *)(arg->tuplep),
852 * Requeue ourselves behind the receive operation.
855 (void) requeue_operation(con, CON_INPUT, op);
861 /*----------------------------------------------------------*/
865 /* Second init routine for accepting a connection.
866 /* This one is run after the operation is requeued on
867 /* the new connection. By the time we run here, the
868 /* attempt to receive the tuple has already been made.
869 /* We just check on status and clean-up.
871 /*----------------------------------------------------------*/
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));