3

In the code for my Linux Kernel module, I create a kobject, which becomes a directory in the /sys/ directory, and then create a sysfs file in it. So it looks like /sys/KobjName/SysfsFile. This is the only file in that kobject directory, and both the sysfs file and the kobject are to be deleted after the rmmod moduleX command is executed.

Currently my clean_up code for moduleX looks like

static void cleanup(void)
{
    /* Remove test file */
    if (file_ok)
        sysfs_remove_file(kobj, &attributes.attr);

    /* Remove /sys/ddttest directory */
    if (kobj)
        kobject_put(kobj);
}

where file_ok is an integer of 0 or 1 representing if the sysfs_create_file(kobj, &attributes.attr) function executed corrected (e.g. a file is created).

My question now is if it is even necessary to remove the sysfs file before removing the kobject. When navigating the file system in the user space, one can simply delete a directory, and that will remove all the files within it. Is that the case with kobjectfile systems in the kernel space?

Sam Protsenko
  • 14,045
  • 4
  • 59
  • 75
Jon
  • 359
  • 1
  • 2
  • 10
  • when calling `rmdir` the utility will complain and not remove the directory, if the directory is not empty. so it might not work. – user3629249 Oct 24 '15 at 18:14
  • rather than using kobject_put() to reduce the usage count, it might be more reliable to use the function: `void kobject_del(struct kobject *kobj);` – user3629249 Oct 24 '15 at 18:15

0 Answers0