Process Names

Hello,

I’m trying to get the process name by calling devctl, but it fails and errno
returns a “Bad file descriptor”. I have no idea how to tell (other then the
return being -1) that the file descriptor returned from an open is valid.
Can anyone see a problem with the following code. I essentially took this
code from an example in a previous post on the subject.

Thanks,
Eric

int proc_fd, status;

// Structure used to get the process info
struct dinfo_s
{
procfs_debuginfo info;
char pathbuffer[_POSIX_PATH_MAX];
};

struct dinfo_s dinfo;
char buf[_POSIX_PATH_MAX + 1];

// Open up the proc namespace to find our process info
sprintf( buf, “%s/%d/as”, PROC_BASE, getpid() );
printf(“buf: %s\n”,buf);
if( (proc_fd = open(buf, O_RDONLY)) == -1 )
{
fprintf( stderr, “%s: Error opening path to %s.\n”, FunctionName, buf );
close(proc_fd);
return(-1);
}

memset(&dinfo, 0, sizeof(dinfo));
// Get the info from procmgr
status = devctl( proc_fd, DCMD_PROC_MAPDEBUG_BASE, &dinfo, sizeof(dinfo),
0 );
if( status != EOK )
{
fprintf( stderr, “%s: Error in devctl() call. %s\n”,
FunctionName,strerror(status) );
close(proc_fd);
return(-1);
}
close(proc_fd);

devctl doesn’t set errno - the return value is the error. It SHOULD
return 0, otherwise you can interpret the value the same as errno.

Your code below works for me…


Eric Norton wrote:

Hello,

I’m trying to get the process name by calling devctl, but it fails and errno
returns a “Bad file descriptor”. I have no idea how to tell (other then the
return being -1) that the file descriptor returned from an open is valid.
Can anyone see a problem with the following code. I essentially took this
code from an example in a previous post on the subject.

Thanks,
Eric

int proc_fd, status;

// Structure used to get the process info
struct dinfo_s
{
procfs_debuginfo info;
char pathbuffer[_POSIX_PATH_MAX];
};

struct dinfo_s dinfo;
char buf[_POSIX_PATH_MAX + 1];

// Open up the proc namespace to find our process info
sprintf( buf, “%s/%d/as”, PROC_BASE, getpid() );
printf(“buf: %s\n”,buf);
if( (proc_fd = open(buf, O_RDONLY)) == -1 )
{
fprintf( stderr, “%s: Error opening path to %s.\n”, FunctionName, buf );
close(proc_fd);
return(-1);
}

memset(&dinfo, 0, sizeof(dinfo));
// Get the info from procmgr
status = devctl( proc_fd, DCMD_PROC_MAPDEBUG_BASE, &dinfo, sizeof(dinfo),
0 );
if( status != EOK )
{
fprintf( stderr, “%s: Error in devctl() call. %s\n”,
FunctionName,strerror(status) );
close(proc_fd);
return(-1);
}
close(proc_fd);


cburgess@qnx.com

Eric Norton wrote:

Hello,

I’m trying to get the process name by calling devctl, but it fails and errno
returns a “Bad file descriptor”. I have no idea how to tell (other then the
return being -1) that the file descriptor returned from an open is valid.
Can anyone see a problem with the following code. I essentially took this
code from an example in a previous post on the subject.

Rearranging your code a bit (includes, etc) the following works for me.
Alternatively, you could use argv[0] which might be easier (also added
to the example).


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/mman.h>
#include <sys/neutrino.h>
#include <sys/procfs.h>

#define PROC_BASE “/proc”

int proc_fd, status;

// Structure used to get the process info
struct dinfo_s {
procfs_debuginfo info;
char pathbuffer[_POSIX_PATH_MAX];
};

struct dinfo_s dinfo;
char buf[_POSIX_PATH_MAX + 1];

int main(int argc, char* argv[]) {
// Open up the proc namespace to find our process info
sprintf( buf, “%s/%d/as”, PROC_BASE, getpid() );
printf(“buf: %s\n”,buf);
if( (proc_fd = open(buf, O_RDONLY)) == -1 ) {
fprintf( stderr, “%s: Error opening path to %s.\n”,
FUNCTION, buf );
close(proc_fd);
return(-1);
}

memset(&dinfo, 0, sizeof(dinfo));
// Get the info from procmgr
status = devctl( proc_fd, DCMD_PROC_MAPDEBUG_BASE, &dinfo,
sizeof(dinfo), 0);
if( status != EOK ) {
fprintf( stderr, “%s: Error in devctl() call. %s\n”,
FUNCTION,strerror(status) );
close(proc_fd);
return(-1);
}

printf(“name: %s\n”, dinfo.info.path);
printf(“argv[] %s\n”, argv[0]);
close(proc_fd);
}


\

Cheers,
Adam

QNX Software Systems Ltd.
[ amallory@qnx.com ]

With a PC, I always felt limited by the software available.
On Unix, I am limited only by my knowledge.
–Peter J. Schoenster <pschon@baste.magibox.net>

Any thoughts as to why it would work in your case and not mine?

“Colin Burgess” <cburgess@qnx.com> wrote in message
news:ce8a74$hqs$2@inn.qnx.com

devctl doesn’t set errno - the return value is the error. It SHOULD
return 0, otherwise you can interpret the value the same as errno.

Your code below works for me…


Eric Norton wrote:
Hello,

I’m trying to get the process name by calling devctl, but it fails and
errno
returns a “Bad file descriptor”. I have no idea how to tell (other then
the
return being -1) that the file descriptor returned from an open is
valid.
Can anyone see a problem with the following code. I essentially took
this
code from an example in a previous post on the subject.

Thanks,
Eric

int proc_fd, status;

// Structure used to get the process info
struct dinfo_s
{
procfs_debuginfo info;
char pathbuffer[_POSIX_PATH_MAX];
};

struct dinfo_s dinfo;
char buf[_POSIX_PATH_MAX + 1];

// Open up the proc namespace to find our process info
sprintf( buf, “%s/%d/as”, PROC_BASE, getpid() );
printf(“buf: %s\n”,buf);
if( (proc_fd = open(buf, O_RDONLY)) == -1 )
{
fprintf( stderr, “%s: Error opening path to %s.\n”, FunctionName,
buf );
close(proc_fd);
return(-1);
}

memset(&dinfo, 0, sizeof(dinfo));
// Get the info from procmgr
status = devctl( proc_fd, DCMD_PROC_MAPDEBUG_BASE, &dinfo,
sizeof(dinfo),
0 );
if( status != EOK )
{
fprintf( stderr, “%s: Error in devctl() call. %s\n”,
FunctionName,strerror(status) );
close(proc_fd);
return(-1);
}
close(proc_fd);


\

cburgess@qnx.com

I found the problem it was a compiler option I was using; -fpack-struct.
Its a option in our global CFLAGS variable for make. Do you know why that
causes problems? And should it?

Eric

“Adam Mallory” <amallory@qnx.com> wrote in message
news:ce8aq6$jck$1@inn.qnx.com

Eric Norton wrote:
Hello,

I’m trying to get the process name by calling devctl, but it fails and
errno
returns a “Bad file descriptor”. I have no idea how to tell (other then
the
return being -1) that the file descriptor returned from an open is
valid.
Can anyone see a problem with the following code. I essentially took
this
code from an example in a previous post on the subject.

Rearranging your code a bit (includes, etc) the following works for me.
Alternatively, you could use argv[0] which might be easier (also added
to the example).


#include <stdio.h
#include <stdlib.h
#include <unistd.h
#include <fcntl.h
#include <errno.h
#include <sys/mman.h
#include <sys/neutrino.h
#include <sys/procfs.h

#define PROC_BASE “/proc”

int proc_fd, status;

// Structure used to get the process info
struct dinfo_s {
procfs_debuginfo info;
char pathbuffer[_POSIX_PATH_MAX];
};

struct dinfo_s dinfo;
char buf[_POSIX_PATH_MAX + 1];

int main(int argc, char* argv[]) {
// Open up the proc namespace to find our process info
sprintf( buf, “%s/%d/as”, PROC_BASE, getpid() );
printf(“buf: %s\n”,buf);
if( (proc_fd = open(buf, O_RDONLY)) == -1 ) {
fprintf( stderr, “%s: Error opening path to %s.\n”,
FUNCTION, buf );
close(proc_fd);
return(-1);
}

memset(&dinfo, 0, sizeof(dinfo));
// Get the info from procmgr
status = devctl( proc_fd, DCMD_PROC_MAPDEBUG_BASE, &dinfo,
sizeof(dinfo), 0);
if( status != EOK ) {
fprintf( stderr, “%s: Error in devctl() call. %s\n”,
FUNCTION,strerror(status) );
close(proc_fd);
return(-1);
}

printf(“name: %s\n”, dinfo.info.path);
printf(“argv[] %s\n”, argv[0]);
close(proc_fd);
}


\

Cheers,
Adam

QNX Software Systems Ltd.
[ > amallory@qnx.com > ]

With a PC, I always felt limited by the software available.
On Unix, I am limited only by my knowledge.
–Peter J. Schoenster <> pschon@baste.magibox.net

Eric Norton wrote:

I found the problem it was a compiler option I was using; -fpack-struct.
Its a option in our global CFLAGS variable for make. Do you know why that
causes problems? And should it?

The structure you’re using and the structure filled in via Proc aren’t
in agreement any longer (ie. size, alignment etc) - I would expect
failure, or even worse success, but with bad values.

