LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Regarding how to use Global variable using our created modules (https://www.linuxquestions.org/questions/programming-9/regarding-how-to-use-global-variable-using-our-created-modules-919204/)

vishaluttamsalve 12-17-2011 08:30 AM

Regarding how to use Global variable using our created modules
 
Hi.. We have defined one global variable named as "trace_flag". we have also check that, its entry in kernel global symbol table,Yes it has inserted in table.
now our problem is that,we want to create module. Through that module we want to set that flag value to 1. But we are unable to use that "trace_flag" variable. We are thinking about ioctl() function. But still we are not getting it.. Please suggest.

jb_gpk 12-21-2011 04:10 AM

this variable is exported?

AFAIK, the global symbols have to be exported, so, all kernel can access it.

grep for EXPORT_SYMBOL() macro, the kernel use it everywhere.

vishaluttamsalve 12-21-2011 08:09 AM

Yes we have use EXPORT_SYMBOL. And we have defined one more function to access this "trace_flag",the function name is "set_trace(int)".

void set_trace(int pass)
{
trace_flag=pass
}

For this function also,we have used EXPORT_SYMBOL. And we have checked its existence in global table, YES,both are present..
Now, we have created one module to set this flag to 1,named "tracemod.c"

now in tracemod.c

in init_module()
{
set_trace(1);
}
and
in cleanup_module()
{
set_trace(0);
}


Ok..

Now our problem is ,the value of trace_flag is remain zero itself. It is not get updated...

Please suggest..

Mara 12-21-2011 04:35 PM

I'd just add some printk() in the set_trace function and in the module init/cleanup.

vishaluttamsalve 12-21-2011 11:53 PM

We have done that also. Only problem is that trace_flag is not get updated though it is global...

jb_gpk 12-23-2011 07:35 PM

Hello,

I can not understand why it is not working on your module, perhaps you are forgetting some basic step:

I tried to simulate your problem this way:

1) put the global variable and the exported function on somewhere on the kernel, I choose the init/main.c file:

Code:


diff --git a/init/main.c b/init/main.c
index 63f5f6f..e4e0d33 100644
--- a/init/main.c
+++ b/init/main.c
@@ -844,3 +844,11 @@ static int __init kernel_init(void * unused)
        init_post();
        return 0;
 }
+int trace_flag = 0;
+void my_set_trace(int pass)
+{
+      printk(KERN_INFO "setting trace_flag to %d\n",pass);
+      trace_flag = pass;
+      printk(KERN_INFO "trace_flag is now %d\n",trace_flag);
+}
+EXPORT_SYMBOL(my_set_trace);

2) put the function prototype on some header, so my module can get it, I choose the header linux/kernel.h:
Code:

diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index 4c0d3b2..4696ec1 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -728,7 +728,7 @@ extern int __build_bug_on_failed;
 
 struct sysinfo;
 extern int do_sysinfo(struct sysinfo *info);
-
+void my_set_trace (int pass);
 #endif /* __KERNEL__ */
 
 #define SI_LOAD_SHIFT  16

3) create a module to call the function:

Code:

#include <linux/kernel.h>
#include <linux/module.h>


int trace_init(void)
{
        printk(KERN_ERR "hello\n");
        my_set_trace(23);
        return 0;
}

void trace_exit(void)
{
        printk(KERN_ERR "bye\n");
}
module_init(trace_init);
module_exit(trace_exit);

so, when I insmod the module it I got:

Quote:

[ 5575.130326] hello
[ 5575.130761] setting trace_flag to 23
[ 5575.130779] trace_flag is now 23
is something like this that you are trying to do?
if yes, this is quite simple, there is no reason to fail.
please review your code and write here what you found.

happy hollyday! :)


All times are GMT -5. The time now is 05:00 AM.