On Tue, 20 Dec 2022 13:29:23 +0100
aitor <aitor_czr@???> wrote:
> Hi,
>
> In order to simplify the code of simple-netaid i'm trying to get the tty of a running process
> using the struct `proc_t` declared the `procps` project:
>
> https://salsa.debian.org/debian/procps/-/blob/master/library/include/readproc.h
>
> Pay attention to the integer defined in the line nº 155:
>
> int tty; // stat- full device number of controlling terminal
>
> This is indeed the value i'm trying to request, and a|ccording to the
> comment done at the right of the variable ||containing the word *stat*, this is supposedly the flag to be passed to
> openproc():|
>
> |https://manpages.debian.org/testing/procps/openproc.3.en.html
> |
>
> |i.e. the flag PROC_FILLSTAT, as shown in the following C program:|
>
> |
> |
>
> |______ MAIN.C _______
> |
>
> |#include <stdio.h>
> #include <stdlib.h>
> #include <proc/readproc.h>
>
> int main(int argc, char **argv)
> {
> proc_t *proc_info;|
>
> |PROCTAB *proc = openproc(PROC_FILLSTAT);
>
> while(proc_info = readproc(proc, NULL)) {
>
> if(proc_info->tgid == atoi(argv[1])) {
>
> printf("/dev/pts/%d\n", proc_info->tty);
> }
>
> freeproc(proc_info);
> }
>
> closeproc(proc);|
>
> |return 0;
> }
> |
>
> |_______ END ________|
>
> |
> |
>
> |You need to install libprocps-dev. Then, compile the program as follows:|
>
> |$ gcc main.c -o main -lprocps|
>
> |If you open a terminal and try to run the program passing the pid of
> the process as argument in the command line, that is:|
>
> |$ ./main $(echo $$)|
>
> |It should print the tty of the current terminal, in the same way as tty
> does:|
>
> |$ tty
> /dev/pts/4
> |
>
> |But this is what i'm getting instead:|
>
> |$ ./main $(echo $$)
> /dev/pts/34820|
>
$$ is the process ID (PID) of the script itself.
> |As you can see, a very high number. Whereas the printed value should be
> equal to 4. If you try passing the pid of another running process,
> you'll get similar results or zero. Any thoughts on what's wrong in the
> program? |||May this be a bug in procps?| |
>
> |Thanks in advance,|
>
> |Aitor.|
>
> |
> |