pid_t findProcessPid(const char *process) {
DIR *dirp;
struct dirent *dire;
char buffer[32];
int fd=0, status;
pid_t pid;
struct dinfo_s {
procfs_debuginfo info;
char pathbuffer[PATH_MAX];
} dinfo;
if ((dirp = opendir("/proc")) == NULL) {
closedir(dirp);
qWarning() << "Could not open '/proc'";
return -1;
}
while (true) {
if ((dire = readdir(dirp)) == NULL) break;
if (isdigit(dire->d_name[0])) {
pid = strtoul(dire->d_name, NULL, 0);
sprintf(buffer, "/proc/%d/as", pid);
if (fd > 0) close(fd);
fd = open(buffer, O_RDONLY);
if (fd > 0) {
status = devctl(fd, DCMD_PROC_MAPDEBUG_BASE, &dinfo, sizeof(dinfo), NULL);
if (status == EOK) {
if (!strcmp(process, basename(dinfo.info.path))) {
if (fd > 0) close( fd );
closedir(dirp);
return pid;
}
}
if (fd > 0) close(fd);
}
}
}
if (fd > 0) close(fd);
closedir(dirp);
return 0;
}
Comments