zsh-users
 help / color / mirror / code / Atom feed
From: Jim <linux.tech.guy@gmail.com>
To: Pier Paolo Grassi <pierpaolog@gmail.com>
Cc: zsh <zsh-users@zsh.org>
Subject: Re: read with redirected stdin
Date: Sat, 7 Jan 2023 22:28:56 -0600	[thread overview]
Message-ID: <CA+rB6GJo8m1v-v1dwQXddhFDi9q-=Z0o5BsfX6C3-cWCa47e=A@mail.gmail.com> (raw)
In-Reply-To: <CAP+y1xBGBDhmf3uQmxoTrkakTc_1=n54OtH3MC6eTdtsACJFiA@mail.gmail.com>


[-- Attachment #1.1: Type: text/plain, Size: 1007 bytes --]

On Sat, Jan 7, 2023 at 11:23 AM Pier Paolo Grassi <pierpaolog@gmail.com>
wrote:

> Thanks, but i don't _always_ redirect stdin.
> i wonder how is ps getting the tty (it displays an ? when there is no tty
> for the process)
> I suppose it uses /proc, but couldn't find a reference or the info
> exploring the /proc fs by myself.
> for now I am asking directly ps:
> [[ "$(ps h -o tty $$)" != "?" ]] && ...
>
> Pier Paolo Grassi
>
> Hi,

Don't really have an answer how ps gets its info but the attached script is
something
I have been working on. It uses the /proc file system directly to get info.
It uses zstat
to look for the tty info.  If nothing is returned it substitutes '?".  Some
processes point to
/dev/null. The script is still a work in progress. I was trying to get the
same results as
"ps -eHo uid=,pid=,ppid=,tty=,comm=" via a zsh script.

This doesn't answer your original question, but may give you some insight
into the
/proc file system.

Hope the script is helpful.

Regards,

Jim Murphy

[-- Attachment #1.2: Type: text/html, Size: 1789 bytes --]

[-- Attachment #2: ps_eHo_via_zsh_script --]
[-- Type: application/octet-stream, Size: 1277 bytes --]

emulate -L zsh
setopt extendedglob
# Function:  ps_eHo_via_zsh_script
# WIP, still need to figure out how to make a tree(forest)
  [[ -v modules[zsh/stat] ]] || zmodload zstat
local     E L M N V
local -a  A B X Y Z Ps
local -A  proc_comm proc_tty proc_ppid proc_uid
Ps=(Pid: PPid: Name: Uid:) ; P="${(j.|.)Ps}"
# "ps -eHo uid=,pid=,ppid=,tty=,comm=" -- "ps"  vs  "ps via zsh shell"
# issue:  making a tree(forest) formatted output like the ps command
A=(/proc/<->(/DN)) ; A=(${A##*/})
for E ($A) {
  [[ ! -d /proc/$E ]] && { N=${A[(i)$E]} ; [[ $N -le $#A ]] && A[N]=() } }
for E ($A) B+=(${(f)"$(</proc/$E/comm)"}) ; M=${A[${B[(i)firefox-esr]}]}
for E ($A) {
  B=(${(M)${(f)"$(</proc/$E/status)"}:#($~P)*})
  Z=(${(z)B[${B[(i)Uid:*]}]}) ; [[ $Z[2] -ne 0 && $Z[2] -ne $UID ]] && continue
  Z=(${(z)B[${B[(i)PPid:*]}]}) # skip if ppid is 2
    [[ $Z[2] -eq 2 || $Z[2] == $M ]] && continue || proc_ppid[$E]=${Z[2]}
  Z=(${=B[${B[(i)Name:*]}]}) ; proc_comm[$E]=${Z[2,-1]}
  Z=(${(z)B[${B[(i)Uid:*]}]})  ; proc_uid[$E]=${Z[2]}
  V=$(zstat +link /proc/$E/fd/0 2>/dev/null) \
    proc_tty[$E]=${${${V:=\?}#/dev/}%:*}
}
L=${#${(O@v)proc_tty//?/X}[1]}
for E (${(onk)proc_comm}) {
  printf "%5s %5s %5s %-${L}s %s\n" $E \
    $proc_ppid[$E] $proc_uid[$E] $proc_tty[$E] $proc_comm[$E]
}

  parent reply	other threads:[~2023-01-08  4:29 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-07 13:31 Pier Paolo Grassi
2023-01-07 13:44 ` Roman Perepelitsa
2023-01-07 17:21   ` Pier Paolo Grassi
2023-01-07 17:31     ` Roman Perepelitsa
2023-01-07 17:36       ` Pier Paolo Grassi
2023-01-07 17:52         ` Roman Perepelitsa
2023-01-08 13:23       ` Daniel Shahaf
2023-01-08 13:48         ` Roman Perepelitsa
2023-01-08 14:21           ` Daniel Shahaf
2023-01-08 14:42             ` Roman Perepelitsa
2023-01-07 17:33     ` Pier Paolo Grassi
2023-01-08  4:28     ` Jim [this message]
2023-01-08  4:44       ` Pier Paolo Grassi
2023-01-08 21:18         ` Jim
2023-01-09 16:01           ` Pier Paolo Grassi
2023-01-09 22:47             ` Jim
2023-01-10  2:46               ` Bart Schaefer
2023-01-10  6:03                 ` Pier Paolo Grassi
2023-01-08 14:44       ` Ray Andrews
2023-01-08 15:06         ` Roman Perepelitsa
2023-01-08 20:23           ` Ray Andrews

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CA+rB6GJo8m1v-v1dwQXddhFDi9q-=Z0o5BsfX6C3-cWCa47e=A@mail.gmail.com' \
    --to=linux.tech.guy@gmail.com \
    --cc=linuxtechguy@gmail.com \
    --cc=pierpaolog@gmail.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).