hello-5.c 2.2 KB

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