ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
not that i know of, but would be interesting. i think there exist a function in the kernel layer, dont know how to access and use it, i would be glad if someone can say somethin about it :>
however, i needed it in the past too, and wrote out of this implementation a crappy function by myself(but worked for me) which looked like this
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,788
Rep:
Although it will make your C code more complicated, you do not need to call a shell command from C to gather information about processes, you can instead directly browse the /proc filesystem.
Originally posted by jlliagre
Although it will make your C code more complicated, you do not need to call a shell command from C to gather information about processes, you can instead directly browse the /proc filesystem.
I've tried iterating through /proc/<pid> to resolve the symlinks called "exe" to a full path, and then counting how many are the same string as a given path of an executable.
But if I'm not root, I have a lot of "permission denied revolving symlink"-like errors (through perror()). A regular user however can read /proc/<pid>/cmdline which contains (more or less) the name of the executable also. But this isn't accurate as it does not allways have the full path. Worse yet, programs can change argv[0] (I guess /proc/<pid>/cmdline == argv[0] ). And some programs actually do this. E.g. I've read somewhere sendmail does this.
Is it at all possible, for a regular user to see (accurately) if a given executable file is running?
I don't konw how to do it gracefully.
There must be some APIs, but I don't know. Looking forward to your help.
Here are my code......
Code:
/************************************************************************/
/* Function Name : GetProcessID */
/* Description : get the ID of a specified process */
/* In : pProcessName. name of the process,not case sensitive */
/* : PidNum. number of dwPID[]'s elements */
/* Out : PID. conserve the process ID */
/* : PidNum. hold the number of retrieved PID */
/* Return : true if succeeds; false if fails or process not exist*/
/* Note : Get info from file "/proc/[PID]/stat", */
/* It contains contains pid,process name,etc. */
/************************************************************************/
bool GetProcessID(const char *pProcessName, pid_t PID[], int &PidNum)
{
char fullPath[PATH_MAX] = { 0 };
const char *pdir = "/proc/"; // Directory, ended with '/'
const char *pFile = "/stat"; // File, contains pid,processname,etc.
int pidnumber = 0; // Conserve the pid number that matches
DIR *dp = opendir(pdir);
if (NULL == dp) return false;
struct dirent *dirp = NULL;
// Now traverse the directory
while ((dirp=readdir(dp)) != NULL) { // Here, If has error, I ignore
// Ignore current and parent directory
if (strcmp(dirp->d_name, ".")==0 || strcmp(dirp->d_name, "..")==0) {
continue;
}
// Get full path of the file or directory
strncpy(fullPath, pdir, sizeof(fullPath) - 1);
strncat(fullPath, dirp->d_name, sizeof(fullPath)-strlen(fullPath)-1);
struct stat buf;
int rtn = stat(fullPath, &buf);
if (-1 == rtn) {
continue; // Ignore if cannot get status
}
// There're many files, I care about the PID directory only
if (S_ISDIR(buf.st_mode)) {
if (atoi(dirp->d_name) <= 0) continue; // Not PID directory
// Get the full path of the file that we need
strncat(fullPath, pFile, sizeof(fullPath)-strlen(fullPath)-1);
FILE *pfile = fopen(fullPath, "r");
if (NULL == pfile) continue; // Ignore if cannot open
int procId = 0;
char procName[512] = { 0 };
char *pName = procName;
// Get the info from the file
fscanf(pfile, "%d%s", &procId, procName);
fclose(pfile);
// Get process name, eliminate the brackets '(' and ')'
if (procName[0] == '(' && procName[strlen(procName)-1] == ')') {
pName++;
procName[strlen(procName) - 1] = '\0';
}
if (strcasecmp(pProcessName, pName) == 0 // Same process name
&& procId > 0) // Legal pid
{
if (pidnumber >= PidNum) break;
else PID[pidnumber++] = procId;
}
}
}
closedir(dp);
PidNum = pidnumber;
if (0 == pidnumber) // Not found
return false;
else
return true;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.