From 3b59a1cc7b3d9ea38ae3501c7a4e09597f7acdaa Mon Sep 17 00:00:00 2001
From: binlingyu <binlingyu@uniontech.com>
Date: Sun, 28 Apr 2024 13:41:35 +0800
Subject: [PATCH] fix use_pty function error

---
 lib/util/src/term.rs |  5 +++--
 src/exec_monitor.rs  | 12 ++++++------
 src/exec_pty.rs      | 38 +++++++++++++++++++++++---------------
 3 files changed, 32 insertions(+), 23 deletions(-)

diff --git a/lib/util/src/term.rs b/lib/util/src/term.rs
index 15bff23..c87c882 100644
--- a/lib/util/src/term.rs
+++ b/lib/util/src/term.rs
@@ -648,7 +648,7 @@ unsafe extern "C" fn sudo_term_raw_v1(fd: libc::c_int, isig: libc::c_int) -> boo
     };
     debug_decl!(stdext::function_name!().as_ptr(), SUDO_DEBUG_UTIL);
 
-    if changed != 0 && tcgetattr(fd, &mut oterm) != 0 {
+    if changed == 0 && tcgetattr(fd, &mut oterm) != 0 {
         debug_return_bool!(false);
     }
     memcpy(
@@ -684,7 +684,7 @@ unsafe extern "C" fn sudo_term_raw_v1(fd: libc::c_int, isig: libc::c_int) -> boo
 unsafe extern "C" fn sudo_term_cbreak_v1(fd: libc::c_int) -> bool {
     debug_decl!(stdext::function_name!().as_ptr(), SUDO_DEBUG_UTIL);
 
-    if changed != 0 && tcgetattr(fd, &mut oterm) != 0 {
+    if changed == 0 && tcgetattr(fd, &mut oterm) != 0 {
         debug_return_bool!(false);
     }
 
@@ -789,3 +789,4 @@ unsafe extern "C" fn sudo_term_copy_v1(src: libc::c_int, dst: libc::c_int) -> bo
     }
     debug_return_bool!(true)
 }
+
diff --git a/src/exec_monitor.rs b/src/exec_monitor.rs
index 890e30d..0ba0658 100644
--- a/src/exec_monitor.rs
+++ b/src/exec_monitor.rs
@@ -357,7 +357,6 @@ unsafe extern "C" fn mon_handle_sigchld(mut mc: *mut monitor_closure) {
         if !(pid == -(1 as libc::c_int) && errno!() == EINTR) {
             break;
         }
-        break;
     }
     match pid {
         0 => {
@@ -420,7 +419,7 @@ unsafe extern "C" fn mon_handle_sigchld(mut mc: *mut monitor_closure) {
             (*mc).cmnd_pid,
             signame.as_mut_ptr()
         );
-        (*mc).cmnd_pid == -(1 as libc::c_int);
+        (*mc).cmnd_pid = -(1 as libc::c_int);
     } else if WIFEXITED!(status) {
         sudo_debug_printf!(
             SUDO_DEBUG_INFO,
@@ -429,7 +428,7 @@ unsafe extern "C" fn mon_handle_sigchld(mut mc: *mut monitor_closure) {
             (*mc).cmnd_pid,
             WEXITSTATUS!(status)
         );
-        (*mc).cmnd_pid == -(1 as libc::c_int);
+        (*mc).cmnd_pid = -(1 as libc::c_int);
     } else {
         sudo_debug_printf!(
             SUDO_DEBUG_WARN,
@@ -772,7 +771,7 @@ unsafe extern "C" fn fill_exec_closure_monitor(
     }
 
     /* Event for forwarded signals via backchannel. */
-    let ref mut backchannel_event0 = (*mc).errpipe_event;
+    let ref mut backchannel_event0 = (*mc).backchannel_event;
     *backchannel_event0 = sudo_ev_alloc_v1(
         backchannel,
         (SUDO_EV_READ | SUDO_EV_PERSIST) as libc::c_short,
@@ -800,7 +799,7 @@ unsafe extern "C" fn fill_exec_closure_monitor(
     }
 
     /* Events for local signals. */
-    let ref mut sigint_event0 = (*mc).errpipe_event;
+    let ref mut sigint_event0 = (*mc).sigint_event;
     *sigint_event0 = sudo_ev_alloc_v1(
         SIGINT,
         SUDO_EV_SIGINFO as libc::c_short,
@@ -823,7 +822,7 @@ unsafe extern "C" fn fill_exec_closure_monitor(
         sudo_fatal!(b"unable to add event to queue\0" as *const u8 as *const libc::c_char,);
     }
 
-    let ref mut sigquit_event0 = (*mc).errpipe_event;
+    let ref mut sigquit_event0 = (*mc).sigquit_event;
     *sigquit_event0 = sudo_ev_alloc_v1(
         SIGQUIT,
         SUDO_EV_SIGINFO as libc::c_short,
@@ -1249,3 +1248,4 @@ pub unsafe extern "C" fn exec_monitor(
     }
     debug_return_int!(-(1 as libc::c_int));
 }
+
diff --git a/src/exec_pty.rs b/src/exec_pty.rs
index 7e14e72..a5cc675 100644
--- a/src/exec_pty.rs
+++ b/src/exec_pty.rs
@@ -2603,11 +2603,11 @@ pub unsafe extern "C" fn exec_pty(
     if io_pipe[STDIN_FILENO as usize][0 as usize] != -(1 as libc::c_int) {
         close(io_pipe[STDIN_FILENO as usize][0 as usize]);
     }
-    if io_pipe[STDOUT_FILENO as usize][0 as usize] != -(1 as libc::c_int) {
-        close(io_pipe[STDOUT_FILENO as usize][0 as usize]);
+    if io_pipe[STDOUT_FILENO as usize][1 as usize] != -(1 as libc::c_int) {
+        close(io_pipe[STDOUT_FILENO as usize][1 as usize]);
     }
-    if io_pipe[STDERR_FILENO as usize][0 as usize] != -(1 as libc::c_int) {
-        close(io_pipe[STDERR_FILENO as usize][0 as usize]);
+    if io_pipe[STDERR_FILENO as usize][1 as usize] != -(1 as libc::c_int) {
+        close(io_pipe[STDERR_FILENO as usize][1 as usize]);
     }
     close(sv[1 as usize]);
 
@@ -2701,19 +2701,24 @@ unsafe extern "C" fn add_io_events(mut evbase: *mut sudo_event_base) {
         }
         if !((*iob).wevent).is_null() {
             /* Enable writer if buffer is not empty. */
-            sudo_debug_printf!(
-                SUDO_DEBUG_INFO,
-                b"added I/O wevent %p, fd %d, events %d\0" as *const u8 as *const libc::c_char,
-                (*iob).wevent,
-                (*(*iob).wevent).fd,
-                (*(*iob).wevent).events as libc::c_int
-            );
-            if sudo_ev_add_v2(evbase, (*iob).wevent, 0 as *mut timespec, false)
-                == -(1 as libc::c_int)
-            {
-                sudo_fatal!(b"unable to add event to queue\0" as *const u8 as *const libc::c_char,);
+            if (*iob).len > (*iob).off {
+                sudo_debug_printf!(
+                    SUDO_DEBUG_INFO,
+                    b"added I/O wevent %p, fd %d, events %d\0" as *const u8 as *const libc::c_char,
+                    (*iob).wevent,
+                    (*(*iob).wevent).fd,
+                    (*(*iob).wevent).events as libc::c_int
+                );
+                if sudo_ev_add_v2(evbase, (*iob).wevent, 0 as *mut timespec, false)
+                    == -(1 as libc::c_int)
+                {
+                    sudo_fatal!(
+                        b"unable to add event to queue\0" as *const u8 as *const libc::c_char,
+                    );
+                }
             }
         }
+        iob = (*iob).entries.sle_next;
     }
     debug_return!();
 }
@@ -2833,6 +2838,7 @@ unsafe extern "C" fn del_io_events(mut nonblocking: bool) {
                     }
                 }
             }
+            iob = (*iob).entries.sle_next;
         }
         sudo_debug_printf!(
             SUDO_DEBUG_INFO,
@@ -2857,6 +2863,7 @@ unsafe extern "C" fn del_io_events(mut nonblocking: bool) {
                     );
                 }
             }
+            iob = (*iob).entries.sle_next;
         }
     }
 
@@ -2977,3 +2984,4 @@ unsafe extern "C" fn safe_close(mut fd: libc::c_int) -> libc::c_int {
     );
     debug_return_int!(close(fd));
 }
+
-- 
2.43.0

