LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-30-2003, 01:34 AM   #16
LinuxTiro
Member
 
Registered: Aug 2003
Posts: 59

Rep: Reputation: 15

well guys dont get personal out here ,
just stick to the topic under discussion.

Last edited by LinuxTiro; 08-30-2003 at 01:37 AM.
 
Old 08-30-2003, 08:55 AM   #17
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
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;
}
 
Old 08-30-2003, 09:01 AM   #18
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Original Poster
Rep: Reputation: 33
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....
 
Old 08-30-2003, 09:04 AM   #19
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
No, I guess there isn't such a function. I browsed the sources of 'ps' and it apparently also just reads /proc.
 
Old 08-30-2003, 09:15 AM   #20
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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;
}

Last edited by Hko; 08-30-2003 at 09:24 AM.
 
Old 08-30-2003, 10:10 AM   #21
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Original Poster
Rep: Reputation: 33
Grt work Hko thanx for the help....I too would luv to browse the
sources of "ps" . How do I do that ????
 
Old 08-31-2003, 07:19 AM   #22
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Well, download the sources, un-tar them, and read them...

I use Debian, so I did:

dpkg -S `which ps`

This shows "ps" is in the "procps" Debian package.
Then I searched for "procps" on http://packages.debian.org/ .
Then clicked on, to find this link:
http://ftp.debian.org/debian/pool/ma....7.orig.tar.gz

Downloaded them.
tar xzf procps_2.0.7.orig.tar.gz

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.

Last edited by Hko; 08-31-2003 at 07:26 AM.
 
Old 08-31-2003, 07:44 AM   #23
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Original Poster
Rep: Reputation: 33
I am working on RedHat 9.0 version . I tried to search in

http://sources.redhat.com/

but couldn't get the source codes ..

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

Anyways thanx
 
Old 08-31-2003, 08:34 AM   #24
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
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:

http://ftp.redhat.com/pub/redhat/linux/9/en/iso/i386/

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/

All the GNU program's including sources are mirrored al over the world:
See: http://www.gnu.org/prep/ftp.html

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!

Many other programs/utilities can be searched for easily on http://sf.net/ , including "ps" on: http://procps.sf.net/

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!

You didn't really try....did you?

Last edited by Hko; 08-31-2003 at 06:19 PM.
 
Old 09-01-2003, 10:57 AM   #25
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Original Poster
Rep: Reputation: 33
Thanx for ur help I downloaded the sources of "ps" and I wasn't able to make either the head or tail of it ....

I have started UNIX SHELL PROGRAMMING just some time back n
so maybe it'll take some more time for me .

Anyways thanx ....
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Any way to view the current processes? alyks SUSE / openSUSE 7 10-18-2004 10:04 AM
monitoring active processes and identifying the required processes. gajaykrishnan Programming 2 08-13-2004 01:58 AM
Too many processes? mooreted Linux - Software 2 04-05-2004 03:27 PM
current Slack-Current giving troubles? r_jensen11 Slackware 5 02-02-2004 05:08 PM
Staying Current (with current) hjles Slackware 1 01-21-2003 07:03 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration