fd.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (C) 2001 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <termios.h>
  9. #include <errno.h>
  10. #include "user.h"
  11. #include "user_util.h"
  12. #include "chan_user.h"
  13. struct fd_chan {
  14. int fd;
  15. int raw;
  16. struct termios tt;
  17. char str[sizeof("1234567890\0")];
  18. };
  19. static void *fd_init(char *str, int device, struct chan_opts *opts)
  20. {
  21. struct fd_chan *data;
  22. char *end;
  23. int n;
  24. if(*str != ':'){
  25. printk("fd_init : channel type 'fd' must specify a file "
  26. "descriptor\n");
  27. return(NULL);
  28. }
  29. str++;
  30. n = strtoul(str, &end, 0);
  31. if((*end != '\0') || (end == str)){
  32. printk("fd_init : couldn't parse file descriptor '%s'\n", str);
  33. return(NULL);
  34. }
  35. data = um_kmalloc(sizeof(*data));
  36. if(data == NULL) return(NULL);
  37. *data = ((struct fd_chan) { .fd = n,
  38. .raw = opts->raw });
  39. return(data);
  40. }
  41. static int fd_open(int input, int output, int primary, void *d, char **dev_out)
  42. {
  43. struct fd_chan *data = d;
  44. int err;
  45. if(data->raw && isatty(data->fd)){
  46. CATCH_EINTR(err = tcgetattr(data->fd, &data->tt));
  47. if(err)
  48. return(err);
  49. err = raw(data->fd);
  50. if(err)
  51. return(err);
  52. }
  53. sprintf(data->str, "%d", data->fd);
  54. *dev_out = data->str;
  55. return(data->fd);
  56. }
  57. static void fd_close(int fd, void *d)
  58. {
  59. struct fd_chan *data = d;
  60. int err;
  61. if(data->raw && isatty(fd)){
  62. CATCH_EINTR(err = tcsetattr(fd, TCSAFLUSH, &data->tt));
  63. if(err)
  64. printk("Failed to restore terminal state - "
  65. "errno = %d\n", -err);
  66. data->raw = 0;
  67. }
  68. }
  69. static int fd_console_write(int fd, const char *buf, int n, void *d)
  70. {
  71. struct fd_chan *data = d;
  72. return(generic_console_write(fd, buf, n, &data->tt));
  73. }
  74. struct chan_ops fd_ops = {
  75. .type = "fd",
  76. .init = fd_init,
  77. .open = fd_open,
  78. .close = fd_close,
  79. .read = generic_read,
  80. .write = generic_write,
  81. .console_write = fd_console_write,
  82. .window_size = generic_window_size,
  83. .free = generic_free,
  84. .winch = 1,
  85. };
  86. /*
  87. * Overrides for Emacs so that we follow Linus's tabbing style.
  88. * Emacs will notice this stuff at the end of the file and automatically
  89. * adjust the settings for this buffer only. This must remain at the end
  90. * of the file.
  91. * ---------------------------------------------------------------------------
  92. * Local variables:
  93. * c-file-style: "linux"
  94. * End:
  95. */