LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-14-2018, 10:55 AM   #1
illy
LQ Newbie
 
Registered: Jun 2018
Posts: 8

Rep: Reputation: Disabled
gcc, init.h not found.


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.
 
Old 06-14-2018, 11:25 AM   #2
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 7,567
Blog Entries: 19

Rep: Reputation: 4448Reputation: 4448Reputation: 4448Reputation: 4448Reputation: 4448Reputation: 4448Reputation: 4448Reputation: 4448Reputation: 4448Reputation: 4448Reputation: 4448
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.
 
Old 06-14-2018, 11:30 AM   #3
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Look at this Makefile
https://www.tldp.org/LDP/lkmpg/2.4/html/x208.htm
A bit old but still work I think
 
Old 06-14-2018, 01:16 PM   #4
illy
LQ Newbie
 
Registered: Jun 2018
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
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?

Quote:
Look at this Makefile
https://www.tldp.org/LDP/lkmpg/2.4/html/x208.htm
A bit old but still work I think
Did I write the Makefile correctly?
Code:
${hello}.o: ${hello}.c

.PHONY: clean

clean:
	rm -rf ${hello}.o
I get
rm -rf .o
for make command and nothing happens.
 
Old 06-14-2018, 02:21 PM   #5
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Do you have hello defined somewhere in the Makefile?
Could you post the whole file?

I suggested the Makefile from the link as it gives specific options to gcc for compiling a kernel module
 
1 members found this post helpful.
Old 06-14-2018, 03:14 PM   #6
illy
LQ Newbie
 
Registered: Jun 2018
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by keefaz View Post
Do you have hello defined somewhere in the Makefile?
Could you post the whole file?

I suggested the Makefile from the link as it gives specific options to gcc for compiling a kernel module
Yeah, sorry, I misunderstood the format.

Code:
TARGET  := hello
WARN    := -W -Wall -Wstrict-prototypes -Wmissing-prototypes
INCLUDE := -isystem /lib/modules/`uname -r`/build/include
CFLAGS  := -O2 -DMODULE -D__KERNEL__ ${WARN} ${INCLUDE}
CC      := gcc-3.0
	
${TARGET}.o: ${TARGET}.c

.PHONY: clean

clean:
	rm -rf ${TARGET}.o
With this Makefile I get
Code:
gcc-3.0 -O2 -DMODULE -D__KERNEL__ -W -Wall -Wstrict-prototypes -Wmissing-prototypes -isystem /lib/modules/`uname -r`/build/include   -c -o hello.o hello.c
/bin/sh: 1: gcc-3.0: not found
<builtin>: recipe for target 'hello.o' failed
make: *** [hello.o] Error 127
when I use make command.
 
Old 06-14-2018, 04:51 PM   #7
illy
LQ Newbie
 
Registered: Jun 2018
Posts: 8

Original Poster
Rep: Reputation: Disabled
Also when i input make with Makefile like this
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 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.

Last edited by illy; 06-14-2018 at 04:52 PM.
 
Old 06-14-2018, 06:40 PM   #8
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
In the other Makefile, change
Code:
CC      := gcc-3.0
To:
Code:
CC      := gcc
 
1 members found this post helpful.
Old 06-14-2018, 06:40 PM   #9
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
In the other Makefile, change
Code:
CC      := gcc-3.0
To:
Code:
CC      := gcc
 
1 members found this post helpful.
Old 06-15-2018, 04:34 AM   #10
illy
LQ Newbie
 
Registered: Jun 2018
Posts: 8

Original Poster
Rep: Reputation: Disabled
Changing to gcc resulted in
Code:
gcc -O2 -DMODULE -D__KERNEL__ -W -Wall -Wstrict-prototypes -Wmissing-prototypes -isystem /lib/modules/`uname -r`/build/include /usr/src/linux-headers-4.15.0-23/arch/metag/include   -c -o hello.o hello.c
In file included from /usr/src/linux-headers-4.15.0-23/include/linux/init.h:5:0,
                 from hello.c:5:
/usr/src/linux-headers-4.15.0-23/include/linux/compiler.h:242:10: fatal error: asm/barrier.h: No such file or directory
 #include <asm/barrier.h>
          ^~~~~~~~~~~~~~~
compilation terminated.
<builtin>: recipe for target 'hello.o' failed
make: *** [hello.o] Error 1
 
Old 06-15-2018, 07:35 AM   #11
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Reviewing kernel.org instruction:
https://www.kernel.org/doc/Documenta...ld/modules.txt

Create a file with name: Kbuild

Kbuild content:
Code:
obj-m := hello.o
Create Makefile file

Makefile content:
Code:
KDIR ?= /lib/modules/`uname -r`/build

default:
	$(MAKE) -C $(KDIR) M=$$PWD
Now to compile the module, just type: make

Last edited by keefaz; 06-15-2018 at 07:37 AM.
 
1 members found this post helpful.
Old 06-15-2018, 08:51 AM   #12
illy
LQ Newbie
 
Registered: Jun 2018
Posts: 8

Original Poster
Rep: Reputation: Disabled
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.
 
Old 06-15-2018, 09:23 AM   #13
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
I tried compiling the code, it ended with a hello.ko kernel module, with a warning but no errors

My system runs with kernel 4.4.118

It's possible that kernel header files change from version to another version

Try these changes at top of your hello.c file:
Code:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include <linux/uaccess.h>
Try a compile, if it fails, then try changing includes to:
Code:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include <linux/uaccess.h>
#include <asm/uaccess.h>
 
1 members found this post helpful.
Old 06-15-2018, 09:37 AM   #14
illy
LQ Newbie
 
Registered: Jun 2018
Posts: 8

Original Poster
Rep: Reputation: Disabled
Code:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include <linux/uaccess.h>
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.

Again, thanks for your help and time.
 
Old 06-15-2018, 11:34 AM   #15
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Kernel Panic: No Init Found Try Passing Init= Opt DebianUser Linux - Kernel 3 09-09-2014 05:36 AM
kernal panic no init found try passing init option to kernel m2azer Linux - General 3 11-08-2007 01:51 PM
Kernal Panic: No init found. Try passing init= option to kernel raees Linux - General 12 03-18-2004 11:10 PM
error "Kernel panic: No init found. Try passing init= option Anauj0101 Linux - Newbie 3 05-06-2003 10:18 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration