Linux: unable to retrieve process name from pid via ioctl

370 views Asked by At

I'm trying to retrieve retrieve process name from pid via ioctl, this is the C code:

#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/signal.h>
#include <sys/syscall.h>
#include <sys/procfs.h>
#include <stdio.h>
#include <signal.h>
#include <elf.h>
#include <stdlib.h>
#include <errno.h>

int main(int argc, char **argv) {
char ProcID[50]="/proc/";
int          fd;
prpsinfo_t   ProcessInfo;
int          ioctlResult;

if ( argc != 2 ) {
    printf("usage: %s <pid>\n", argv[0]);
    return 0;
}

strcat(ProcID,argv[1]);
fd = open(ProcID, O_RDONLY, 0);

if (fd == -1) {
    printf("open error: [%d] [%s]\n",errno, strerror(errno));
    return 0;
}

ioctlResult = ioctl(fd, NT_PRPSINFO, &ProcessInfo);

if (ioctlResult == -1)
{
    printf("Error ioctl: [%d] [%s]\n",errno, strerror(errno));
} else {
    printf("Process name: %s\n",ProcessInfo.pr_fname);
}

close(fd);
return 0;
}

When I try to execute it I obtain errno 25 (Inappropriate ioctl for device). I think the file descriptor open on "/proc/" isn't correct; is there another path to consider ?

0

There are 0 answers