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.
Originally posted by SaTaN Hey but this would be giving all the files in /proc . How do I check whether they belong to me or belong to a different user.....
Well, it does not give all the files in /proc. Only the ones that start with a number (see filter() function), in other words, only the process-subdir's inside /proc.
If you only want to have the pid's of which you are the owner (like ps), you only need to extend the processdir() function, like you already tried. But IMHO here's a better method to do this:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <fnmatch.h>
/* Added includes for stat() and getuid(). */
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
/* Added include for some string functions. */
#include <string.h>
void processdir(const struct dirent *dir)
{
/* This function gets called for each dir found. */
/* Here we have the name of the directory (subdir */
/* of /proc that is a number (see filter()). */
/* So here we can access all info about the dir, */
/* the process and user-ID it belongs to, and also */
/* parse info in the files in this dir. This we */
/* we don't need here, since the owner of the dir */
/* is the user-ID of this program itself. So we */
/* just need to get those two ID's and compare them. */
uid_t user, dirowner;
struct stat dirinfo;
/* Calculate size of string length to contain */
/* "/proc/" + length of dir->d_name + terminating */
/* '\0' character. */
/* We need this entire path to call stat() which */
/* gives access to all info about the dir itself. */
int len = strlen(dir->d_name) + 7;
char path[len];
/* Construct entire path from filesys-root of dir. */
strcpy(path, "/proc/");
strcat(path, dir->d_name);
/* Get user ID of this program itself. */
user = getuid();
/* Get owner of the current subdir. */
if (stat(path, &dirinfo) < 0) {
perror("processdir() ==> stat()");
exit(EXIT_FAILURE);
}
dirowner = dirinfo.st_uid;
/* If the 2 UID's match, print pid. The user ID is now */
/* available for further processing as a number (in the */
/* user or dirowner variable) or as a string (the name */
/* of the directory: dir->d_name). */
/* Again, for this example, just printing the string. */
if (dirowner == user) {
puts(dir->d_name);
}
}
int filter(const struct dirent *dir)
{
return !fnmatch("[1-9]*", dir->d_name, 0);
}
int main() {
/* Based on example in "man scandir" */
struct dirent **namelist;
int n;
n = scandir("/proc", &namelist, filter, 0);
if (n < 0)
perror("Not enough memory.");
else {
while(n--) {
processdir(namelist[n]);
free(namelist[n]);
}
free(namelist);
}
return 0;
}
Thanx u r programme is working fine.....
When I started the thread I thought that there would be some function which would return pids ... Looks like there isn't any such function....
Thanx for ur help....
As a second thought about my program, I think it would be nicer to inlcude the pid-comparing code in filter() instead of processdir().
This way the processdir() function is only used for actual processing, in this case print the PID. After all, it is the reason for scandir() to have a pointer to a filtering function (or "selecting" as the man-page calls it).
Like this it's nicer I think:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <fnmatch.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
void processdir(const struct dirent *dir)
{
puts(dir->d_name);
}
int filter(const struct dirent *dir)
{
uid_t user;
struct stat dirinfo;
int len = strlen(dir->d_name) + 7;
char path[len];
strcpy(path, "/proc/");
strcat(path, dir->d_name);
user = getuid();
if (stat(path, &dirinfo) < 0) {
perror("processdir() ==> stat()");
exit(EXIT_FAILURE);
}
return !fnmatch("[1-9]*", dir->d_name, 0) && user == dirinfo.st_uid;
}
int main() {
/* Based on example in "man scandir" */
struct dirent **namelist;
int n;
n = scandir("/proc", &namelist, filter, 0);
if (n < 0)
perror("Not enough memory.");
else {
while(n--) {
processdir(namelist[n]);
free(namelist[n]);
}
free(namelist);
}
return 0;
}
Changed the directory just created by tar, run "etags" for easy finding of functions inside the sources. Then start emacs. Execute "C-x visit-tags-table" from within emacs. Open subdir "src", and open "ps.c".
That's about it.
The package includes more utils for the /proc filesystem than just "ps".
Getting sources of Linux tools and programs is actually quite trivial. Every Linux distribution should offer you the sources of all software it uses, except for some closed source program it may include (e.g. acroread). The sources are all over the internet and some distributions offer "source-packages" (sources packaged in rpm or deb format). Debian also has the possibility to apt-get sources if the /etc/apt/sources.list is configured for this.
Originally posted by SaTaN People say that LINUX is a open software n stuff but I never manage to get any source code or maybe I dunno where to look
You must be kidding...
If you buy RedHat, you get 3 CD's containing all sources (in SRPM format)! You can also download the ISO files for RedHat source-CD's to burn the CD's yourself from:
This link is easily found by going to http://www.redhat.com/ , click on the "download" button on the link-bar at the top of the page, and browse a little around.
Many of the core utilities on a linux system are provided by the GNU project (you must have seen the name "GNU" before somewhere on your RedHat system...). The sources for more than 2000 (including "cat", "bash", "grep", "awk", etc. etc...) of the GNU programs can be found on: http://www.gnu.org/directory/
Just searching google for "grep" for example, then clicking the top link, gets you on the way to get the sources for "grep". Same with the C-compiler gcc...everything!
Also browsing/searching http://freshmeat.net/ will get you to the sources of thousands of Linux programs, from the kernel, to the coreutils included on virtually every Linux distribution to loads of editors, games, compilers, ...just name it!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.