pty.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <errno.h>
  9. #include <fcntl.h>
  10. #include <string.h>
  11. #include <termios.h>
  12. #include <sys/stat.h>
  13. #include "chan_user.h"
  14. #include <os.h>
  15. #include <um_malloc.h>
  16. struct pty_chan {
  17. void (*announce)(char *dev_name, int dev);
  18. int dev;
  19. int raw;
  20. struct termios tt;
  21. char dev_name[sizeof("/dev/pts/0123456\0")];
  22. };
  23. static void *pty_chan_init(char *str, int device, const struct chan_opts *opts)
  24. {
  25. struct pty_chan *data;
  26. data = uml_kmalloc(sizeof(*data), UM_GFP_KERNEL);
  27. if (data == NULL)
  28. return NULL;
  29. *data = ((struct pty_chan) { .announce = opts->announce,
  30. .dev = device,
  31. .raw = opts->raw });
  32. return data;
  33. }
  34. static int pts_open(int input, int output, int primary, void *d,
  35. char **dev_out)
  36. {
  37. struct pty_chan *data = d;
  38. char *dev;
  39. int fd, err;
  40. fd = get_pty();
  41. if (fd < 0) {
  42. err = -errno;
  43. printk(UM_KERN_ERR "open_pts : Failed to open pts\n");
  44. return err;
  45. }
  46. if (data->raw) {
  47. CATCH_EINTR(err = tcgetattr(fd, &data->tt));
  48. if (err)
  49. goto out_close;
  50. err = raw(fd);
  51. if (err)
  52. goto out_close;
  53. }
  54. dev = ptsname(fd);
  55. sprintf(data->dev_name, "%s", dev);
  56. *dev_out = data->dev_name;
  57. if (data->announce)
  58. (*data->announce)(dev, data->dev);
  59. return fd;
  60. out_close:
  61. close(fd);
  62. return err;
  63. }
  64. static int getmaster(char *line)
  65. {
  66. struct stat buf;
  67. char *pty, *bank, *cp;
  68. int master, err;
  69. pty = &line[strlen("/dev/ptyp")];
  70. for (bank = "pqrs"; *bank; bank++) {
  71. line[strlen("/dev/pty")] = *bank;
  72. *pty = '0';
  73. /* Did we hit the end ? */
  74. if ((stat(line, &buf) < 0) && (errno == ENOENT))
  75. break;
  76. for (cp = "0123456789abcdef"; *cp; cp++) {
  77. *pty = *cp;
  78. master = open(line, O_RDWR);
  79. if (master >= 0) {
  80. char *tp = &line[strlen("/dev/")];
  81. /* verify slave side is usable */
  82. *tp = 't';
  83. err = access(line, R_OK | W_OK);
  84. *tp = 'p';
  85. if (!err)
  86. return master;
  87. close(master);
  88. }
  89. }
  90. }
  91. printk(UM_KERN_ERR "getmaster - no usable host pty devices\n");
  92. return -ENOENT;
  93. }
  94. static int pty_open(int input, int output, int primary, void *d,
  95. char **dev_out)
  96. {
  97. struct pty_chan *data = d;
  98. int fd, err;
  99. char dev[sizeof("/dev/ptyxx\0")] = "/dev/ptyxx";
  100. fd = getmaster(dev);
  101. if (fd < 0)
  102. return fd;
  103. if (data->raw) {
  104. err = raw(fd);
  105. if (err) {
  106. close(fd);
  107. return err;
  108. }
  109. }
  110. if (data->announce)
  111. (*data->announce)(dev, data->dev);
  112. sprintf(data->dev_name, "%s", dev);
  113. *dev_out = data->dev_name;
  114. return fd;
  115. }
  116. const struct chan_ops pty_ops = {
  117. .type = "pty",
  118. .init = pty_chan_init,
  119. .open = pty_open,
  120. .close = generic_close,
  121. .read = generic_read,
  122. .write = generic_write,
  123. .console_write = generic_console_write,
  124. .window_size = generic_window_size,
  125. .free = generic_free,
  126. .winch = 0,
  127. };
  128. const struct chan_ops pts_ops = {
  129. .type = "pts",
  130. .init = pty_chan_init,
  131. .open = pts_open,
  132. .close = generic_close,
  133. .read = generic_read,
  134. .write = generic_write,
  135. .console_write = generic_console_write,
  136. .window_size = generic_window_size,
  137. .free = generic_free,
  138. .winch = 0,
  139. };