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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
|
11-25-2003, 01:49 PM
|
#1
|
Member
Registered: Sep 2003
Posts: 104
Rep:
|
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
|
|
|
11-25-2003, 01:51 PM
|
#2
|
Member
Registered: Mar 2003
Posts: 804
Rep:
|
u can use ps to print out user formatted output, 'man ps' for more. ps gets that data from the /proc directory i believe.
|
|
|
11-25-2003, 02:36 PM
|
#3
|
Member
Registered: Sep 2003
Posts: 104
Original Poster
Rep:
|
ps uses which file in the /proc directory? o.o I was hoping their was some functions already made in C to do this
|
|
|
11-25-2003, 04:18 PM
|
#4
|
Member
Registered: Mar 2003
Posts: 804
Rep:
|
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:
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.
|
|
|
11-25-2003, 04:37 PM
|
#5
|
Member
Registered: Mar 2003
Posts: 804
Rep:
|
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);
}
|
|
|
11-25-2003, 06:03 PM
|
#6
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536
Rep:
|
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.
|
|
|
11-25-2003, 09:02 PM
|
#7
|
Member
Registered: Sep 2003
Posts: 104
Original Poster
Rep:
|
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)
|
|
|
|
11-26-2003, 12:56 PM
|
#8
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536
Rep:
|
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.
|
|
|
All times are GMT -5. The time now is 10:17 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|