LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 11-25-2003, 01:49 PM   #1
zer0python
Member
 
Registered: Sep 2003
Posts: 104

Rep: Reputation: 20
Get Next PID of Process Tree


I'm not sure, if that is the proper way to phrase, what I am asking, anyway...

I am making a program which allows me to moniter users using my box, (knock 'em off sshd/view what they're doing, etc) but I can't figure out how to kill the proper terminal, I can get the login pid using utmp, but I need the tty/pty/pts id.. Any ideas on how to do this?

Thanks in advanced
 
Old 11-25-2003, 01:51 PM   #2
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
u can use ps to print out user formatted output, 'man ps' for more. ps gets that data from the /proc directory i believe.
 
Old 11-25-2003, 02:36 PM   #3
zer0python
Member
 
Registered: Sep 2003
Posts: 104

Original Poster
Rep: Reputation: 20
ps uses which file in the /proc directory? o.o I was hoping their was some functions already made in C to do this
 
Old 11-25-2003, 04:18 PM   #4
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
well using a utility like strace would give you that answer, but anyways, in the proc directory if do ls you will see this:
Quote:
1 1123 1130 2 3959 514 715 799 944 963 bus execdomains ioports mdstat partitions swaps
1006 1124 1131 3 3960 571 729 8 955 964 cmdline fb irq meminfo pci sys
1007 1125 1132 3913 3974 575 738 800 956 985 cpuinfo filesystems kcore misc scsi sysvipc
1034 1126 1133 3940 3991 6 769 801 957 986 devices fs kmsg modules self tty
1035 1127 1135 3941 4 675 787 902 960 987 dma ide ksyms mounts slabinfo uptime
1062 1128 12 3942 462 68 797 903 961 988 driver interrupts loadavg mtrr speakup version
1063 1129 186 3943 5 7 798 943 962 apm es1371 iomem locks net stat
the numbers are all process id's and are folders. in a process id folder it looks like this:
Quote:
<o7:> cd 985
<o7:> ls
cmdline cwd environ exe fd maps mem mounts root stat statm status
<o7:> cat stat
985 (xmms) S 1 984 984 0 -1 0 589 0 1501 0 5484 925 0 0 15 0 0 0 18942 50507776 2066 4294967295 134512640 135467008 3221224240 3221223788 1108157003 0 0 4102 1024 3222420269 0 0 17 0
however using /proc/, to quote stevens, is an ugly hodgepodge. iirc the way to do this stuff is thru sysctl calls. i've used sysctl for routing and network stuff, but not for process stuff yet. however it would be a fun project and im going to try now i think thanks for the inspiration! for now tho you can just parse the data in /proc/your_pid/stat file.

ps. if you're interested in doing the sysctl way, have a look at file: /usr/include/linux/sysctl.h it shows you the request structure kinda. you'll need to read the man pages and prolly google for a sysctl() tutorial. i may have some old code lying around if you want to check it out?

Last edited by infamous41md; 11-25-2003 at 04:20 PM.
 
Old 11-25-2003, 04:37 PM   #5
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
damn there seems to be not much info at all on how to use sysctl. this seems to be another one of those things to just hack ur way thru. all of the standard linux utilities DONT use sysctl, they all cheat and use /proc. does anyone else have any source for utilities that use sysctl? here is teh little piece of code i wrote awhile ago, it shows the general idea. if you read the above mentioned header file you'll see how it works. the requests form a hierarchical structure.
Code:
<:oo7:> cat tcp_keepalive.c 
#include <stdio.h>
#include <sys/param.h>
#include <sys/sysctl.h>
#include <netinet/in.h>

int
main(int argc, char **argv)
{
        int             mib[10], val;
        size_t  len = sizeof(val);

        mib[0] = CTL_NET;
        mib[1] = NET_IPV4;
        mib[2] = NET_IPV4_TCP_KEEPALIVE_PROBES;
        
        
        if(sysctl(mib, 3, &val, &len, NULL, 0) < 0)
                err_quit("sysctl");
        
        printf("tcp sends %d keepalive probes b4 giving up\n", val);

        exit(0);
}
 
Old 11-25-2003, 06:03 PM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
man sysctl says:
sysctl is used to modify kernel parameters at runtime. The parameters available are those listed under /proc/sys/
If it only does the /proc/sys things, it cannot be used for the pid things. IMHO it's not "ugly" to use /proc directly from a program. It is the way to do it AFAIK.
 
Old 11-25-2003, 09:02 PM   #7
zer0python
Member
 
Registered: Sep 2003
Posts: 104

Original Poster
Rep: Reputation: 20
Well, the problem is..I don't wanna control my shell, I wanna be able to control other people's shells...

Quote:

I am making a program which allows me to moniter users using my box, (knock 'em off sshd/view what they're doing, etc)
 
Old 11-26-2003, 12:56 PM   #8
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Re: Get Next PID of Process Tree

Quote:
Originally posted by zer0python
[...] but I can't figure out how to kill the proper terminal, I can get the login pid using utmp, but I need the tty/pty/pts id.. Any ideas on how to do this?
Getting the terminal name (/dev/pts/2) is as easy as getting the login PID:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <utmp.h>

int main()
{
     struct utmp *u;

     setutent();
     atexit(endutent);
     printf("User       PID        Term\n");
     printf("~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
     while ((u = getutent()) != NULL) {
	  if (u->ut_type == USER_PROCESS) {
	       printf("%-10s %-10d /dev/%s\n", u->ut_user, u->ut_pid, u->ut_line);
	  }
     }
     return 0;
}

Last edited by Hko; 11-26-2003 at 12:58 PM.
 
  


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
Getting PID of running process in C/C++ laikos Programming 16 10-30-2015 07:16 PM
Getting pid of a process!! vishamr2000 Programming 34 03-12-2015 08:12 AM
Start a Process with dedicated PID murder Linux - Newbie 5 08-15-2005 04:49 PM
how to get more process' info according to its PID? iclinux Programming 1 02-05-2005 06:30 AM
ps showing duplicate process same PID dooahhdoo Red Hat 0 07-09-2004 06:52 AM

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

All times are GMT -5. The time now is 10:17 PM.

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