I need to detemine if an application/daemon is executing under Fedora 64-bit Linux.


The application I need to check I do not have source for, and this is not
the condition of trying to write a singleton solution.

There have been a couple of solutions proposed at the following URLs for
this:

http://developer.qt.nokia.com/forums/viewthread/581

Of the first solution, using the sysctl method, under Fedora, there are
many undefines included KERN_PROC, KERN_PROC_ALL, and kinfo_proc. These
appear to be BSD specific, and it is not clear that the implementation
of sysctl under Fedora Linux would work if I were to cobble together the
BSD header defining these values.

Qt Code:
  1. struct kinfo_proc *result, *ptr;
  2. int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 };
  3. size_t length, i;
  4. result = NULL;
  5. length = 0;
  6.  
  7. if (sysctl((int *)name,(sizeof(name)/sizeof(*name)) - 1, NULL, &length, NULL, 0) == -1)
  8. return 0;
  9.  
  10. if ((result = (kinfo_proc *)malloc(length)) == NULL)
  11. return 0;
  12.  
  13. if (sysctl((int *)name, (sizeof(name)/sizeof(*name)) - 1, result, &length, NULL, 0) == -1)
  14. {
  15. free(result);
  16. return 0;
  17. }
  18.  
  19. for (i = 0, ptr = result; (i < (length/sizeof(kinfo_proc)); ++i, ++ptr)
  20. {
  21. // ptr->kp_proc.p_pid contains the pid
  22. // ptr->kp_proc.p_comm contains the name
  23. pid_t iCurrentPid = ptr->kp_proc.p_pid;
  24. if (strncmp(processName, ptr->kp_proc.p_comm, MAXCOMLEN) == 0)
  25. {
  26. free(sProcesses);
  27. return iCurrentPid;
  28. }
  29. }
  30.  
  31. free(result);
To copy to clipboard, switch view to plain text mode 

Moving on to the next suggested option, was to use a QDirIterator to simply
iterate through the "/proc" file system looking for the executable name or
cmd line which matches my application.

The problem I am having with the 2nd approach is that "/proc" is so heavily
nested and full of content that I blow up my stack as a result of the iteration.

I have something like the following code segment:

Qt Code:
  1. QDirIterator it("/proc", QDirIterator::Subdirectories)
  2. while (it.hasNext())
  3. {
  4. it.next();
  5. QFileInfo child = it.fileInfo();
  6.  
  7. if (child.fileName() == ".." || child.fileName() == ".")
  8. continue;
  9.  
  10. ...
  11. }
To copy to clipboard, switch view to plain text mode 

I am out of ideas, anyone think of some other options or solutions.

Is there a way of scoping QDirIterator so that it only goes 1 level deep?

From what I can tell from inspecting "/proc" on my machine, there are
running daemons for which I know the process id using the task manager,
but inspecting the entries in "/proc" does not show anything for either
the "cmdLine" or "exe" entries, which makes me question if this technique
will work.