ioctl.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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: ioctl.c,v 1.4 2003/07/11 22:29:39 cerpa Exp $
  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=%d, returning it\n", (int) arg);
  103. return (int) arg;
  104. break;
  105. /* EXAMPLE START ioctl-server.c */
  106. case IOCTL_TEST3: /* returns data to the client */
  107. d = arg;
  108. printf("ioctl server: got test3 request (read-only)\n");/* SKIPLINE */
  109. printf("ioctl server: ...returning test strings for client to read\n"); /* SKIPLINE */
  110. strcpy(d->string1, TEST3_STRING1);
  111. strcpy(d->string2, TEST3_STRING2);
  112. return 0;
  113. break;
  114. case IOCTL_TEST4: /* gets data from the client */
  115. d = arg;
  116. printf("ioctl server: got test4 request (write-only)\n"); /* SKIPLINE */
  117. printf("ioctl server: ...got the following strings written by client:\n"); /* SKIPLINE */
  118. printf("ioctl server: test4, string1: got '%s'\n", d->string1);
  119. printf("ioctl server: test4, string2: got '%s'\n", d->string2);
  120. CHECK(!strcmp(d->string1, TEST4_STRING1));/* SKIPLINE */
  121. CHECK(!strcmp(d->string2, TEST4_STRING2)); /* SKIPLINE */
  122. return 0;
  123. break;
  124. /* EXAMPLE STOP ioctl-server.c */
  125. case IOCTL_TEST5:
  126. d = arg;
  127. printf("ioctl server: got test5 request (read+write)\n");
  128. printf("ioctl server: test5, string1: got '%s'\n", d->string1);
  129. printf("ioctl server: test5, string2: got '%s'\n", d->string2);
  130. printf("ioctl server: capitalizing the strings and returning them\n");
  131. for (c = d->string1; *c; c++)
  132. *c = toupper(*c);
  133. for (c = d->string2; *c; c++)
  134. *c = toupper(*c);
  135. return 0;
  136. break;
  137. case IOCTL_TEST_TERMINATE:
  138. printf("ioctl server: got request to terminate, calling exit(%d)\n",
  139. errors);
  140. printf("ioctl server: note: client should see -EPIPE\n");
  141. exit(errors);
  142. break;
  143. /* EXAMPLE START ioctl-server.c */
  144. default:
  145. printf("ioctl server: got unknown cmd, sigh, this is broken\n");
  146. return -EINVAL;
  147. break;
  148. }
  149. return 0;
  150. }
  151. /* EXAMPLE STOP ioctl-server.c */
  152. int main(int argc, char *argv[])
  153. {
  154. pid_t server_pid, retpid;
  155. if ((server_pid = fork()) < 0) {
  156. perror("error creating server");
  157. exit(1);
  158. }
  159. if (server_pid == 0) {
  160. /* ioctl server */
  161. struct fusd_file_operations f = { open: zeroreturn, close: zeroreturn,
  162. ioctl: do_ioctl};
  163. if (fusd_register("ioctltest", 0666, NULL, &f) < 0)
  164. perror("registering ioctltest");
  165. printf("server starting\n");
  166. fusd_run();
  167. } else {
  168. /* ioctl client */
  169. /* EXAMPLE START ioctl-client.c */
  170. int fd, ret;
  171. struct ioctl_data_t d;
  172. /* EXAMPLE STOP ioctl-client.c */
  173. int errors, status;
  174. errors = 0;
  175. sleep(1);
  176. /* EXAMPLE START ioctl-client.c */
  177. if ((fd = open("/dev/ioctltest", O_RDWR)) < 0) {
  178. perror("client: can't open ioctltest");
  179. exit(1);
  180. }
  181. /* EXAMPLE STOP ioctl-client.c */
  182. errors = 0;
  183. /* test0: simply issue a command and get a retval */
  184. ret = ioctl(fd, IOCTL_TEST0);
  185. printf("ioctl test0: got %d (expecting 0)\n\n", ret);
  186. CHECK(ret == 0);
  187. /* test1: issue a command with a simple (integer) argument */
  188. ret = ioctl(fd, IOCTL_TEST1, TEST1_NUM);
  189. CHECK(ret == TEST1_NUM);
  190. CHECK(errno == 0);
  191. printf("ioctl test1: got %d, errno=%d (expecting %d, errno=0)\n\n",
  192. ret, errno, TEST1_NUM);
  193. /* test2 again: make sure errno is set properly */
  194. ret = ioctl(fd, IOCTL_TEST2, -ELIBBAD);
  195. CHECK(errno == ELIBBAD);
  196. CHECK(ret == -1);
  197. printf("ioctl test2: got %d, errno=%d (expecting -1, errno=%d)\n\n",
  198. ret, errno, ELIBBAD);
  199. printf("ioctl test3: expecting retval 0, string This Is Test3\n");
  200. /* EXAMPLE START ioctl-client.c */
  201. /* test3: make sure we can get data FROM a driver using ioctl */
  202. ret = ioctl(fd, IOCTL_TEST3, &d);
  203. CHECK(ret == 0); /* SKIPLINE */
  204. CHECK(!strcmp(d.string1, TEST3_STRING1)); /* SKIPLINE */
  205. CHECK(!strcmp(d.string2, TEST3_STRING2)); /* SKIPLINE */
  206. printf("ioctl test3: got retval=%d\n", ret);
  207. printf("ioctl test3: got string1='%s'\n", d.string1);
  208. printf("ioctl test3: got string2='%s'\n", d.string2);
  209. printf("\n"); /* SKIPLINE */
  210. /* test4: make sure we can send data TO a driver using an ioctl */
  211. printf("ioctl test4: server should see string 'This Is Test4'\n");/* SKIPLINE */
  212. sprintf(d.string1, TEST4_STRING1);
  213. sprintf(d.string2, TEST4_STRING2);
  214. ret = ioctl(fd, IOCTL_TEST4, &d);
  215. /* EXAMPLE STOP ioctl-client.c */
  216. CHECK(ret == 0);
  217. printf("\n");
  218. /* test5: we send 2 strings to the ioctl server, they should come
  219. * back in all caps */
  220. printf("ioctl test5: we send strings that should come back capitalized\n");
  221. sprintf(d.string1, TEST5_STRING1_IN);
  222. sprintf(d.string2, TEST5_STRING2_IN);
  223. printf("ioctl test5: sending string1='%s'\n", d.string1);
  224. printf("ioctl test5: sending string2='%s'\n", d.string2);
  225. ret = ioctl(fd, IOCTL_TEST5, &d);
  226. CHECK(ret == 0);
  227. CHECK(!strcmp(d.string1, TEST5_STRING1_OUT));
  228. CHECK(!strcmp(d.string2, TEST5_STRING2_OUT));
  229. printf("ioctl test5: got retval=%d\n", ret);
  230. printf("ioctl test5: got back string1='%s'\n", d.string1);
  231. printf("ioctl test5: got back string2='%s'\n", d.string2);
  232. printf("\n");
  233. /* now tell the server to terminate, we should get EPIPE */
  234. ret = ioctl(fd, IOCTL_TEST_TERMINATE);
  235. CHECK(errno == EPIPE);
  236. CHECK(ret == -1);
  237. printf("ioctl termination test: got %d (errno=%d)\n", ret, errno);
  238. printf("ioctl termination tets: expecting ret=-1, errno=%d\n\n", EPIPE);
  239. printf("ioctl client: waiting for server to terminate...\n");
  240. retpid = wait(&status);
  241. CHECK(retpid == server_pid);
  242. CHECK(WEXITSTATUS(status) == 0);
  243. printf("ioctl test done - %d errors\n", errors);
  244. if (errors) {
  245. printf("IOCTL REGRESSION TEST FAILED\n");
  246. exit(1);
  247. } else {
  248. printf("all tests passed\n");
  249. exit(0);
  250. }
  251. }
  252. return 0;
  253. }