dkonigsberg

Find process PID on QNX

May 13th, 2015
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.21 KB | None | 0 0
  1. pid_t findProcessPid(const char *process) {
  2.     DIR *dirp;
  3.     struct dirent *dire;
  4.     char buffer[32];
  5.     int fd=0, status;
  6.     pid_t pid;
  7.     struct dinfo_s {
  8.         procfs_debuginfo info;
  9.         char pathbuffer[PATH_MAX];
  10.     } dinfo;
  11.  
  12.     if ((dirp = opendir("/proc")) == NULL) {
  13.         closedir(dirp);
  14.         qWarning() << "Could not open '/proc'";
  15.         return -1;
  16.     }
  17.  
  18.     while (true) {
  19.         if ((dire = readdir(dirp)) == NULL) break;
  20.         if (isdigit(dire->d_name[0])) {
  21.             pid = strtoul(dire->d_name, NULL, 0);
  22.             sprintf(buffer, "/proc/%d/as", pid);
  23.             if (fd > 0) close(fd);
  24.             fd = open(buffer, O_RDONLY);
  25.             if (fd > 0) {
  26.                 status = devctl(fd, DCMD_PROC_MAPDEBUG_BASE, &dinfo, sizeof(dinfo), NULL);
  27.                 if (status == EOK) {
  28.                     if (!strcmp(process, basename(dinfo.info.path))) {
  29.                         if (fd > 0) close( fd );
  30.                         closedir(dirp);
  31.                         return pid;
  32.                     }
  33.                 }
  34.                 if (fd > 0) close(fd);
  35.             }
  36.         }
  37.     }
  38.     if (fd > 0) close(fd);
  39.     closedir(dirp);
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment