Print this page
Enable pty-based serial console emulation in ioemu.

Signed-off-by: John Levon <john.levon@sun.com>

Split Close
Expand all
Collapse all
          --- old/tools/ioemu/vl.c
          +++ new/tools/ioemu/vl.c
↓ open down ↓ 46 lines elided ↑ open up ↑
  47   47  #include <sys/stat.h>
  48   48  #ifndef __APPLE__
  49   49  #include <libutil.h>
  50   50  #endif
  51   51  #else
  52   52  #ifdef __sun__
  53   53  #include <libdlpi.h>
  54   54  #include <sys/ethernet.h>
  55   55  #include <stropts.h>
  56   56  #include <sys/bufmod.h>
       57 +#include <assert.h>
  57   58  #else
  58   59  #include <linux/if.h>
  59   60  #include <linux/if_tun.h>
  60   61  #include <pty.h>
  61   62  #include <malloc.h>
  62   63  #include <linux/rtc.h>
  63   64  #include <linux/ppdev.h>
  64   65  #endif
  65   66  #endif
  66   67  #endif
↓ open down ↓ 1748 lines elided ↑ open up ↑
1815 1816          return -1;
1816 1817      }
1817 1818  
1818 1819      free(path);
1819 1820      xs_daemon_close(xs);
1820 1821      close(xc_handle);
1821 1822  
1822 1823      return 0;
1823 1824  }
1824 1825  
1825      -#if defined(__linux__)
     1826 +#ifdef __sun__
     1827 +static int openpty(int *amaster, int *aslave, char *name,
     1828 +                   struct termios *termp, struct winsize *winp)
     1829 +{
     1830 +        const char *slave;
     1831 +        int mfd = -1, sfd = -1;
     1832 +
     1833 +        *amaster = *aslave = -1;
     1834 +
     1835 +        mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
     1836 +        if (mfd < 0)
     1837 +                goto err;
     1838 +
     1839 +        if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
     1840 +                goto err;
     1841 +
     1842 +        if ((slave = ptsname(mfd)) == NULL)
     1843 +                goto err;
     1844 +
     1845 +        if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
     1846 +                goto err;
     1847 +
     1848 +        if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
     1849 +            ioctl(sfd, I_PUSH, "ldterm") == -1)
     1850 +                goto err;
     1851 +
     1852 +        if (amaster)
     1853 +                *amaster = mfd;
     1854 +        if (aslave)
     1855 +                *aslave = sfd;
     1856 +        if (winp)
     1857 +                ioctl(sfd, TIOCSWINSZ, winp);
     1858 +
     1859 +        if (termp)
     1860 +                tcsetattr(sfd, TCSANOW, termp);
     1861 +
     1862 +        assert(name == NULL);
     1863 +
     1864 +        return 0;
     1865 +
     1866 +err:
     1867 +        if (sfd != -1)
     1868 +                close(sfd);
     1869 +        close(mfd);
     1870 +        return -1;
     1871 +}
     1872 +
     1873 +void cfmakeraw (struct termios *termios_p)
     1874 +{
     1875 +        termios_p->c_iflag &=
     1876 +                ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
     1877 +        termios_p->c_oflag &= ~OPOST;
     1878 +        termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
     1879 +        termios_p->c_cflag &= ~(CSIZE|PARENB);
     1880 +        termios_p->c_cflag |= CS8;
     1881 +
     1882 +        termios_p->c_cc[VMIN] = 0;
     1883 +        termios_p->c_cc[VTIME] = 0;
     1884 +}
     1885 +
     1886 +#endif /* __sun__ */
     1887 +
     1888 +#if defined(__linux__) || defined(__sun__)
1826 1889  static CharDriverState *qemu_chr_open_pty(void)
1827 1890  {
1828 1891      struct termios tty;
1829 1892      int master_fd, slave_fd;
1830 1893      
1831      -    /* Not satisfying */
1832 1894      if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) < 0) {
1833 1895          return NULL;
1834 1896      }
1835 1897      
1836 1898      /* Set raw attributes on the pty. */
     1899 +    tcgetattr(slave_fd, &tty);
1837 1900      cfmakeraw(&tty);
1838      -    tcsetattr(slave_fd, TCSAFLUSH, &tty);
     1901 +    tcsetattr(slave_fd, TCSANOW, &tty);
1839 1902      
1840 1903      fprintf(stderr, "char device redirected to %s\n", ptsname(master_fd));
1841 1904  
1842 1905      return qemu_chr_open_fd(master_fd, master_fd);
1843 1906  }
     1907 +#else /* defined(__linux__) || defined(__sun__) */
     1908 +static CharDriverState *qemu_chr_open_pty(void)
     1909 +{
     1910 +    return NULL;
     1911 +}
     1912 +#endif /* defined(__linux__) || defined(__sun__) */
1844 1913  
     1914 +#ifdef __linux__
     1915 +
1845 1916  static void tty_serial_init(int fd, int speed, 
1846 1917                              int parity, int data_bits, int stop_bits)
1847 1918  {
1848 1919      struct termios tty;
1849 1920      speed_t spd;
1850 1921  
1851 1922  #if 0
1852 1923      printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n", 
1853 1924             speed, parity, data_bits, stop_bits);
1854 1925  #endif
↓ open down ↓ 180 lines elided ↑ open up ↑
2035 2106      }
2036 2107      chr->opaque = (void *)fd;
2037 2108      chr->chr_write = null_chr_write;
2038 2109      chr->chr_ioctl = pp_ioctl;
2039 2110  
2040 2111      qemu_chr_reset(chr);
2041 2112  
2042 2113      return chr;
2043 2114  }
2044 2115  
2045      -#else
2046      -static CharDriverState *qemu_chr_open_pty(void)
2047      -{
2048      -    return NULL;
2049      -}
2050      -#endif
     2116 +#endif /* __linux__ */
2051 2117  
2052 2118  #endif /* !defined(_WIN32) */
2053 2119  
2054 2120  #ifdef _WIN32
2055 2121  typedef struct {
2056 2122      CharDriverState *chr;
2057 2123      int max_size;
2058 2124      HANDLE hcom, hrecv, hsend;
2059 2125      OVERLAPPED orecv, osend;
2060 2126      BOOL fpipe;
↓ open down ↓ 6301 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX