Re: [Toybox] [New toy] rudimentary getty

2013-12-19 Thread Ashwini Sharma
Hi,

What a coincidence, even I was about to send getty implementation in this
forum.

Attached is a little largeish version of getty.
Supporting TERM settings, baud rate setting, issue file, updating utmp
entries

if this suits, can add to the tree.
Looking forward to your inputs.

regards,
Ashwini


On Thu, Dec 19, 2013 at 12:49 PM, ibid...@gmail.com wrote:

 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

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


/* getty.c - A getty program to get controlling terminal.
 *
 * Copyright 2012 Sandeep Sharma sandeep.jack2...@gamil.com
 * Copyright 2013 Kyungwan Han asura...@gmail.com
 *
 * No Standard.

USE_GETTY(NEWTOY(getty, 2t#0H:I:l:f:iwnmLh,TOYFLAG_SBIN))

config GETTY
  bool getty
  default n
  help
Usage: getty [OPTIONS] BAUD_RATE[,BAUD_RATE]... TTY [TERMTYPE]

-hEnable hardware RTS/CTS flow control
-LSet CLOCAL (ignore Carrier Detect state)
-mGet baud rate from modem's CONNECT status message
-nDon't prompt for login name
-wWait for CR or LF before sending /etc/issue
-iDon't display /etc/issue
-f ISSUE_FILE  Display ISSUE_FILE instead of /etc/issue
-l LOGIN  Invoke LOGIN instead of /bin/login
-t SECTerminate after SEC if no login name is read
-I INITSTR  Send INITSTR before anything else
-H HOSTLog HOST into the utmp file as the hostname
*/
#define FOR_getty
#include toys.h
#include utmp.h

GLOBALS(
  char *issue_str;
  char *login_str;
  char *init_str;
  char *host_str; 
  long timeout;
  
  char *tty_name;  
  int  speeds[20];
  int  sc;  
  struct termios termios;
  char buff[128];
)

#define CTL(x)((x) ^ 0100) 
#define HOSTNAME_SIZE 32

typedef void (*sighandler_t)(int);
struct speed_mapper {
  long speed;
  speed_t code;
};

struct speed_mapper speedtab[] = {
  {50, B50}, {75, B75}, {110, B110}, {134, B134}, {150, B150}, {200, B200},
  {300, B300}, {600, B600}, {1200, B1200}, {1800, B1800}, {2400, B2400},
  {4800, B4800}, {9600, B9600},
#ifdef  B19200
  {19200, B19200},
#endif
#ifdef  B38400
  {38400, B38400},
#endif
#ifdef  EXTA
  {19200, EXTA},
#endif
#ifdef  EXTB
  {38400, B38400},
#endif
#ifdef B57600
  {57600, B57600},
#endif
#ifdef B115200
  {115200, B115200},
#endif
#ifdef B230400
  {230400, B230400},
#endif
  {0, 0},
};

// Find speed from mapper array 
static speed_t encode(char *s)
{
  struct speed_mapper *sp;
  long speed = atolx(s);

  if (!speed) return 0;
  for (sp = speedtab; sp-speed; sp++) if (sp-speed == speed) return sp-code;
  return (speed_t) -1;
}

static void get_speed(char *sp)
{
  char *ptr;

  TT.sc = 0;
  while ((ptr = strsep(sp, ,))) {
TT.speeds[TT.sc] = encode(ptr);
if (TT.speeds[TT.sc]  0) perror_exit(Bad Speed);
if (++TT.sc  10) perror_exit(Too many alternate speeds, Max is 10);
  }
}

// Parse args and set TERM env. variable
static void parse_arguments(void)
{
  if (isdigit(**toys.optargs)) {
get_speed(*toys.optargs);
if (*++toys.optargs) TT.tty_name = xmsprintf(%s, *toys.optargs);
  } else {
TT.tty_name = xmsprintf(%s, *toys.optargs);
if (*++toys.optargs) get_speed(*toys.optargs);
  } 
  if (*++toys.optargs) setenv(TERM, *toys.optargs, 1);
}

// Get controlling terminal and redirect stdio 
static void open_tty(void)
{
  if (strcmp(TT.tty_name, -)) {
if (*(TT.tty_name) != '/') TT.tty_name = xmsprintf(/dev/%s, TT.tty_name);
// Sends SIGHUP to all foreground process if Session leader don't die,Ignore
sighandler_t sig = signal(SIGHUP, SIG_IGN); 
ioctl(0, TIOCNOTTY, 0); // Giveup if there is any controlling 

[Toybox] [New toy] rudimentary getty

2013-12-18 Thread ibid . ag
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 ibid...@gmail.com
 *
 * 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
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net