hello-5.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * hello-5.c - Demonstrates command line argument passing to a module.
  3. */
  4. #include <linux/init.h>
  5. #include <linux/kernel.h>
  6. #include <linux/module.h>
  7. #include <linux/moduleparam.h>
  8. #include <linux/stat.h>
  9. MODULE_LICENSE("GPL");
  10. MODULE_AUTHOR("Peter Jay Salzman");
  11. static short int myshort = 1;
  12. static int myint = 420;
  13. static long int mylong = 9999;
  14. static char *mystring = "blah";
  15. static int myintArray[2] = {-1, -1};
  16. static int arr_argc = 0;
  17. /*
  18. * module_param(foo, int, 0000)
  19. * The first param is the parameters name
  20. * The second param is it's data type
  21. * The final argument is the permissions bits,
  22. * for exposing parameters in sysfs (if non-zero) at a later stage.
  23. */
  24. module_param(myshort, short, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
  25. MODULE_PARM_DESC(myshort, "A short integer");
  26. module_param(myint, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  27. MODULE_PARM_DESC(myint, "An integer");
  28. module_param(mylong, long, S_IRUSR);
  29. MODULE_PARM_DESC(mylong, "A long integer");
  30. module_param(mystring, charp, 0000);
  31. MODULE_PARM_DESC(mystring, "A character string");
  32. /*
  33. * module_param_array(name, type, num, perm);
  34. * The first param is the parameter's (in this case the array's) name
  35. * The second param is the data type of the elements of the array
  36. * The third argument is a pointer to the variable that will store the number
  37. * of elements of the array initialized by the user at module loading time
  38. * The fourth argument is the permission bits
  39. */
  40. module_param_array(myintArray, int, &arr_argc, 0000);
  41. MODULE_PARM_DESC(myintArray, "An array of integers");
  42. static int __init hello_5_init(void)
  43. {
  44. int i;
  45. pr_info("Hello, world 5\n=============\n");
  46. pr_info("myshort is a short integer: %hd\n", myshort);
  47. pr_info("myint is an integer: %d\n", myint);
  48. pr_info("mylong is a long integer: %ld\n", mylong);
  49. pr_info("mystring is a string: %s\n", mystring);
  50. for (i = 0; i < (sizeof myintArray / sizeof(int)); i++)
  51. pr_info("myintArray[%d] = %d\n", i, myintArray[i]);
  52. pr_info("got %d arguments for myintArray.\n", arr_argc);
  53. return 0;
  54. }
  55. static void __exit hello_5_exit(void)
  56. {
  57. pr_info("Goodbye, world 5\n");
  58. }
  59. module_init(hello_5_init);
  60. module_exit(hello_5_exit);