From: Eric Freese <ericdfreese@gmail.com>
To: zsh-workers@zsh.org
Cc: Eric Freese <ericdfreese@gmail.com>
Subject: [PATCH] Remove zpty exit hook from forked processes
Date: Tue, 31 Jan 2017 23:12:02 -0700 [thread overview]
Message-ID: <20170201061202.47726-1-ericdfreese@gmail.com> (raw)
Fixes a bug where `zpty -d foo` kills `foo` and all pty commands started
before `foo`.
Steps to reproduce before the patch:
% zmodload zsh/zpty
% zpty -b foo cat
% zpty -b bar cat
% zpty -b baz cat
% zpty -d bar
% zpty -t foo; echo $?
1
% zpty -t bar; echo $?
zpty: no such pty command: bar
1
% zpty -t baz; echo $?
0
Results after the patch (note the exit code of `zpty -t foo`):
% zmodload zsh/zpty
% zpty -b foo cat
% zpty -b bar cat
% zpty -b baz cat
% zpty -d bar
% zpty -t foo; echo $?
0
% zpty -t bar; echo $?
zpty: no such pty command: bar
1
% zpty -t baz; echo $?
0
---
Src/Modules/zpty.c | 91 +++++++++++++++++++++++++++---------------------------
1 file changed, 46 insertions(+), 45 deletions(-)
diff --git a/Src/Modules/zpty.c b/Src/Modules/zpty.c
index 2c87be1..fbc1a88 100644
--- a/Src/Modules/zpty.c
+++ b/Src/Modules/zpty.c
@@ -294,6 +294,51 @@ get_pty(int master, int *retfd)
#endif /* /dev/ptmx or alternatives */
+static void
+deleteptycmd(Ptycmd cmd)
+{
+ Ptycmd p, q;
+
+ for (q = NULL, p = ptycmds; p != cmd; q = p, p = p->next);
+
+ if (p != cmd)
+ return;
+
+ if (q)
+ q->next = p->next;
+ else
+ ptycmds = p->next;
+
+ zsfree(p->name);
+ freearray(p->args);
+
+ zclose(cmd->fd);
+
+ /* We kill the process group the command put itself in. */
+
+ kill(-(p->pid), SIGHUP);
+
+ zfree(p, sizeof(*p));
+}
+
+static void
+deleteallptycmds(void)
+{
+ Ptycmd p, n;
+
+ for (p = ptycmds; p; p = n) {
+ n = p->next;
+ deleteptycmd(p);
+ }
+}
+
+static int
+ptyhook(UNUSED(Hookdef d), UNUSED(void *dummy))
+{
+ deleteallptycmds();
+ return 0;
+}
+
static int
newptycmd(char *nam, char *pname, char **args, int echo, int nblock)
{
@@ -331,6 +376,7 @@ newptycmd(char *nam, char *pname, char **args, int echo, int nblock)
/* This code copied from the clone module, except for getting *
* the descriptor from get_pty() and duplicating it to 0/1/2. */
+ deletehookfunc("exit", ptyhook);
clearjobtab(0);
ppid = getppid();
mypid = getpid();
@@ -469,44 +515,6 @@ newptycmd(char *nam, char *pname, char **args, int echo, int nblock)
return 0;
}
-static void
-deleteptycmd(Ptycmd cmd)
-{
- Ptycmd p, q;
-
- for (q = NULL, p = ptycmds; p != cmd; q = p, p = p->next);
-
- if (p != cmd)
- return;
-
- if (q)
- q->next = p->next;
- else
- ptycmds = p->next;
-
- zsfree(p->name);
- freearray(p->args);
-
- zclose(cmd->fd);
-
- /* We kill the process group the command put itself in. */
-
- kill(-(p->pid), SIGHUP);
-
- zfree(p, sizeof(*p));
-}
-
-static void
-deleteallptycmds(void)
-{
- Ptycmd p, n;
-
- for (p = ptycmds; p; p = n) {
- n = p->next;
- deleteptycmd(p);
- }
-}
-
/**** a better process handling would be nice */
static void
@@ -852,13 +860,6 @@ bin_zpty(char *nam, char **args, Options ops, UNUSED(int func))
}
}
-static int
-ptyhook(UNUSED(Hookdef d), UNUSED(void *dummy))
-{
- deleteallptycmds();
- return 0;
-}
-
static struct builtin bintab[] = {
BUILTIN("zpty", 0, bin_zpty, 0, -1, 0, "ebdmrwLnt", NULL),
--
2.9.0
next reply other threads:[~2017-02-02 8:06 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-02-01 6:12 Eric Freese [this message]
2017-02-01 20:41 Eric Freese
2017-02-02 0:56 ` Bart Schaefer
2017-02-02 1:25 ` Eric Freese
2017-02-02 17:38 ` Eric Freese
2017-02-03 0:49 ` Bart Schaefer
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=20170201061202.47726-1-ericdfreese@gmail.com \
--to=ericdfreese@gmail.com \
--cc=zsh-workers@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).