ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm trying to do the very first book project that wants to show me how to create a new entry in /proc file system. They gave me a program called hello.c and said that it creates a /proc entry named /proc/hello but I can't get the program to execute. Spent 5-6 hours messing around with it today.
Program looks like this
Code:
/**
* Kernel module that communicates with /proc file system.
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include <asm/uaccess.h>
#define BUFFER_SIZE 128
#define PROC_NAME "hello"
#define MESSAGE "Hello World\n"
/**
* Function prototypes
*/
ssize_t proc_read(struct file *file, char *buf, size_t count, loff_t *pos);
static struct file_operations proc_ops = {
.owner = THIS_MODULE,
.read = proc_read,
};
/* This function is called when the module is loaded. */
int proc_init(void)
{
// creates the /proc/hello entry
// the following function call is a wrapper for
// proc_create_data() passing NULL as the last argument
proc_create(PROC_NAME, 0, NULL, &proc_ops);
printk(KERN_INFO "/proc/%s created\n", PROC_NAME);
return 0;
}
/* This function is called when the module is removed. */
void proc_exit(void) {
// removes the /proc/hello entry
remove_proc_entry(PROC_NAME, NULL);
printk( KERN_INFO "/proc/%s removed\n", PROC_NAME);
}
/**
* This function is called each time the /proc/hello is read.
*
* This function is called repeatedly until it returns 0, so
* there must be logic that ensures it ultimately returns 0
* once it has collected the data that is to go into the
* corresponding /proc file.
*
* params:
*
* file:
* buf: buffer in user space
* count:
* pos:
*/
ssize_t proc_read(struct file *file, char __user *usr_buf, size_t count, loff_t *pos)
{
int rv = 0;
char buffer[BUFFER_SIZE];
static int completed = 0;
if (completed) {
completed = 0;
return 0;
}
completed = 1;
rv = sprintf(buffer, "Hello World\n");
// copies the contents of buffer to userspace usr_buf
copy_to_user(usr_buf, buffer, rv);
return rv;
}
/* Macros for registering module entry and exit points. */
module_init( proc_init );
module_exit( proc_exit );
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Hello Module");
MODULE_AUTHOR("SGG");
When I enter gcc hello.c I get
Code:
hello.c:5:10: fatal error: linux/init.h: No such file or directory
#include <linux/init.h>
^~~~~~~~~~~~~~
compilation terminated.
I have tried installing various packs that people recommended to other people with same problem.
I also made a Makefile that I found on this forum, it was posted for someone with same problem as me. But it didn't help me.
Code:
obj-m += hello.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
I'm using ubuntu. And This is only my 3rd day using Linux so I don't know much. Never compiled programs trough terminal either, did everything in IDE. Previous part of this project had a Makefile where I created a module. This part only had one .c file. Source code can be found here http://os-book.com/, 10th edition.
init.h is one of the kernel's built-in headers, the ones used to actually build kernels from source. It isn't included in the exported headers that go into /usr/include/linux. Many distros provide these kernel headers as a separate package, since people need them for building proprietary driver modules. If you can't get that, use the kernel source itself.
init.h is one of the kernel's built-in headers, the ones used to actually build kernels from source. It isn't included in the exported headers that go into /usr/include/linux. Many distros provide these kernel headers as a separate package, since people need them for building proprietary driver modules. If you can't get that, use the kernel source itself.
I have tried something like this
gcc -c -I/usr/src/linux-headers-4.15.0-23-generic/include/ hello.c
by searing for init.h and seeing where it was but then it said that <asm/linkage.h> could not be found. I did the same for linkage.h and the amount of errors filled entire terminal. How would I use source exactly?
obj-m += hello.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
I get this error in the terminal
Code:
make -C /lib/modules/4.15.0-23-generic/build M=/home/illya/Desktop/OS modules
make[1]: Entering directory '/usr/src/linux-headers-4.15.0-23-generic'
CC [M] /home/illya/Desktop/OS/hello.o
In file included from /home/illya/Desktop/OS/hello.c:9:0:
./arch/x86/include/asm/uaccess.h: In function ‘set_fs’:
./arch/x86/include/asm/uaccess.h:32:9: error: dereferencing pointer to incomplete type ‘struct task_struct’
current->thread.addr_limit = fs;
^~
/home/illya/Desktop/OS/hello.c: In function ‘proc_read’:
/home/illya/Desktop/OS/hello.c:81:9: error: implicit declaration of function ‘copy_to_user’; did you mean ‘raw_copy_to_user’? [-Werror=implicit-function-declaration]
copy_to_user(usr_buf, buffer, rv);
^~~~~~~~~~~~
raw_copy_to_user
cc1: some warnings being treated as errors
scripts/Makefile.build:339: recipe for target '/home/illya/Desktop/OS/hello.o' failed
make[2]: *** [/home/illya/Desktop/OS/hello.o] Error 1
Makefile:1552: recipe for target '_module_/home/illya/Desktop/OS' failed
make[1]: *** [_module_/home/illya/Desktop/OS] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-4.15.0-23-generic'
Makefile:3: recipe for target 'all' failed
make: *** [all] Error 2
I tried changing program to "raw_copy_to_user" like terminal suggested but it only made it worse.
Unfortunately that resulted in same errors as in my post No.7.
Could there be something wrong with my system or the program? I'm almost ready to contact the author at this point and ask him directly what I'm supposed to do here. Feels like I could have already been few chapters in but instead I spend time wrestling with this project because the book didn't really teach how to make programs like these from scratch just "type make in terminal and see what happens" so far. Really frustrating when it doesn't work.
That did it!
It compiled successfully. Thank you very much for your time.
It's beyond my comprehension how that one #include made it successful. Does that means that my uaccess.h was in linux directory and pre-made program assumed it was in asm? I think at one point I tried to do a search for uaccess.h file but it showed so many results that I got somewhat overwhelmed.
Yes, header files may have changed from the time the program was written in the book and now.
Glad the compilation works now, else I wouldn't see where to go
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.