opening a message handler in the same process

I’m trying to do something that seems very straightforward.

I’ve made a photon application with Phab that starts up a message handler
thread in it’s init.c file.

Then, from that photon application I create a button callback function to
open the message handler from it’s filespace name. When I run this callback
function I get a file descriptor of -1 and the open command returns an error
of ‘No such file or directory’.

Just to assure you that the message handler side is working correctly I
wrote the following simple program that I can run from the command prompt
while the photon application (and it’s message handler) is running. And
everything works fine, the open command returns a valid file descriptor.

The button callback function is almost identical to the function below (the
open statement IS identical).

I’ve created non-photon applications that similarly have one thread as a
message handler and another thread in the same process passing messages to
it. But I’ve never done it in photon.

Does anyone know what could be going on?

Thanks in advance.

-Dan


#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>


int main(){
int telMsgFd;

telMsgFd = open("/dev/telemetry", O_RDWR);

printf (“telMsgFd = %d\n”, telMsgFd);

close (telMsgFd);

return 0;
}

Generally you cannot open your own resmgr interfaces. This is to avoid
deadlock and also allow you to gain access to any other paths/prefixes that
where registered before your program was run. You can change this by
passing in _RESMGR_FLAG_SELF to resmgr_attach().

chris


Dan Helmick <danielhelmick@earthlink.net> wrote:

I’m trying to do something that seems very straightforward.

I’ve made a photon application with Phab that starts up a message handler
thread in it’s init.c file.

Then, from that photon application I create a button callback function to
open the message handler from it’s filespace name. When I run this callback
function I get a file descriptor of -1 and the open command returns an error
of ‘No such file or directory’.

Just to assure you that the message handler side is working correctly I
wrote the following simple program that I can run from the command prompt
while the photon application (and it’s message handler) is running. And
everything works fine, the open command returns a valid file descriptor.

The button callback function is almost identical to the function below (the
open statement IS identical).

I’ve created non-photon applications that similarly have one thread as a
message handler and another thread in the same process passing messages to
it. But I’ve never done it in photon.

Does anyone know what could be going on?

Thanks in advance.

-Dan


#include <stdio.h
#include <fcntl.h
#include <sys/stat.h
#include <sys/types.h


int main(){
int telMsgFd;

telMsgFd = open("/dev/telemetry", O_RDWR);

printf (“telMsgFd = %d\n”, telMsgFd);

close (telMsgFd);

return 0;
}
\

cdm@qnx.com > “The faster I go, the behinder I get.”

Chris McKillop – Lewis Carroll –
Software Engineer, QSSL
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

I tried this and still get the same error ‘No such file or directory’…
but I see the name when I do an ls in /dev (although it does have a #
after the name, do you know what this means?).

Isn’t the _RESMGR_FLAG_SELF flag for sending messages to the
handler from within the handler itself?

I understand the possibility for deadlock by sending a message from
within the message handler but I don’t see how the threat of deadlock is
any greater when sending a message from another thread in the same
process (compared to sending it from a completely different process).

Any other ideas? This is so simple… it must be a fundamental problem
in how I’m setting up the resource manager, but I can’t figure it out.

Thanks,
Dan

dan helmick <danielhelmick@earthlink.net> wrote:

Any other ideas? This is so simple… it must be a fundamental problem
in how I’m setting up the resource manager, but I can’t figure it out.

post the code you are using for setting up the resmgr and then trying
to open it withing the same process. :wink:

chris

\

cdm@qnx.com > “The faster I go, the behinder I get.”

Chris McKillop – Lewis Carroll –
Software Engineer, QSSL
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

dan helmick <danielhelmick@earthlink.net> wrote:

I tried this and still get the same error ‘No such file or directory’…
but I see the name when I do an ls in /dev (although it does have a #
after the name, do you know what this means?).

Isn’t the _RESMGR_FLAG_SELF flag for sending messages to the
handler from within the handler itself?

I guess you mean you’ve set it but still can’t open yourself?
We need the code to know if you’ve done sth wrong.

I understand the possibility for deadlock by sending a message from
within the message handler but I don’t see how the threat of deadlock is
any greater when sending a message from another thread in the same
process (compared to sending it from a completely different process).

Most likely, the “sending thread” could hold a mutex where the
“handler” may wish to lock. And things could be more complicate if
more threads involved (A hold Mutex A, goes to b, and b goes to c,
c send, but “handler” want to lock A).

-xtang

Here is the resmgr setup code and the function that tries to open the
resmgr.

