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
32 /*
33 * FUSD - The Framework for UserSpace Devices - Example program
34 *
35 * Jeremy Elson <jelson@circlemud.org>
36 *
37 * hello-world: Simply creates a device called /dev/hello-world, which
38 * greets you when you try to get it.
39 *
40 * $Id: helloworld.c,v 1.12 2007-12-08 04:05:01 girod Exp $
41 */
42
43 /* EXAMPLE START helloworld.c */
44 #include <stdio.h>
45 #include <string.h>
46 #include <errno.h>
47 #include "fusd.h"
48
49 #define GREETING "Hello, world!\n"
50
51 int do_open_or_close(struct fusd_file_info *file)
52 {
53 return 0; /* attempts to open and close this file always succeed */
54 }
55
56 ssize_t do_read(struct fusd_file_info *file, char *user_buffer,
57 size_t user_length, loff_t *offset)
58 {
59 int retval = 0;
60
61 /* The first read to the device returns a greeting. The second read
62 * returns EOF. */
63 if (*offset == 0) {
64 if (user_length < strlen(GREETING))
65 retval = -EINVAL; /* the user must supply a big enough buffer! */
66 else {
67 memcpy(user_buffer, GREETING, strlen(GREETING)); /* greet user */
68 retval = strlen(GREETING); /* retval = number of bytes returned */
69 *offset += retval; /* advance user's file pointer */
70 }
71 }
72
73 return retval;
74 }
75
76 int main(int argc, char *argv[])
77 {
78 struct fusd_file_operations fops = {
79 open: do_open_or_close,
80 read: do_read,
81 close: do_open_or_close };
82
83 if (fusd_register("/dev/hello-world", 0666, NULL, &fops) < 0)
84 perror("Unable to register device");
85 else {
86 printf("/dev/hello-world should now exist - calling fusd_run...\n");
87 fusd_run();
88 }
89 return 0;
90 }
91 /* EXAMPLE STOP helloworld.c */
92
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.