ioctl.c 9.5 KB

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