helloworld.c 2.9 KB

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