This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git

commit 4dead4cb9f5283c5563f0eda686ee71c55fe7e50
Author: Marco Casaroli <[email protected]>
AuthorDate: Fri Oct 23 17:52:05 2026 +0200

    system/nxinit: Make the console the controlling terminal of a service.
    
    A service with the "console" option got the console device as stdin,
    stdout and stderr, but the console was not its controlling terminal.
    Ctrl-C and Ctrl-Z sent no signal to it, and a shell on the console
    found no foreground process group with tcgetpgrp() and turned job
    control off ("can't access tty; job control turned off").
    
    After the start of such a service, NxInit now makes the console its
    controlling terminal with TIOCSCTTY and the PID of the service, as init
    and getty do on other systems.  A console without TIOCSCTTY (for
    example without CONFIG_TTY_SIGINT and CONFIG_TTY_SIGTSTP) is not an
    error.  The NSH sh program gives up the terminal at its prompt (the
    previous commit), so Ctrl-C at the prompt of a console sh does not end
    it.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 system/nxinit/service.c | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)

diff --git a/system/nxinit/service.c b/system/nxinit/service.c
index c4df0bbe2..bec827ecc 100644
--- a/system/nxinit/service.c
+++ b/system/nxinit/service.c
@@ -35,6 +35,7 @@
 #include <time.h>
 #include <signal.h>
 #include <spawn.h>
+#include <sys/ioctl.h>
 #include <sys/param.h>
 #include <unistd.h>
 
@@ -489,6 +490,38 @@ errout:
   return -ret;
 }
 
+/****************************************************************************
+ * Name: service_ctty
+ *
+ * Description:
+ *   Make the console device the controlling terminal of the service with
+ *   the PID pid.  A device without TIOCSCTTY is not an error.
+ *
+ ****************************************************************************/
+
+static void service_ctty(FAR struct service_s *service, pid_t pid)
+{
+  FAR const char *dev = service->console ?
+                        service->console :
+                        CONFIG_SYSTEM_NXINIT_CONSOLE_DEV;
+  int fd;
+
+  fd = open(dev, O_RDWR | O_NOCTTY | O_CLOEXEC);
+  if (fd < 0)
+    {
+      init_warn("Open console '%s' %d", dev, errno);
+      return;
+    }
+
+  if (ioctl(fd, TIOCSCTTY, pid) < 0)
+    {
+      init_info("Console '%s' of pid %d: no controlling terminal %d", dev,
+                pid, errno);
+    }
+
+  close(fd);
+}
+
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -710,6 +743,11 @@ int init_service_start(FAR struct service_s *service)
       return -ret;
     }
 
+  if (check_flags(service, SVC_CONSOLE))
+    {
+      service_ctty(service, pid);
+    }
+
   service->pid = pid;
   add_flags(service, SVC_RUNNING);
   remove_flags(service, SVC_RESTARTING);

Reply via email to