helloworld.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. * hello-world: Simply creates a device called /dev/hello-world, which
  36. * greets you when you try to get it.
  37. *
  38. */
  39. /* EXAMPLE START helloworld.c */
  40. #include <stdio.h>
  41. #include <string.h>
  42. #include <errno.h>
  43. #include "fusd.h"
  44. #define GREETING "Hello, world!\n"
  45. int do_open_or_close(struct fusd_file_info *file)
  46. {
  47. return 0; /* attempts to open and close this file always succeed */
  48. }
  49. ssize_t do_read(struct fusd_file_info *file, char *user_buffer,
  50. size_t user_length, loff_t *offset)
  51. {
  52. int retval = 0;
  53. /* The first read to the device returns a greeting. The second read
  54. * returns EOF. */
  55. if (*offset == 0) {
  56. if (user_length < strlen(GREETING))
  57. retval = -EINVAL; /* the user must supply a big enough buffer! */
  58. else {
  59. memcpy(user_buffer, GREETING, strlen(GREETING)); /* greet user */
  60. retval = strlen(GREETING); /* retval = number of bytes returned */
  61. *offset += retval; /* advance user's file pointer */
  62. }
  63. }
  64. return retval;
  65. }
  66. int main(int argc, char *argv[])
  67. {
  68. struct fusd_file_operations fops = {
  69. open: do_open_or_close,
  70. read: do_read,
  71. close: do_open_or_close };
  72. if (fusd_register("/dev/hello-world", "test", "hello-world", 0666, NULL, &fops) < 0)
  73. perror("Unable to register device");
  74. else {
  75. printf("/dev/hello-world should now exist - calling fusd_run...\n");
  76. fusd_run();
  77. }
  78. return 0;
  79. }
  80. /* EXAMPLE STOP helloworld.c */