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 * ioctl.c: Shows both the client side and server side of FUSD ioctl
38 * servicing.
39 *
40 * There's a lot of extra cruft in this example program (compared to
41 * the other examples, anyway), because this program is both an
42 * example and part of the regression test suite.
43 *
44 * $Id: ioctl.c,v 1.5 2007-12-08 04:05:01 girod Exp $
45 */
46
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <unistd.h>
50 #include <string.h>
51 #include <ctype.h>
52 #include <errno.h>
53 #include <sys/fcntl.h>
54 #include <sys/ioctl.h>
55 #include <sys/wait.h>
56
57 #include "fusd.h"
58
59 /* EXAMPLE START ioctl.h */
60 /* definition of the structure exchanged between client and server */
61 struct ioctl_data_t {
62 char string1[60];
63 char string2[60];
64 };
65
66 #define IOCTL_APP_TYPE 71 /* arbitrary number unique to this app */
67
68 #define IOCTL_TEST0 _IO(IOCTL_APP_TYPE, 0) /* no argument */ /* SKIPLINE */
69 #define IOCTL_TEST1 _IO(IOCTL_APP_TYPE, 1) /* int argument */ /* SKIPLINE */
70 #define IOCTL_TEST2 _IO(IOCTL_APP_TYPE, 2) /* int argument */
71 #define IOCTL_TEST3 _IOR(IOCTL_APP_TYPE, 3, struct ioctl_data_t)
72 #define IOCTL_TEST4 _IOW(IOCTL_APP_TYPE, 4, struct ioctl_data_t)
73 #define IOCTL_TEST5 _IOWR(IOCTL_APP_TYPE, 5, struct ioctl_data_t)
74 /* EXAMPLE STOP ioctl.h */
75 #define IOCTL_TEST_TERMINATE _IO(IOCTL_APP_TYPE, 6)
76
77 #define TEST1_NUM 12345
78 #define TEST3_STRING1 "This is test3 - string1"
79 #define TEST3_STRING2 "This is test3 - string2"
80 #define TEST4_STRING1 "This is test 4's string1"
81 #define TEST4_STRING2 "This is test 4's string2"
82 #define TEST5_STRING1_IN "If you're happy and you know it"
83 #define TEST5_STRING2_IN "clap your hands!"
84 #define TEST5_STRING1_OUT "IF YOU'RE HAPPY AND YOU KNOW IT"
85 #define TEST5_STRING2_OUT "CLAP YOUR HANDS!"
86
87
88 #define CHECK(condition) do { \
89 if (!(condition)) { \
90 printf("%s: TEST FAILED\n", __STRING(condition)); \
91 errors++; \
92 } \
93 } while(0)
94
95
96 int zeroreturn(struct fusd_file_info *file) { return 0; }
97
98 /* EXAMPLE START ioctl-server.c */
99 /* This function is run by the driver */
100 int do_ioctl(struct fusd_file_info *file, int cmd, void *arg)
101 {
102 static int errors = 0; /* SKIPLINE */
103 char *c; /* SKIPLINE */
104 struct ioctl_data_t *d;
105
106 if (_IOC_TYPE(cmd) != IOCTL_APP_TYPE)
107 return 0;
108
109 switch (cmd) {
110 /* EXAMPLE STOP ioctl-server.c */
111 case IOCTL_TEST0:
112 printf("ioctl server: got test0, returning 0\n");
113 return 0;
114 break;
115
116 case IOCTL_TEST1:
117 case IOCTL_TEST2:
118 printf("ioctl server: got test1/2, arg=%d, returning it\n", (int)(size_t) arg);
119 return (int)(size_t) arg;
120 break;
121
122 /* EXAMPLE START ioctl-server.c */
123 case IOCTL_TEST3: /* returns data to the client */
124 d = arg;
125 printf("ioctl server: got test3 request (read-only)\n");/* SKIPLINE */
126 printf("ioctl server: ...returning test strings for client to read\n"); /* SKIPLINE */
127 strcpy(d->string1, TEST3_STRING1);
128 strcpy(d->string2, TEST3_STRING2);
129 return 0;
130 break;
131
132 case IOCTL_TEST4: /* gets data from the client */
133 d = arg;
134 printf("ioctl server: got test4 request (write-only)\n"); /* SKIPLINE */
135 printf("ioctl server: ...got the following strings written by client:\n"); /* SKIPLINE */
136 printf("ioctl server: test4, string1: got '%s'\n", d->string1);
137 printf("ioctl server: test4, string2: got '%s'\n", d->string2);
138 CHECK(!strcmp(d->string1, TEST4_STRING1));/* SKIPLINE */
139 CHECK(!strcmp(d->string2, TEST4_STRING2)); /* SKIPLINE */
140 return 0;
141 break;
142 /* EXAMPLE STOP ioctl-server.c */
143
144 case IOCTL_TEST5:
145 d = arg;
146 printf("ioctl server: got test5 request (read+write)\n");
147 printf("ioctl server: test5, string1: got '%s'\n", d->string1);
148 printf("ioctl server: test5, string2: got '%s'\n", d->string2);
149 printf("ioctl server: capitalizing the strings and returning them\n");
150 for (c = d->string1; *c; c++)
151 *c = toupper(*c);
152 for (c = d->string2; *c; c++)
153 *c = toupper(*c);
154 return 0;
155 break;
156
157 case IOCTL_TEST_TERMINATE:
158 printf("ioctl server: got request to terminate, calling exit(%d)\n",
159 errors);
160 printf("ioctl server: note: client should see -EPIPE\n");
161 exit(errors);
162 break;
163
164 /* EXAMPLE START ioctl-server.c */
165 default:
166 printf("ioctl server: got unknown cmd, sigh, this is broken\n");
167 return -EINVAL;
168 break;
169 }
170
171 return 0;
172 }
173 /* EXAMPLE STOP ioctl-server.c */
174
175 int main(int argc, char *argv[])
176 {
177 pid_t server_pid, retpid;
178
179 if ((server_pid = fork()) < 0) {
180 perror("error creating server");
181 exit(1);
182 }
183
184 if (server_pid == 0) {
185 /* ioctl server */
186 struct fusd_file_operations f = { open: zeroreturn, close: zeroreturn,
187 ioctl: do_ioctl};
188 if (fusd_register("ioctltest", 0666, NULL, &f) < 0)
189 perror("registering ioctltest");
190 printf("server starting\n");
191 fusd_run();
192 } else {
193 /* ioctl client */
194 /* EXAMPLE START ioctl-client.c */
195 int fd, ret;
196 struct ioctl_data_t d;
197 /* EXAMPLE STOP ioctl-client.c */
198 int errors, status;
199
200 errors = 0;
201
202 sleep(1);
203 /* EXAMPLE START ioctl-client.c */
204
205 if ((fd = open("/dev/ioctltest", O_RDWR)) < 0) {
206 perror("client: can't open ioctltest");
207 exit(1);
208 }
209
210 /* EXAMPLE STOP ioctl-client.c */
211 errors = 0;
212
213 /* test0: simply issue a command and get a retval */
214 ret = ioctl(fd, IOCTL_TEST0);
215 printf("ioctl test0: got %d (expecting 0)\n\n", ret);
216 CHECK(ret == 0);
217
218 /* test1: issue a command with a simple (integer) argument */
219 ret = ioctl(fd, IOCTL_TEST1, TEST1_NUM);
220 CHECK(ret == TEST1_NUM);
221 CHECK(errno == 0);
222 printf("ioctl test1: got %d, errno=%d (expecting %d, errno=0)\n\n",
223 ret, errno, TEST1_NUM);
224
225 /* test2 again: make sure errno is set properly */
226 ret = ioctl(fd, IOCTL_TEST2, -ELIBBAD);
227 CHECK(errno == ELIBBAD);
228 CHECK(ret == -1);
229 printf("ioctl test2: got %d, errno=%d (expecting -1, errno=%d)\n\n",
230 ret, errno, ELIBBAD);
231
232 printf("ioctl test3: expecting retval 0, string This Is Test3\n");
233 /* EXAMPLE START ioctl-client.c */
234 /* test3: make sure we can get data FROM a driver using ioctl */
235 ret = ioctl(fd, IOCTL_TEST3, &d);
236 CHECK(ret == 0); /* SKIPLINE */
237 CHECK(!strcmp(d.string1, TEST3_STRING1)); /* SKIPLINE */
238 CHECK(!strcmp(d.string2, TEST3_STRING2)); /* SKIPLINE */
239 printf("ioctl test3: got retval=%d\n", ret);
240 printf("ioctl test3: got string1='%s'\n", d.string1);
241 printf("ioctl test3: got string2='%s'\n", d.string2);
242 printf("\n"); /* SKIPLINE */
243
244 /* test4: make sure we can send data TO a driver using an ioctl */
245 printf("ioctl test4: server should see string 'This Is Test4'\n");/* SKIPLINE */
246 sprintf(d.string1, TEST4_STRING1);
247 sprintf(d.string2, TEST4_STRING2);
248 ret = ioctl(fd, IOCTL_TEST4, &d);
249 /* EXAMPLE STOP ioctl-client.c */
250 CHECK(ret == 0);
251 printf("\n");
252
253 /* test5: we send 2 strings to the ioctl server, they should come
254 * back in all caps */
255 printf("ioctl test5: we send strings that should come back capitalized\n");
256 sprintf(d.string1, TEST5_STRING1_IN);
257 sprintf(d.string2, TEST5_STRING2_IN);
258 printf("ioctl test5: sending string1='%s'\n", d.string1);
259 printf("ioctl test5: sending string2='%s'\n", d.string2);
260 ret = ioctl(fd, IOCTL_TEST5, &d);
261 CHECK(ret == 0);
262 CHECK(!strcmp(d.string1, TEST5_STRING1_OUT));
263 CHECK(!strcmp(d.string2, TEST5_STRING2_OUT));
264 printf("ioctl test5: got retval=%d\n", ret);
265 printf("ioctl test5: got back string1='%s'\n", d.string1);
266 printf("ioctl test5: got back string2='%s'\n", d.string2);
267 printf("\n");
268
269 /* now tell the server to terminate, we should get EPIPE */
270 ret = ioctl(fd, IOCTL_TEST_TERMINATE);
271 CHECK(errno == EPIPE);
272 CHECK(ret == -1);
273 printf("ioctl termination test: got %d (errno=%d)\n", ret, errno);
274 printf("ioctl termination tets: expecting ret=-1, errno=%d\n\n", EPIPE);
275
276 printf("ioctl client: waiting for server to terminate...\n");
277 retpid = wait(&status);
278 CHECK(retpid == server_pid);
279 CHECK(WEXITSTATUS(status) == 0);
280
281 printf("ioctl test done - %d errors\n", errors);
282 if (errors) {
283 printf("IOCTL REGRESSION TEST FAILED\n");
284 exit(1);
285 } else {
286 printf("all tests passed\n");
287 exit(0);
288 }
289 }
290
291 return 0;
292 }
293
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.