Hi,
The -f option for netcat doesn't seem to be doing anything right now. It is
missing a call to pollinate() after opening the specified device file.
The patch adds back that line of pollinate().
Also make sure that the timeout handler is not armed for -f mode as -f
shouldn't timeout. File open() should just succeed or fail immediately.
From 32934abc3781f45a49f2698db788a4f0e79bd345 Mon Sep 17 00:00:00 2001
From: Yi-Yo Chiang <yochi...@google.com>
Date: Fri, 10 May 2024 18:36:38 +0800
Subject: [PATCH] Allow netcat to communicate with serial consoles (-f)

Right now the (-f) option just opens the tty device and does nothing.
Change it so that we actually read from / write to the specified file.

Also don't arm the timeout signal handler if using (-f) mode as the
timeout option is for connecting to / listening on sockets.
If (-f) is specified then we are opening a device node, which would
just succeed or fail immediately.

Bug: 335362012
Test: adb shell netcat -f /dev/pts/0
Change-Id: I8b53c1eaa8c6363f77da5d023c86dd286e442265
---
 toys/net/netcat.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/toys/net/netcat.c b/toys/net/netcat.c
index 4e76fa7f..82bce20f 100644
--- a/toys/net/netcat.c
+++ b/toys/net/netcat.c
@@ -89,12 +89,6 @@ void netcat_main(void)
   TT.W = TT.W ? TT.W*1000 : -1;
   TT.q = TT.q ? TT.q*1000 : -1;
 
-  xsignal(SIGCHLD, SIG_IGN);
-  if (TT.w) {
-    xsignal(SIGALRM, timeout);
-    alarm(TT.w);
-  }
-
   // The argument parsing logic can't make "<2" conditional on other
   // arguments like -f and -l, so do it by hand here.
   if (FLAG(f) ? toys.optc : (!FLAG(l) && !FLAG(L) && toys.optc!=2-FLAG(U)))
@@ -104,8 +98,16 @@ void netcat_main(void)
   else if (FLAG(6)) family = AF_INET6;
   else if (FLAG(U)) family = AF_UNIX;
 
-  if (TT.f) in1 = out2 = xopen(TT.f, O_RDWR);
-  else {
+  if (TT.f) {
+    in1 = out2 = xopen(TT.f, O_RDWR);
+    pollinate(in1, in2, out1, out2, TT.W, TT.q);
+  } else {
+    xsignal(SIGCHLD, SIG_IGN);
+    if (TT.w) {
+      xsignal(SIGALRM, timeout);
+      alarm(TT.w);
+    }
+
     // Setup socket
     if (!FLAG(l) && !FLAG(L)) {
       char *host = toys.optargs[0];
-- 
2.45.0.118.g7fe29c98d7-goog

_______________________________________________
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to