1 //%% (c) Copyright 1993, 1994 Hewlett-Packard Company
2 //%% (c) Copyright 1993, 1994 International Business Machines Corp.
3 //%% (c) Copyright 1993, 1994 Sun Microsystems, Inc.
4 //%% (c) Copyright 1993, 1994 Novell, Inc.
5 //%% $TOG: mp_session.C /main/13 1999/09/16 13:46:46 mgreess $
7 * @(#)mp_session.C 1.84 96/01/10
9 * Tool Talk Message Passer (MP) - mp_session.cc
11 * Copyright (c) 1990,1992 by Sun Microsystems, Inc.
13 * Implementation of the _Tt_session class.
15 #include "util/tt_global_env.h"
16 #include "util/tt_xdr_version.h"
17 #include "util/tt_host.h"
18 #include "util/tt_port.h"
19 #include "util/tt_gettext.h"
20 #include "mp/mp_global.h"
21 #include "mp/mp_message.h"
23 #include "mp/mp_c_global.h"
24 #include "mp/mp_c_session.h"
25 #include "mp/mp_c_mp.h"
26 #include "mp/mp_pattern.h"
27 #include "mp/mp_rpc_client.h"
28 #include "mp/mp_rpc_interface.h"
29 #include "mp/mp_session.h"
30 #include "mp/mp_desktop.h"
31 #include "mp/mp_xdr_functions.h"
34 #ifdef OPT_UNIX_SOCKET_RPC
35 # include <sys/socket.h>
37 #endif // OPT_UNIX_SOCKET_RPC
44 _is_server = 0; // default server mode
53 if (! _desktop.is_null()) {
54 if (_is_server && _env == _TT_ENV_X11 && (_id.len() > 0)) {
55 if ( (! _desktop->del_prop(TT_XATOM_NAME))
56 || (! _desktop->del_prop(TT_CDE_XATOM_NAME)))
58 _tt_syslog( 0, LOG_WARNING,
59 catgets( _ttcatd, 1, 14,
60 "could not delete the X "
61 "root window property %s "
68 #ifdef OPT_UNIX_SOCKET_RPC
69 if (_is_server && _socket_name.len()) {
70 (void)unlink((char *)_socket_name);
77 #ifdef OPT_UNIX_SOCKET_RPC
79 // Returns the name of the socket file if the unix socket rpc option is
80 // enabled. Note that the name should be unique for all sessions running
81 // on the same machine so it has to be derived from the session id
82 // (which has the required uniqueness properties).
87 #define SPRFX "/tmp/.TT"
89 if (_socket_name.len() == 0) {
90 char *sname = (char *)malloc(_id.len() + strlen(SPRFX) + 1);
93 sprintf(sname,"%s%s", SPRFX, (char *)_id);
101 _socket_name = sname;
102 free((MALLOCTYPE *)sname);
105 return((char *)_socket_name);
109 // Opens a connection to the unix socket bound to the given socket name.
110 // If successful returns the fd for the connection. Otherwise, returns
114 c_open_unix_socket(char *socket_name)
117 struct sockaddr_un server_addr;
118 sock = socket(AF_UNIX, SOCK_STREAM, 0);
120 _tt_syslog( 0, LOG_ERR, "c_open_unix_socket(): socket(): %m" );
123 memset(&server_addr, 0, sizeof(server_addr));
124 server_addr.sun_family = AF_UNIX;
125 strcpy(server_addr.sun_path, socket_name);
126 #if defined(ultrix) || defined(_AIX) || defined(hpux) || defined(__osf__)
127 int servlen = strlen(server_addr.sun_path) + sizeof(server_addr.sun_fam\
129 if (connect(sock, (sockaddr *)&server_addr, servlen) < 0) {
131 if (connect(sock, (sockaddr *)&server_addr, sizeof(sockaddr_un)) < 0) {
139 #endif // OPT_UNIX_SOCKET_RPC
143 // Called if an event comes in from our desktop connection.
146 desktop_event_callback()
148 if (! _desktop->process_event()) {
157 // Initializes a session with the assumption that the server session is
158 // already running. If the server session isn't running then this method
159 // will return an error as opposed to attempting to autostart a new
160 // session (see _Tt_session::c_init). This is intended as a lower-level
161 // init function used by c_init or whenever autostarting is not required.
165 // TT_ERR_ACCESS Could not init desktop (diagnostic emitted)
166 // TT_WRN_NOTFOUND No advertised address
167 // TT_ERR_INVALID Could not parse advertised address
168 // TT_ERR_NO_MATCH Advertised address's version is too new
169 // (diagnostic emitted)
170 // TT_ERR_ADDRESS Could not find advertised host
171 // TT_ERR_NOMP Could not init as rpc client
173 Tt_status _Tt_session::
174 client_session_init()
176 // this is a client of this session. Note that since this
177 // function may be called in the server, we can't use
178 // _tt_mp->in_server().
181 int rpc_init_done = 0;
186 if (env() == _TT_ENV_X11 && _desktop.is_null()) {
187 _desktop = new _Tt_desktop();
188 if (! _desktop->init(_displayname, _TT_DESKTOP_X11)) {
189 return(TT_ERR_ACCESS);
192 if (_address_string.len() == 0) {
193 if (find_advertised_address(_address_string) != TT_OK) {
194 return(TT_WRN_NOTFOUND);
197 if (_address_string.len() == 0) {
198 // Don't call parsed_address() with an empty string,
199 // or it will think we're trying to create an address
200 // string instead of parse it. parsed_address()
201 // shouldn't be this cute. XXX
202 return TT_ERR_INVALID;
204 if ((err = parsed_address(_address_string)) != TT_OK) {
205 _address_string = (char *)0;
208 if (_TT_AUTH_ICEAUTH == _auth.auth_level() &&
209 (err = _auth.retrieve_auth_cookie()) != TT_OK) {
212 if (! _tt_global->find_host(_hostaddr, _host, 1)) {
213 return(TT_ERR_ADDRESS);
216 if (_id.len() == 0) {
220 #ifdef OPT_UNIX_SOCKET_RPC
221 if (_tt_global->get_local_host(lh)) {
222 // if we are in the same host as the server session
223 // then we attempt to connect to its advertised unix
224 // socket to set up an rpc connection. If we succeed
225 // then we set rpc_init_done to 1 to prevent a normal
226 // tcp rpc connection from being made.
227 if (lh->stringaddr() == _host->stringaddr()) {
228 _u_sock = c_open_unix_socket(local_socket_name());
230 _rpc_client = new _Tt_rpc_client(_u_sock);
231 if (_rpc_client->init(lh, _rpc_program,
236 // Since we opened the fd ourselves,
237 // clnt_destroy won\'t close it
238 // unless we tell it it\'s OK.
239 clnt_control(_rpc_client->rpc_handle(),
246 #endif // OPT_UNIX_SOCKET_RPC
247 if (! rpc_init_done) {
248 _rpc_client = new _Tt_rpc_client();
249 if (! _rpc_client->init(_host, _rpc_program, _rpc_version,
250 _server_uid, _auth)) {
255 // Get and save the file descriptor for this session.
256 _tt_mp->save_session_fd(_rpc_client->socket());
262 // Verifies that the server side of the session is alive and is a
263 // ToolTalk session. Currently this is done by first invoking rpc
264 // procedure 0 to see if the session is responding to rpc requests. If
265 // that succeeds then we invoke rpc procedure TT_RPC_VRFY_SESSION which
266 // is a reasonably large number. The reply to this procedure should be
267 // the session id of the session we're talking to. This is a fairly good
268 // (but not foolproof!) method of verifying that this is a ToolTalk
271 // In fact it seems to be too picky, if the session id we have is
272 // different only in the IP address, it may just be an alternate
273 // IP address for the very same session.
277 // TT_ERR_NOMP Session unreachable or a hoaxer
278 // TT_ERR_NO_MATCH Session has wrong version
279 // TT_ERR_INVALID Could not parse advertised address
280 // TT_ERR_AUTHORIZATION RPC authorization error
282 Tt_status _Tt_session::
288 // We used to call the NULLPROC here first, but that seems
289 // to be a waste of a round-trip since it doesn't tell
290 // us anything that the TT_RPC_VRFY_SESSION call doesn't.
292 is_live = call((int)TT_RPC_VRFY_SESSION,
293 (xdrproc_t)xdr_void, 0,
294 (xdrproc_t)tt_xdr_string,
297 // We used to string-compare the returned session id with the one we
298 // have, but that was too picky since the IP address can usefully be
299 // different. Skip the check for now, perhaps we should check at
300 // least some of the components (UNIX pid?) We already know the
301 // transient RPC number is good, and most everything else is
302 // constant. We do make a minimal check that the id string is non
303 // null, this might catch the (unlikely) case where the program at
304 // the other end isn't a ttsession but does happen to have a
305 // procedure with a number the same as TT_RPC_VRFY_SESSION,
306 // although in that case I'd expect the RPC to fail in the
309 if (is_live == TT_OK && 0 == sid.len()) {
310 is_live = TT_ERR_NOMP;
314 if (is_live == TT_ERR_UNIMP) {
315 is_live = TT_ERR_INTERNAL;
318 // And finally, for reasons I haven't quite figured out,
319 // if the session id we have *isn't* equal to the one returned
320 // by ttsession, file scoped messages fail. I suspect
321 // pattern matching somewhere compares the ids.
322 // So, if the ttsession thinks its name is different, replace
323 // our idea of the name with its idea of its name.
325 if (is_live == TT_OK) {
334 // Invokes an rpc call named by the rpc_proc parameter on the server
335 // session and returns any results. If timeout is anything other than -1
336 // then it will be used as the timeout value. Otherwise a suitable
337 // timeout value is chosen. If rebind is 1 then a new rpc connection will
338 // be attempted if this rpc fails.
342 // TT_ERR_NOMP Could not talk to session
343 // TT_ERR_VERSION_MISMATCH RPC VERSMISMATCH
344 // TT_ERR_UNIMP rpc_proc not implemented by session
345 // TT_ERR_INVALID Could not parse address
346 // TT_ERR_AUTHORIZATION RPC authorization error
348 Tt_status _Tt_session::
350 xdrproc_t xdr_arg_fn, char *arg,
351 xdrproc_t xdr_res_fn, char *res,
352 int timeout, int rebind)
355 // Ensure global xdr version is set to proper value for this session.
357 int xdr_version_2_use = _rpc_version;
359 // Some rpc calls use an xdr version number that is greater
360 // than the version number of the rpc protocol they exist in.
361 // See e.g. _tt_rpc_add_pattern_with_context().
364 case TT_RPC_ADD_PATTERN_WITH_CONTEXT:
365 if (xdr_version_2_use < TT_CONTEXTS_XDR_VERSION) {
366 xdr_version_2_use = TT_CONTEXTS_XDR_VERSION;
370 _Tt_xdr_version ver(xdr_version_2_use);
372 clnt_stat rpc_status;
377 if (_rpc_client.is_null()) {
378 return(TT_ERR_INTERNAL);
383 case TT_RPC_HDISPATCH:
384 case TT_RPC_HUPDATE_MSG:
387 case TT_RPC_DISPATCH_2:
388 case TT_RPC_DISPATCH_2_WITH_CONTEXT:
389 case TT_RPC_UPDATE_MSG_2:
390 case TT_RPC_MSGREAD_2:
394 tmout = TT_RPC_TMOUT;
403 status = TT_ERR_NOMP;
404 while (processing && retry >= 0) {
406 // We sometimes hang if we call a dead session
407 rpc_status = RPC_CANTRECV;
408 if (! _rpc_client->init(_host, _rpc_program,
412 status = TT_ERR_NOMP;
418 rpc_status = _rpc_client->call(rpc_proc, xdr_arg_fn,
419 arg, xdr_res_fn, res,
422 switch (rpc_status) {
429 status = TT_ERR_NOMP;
432 case RPC_VERSMISMATCH:
433 case RPC_PROGVERSMISMATCH:
434 status = TT_ERR_VERSION_MISMATCH;
438 if (tmout == 0 || tmout == -1) {
439 // rpc always returns a timeout error if we're
440 // in message-passing mode
444 status = TT_ERR_NOMP;
450 status = TT_ERR_NOMP;
454 // try to rebind _rpc_client and try again
455 if (! _rpc_client->init(_host, _rpc_program,
459 status = TT_ERR_NOMP;
464 case RPC_PROCUNAVAIL:
465 // We get this if there is a mild version
466 // mismatch, e.g. a 1.1 library talking to
467 // a 1.0.x server, and one of the new
468 // API calls is used. This is relatively
469 // benign, just let the user know that
470 // his call is unimplemented in this system
471 status = TT_ERR_UNIMP;
475 status = TT_ERR_AUTHORIZATION;
479 status = TT_ERR_INTERNAL;
485 if (status == TT_ERR_NOMP &&
486 !_tt_mp->in_server() &&
487 !_tt_c_mp->default_c_session.is_null() &&
488 has_id(_tt_c_mp->default_c_session->address_string())) {
489 _tt_c_mp->default_c_session = NULL;
497 // Attempts to find the address of the server session. This is done
498 // according to what kind of environment or session type we're in.
500 Tt_status _Tt_session::
501 find_advertised_address(_Tt_string &session_addr)
505 if ((! _desktop->get_prop(TT_CDE_XATOM_NAME, session_addr)) &&
506 (! _desktop->get_prop(TT_XATOM_NAME, session_addr))) {
510 case _TT_ENV_PROCESS_TREE:
511 if (_address_string.len() == 0) {
512 session_addr = _tt_get_first_set_env_var(2, TT_CDE_XATOM_NAME, TT_XATOM_NAME);
514 session_addr = _address_string;
525 // Returns a number indicating what type of session we're in.
527 _Tt_env _Tt_session::
530 if (_tt_mp->in_server()) {
531 if (_env == _TT_ENV_LAST && getenv("DISPLAY")) {
535 } else if (_env != _TT_ENV_LAST) {
538 if (_tt_get_first_set_env_var(2, TT_CDE_XATOM_NAME, TT_XATOM_NAME)) {
539 return(_env = _TT_ENV_PROCESS_TREE);
540 } else if (getenv("DISPLAY")) {
541 return(_env = _TT_ENV_X11);
543 return(_TT_ENV_LAST);
550 // Sets the session-type for the session. This affects how the session id
551 // is advertised to potential clients.
554 set_env(_Tt_env session_env, _Tt_string arg)
569 // Returns the ToolTalk session id for the X session named by xdisp. Note
570 // that no actual contact is attempted.
572 _Tt_string _Tt_session::
573 Xid(_Tt_string xdisp)
575 return _desktop->session_name(xdisp);
580 print(FILE *fs) const
582 fprintf(fs,"_Tt_session::\n");
587 // Used to set or get the address string of a session as well as set
588 // appropiately the _rpc_version field of the session. If addr_string is
589 // null, then parsed_address will set the address string of the session.
590 // In this case, it sets _rpc_version to the default TT_RPC_VERSION. If
591 // addr_string is not null then parsed_address will attempt to parse the
592 // string and extract the information in it. In this case, if the
593 // addr_string happens to be an fcs1 version of session addresses then
594 // _rpc_version is set to 1, otherwise it is set to TT_RPC_VERSION.
596 // Note that the intended convention for address strings is that only the
597 // information that is known to this method will be parsed out of them.
598 // If there is any information beyond that then it is left uninterpreted.
599 // This means that future ToolTalk versions are free to append
600 // information to the end of an address string (as is already done
601 // below) without compromising compatibility with older clients.
605 // TT_ERR_INVALID Could not parse address
606 // TT_ERR_NO_MATCH Address version is too new (diagnostic emitted)
608 Tt_status _Tt_session::
609 parsed_address(_Tt_string &addr_string)
611 #define IPVADDRLEN 16
612 char session_host[IPVADDRLEN];
614 int junk_version = 1;
616 const char *addr_format_fmt = "%%ld %%d %%d %%d %%lu %%%ds %%d";
617 char addr_format[32];
618 const char *fcs1_addr_format_fmt = "%%ld %%d %%d %%d %%lu %%%ds";
619 char fcs1_addr_format[32];
622 // Note: the fcs 1.0 version of tooltalk uses the first 6
623 // fields in addr_format. Changing these first 6 fields
624 // compromises binary compatibility with the 1.0 version.
625 // Additional fields can be added at the end of addr_format as
626 // long as if the parsing fails then the old format
627 // (fcs1_addr_format) is tried.
629 _rpc_version = TT_RPC_VERSION;
630 if (addr_string.len() == 0) {
633 // set address string
634 if (_host.is_null()) {
635 return(TT_ERR_INTERNAL);
637 if (_host->stringaddr().len() == 0) {
638 return(TT_ERR_INTERNAL);
641 ipaddr_len = _host->stringaddr().len();
642 if (ipaddr_len > IPVADDRLEN) ipaddr_len = IPVADDRLEN;
644 sprintf(fcs1_addr_format, fcs1_addr_format_fmt, ipaddr_len);
645 sprintf(addr_format, addr_format_fmt, ipaddr_len);
647 // put version number for format first (this is so we
648 // can change the format later)
649 sprintf(strid, "%02d ", _TT_XATOM_VERSION);
650 sprintf(strid+3, addr_format,
654 (int)_auth.auth_level(),
656 (char *)_host->stringaddr(),
660 if ((status = _auth.set_sessionid(
664 _rpc_version)) != TT_OK) {
665 _tt_syslog(0, LOG_ERR,"_auth.set_sessionid() != TT_OK");
669 // get (parse) address string
670 char *str = (char *)addr_string;
672 sprintf(fcs1_addr_format, fcs1_addr_format_fmt, IPVADDRLEN);
673 sprintf(addr_format, addr_format_fmt, IPVADDRLEN);
675 // check version number of format first
677 // XXX: note that if _TT_XATOM_VERSION is ever changed
678 // then backward compatibility is seriously
681 if (atoi(str) != _TT_XATOM_VERSION) {
682 _tt_syslog( 0, LOG_ERR,
683 catgets( _ttcatd, 1, 15,
684 "address version is %d, but "
685 "I only understand %d! (address: "
687 atoi(str), _TT_XATOM_VERSION,
688 addr_string.operator const char *() );
689 return TT_ERR_NO_MATCH;
692 // XXX: Originally, the fcs1_addr_format put in a
693 // field describing the rpc version of the server.
694 // However, the fcs1 version of client_session_init
695 // erroneously initialized the rpc connection with the
696 // *server* rpc version rather than its own. Thus we
697 // can't use the rpc version field in fcs1_addr_format
698 // because then old fcs1.0 clients would mistakenly
699 // initialize themselves as version 1+ clients. Thus
700 // (sigh) we have to add a field at the end of
701 // fcs1_addr_format that is the real rpc version of
702 // the server. This is the reason that the first rpc
703 // version parsed from addr_format is thrown away.
706 long long_server_uid;
707 _Tt_auth_level auth_level;
708 if (7 != sscanf(str+3, addr_format,
711 &junk_version, /* always 1 ... */
717 // new format scan failed. Try to parse the
718 // string for the old format
720 if (6 != sscanf(str+3, fcs1_addr_format,
727 _pid = (pid_t) long_pid;
728 _server_uid = (uid_t) long_server_uid;
729 return(TT_ERR_INVALID);
734 _pid = (pid_t) long_pid;
735 _server_uid = (uid_t) long_server_uid;
736 _hostaddr = session_host;
737 _auth.set_auth_level(auth_level);
738 if ((status = _auth.set_sessionid(
742 _rpc_version)) != TT_OK) {
743 _tt_syslog(0, LOG_ERR,"_auth.set_sessionid() != TT_OK");
753 // Sets the id of a session. Note that addressing information (the rpc
754 // number and version) are not part of the id. This makes it possible in
755 // the future to provide server address rebinding.
757 Tt_status _Tt_session::
765 if (id != (char *)0) {
769 // construct a suitable DISPLAY name from the id
770 // which should have the format "X <host> <server_num>"
771 if (sscanf((char *)id, "X %s %d", host, &svnum) != 2) {
772 return(TT_ERR_SESSION);
774 sprintf(dpname, "%s:%d", host, svnum);
775 _displayname = dpname;
780 // A process-tree session id is just an address string
782 _address_string = _id.mid(2, _id.len());
783 _env = _TT_ENV_PROCESS_TREE;
786 return(TT_ERR_SESSION);
790 // set the "id" which is the publicly visible *logical*
791 // address of this session.
792 ssid = _tt_get_first_set_env_var(2, TT_CDE_START_SID, TT_START_SID);
793 if (ssid != (char *)0) {
799 if (! _displayname.len()) {
800 _displayname = _tt_global->xdisplayname;
802 _id = Xid(_displayname);
804 case _TT_ENV_PROCESS_TREE:
807 // sami said originally:
808 // we need to include the address string in
809 // the id of a process-tree session because
810 // there is no alternate means of finding
811 // the address (ie. for X11 the id could be
812 // rid of address info because the X server
813 // served as the alternate way of finding
815 // rfm sez, 16 June 94:
816 // Of course that alternate way was a bad
817 // idea since it means other users can't
818 // send messages using the session because
819 // they can't contact the X server.
820 _id = process_tree_id();
824 return(TT_ERR_INTERNAL);
834 // Returns a process-tree id for this session. This is essentially just
835 // the address of the session preceded by a "P". The purpose of this is
836 // that whenever this exact session needs to be advertised to a
837 // persistent medium (as is done with file-scope messages) this version
838 // of the session id is used so that if the session goes down and comes
839 // up again, the session id stored on disk will become stale.
841 _Tt_string _Tt_session::
844 _Tt_string result = "P ";
846 return(result.cat(_address_string));
853 return(_id.xdr(xdrs) && _address_string.xdr(xdrs));
857 _Tt_string _Tt_session::
860 return(_address_string);
863 _Tt_string _Tt_session::
866 return(_displayname);
871 _tt_session_address(_Tt_object_ptr &o)
873 return ((_Tt_session *)o.c_pointer())->address_string();
878 // Returns 1 if the given id is the same as the id of this session or if
879 // it is a process-tree id then it has the same address as this session.
882 has_id(const _Tt_string &id)
892 if (id == _address_string) {
897 _Tt_string addr = id.mid(2,id.len());
898 return(addr == _address_string);
905 // ...for strings list
907 has_id(const _Tt_string_list_ptr slist_p)
909 _Tt_string_list_cursor c(slist_p);