fd.c 2.2 KB

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