Multiple NSH Sessions

Q: I would like to run the NuttShell on multiple serial ports, but haven't figured it out yet; can you point me in the right direction?

A: Easy. Don't use apps/examples/nsh_main.c. Create your own main function function something like this (with all error handling omitted for simplicity). By the way, this is all standard POSIX stuff that you can get detailed information about by just googling dup2 or maybe I/O redirection:

int my_main(int argc, char **argv)
  const char *tty = argv[1];
  int fd = open(tty, O_RDWR);
  (void)dup2(fd, 0);
  (void)dup2(fd, 1);
  (void)dup2(fd, 2);

And the rest is just like the original nsh_main() function (in fact, perhaps the existing nsh_main() function could be optionally extended to accept a console device string?). Then you can start a new NSH session on any TTY like:

nsh> mynsh /dev/ttyS2 &

This should cause a new NSH session to appear on on ttyS2. That session will persist until you do the following from the new session:

 nsh> exit

Then the new session, i.e., my_main() will exit.

If you were to do something like:

nsh> mynsh /dev/console

then you would get nested NSH sessions on the same console. The first session would halt and wait for the second session will take control of the console until it exits. Then the first session will take over console again.


In a previous discussion, I have talked about implementing the moral equivalent of getty in NuttX (of course, I want to call it nutty). A simple implementation of nutty would work like this:

  • It would wait on poll() on every (configured) serial device.
  • Whenever it is awakened, it would start something like my_main() above on the active serial port.
  • NSH has an option to enable logins, but it would be better to remove the existing login information from NSH centralize it in nutty.

That way, you could connect to any TTY, hit enter, and you would get an NSH session. Hmm… I am not sure how nutty would get the TTY back after the session is closed. I would need to think about that.

Other Ideas

There are other ways to get multiple NSH sessions:

  • Telnet already supports multiple sessions,
  • Implement the existing NSH as a ELF program then you an get multiple NSH sessions with posix_spawn by simply redirecting I/O
  • Using the tiny NxWM window managers, multiple NSH windows are already supported.