LKM Implicit declaration of function stat - c

I am currently writing a Linux Kernel Module (for the first time).
I am trying to use the stat function. I understand that regular imports are not going to work in Kernel code, so I imported their equivalent (that is what I thought...):
#include <linux/stat.h>
#include <linux/errno.h>
#include <linux/unistd.h>
I don't get any error about the imports directly, but I get the following messages:
error: implicit declaration of function ‘stat’ [-Werror=implicit-function-declaration]
error: ‘errno’ undeclared (first use in this function)
For the two lines respectively:
int ret = stat(pathname, statbuf);
and
errno = ENOENT;
Any help woule be welcome.
Thank you.
PS: I am working on Kali 4.9.0-kali4-amd64 but I am trying to write something generic, that could work on multiple versions of Linux.

Related

Linking md5.h library for HTTP Digest sample implementation

I am struggling to compile a simple C program from RFC 2617. The program is digtest.c and it uses digcalc.c, another file from the sample implementation. The latter one depends on two files that my compiler doesn't know about:
#include <global.h>
#include <md5.h>
At first I got this error:
digcalc.c:5:20: fatal error: global.h: No such file or directory
I resolved that by changing <global.h> to <stddef.h>, it seems. But I still get this error:
digcalc.c:7:17: fatal error: md5.h: No such file or directory
Now, md5.h seems to refer to the file found in libbsd. So I installed libbsd-dev and tried to compile the files like this:
gcc digcalc.c digtest.c -o digtest -L/usr/lib/x86_64-linux-gnu -lbsd
where /usr/lib/x86_64-linux-gnu is the location of libbsd.so and libbsd.a files. However, this does not resolve the last compilation error.
Could anyone point out what am I missing here?
Figured it out. Had to change <md5.h> to <bsd/md5.h>, as noted on libbsd page.
So instead of the original headers in digcalc.c:
#include <global.h>
#include <md5.h>
I used:
#include <stddef.h>
#include <bsd/md5.h>
Also had to change function stricmp to strcasecmp, its POSIX equivalent. After that the sample code compiled seamlessly.

Error: "Implicit declaration" for device_create_with_groups

