LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Get Next PID of Process Tree (https://www.linuxquestions.org/questions/programming-9/get-next-pid-of-process-tree-119832/)

zer0python 11-25-2003 12:49 PM

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

infamous41md 11-25-2003 12:51 PM

u can use ps to print out user formatted output, 'man ps' for more. ps gets that data from the /proc directory i believe.

zer0python 11-25-2003 01:36 PM

ps uses which file in the /proc directory? o.o I was hoping their was some functions already made in C to do this

infamous41md 11-25-2003 03:18 PM

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:

<:oo7:> cd 985
<:oo7:> ls
cmdline cwd environ exe fd maps mem mounts root stat statm status
<:oo7:> 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 :D 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?

infamous41md 11-25-2003 03:37 PM

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);
}


Hko 11-25-2003 05:03 PM

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.

zer0python 11-25-2003 08:02 PM

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)


Hko 11-26-2003 11:56 AM

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;
}



All times are GMT -5. The time now is 05:35 AM.