Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / lib / tt / slib / mp_rpc_implement.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 //%%  (c) Copyright 1993, 1994 Hewlett-Packard Company                  
24 //%%  (c) Copyright 1993, 1994 International Business Machines Corp.    
25 //%%  (c) Copyright 1993, 1994 Sun Microsystems, Inc.                   
26 //%%  (c) Copyright 1993, 1994 Novell, Inc.                             
27 //%%  $TOG: mp_rpc_implement.C /main/9 1999/10/14 18:42:58 mgreess $                                                    
28 /*
29  * mp_rpc_implement.C 1.40 29 Jul 1993
30  *
31  * mp_rpc_implement.cc -- implementation of RPC functions
32  *
33  * Copyright (c) 1990,1992 by Sun Microsystems, Inc.
34  * 
35  * This file implements the RPC wrapper functions for each RPC
36  * procedure number. The responsibility of each wrapper is to decode and
37  * encode arguments and results and invoke the right methods
38  * corresponding to each activity.
39  *
40  */
41
42 #include <stdio.h>
43 #include <errno.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <sys/param.h>
47 #include <sys/types.h>
48 #include <sys/stat.h>
49 #include <sys/uio.h>
50 #include <fcntl.h>
51 #include <unistd.h>
52 #if defined(linux)
53 # include <g++/minmax.h>
54 #else
55 # include <macros.h>
56 #endif
57 #include "tt_options.h"
58 #include "util/tt_xdr_version.h"
59 #include <rpc/rpc.h>
60 #include <limits.h>
61 #include "mp/mp_global.h"
62 #include "mp_s_file.h"
63 #include "mp_s_file_utils.h"
64 #include "mp/mp_mp.h"
65 #include "mp_s_mp.h"
66 #include "mp_s_message.h"
67 #include "mp_otype.h"
68 #include "mp_signature.h"
69 #include "mp/mp_arg.h"
70 #include "mp_s_msg_context.h"
71 #include "mp_s_pattern.h"
72 #include "mp_s_procid.h"
73 #include "mp/mp_rpc_fns.h"
74 #include "mp_rpc_implement.h"
75 #include "mp_s_session.h"
76 #include "mp/mp_xdr_functions.h"
77 #include "mp_s_xdr_functions.h"
78 #include "util/tt_trace.h"
79 #include "mp_typedb.h"
80
81 #if defined(OPT_SECURE_RPC)
82
83 static  uid_t   req_auth_uid;
84 static  gid_t   req_auth_gid;
85 static  short   req_auth_gidlen;
86
87 // authdes_getucred() in 4.x is declared to take int* instead of gid_t*
88 #if defined(OPT_BUG_SUNOS_4)
89 static  int     req_auth_gids[10];
90 #else
91 static  gid_t   req_auth_gids[NGROUPS_MAX];
92 #endif /* OPT_BUG_SUNOS_4 */
93
94 #endif /* OPT_SECURE_RPC */
95
96 #if defined(OPT_BUG_SUNOS_4)
97 #       define RPC_ARG_T        char *
98 #else
99 #       define RPC_ARG_T        caddr_t
100 #endif /* OPT_BUG_SUNOS_4 */
101
102 #if defined(OPT_BUG_SUNOS_5)
103         typedef (*Hack_getucred)(const struct authdes_cred *,
104                                  uid_t *,
105                                  gid_t *,
106                                  short *,
107                                  gid_t *);
108 #endif /* OPT_BUG_SUNOS_5 */
109
110 void _tt_rpc_nullproc(SVCXPRT *); 
111 void _tt_rpc_dispatch(SVCXPRT *);
112 void _tt_rpc_dispatch_with_context(SVCXPRT *);
113 void _tt_rpc_update_msg(SVCXPRT *);
114 void _tt_rpc_join_file(SVCXPRT *);
115 void _tt_rpc_quit_file(SVCXPRT *);
116 void _tt_rpc_join_session(SVCXPRT *);
117 void _tt_rpc_quit_session(SVCXPRT *);
118 void _tt_rpc_join_context(SVCXPRT *);
119 void _tt_rpc_quit_context(SVCXPRT *);
120 void _tt_rpc_next_message(SVCXPRT *);
121 void _tt_rpc_add_pattern(SVCXPRT *);
122 void _tt_rpc_add_pattern_with_context(SVCXPRT *);
123 void _tt_rpc_del_pattern(SVCXPRT *);
124 void _tt_rpc_declare_ptype(SVCXPRT *);
125 void _tt_rpc_set_fd_channel(SVCXPRT *);
126 void _tt_rpc_set_prop(SVCXPRT *);
127 void _tt_rpc_add_prop(SVCXPRT *);
128 void _tt_rpc_get_prop(SVCXPRT *);
129 void _tt_rpc_prop_count(SVCXPRT *);
130 void _tt_rpc_prop_name(SVCXPRT *);
131 void _tt_rpc_prop_names_count(SVCXPRT *);
132 void _tt_rpc_alloc_procid_key(SVCXPRT *);
133 void _tt_rpc_close_procid(SVCXPRT *);
134 void _tt_rpc_otype_deriveds_count(SVCXPRT *);
135 void _tt_rpc_otype_derived(SVCXPRT *);
136 void _tt_rpc_otype_base(SVCXPRT *);
137 void _tt_rpc_otype_is_derived(SVCXPRT *);
138 void _tt_rpc_otype_osig_count(SVCXPRT *);
139 void _tt_rpc_otype_hsig_count(SVCXPRT *);
140 void _tt_rpc_otype_osig_op(SVCXPRT *);
141 void _tt_rpc_otype_hsig_op(SVCXPRT *);
142 void _tt_rpc_otype_osig_args_count(SVCXPRT *);
143 void _tt_rpc_otype_hsig_args_count(SVCXPRT *);
144 void _tt_rpc_otype_osig_arg_mode(SVCXPRT *);
145 void _tt_rpc_otype_hsig_arg_mode(SVCXPRT *);
146 void _tt_rpc_otype_osig_arg_type(SVCXPRT *);
147 void _tt_rpc_otype_hsig_arg_type(SVCXPRT *);
148 void _tt_rpc_hdispatch(SVCXPRT *);
149 void _tt_rpc_hupdate_msg(SVCXPRT *);
150 void _tt_rpc_get_omap(SVCXPRT *);
151 void _tt_rpc_add_omap(SVCXPRT *);
152 void _tt_rpc_dispatch_2(SVCXPRT *);
153 void _tt_rpc_dispatch_2_with_context(SVCXPRT *);
154 void _tt_rpc_update_msg_2(SVCXPRT *);
155 void _tt_rpc_msgread_2(SVCXPRT *);
156 void _tt_rpc_vrfy_session(SVCXPRT *);
157 void _tt_rpc_dispatch_on_exit(SVCXPRT *);
158 void _tt_rpc_undeclare_ptype(SVCXPRT *);
159 void _tt_rpc_exists_ptype(SVCXPRT *);
160 void _tt_rpc_unblock_ptype(SVCXPRT *);
161 void _tt_rpc_load_types(SVCXPRT *);
162
163
164 typedef void (*_Tt_rpc_stub)(SVCXPRT *);
165 /* 
166  * Dispatch table for rpc procedures. Note that the position of the
167  * dispatch functions must correspond to the _Tt_rpc_procedure number
168  * enum. 
169  */
170 const _Tt_rpc_stub      _tt_rpc_dispatch_table[TT_RPC_LAST] = {
171         0,                              /* 0 -  TT_RPC_NULLPROC */
172         _tt_rpc_dispatch,               /* 1 -  TT_RPC_DISPATCH */
173         _tt_rpc_update_msg,             /* 2 -  TT_RPC_UPDATE_MSG */
174         _tt_rpc_join_file,              /* 3 -  TT_RPC_JOIN_FILE */
175         _tt_rpc_quit_file,              /* 4 -  TT_RPC_QUIT_FILE */
176         _tt_rpc_join_session,           /* 5 -  TT_RPC_JOIN_SESSION */
177         _tt_rpc_quit_session,           /* 6 -  TT_RPC_QUIT_SESSION */
178         _tt_rpc_next_message,           /* 7 -  TT_RPC_NEXT_MESSAGE */
179         _tt_rpc_add_pattern,            /* 8 -  TT_RPC_ADD_PATTERN */
180         _tt_rpc_del_pattern,            /* 9 -  TT_RPC_DEL_PATTERN */
181         _tt_rpc_declare_ptype,          /* 10 - TT_RPC_DECLARE_PTYPE */
182         _tt_rpc_set_fd_channel,         /* 11 - TT_RPC_SET_FD_CHANNEL */
183         _tt_rpc_set_prop,               /* 12 - TT_RPC_SET_PROP */
184         _tt_rpc_add_prop,               /* 13 - TT_RPC_ADD_PROP */
185         _tt_rpc_get_prop,               /* 14 - TT_RPC_GET_PROP */
186         _tt_rpc_prop_count,             /* 15 - TT_RPC_PROP_COUNT */
187         _tt_rpc_prop_name,              /* 16 - TT_RPC_PROP_NAME */
188         _tt_rpc_prop_names_count,       /* 17 - TT_RPC_PROP_NAMES_COUNT */
189         _tt_rpc_alloc_procid_key,       /* 18 - TT_RPC_ALLOC_PROCID_KEY */
190         _tt_rpc_close_procid,           /* 19 - TT_RPC_CLOSE_PROCID */
191         _tt_rpc_otype_deriveds_count,   /* 20 - TT_RPC_OTYPE_DERIVEDS_COUNT */
192         _tt_rpc_otype_derived,          /* 21 - TT_RPC_OTYPE_DERIVED */
193         _tt_rpc_otype_base,             /* 22 - TT_RPC_OTYPE_BASE */
194         _tt_rpc_otype_is_derived,       /* 23 - TT_RPC_OTYPE_IS_DERIVED */
195         _tt_rpc_otype_osig_count,       /* 24 - TT_RPC_OTYPE_OSIG_COUNT */
196         _tt_rpc_otype_hsig_count,       /* 25 - TT_RPC_OTYPE_HSIG_COUNT */
197         _tt_rpc_otype_osig_op,          /* 26 - TT_RPC_OTYPE_OSIG_OP */
198         _tt_rpc_otype_hsig_op,          /* 27 - TT_RPC_OTYPE_HSIG_OP */
199         _tt_rpc_otype_osig_args_count,  /* 28 - TT_RPC_OTYPE_OSIG_ARGS_COUNT */
200         _tt_rpc_otype_hsig_args_count,  /* 29 - TT_RPC_OTYPE_HSIG_ARGS_COUNT */
201         _tt_rpc_otype_osig_arg_mode,    /* 30 - TT_RPC_OTYPE_OSIG_ARG_MODE */
202         _tt_rpc_otype_hsig_arg_mode,    /* 31 - TT_RPC_OTYPE_HSIG_ARG_MODE */
203         _tt_rpc_otype_osig_arg_type,    /* 32 - TT_RPC_OTYPE_OSIG_ARG_TYPE */
204         _tt_rpc_otype_hsig_arg_type,    /* 33 - TT_RPC_OTYPE_HSIG_ARG_TYPE */
205         _tt_rpc_hdispatch,              /* 34 - TT_RPC_HDISPATCH */
206         _tt_rpc_hupdate_msg,            /* 35 - TT_RPC_HUPDATE_MSG */
207         _tt_rpc_dispatch_2,             /* 36 - TT_RPC_DISPATCH_2 */
208         _tt_rpc_update_msg_2,           /* 37 - TT_RPC_UPDATE_MSG_2 */
209         _tt_rpc_msgread_2,              /* 38 - TT_RPC_MSGREAD_2 */
210         _tt_rpc_dispatch_on_exit,       /* 39 - TT_RPC_DISPATCH_ON_EXIT */
211         _tt_rpc_undeclare_ptype,        /* 40 - TT_RPC_UNDECLARE_PTYPE */
212         _tt_rpc_exists_ptype,           /* 41 - TT_RPC_EXISTS_PTYPE */
213         _tt_rpc_unblock_ptype,          /* 42 - TT_RPC_UNBLOCK_PTYPE */
214         _tt_rpc_join_context,           /* 43 - TT_RPC_JOIN_CONTEXT */
215         _tt_rpc_quit_context,           /* 44 - TT_RPC_QUIT_CONTEXT */
216         _tt_rpc_dispatch_with_context,  /* 45 - TT_RPC_DISPATCH_WITH_CONTEXT */
217         _tt_rpc_dispatch_2_with_context,/* 46 - TT_RPC_DISPATCH_2_WITH_CONTEXT */
218         _tt_rpc_add_pattern_with_context,/* 47 - TT_RPC_ADD_PATTERN_WITH_CONTEXT */
219         _tt_rpc_load_types              /* 48 - TT_RPC_LOAD_TYPES */
220 };
221
222
223 /* 
224  * Dispatch function invoked whenever an RPC request comes in. 
225  */
226 void
227 _tt_service_rpc(svc_req *rqstp, SVCXPRT *transp)
228 {
229         _Tt_auth_level          session_auth;
230 #if defined(OPT_SECURE_RPC)
231         authdes_cred            *des_cred;
232 #endif
233
234         // Increment the counter for the number of RPC calls
235         // handled during the life of this process. 
236         _tt_global->event_counter++; 
237  
238         //
239         // authenticate request
240         //
241         session_auth = _tt_s_mp->initial_s_session->auth_level();
242         switch (rqstp->rq_cred.oa_flavor) {
243             case AUTH_UNIX:
244             case AUTH_SHORT:
245                 if (session_auth == _TT_AUTH_DES ||
246                     session_auth == _TT_AUTH_ICEAUTH) {
247                         svcerr_auth(transp, AUTH_TOOWEAK);
248                         return;
249                 }
250                 else if (session_auth == _TT_AUTH_UNIX) {
251                         if (_tt_s_mp->unix_cred_chk_flag) {
252
253                                 // "authunix_parms" is now deprecated in
254                                 // favor of "authsys_parms", but
255                                 // "authsys_parms" does not exist on
256                                 // all platforms yet.
257                                 
258                                 authunix_parms* sys_cred =
259                                         (authunix_parms *) rqstp->rq_clntcred;
260                                 if (sys_cred->aup_uid != geteuid()) {
261                                         svcerr_auth(transp, AUTH_BADCRED);
262                                         return;
263                                 }
264                         }
265                         // else, we are not doing auth checking
266                 }
267                                         
268                 break;
269 #if defined(OPT_SECURE_RPC)
270             case AUTH_DES:
271                 if (session_auth != _TT_AUTH_DES) {
272                         svcerr_auth(transp, AUTH_TOOWEAK);
273                         return;
274                 }
275                 des_cred = (authdes_cred *)rqstp->rq_clntcred;
276 #if !defined(OPT_BUG_SUNOS_5)
277                 if (! authdes_getucred(des_cred,
278                                        &req_auth_uid,
279                                        &req_auth_gid,
280                                        &req_auth_gidlen,
281                                        req_auth_gids)) {
282                         svcerr_auth(transp, AUTH_BADCRED);
283                 }
284 #else           
285                 Hack_getucred hack_getucred_p;
286                         
287                 hack_getucred_p = (Hack_getucred)authdes_getucred;
288                 if (! (*hack_getucred_p)(des_cred,
289                                          &req_auth_uid,
290                                          &req_auth_gid,
291                                          &req_auth_gidlen,
292                                          req_auth_gids)) {
293                         svcerr_auth(transp, AUTH_BADCRED);
294                 }
295 #endif                  /* OPT_BUG_SUNOS_5 */
296                         
297                 // Now that we have the credentials, make sure they
298                 // are the right ones
299                                         
300                 if (req_auth_uid == geteuid() &&
301                     req_auth_gid == getegid()) {
302                                                 
303                         break;  // else fall through to svcerr_weakauth
304                 }
305 #endif                          /* OPT_SECURE_RPC */
306             default:
307                 if (session_auth == _TT_AUTH_ICEAUTH) {
308                     _Tt_string  clnt_cookie;
309                     if (!_tt_svc_getargs(transp,
310                                              (xdrproc_t) tt_xdr_string,
311                                              (char *) &clnt_cookie)) {
312                         svcerr_auth(transp, AUTH_BADCRED);
313                         return;
314                     }
315
316                     _Tt_string  &svr_cookie =
317                         _tt_s_mp->initial_s_session->auth_cookie();
318                     if (clnt_cookie != svr_cookie) {
319                         svcerr_auth(transp, AUTH_BADCRED);
320                         return;
321                     }
322                 }
323         }
324         
325         //
326         // dispatch request to appropiate dispatch function
327         //
328                                 
329         //
330         // Set and reset xdr version on entry and exit.  See tt_xdr_version.h
331         //
332         _Tt_xdr_version         xvers((int)rqstp->rq_vers);
333         
334         switch (rqstp->rq_proc) {
335             case NULLPROC:
336                 (void)svc_sendreply(transp, (xdrproc_t)xdr_void, 0);
337                 break;
338             case TT_RPC_VRFY_SESSION:
339                 _tt_rpc_vrfy_session(transp);
340                 break;
341             default:
342                 if (rqstp->rq_proc >= 1 && rqstp->rq_proc < TT_RPC_LAST) {
343                         (*_tt_rpc_dispatch_table[rqstp->rq_proc])(transp);
344                 } else {
345                         svcerr_noproc(transp);
346                 }
347                 break;
348         }
349 }
350
351 /* 
352  * Called when a sender sends a message to the server.
353  * This original version of _tt_rpc_dispatch() is a synchronous RPC
354  * call, and it returns a _Tt_dispatch_reply_args, which contains
355  * dispatch status and file-scoped queuing info.  The new version,
356  * _tt_rpc_dispatch_2(), is asynchronous and thus faster.
357  */
358 void
359 _tt_rpc_dispatch(SVCXPRT *transp)
360 {
361         _Tt_s_message_ptr               msg;
362         _Tt_dispatch_reply_args         args;
363
364         msg = new _Tt_s_message();
365         if (!_tt_svc_getargs(transp,
366                              (xdrproc_t)tt_xdr_message,
367                              (char *)&msg)) {
368                 svcerr_decode(transp);
369                 return;
370         }
371
372         args.status = TT_OK;
373         _Tt_msg_trace trace( *msg, TTDR_MESSAGE_SEND );
374         //
375         // XXX Turn this off until we do TT_ERR_OVERFLOW right
376         //
377         //if (_tt_s_mp->active_messages > _tt_s_mp->max_active_messages)
378         if (0)
379         {
380                 args.status = TT_ERR_OVERFLOW;
381         } else {
382                 // dispatch the message so that any information regarding
383                 // scope, and queueing can be relayed back to the sender.
384                 msg->dispatch( trace );
385                 msg->qmsg_info(args.qmsg_info);
386         }
387         if (msg->scope() != TT_SESSION) {
388                 // if no file-scope patterns exist for this file then
389                 // return an error to allow the client to remove this
390                 // session from the file's scope.
391                 if (! _tt_s_mp->in_file_scope(msg->file())) {
392                         args.status = TT_ERR_FILE;
393                 }
394         }
395         if (!svc_sendreply(transp,
396                            (xdrproc_t)tt_xdr_dispatch_reply_args,
397                            (RPC_ARG_T)&args)) {
398                 return;
399         }
400
401         if (args.status == TT_OK || args.status == TT_ERR_FILE) {
402                 // now deliver the message
403                 msg->deliver_to_observers_and_handlers( trace );
404         }
405 }
406
407
408 /* 
409  * Called when a sender sends a contextful message
410  * to the server. 
411  */
412 void
413 _tt_rpc_dispatch_with_context(SVCXPRT *transp)
414 {
415         //
416         // In messages, xdr versioning for contexts isn't necessary,
417         // since a bitmask controls what fields of a _Tt_message get
418         // xdr'd.  This separate RPC call exists only so that
419         // context-aware clients can tell whether a server understands
420         // contexts.
421         //
422         _tt_rpc_dispatch(transp);
423 }
424
425
426 /* 
427  * High-performance version of _tt_rpc_dispatch().  No information
428  * is relayed back to the calling client.
429  */
430 void
431 _tt_rpc_dispatch_2(SVCXPRT *transp)
432 {
433         _Tt_s_message_ptr               msg;
434
435         msg = new _Tt_s_message();
436         if (!_tt_svc_getargs(transp,
437                              (xdrproc_t)tt_xdr_message,
438                              (char *)&msg)) {
439                 svcerr_decode(transp);
440                 return;
441         }
442         _Tt_msg_trace trace( *msg, TTDR_MESSAGE_SEND );
443         msg->dispatch( trace );
444         // XXX holtz 93/09/17 Should we really deliver() if dispatch() fails?
445         // Note: this applies to every dispatch() call in this file.
446         msg->deliver_to_observers_and_handlers( trace );
447 }
448
449
450 /* 
451  * Called when a sender sends a contextful session-scoped message
452  * to the server. 
453  */
454 void
455 _tt_rpc_dispatch_2_with_context(SVCXPRT *transp)
456 {
457         //
458         // In messages, xdr versioning for contexts isn't necessary,
459         // since a bitmask controls what fields of a _Tt_message get
460         // xdr'd.  This separate RPC call exists only so that
461         // context-aware clients can tell whether a server understands
462         // contexts.
463         //
464         _tt_rpc_dispatch_2(transp);
465 }
466
467
468
469 /* 
470  * Called when a handler or voter updates a message in any way (ie. state change)
471  */
472 void
473 _tt_update_msg(SVCXPRT *transp, int reply)
474 {
475         _Tt_s_procid_ptr                proc;
476         Tt_status                       status;
477
478         if (!_tt_svc_getargs(transp,
479                              (xdrproc_t)tt_xdr_update_args,
480                              (char *)&(_tt_s_mp->update_args))) {
481                 svcerr_decode(transp);
482                 return;
483         }
484
485         int found = 0;
486         _Tt_message_ptr &msg = _tt_s_mp->update_args.message;
487         //
488         // We have to rummage around in the message to see which
489         // procid is talking to us.
490         //
491         if (! msg->handler().is_null()) {
492                 //
493                 // Assume the handler is the updater.
494                 //
495                 found = _tt_s_mp->find_proc(msg->handler(), proc, 0);
496         } else if (msg->message_class() == TT_OFFER) {
497                 //
498                 // Assume the last voter is the updater.
499                 //
500                 _Tt_procid_list_ptr proc_list;
501                 switch (_tt_s_mp->update_args.newstate) {
502                     case TT_REJECTED:
503                         proc_list = msg->rejecters();
504                         break;
505                     case TT_ACCEPTED:
506                         proc_list = msg->accepters();
507                         break;
508                     case TT_ABSTAINED:
509                         proc_list = msg->abstainers();
510                         break;
511                 }
512                 if ((! proc_list.is_null()) && (proc_list->count() > 0)) {
513                         found = _tt_s_mp->find_proc(proc_list->bot(), proc, 0);
514                 }
515         }
516         if (found) {
517                 status = TT_OK;
518         } else {
519                 status = TT_ERR_PROCID;
520         }
521
522         if (reply && (! svc_sendreply(transp, (xdrproc_t)xdr_int,
523                                       (RPC_ARG_T)&status)))
524         {
525                 return;
526         }
527
528         if (status != TT_OK) {
529                 return;
530         }
531         if (msg->is_observer()) {
532                 // Clears _handler when observer replies to START_MESSAGE
533                 // XXX _handler is used to tell us which procid is talking
534                 // to us, because nothing in the message tells us who
535                 // is observing it.  Ugly.
536                 msg->clr_observer_procid();
537         }
538         proc->update_message(msg, _tt_s_mp->update_args.newstate);
539 }
540
541
542 /* 
543  * Called when a handler updates a message in any way (ie. state change)
544  */
545 void
546 _tt_rpc_update_msg(SVCXPRT *transp)
547 {
548         _tt_update_msg(transp, 1);
549 }
550
551 /* 
552  * Asynchronous, higher-performance version of _tt_rpc_update_msg()
553  */
554 void
555 _tt_rpc_update_msg_2(SVCXPRT *transp)
556 {
557         _tt_update_msg(transp, 0);
558 }
559
560
561 /* 
562  * Called when a process joins a file.
563  */
564 void
565 _tt_rpc_join_file(SVCXPRT *transp)
566 {
567         _Tt_file_join_args      args;
568         _Tt_s_procid_ptr        proc;
569         Tt_status               status;
570         _Tt_s_file_ptr          fptr;
571
572         if (! _tt_svc_getargs(transp, (xdrproc_t)tt_xdr_file_join_args,
573                               (char *)&args)) {
574                 svcerr_decode(transp);
575         }
576         if (_tt_s_mp->find_proc(args.procid, proc, 1)) {
577                 fptr = new _Tt_s_file(args.path);
578                 status = fptr->s_join(proc);
579         } else {
580                 status = TT_ERR_PROCID;
581         }
582         (void)svc_sendreply(transp, (xdrproc_t)xdr_int, (RPC_ARG_T)&status);
583 }
584
585
586 /* 
587  * Called when a process quits a file.
588  */
589 void
590 _tt_rpc_quit_file(SVCXPRT *transp)
591 {
592         _Tt_file_join_args      args;
593         _Tt_s_procid_ptr        proc;
594         Tt_status               status;
595         _Tt_s_file_ptr          fptr;
596
597         if (! _tt_svc_getargs(transp, (xdrproc_t)tt_xdr_file_join_args,
598                               (char *)&args)) {
599                 svcerr_decode(transp);
600         }
601
602         if (_tt_s_mp->find_proc(args.procid, proc, 1)) {
603                 fptr = new _Tt_s_file(args.path);
604                 status = fptr->s_quit(proc);
605         } else {
606                 status = TT_ERR_PROCID;
607         }
608         (void)svc_sendreply(transp, (xdrproc_t)xdr_int, (RPC_ARG_T)&status);
609 }
610
611 /* 
612  * Called when a procid joins a session.
613  */
614 void
615 _tt_rpc_join_session(SVCXPRT *transp)
616 {
617         _Tt_procid_ptr          cproc;
618         _Tt_s_procid_ptr        proc;
619         Tt_status               status;
620
621         if (! _tt_svc_getargs(transp,
622                               (xdrproc_t)tt_xdr_procid,
623                               (char *)&cproc)) {
624                 svcerr_decode(transp);
625                 return;
626         }
627         if (_tt_s_mp->find_proc(cproc, proc, 1)) {
628                 _Tt_s_procid    *sp = (_Tt_s_procid *)proc.c_pointer();
629                 if (cproc->start_token().len() != 0) {
630                         sp->set_start_token(cproc->start_token());
631                 }
632                 status = _tt_s_mp->initial_s_session->s_join(proc);
633         } else {
634                 status = TT_ERR_PROCID;
635         }
636
637         if (svc_sendreply(transp, (xdrproc_t)xdr_int, (RPC_ARG_T)&status) == 0) {
638                 return;
639         }
640 }
641
642
643 /* 
644  * Called when a procid quits a session.
645  */
646 void
647 _tt_rpc_quit_session(SVCXPRT *transp)
648 {
649         _Tt_procid_ptr          cproc;
650         _Tt_s_procid_ptr        proc;
651         Tt_status               status;
652
653         if (! _tt_svc_getargs(transp,
654                               (xdrproc_t)tt_xdr_procid,
655                               (char *)&cproc)) {
656                 svcerr_decode(transp);
657                 return;
658         }
659         if (_tt_s_mp->find_proc(cproc, proc, 1)) {
660                 status = _tt_s_mp->initial_s_session->s_quit(proc);
661         } else {
662                 status = TT_ERR_PROCID;
663         }
664
665         if (svc_sendreply(transp, (xdrproc_t)xdr_int, (RPC_ARG_T)&status) == 0) {
666                 return;
667         }
668 }
669
670 /*
671  * Called when a procid wants to join a context.
672  */
673 void
674 _tt_rpc_join_context(SVCXPRT *transp)
675 {
676         _Tt_context_join_args   args;
677         _Tt_s_procid_ptr        proc;
678         Tt_status               status;
679
680         if (! _tt_svc_getargs( transp, (xdrproc_t)tt_xdr_context_join_args,
681                               (char *)&args))
682         {
683                 svcerr_decode(transp);
684         }
685         if (_tt_s_mp->find_proc( args.procid, proc, 1 )) {
686                 status = ((_Tt_s_msg_context &)*args.context).s_join( proc );
687         } else {
688                 status = TT_ERR_PROCID;
689         }
690         svc_sendreply( transp, (xdrproc_t)xdr_int, (RPC_ARG_T)&status );
691 }
692
693 /*
694  * Called when a procid wants to quit a context.
695  */
696 void
697 _tt_rpc_quit_context(SVCXPRT *transp)
698 {
699         _Tt_context_join_args   args;
700         _Tt_s_procid_ptr        proc;
701         Tt_status               status;
702
703         if (! _tt_svc_getargs( transp, (xdrproc_t)tt_xdr_context_join_args,
704                               (char *)&args))
705         {
706                 svcerr_decode(transp);
707         }
708         if (_tt_s_mp->find_proc( args.procid, proc, 1 )) {
709                 status = ((_Tt_s_msg_context &)*args.context).s_quit( proc );
710         } else {
711                 status = TT_ERR_PROCID;
712         }
713         svc_sendreply( transp, (xdrproc_t)xdr_int, (RPC_ARG_T)&status );
714 }
715
716
717 /* 
718  * Called when a procid wants to retrieve the next undelivered message
719  * for it.
720  */
721 void
722 _tt_rpc_next_message(SVCXPRT *transp)
723 {
724         _Tt_procid_ptr          cproc;
725         _Tt_next_message_args   args;
726         _Tt_s_procid_ptr        proc;
727
728         if (! _tt_svc_getargs(transp,
729                               (xdrproc_t)tt_xdr_procid,
730                               (char *)&cproc)) {
731                 svcerr_decode(transp);
732                 return;
733         }
734         // default is to tell client to clear fd, that way in case of
735         // any errors, at least the client won't loop.
736         args.clear_signal = 1;
737         args.msgs = (_Tt_message_list *)0;
738         if (_tt_s_mp->find_proc(cproc, proc, 0)) {
739                 _Tt_s_procid    *sp = (_Tt_s_procid *)proc.c_pointer();
740                 sp->next_message(args);
741         }
742
743         if (svc_sendreply(transp,
744                           (xdrproc_t)tt_xdr_next_message_args,
745                           (RPC_ARG_T)&args) == 0) {
746                 return;
747         }
748 }
749
750
751 /* 
752  * Called when a procid wants to register a new pattern.
753  */
754 void
755 _tt_rpc_add_pattern(SVCXPRT *transp)
756 {
757         _Tt_s_procid_ptr        proc;
758         _Tt_s_add_pattern_args  args;
759         Tt_status               status;
760
761         if (! _tt_svc_getargs(transp,
762                               (xdrproc_t)tt_s_xdr_add_pattern_args,
763                               (char *)&args)) {
764                 svcerr_decode(transp);
765                 return;
766         }
767
768         if (_tt_s_mp->find_proc(args.procid, proc, 1)) {
769                 _Tt_s_procid    *sp = (_Tt_s_procid *)proc.c_pointer();
770                 status = sp->add_pattern(args.pattern);
771         } else {
772                 status = TT_ERR_PROCID;
773         }
774
775         if (svc_sendreply(transp,(xdrproc_t)xdr_int,(RPC_ARG_T)&status) == 0) {
776                 return;
777         }
778 }
779
780
781 /* 
782  * Called when a procid wants to register a new context-ful pattern.
783  */
784 void
785 _tt_rpc_add_pattern_with_context(SVCXPRT *transp)
786 {
787         //
788         // Contexts are only xdr'd in _Tt_patterns in xdr version 3 or
789         // later.  The rpc protocol version number represents a floor
790         // for the xdr version number.  Even though this rpc call was
791         // introduced into protocol version 2, it can be assumed that
792         // the caller is using at least xdr version 3.
793         //
794         _Tt_xdr_version xvers( max(_tt_global->xdr_version(),
795                                    TT_CONTEXTS_XDR_VERSION) );
796
797         _tt_rpc_add_pattern(transp);
798 }
799
800
801 /* 
802  * Called when a procid wants to unregister a pattern with the server.
803  */
804 void
805 _tt_rpc_del_pattern(SVCXPRT *transp)
806 {
807         _Tt_s_procid_ptr        proc;
808         _Tt_s_del_pattern_args  args;
809         Tt_status               status;
810
811         if (! _tt_svc_getargs(transp,
812                               (xdrproc_t)tt_s_xdr_del_pattern_args,
813                               (char *)&args)) {
814                 svcerr_decode(transp);
815                 return;
816         }
817
818         if (_tt_s_mp->find_proc(args.procid, proc, 1)) {
819                 _Tt_s_procid    *sp = (_Tt_s_procid *)proc.c_pointer();
820                 status = sp->del_pattern(args.pattern_id);
821         } else {
822                 status = TT_ERR_PROCID;
823         }
824
825         if (svc_sendreply(transp,(xdrproc_t)xdr_int,(RPC_ARG_T)&status) == 0) {
826                 return;
827         }
828 }
829
830
831
832 /* 
833  * Called by a procid to declare a ptype.
834  */
835 void
836 _tt_rpc_declare_ptype(SVCXPRT *transp)
837 {
838         _Tt_declare_ptype_args  args;
839         _Tt_s_procid_ptr        proc;
840         Tt_status               status;
841
842         if (! _tt_svc_getargs(transp,
843                               (xdrproc_t)tt_xdr_declare_ptype_args,
844                               (char *)&args)) {
845                 svcerr_decode(transp);
846                 return;
847         }
848
849         if (_tt_s_mp->find_proc(args.procid, proc, 1)) {
850                 _Tt_s_procid    *sp = (_Tt_s_procid *)proc.c_pointer();
851                 status = sp->declare_ptype(args.ptid);
852         } else {
853                 status = TT_ERR_PROCID;
854         }       
855
856         if (svc_sendreply(transp,(xdrproc_t)xdr_int,(RPC_ARG_T)&status) == 0) {
857                 return;
858         }
859 }
860
861 /* 
862  * Called by a procid to undeclare a ptype.
863  */
864 void
865 _tt_rpc_undeclare_ptype(SVCXPRT *transp)
866 {
867         _Tt_declare_ptype_args  args;
868         _Tt_s_procid_ptr        proc;
869         Tt_status               status;
870
871         if (! _tt_svc_getargs(transp,
872                               (xdrproc_t)tt_xdr_declare_ptype_args,
873                               (char *)&args)) {
874                 svcerr_decode(transp);
875                 return;
876         }
877
878         if (_tt_s_mp->find_proc(args.procid, proc, 1)) {
879                 _Tt_s_procid    *sp = (_Tt_s_procid *)proc.c_pointer();
880                 status = sp->undeclare_ptype(args.ptid);
881         } else {
882                 status = TT_ERR_PROCID;
883         }       
884
885         if (svc_sendreply(transp,(xdrproc_t)xdr_int,(RPC_ARG_T)&status) == 0) {
886                 return;
887         }
888 }
889
890 /* 
891  * Called by a procid to test existence of a ptype.
892  */
893 void
894 _tt_rpc_exists_ptype(SVCXPRT *transp)
895 {
896         _Tt_declare_ptype_args  args;
897         _Tt_s_procid_ptr        proc;
898         Tt_status               status;
899
900         if (! _tt_svc_getargs(transp,
901                               (xdrproc_t)tt_xdr_declare_ptype_args,
902                               (char *)&args)) {
903                 svcerr_decode(transp);
904                 return;
905         }
906
907         if (_tt_s_mp->find_proc(args.procid, proc, 1)) {
908                 _Tt_s_procid    *sp = (_Tt_s_procid *)proc.c_pointer();
909                 status = sp->exists_ptype(args.ptid);
910         } else {
911                 status = TT_ERR_PROCID;
912         }       
913
914         if (svc_sendreply(transp,(xdrproc_t)xdr_int,(RPC_ARG_T)&status) == 0) {
915                 return;
916         }
917 }
918
919
920 /* 
921  * Called by a procid to unblock queued messages for its ptype.
922  */
923 void
924 _tt_rpc_unblock_ptype(SVCXPRT *transp)
925 {
926         _Tt_declare_ptype_args  args;
927         _Tt_s_procid_ptr        proc;
928         Tt_status               status;
929
930         if (! _tt_svc_getargs(transp,
931                               (xdrproc_t)tt_xdr_declare_ptype_args,
932                               (char *)&args)) {
933                 svcerr_decode(transp);
934                 return;
935         }
936
937         if (_tt_s_mp->find_proc(args.procid, proc, 1)) {
938                 _Tt_s_procid    *sp = (_Tt_s_procid *)proc.c_pointer();
939                 status = sp->unblock_ptype(args.ptid);
940         } else {
941                 status = TT_ERR_PROCID;
942         }       
943
944         if (svc_sendreply(transp,(xdrproc_t)xdr_int,(RPC_ARG_T)&status) == 0) {
945                 return;
946         }
947 }
948
949 /* 
950  * Called by a procid to load new types into its associated ttsession.
951  */
952 void
953 _tt_rpc_load_types(SVCXPRT *transp)
954 {
955         _Tt_load_types_args     args;
956         Tt_status               status;
957         XDR                     xdrs;
958
959         if (! _tt_svc_getargs(transp,
960                               (xdrproc_t)tt_xdr_load_types_args,
961                               (char *)&args)) {
962                 svcerr_decode(transp);
963                 return;
964         }
965
966         xdrmem_create(&xdrs,
967                       (char *)args.xdrtypes,
968                       (u_int)args.xdrtypes.len(),
969                       XDR_DECODE);
970
971         int junk;
972         status = _Tt_typedb::merge_from(&xdrs,_tt_s_mp->tdb, junk);
973
974         if (status==TT_OK) {
975                 _tt_s_mp->install_ptable(_tt_s_mp->tdb->ptable);
976                 _tt_s_mp->install_otable(_tt_s_mp->tdb->otable);
977         }
978
979         if (svc_sendreply(transp,(xdrproc_t)xdr_int,(RPC_ARG_T)&status) == 0) {
980                 return;
981         }
982 }
983
984
985
986 /* 
987  * Called by a procid to notify the server of its fd signalling channel. 
988  */
989 void
990 _tt_rpc_set_fd_channel(SVCXPRT *transp)
991 {
992         _Tt_fd_args             fd_args;
993         _Tt_s_procid_ptr        proc;
994         Tt_status               status;
995
996         if (! _tt_svc_getargs(transp,
997                               (xdrproc_t)tt_xdr_fd_args,
998                               (char *)&fd_args)) {
999                 svcerr_decode(transp);
1000                 return;
1001         }
1002
1003         if (_tt_s_mp->find_proc(fd_args.procid, proc, 1)) {
1004                 status = TT_OK;
1005         } else {
1006                 status = TT_ERR_PROCID;
1007         }
1008
1009         //
1010         // Note: it's important to reply immediately here because
1011         // setting up the channel involves connecting to the socket
1012         // on the client side so if we attempted to reply after
1013         // the operation then we would get deadlock.
1014         //
1015         if (svc_sendreply(transp,
1016                           (xdrproc_t)xdr_int,
1017                           (RPC_ARG_T)&status) == 0) {
1018                 return;
1019         }
1020
1021         if (status == TT_OK) {
1022                 _Tt_s_procid    *sp = (_Tt_s_procid *)proc.c_pointer();
1023                 sp->set_start_token(fd_args.start_token);
1024                 sp->set_fd_channel(fd_args.fd);
1025         }
1026 }
1027
1028
1029 /* 
1030  * Set the value of a session property
1031  */
1032 void
1033 _tt_rpc_set_prop(SVCXPRT *transp)
1034 {
1035         _Tt_prop_args           args;
1036         Tt_status               status;
1037
1038         if (! _tt_svc_getargs(transp,
1039                               (xdrproc_t)tt_xdr_prop_args,
1040                               (char *)&args)) {
1041                 svcerr_decode(transp);
1042                 return;
1043         }
1044
1045         status = _tt_s_mp->initial_s_session->s_setprop(args.prop, args.value);
1046
1047         if (svc_sendreply(transp,(xdrproc_t)xdr_int,(RPC_ARG_T)&status) == 0) {
1048                 return;
1049         }
1050 }
1051
1052 /* 
1053  * Add a value to a session property
1054  */
1055 void
1056 _tt_rpc_add_prop(SVCXPRT *transp)
1057 {
1058         _Tt_prop_args           args;
1059         Tt_status               status;
1060
1061         if (! _tt_svc_getargs(transp,
1062                               (xdrproc_t)tt_xdr_prop_args,
1063                               (char *)&args)) {
1064                 svcerr_decode(transp);
1065                 return;
1066         }
1067
1068         status = _tt_s_mp->initial_s_session->s_addprop(args.prop, args.value);
1069         if (svc_sendreply(transp,(xdrproc_t)xdr_int,(RPC_ARG_T)&status) == 0) {
1070                 return;
1071         }
1072 }
1073
1074 /* 
1075  * Get a value from a session property
1076  */
1077 void
1078 _tt_rpc_get_prop(SVCXPRT *transp)
1079 {
1080         _Tt_prop_args           args;
1081         Tt_status               status;
1082         _Tt_string              value;
1083         _Tt_rpc_result          result;
1084
1085         if (! _tt_svc_getargs(transp,
1086                               (xdrproc_t)tt_xdr_prop_args,
1087                               (char *)&args)) {
1088                 svcerr_decode(transp);
1089                 return;
1090         }
1091
1092         
1093         if ((status = _tt_s_mp->initial_s_session->s_getprop(args.prop, args.num, value))
1094             != TT_OK) {
1095                 result.status = status;
1096                 result.str_val = "";
1097                 result.int_val = 0;
1098         } else {
1099                 result.status = TT_OK;
1100                 result.str_val = value;
1101                 result.int_val = 0;
1102         }
1103         if (svc_sendreply(transp, (xdrproc_t)tt_xdr_rpc_result,
1104                           (RPC_ARG_T)&result) == 0) {
1105                 return;
1106         }
1107 }
1108
1109 /* 
1110  * Return the number of values for a session property
1111  */
1112 void
1113 _tt_rpc_prop_count(SVCXPRT *transp)
1114 {
1115         _Tt_prop_args           args;
1116         int                     cnt;
1117         _Tt_rpc_result          result;
1118
1119         if (! _tt_svc_getargs(transp,
1120                               (xdrproc_t)tt_xdr_prop_args,
1121                               (char *)&args)) {
1122                 svcerr_decode(transp);
1123                 return;
1124         }
1125
1126         
1127         result.status = _tt_s_mp->initial_s_session->s_propcount(args.prop, cnt);
1128         result.str_val = "";
1129         if (result.status == TT_OK) {
1130                 result.int_val = cnt;
1131         } else {
1132                 result.int_val = 0;
1133         }
1134
1135         if (svc_sendreply(transp, (xdrproc_t)tt_xdr_rpc_result,
1136                           (RPC_ARG_T)&result) == 0) {
1137                 return;
1138         }
1139 }
1140
1141 /* 
1142  * Return a session property name
1143  */
1144 void
1145 _tt_rpc_prop_name(SVCXPRT *transp)
1146 {
1147         _Tt_prop_args           args;
1148         Tt_status               status;
1149         _Tt_string              prop;
1150         _Tt_rpc_result          result;
1151
1152         if (! _tt_svc_getargs(transp,
1153                               (xdrproc_t)tt_xdr_prop_args,
1154                               (char *)&args)) {
1155                 svcerr_decode(transp);
1156                 return;
1157         }
1158
1159         if ((status = _tt_s_mp->initial_s_session->s_propname(args.num, prop))
1160             != TT_OK) {
1161                 result.status = status;
1162                 result.str_val = "";
1163                 result.int_val = 0;
1164         } else {
1165                 result.status = TT_OK;
1166                 result.str_val = prop;
1167                 result.int_val = 0;
1168         }
1169
1170         if (svc_sendreply(transp, (xdrproc_t)tt_xdr_rpc_result,
1171                           (RPC_ARG_T)&result) == 0) {
1172                 return;
1173         }
1174 }
1175
1176 /* 
1177  * Return the number of session properties
1178  */
1179 void
1180 _tt_rpc_prop_names_count(SVCXPRT *transp)
1181 {
1182         _Tt_prop_args           args;
1183         int                     cnt;
1184         _Tt_rpc_result          result;
1185
1186         if (! _tt_svc_getargs(transp,
1187                               (xdrproc_t)tt_xdr_prop_args,
1188                               (char *)&args)) {
1189                 svcerr_decode(transp);
1190                 return;
1191         }
1192
1193         result.status = _tt_s_mp->initial_s_session->s_propnames_count(cnt);
1194         result.str_val = "";
1195         if (result.status == TT_OK) {
1196                 result.int_val = cnt;
1197         } else {
1198                 result.int_val = 0;
1199         }
1200
1201         if (svc_sendreply(transp, (xdrproc_t)tt_xdr_rpc_result,
1202                           (RPC_ARG_T)&result) == 0) {
1203                 return;
1204         }
1205 }
1206
1207
1208 void
1209 _tt_rpc_alloc_procid_key(SVCXPRT *transp)
1210 {
1211         _Tt_string              result;
1212
1213         if (! _tt_svc_getargs(transp,(xdrproc_t)xdr_void,0)) {
1214                 svcerr_decode(transp);
1215                 return;
1216         }
1217
1218         result = _tt_s_mp->alloc_procid_key();
1219
1220         if (svc_sendreply(transp, (xdrproc_t)tt_xdr_string,
1221                           (RPC_ARG_T)&result) == 0) {
1222                 return;
1223         }
1224 }
1225
1226 void
1227 _tt_rpc_close_procid(SVCXPRT *transp)
1228 {
1229         _Tt_procid_ptr          p;
1230         _Tt_s_procid_ptr        rp;
1231         Tt_status               status;
1232
1233         if (! _tt_svc_getargs(transp,
1234                               (xdrproc_t)tt_xdr_procid,
1235                               (char *)&p)) {
1236                 svcerr_decode(transp);
1237         }
1238         if (_tt_s_mp->find_proc(p, rp, 0)) {
1239                 status = _tt_s_mp->s_remove_procid(*rp);
1240         } else {
1241                 status = TT_ERR_PROCID;
1242         }
1243         (void)svc_sendreply(transp,(xdrproc_t)xdr_int, (RPC_ARG_T)&status);
1244 }
1245
1246 void
1247 _tt_rpc_vrfy_session(SVCXPRT *transp)
1248 {
1249         _Tt_string              id;
1250
1251         if (! _tt_svc_getargs(transp, (xdrproc_t)xdr_void, 0)) {
1252                 svcerr_decode(transp);
1253                 return;
1254         }
1255         if (_tt_global->xdr_version() == 1) {
1256                 id = _tt_s_mp->initial_s_session->id();
1257         } else {
1258                 id = _tt_s_mp->initial_s_session->process_tree_id();
1259         }
1260         if (!svc_sendreply(transp,
1261                            (xdrproc_t)tt_xdr_string,
1262                            (RPC_ARG_T)&id)) {
1263                 svcerr_decode(transp);
1264         }
1265 }
1266 /* 
1267  * Return the number of otypes derived from a base otype
1268  */
1269 void
1270 _tt_rpc_otype_deriveds_count(SVCXPRT *transp)
1271 {
1272         _Tt_otype_args          args;
1273         _Tt_rpc_result          result;
1274         _Tt_otype_ptr           base_otype;
1275
1276         if (! _tt_svc_getargs(transp,
1277                               (xdrproc_t)tt_xdr_otype_args,
1278                               (char *)&args)) {
1279                 svcerr_decode(transp);
1280                 return;
1281         }
1282
1283         result.str_val = "";
1284         base_otype =  _tt_s_mp->otable->lookup(args.base_otid);
1285         if (base_otype.is_null()) {
1286                 result.status = TT_ERR_OTYPE;
1287         } else {
1288                 result.int_val = base_otype->children()->count();
1289                 result.status = TT_OK;
1290         }
1291
1292         if (svc_sendreply(transp, (xdrproc_t)tt_xdr_rpc_result,
1293                           (RPC_ARG_T)&result) == 0) {
1294                 return;
1295         }
1296 }
1297
1298 /* 
1299  * Return the n'th otype derived from a base otype
1300  */
1301 void
1302 _tt_rpc_otype_derived(SVCXPRT *transp)
1303 {
1304         _Tt_otype_args          args;
1305         _Tt_rpc_result          result;
1306         _Tt_string_list_ptr     children;
1307         _Tt_otype_ptr           base_otype;
1308
1309         if (! _tt_svc_getargs(transp,
1310                               (xdrproc_t)tt_xdr_otype_args,
1311                               (char *)&args)) {
1312                 svcerr_decode(transp);
1313                 return;
1314         }
1315
1316         result.str_val = "";
1317         base_otype =  _tt_s_mp->otable->lookup(args.base_otid);
1318         if (base_otype.is_null()) {
1319                 result.status = TT_ERR_OTYPE;
1320         } else {
1321                 children =  base_otype->children();
1322                 if (args.num<0 || children->count()<args.num) {
1323                         result.status = TT_ERR_NUM;
1324                         result.str_val = "";
1325                 } else if (children->count()==args.num) {
1326                         result.status = TT_OK;
1327                         result.str_val = (char *)0;
1328                 } else {
1329                         result.status = TT_OK;
1330                         result.str_val = (*children)[args.num];
1331                 }
1332         }
1333         if (svc_sendreply(transp, (xdrproc_t)tt_xdr_rpc_result,
1334                           (RPC_ARG_T)&result) == 0) {
1335                 return;
1336         }
1337 }
1338
1339 /* 
1340  * Return the otype that is the base of a derived otype
1341  */
1342 void
1343 _tt_rpc_otype_base(SVCXPRT *transp)
1344 {
1345         _Tt_otype_args          args;
1346         _Tt_rpc_result          result;
1347         _Tt_string_list_ptr     parents;
1348         _Tt_otype_ptr           derived_otype;
1349
1350         if (! _tt_svc_getargs(transp,
1351                               (xdrproc_t)tt_xdr_otype_args,
1352                               (char *)&args)) {
1353                 svcerr_decode(transp);
1354                 return;
1355         }
1356
1357         derived_otype = _tt_s_mp->otable->lookup(args.derived_otid);
1358         if (derived_otype.is_null()) {
1359                 result.str_val = (char *)0;
1360                 result.status = TT_ERR_OTYPE;
1361         } else {
1362                 parents =  derived_otype->parents();
1363                 if (parents.is_null() || parents->is_empty()) {
1364                         result.status = TT_OK;
1365                         result.str_val = (char *)0;
1366                 } else {
1367                         result.status = TT_OK;
1368                         result.str_val = parents->top();
1369                 }
1370         }
1371
1372         if (svc_sendreply(transp, (xdrproc_t)tt_xdr_rpc_result,
1373                           (RPC_ARG_T)&result) == 0) {
1374                 return;
1375         }
1376 }
1377
1378 /* 
1379  * Return 1 iff the derived otype derives directly or indirectly from the
1380  * base otype.
1381  */
1382 void
1383 _tt_rpc_otype_is_derived(SVCXPRT *transp)
1384 {
1385         _Tt_otype_args          args;
1386         _Tt_rpc_result          result;
1387         _Tt_string_list_ptr     parents;
1388         _Tt_otype_ptr           derived_otype;
1389
1390         if (! _tt_svc_getargs(transp,
1391                               (xdrproc_t)tt_xdr_otype_args,
1392                               (char *)&args)) {
1393                 svcerr_decode(transp);
1394                 return;
1395         }
1396
1397         result.status = TT_OK;
1398         result.str_val = "";
1399         if (args.derived_otid==args.base_otid) {
1400                 result.int_val = 1;
1401         } else {
1402                 derived_otype = _tt_s_mp->otable->lookup(args.derived_otid);
1403                 if (derived_otype.is_null()) {
1404                         result.status = TT_ERR_OTYPE;
1405                 } else {
1406                         parents =  derived_otype->parents();
1407                         result.int_val = 0;
1408                         _Tt_otype_ptr parent;
1409                         while (!parents->is_empty()) {
1410                                 parent = _tt_s_mp->otable->
1411                                         lookup(parents->top());
1412                                 if (parent.is_null()) {
1413                                         break;
1414                                 } else if (parent->otid()==args.base_otid) {
1415                                         result.int_val = 1;
1416                                         break;
1417                                 } else {
1418                                         parents = parent->parents();
1419                                 }
1420                         }
1421                 }
1422         }
1423         if (svc_sendreply(transp, (xdrproc_t)tt_xdr_rpc_result,
1424                           (RPC_ARG_T)&result) == 0) {
1425                 return;
1426         }
1427 }
1428
1429 /* 
1430  * Return the number of observer signatures for the specified otype
1431  */
1432 void
1433 _tt_rpc_otype_osig_count(SVCXPRT *transp)
1434 {
1435         _Tt_otype_args          args;
1436         _Tt_rpc_result          result;
1437         _Tt_otype_ptr           otype;
1438
1439         if (! _tt_svc_getargs(transp,
1440                               (xdrproc_t)tt_xdr_otype_args,
1441                               (char *)&args)) {
1442                 svcerr_decode(transp);
1443                 return;
1444         }
1445
1446         result.status = TT_OK;
1447         result.str_val = "";
1448         result.int_val = 0;
1449         otype =  _tt_s_mp->otable->lookup(args.base_otid);
1450         if (otype.is_null()) {
1451                 result.status = TT_ERR_OTYPE;
1452         } else {
1453                 _Tt_signature_list_ptr sigs = otype->osigs();
1454                 result.int_val = sigs->count();
1455         }
1456         if (svc_sendreply(transp, (xdrproc_t)tt_xdr_rpc_result,
1457                           (RPC_ARG_T)&result) == 0) {
1458                 return;
1459         }
1460 }
1461
1462 /* 
1463  * Return the number of handler signatures for the specified otype
1464  */
1465 void
1466 _tt_rpc_otype_hsig_count(SVCXPRT *transp)
1467 {
1468         _Tt_otype_args          args;
1469         _Tt_rpc_result          result;
1470         _Tt_otype_ptr           otype;
1471
1472         if (! _tt_svc_getargs(transp,
1473                               (xdrproc_t)tt_xdr_otype_args,
1474                               (char *)&args)) {
1475                 svcerr_decode(transp);
1476                 return;
1477         }
1478
1479         result.status = TT_OK;
1480         result.str_val = "";
1481         result.int_val = 0;
1482         otype =  _tt_s_mp->otable->lookup(args.base_otid);
1483         if (otype.is_null()) {
1484                 result.status = TT_ERR_OTYPE;
1485         } else {
1486                 _Tt_signature_list_ptr sigs = otype->hsigs();
1487                 result.int_val = sigs->count();
1488         }
1489         if (svc_sendreply(transp, (xdrproc_t)tt_xdr_rpc_result,
1490                           (RPC_ARG_T)&result) == 0) {
1491                 return;
1492         }
1493 }
1494
1495 /* 
1496  * Return the op for an observer signature of an otype
1497  */
1498 void
1499 _tt_rpc_otype_osig_op(SVCXPRT *transp)
1500 {
1501         _Tt_otype_args          args;
1502         _Tt_rpc_result          result;
1503         _Tt_otype_ptr           otype;
1504
1505         if (! _tt_svc_getargs(transp,
1506                               (xdrproc_t)tt_xdr_otype_args,
1507                               (char *)&args)) {
1508                 svcerr_decode(transp);
1509                 return;
1510         }
1511
1512         result.status = TT_OK;
1513         result.str_val = "";
1514         result.int_val = 0;
1515         otype =  _tt_s_mp->otable->lookup(args.base_otid);
1516         if (otype.is_null()) {
1517                 result.status = TT_ERR_OTYPE;
1518         } else {
1519                 _Tt_signature_list_ptr sigs = otype->osigs();
1520                 if (args.num < 0 || args.num >= sigs->count()) {
1521                         result.status = TT_ERR_NUM;
1522                 } else {
1523                         _Tt_signature_ptr sig = (*sigs)[args.num];
1524                         result.str_val = sig->op();
1525                 }
1526         }
1527         if (svc_sendreply(transp, (xdrproc_t)tt_xdr_rpc_result,
1528                           (RPC_ARG_T)&result) == 0) {
1529                 return;
1530         }
1531 }
1532
1533 /* 
1534  * Return the op for a handler signature of an otype
1535  */
1536 void
1537 _tt_rpc_otype_hsig_op(SVCXPRT *transp)
1538 {
1539         _Tt_otype_args          args;
1540         _Tt_rpc_result          result;
1541         _Tt_otype_ptr           otype;
1542
1543         if (! _tt_svc_getargs(transp,
1544                               (xdrproc_t)tt_xdr_otype_args,
1545                               (char *)&args)) {
1546                 svcerr_decode(transp);
1547                 return;
1548         }
1549
1550         result.status = TT_OK;
1551         result.str_val = "";
1552         result.int_val = 0;
1553         otype =  _tt_s_mp->otable->lookup(args.base_otid);
1554         if (otype.is_null()) {
1555                 result.status = TT_ERR_OTYPE;
1556         } else {
1557                 _Tt_signature_list_ptr sigs = otype->hsigs();
1558                 if (args.num < 0 || args.num >= sigs->count()) {
1559                         result.status = TT_ERR_NUM;
1560                 } else {
1561                         _Tt_signature_ptr sig = (*sigs)[args.num];
1562                         result.str_val = sig->op();
1563                 }
1564         }
1565         if (svc_sendreply(transp, (xdrproc_t)tt_xdr_rpc_result,
1566                           (RPC_ARG_T)&result) == 0) {
1567                 return;
1568         }
1569 }
1570
1571 /* 
1572  * Return the number of arguments of an observer signature of an otype
1573  */
1574 void
1575 _tt_rpc_otype_osig_args_count(SVCXPRT *transp)
1576 {
1577         _Tt_otype_args          args;
1578         _Tt_rpc_result          result;
1579         _Tt_otype_ptr           otype;
1580
1581         if (! _tt_svc_getargs(transp,
1582                               (xdrproc_t)tt_xdr_otype_args,
1583                               (char *)&args)) {
1584                 svcerr_decode(transp);
1585                 return;
1586         }
1587
1588         result.status = TT_OK;
1589         result.str_val = "";
1590         result.int_val = 0;
1591         otype =  _tt_s_mp->otable->lookup(args.base_otid);
1592         if (otype.is_null()) {
1593                 result.status = TT_ERR_OTYPE;
1594         } else {
1595                 _Tt_signature_list_ptr sigs = otype->osigs();
1596                 if (args.num < 0 || args.num >= sigs->count()) {
1597                         result.status = TT_ERR_NUM;
1598                 } else {
1599                         _Tt_signature_ptr sig = (*sigs)[args.num];
1600                         _Tt_arg_list_ptr sargs = sig->args();
1601                         result.int_val = sargs->count();
1602                 }
1603         }
1604         if (svc_sendreply(transp, (xdrproc_t)tt_xdr_rpc_result,
1605                           (RPC_ARG_T)&result) == 0) {
1606                 return;
1607         }
1608 }
1609
1610 /* 
1611  * Return the number of arguments of a handler signature of an otype
1612  */
1613 void
1614 _tt_rpc_otype_hsig_args_count(SVCXPRT *transp)
1615 {
1616         _Tt_otype_args          args;
1617         _Tt_rpc_result          result;
1618         _Tt_otype_ptr           otype;
1619
1620         if (! _tt_svc_getargs(transp,
1621                               (xdrproc_t)tt_xdr_otype_args,
1622                               (char *)&args)) {
1623                 svcerr_decode(transp);
1624                 return;
1625         }
1626
1627         result.status = TT_OK;
1628         result.str_val = "";
1629         result.int_val = 0;
1630         otype =  _tt_s_mp->otable->lookup(args.base_otid);
1631         if (otype.is_null()) {
1632                 result.status = TT_ERR_OTYPE;
1633         } else {
1634                 _Tt_signature_list_ptr sigs = otype->hsigs();
1635                 if (args.num < 0 || args.num >= sigs->count()) {
1636                         result.status = TT_ERR_NUM;
1637                 } else {
1638                         _Tt_signature_ptr sig = (*sigs)[args.num];
1639                         _Tt_arg_list_ptr sargs = sig->args();
1640                         result.int_val = sargs->count();
1641                 }
1642         }
1643         if (svc_sendreply(transp, (xdrproc_t)tt_xdr_rpc_result,
1644                           (RPC_ARG_T)&result) == 0) {
1645                 return;
1646         }
1647 }
1648
1649 /* 
1650  * Return the mode of the specified arg of an observer signature of an otype
1651  */
1652 void
1653 _tt_rpc_otype_osig_arg_mode(SVCXPRT *transp)
1654 {
1655         _Tt_otype_args          args;
1656         _Tt_rpc_result          result;
1657         _Tt_otype_ptr           otype;
1658
1659         if (! _tt_svc_getargs(transp,
1660                               (xdrproc_t)tt_xdr_otype_args,
1661                               (char *)&args)) {
1662                 svcerr_decode(transp);
1663                 return;
1664         }
1665
1666         result.status = TT_OK;
1667         result.str_val = "";
1668         result.int_val = 0;
1669         otype =  _tt_s_mp->otable->lookup(args.base_otid);
1670         if (otype.is_null()) {
1671                 result.status = TT_ERR_OTYPE;
1672         } else {
1673                 _Tt_signature_list_ptr sigs = otype->osigs();
1674                 if (args.num < 0 || args.num >= sigs->count()) {
1675                         result.status = TT_ERR_NUM;
1676                 } else {
1677                         _Tt_signature_ptr sig = (*sigs)[args.num];
1678                         _Tt_arg_list_ptr sargs = sig->args();
1679                         if (args.num2 < 0 || args.num2 >= sargs->count()) {
1680                                 result.status = TT_ERR_NUM;
1681                         } else {
1682                                 _Tt_arg_ptr sarg = (*sargs)[args.num2];
1683                                 result.int_val = (int)sarg->mode();
1684                         }
1685                 }
1686         }
1687         if (svc_sendreply(transp, (xdrproc_t)tt_xdr_rpc_result,
1688                           (RPC_ARG_T)&result) == 0) {
1689                 return;
1690         }
1691 }
1692
1693 /* 
1694  * Return the mode of the specified arg of a handler signature of an otype
1695  */
1696 void
1697 _tt_rpc_otype_hsig_arg_mode(SVCXPRT *transp)
1698 {
1699         _Tt_otype_args          args;
1700         _Tt_rpc_result          result;
1701         _Tt_otype_ptr           otype;
1702
1703         if (! _tt_svc_getargs(transp,
1704                               (xdrproc_t)tt_xdr_otype_args,
1705                               (char *)&args)) {
1706                 svcerr_decode(transp);
1707                 return;
1708         }
1709
1710         result.status = TT_OK;
1711         result.str_val = "";
1712         result.int_val = 0;
1713         otype =  _tt_s_mp->otable->lookup(args.base_otid);
1714         if (otype.is_null()) {
1715                 result.status = TT_ERR_OTYPE;
1716         } else {
1717                 _Tt_signature_list_ptr sigs = otype->hsigs();
1718                 if (args.num < 0 || args.num >= sigs->count()) {
1719                         result.status = TT_ERR_NUM;
1720                 } else {
1721                         _Tt_signature_ptr sig = (*sigs)[args.num];
1722                         _Tt_arg_list_ptr sargs = sig->args();
1723                         if (args.num2 < 0 || args.num2 >= sargs->count()) {
1724                                 result.status = TT_ERR_NUM;
1725                         } else {
1726                                 _Tt_arg_ptr sarg = (*sargs)[args.num2];
1727                                 result.int_val = (int)sarg->mode();
1728                         }
1729                 }
1730         }
1731         if (svc_sendreply(transp, (xdrproc_t)tt_xdr_rpc_result,
1732                           (RPC_ARG_T)&result) == 0) {
1733                 return;
1734         }
1735 }
1736
1737 /* 
1738  * Return the type of the specified arg of an observer signature of an otype
1739  */
1740 void
1741 _tt_rpc_otype_osig_arg_type(SVCXPRT *transp)
1742 {
1743         _Tt_otype_args          args;
1744         _Tt_rpc_result          result;
1745         _Tt_otype_ptr           otype;
1746
1747         if (! _tt_svc_getargs(transp,
1748                               (xdrproc_t)tt_xdr_otype_args,
1749                               (char *)&args)) {
1750                 svcerr_decode(transp);
1751                 return;
1752         }
1753
1754         result.status = TT_OK;
1755         result.str_val = "";
1756         result.int_val = 0;
1757         otype =  _tt_s_mp->otable->lookup(args.base_otid);
1758         if (otype.is_null()) {
1759                 result.status = TT_ERR_OTYPE;
1760         } else {
1761                 _Tt_signature_list_ptr sigs = otype->osigs();
1762                 if (args.num < 0 || args.num >= sigs->count()) {
1763                         result.status = TT_ERR_NUM;
1764                 } else {
1765                         _Tt_signature_ptr sig = (*sigs)[args.num];
1766                         _Tt_arg_list_ptr sargs = sig->args();
1767                         if (args.num2 < 0 || args.num2 >= sargs->count()) {
1768                                 result.status = TT_ERR_NUM;
1769                         } else {
1770                                 _Tt_arg_ptr sarg = (*sargs)[args.num2];
1771                                 result.str_val = sarg->type();
1772                         }
1773                 }
1774         }
1775         if (svc_sendreply(transp, (xdrproc_t)tt_xdr_rpc_result,
1776                           (RPC_ARG_T)&result) == 0) {
1777                 return;
1778         }
1779 }
1780
1781 /* 
1782  * Return the type of the specified arg of a handler signature of an otype
1783  */
1784 void
1785 _tt_rpc_otype_hsig_arg_type(SVCXPRT *transp)
1786 {
1787         _Tt_otype_args          args;
1788         _Tt_rpc_result          result;
1789         _Tt_otype_ptr           otype;
1790
1791         if (! _tt_svc_getargs(transp,
1792                               (xdrproc_t)tt_xdr_otype_args,
1793                               (char *)&args)) {
1794                 svcerr_decode(transp);
1795                 return;
1796         }
1797
1798         result.status = TT_OK;
1799         result.str_val = "";
1800         result.int_val = 0;
1801         otype =  _tt_s_mp->otable->lookup(args.base_otid);
1802         if (otype.is_null()) {
1803                 result.status = TT_ERR_OTYPE;
1804         } else {
1805                 _Tt_signature_list_ptr sigs = otype->hsigs();
1806                 if (args.num < 0 || args.num >= sigs->count()) {
1807                         result.status = TT_ERR_NUM;
1808                 } else {
1809                         _Tt_signature_ptr sig = (*sigs)[args.num];
1810                         _Tt_arg_list_ptr sargs = sig->args();
1811                         if (args.num2 < 0 || args.num2 >= sargs->count()) {
1812                                 result.status = TT_ERR_NUM;
1813                         } else {
1814                                 _Tt_arg_ptr sarg = (*sargs)[args.num2];
1815                                 result.str_val = sarg->type();
1816                         }
1817                 }
1818         }
1819         if (svc_sendreply(transp, (xdrproc_t)tt_xdr_rpc_result,
1820                           (RPC_ARG_T)&result) == 0) {
1821                 return;
1822         }
1823 }
1824
1825 //
1826 // Horizontal (inter-ttsession) dispatch
1827 //
1828 void
1829 _tt_rpc_hdispatch(SVCXPRT *transp)
1830 {
1831         _Tt_s_message_ptr               msg;
1832         _Tt_qmsg_info_ptr               qm = (_Tt_qmsg_info *)0;
1833         _Tt_dispatch_reply_args         args;
1834
1835         msg = new _Tt_s_message();
1836         if (!_tt_svc_getargs(transp,
1837                              (xdrproc_t)tt_xdr_message,
1838                              (char *)&msg)) {
1839                 svcerr_decode(transp);
1840                 return;
1841         }
1842         _Tt_msg_trace trace( *msg, TTDR_HDISPATCH );
1843         (void)msg->dispatch( trace );
1844         (void)msg->deliver_to_observers_and_handlers( trace );
1845 }
1846
1847 //
1848 // Horizontal (inter-ttsession) update
1849 //
1850 void
1851 _tt_rpc_hupdate_msg(SVCXPRT *transp)
1852 {
1853         _Tt_s_procid_ptr                sender;
1854         _Tt_s_update_args               args;
1855
1856         if (!_tt_svc_getargs(transp,
1857                              (xdrproc_t)tt_s_xdr_update_args,
1858                              (char *)&args))
1859                 {
1860                         svcerr_decode(transp);
1861                         return;
1862                 }
1863         if (! _tt_s_mp->find_proc(args.message->sender(), sender, 0)) {
1864                 return;
1865         }
1866
1867         if (   (args.newstate == TT_FAILED)
1868                && (args.message->status() == TT_ERR_NO_MATCH)
1869                && (args.message->handler().is_null()))
1870         {
1871                 //
1872                 // No remote session could find a handler.
1873                 // Simulate a rejection, and redeliver the message.
1874                 // _tried prevents re-offers, but any new handler
1875                 // will get a crack at the request, which is nice.
1876                 // _rsessions will be empty, so if we again find no
1877                 // handler, we will perform disposition instead of
1878                 // hdispatch()ing to remote sessions.
1879                 //
1880                 args.message->set_state(TT_SENT);
1881                 args.message->set_status((int)TT_OK);
1882                 _Tt_msg_trace trace( *args.message, TTDR_HUPDATE );
1883                 if (args.message->indoctrinate( trace ) == TT_OK) {
1884                         args.message->change_state( 0, TT_REJECTED, trace );
1885                 }
1886         } else {
1887                 args.message->set_state(args.newstate);
1888                 // Tracing occurs inside ::add_message()
1889                 sender->add_message(args.message);
1890         }
1891 }
1892
1893
1894 void
1895 _tt_rpc_msgread_2(SVCXPRT *transp)
1896 {
1897 #ifdef OPT_ADDMSG_DIRECT
1898         _Tt_procid_ptr          p;
1899         _Tt_s_procid_ptr        rp;
1900
1901         if (! _tt_svc_getargs(transp,
1902                               (xdrproc_t)tt_xdr_procid,
1903                               (char *)&p)) {
1904                 svcerr_decode(transp);
1905         }
1906         if (_tt_s_mp->find_proc(p, rp, 0)) {
1907                 rp->msgread();
1908         }
1909 #else
1910         svcerr_noproc(transp);
1911 #endif                          // OPT_ADDMSG_DIRECT
1912 }
1913
1914
1915 /* 
1916  * Called when a sender sends a message to the server for delivery on exit.
1917  */
1918 void
1919 _tt_rpc_dispatch_on_exit(SVCXPRT *transp)
1920 {
1921         _Tt_s_message_ptr               msg;
1922         _Tt_s_procid_ptr                proc;
1923         _Tt_dispatch_reply_args         args;
1924
1925         msg = new _Tt_s_message();
1926         if (!_tt_svc_getargs(transp,
1927                              (xdrproc_t)tt_xdr_message,
1928                              (char *)&msg)) {
1929                 svcerr_decode(transp);
1930                 return;
1931         }
1932
1933         args.status = TT_OK; 
1934
1935         if (_tt_s_mp->find_proc(msg->sender(),proc, 0)) {
1936                 (void)proc->add_on_exit_message(msg);
1937         } else {
1938                 args.status = TT_ERR_PROCID;
1939         }
1940
1941         if (!svc_sendreply(transp,
1942                            (xdrproc_t)tt_xdr_dispatch_reply_args,
1943                            (RPC_ARG_T)&args)) {
1944                 return;
1945         }
1946
1947 }