fd.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. struct chan_ops fd_ops = {
  70. .type = "fd",
  71. .init = fd_init,
  72. .open = fd_open,
  73. .close = fd_close,
  74. .read = generic_read,
  75. .write = generic_write,
  76. .console_write = generic_console_write,
  77. .window_size = generic_window_size,
  78. .free = generic_free,
  79. .winch = 1,
  80. };
  81. /*
  82. * Overrides for Emacs so that we follow Linus's tabbing style.
  83. * Emacs will notice this stuff at the end of the file and automatically
  84. * adjust the settings for this buffer only. This must remain at the end
  85. * of the file.
  86. * ---------------------------------------------------------------------------
  87. * Local variables:
  88. * c-file-style: "linux"
  89. * End:
  90. */