{"id":5084,"date":"2014-03-30T18:43:03","date_gmt":"2014-03-30T18:43:03","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-can-i-use-a-sysfs-kobject-as-a-global-variable-collection-of-common-programming-errors\/"},"modified":"2014-03-30T18:43:03","modified_gmt":"2014-03-30T18:43:03","slug":"how-can-i-use-a-sysfs-kobject-as-a-global-variable-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-can-i-use-a-sysfs-kobject-as-a-global-variable-collection-of-common-programming-errors\/","title":{"rendered":"How can I use a sysfs kobject as a global variable?-Collection of common programming errors"},"content":{"rendered":"<p>I would like to use an user-editable global variable in the linux kernel. Is that possible? That&#8217;s what I came up with using the example provided with the source code:<\/p>\n<p><strong>arch\/x86\/kernel\/foo.c<\/strong><\/p>\n<pre><code>#include \n#include \n#include \n#include \n#include \n\nint foo = 12;\n\nstatic ssize_t foo_show(struct kobject *kobj, struct kobj_attribute *attr,\n            char *buf)\n{\nreturn sprintf(buf, \"%d\\n\", foo);\n}\n\nstatic ssize_t foo_store(struct kobject *kobj, struct kobj_attribute *attr,\n         const char *buf, size_t count)\n{\nsscanf(buf, \"%du\", &amp;foo);\nreturn count;\n}\n\nstatic struct kobj_attribute foo_attribute =\n__ATTR(foo, 0666, foo_show, foo_store);\n\nstatic struct attribute *attrs[] = {\n&amp;foo_attribute.attr,\nNULL,\n};\n\nstatic struct attribute_group attr_group = {\n.attrs = attrs,\n};\n\nstatic struct kobject *example_kobj;\n\nstatic int __init example_init(void)\n{\nint retval;\n\nexample_kobj = kobject_create_and_add(\"kobject_example\", kernel_kobj);\nif (!example_kobj)\n    return -ENOMEM;\n\nretval = sysfs_create_group(example_kobj, &amp;attr_group);\nif (retval)\n    kobject_put(example_kobj);\n\nreturn retval;\n}\n\nstatic void __exit example_exit(void)\n{\nkobject_put(example_kobj);\n}\n\nmodule_init(example_init);\nmodule_exit(example_exit);\n<\/code><\/pre>\n<p><strong>include\/linux\/foo.h<\/strong><\/p>\n<pre><code>#ifndef FOO_H\n#define FOO_H\nextern unsigned int foo;\n#endif\n<\/code><\/pre>\n<p><strong>arch\/x86\/randomfile.c<\/strong><\/p>\n<pre><code>#include \n....\nint foobar = ( 12 + foo );\n....\n<\/code><\/pre>\n<p>I get this <strong>error: initializer element is not constant<\/strong> which makes me realize I must be doing something really wrong, but as much as I search I can&#8217;t find anything and I can&#8217;t figure out how to do it from looking at other implementations in the kernel&#8230;<\/p>\n<p>Could someone point me to the right direction, possibly with a practical example?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I would like to use an user-editable global variable in the linux kernel. Is that possible? That&#8217;s what I came up with using the example provided with the source code: arch\/x86\/kernel\/foo.c #include #include #include #include #include int foo = 12; static ssize_t foo_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { return sprintf(buf, &#8220;%d\\n&#8221;, foo); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5084","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5084","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5084"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5084\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5084"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5084"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5084"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}