Welcome to the Linux Foundation Forum!

programming a module

Posts: 1
edited June 2023 in Kernel Development

Hi,

i am trying experimentally to program a kernel module that checks a file on the system, namely in /etc.

If I compile the module dynamically, the code works. But if I compile it statically it doesn't work, it doesn't find the file in /etc

the code goes something like this:

  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/init.h>
  4. #include <linux/fs.h>
  5. #include <linux/uaccess.h>
  6.  
  7. MODULE_AUTHOR("my name");
  8. MODULE_DESCRIPTION("file check");
  9. MODULE_LICENSE("GPL");
  10.  
  11. static int isRegistered(void) {
  12. struct file *file;
  13. char buffer[16];
  14. int registered = 0;
  15.  
  16. // Apre il file di configurazione
  17. file = filp_open("/etc/testfile", O_RDONLY, 0);
  18. if (IS_ERR(file)) {
  19. // open error
  20. return registered;
  21. }
  22.  
  23. // Reads the contents of the file
  24. if (file->f_op->read(file, buffer, sizeof(buffer), &file->f_pos) > 0) {
  25. if (strncmp(buffer, "registered", 10) == 0) {
  26. registered = 1;
  27. }
  28. }
  29.  
  30. // Closes the file
  31. filp_close(file, NULL);
  32.  
  33. return registered;
  34. }
  35.  
  36. int check_registration(void) {
  37. if (!isRegistered()) {
  38. printk(KERN_ALERT "Not check.\n");
  39. return -1;
  40. }
  41.  
  42. printk(KERN_INFO "check.\n");
  43. return 0;
  44. }
  45. EXPORT_SYMBOL_GPL(check_registration);
  46.  
  47. static int __init registration_check_init(void) {
  48. /* Does not execute control logic here */
  49. return 0;
  50. }
  51.  
  52. static void __exit registration_check_exit(void) {
  53. printk(KERN_INFO "Registration control form terminated.\n");
  54. }
  55.  
  56. module_init(registration_check_init);
  57. module_exit(registration_check_exit);
  58.  
  59. /* Performs control logic */
  60. late_initcall(check_registration);

Answers

  • Posts: 2
    edited August 2023

    What do you mean build statically? cause to my knowledge you either build it as a .ko file (dynamically) for insertion later, or you literally bake it into the kernel (statically) in which you need to add it to the kernel source code tree, add kconfig files, select it and rebuild the entire linux kernel.

  • Well, I think there are some minor issues with your code, you can try below code it may resolve your error.

    include <linux/module.h>

    include <linux/kernel.h>

    include <linux/init.h>

    include <linux/fs.h>

    include <linux/uaccess.h>

    MODULE_AUTHOR("my name");
    MODULE_DESCRIPTION("file check");
    MODULE_LICENSE("GPL");

    static int isRegistered(void) {
    // Your existing code for checking the file
    }

    static void check_registration_callback(void) {
    if (!isRegistered()) {
    printk(KERN_ALERT "Not registered.\n");
    } else {
    printk(KERN_INFO "Registered.\n");
    }
    }

    int check_registration(void) {
    return 0;
    }
    EXPORT_SYMBOL_GPL(check_registration);

    static int __init registration_check_init(void) {
    /* Register your callback here */
    on_each_cpu(check_registration_callback, NULL, 1);
    return 0;
    }

    static void __exit registration_check_exit(void) {
    printk(KERN_INFO "Registration control form terminated.\n");
    }

    module_init(registration_check_init);
    module_exit(registration_check_exit);

    Thanks

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Categories

Upcoming Training