You shouldn’t pack your structs willynilly. Ignoring the problem of
structure size/member location mismatch, you’re taking a performance hit
for unaligned accesses on x86.


Cheers,
Adam

QNX Software Systems Ltd.
[ amallory@qnx.com ]

With a PC, I always felt limited by the software available.
On Unix, I am limited only by my knowledge.
–Peter J. Schoenster <pschon@baste.magibox.net>

Absolutely. Take a look at the devctl line after pre-processin.

qcc -P test.c

vi test.i

Although I would think that you should get enosys or enotsup, but it
could be that proc is getting confused with another devctl cmd.

Eric Norton wrote:

I found the problem it was a compiler option I was using; -fpack-struct.
Its a option in our global CFLAGS variable for make. Do you know why that
causes problems? And should it?

Eric

“Adam Mallory” <> amallory@qnx.com> > wrote in message
news:ce8aq6$jck$> 1@inn.qnx.com> …

Eric Norton wrote:

Hello,

I’m trying to get the process name by calling devctl, but it fails and

errno

returns a “Bad file descriptor”. I have no idea how to tell (other then

the

return being -1) that the file descriptor returned from an open is

valid.

Can anyone see a problem with the following code. I essentially took

this

code from an example in a previous post on the subject.

Rearranging your code a bit (includes, etc) the following works for me.
Alternatively, you could use argv[0] which might be easier (also added
to the example).


#include <stdio.h
#include <stdlib.h
#include <unistd.h
#include <fcntl.h
#include <errno.h
#include <sys/mman.h
#include <sys/neutrino.h
#include <sys/procfs.h

#define PROC_BASE “/proc”

int proc_fd, status;

// Structure used to get the process info
struct dinfo_s {
procfs_debuginfo info;
char pathbuffer[_POSIX_PATH_MAX];
};

struct dinfo_s dinfo;
char buf[_POSIX_PATH_MAX + 1];

int main(int argc, char* argv[]) {
// Open up the proc namespace to find our process info
sprintf( buf, “%s/%d/as”, PROC_BASE, getpid() );
printf(“buf: %s\n”,buf);
if( (proc_fd = open(buf, O_RDONLY)) == -1 ) {
fprintf( stderr, “%s: Error opening path to %s.\n”,
FUNCTION, buf );
close(proc_fd);
return(-1);
}

memset(&dinfo, 0, sizeof(dinfo));
// Get the info from procmgr
status = devctl( proc_fd, DCMD_PROC_MAPDEBUG_BASE, &dinfo,
sizeof(dinfo), 0);
if( status != EOK ) {
fprintf( stderr, “%s: Error in devctl() call. %s\n”,
FUNCTION,strerror(status) );
close(proc_fd);
return(-1);
}

printf(“name: %s\n”, dinfo.info.path);
printf(“argv[] %s\n”, argv[0]);
close(proc_fd);
}


\

Cheers,
Adam

QNX Software Systems Ltd.
[ > amallory@qnx.com > ]

With a PC, I always felt limited by the software available.
On Unix, I am limited only by my knowledge.
–Peter J. Schoenster <> pschon@baste.magibox.net
\


cburgess@qnx.com

Thanks Guys. The compiler option is temporary as we port code from qnx4 to
qnx6. The reason we’re doing it is so that some of the ported code can
still communicate with qnx4 code using the same data type definition. It
makes sense with the misalignment, I was just confused by the cause and
effect, i.e. the problem being the misalignment of the struct and devctl
returning EBADF.

Eric



“Adam Mallory” <amallory@qnx.com> wrote in message
news:ce8cvk$l72$1@inn.qnx.com

Eric Norton wrote:
I found the problem it was a compiler option I was using; -fpack-struct.
Its a option in our global CFLAGS variable for make. Do you know why
that
causes problems? And should it?

The structure you’re using and the structure filled in via Proc aren’t
in agreement any longer (ie. size, alignment etc) - I would expect
failure, or even worse success, but with bad values.

You shouldn’t pack your structs willynilly. Ignoring the problem of
structure size/member location mismatch, you’re taking a performance hit
for unaligned accesses on x86.


Cheers,
Adam

QNX Software Systems Ltd.
[ > amallory@qnx.com > ]

With a PC, I always felt limited by the software available.
On Unix, I am limited only by my knowledge.
–Peter J. Schoenster <> pschon@baste.magibox.net

Eric Norton <enorton_spam@fct.ca> wrote:

Thanks Guys. The compiler option is temporary as we port code from qnx4 to
qnx6. The reason we’re doing it is so that some of the ported code can
still communicate with qnx4 code using the same data type definition.

But the structures for QNX4 <-> QNX6 communication in a seperate header
file (or set of header files) and only change the structure packing for
that header file (or files).

-David


Please follow-up to newsgroup, rather than personal email.
David Gibbs
QNX Training Services
dagibbs@qnx.com