hello-5.c 2.1 KB

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