int telMsgServer::loop(char * path)
{
resmgr_attr_t resmgr_attr;
message_attr_t message_attr;
dispatch_t *dpp;
int resmgr_id = 0;
int message_id = 0;
dispatch_context_t *ctp, *ctp_ret;

static resmgr_connect_funcs_t ConnectFuncs;
static resmgr_io_funcs_t IoFuncs;
iofunc_attr_t IoFuncAttr;



/* Create the Dispatch Interface */
dpp = dispatch_create();
if( dpp == NULL )
{
fprintf( stderr, “dispatch_create() failed: %s\n”,
strerror( errno ) );
return EXIT_FAILURE;
}

memset( &resmgr_attr, 0, sizeof( resmgr_attr ) );
resmgr_attr.nparts_max = 1;
resmgr_attr.msg_max_size = 255;
resmgr_attr.flags = _RESMGR_FLAG_SELF;

/* Setup the default I/O functions to handle open/read/write/… */
iofunc_func_init( _RESMGR_CONNECT_NFUNCS, &ConnectFuncs,
_RESMGR_IO_NFUNCS, &IoFuncs );

/* Setup the attribute for the entry in the filesystem */
iofunc_attr_init( &IoFuncAttr, S_IFNAM | 0666, 0, 0 );

resmgr_id = resmgr_attach( dpp, &resmgr_attr, path, _FTYPE_ANY,
0, &ConnectFuncs, &IoFuncs, &IoFuncAttr );
if( resmgr_id == -1 )
{
char errMsg[255];
sprintf(errMsg, “resmgr_attach() failed: %s\n”, strerror( errno ) );
PtEnter(Pt_EVENT_PROCESS_PREVENT);
PtMultiTextModifyText(ABW_msgTxt, 1, 1, -1, errMsg,
sizeof(errMsg), NULL, 0);
PtLeave(Pt_EVENT_PROCESS_PREVENT);
return EXIT_FAILURE;
}

/* Setup our message handler */
memset( &message_attr, 0, sizeof( message_attr ) );
message_attr.nparts_max = 1;
message_attr.msg_max_size = 255;

/* Attach a handler for all command messages /
message_id = message_attach( dpp, &message_attr, _IO_MAX + 1,
_IO_MAX + 20, &msgHandlerWrapper, (void
)this );
if( message_id == -1 )
{
fprintf( stderr, “message_attach() failed: %s\n”, strerror( errno ) );
return EXIT_FAILURE;
}

/* Setup a context for the dispatch layer to use */
ctp = dispatch_context_alloc( dpp );
if( ctp == NULL )
{
fprintf( stderr, “dispatch_context_alloc() failed: %s\n”,
strerror( errno ) );
return EXIT_FAILURE;
}

/* The “Data Pump” - get and process messages */

while( 1 )
{
ctp_ret = dispatch_block( ctp );
if( ctp_ret )
{
dispatch_handler( ctp );
}
else
{
fprintf( stderr, “dispatch_block() failed: %s\n”,
strerror( errno ) );
return EXIT_FAILURE;
}
}

return EXIT_SUCCESS;
}


int
openResMgr( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo )
{
char post[255];
int telMsgFd;
char telPath[20];

sprintf(telPath, “/dev/telemetry”);
telMsgFd = open(telPath, O_RDWR);

sprintf(post,“path = %s\ntelMsgFd = %d\nerr = %s\n”, telPath, telMsgFd,
strerror(errno));

PtMultiTextModifyText(ABW_msgTxt, 1, 1, -1, post,
strlen(post), NULL, 0);

/* eliminate ‘unreferenced’ warnings */
widget = widget, apinfo = apinfo, cbinfo = cbinfo;

return (Pt_CONTINUE);

}

dan helmick <danielhelmick@earthlink.net> wrote:

Here is the resmgr setup code and the function that tries to open the
resmgr.

[SNIP]



resmgr_attr.flags = _RESMGR_FLAG_SELF;

Not here…^^^^

resmgr_id = resmgr_attach( dpp, &resmgr_attr, path, _FTYPE_ANY,
0, &ConnectFuncs, &IoFuncs, &IoFuncAttr );

but here…^^^^

ie:

resmgr_id = resmgr_attach( dpp, &resmgr_attr, path, _FTYPE_ANY,
_RESMGR_FLAG_SELF, &ConnectFuncs,
&IoFuncs, &IoFuncAttr );


chris

\

cdm@qnx.com > “The faster I go, the behinder I get.”

Chris McKillop – Lewis Carroll –
Software Engineer, QSSL
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Thanks guys (Xtang came up with the same answer in
qdn.public.qnxrtp.photon).

That fixed my problem.

Just a side note for anyone who cares… I had to do a PtLeave and then a
PtEnter, around my MsgSend call.

-Dan