public inbox for cygwin-patches@cygwin.com
 help / color / mirror / Atom feed
* [PATCH] Cygwin: Correct /proc/*/stat for processes without ctty
@ 2022-11-09 21:36 Andy Koppe
  2022-11-10  9:10 ` Corinna Vinschen
  0 siblings, 1 reply; 2+ messages in thread
From: Andy Koppe @ 2022-11-09 21:36 UTC (permalink / raw)
  To: cygwin-patches

[-- 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


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] Cygwin: Correct /proc/*/stat for processes without ctty
  2022-11-09 21:36 [PATCH] Cygwin: Correct /proc/*/stat for processes without ctty Andy Koppe
@ 2022-11-10  9:10 ` Corinna Vinschen
  0 siblings, 0 replies; 2+ messages in thread
From: Corinna Vinschen @ 2022-11-10  9:10 UTC (permalink / raw)
  To: cygwin-patches

On Nov  9 21:36, Andy Koppe wrote:
> 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.

Pushed in master.  We won't do another 3.3, so I skipped it.


Thanks,
Corinna

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-11-10  9:10 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-09 21:36 [PATCH] Cygwin: Correct /proc/*/stat for processes without ctty Andy Koppe
2022-11-10  9:10 ` Corinna Vinschen

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).