Tooltalk fixes for OpenBSD. This consists mainly of #ifdefs, casts and some small...
[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) || defined(CSRG_BASED)
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                     const _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         if(_tt_global->xdr_version() > TT_CONTEXTS_XDR_VERSION) {
797                 _Tt_xdr_version xvers(_tt_global->xdr_version() );
798         } else {
799                 _Tt_xdr_version xvers(TT_CONTEXTS_XDR_VERSION);
800         }
801
802         _tt_rpc_add_pattern(transp);
803 }
804
805
806 /* 
807  * Called when a procid wants to unregister a pattern with the server.
808  */
809 void
810 _tt_rpc_del_pattern(SVCXPRT *transp)
811 {
812         _Tt_s_procid_ptr        proc;
813         _Tt_s_del_pattern_args  args;
814         Tt_status               status;
815
816         if (! _tt_svc_getargs(transp,
817                               (xdrproc_t)tt_s_xdr_del_pattern_args,
818                               (char *)&args)) {
819                 svcerr_decode(transp);
820                 return;
821         }
822
823         if (_tt_s_mp->find_proc(args.procid, proc, 1)) {
824                 _Tt_s_procid    *sp = (_Tt_s_procid *)proc.c_pointer();
825                 status = sp->del_pattern(args.pattern_id);
826         } else {
827                 status = TT_ERR_PROCID;
828         }
829
830         if (svc_sendreply(transp,(xdrproc_t)xdr_int,(RPC_ARG_T)&status) == 0) {
831                 return;
832         }
833 }
834
835
836
837 /* 
838  * Called by a procid to declare a ptype.
839  */
840 void
841 _tt_rpc_declare_ptype(SVCXPRT *transp)
842 {
843         _Tt_declare_ptype_args  args;
844         _Tt_s_procid_ptr        proc;
845         Tt_status               status;
846
847         if (! _tt_svc_getargs(transp,
848                               (xdrproc_t)tt_xdr_declare_ptype_args,
849                               (char *)&args)) {
850                 svcerr_decode(transp);
851                 return;
852         }
853
854         if (_tt_s_mp->find_proc(args.procid, proc, 1)) {
855                 _Tt_s_procid    *sp = (_Tt_s_procid *)proc.c_pointer();
856                 status = sp->declare_ptype(args.ptid);
857         } else {
858                 status = TT_ERR_PROCID;
859         }       
860
861         if (svc_sendreply(transp,(xdrproc_t)xdr_int,(RPC_ARG_T)&status) == 0) {
862                 return;
863         }
864 }
865
866 /* 
867  * Called by a procid to undeclare a ptype.
868  */
869 void
870 _tt_rpc_undeclare_ptype(SVCXPRT *transp)
871 {
872         _Tt_declare_ptype_args  args;
873         _Tt_s_procid_ptr        proc;
874         Tt_status               status;
875
876         if (! _tt_svc_getargs(transp,
877                               (xdrproc_t)tt_xdr_declare_ptype_args,
878                               (char *)&args)) {
879                 svcerr_decode(transp);
880                 return;
881         }
882
883         if (_tt_s_mp->find_proc(args.procid, proc, 1)) {
884                 _Tt_s_procid    *sp = (_Tt_s_procid *)proc.c_pointer();
885                 status = sp->undeclare_ptype(args.ptid);
886         } else {
887                 status = TT_ERR_PROCID;
888         }       
889
890         if (svc_sendreply(transp,(xdrproc_t)xdr_int,(RPC_ARG_T)&status) == 0) {
891                 return;
892         }
893 }
894
895 /* 
896  * Called by a procid to test existence of a ptype.
897  */
898 void
899 _tt_rpc_exists_ptype(SVCXPRT *transp)
900 {
901         _Tt_declare_ptype_args  args;
902         _Tt_s_procid_ptr        proc;
903         Tt_status               status;
904
905         if (! _tt_svc_getargs(transp,
906                               (xdrproc_t)tt_xdr_declare_ptype_args,
907                               (char *)&args)) {
908                 svcerr_decode(transp);
909                 return;
910         }
911
912         if (_tt_s_mp->find_proc(args.procid, proc, 1)) {
913                 _Tt_s_procid    *sp = (_Tt_s_procid *)proc.c_pointer();
914                 status = sp->exists_ptype(args.ptid);
915         } else {
916                 status = TT_ERR_PROCID;
917         }       
918
919         if (svc_sendreply(transp,(xdrproc_t)xdr_int,(RPC_ARG_T)&status) == 0) {
920                 return;
921         }
922 }
923
924
925 /* 
926  * Called by a procid to unblock queued messages for its ptype.
927  */
928 void
929 _tt_rpc_unblock_ptype(SVCXPRT *transp)
930 {
931         _Tt_declare_ptype_args  args;
932         _Tt_s_procid_ptr        proc;
933         Tt_status               status;
934
935         if (! _tt_svc_getargs(transp,
936                               (xdrproc_t)tt_xdr_declare_ptype_args,
937                               (char *)&args)) {
938                 svcerr_decode(transp);
939                 return;
940         }
941
942         if (_tt_s_mp->find_proc(args.procid, proc, 1)) {
943                 _Tt_s_procid    *sp = (_Tt_s_procid *)proc.c_pointer();
944                 status = sp->unblock_ptype(args.ptid);
945         } else {
946                 status = TT_ERR_PROCID;
947         }       
948
949         if (svc_sendreply(transp,(xdrproc_t)xdr_int,(RPC_ARG_T)&status) == 0) {
950                 return;
951         }
952 }
953
954 /* 
955  * Called by a procid to load new types into its associated ttsession.
956  */
957 void
958 _tt_rpc_load_types(SVCXPRT *transp)
959 {
960         _Tt_load_types_args     args;
961         Tt_status               status;
962         XDR                     xdrs;
963
964         if (! _tt_svc_getargs(transp,
965                               (xdrproc_t)tt_xdr_load_types_args,
966                               (char *)&args)) {
967                 svcerr_decode(transp);
968                 return;
969         }
970
971         xdrmem_create(&xdrs,
972                       (char *)args.xdrtypes,
973                       (u_int)args.xdrtypes.len(),
974                       XDR_DECODE);
975
976         int junk;
977         status = _Tt_typedb::merge_from(&xdrs,_tt_s_mp->tdb, junk);
978
979         if (status==TT_OK) {
980                 _tt_s_mp->install_ptable(_tt_s_mp->tdb->ptable);
981                 _tt_s_mp->install_otable(_tt_s_mp->tdb->otable);
982         }
983
984         if (svc_sendreply(transp,(xdrproc_t)xdr_int,(RPC_ARG_T)&status) == 0) {
985                 return;
986         }
987 }
988
989
990
991 /* 
992  * Called by a procid to notify the server of its fd signalling channel. 
993  */
994 void
995 _tt_rpc_set_fd_channel(SVCXPRT *transp)
996 {
997         _Tt_fd_args             fd_args;
998         _Tt_s_procid_ptr        proc;
999         Tt_status               status;
1000
1001         if (! _tt_svc_getargs(transp,
1002                               (xdrproc_t)tt_xdr_fd_args,
1003                               (char *)&fd_args)) {
1004                 svcerr_decode(transp);
1005                 return;
1006         }
1007
1008         if (_tt_s_mp->find_proc(fd_args.procid, proc, 1)) {
1009                 status = TT_OK;
1010         } else {
1011                 status = TT_ERR_PROCID;
1012         }
1013
1014         //
1015         // Note: it's important to reply immediately here because
1016         // setting up the channel involves connecting to the socket
1017         // on the client side so if we attempted to reply after
1018         // the operation then we would get deadlock.
1019         //
1020         if (svc_sendreply(transp,
1021                           (xdrproc_t)xdr_int,
1022                           (RPC_ARG_T)&status) == 0) {
1023                 return;
1024         }
1025
1026         if (status == TT_OK) {
1027                 _Tt_s_procid    *sp = (_Tt_s_procid *)proc.c_pointer();
1028                 sp->set_start_token(fd_args.start_token);
1029                 sp->set_fd_channel(fd_args.fd);
1030         }
1031 }
1032
1033
1034 /* 
1035  * Set the value of a session property
1036  */
1037 void
1038 _tt_rpc_set_prop(SVCXPRT *transp)
1039 {
1040         _Tt_prop_args           args;
1041         Tt_status               status;
1042
1043         if (! _tt_svc_getargs(transp,
1044                               (xdrproc_t)tt_xdr_prop_args,
1045                               (char *)&args)) {
1046                 svcerr_decode(transp);
1047                 return;
1048         }
1049
1050         status = _tt_s_mp->initial_s_session->s_setprop(args.prop, args.value);
1051
1052         if (svc_sendreply(transp,(xdrproc_t)xdr_int,(RPC_ARG_T)&status) == 0) {
1053                 return;
1054         }
1055 }
1056
1057 /* 
1058  * Add a value to a session property
1059  */
1060 void
1061 _tt_rpc_add_prop(SVCXPRT *transp)
1062 {
1063         _Tt_prop_args           args;
1064         Tt_status               status;
1065
1066         if (! _tt_svc_getargs(transp,
1067                               (xdrproc_t)tt_xdr_prop_args,
1068                               (char *)&args)) {
1069                 svcerr_decode(transp);
1070                 return;
1071         }
1072
1073         status = _tt_s_mp->initial_s_session->s_addprop(args.prop, args.value);
1074         if (svc_sendreply(transp,(xdrproc_t)xdr_int,(RPC_ARG_T)&status) == 0) {
1075                 return;
1076         }
1077 }
1078
1079 /* 
1080  * Get a value from a session property
1081  */
1082 void
1083 _tt_rpc_get_prop(SVCXPRT *transp)
1084 {
1085         _Tt_prop_args           args;
1086         Tt_status               status;
1087         _Tt_string              value;
1088         _Tt_rpc_result          result;
1089
1090         if (! _tt_svc_getargs(transp,
1091                               (xdrproc_t)tt_xdr_prop_args,
1092                               (char *)&args)) {
1093                 svcerr_decode(transp);
1094                 return;
1095         }
1096
1097         
1098         if ((status = _tt_s_mp->initial_s_session->s_getprop(args.prop, args.num, value))
1099             != TT_OK) {
1100                 result.status = status;
1101                 result.str_val = "";
1102                 result.int_val = 0;
1103         } else {
1104                 result.status = TT_OK;
1105                 result.str_val = value;
1106                 result.int_val = 0;
1107         }
1108         if (svc_sendreply(transp, (xdrproc_t)tt_xdr_rpc_result,
1109                           (RPC_ARG_T)&result) == 0) {
1110                 return;
1111         }
1112 }
1113
1114 /* 
1115  * Return the number of values for a session property
1116  */
1117 void
1118 _tt_rpc_prop_count(SVCXPRT *transp)
1119 {
1120         _Tt_prop_args           args;
1121         int                     cnt;
1122         _Tt_rpc_result          result;
1123
1124         if (! _tt_svc_getargs(transp,
1125                               (xdrproc_t)tt_xdr_prop_args,
1126                               (char *)&args)) {
1127                 svcerr_decode(transp);
1128                 return;
1129         }
1130
1131         
1132         result.status = _tt_s_mp->initial_s_session->s_propcount(args.prop, cnt);
1133         result.str_val = "";
1134         if (result.status == TT_OK) {
1135                 result.int_val = cnt;
1136         } else {
1137                 result.int_val = 0;
1138         }
1139
1140         if (svc_sendreply(transp, (xdrproc_t)tt_xdr_rpc_result,
1141                           (RPC_ARG_T)&result) == 0) {
1142                 return;
1143         }
1144 }
1145
1146 /* 
1147  * Return a session property name
1148  */
1149 void
1150 _tt_rpc_prop_name(SVCXPRT *transp)
1151 {
1152         _Tt_prop_args           args;
1153         Tt_status               status;
1154         _Tt_string              prop;
1155         _Tt_rpc_result          result;
1156
1157         if (! _tt_svc_getargs(transp,
1158                               (xdrproc_t)tt_xdr_prop_args,
1159                               (char *)&args)) {
1160                 svcerr_decode(transp);
1161                 return;
1162         }
1163
1164         if ((status = _tt_s_mp->initial_s_session->s_propname(args.num, prop))
1165             != TT_OK) {
1166                 result.status = status;
1167                 result.str_val = "";
1168                 result.int_val = 0;
1169         } else {
1170                 result.status = TT_OK;
1171                 result.str_val = prop;
1172                 result.int_val = 0;
1173         }
1174
1175         if (svc_sendreply(transp, (xdrproc_t)tt_xdr_rpc_result,
1176                           (RPC_ARG_T)&result) == 0) {
1177                 return;
1178         }
1179 }
1180
1181 /* 
1182  * Return the number of session properties
1183  */
1184 void
1185 _tt_rpc_prop_names_count(SVCXPRT *transp)
1186 {
1187         _Tt_prop_args           args;
1188         int                     cnt;
1189         _Tt_rpc_result          result;
1190
1191         if (! _tt_svc_getargs(transp,
1192                               (xdrproc_t)tt_xdr_prop_args,
1193                               (char *)&args)) {
1194                 svcerr_decode(transp);
1195                 return;
1196         }
1197
1198         result.status = _tt_s_mp->initial_s_session->s_propnames_count(cnt);
1199         result.str_val = "";
1200         if (result.status == TT_OK) {
1201                 result.int_val = cnt;
1202         } else {
1203                 result.int_val = 0;
1204         }
1205
1206         if (svc_sendreply(transp, (xdrproc_t)tt_xdr_rpc_result,
1207                           (RPC_ARG_T)&result) == 0) {
1208                 return;
1209         }
1210 }
1211
1212
1213 void
1214 _tt_rpc_alloc_procid_key(SVCXPRT *transp)
1215 {
1216         _Tt_string              result;
1217
1218         if (! _tt_svc_getargs(transp,(xdrproc_t)xdr_void,0)) {
1219                 svcerr_decode(transp);
1220                 return;
1221         }
1222
1223         result = _tt_s_mp->alloc_procid_key();
1224
1225         if (svc_sendreply(transp, (xdrproc_t)tt_xdr_string,
1226                           (RPC_ARG_T)&result) == 0) {
1227                 return;
1228         }
1229 }
1230
1231 void
1232 _tt_rpc_close_procid(SVCXPRT *transp)
1233 {
1234         _Tt_procid_ptr          p;
1235         _Tt_s_procid_ptr        rp;
1236         Tt_status               status;
1237
1238         if (! _tt_svc_getargs(transp,
1239                               (xdrproc_t)tt_xdr_procid,
1240                               (char *)&p)) {
1241                 svcerr_decode(transp);
1242         }
1243         if (_tt_s_mp->find_proc(p, rp, 0)) {
1244                 status = _tt_s_mp->s_remove_procid(*rp);
1245         } else {
1246                 status = TT_ERR_PROCID;
1247         }
1248         (void)svc_sendreply(transp,(xdrproc_t)xdr_int, (RPC_ARG_T)&status);
1249 }
1250
1251 void
1252 _tt_rpc_vrfy_session(SVCXPRT *transp)
1253 {
1254         _Tt_string              id;
1255
1256         if (! _tt_svc_getargs(transp, (xdrproc_t)xdr_void, 0)) {
1257                 svcerr_decode(transp);
1258                 return;
1259         }
1260         if (_tt_global->xdr_version() == 1) {
1261                 id = _tt_s_mp->initial_s_session->id();
1262         } else {
1263                 id = _tt_s_mp->initial_s_session->process_tree_id();
1264         }
1265         if (!svc_sendreply(transp,
1266                            (xdrproc_t)tt_xdr_string,
1267                            (RPC_ARG_T)&id)) {
1268                 svcerr_decode(transp);
1269         }
1270 }
1271 /* 
1272  * Return the number of otypes derived from a base otype
1273  */
1274 void
1275 _tt_rpc_otype_deriveds_count(SVCXPRT *transp)
1276 {
1277         _Tt_otype_args          args;
1278         _Tt_rpc_result          result;
1279         _Tt_otype_ptr           base_otype;
1280
1281         if (! _tt_svc_getargs(transp,
1282                               (xdrproc_t)tt_xdr_otype_args,
1283                               (char *)&args)) {
1284                 svcerr_decode(transp);
1285                 return;
1286         }
1287
1288         result.str_val = "";
1289         base_otype =  _tt_s_mp->otable->lookup(args.base_otid);
1290         if (base_otype.is_null()) {
1291                 result.status = TT_ERR_OTYPE;
1292         } else {
1293                 result.int_val = base_otype->children()->count();
1294                 result.status = TT_OK;
1295         }
1296
1297         if (svc_sendreply(transp, (xdrproc_t)tt_xdr_rpc_result,
1298                           (RPC_ARG_T)&result) == 0) {
1299                 return;
1300         }
1301 }
1302
1303 /* 
1304  * Return the n'th otype derived from a base otype
1305  */
1306 void
1307 _tt_rpc_otype_derived(SVCXPRT *transp)
1308 {
1309         _Tt_otype_args          args;
1310         _Tt_rpc_result          result;
1311         _Tt_string_list_ptr     children;
1312         _Tt_otype_ptr           base_otype;
1313
1314         if (! _tt_svc_getargs(transp,
1315                               (xdrproc_t)tt_xdr_otype_args,
1316                               (char *)&args)) {
1317                 svcerr_decode(transp);
1318                 return;
1319         }
1320
1321         result.str_val = "";
1322         base_otype =  _tt_s_mp->otable->lookup(args.base_otid);
1323         if (base_otype.is_null()) {
1324                 result.status = TT_ERR_OTYPE;
1325         } else {
1326                 children =  base_otype->children();
1327                 if (args.num<0 || children->count()<args.num) {
1328                         result.status = TT_ERR_NUM;
1329                         result.str_val = "";
1330                 } else if (children->count()==args.num) {
1331                         result.status = TT_OK;
1332                         result.str_val = (char *)0;
1333                 } else {
1334                         result.status = TT_OK;
1335                         result.str_val = (*children)[args.num];
1336                 }
1337         }
1338         if (svc_sendreply(transp, (xdrproc_t)tt_xdr_rpc_result,
1339                           (RPC_ARG_T)&result) == 0) {
1340                 return;
1341         }
1342 }
1343
1344 /* 
1345  * Return the otype that is the base of a derived otype
1346  */
1347 void
1348 _tt_rpc_otype_base(SVCXPRT *transp)
1349 {
1350         _Tt_otype_args          args;
1351         _Tt_rpc_result          result;
1352         _Tt_string_list_ptr     parents;
1353         _Tt_otype_ptr           derived_otype;
1354
1355         if (! _tt_svc_getargs(transp,
1356                               (xdrproc_t)tt_xdr_otype_args,
1357                               (char *)&args)) {
1358                 svcerr_decode(transp);
1359                 return;
1360         }
1361
1362         derived_otype = _tt_s_mp->otable->lookup(args.derived_otid);
1363         if (derived_otype.is_null()) {
1364                 result.str_val = (char *)0;
1365                 result.status = TT_ERR_OTYPE;
1366         } else {
1367                 parents =  derived_otype->parents();
1368                 if (parents.is_null() || parents->is_empty()) {
1369                         result.status = TT_OK;
1370                         result.str_val = (char *)0;
1371                 } else {
1372                         result.status = TT_OK;
1373                         result.str_val = parents->top();
1374                 }
1375         }
1376
1377         if (svc_sendreply(transp, (xdrproc_t)tt_xdr_rpc_result,
1378                           (RPC_ARG_T)&result) == 0) {
1379                 return;
1380         }
1381 }
1382
1383 /* 
1384  * Return 1 iff the derived otype derives directly or indirectly from the
1385  * base otype.
1386  */
1387 void
1388 _tt_rpc_otype_is_derived(SVCXPRT *transp)
1389 {
1390         _Tt_otype_args          args;
1391         _Tt_rpc_result          result;
1392         _Tt_string_list_ptr     parents;
1393         _Tt_otype_ptr           derived_otype;
1394
1395         if (! _tt_svc_getargs(transp,
1396                               (xdrproc_t)tt_xdr_otype_args,
1397                               (char *)&args)) {
1398                 svcerr_decode(transp);
1399                 return;
1400         }
1401
1402         result.status = TT_OK;
1403         result.str_val = "";
1404         if (args.derived_otid==args.base_otid) {
1405                 result.int_val = 1;
1406         } else {
1407                 derived_otype = _tt_s_mp->otable->lookup(args.derived_otid);
1408                 if (derived_otype.is_null()) {
1409                         result.status = TT_ERR_OTYPE;
1410                 } else {
1411                         parents =  derived_otype->parents();
1412                         result.int_val = 0;
1413                         _Tt_otype_ptr parent;
1414                         while (!parents->is_empty()) {
1415                                 parent = _tt_s_mp->otable->
1416                                         lookup(parents->top());
1417                                 if (parent.is_null()) {
1418                                         break;
1419                                 } else if (parent->otid()==args.base_otid) {
1420                                         result.int_val = 1;
1421                                         break;
1422                                 } else {
1423                                         parents = parent->parents();
1424                                 }
1425                         }
1426                 }
1427         }
1428         if (svc_sendreply(transp, (xdrproc_t)tt_xdr_rpc_result,
1429                           (RPC_ARG_T)&result) == 0) {
1430                 return;
1431         }
1432 }
1433
1434 /* 
1435  * Return the number of observer signatures for the specified otype
1436  */
1437 void
1438 _tt_rpc_otype_osig_count(SVCXPRT *transp)
1439 {
1440         _Tt_otype_args          args;
1441         _Tt_rpc_result          result;
1442         _Tt_otype_ptr           otype;
1443
1444         if (! _tt_svc_getargs(transp,
1445                               (xdrproc_t)tt_xdr_otype_args,
1446                               (char *)&args)) {
1447                 svcerr_decode(transp);
1448                 return;
1449         }
1450
1451         result.status = TT_OK;
1452         result.str_val = "";
1453         result.int_val = 0;
1454         otype =  _tt_s_mp->otable->lookup(args.base_otid);
1455         if (otype.is_null()) {
1456                 result.status = TT_ERR_OTYPE;
1457         } else {
1458                 _Tt_signature_list_ptr sigs = otype->osigs();
1459                 result.int_val = sigs->count();
1460         }
1461         if (svc_sendreply(transp, (xdrproc_t)tt_xdr_rpc_result,
1462                           (RPC_ARG_T)&result) == 0) {
1463                 return;
1464         }
1465 }
1466
1467 /* 
1468  * Return the number of handler signatures for the specified otype
1469  */
1470 void
1471 _tt_rpc_otype_hsig_count(SVCXPRT *transp)
1472 {
1473         _Tt_otype_args          args;
1474         _Tt_rpc_result          result;
1475         _Tt_otype_ptr           otype;
1476
1477         if (! _tt_svc_getargs(transp,
1478                               (xdrproc_t)tt_xdr_otype_args,
1479                               (char *)&args)) {
1480                 svcerr_decode(transp);
1481                 return;
1482         }
1483
1484         result.status = TT_OK;
1485         result.str_val = "";
1486         result.int_val = 0;
1487         otype =  _tt_s_mp->otable->lookup(args.base_otid);
1488         if (otype.is_null()) {
1489                 result.status = TT_ERR_OTYPE;
1490         } else {
1491                 _Tt_signature_list_ptr sigs = otype->hsigs();
1492                 result.int_val = sigs->count();
1493         }
1494         if (svc_sendreply(transp, (xdrproc_t)tt_xdr_rpc_result,
1495                           (RPC_ARG_T)&result) == 0) {
1496                 return;
1497         }
1498 }
1499
1500 /* 
1501  * Return the op for an observer signature of an otype
1502  */
1503 void
1504 _tt_rpc_otype_osig_op(SVCXPRT *transp)
1505 {
1506         _Tt_otype_args          args;
1507         _Tt_rpc_result          result;
1508         _Tt_otype_ptr           otype;
1509
1510         if (! _tt_svc_getargs(transp,
1511                               (xdrproc_t)tt_xdr_otype_args,
1512                               (char *)&args)) {
1513                 svcerr_decode(transp);
1514                 return;
1515         }
1516
1517         result.status = TT_OK;
1518         result.str_val = "";
1519         result.int_val = 0;
1520         otype =  _tt_s_mp->otable->lookup(args.base_otid);
1521         if (otype.is_null()) {
1522                 result.status = TT_ERR_OTYPE;
1523         } else {
1524                 _Tt_signature_list_ptr sigs = otype->osigs();
1525                 if (args.num < 0 || args.num >= sigs->count()) {
1526                         result.status = TT_ERR_NUM;
1527                 } else {
1528                         _Tt_signature_ptr sig = (*sigs)[args.num];
1529                         result.str_val = sig->op();
1530                 }
1531         }
1532         if (svc_sendreply(transp, (xdrproc_t)tt_xdr_rpc_result,
1533                           (RPC_ARG_T)&result) == 0) {
1534                 return;
1535         }
1536 }
1537
1538 /* 
1539  * Return the op for a handler signature of an otype
1540  */
1541 void
1542 _tt_rpc_otype_hsig_op(SVCXPRT *transp)
1543 {
1544         _Tt_otype_args          args;
1545         _Tt_rpc_result          result;
1546         _Tt_otype_ptr           otype;
1547
1548         if (! _tt_svc_getargs(transp,
1549                               (xdrproc_t)tt_xdr_otype_args,
1550                               (char *)&args)) {
1551                 svcerr_decode(transp);
1552                 return;
1553         }
1554
1555         result.status = TT_OK;
1556         result.str_val = "";
1557         result.int_val = 0;
1558         otype =  _tt_s_mp->otable->lookup(args.base_otid);
1559         if (otype.is_null()) {
1560                 result.status = TT_ERR_OTYPE;
1561         } else {
1562                 _Tt_signature_list_ptr sigs = otype->hsigs();
1563                 if (args.num < 0 || args.num >= sigs->count()) {
1564                         result.status = TT_ERR_NUM;
1565                 } else {
1566                         _Tt_signature_ptr sig = (*sigs)[args.num];
1567                         result.str_val = sig->op();
1568                 }
1569         }
1570         if (svc_sendreply(transp, (xdrproc_t)tt_xdr_rpc_result,
1571                           (RPC_ARG_T)&result) == 0) {
1572                 return;
1573         }
1574 }
1575
1576 /* 
1577  * Return the number of arguments of an observer signature of an otype
1578  */
1579 void
1580 _tt_rpc_otype_osig_args_count(SVCXPRT *transp)
1581 {
1582         _Tt_otype_args          args;
1583         _Tt_rpc_result          result;
1584         _Tt_otype_ptr           otype;
1585
1586         if (! _tt_svc_getargs(transp,
1587                               (xdrproc_t)tt_xdr_otype_args,
1588                               (char *)&args)) {
1589                 svcerr_decode(transp);
1590                 return;
1591         }
1592
1593         result.status = TT_OK;
1594         result.str_val = "";
1595         result.int_val = 0;
1596         otype =  _tt_s_mp->otable->lookup(args.base_otid);
1597         if (otype.is_null()) {
1598                 result.status = TT_ERR_OTYPE;
1599         } else {
1600                 _Tt_signature_list_ptr sigs = otype->osigs();
1601                 if (args.num < 0 || args.num >= sigs->count()) {
1602                         result.status = TT_ERR_NUM;
1603                 } else {
1604                         _Tt_signature_ptr sig = (*sigs)[args.num];
1605                         _Tt_arg_list_ptr sargs = sig->args();
1606                         result.int_val = sargs->count();
1607                 }
1608         }
1609         if (svc_sendreply(transp, (xdrproc_t)tt_xdr_rpc_result,
1610                           (RPC_ARG_T)&result) == 0) {
1611                 return;
1612         }
1613 }
1614
1615 /* 
1616  * Return the number of arguments of a handler signature of an otype
1617  */
1618 void
1619 _tt_rpc_otype_hsig_args_count(SVCXPRT *transp)
1620 {
1621         _Tt_otype_args          args;
1622         _Tt_rpc_result          result;
1623         _Tt_otype_ptr           otype;
1624
1625         if (! _tt_svc_getargs(transp,
1626                               (xdrproc_t)tt_xdr_otype_args,
1627                               (char *)&args)) {
1628                 svcerr_decode(transp);
1629                 return;
1630         }
1631
1632         result.status = TT_OK;
1633         result.str_val = "";
1634         result.int_val = 0;
1635         otype =  _tt_s_mp->otable->lookup(args.base_otid);
1636         if (otype.is_null()) {
1637                 result.status = TT_ERR_OTYPE;
1638         } else {
1639                 _Tt_signature_list_ptr sigs = otype->hsigs();
1640                 if (args.num < 0 || args.num >= sigs->count()) {
1641                         result.status = TT_ERR_NUM;
1642                 } else {
1643                         _Tt_signature_ptr sig = (*sigs)[args.num];
1644                         _Tt_arg_list_ptr sargs = sig->args();
1645                         result.int_val = sargs->count();
1646                 }
1647         }
1648         if (svc_sendreply(transp, (xdrproc_t)tt_xdr_rpc_result,
1649                           (RPC_ARG_T)&result) == 0) {
1650                 return;
1651         }
1652 }
1653
1654 /* 
1655  * Return the mode of the specified arg of an observer signature of an otype
1656  */
1657 void
1658 _tt_rpc_otype_osig_arg_mode(SVCXPRT *transp)
1659 {
1660         _Tt_otype_args          args;
1661         _Tt_rpc_result          result;
1662         _Tt_otype_ptr           otype;
1663
1664         if (! _tt_svc_getargs(transp,
1665                               (xdrproc_t)tt_xdr_otype_args,
1666                               (char *)&args)) {
1667                 svcerr_decode(transp);
1668                 return;
1669         }
1670
1671         result.status = TT_OK;
1672         result.str_val = "";
1673         result.int_val = 0;
1674         otype =  _tt_s_mp->otable->lookup(args.base_otid);
1675         if (otype.is_null()) {
1676                 result.status = TT_ERR_OTYPE;
1677         } else {
1678                 _Tt_signature_list_ptr sigs = otype->osigs();
1679                 if (args.num < 0 || args.num >= sigs->count()) {
1680                         result.status = TT_ERR_NUM;
1681                 } else {
1682                         _Tt_signature_ptr sig = (*sigs)[args.num];
1683                         _Tt_arg_list_ptr sargs = sig->args();
1684                         if (args.num2 < 0 || args.num2 >= sargs->count()) {
1685                                 result.status = TT_ERR_NUM;
1686                         } else {
1687                                 _Tt_arg_ptr sarg = (*sargs)[args.num2];
1688                                 result.int_val = (int)sarg->mode();
1689                         }
1690                 }
1691         }
1692         if (svc_sendreply(transp, (xdrproc_t)tt_xdr_rpc_result,
1693                           (RPC_ARG_T)&result) == 0) {
1694                 return;
1695         }
1696 }
1697
1698 /* 
1699  * Return the mode of the specified arg of a handler signature of an otype
1700  */
1701 void
1702 _tt_rpc_otype_hsig_arg_mode(SVCXPRT *transp)
1703 {
1704         _Tt_otype_args          args;
1705         _Tt_rpc_result          result;
1706         _Tt_otype_ptr           otype;
1707
1708         if (! _tt_svc_getargs(transp,
1709                               (xdrproc_t)tt_xdr_otype_args,
1710                               (char *)&args)) {
1711                 svcerr_decode(transp);
1712                 return;
1713         }
1714
1715         result.status = TT_OK;
1716         result.str_val = "";
1717         result.int_val = 0;
1718         otype =  _tt_s_mp->otable->lookup(args.base_otid);
1719         if (otype.is_null()) {
1720                 result.status = TT_ERR_OTYPE;
1721         } else {
1722                 _Tt_signature_list_ptr sigs = otype->hsigs();
1723                 if (args.num < 0 || args.num >= sigs->count()) {
1724                         result.status = TT_ERR_NUM;
1725                 } else {
1726                         _Tt_signature_ptr sig = (*sigs)[args.num];
1727                         _Tt_arg_list_ptr sargs = sig->args();
1728                         if (args.num2 < 0 || args.num2 >= sargs->count()) {
1729                                 result.status = TT_ERR_NUM;
1730                         } else {
1731                                 _Tt_arg_ptr sarg = (*sargs)[args.num2];
1732                                 result.int_val = (int)sarg->mode();
1733                         }
1734                 }
1735         }
1736         if (svc_sendreply(transp, (xdrproc_t)tt_xdr_rpc_result,
1737                           (RPC_ARG_T)&result) == 0) {
1738                 return;
1739         }
1740 }
1741
1742 /* 
1743  * Return the type of the specified arg of an observer signature of an otype
1744  */
1745 void
1746 _tt_rpc_otype_osig_arg_type(SVCXPRT *transp)
1747 {
1748         _Tt_otype_args          args;
1749         _Tt_rpc_result          result;
1750         _Tt_otype_ptr           otype;
1751
1752         if (! _tt_svc_getargs(transp,
1753                               (xdrproc_t)tt_xdr_otype_args,
1754                               (char *)&args)) {
1755                 svcerr_decode(transp);
1756                 return;
1757         }
1758
1759         result.status = TT_OK;
1760         result.str_val = "";
1761         result.int_val = 0;
1762         otype =  _tt_s_mp->otable->lookup(args.base_otid);
1763         if (otype.is_null()) {
1764                 result.status = TT_ERR_OTYPE;
1765         } else {
1766                 _Tt_signature_list_ptr sigs = otype->osigs();
1767                 if (args.num < 0 || args.num >= sigs->count()) {
1768                         result.status = TT_ERR_NUM;
1769                 } else {
1770                         _Tt_signature_ptr sig = (*sigs)[args.num];
1771                         _Tt_arg_list_ptr sargs = sig->args();
1772                         if (args.num2 < 0 || args.num2 >= sargs->count()) {
1773                                 result.status = TT_ERR_NUM;
1774                         } else {
1775                                 _Tt_arg_ptr sarg = (*sargs)[args.num2];
1776                                 result.str_val = sarg->type();
1777                         }
1778                 }
1779         }
1780         if (svc_sendreply(transp, (xdrproc_t)tt_xdr_rpc_result,
1781                           (RPC_ARG_T)&result) == 0) {
1782                 return;
1783         }
1784 }
1785
1786 /* 
1787  * Return the type of the specified arg of a handler signature of an otype
1788  */
1789 void
1790 _tt_rpc_otype_hsig_arg_type(SVCXPRT *transp)
1791 {
1792         _Tt_otype_args          args;
1793         _Tt_rpc_result          result;
1794         _Tt_otype_ptr           otype;
1795
1796         if (! _tt_svc_getargs(transp,
1797                               (xdrproc_t)tt_xdr_otype_args,
1798                               (char *)&args)) {
1799                 svcerr_decode(transp);
1800                 return;
1801         }
1802
1803         result.status = TT_OK;
1804         result.str_val = "";
1805         result.int_val = 0;
1806         otype =  _tt_s_mp->otable->lookup(args.base_otid);
1807         if (otype.is_null()) {
1808                 result.status = TT_ERR_OTYPE;
1809         } else {
1810                 _Tt_signature_list_ptr sigs = otype->hsigs();
1811                 if (args.num < 0 || args.num >= sigs->count()) {
1812                         result.status = TT_ERR_NUM;
1813                 } else {
1814                         _Tt_signature_ptr sig = (*sigs)[args.num];
1815                         _Tt_arg_list_ptr sargs = sig->args();
1816                         if (args.num2 < 0 || args.num2 >= sargs->count()) {
1817                                 result.status = TT_ERR_NUM;
1818                         } else {
1819                                 _Tt_arg_ptr sarg = (*sargs)[args.num2];
1820                                 result.str_val = sarg->type();
1821                         }
1822                 }
1823         }
1824         if (svc_sendreply(transp, (xdrproc_t)tt_xdr_rpc_result,
1825                           (RPC_ARG_T)&result) == 0) {
1826                 return;
1827         }
1828 }
1829
1830 //
1831 // Horizontal (inter-ttsession) dispatch
1832 //
1833 void
1834 _tt_rpc_hdispatch(SVCXPRT *transp)
1835 {
1836         _Tt_s_message_ptr               msg;
1837         _Tt_qmsg_info_ptr               qm = (_Tt_qmsg_info *)0;
1838         _Tt_dispatch_reply_args         args;
1839
1840         msg = new _Tt_s_message();
1841         if (!_tt_svc_getargs(transp,
1842                              (xdrproc_t)tt_xdr_message,
1843                              (char *)&msg)) {
1844                 svcerr_decode(transp);
1845                 return;
1846         }
1847         _Tt_msg_trace trace( *msg, TTDR_HDISPATCH );
1848         (void)msg->dispatch( trace );
1849         (void)msg->deliver_to_observers_and_handlers( trace );
1850 }
1851
1852 //
1853 // Horizontal (inter-ttsession) update
1854 //
1855 void
1856 _tt_rpc_hupdate_msg(SVCXPRT *transp)
1857 {
1858         _Tt_s_procid_ptr                sender;
1859         _Tt_s_update_args               args;
1860
1861         if (!_tt_svc_getargs(transp,
1862                              (xdrproc_t)tt_s_xdr_update_args,
1863                              (char *)&args))
1864                 {
1865                         svcerr_decode(transp);
1866                         return;
1867                 }
1868         if (! _tt_s_mp->find_proc(args.message->sender(), sender, 0)) {
1869                 return;
1870         }
1871
1872         if (   (args.newstate == TT_FAILED)
1873                && (args.message->status() == TT_ERR_NO_MATCH)
1874                && (args.message->handler().is_null()))
1875         {
1876                 //
1877                 // No remote session could find a handler.
1878                 // Simulate a rejection, and redeliver the message.
1879                 // _tried prevents re-offers, but any new handler
1880                 // will get a crack at the request, which is nice.
1881                 // _rsessions will be empty, so if we again find no
1882                 // handler, we will perform disposition instead of
1883                 // hdispatch()ing to remote sessions.
1884                 //
1885                 args.message->set_state(TT_SENT);
1886                 args.message->set_status((int)TT_OK);
1887                 _Tt_msg_trace trace( *args.message, TTDR_HUPDATE );
1888                 if (args.message->indoctrinate( trace ) == TT_OK) {
1889                         args.message->change_state( 0, TT_REJECTED, trace );
1890                 }
1891         } else {
1892                 args.message->set_state(args.newstate);
1893                 // Tracing occurs inside ::add_message()
1894                 sender->add_message(args.message);
1895         }
1896 }
1897
1898
1899 void
1900 _tt_rpc_msgread_2(SVCXPRT *transp)
1901 {
1902 #ifdef OPT_ADDMSG_DIRECT
1903         _Tt_procid_ptr          p;
1904         _Tt_s_procid_ptr        rp;
1905
1906         if (! _tt_svc_getargs(transp,
1907                               (xdrproc_t)tt_xdr_procid,
1908                               (char *)&p)) {
1909                 svcerr_decode(transp);
1910         }
1911         if (_tt_s_mp->find_proc(p, rp, 0)) {
1912                 rp->msgread();
1913         }
1914 #else
1915         svcerr_noproc(transp);
1916 #endif                          // OPT_ADDMSG_DIRECT
1917 }
1918
1919
1920 /* 
1921  * Called when a sender sends a message to the server for delivery on exit.
1922  */
1923 void
1924 _tt_rpc_dispatch_on_exit(SVCXPRT *transp)
1925 {
1926         _Tt_s_message_ptr               msg;
1927         _Tt_s_procid_ptr                proc;
1928         _Tt_dispatch_reply_args         args;
1929
1930         msg = new _Tt_s_message();
1931         if (!_tt_svc_getargs(transp,
1932                              (xdrproc_t)tt_xdr_message,
1933                              (char *)&msg)) {
1934                 svcerr_decode(transp);
1935                 return;
1936         }
1937
1938         args.status = TT_OK; 
1939
1940         if (_tt_s_mp->find_proc(msg->sender(),proc, 0)) {
1941                 (void)proc->add_on_exit_message(msg);
1942         } else {
1943                 args.status = TT_ERR_PROCID;
1944         }
1945
1946         if (!svc_sendreply(transp,
1947                            (xdrproc_t)tt_xdr_dispatch_reply_args,
1948                            (RPC_ARG_T)&args)) {
1949                 return;
1950         }
1951
1952 }