From: Corinna Vinschen <corinna-cygwin@cygwin.com>
To: cygwin-patches@cygwin.com
Subject: Re: [PATCH v2 4/5] Cygwin: pty: Prevent pty from changing code page of parent console.
Date: Mon, 18 Jan 2021 13:39:01 +0100 [thread overview]
Message-ID: <20210118123901.GB59030@calimero.vinschen.de> (raw)
In-Reply-To: <20210118112447.1518-1-takashi.yano@nifty.ne.jp>
Hi Takashi,
On Jan 18 20:24, Takashi Yano via Cygwin-patches wrote:
> @@ -59,6 +59,46 @@ struct pipe_reply {
> DWORD error;
> };
>
> +extern HANDLE attach_mutex; /* Defined in fhandler_console.cc */
> +
> +static DWORD
> +get_console_process_id (DWORD pid, bool match)
> +{
> + DWORD list1;
> + DWORD num, num_req;
> + num = 1;
> + num_req = GetConsoleProcessList (&list1, num);
> + DWORD *list;
> + if (num_req == 1)
> + list = &list1;
> + else
> + while (true)
> + {
> + list = (DWORD *)
> + HeapAlloc (GetProcessHeap (), 0, num_req * sizeof (DWORD));
> + num = num_req;
> + num_req = GetConsoleProcessList (list, num);
> + if (num_req > num)
> + HeapFree (GetProcessHeap (), 0, list);
> + else
> + break;
> + }
> + num = num_req;
> +
> + DWORD res = 0;
> + /* Last one is the oldest. */
> + /* https://github.com/microsoft/terminal/issues/95 */
> + for (int i = (int) num - 1; i >= 0; i--)
> + if ((match && list[i] == pid) || (!match && list[i] != pid))
> + {
> + res = list[i];
> + break;
> + }
> + if (num > 1)
> + HeapFree (GetProcessHeap (), 0, list);
> + return res;
> +}
Sorry if I'm slow, but I was just mulling over this code snippet again,
and I was wondering if we couldn't do without the HeapAlloc loop.
Assuming you use a tmp_pathbuf here, you'd have space for 16384
processes per console. Shouldn't that be more than enough? I.e.
static DWORD
get_console_process_id (DWORD pid, bool match)
{
tmp_pathbuf tp;
DWORD *list = (DWORD *) tp.w_get ();
const DWORD num = NT_MAX_PATH * sizeof (WCHAR) / sizeof (DWORD);
DWORD res = 0;
num = GetConsoleProcessList (&list, num);
/* Last one is the oldest. */
/* https://github.com/microsoft/terminal/issues/95 */
for (int i = (int) num - 1; i >= 0; i--)
if ((match && list[i] == pid) || (!match && list[i] != pid))
{
res = list[i];
break;
}
return res;
}
What do you think?
Corinna
next prev parent reply other threads:[~2021-01-18 12:39 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-01-18 11:24 Takashi Yano
2021-01-18 12:39 ` Corinna Vinschen [this message]
2021-01-18 12:57 ` Takashi Yano
2021-01-18 13:00 ` 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=20210118123901.GB59030@calimero.vinschen.de \
--to=corinna-cygwin@cygwin.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).