2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
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
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_procid.C /main/4 1997/07/30 15:43:53 samborn $
30 * mp_procid.cc -- methods for communicating with active processes
32 * Copyright (c) 1990 by Sun Microsystems, Inc.
36 #include "util/tt_host.h"
37 #include "mp/mp_file.h"
38 #include "mp/mp_message.h"
40 #include "mp/mp_pattern.h"
41 #include "mp/mp_procid.h"
42 #include "mp/mp_session.h"
43 #include "mp/mp_stream_socket.h"
44 #include "util/tt_int_rec.h"
45 #include "util/tt_global_env.h"
46 #include <arpa/inet.h>
60 _mxdr_stream = (XDR *)0;
71 // Returns the fd which will be used to signal this procid if there are
72 // new messages for it. -1 means error. This method is only used in
73 // client mode. This method caches the fd returned from the
74 // _Tt_stream_socket::fd method. This method has the side-effect that it
75 // will accept the connection on the socket if not already done. See the
76 // comments in _Tt_c_procid::set_fd_channel for more details.
81 if (_socket.is_null()) {
83 } else if (_fd == -1) {
92 // Returns the string version of the id for this procid. See
93 // _Tt_c_procid::init() for a description of the format for this id.
95 // XXX: Warning! Do not inline the following. Causes weirdness with
98 const _Tt_string & _Tt_procid::
101 // return string representation
107 // Returns 1 if this procid's id is the same as the one given.
110 is_equal(_Tt_string &i)
121 // Returns the TCP port to connect to to signal a procid for a new
122 // message. This method is only used in server mode.
127 if (_socket.is_null()) {
130 return(_socket->port());
136 // XDR encodes/decodes a procid.
143 if (! _id.xdr(xdrs)) {
147 if (xdrs->x_op == XDR_ENCODE) {
150 if (! xdr_long(xdrs, &localpid)) {
153 if (xdrs->x_op == XDR_DECODE) {
154 _pid = (pid_t)localpid;
157 return(_proc_host_ipaddr.xdr(xdrs));
162 print(const _Tt_ostream &os) const
164 os << _id << " [" << _pid;
165 if (! _proc_host.is_null()) {
166 os << "@" << _proc_host->name();
173 // Returns 1 if this procid is handling m or is voting on it.
176 processing(const _Tt_message &m)
182 // if message state is already final then it can't be
183 // handled by anyone.
188 if (!m.handler().is_null()) {
189 return m.handler()->is_equal( this );
190 } else if (m.message_class() == TT_OFFER) {
199 // Wrapper to invoke the _Tt_procid_ptr::xdr method.
202 tt_xdr_procid(XDR *xdrs, _Tt_procid_ptr *patp)
204 return((*patp).xdr(xdrs));
209 // Wrapper to invoke the _Tt_message_list_ptr::xdr method.
212 tt_xdr_message_list(XDR *xdrs, _Tt_message_list_ptr *mptr)
214 return((*mptr).xdr(xdrs));
219 // Add the given path to the list of files this procid object has
223 add_joined_file(_Tt_string fpath)
225 if (joined_to_file(fpath)) {
228 if (_joined_files.is_null()) {
229 _joined_files = new _Tt_string_list();
231 _joined_files->push(fpath);
236 // Delete the given path from the list of files this procid object has
240 del_joined_file(_Tt_string fpath)
242 if (_joined_files.is_null()) {
245 _Tt_string_list_cursor jc(_joined_files);
256 // Return 1 if the given path represents a file this procid object has
260 joined_to_file(_Tt_string fpath)
262 if (_joined_files.is_null()) {
266 _Tt_string_list_cursor jc(_joined_files);
278 // Invokes _Tt_procid::id method on o which is assumed to point to a
279 // _Tt_procid. Used by the table package to generate keys.
282 _tt_procid_id(_Tt_object_ptr &o)
284 return(((_Tt_procid *)o.c_pointer())->id());
290 #ifdef OPT_ADDMSG_DIRECT
292 // If this option is turned on, these functions are used to read/write
293 // a given counted buffer to the given _Tt_stream_socket object.
294 // "iohandle" is really a pointer to this _Tt_stream_socket object (it
295 // is given to xdrrec_create, and then XDR will give it to these
296 // functions to do the read/writes to an xdr stream. See
297 // _Tt_s_procid::signal_new_message(_Tt_message_ptr &m), and
298 // _Tt_c_procid::next_message.)
304 _tt_xdr_readit(char *iohandle, char *buf, int nbytes)
310 _Tt_stream_socket *sptr = (_Tt_stream_socket *)iohandle;
317 if (select(FD_SETSIZE, &readfds, 0, 0, &tmout) <= 0) {
318 if (errno == EBADF) {
319 _tt_syslog( 0, LOG_ERR, "_tt_xdr_readit(): %m" );
323 if (!FD_ISSET(fd, &readfds)) {
324 _tt_syslog( 0, LOG_ERR, "_tt_xdr_readit(): !FD_ISSET()" );
327 rval = sptr->recv(buf, nbytes);
328 return((rval) ? rval : -1);
333 _tt_xdr_writeit(char *iohandle, char *buf, int nbytes)
335 return(((_Tt_stream_socket *)iohandle)->send(buf,nbytes));
337 #endif // OPT_ADDMSG_DIRECT