简体   繁体   中英

Declaring a variable in a Linux kernel file to be used in a custom C file?

So I have been researching this tirelessly and nothing seems to be working. All I want to do is initialize an int to 0 on startup. Currently, I am in /kernel/sys.c and I have

/kernel/sys.c

int process_log_level;
EXPORT_SYMBOL(process_log_level);

Below that is some custom system calls that simply increment the variable. The system calls are called in the file below, and the variable increments just fine if you run

sudo dmesg -c

but all I want to have my C file read int process_log_level. I have the variable declared like this. ../process_log/process_log.c

#include <unistd.h>
#include <linux/module.h>
#include <stdio.h>
#include "process_log.h"

extern int process_log_level;

int main(int argc, char *argv[])
{
    printf("process_log_level = %d", process_log_level);
    return 0;
}

Whenever I try to compile the file I get told there is an undefined reference to "process_log_level." All I want to do is be able to access this variable in process_log.c. Is there something I'm missing here?

Userspace and kernel communicate via system calls.

Below that is some custom system calls

all I want to have my C file read int process_log_level.

So just add another system call that would return the value to userspace program. Then call that syscall from your program to retrieve the value from kernelspace.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM