aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKoichi Murase <myoga.murase@gmail.com>2021-08-24 06:25:05 +0900
committerHiltjo Posthuma <hiltjo@codemadness.org>2021-08-24 13:30:11 +0200
commit1d3142da968da7f6f61f1c1708f39ca233eda150 (patch)
treeb4c2746f480c6d2d03a4dd477b57dcd6990d2dfe
parent2ec571a30c0c3b1a17f6b3631c80d573582f59a1 (diff)
downloadst-patched-1d3142da968da7f6f61f1c1708f39ca233eda150.tar.bz2
st-patched-1d3142da968da7f6f61f1c1708f39ca233eda150.tar.xz
st-patched-1d3142da968da7f6f61f1c1708f39ca233eda150.zip
fix a problem that the standard streams are unexpectedly closed
In the current implementation, the slave PTY (assigned to the variable `s') is always closed after duplicating it to file descriptors of standard streams (0, 1, and 2). However, when the allocated slave PTY `s' is already one of 0, 1, or 2, this causes unexpected closing of a standard stream. The same problem occurs when the file descriptor of the master PTY (the variable `m') is one of 0, 1, or 2. In this patch, the original master PTY (m) is closed before it would be overwritten by duplicated slave PTYs. The original slave PTY (s) is closed only when it is not one of the stanrad streams.
-rw-r--r--st.c5
1 files changed, 3 insertions, 2 deletions
diff --git a/st.c b/st.c
index ebdf360..a9338e1 100644
--- a/st.c
+++ b/st.c
@@ -793,14 +793,15 @@ ttynew(const char *line, char *cmd, const char *out, char **args)
793 break; 793 break;
794 case 0: 794 case 0:
795 close(iofd); 795 close(iofd);
796 close(m);
796 setsid(); /* create a new process group */ 797 setsid(); /* create a new process group */
797 dup2(s, 0); 798 dup2(s, 0);
798 dup2(s, 1); 799 dup2(s, 1);
799 dup2(s, 2); 800 dup2(s, 2);
800 if (ioctl(s, TIOCSCTTY, NULL) < 0) 801 if (ioctl(s, TIOCSCTTY, NULL) < 0)
801 die("ioctl TIOCSCTTY failed: %s\n", strerror(errno)); 802 die("ioctl TIOCSCTTY failed: %s\n", strerror(errno));
802 close(s); 803 if (s > 2)
803 close(m); 804 close(s);
804#ifdef __OpenBSD__ 805#ifdef __OpenBSD__
805 if (pledge("stdio getpw proc exec", NULL) == -1) 806 if (pledge("stdio getpw proc exec", NULL) == -1)
806 die("pledge\n"); 807 die("pledge\n");