public inbox for cygwin-patches@cygwin.com
 help / color / mirror / Atom feed
From: Andy Koppe <andy.koppe@gmail.com>
To: cygwin-patches@cygwin.com
Subject: [PATCH] Cygwin: Correct /proc/*/stat for processes without ctty
Date: Wed, 9 Nov 2022 21:36:30 +0000	[thread overview]
Message-ID: <CAHWeT-a3FhOO2Fc6bEm6ZuW4qcHkY2wz47OMuXKzfSgROzUOMg@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 1207 bytes --]

Hi,

I had noticed that selecting or excluding processes without a
controlling terminal doesn't work in procps on Cygwin.

For example, the mintty process shouldn't appear in the following, as
the 'f' (for forest) argument triggers procps's "BSD personality",
where processes without a controlling terminal are supposed to be
excluded by default:

$ procps f
    PID TTY    STAT  STIME COMMAND
   1809 ?      Ss    19:49 /usr/bin/mintty -
   1810 pty0   Ss    19:49  \_ -zsh
   2075 pty0   R     21:14      \_ procps f

Similarly, this should list the processes without a terminal, but
comes up empty:

$ procps -t -

I tracked this down to a difference in the tty field of /proc/*/stat
(which is the 7th field). On Linux, processes without a terminal have
value 0 there, and that's what procps expects.

Cygwin 3.3 has -1 instead, whereas on master the bits of the tty field
were rearranged in commit 437d0a8f88, which turns the -1 into
268435455 (i.e. 0xFFFFFFF). Either way, procps treats such processes
as having terminals. (The ? in the TTY column output is generated by a
different code path in procps that uses /proc/*/ctty on Cygwin.)

Patches for the 3.3 branch and master attached.

Regards,
Andy

[-- Attachment #2: 0001-Cygwin-proc-stat-ctty.patch --]
[-- Type: application/octet-stream, Size: 1342 bytes --]

From 09e134bdd4594b1727cdbe28110915fd8424c3a8 Mon Sep 17 00:00:00 2001
From: Andy Koppe <andy.koppe@gmail.com>
Date: Wed, 9 Nov 2022 20:34:30 +0000
Subject: [PATCH] Cygwin: Correct /proc/*/stat for processes without ctty

Report 0 instead of 268435455 (i.e. 0xFFFFFFF) in the tty field of
/proc/*/stat for processes without a controlling terminal. This is what
the procps utility expects when selecting or excluding such processes.
---
 winsup/cygwin/fhandler/process.cc | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/winsup/cygwin/fhandler/process.cc b/winsup/cygwin/fhandler/process.cc
index a8c17f1b0..b0aef2ebe 100644
--- a/winsup/cygwin/fhandler/process.cc
+++ b/winsup/cygwin/fhandler/process.cc
@@ -1092,9 +1092,11 @@ format_process_stat (void *data, char *&destbuf)
   int nice = 0;
 /* ctty maj is 31:16, min is 15:0; tty_nr s/b maj 15:8, min 31:20, 7:0;
    maj is 31:16 >> 16 & fff << 8; min is 15:0 >> 8 & ff << 20 | & ff */
-  int tty_nr =    (((p->ctty >>  8) & 0xff)  << 20)
-		| (((p->ctty >> 16) & 0xfff) <<  8)
-		|   (p->ctty        & 0xff);
+  int tty_nr = 0;
+  if (p->ctty > 0)
+    tty_nr =   (((p->ctty >>  8) & 0xff)  << 20)
+	     | (((p->ctty >> 16) & 0xfff) <<  8)
+	     |   (p->ctty        & 0xff);
 
   if (p->process_state & PID_EXITED)
     strcpy (cmd, "<defunct>");
-- 
2.38.1


[-- Attachment #3: 0001-Cygwin-proc-stat-ctty-3.3.patch --]
[-- Type: application/octet-stream, Size: 1165 bytes --]

From 259e1bd425da890043da137bb963a6634040043c Mon Sep 17 00:00:00 2001
From: Andy Koppe <andy.koppe@gmail.com>
Date: Wed, 9 Nov 2022 21:01:58 +0000
Subject: [PATCH] Cygwin: Correct /proc/*/stat for processes without ctty

Report 0 instead of -1 in the tty field of /proc/*/stat for processes
without a controlling terminal. This is what the procps utility expects
when selecting or excluding such processes.
---
 winsup/cygwin/fhandler_process.cc | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/winsup/cygwin/fhandler_process.cc b/winsup/cygwin/fhandler_process.cc
index b5bc0e4fc..7f475580d 100644
--- a/winsup/cygwin/fhandler_process.cc
+++ b/winsup/cygwin/fhandler_process.cc
@@ -1183,8 +1183,8 @@ format_process_stat (void *data, char *&destbuf)
 				   "%U %lu "
 				   "%ld %lu\n",
 			  p->pid, cmd, state,
-			  p->ppid, p->pgid, p->sid, p->ctty, -1,
-			  0, fault_count, fault_count, 0, 0, utime, stime,
+			  p->ppid, p->pgid, p->sid, (p->ctty > 0 ? p->ctty : 0),
+			  -1, 0, fault_count, fault_count, 0, 0, utime, stime,
 			  utime, stime, NZERO + nice, nice, 0, 0,
 			  start_time, vmsize,
 			  vmrss, vmmaxrss
-- 
2.38.1


             reply	other threads:[~2022-11-09 21:36 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-09 21:36 Andy Koppe [this message]
2022-11-10  9:10 ` Corinna Vinschen

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=CAHWeT-a3FhOO2Fc6bEm6ZuW4qcHkY2wz47OMuXKzfSgROzUOMg@mail.gmail.com \
    --to=andy.koppe@gmail.com \
    --cc=cygwin-patches@cygwin.com \
    /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.
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 read-only IMAP folder(s) and NNTP newsgroup(s).