2

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 ?

Patrice
  • 4,641
  • 9
  • 33
  • 43
  • I'm not entirely sure, but I don't think `NT_PRPSINFO` is an ioctl - it's an ELF core dump note type. And even if it is an ioctl, I'm not sure applying it to the directory `/proc/` is the right way to use it... – twalberg Dec 04 '13 at 17:55

0 Answers0