fd.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{linux.intel,addtoit}.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 <termios.h>
  10. #include "chan_user.h"
  11. #include "kern_constants.h"
  12. #include "os.h"
  13. #include "um_malloc.h"
  14. #include "user.h"
  15. struct fd_chan {
  16. int fd;
  17. int raw;
  18. struct termios tt;
  19. char str[sizeof("1234567890\0")];
  20. };
  21. static void *fd_init(char *str, int device, const struct chan_opts *opts)
  22. {
  23. struct fd_chan *data;
  24. char *end;
  25. int n;
  26. if (*str != ':') {
  27. printk(UM_KERN_ERR "fd_init : channel type 'fd' must specify a "
  28. "file descriptor\n");
  29. return NULL;
  30. }
  31. str++;
  32. n = strtoul(str, &end, 0);
  33. if ((*end != '\0') || (end == str)) {
  34. printk(UM_KERN_ERR "fd_init : couldn't parse file descriptor "
  35. "'%s'\n", str);
  36. return NULL;
  37. }
  38. data = uml_kmalloc(sizeof(*data), UM_GFP_KERNEL);
  39. if (data == NULL)
  40. return NULL;
  41. *data = ((struct fd_chan) { .fd = n,
  42. .raw = opts->raw });
  43. return data;
  44. }
  45. static int fd_open(int input, int output, int primary, void *d, char **dev_out)
  46. {
  47. struct fd_chan *data = d;
  48. int err;
  49. if (data->raw && isatty(data->fd)) {
  50. CATCH_EINTR(err = tcgetattr(data->fd, &data->tt));
  51. if (err)
  52. return err;
  53. err = raw(data->fd);
  54. if (err)
  55. return err;
  56. }
  57. sprintf(data->str, "%d", data->fd);
  58. *dev_out = data->str;
  59. return data->fd;
  60. }
  61. static void fd_close(int fd, void *d)
  62. {
  63. struct fd_chan *data = d;
  64. int err;
  65. if (!data->raw || !isatty(fd))
  66. return;
  67. CATCH_EINTR(err = tcsetattr(fd, TCSAFLUSH, &data->tt));
  68. if (err)
  69. printk(UM_KERN_ERR "Failed to restore terminal state - "
  70. "errno = %d\n", -err);
  71. data->raw = 0;
  72. }
  73. const struct chan_ops fd_ops = {
  74. .type = "fd",
  75. .init = fd_init,
  76. .open = fd_open,
  77. .close = fd_close,
  78. .read = generic_read,
  79. .write = generic_write,
  80. .console_write = generic_console_write,
  81. .window_size = generic_window_size,
  82. .free = generic_free,
  83. .winch = 1,
  84. };