echo.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. *
  3. * Copyright (c) 2003 The Regents of the University of California. All
  4. * rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. *
  13. * - Neither the name of the University nor the names of its
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
  18. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  19. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  20. * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
  21. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  22. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  23. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  25. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. */
  30. /*
  31. * FUSD - The Framework for UserSpace Devices - Example program
  32. *
  33. * Jeremy Elson <jelson@circlemud.org>
  34. *
  35. * echo.c: Example of how to use both the 'read' and 'write' callbacks.
  36. *
  37. * This example creates a single device, /dev/echo. If you write
  38. * something to /dev/echo (e.g., "echo HI THERE > /dev/echo"), it gets
  39. * stored. Then, when you read (e.g. "cat /dev/echo"), you get back
  40. * whatever you wrote most recently.
  41. *
  42. */
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #include <errno.h>
  47. #include <unistd.h>
  48. #include "fusd.h"
  49. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  50. /* EXAMPLE START echo.c */
  51. char *data = NULL;
  52. int data_length = 0;
  53. ssize_t echo_read(struct fusd_file_info *file, char *user_buffer,
  54. size_t user_length, loff_t *offset)
  55. {
  56. /* if the user has read past the end of the data, return EOF */
  57. if (*offset >= data_length)
  58. return 0;
  59. /* only return as much data as we have */
  60. user_length = MIN(user_length, data_length - *offset);
  61. /* copy data to user starting from the first byte they haven't seen */
  62. memcpy(user_buffer, data + *offset, user_length);
  63. *offset += user_length;
  64. /* tell them how much data they got */
  65. return user_length;
  66. }
  67. ssize_t echo_write(struct fusd_file_info *file, const char *user_buffer,
  68. size_t user_length, loff_t *offset)
  69. {
  70. /* free the old data, if any */
  71. if (data != NULL) {
  72. free(data);
  73. data = NULL;
  74. data_length = 0;
  75. }
  76. /* allocate space for new data; return error if that fails */
  77. if ((data = malloc(user_length)) == NULL)
  78. return -ENOMEM;
  79. /* make a copy of user's data; tell the user we copied everything */
  80. memcpy(data, user_buffer, user_length);
  81. data_length = user_length;
  82. return user_length;
  83. }
  84. /* EXAMPLE STOP */
  85. int do_open_or_close(struct fusd_file_info *file)
  86. {
  87. return 0; /* opens and closes always succeed */
  88. }
  89. struct fusd_file_operations echo_fops = {
  90. open: do_open_or_close,
  91. read: echo_read,
  92. write: echo_write,
  93. close: do_open_or_close
  94. };
  95. int main(int argc, char *argv[])
  96. {
  97. if (fusd_register("/dev/echo", "misc", "echo", 0666, NULL, &echo_fops) < 0) {
  98. perror("register of /dev/echo failed");
  99. exit(1);
  100. }
  101. fprintf(stderr, "calling fusd_run...\n");
  102. fusd_run();
  103. return 0;
  104. }