source: rtems/cpukit/telnetd/pty.c @ e652e5f8

4.115
Last change on this file since e652e5f8 was e652e5f8, checked in by Sebastian Huber <sebastian.huber@…>, on 08/20/12 at 10:10:03

telnetd: Inform client that we will echo

The standard line editor rtems_shell_line_editor() produces an echo.

  • Property mode set to 100644
File size: 17.0 KB
Line 
1/*
2 * /dev/ptyXX  (Support for pseudo-terminals)
3 *
4 *  Original Author: Fernando RUIZ CASAS (fernando.ruiz@ctv.es)
5 *  May 2001
6 *
7 *  This program is distributed in the hope that it will be useful,
8 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
9 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 *
11 *  Till Straumann <strauman@slac.stanford.edu>
12 *
13 *   - converted into a loadable module
14 *   - NAWS support / ioctls for querying/setting the window
15 *     size added.
16 *   - don't delete the running task when the connection
17 *     is closed. Rather let 'read()' return a 0 count so
18 *     they may cleanup. Some magic hack works around termios
19 *     limitation.
20 */
21
22#ifdef HAVE_CONFIG_H
23#include "config.h"
24#endif
25
26#define DEBUG_WH    (1<<0)
27#define DEBUG_DETAIL  (1<<1)
28
29/* #define DEBUG DEBUG_WH */
30
31/*-----------------------------------------*/
32#include <termios.h>
33#include <rtems/termiostypes.h>
34#include <sys/ttycom.h>
35#include <rtems.h>
36#include <rtems/libio.h>
37#include <rtems/bspIo.h>
38#include <errno.h>
39#include <sys/socket.h>
40/*-----------------------------------------*/
41#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
44#include <unistd.h>
45/*-----------------------------------------*/
46#define IAC_ESC    255
47#define IAC_DONT   254
48#define IAC_DO     253
49#define IAC_WONT   252
50#define IAC_WILL   251
51#define IAC_SB     250
52#define IAC_GA     249
53#define IAC_EL     248
54#define IAC_EC     247
55#define IAC_AYT    246
56#define IAC_AO     245
57#define IAC_IP     244
58#define IAC_BRK    243
59#define IAC_DMARK  242
60#define IAC_NOP    241
61#define IAC_SE     240
62#define IAC_EOR    239
63
64#define SB_MAX    16
65
66extern int rtems_telnetd_maximum_ptys;
67
68struct pty_tt;
69typedef struct pty_tt pty_t;
70
71struct pty_tt {
72 char                     *devname;
73 struct rtems_termios_tty *ttyp;
74 tcflag_t                  c_cflag;
75 int                       opened;
76 int                       socket;
77 int                       last_cr;
78 unsigned                  iac_mode;
79 unsigned char             sb_buf[SB_MAX];
80 int                       sb_ind;
81 int                       width;
82 int                       height;
83};
84
85
86static int    telnet_pty_inited=FALSE;
87static pty_t *telnet_ptys;
88
89static rtems_device_major_number pty_major;
90
91static
92int send_iac(int minor,unsigned char mode,unsigned char option)
93{
94  unsigned char buf[3];
95
96  buf[0]=IAC_ESC;
97  buf[1]=mode;
98  buf[2]=option;
99  return write(telnet_ptys[minor].socket,buf,sizeof(buf));
100}
101
102/* This procedure returns the devname for a pty slot free.
103 * If not slot availiable (field socket>=0)
104 *  then the socket argument is closed
105 */
106
107char *  telnet_get_pty(int socket)
108{
109  int ndx;
110
111  if (telnet_pty_inited) {
112#if 0
113    if ( rtems_telnetd_maximum_ptys < 5 )
114      rtems_telnetd_maximum_ptys = 5;
115
116    telnet_ptys = malloc( rtems_telnetd_maximum_ptys * sizeof (pty_t) );
117#endif
118    if ( !telnet_ptys ) {
119      return NULL;
120    }
121
122    for (ndx=0;ndx<rtems_telnetd_maximum_ptys;ndx++) {
123
124      if (telnet_ptys[ndx].socket<0) {
125        struct timeval t;
126        /* set a long polling interval to save CPU time */
127        t.tv_sec=2;
128        t.tv_usec=00000;
129        setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, &t, sizeof(t));
130        telnet_ptys[ndx].socket=socket;
131
132        /* inform the client that we will echo */
133        send_iac(ndx, IAC_WILL, 1);
134
135        return telnet_ptys[ndx].devname;
136      };
137    };
138  }
139  close(socket);
140  return NULL;
141}
142
143
144/*-----------------------------------------------------------*/
145/*
146 * The NVT terminal is negociated in PollRead and PollWrite
147 * with every BYTE sendded or received.
148 * A litle status machine in the pty_read_byte(int minor)
149 *
150 */
151static const char IAC_AYT_RSP[]="\r\nAYT? Yes, RTEMS-SHELL is here\r\n";
152static const char IAC_BRK_RSP[]="<*Break*>";
153static const char IAC_IP_RSP []="<*Interrupt*>";
154
155static int
156handleSB(pty_t *pty)
157{
158  switch (pty->sb_buf[0]) {
159    case 31:  /* NAWS */
160      pty->width  = (pty->sb_buf[1]<<8) + pty->sb_buf[2];
161      pty->height = (pty->sb_buf[3]<<8) + pty->sb_buf[4];
162#if DEBUG & DEBUG_WH
163      fprintf(stderr,
164          "Setting width/height to %ix%i\n",
165          pty->width,
166          pty->height);
167#endif
168      break;
169    default:
170      break;
171  }
172  return 0;
173}
174
175static int read_pty(int minor)
176{ /* Characters written to the client side*/
177   unsigned char  value;
178   unsigned int  omod;
179   int      count;
180   int      result;
181   pty_t      *pty=telnet_ptys+minor;
182
183   count=read(pty->socket,&value,sizeof(value));
184   if (count<0)
185    return -1;
186
187   if (count<1) {
188      /* Unfortunately, there is no way of passing an EOF
189       * condition through the termios driver. Hence, we
190       * resort to an ugly hack. Setting cindex>ccount
191       * causes the termios driver to return a read count
192       * of '0' which is what we want here. We leave
193       * 'errno' untouched.
194       */
195      pty->ttyp->cindex=pty->ttyp->ccount+1;
196      return pty->ttyp->termios.c_cc[VEOF];
197   };
198
199   omod=pty->iac_mode;
200   pty->iac_mode=0;
201   switch(omod & 0xff) {
202       case IAC_ESC:
203           switch(value) {
204               case IAC_ESC :
205                   /* in case this is an ESC ESC sequence in SB mode */
206                   pty->iac_mode = omod>>8;
207                   return IAC_ESC;
208               case IAC_DONT:
209               case IAC_DO  :
210               case IAC_WONT:
211               case IAC_WILL:
212                   pty->iac_mode=value;
213                   return -1;
214               case IAC_SB  :
215#if DEBUG & DEBUG_DETAIL
216                   printk("SB\n");
217#endif
218                   pty->iac_mode=value;
219                   pty->sb_ind=0;
220                   return -100;
221               case IAC_GA  :
222                   return -1;
223               case IAC_EL  :
224                   return 0x03; /* Ctrl-C*/
225               case IAC_EC  :
226                   return '\b';
227               case IAC_AYT :
228                   write(pty->socket,IAC_AYT_RSP,strlen(IAC_AYT_RSP));
229                   return -1;
230               case IAC_AO  :
231                   return -1;
232               case IAC_IP  :
233                   write(pty->socket,IAC_IP_RSP,strlen(IAC_IP_RSP));
234                   return -1;
235               case IAC_BRK :
236                   write(pty->socket,IAC_BRK_RSP,strlen(IAC_BRK_RSP));
237                   return -1;
238               case IAC_DMARK:
239                   return -2;
240               case IAC_NOP :
241                   return -1;
242               case IAC_SE  :
243#if DEBUG & DEBUG_DETAIL
244                  {
245                  int i;
246                  printk("SE");
247                  for (i=0; i<pty->sb_ind; i++)
248                    printk(" %02x",pty->sb_buf[i]);
249                  printk("\n");
250                  }
251#endif
252                  handleSB(pty);
253               return -101;
254               case IAC_EOR :
255                   return -102;
256               default      :
257                   return -1;
258           };
259           break;
260
261       case IAC_SB:
262           pty->iac_mode=omod;
263           if (IAC_ESC==value) {
264             pty->iac_mode=(omod<<8)|value;
265           } else {
266             if (pty->sb_ind < SB_MAX)
267               pty->sb_buf[pty->sb_ind++]=value;
268           }
269           return -1;
270
271       case IAC_WILL:
272           if (value==34){
273              send_iac(minor,IAC_DONT,   34);  /*LINEMODE*/
274              send_iac(minor,IAC_DO  ,    1);  /*ECHO    */
275           } else if (value==31) {
276              send_iac(minor,IAC_DO  ,   31);  /*NAWS    */
277#if DEBUG & DEBUG_DETAIL
278              printk("replied DO NAWS\n");
279#endif
280           } else {
281              send_iac(minor,IAC_DONT,value);
282           }
283           return -1;
284       case IAC_DONT:
285           return -1;
286       case IAC_DO  :
287           if (value==3) {
288              send_iac(minor,IAC_WILL,    3);  /* GO AHEAD*/
289           } else  if (value==1) {
290              send_iac(minor,IAC_WILL,    1);  /* ECHO */
291           } else {
292              send_iac(minor,IAC_WONT,value);
293           };
294           return -1;
295       case IAC_WONT:
296           if (value==1) {
297             send_iac(minor,IAC_WILL,    1);
298           } else { /* ECHO */
299             send_iac(minor,IAC_WONT,value);
300           }
301           return -1;
302       default:
303           if (value==IAC_ESC) {
304              pty->iac_mode=value;
305              return -1;
306           } else {
307              result=value;
308              if ( 0
309#if 0               /* pass CRLF through - they should use termios to handle it */
310                 ||  ((value=='\n') && (pty->last_cr))
311#endif
312                /* but map telnet CRNUL to CR down here */
313                 || ((value==0) && pty->last_cr)
314                ) result=-1;
315               pty->last_cr=(value=='\r');
316               return result;
317           };
318   };
319  /* should never get here but keep compiler happy */
320  return -1;
321}
322
323/*-----------------------------------------------------------*/
324static int ptySetAttributes(int minor,const struct termios *t);
325static int ptyPollInitialize(int major,int minor,void * arg) ;
326static int ptyShutdown(int major,int minor,void * arg) ;
327static ssize_t ptyPollWrite(int minor, const char * buf, size_t len) ;
328static int ptyPollRead(int minor) ;
329static const rtems_termios_callbacks * pty_get_termios_handlers(int polled) ;
330/*-----------------------------------------------------------*/
331/* Set the 'Hardware'                                        */
332/*-----------------------------------------------------------*/
333static int
334ptySetAttributes(int minor,const struct termios *t) {
335  if (minor<rtems_telnetd_maximum_ptys) {
336   telnet_ptys[minor].c_cflag=t->c_cflag;
337  } else {
338   return -1;
339  };
340  return 0;
341}
342/*-----------------------------------------------------------*/
343static int
344ptyPollInitialize(int major,int minor,void * arg) {
345  rtems_libio_open_close_args_t * args = (rtems_libio_open_close_args_t*)arg;
346  struct termios t;
347        if (minor<rtems_telnetd_maximum_ptys) {
348         if (telnet_ptys[minor].socket<0) return -1;
349   telnet_ptys[minor].opened=TRUE;
350   telnet_ptys[minor].ttyp= (struct rtems_termios_tty *) args->iop->data1;
351   telnet_ptys[minor].iac_mode=0;
352   telnet_ptys[minor].sb_ind=0;
353   telnet_ptys[minor].width=0;
354   telnet_ptys[minor].height=0;
355   t.c_cflag=B9600|CS8;/* termios default */
356   return ptySetAttributes(minor,&t);
357  } else {
358   return -1;
359  }
360}
361/*-----------------------------------------------------------*/
362static int
363ptyShutdown(int major,int minor,void * arg) {
364  if (minor<rtems_telnetd_maximum_ptys) {
365    telnet_ptys[minor].opened=FALSE;
366    if (telnet_ptys[minor].socket>=0) close(telnet_ptys[minor].socket);
367      telnet_ptys[minor].socket=-1;
368    chown(telnet_ptys[minor].devname,2,0);
369  } else {
370    return -1;
371  }
372  return 0;
373}
374/*-----------------------------------------------------------*/
375/* Write Characters into pty device                          */
376/*-----------------------------------------------------------*/
377static ssize_t
378ptyPollWrite(int minor, const char * buf, size_t len) {
379  size_t count;
380  if (minor<rtems_telnetd_maximum_ptys) {
381    if (telnet_ptys[minor].socket<0)
382      return -1;
383    count=write(telnet_ptys[minor].socket,buf,len);
384  } else {
385   count=-1;
386  }
387  return count;
388}
389/*-----------------------------------------------------------*/
390static int
391ptyPollRead(int minor) {
392  int result;
393
394  if (minor<rtems_telnetd_maximum_ptys) {
395    if (telnet_ptys[minor].socket<0)
396      return -1;
397    result=read_pty(minor);
398    return result;
399  }
400  return -1;
401}
402/*-----------------------------------------------------------*/
403/*  pty_initialize
404 *
405 *  This routine initializes the pty IO driver.
406 *
407 *  Input parameters: NONE
408 *
409 *  Output parameters:  NONE
410 *
411 *  Return values:
412 */
413/*-----------------------------------------------------------*/
414static
415rtems_device_driver my_pty_initialize(
416  rtems_device_major_number  major,
417  rtems_device_minor_number  minor,
418  void                      *arg
419)
420{
421  int ndx;
422  rtems_status_code status;
423
424  if ( rtems_telnetd_maximum_ptys < 5 )
425    rtems_telnetd_maximum_ptys = 5;
426
427  telnet_ptys = malloc( rtems_telnetd_maximum_ptys * sizeof (pty_t) );
428
429  /*
430   * Set up ptys
431   */
432
433  for (ndx=0;ndx<rtems_telnetd_maximum_ptys;ndx++) {
434    telnet_ptys[ndx].devname = (char*)malloc(strlen("/dev/ptyXX")+1);
435    sprintf(telnet_ptys[ndx].devname,"/dev/pty%X",ndx);
436    telnet_ptys[ndx].ttyp    =  NULL;
437    telnet_ptys[ndx].c_cflag = CS8|B9600;
438    telnet_ptys[ndx].socket  = -1;
439    telnet_ptys[ndx].opened  = FALSE;
440    telnet_ptys[ndx].sb_ind  = 0;
441    telnet_ptys[ndx].width   = 0;
442    telnet_ptys[ndx].height  = 0;
443
444  }
445
446  /*
447   * Register the devices
448   */
449  for (ndx=0;ndx<rtems_telnetd_maximum_ptys;ndx++) {
450    status = rtems_io_register_name(telnet_ptys[ndx].devname, major, ndx);
451    if (status != RTEMS_SUCCESSFUL)
452        rtems_fatal_error_occurred(status);
453    chmod(telnet_ptys[ndx].devname,0660);
454    chown(telnet_ptys[ndx].devname,2,0); /* tty,root*/
455  };
456  printk("Device: /dev/pty%X../dev/pty%X (%d)pseudo-terminals registered.\n",
457          0,rtems_telnetd_maximum_ptys-1,rtems_telnetd_maximum_ptys);
458
459  return RTEMS_SUCCESSFUL;
460}
461
462static int pty_do_finalize(void)
463{
464    int ndx;
465    rtems_status_code status;
466
467    if ( !telnet_pty_inited )
468      return 0;
469
470    for (ndx=0;ndx<rtems_telnetd_maximum_ptys;ndx++) {
471      if (telnet_ptys[ndx].opened) {
472          fprintf(stderr,
473            "There are still opened PTY devices, unable to proceed\n");
474          return -1;
475      }
476    }
477    if (RTEMS_SUCCESSFUL != rtems_io_unregister_driver(pty_major)) {
478        fprintf(stderr,"Unable to remove this driver\n");
479        return -1;
480    }
481    for (ndx=0;ndx<rtems_telnetd_maximum_ptys;ndx++) {
482        /* rtems_io_register_name() actually creates a node in the filesystem
483         * (mknod())
484         */
485        status = (rtems_status_code)unlink(telnet_ptys[ndx].devname);
486        if (status != RTEMS_SUCCESSFUL)
487          perror("removing pty device node from file system");
488        else
489          free(telnet_ptys[ndx].devname);
490    };
491
492    free ( telnet_ptys );
493
494    fprintf(stderr,"PTY driver unloaded successfully\n");
495    telnet_pty_inited=FALSE;
496    return 0;
497}
498
499/*
500 *  Open entry point
501 */
502
503static
504rtems_device_driver my_pty_open(
505  rtems_device_major_number major,
506  rtems_device_minor_number minor,
507  void                    * arg
508)
509{
510  rtems_status_code sc;
511  sc = rtems_termios_open(major,minor,arg,pty_get_termios_handlers(FALSE));
512  return sc;
513}
514
515/*
516 *  Close entry point
517 */
518
519static
520rtems_device_driver my_pty_close(
521  rtems_device_major_number major,
522  rtems_device_minor_number minor,
523  void                    * arg
524)
525{
526  return rtems_termios_close(arg);
527}
528
529/*
530 * read bytes from the pty
531 */
532
533static
534rtems_device_driver my_pty_read(
535  rtems_device_major_number major,
536  rtems_device_minor_number minor,
537  void                    * arg
538)
539{
540  return rtems_termios_read(arg);
541}
542
543/*
544 * write bytes to the pty
545 */
546
547static
548rtems_device_driver my_pty_write(
549  rtems_device_major_number major,
550  rtems_device_minor_number minor,
551  void                    * arg
552)
553{
554  return rtems_termios_write(arg);
555}
556
557/*
558 *  IO Control entry point
559 */
560
561static
562rtems_device_driver my_pty_control(
563  rtems_device_major_number major,
564  rtems_device_minor_number minor,
565  void                    * arg
566)
567{
568  rtems_libio_ioctl_args_t *args = (rtems_libio_ioctl_args_t*)arg;
569  struct winsize           *wp = (struct winsize*)args->buffer;
570  pty_t                    *p = &telnet_ptys[minor];
571
572  switch (args->command) {
573
574    case TIOCGWINSZ:
575
576      wp->ws_row = p->height;
577      wp->ws_col = p->width;
578      args->ioctl_return=0;
579#if DEBUG & DEBUG_WH
580      fprintf(stderr,
581          "ioctl(TIOCGWINSZ), returning %ix%i\n",
582          wp->ws_col,
583          wp->ws_row);
584#endif
585
586      return RTEMS_SUCCESSFUL;
587
588    case TIOCSWINSZ:
589#if DEBUG & DEBUG_WH
590      fprintf(stderr,
591          "ioctl(TIOCGWINSZ), setting %ix%i\n",
592          wp->ws_col,
593          wp->ws_row);
594#endif
595
596      p->height = wp->ws_row;
597      p->width  = wp->ws_col;
598      args->ioctl_return=0;
599
600      return RTEMS_SUCCESSFUL;
601
602    default:
603
604     break;
605  }
606
607  return rtems_termios_ioctl(arg);
608}
609
610static rtems_driver_address_table drvPty = {
611    my_pty_initialize,
612    my_pty_open,
613    my_pty_close,
614    my_pty_read,
615    my_pty_write,
616    my_pty_control
617};
618
619/*-----------------------------------------------------------*/
620static const rtems_termios_callbacks pty_poll_callbacks = {
621  ptyPollInitialize,  /* FirstOpen */
622  ptyShutdown,        /* LastClose */
623  ptyPollRead,        /* PollRead  */
624  ptyPollWrite,       /* Write */
625  ptySetAttributes,   /* setAttributes */
626  NULL,               /* stopRemoteTX */
627  NULL,               /* StartRemoteTX */
628  0                   /* outputUsesInterrupts */
629};
630/*-----------------------------------------------------------*/
631static const rtems_termios_callbacks * pty_get_termios_handlers(int polled) {
632  return &pty_poll_callbacks;
633}
634/*-----------------------------------------------------------*/
635
636static int pty_do_initialize(void)
637{
638  if ( !telnet_pty_inited ) {
639    if (RTEMS_SUCCESSFUL==rtems_io_register_driver(0, &drvPty, &pty_major))
640      telnet_pty_inited=TRUE;
641    else
642      fprintf(stderr,"WARNING: registering the PTY driver FAILED\n");
643  }
644  return telnet_pty_inited;
645}
646
647int telnet_pty_initialize(void)
648{
649  return pty_do_initialize();
650}
651
652int telnet_pty_finalize(void)
653{
654  return pty_do_finalize();
655}
Note: See TracBrowser for help on using the repository browser.