Here's a basic getty.
It just reopens the current tty in such a way as to allow blocking code
to work, then calls the login program of your choice (-l).
/etc/issue is ignored, so -i is a nop.
It does NOT modify the environment (including setting TERM), support
baud rate setting or autodetection, do anything with modems, prompt for
any user input, or open new ttys.
Supporting 
getty ttyN $TERM
should be something like this...
char * prepend(char *arg, char *pfix)
{
  if (!arg) return NULL;
  int alen = strlen(arg), plen = strlen(pfix);
  char * ret = xzalloc(alen + plen + 1);
  strcpy(ret, pfix);
  strcpy(ret+plen, arg);
  return ret;
}
  

  char *tty = prepend(toys.optargs[0], "/dev/")
  if (!tty) tty = ttyname(0);
  if (toys.optc > 1) {
    char *termname = prepend(toys.optargs[1], "TERM=");
    if (termname) putenv(termname);
  }

--
With this, I can boot to a login prompt using only a shell and a toybox
binary (with several pending applets enabled, and a hacked version of
one of the many submissions for mount).

I also have a small applet (resolve_modalias) that is mainly useful for 
testing, which I've sent a couple times to illustrate issues;
maybe sometime I should see about putting the core code into a library
function so modprobe and modinfo can share it....

Thanks,
Isaac Dunham
/* getty.c - get a controlling tty
 *
 * Copyright 2013 Isaac Dunham <[email protected]>
 *
 * No standard.

USE_GETTY(NEWTOY(getty, "il:", TOYFLAG_NEEDROOT|TOYFLAG_SBIN))

config GETTY
  bool "getty"
  default n
  help
    usage: getty [-i] [-l LOGIN_CMD]
    open a tty as controlling terminal, spawn a login program
    -i  Ignore /etc/issue (default for now)
    -l  login command (instead of "login")
*/

#define FOR_getty
#include <toys.h>

GLOBALS(
  char *login;

  char *nul; // so we can xexec(&TT.login)
)


void getty_main(void)
{
  char *tty = ttyname(0);
  int fd;
  if (!TT.login) TT.login = "login";

  if (!tty) perror_exit("no tty");
  close(0); close(1); close(2);
  errno = 0;

  fd = xopen(tty, O_RDWR);
  if (fd) dup2(fd, 0);
  dup2(0, 1);
  dup2(0, 2);
  if (errno) xexit();
  xexec(&TT.login);
}
_______________________________________________
Toybox mailing list
[email protected]
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to