fd.c 2.1 KB

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