bob2oneil
8th September 2011, 21:58
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.
struct kinfo_proc *result, *ptr;
int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 };
size_t length, i;
result = NULL;
length = 0;
if (sysctl((int *)name,(sizeof(name)/sizeof(*name)) - 1, NULL, &length, NULL, 0) == -1)
return 0;
if ((result = (kinfo_proc *)malloc(length)) == NULL)
return 0;
if (sysctl((int *)name, (sizeof(name)/sizeof(*name)) - 1, result, &length, NULL, 0) == -1)
{
free(result);
return 0;
}
for (i = 0, ptr = result; (i < (length/sizeof(kinfo_proc)); ++i, ++ptr)
{
// ptr->kp_proc.p_pid contains the pid
// ptr->kp_proc.p_comm contains the name
pid_t iCurrentPid = ptr->kp_proc.p_pid;
if (strncmp(processName, ptr->kp_proc.p_comm, MAXCOMLEN) == 0)
{
free(sProcesses);
return iCurrentPid;
}
}
free(result);
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:
QDirIterator it("/proc", QDirIterator::Subdirectories)
while (it.hasNext())
{
it.next();
QFileInfo child = it.fileInfo();
if (child.fileName() == ".." || child.fileName() == ".")
continue;
...
}
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.
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.
struct kinfo_proc *result, *ptr;
int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 };
size_t length, i;
result = NULL;
length = 0;
if (sysctl((int *)name,(sizeof(name)/sizeof(*name)) - 1, NULL, &length, NULL, 0) == -1)
return 0;
if ((result = (kinfo_proc *)malloc(length)) == NULL)
return 0;
if (sysctl((int *)name, (sizeof(name)/sizeof(*name)) - 1, result, &length, NULL, 0) == -1)
{
free(result);
return 0;
}
for (i = 0, ptr = result; (i < (length/sizeof(kinfo_proc)); ++i, ++ptr)
{
// ptr->kp_proc.p_pid contains the pid
// ptr->kp_proc.p_comm contains the name
pid_t iCurrentPid = ptr->kp_proc.p_pid;
if (strncmp(processName, ptr->kp_proc.p_comm, MAXCOMLEN) == 0)
{
free(sProcesses);
return iCurrentPid;
}
}
free(result);
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:
QDirIterator it("/proc", QDirIterator::Subdirectories)
while (it.hasNext())
{
it.next();
QFileInfo child = it.fileInfo();
if (child.fileName() == ".." || child.fileName() == ".")
continue;
...
}
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.