I am trying to build a custom kernel for NVIDIA Jetson Tk-1 by following the instructions here:
https://github.com/projectara/Android-wiki/wiki/Kernel-Only-Build-Instructions-for-Jetson-reference-platform
Everything goes well until the "Testing Your Custom Kernel" section:
STEP 2. make greybus modules
$ cd $JKB_ROOT/greybus
$ make clean
$ make ARCH=arm KERNELDIR=../kernel-out EXTRA_CFLAGS+=-fno-pic
Here is a snippet for the error I am getting:
/home/jonah/ara_kernel/greybus/loopback.c: In function
'gb_loopback_probe': /home/jonah/ara_kernel/greybus/loopback.c:1207:2:
error: implicit declaration of function 'device_create_with_groups'
[-Werror=implicit-function-declaration] dev =
device_create_with_groups(&loopback_class, ^
/home/jonah/ara_kernel/greybus/loopback.c:1207:6: warning: assignment
makes pointer from integer without a cast [enabled by default] dev =
device_create_with_groups(&loopback_class,
the problem occurs in the loopback.c file, which uses the function device_create_with_groups(...)
Here is the loopback.c file:
https://github.com/projectara/greybus/blob/master/loopback.c
As I understand, this function is found in include/linux/device.h header file, but even if I add #include <linux/device.h> to the beginning of loopback.c, I get the same implicit declaration error.
I am running Ubuntu 14.04 with Linux kernel headers 4.5.0-040500-generic.
Am I using the wrong kernel headers or is the function deprecated or something?
I can get rid of the warning with a declaration in the function above it:
struct device *device_create_with_groups(struct class *class, struct device *parent, dev_t devt, void *drvdata, const struct attribute_group **groups, const char *fmt, ...);
but will this have any severe repercussions when I use the kernel? Or will the function magically be found by the linker eventually?

unknown symbol __class_create (err 0)

This is probably a stupid question, but I looked for hours online and couldn't find an answer...
I'm writing a kernel module that also creates a character device. It compiles with no errors and warnings but when I try sudo insmod my_mod.ko I get:
insmod: error inserting 'my_mod.ko': -1 Unknown symbol in module
and when I try to look at dmesg I see:
my_mod: Unknown symbol __class_create (err 0)
my_mod: Unknown symbol device_create_file (err 0)
my_mod: Unknown symbol device_create (err 0)
I'm guessing that I missed an include but I can't find which...
What are the includes needed?
My includes are currently:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/ip.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/cdev.h>
#include <linux/fs.h>
The function __class_create is exported only for GPL modules (exported with EXPORT_SYMBOL_GPL). So, you need to use a GPL license with MODULE_LICENSE macro to make use of that function. Same goes for other functions as well.
This should do the trick:
MODULE_LICENSE("GPL");
To learn about what exporting is, take a look at here. Basically, dynamic modules do not have access to variables and functions in kernel, and kernel needs to specify what to export, to enable access. That's the purpose of EXPORT_SYMBOL and EXPORT_SYMBOL_GPL macros, which are used everywhere.
And the difference between EXPORT_SYMBOL and EXPORT_SYMBOL_GPL is that the latter only reveals the function or the variable if the module is GPL licensed.

How do I set up my code/VS10 to recognize CUDA function calls within .c files?

Basically, I have the compiler compiling my .cu files and I have (I think) full operation within those .cu files, but when I try to call them (kernel<<<1,1>>>(void)), the compiler registers syntax errors due to the CUDA syntax. Also, calls like cudaMalloc fail within c files.
Here are three really short files, so I can tell you where it is erroring.
//kernel.cu
#include "kernel.h"
#include <cuda.h>
#include <cuda_runtime_api.h>
__global__ int kernel(void){
return 5;
}
and
//kernel.h
#ifndef _KERNEL_h_
#define _KERNEL_h_
extern "C" int kernel(void);
#endif
and
//main.c
#include "kernel.h"
#include <cuda.h>
#include <cuda_runtime_api.h>
int main() {
int* device_a;
cudaMalloc( (void**)&device_a, sizeof(int) );
kernel<<<1,1>>>();
}
I got the header file from some of the SDK examples. Also, I have my build configuration set with CUDA 4.2, hence why the .cu file compiles. If I made any incidental syntax errors, it is because I simplified it for posting, not that it is actually in the source, although please mention it just in case.
kernel.cu compiles fine.
kernel.h has an error: "error C2059: syntax error : 'string'" on the "extern..." line. (Could this be because I took that from a c++ example?)
main.c has an error: "error C2065: 'kernel' : undeclared identifier"
and: "error C2059: syntax error : '<'"
but when I comment out the kernel call, so it is just cudaMalloc, I get: "error LNK2019: unresolved external symbol _cudaMalloc#8 referenced in function _main"
and: "fatal error LNK1120: 1 unresolved externals"
Is it something with Visual Studio 2010, or is it something I am not including? From the SDK examples, I can't tell what I'm doing wrong, other then they found a way, I think, to not use the triple bracket (CTRL+F doesn't find any). Any help is appreciated. Thank you in advance!
EDIT: after looking at some more examples, they do use the triple bracket syntax just fine.
EDIT: For those using this as reference, __global__ functions can only return void. If you try to return anything else, as I did, you will receive compiler errors.
Put the functions that invoke CUDA kernel in .cu files.
Set up VS2010 to compile CU files with the CUDA compiler, not the built in one (use the CUDA rules files (Cuda.props, Cuda.xml, Cuda.targets) located within the CUDA SDK).
I recommend placing kernels in files with a different extension (e.g. .curnel files), so that they will not be compiled directly (only if called).
I recommend putting the declaration of the functions that invoke CUDA kernels in .cuh files.

How can I compile code that uses getsubopt()?

I want to parse a list of options of the form key1=val1, key2=val2, etc (like the options to mount -o). The getsubopt() function seems perfect for this task (http://www.gnu.org/s/hello/manual/libc/Suboptions.html). However, when I try to compile my code using gcc, I get:
warning: implicit declaration of function ‘getsubopt’
and the program segfaults when I run it.
I added #include <stdlib.h> but the compiler doesn't pick up the declaration.
Do you have:
#define _XOPEN_SOURCE 500
#include <stdlib.h>
at the top of the file that contains the call to getsubopt? The error you are getting is what you would expect if you call a function which has not been declared.

Resources