{"id":442,"date":"2023-06-14T20:08:13","date_gmt":"2023-06-15T04:08:13","guid":{"rendered":"http:\/\/bitas.net.cn\/?page_id=442"},"modified":"2023-06-14T20:08:34","modified_gmt":"2023-06-15T04:08:34","slug":"645-lk-kset-example","status":"publish","type":"page","link":"http:\/\/bitas.net.cn\/index.php\/sourcecode\/645-lk-kset-example\/","title":{"rendered":"645 (LK) kset \u793a\u4f8b"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>\/*\n * kset and ktype example\n *\/\n#include &lt;linux\/kobject.h>\n#include &lt;linux\/slab.h>\n#include &lt;linux\/module.h>\n#include &lt;linux\/init.h>\n\n\/*\n * This module shows how to create a kset in sysfs called\n * \/sys\/kernel\/ks_a\n *\/\n\n\n\/*\n * This is our \"object\" that we will create a few of and register them with\n * sysfs.\n *\/\nstruct fu_obj {\n    struct kobject kobj;\n    int par;\n};\n#define to_fu_obj(x) container_of(x, struct fu_obj, kobj)\n\n\/*\n * The release function for our object.  This is REQUIRED by the kernel to\n * have.  We free the memory held in our object here.\n *\n * NEVER try to get away with just a \"blank\" release function to try to be\n * smarter than the kernel.  Turns out, no one ever is...\n *\/\nstatic void fu_release(struct kobject *kobj)\n{\n    struct fu_obj *fu;\n\n    fu = to_fu_obj(kobj);\n    kfree(fu);\n}\n\n\/*\n * Our own ktype for our kobjects.  Here we only specify the\n * release function.\n *\/\nstatic struct kobj_type fu_ktype = { \n    .release = fu_release,\n};\n\nstatic struct kset *ks_a;\nstatic struct fu_obj *fu_a1;\nstatic struct fu_obj *fu_a2;\nstatic struct fu_obj *fu_a3;\n\nstatic struct fu_obj *create_fu_obj(const char *name)\n{\n    struct fu_obj *fu;\n    int retval;\n\n    \/* allocate the memory for the whole object *\/\n    fu = kzalloc(sizeof(*fu), GFP_KERNEL);\n    if (!fu)\n        return NULL;\n\n    \/*\n     * As we have a kset for this kobject, we need to set it before calling\n     * the kobject core.\n     *\/\n    fu->kobj.kset = ks_a;\n\n    \/*\n     * Initialize and add the kobject to the kernel.  All the default files\n     * will be created here.  As we have already specified a kset for this\n     * kobject, we don't have to set a parent for the kobject, the kobject\n     * will be placed beneath that kset automatically.\n     *\/\n    retval = kobject_init_and_add(&amp;fu->kobj, &amp;fu_ktype, NULL, \"%s\", name);\n    if (retval) {\n        kobject_put(&amp;fu->kobj);\n        return NULL;\n    }\n\n    return fu;\n}\n\nstatic void destroy_fu_obj(struct fu_obj *fu)\n{\n    kobject_put(&amp;fu->kobj);\n}\n\nstatic int __init example_init(void)\n{\n    \/*\n     * Create a kset with the name of \"ks_a\",\n     * located under \/sys\/kernel\/\n     *\/\n    ks_a = kset_create_and_add(\"ks_a\", NULL, kernel_kobj);\n    if (!ks_a)\n        return -ENOMEM;\n\n    \/*\n     * Create three objects and register them with our kset\n     *\/\n    fu_a1 = create_fu_obj(\"fu_a1\");\n    if (!fu_a1)\n        goto err_fu_a1;\n\n    fu_a2 = create_fu_obj(\"fu_a2\");\n    if (!fu_a2)\n        goto err_fu_a2;\n\n    fu_a3 = create_fu_obj(\"fu_a3\");\n    if (!fu_a3)\n        goto err_fu_a3;\n\n    return 0;\n\nerr_fu_a3:\n    destroy_fu_obj(fu_a2);\nerr_fu_a2:\n    destroy_fu_obj(fu_a1);\nerr_fu_a1:\n    kset_unregister(ks_a);\n    return -EINVAL;\n}\n\nstatic void __exit example_exit(void)\n{\n    destroy_fu_obj(fu_a3);\n    destroy_fu_obj(fu_a2);\n    destroy_fu_obj(fu_a1);\n    kset_unregister(ks_a);\n}\n\nmodule_init(example_init);\nmodule_exit(example_exit);\nMODULE_LICENSE(\"GPL\");<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"parent":127,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-442","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/bitas.net.cn\/index.php\/wp-json\/wp\/v2\/pages\/442","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bitas.net.cn\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/bitas.net.cn\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/bitas.net.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/bitas.net.cn\/index.php\/wp-json\/wp\/v2\/comments?post=442"}],"version-history":[{"count":1,"href":"http:\/\/bitas.net.cn\/index.php\/wp-json\/wp\/v2\/pages\/442\/revisions"}],"predecessor-version":[{"id":443,"href":"http:\/\/bitas.net.cn\/index.php\/wp-json\/wp\/v2\/pages\/442\/revisions\/443"}],"up":[{"embeddable":true,"href":"http:\/\/bitas.net.cn\/index.php\/wp-json\/wp\/v2\/pages\/127"}],"wp:attachment":[{"href":"http:\/\/bitas.net.cn\/index.php\/wp-json\/wp\/v2\/media?parent=442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}