@@ -52,7 +52,7 @@ typedef struct {
52
52
int history ;
53
53
int w ;
54
54
int h ;
55
- volatile sig_atomic_t need_resize ;
55
+ bool need_resize ;
56
56
} Screen ;
57
57
58
58
typedef struct {
@@ -248,6 +248,10 @@ static const char *shell;
248
248
static Register copyreg ;
249
249
static volatile sig_atomic_t running = true;
250
250
static bool runinall = false;
251
+ static int sigwinch_pipe [] = {-1 , -1 };
252
+ static int sigchld_pipe [] = {-1 , -1 };
253
+
254
+ enum {PIPE_RD , PIPE_WR };
251
255
252
256
static void
253
257
eprint (const char * errstr , ...) {
@@ -698,6 +702,11 @@ get_client_by_coord(unsigned int x, unsigned int y) {
698
702
699
703
static void
700
704
sigchld_handler (int sig ) {
705
+ write (sigchld_pipe [PIPE_WR ], "\0" , 1 );
706
+ }
707
+
708
+ static void
709
+ handle_sigchld () {
701
710
int errsv = errno ;
702
711
int status ;
703
712
pid_t pid ;
@@ -731,6 +740,11 @@ sigchld_handler(int sig) {
731
740
732
741
static void
733
742
sigwinch_handler (int sig ) {
743
+ write (sigwinch_pipe [PIPE_WR ], "\0" , 1 );
744
+ }
745
+
746
+ static void
747
+ handle_sigwinch () {
734
748
screen .need_resize = true;
735
749
}
736
750
@@ -939,6 +953,14 @@ getshell(void) {
939
953
return "/bin/sh" ;
940
954
}
941
955
956
+ static bool
957
+ set_blocking (int fd , bool blocking ) {
958
+ int flags = fcntl (fd , F_GETFL , 0 );
959
+ if (flags < 0 ) return false;
960
+ flags = blocking ? (flags & ~O_NONBLOCK ) : (flags |O_NONBLOCK );
961
+ return (fcntl (fd , F_SETFL , flags ) == 0 ) ? true : false;
962
+ }
963
+
942
964
static void
943
965
setup (void ) {
944
966
shell = getshell ();
@@ -962,6 +984,23 @@ setup(void) {
962
984
colors [i ].pair = vt_color_reserve (colors [i ].fg , colors [i ].bg );
963
985
}
964
986
resize_screen ();
987
+
988
+ int * pipes [] = {& sigwinch_pipe [0 ], & sigchld_pipe [0 ]};
989
+ for (int i = 0 ; i < 2 ; ++ i ) {
990
+ int r = pipe (pipes [i ]);
991
+ if (r < 0 ) {
992
+ perror ("pipe()" );
993
+ exit (EXIT_FAILURE );
994
+ }
995
+
996
+ for (int j = 0 ; j < 2 ; ++ j ) {
997
+ if (!set_blocking (pipes [i ][j ], false)) {
998
+ perror ("fcntl()" );
999
+ exit (EXIT_FAILURE );
1000
+ }
1001
+ }
1002
+ }
1003
+
965
1004
struct sigaction sa ;
966
1005
memset (& sa , 0 , sizeof sa );
967
1006
sa .sa_flags = 0 ;
@@ -1815,20 +1854,13 @@ main(int argc, char *argv[]) {
1815
1854
KeyCombo keys ;
1816
1855
unsigned int key_index = 0 ;
1817
1856
memset (keys , 0 , sizeof (keys ));
1818
- sigset_t emptyset , blockset ;
1819
1857
1820
1858
setenv ("DVTM" , VERSION , 1 );
1821
1859
if (!parse_args (argc , argv )) {
1822
1860
setup ();
1823
1861
startup (NULL );
1824
1862
}
1825
1863
1826
- sigemptyset (& emptyset );
1827
- sigemptyset (& blockset );
1828
- sigaddset (& blockset , SIGWINCH );
1829
- sigaddset (& blockset , SIGCHLD );
1830
- sigprocmask (SIG_BLOCK , & blockset , NULL );
1831
-
1832
1864
while (running ) {
1833
1865
int r , nfds = 0 ;
1834
1866
fd_set rd ;
@@ -1841,9 +1873,15 @@ main(int argc, char *argv[]) {
1841
1873
FD_ZERO (& rd );
1842
1874
FD_SET (STDIN_FILENO , & rd );
1843
1875
1876
+ FD_SET (sigwinch_pipe [PIPE_RD ], & rd );
1877
+ nfds = MAX (nfds , sigwinch_pipe [PIPE_RD ]);
1878
+
1879
+ FD_SET (sigchld_pipe [PIPE_RD ], & rd );
1880
+ nfds = MAX (nfds , sigchld_pipe [PIPE_RD ]);
1881
+
1844
1882
if (cmdfifo .fd != -1 ) {
1845
1883
FD_SET (cmdfifo .fd , & rd );
1846
- nfds = cmdfifo .fd ;
1884
+ nfds = MAX ( nfds , cmdfifo .fd ) ;
1847
1885
}
1848
1886
1849
1887
if (bar .fd != -1 ) {
@@ -1867,7 +1905,7 @@ main(int argc, char *argv[]) {
1867
1905
}
1868
1906
1869
1907
doupdate ();
1870
- r = pselect (nfds + 1 , & rd , NULL , NULL , NULL , & emptyset );
1908
+ r = select (nfds + 1 , & rd , NULL , NULL , NULL );
1871
1909
1872
1910
if (r < 0 ) {
1873
1911
if (errno == EINTR )
@@ -1903,6 +1941,18 @@ main(int argc, char *argv[]) {
1903
1941
continue ;
1904
1942
}
1905
1943
1944
+ if (FD_ISSET (sigwinch_pipe [PIPE_RD ], & rd )) {
1945
+ char buf [512 ];
1946
+ while (read (sigwinch_pipe [PIPE_RD ], & buf , sizeof (buf )) > 0 );
1947
+ handle_sigwinch ();
1948
+ }
1949
+
1950
+ if (FD_ISSET (sigchld_pipe [PIPE_RD ], & rd )) {
1951
+ char buf [512 ];
1952
+ while (read (sigchld_pipe [PIPE_RD ], & buf , sizeof (buf )) > 0 );
1953
+ handle_sigchld ();
1954
+ }
1955
+
1906
1956
if (cmdfifo .fd != -1 && FD_ISSET (cmdfifo .fd , & rd ))
1907
1957
handle_cmdfifo ();
1908
1958
0 commit comments