I am new to kernel module programming and got some problem while compiling some old kernel code.
The code is trying to get tty for the current task using current->tty, but when I compile, I get following an error
error: ‘struct task_struct’ has no member named ‘tty’
my_tty = current->tty;
I checked the linux source code , tty is not there any more.
The 'current' is of task_struct,but tty not in struct task_struct any more.
How can I access them in version 3.13.0-24?
Related
I'm trying to run some tests using IORING_SETUP_SQPOLL, but when it's set and I call io_uring_cqe_get_data(cqe); I get "cqe failed: Bad file descriptor". Reading about Kernel Side Polling in io_uring.pdf, it seems that I might have to call:
/* fills in new sqe entries */
add_more_io();
/*
* need to call io_uring_enter() to make the kernel notice the new IO
* if polled and the thread is now sleeping.
*/
if ((*sqring→flags) & IORING_SQ_NEED_WAKEUP)
io_uring_enter(ring_fd, to_submit, to_wait, IORING_ENTER_SQ_WAKEUP);
but when I try to compile it, gcc cannot find io_uring_enter() (undefined reference to `io_uring_enter')
I'm using Ubuntu 18.04.5 LTS - kernel: 5.4.0-62-generic.
The code can be found here
Does anyone have some experience with this option/flag?
I will give a detailed exp of the program and lead to the issue regarding the use of netlink socket communication.
The last paragraph asks the actual question I need an answer for, so you might wanna start by peeking it first.
Disclaimer before I start:
- I have made an earlier search before asking here and did not find complete solution / alternative to my issue.
- I know how to initialize a module and insert it to kernel.
- I know to handle communication between module and user-space without using netlink sockets. Meaning using struct file_operations func pointers assignments to later be invoked by the module program whenever a user attempts to read/write etc. and answer to the user using copy_to_user / copy_from_user.
- This topic refers to Linux OS, Mint 17 dist.
- Language is C
Okay, so I am building a system with 3 components:
1. user.c : user application (user types commands here)
2. storage.c : storage device ('virtual' disk-on-key)
3. device.ko : kernel module (used as proxy between 1. and 2.)
The purpose of this system is to be able (as a user) to:
- Copy files to the virtual disk-on-key device (2) - like an "upload" from local directory that belongs to the user.
- Save files from the virtual device on local directory - like "download" from the device storage to the user directory.
Design:
Assuming programs (1),(2) are compiled and running + (3) has successfully inserted using the bash command ' sudo insmod device.ko ' , the following should work like this (simulation ofc):
Step 1 (in user.c) -> user types 'download file.txt'
Step 2 (in device.ko) -> the device recognizes the user have tried to 'write' to it (actually user just passing the string "download file.txt") and invokes the 'write' implementation of the method we set on struct file_operation earlier on module_init().
The device (kernel module) now passes the data (string with a command) to the storage.c application, expecting an answer to later be retrieved to the user.c application.
Step 3 (in storage.c) -> now, lets say this program performs a busy-wait loop of 'readmsg()' and that's how a request from module event is triggered and recognized, the storage device now recognizes that the module has sent a request (string with a command \ data). Now, the storage programs shall perform an implementation of some function 'X' to send the data requested using sendmsg() somewhere inside the function.
Now, here comes the issue.
Usually, on all of the examples I've looked on web, the communication between the kernel-module and a user-space (or the storage.c program in our case) using netlink is triggered by the user-space and not vice versa. Meaning that the sendmsg() function from the user-space invokes the 'request(struct sk_buff *skb)' method (which is set on the module_init() part as following:
struct netlink_kernel_cfg cfg = {
.input = request // when storage.c sends something, it invokes the request function
};
so when the storage.c performs something like:
sendmsg(sock_fd,&msg,0); // send a msg to the module
the module invokes and runs the:
static void request(struct sk_buff *skb) {
char *msg ="Hello from kernel";
msg_size=strlen(msg);
netlink_holder=(struct nlmsghdr*)skb->data;
printk(KERN_INFO "Netlink received msg payload:%s\n",(char*)nlmsg_data(netlink_holder));
pid = netlink_holder->nlmsg_pid; // pid of sending process
skb_out = nlmsg_new(msg_size,0);
if(!skb_out){
printk(KERN_ERR "Failed to allocate new skb\n");
return;
}
netlink_holder=nlmsg_put(skb_out,0,0,NLMSG_DONE,msg_size,0); // add a new netlink message to an skb. more info: http://elixir.free-electrons.com/linux/v3.2/source/include/net/netlink.h#L491
NETLINK_CB(skb_out).dst_group = 0; // not in multicast group
strncpy(nlmsg_data(netlink_holder),msg,msg_size); // assign data as char* (variable msg)
result=nlmsg_unicast(sock_netlink,skb_out,pid); // send data to storage. more info: http://elixir.free-electrons.com/linux/latest/source/include/net/netlink.h#L598
if(result<0)
printk(KERN_INFO "Error while sending bak to user\n");
}
and from all that big chunk, the only thing that im interesting in is actually doing this:
result=nlmsg_unicast(sock_netlink,skb_out,pid); // send data to storage.
BUT I can't use nlmsg_unicast() without having the strcut sk_buff* which is provided automatically for me whenever there's an invoke from storage.c !
To sum up everything:
How do I send a msg from the device.ko (kernel module) to the user-space withtout having to wait for request to invoke / rely on the provided strcut sk_buff parameter from the earlier shown 'request()' method ?
Hope this sums up the point.
Thanks.
The only question here is that you need the user-space program connected to kernel-space first to get the pid of your user-program.
After get the pid, you can manually construct the skb_out and send it out through netlink_unicast or nlmsg_unicast.
The pid is always needed, you can set it as static and let your user-space program connect to your device.ko to make a long-maintained link.
Although this question is asked at 2017, I believe OP has already found the answer :D
I was referring:
http://www-users.cs.umn.edu/~boutcher/kprobes/kprobes.txt.html to understand kprobe. I used kprobe_example.c as given in the doc.
I compiled it using the makefile (code taken from the same document)
I got compilation errors because my kernel version is 4.2 and some fields were changed in struct pt_regs. So I replaced eip with ip and eflag with flag in kprobe_example.c which is https://gist.github.com/murlee417/87c2eb43a6afa1954b05404a07813e81. Then I was able to compile it successfully.
Now, as a root user, I did:
#insmod kprobe_example.ko
and I got:
insmod: ERROR: could not insert module kprobe_example.ko: Operation not permitted
My message buffer has:
#dmesg
[ 4537.478408] Couldn't find do_fork to plant kprobe
Please help me to resolve this error and make insmod work.
In x86 do_fork() is known as sys_fork(), so change the code as below
/* For each probe you need to allocate a kprobe structure */
static struct kprobe kp = {
//.symbol_name = "do_fork",
.symbol_name = "sys_fork",
};
I'm implementing the ad hoc AODV routing protocol on ARM based system Sabrelite http://boundarydevices.com/products/sabre-lite-imx6-sbc/ ......... After configuring and building the kernel that match to the one used on the board. I get the following errors:
error: unknown type name '__kernel_ulong_t'
__kernel_ulong_t loads[3]; /* 1, 5, and 15 minute load averages */
error: unknown type name '__kernel_ulong_t'
__kernel_ulong_t totalram; /* Total usable main memory size */
^
/usr/local/oecore-x86_64/sysroots/cortexa9hf-vfp-neon-oe-linux-gnueabi/usr/include/linux/sysinfo.h:18:2: error: unknown type name '__kernel_ulong_t'
__kernel_ulong_t freeram; /* Available memory size */
^
error: unknown type name '__kernel_ulong_t'
__kernel_ulong_t sharedram; /* Amount of shared memory */
^
/usr/local/oecore-x86_64/sysroots/cortexa9hf-vfp-neon-oe-linux-gnueabi/usr/include/linux/sysinfo.h:28:22: error: '__kernel_ulong_t' undeclared here (not in a function)
char _f[20-2*sizeof(__kernel_ulong_t)-sizeof(__u32)]; /* Padding: libc5 uses this.. */
^
make: * [nl-arm.o] Error 1
As the errors suggested, it is something along with the cross compilation source tree but i don't know how on earth i get those errors because the used SDK works perfectly.
I followed the tutorial cited in the README file provided by AODV packages..
Has anyone ever experienced implementing AODV on ARM ? and did they got these errors
How can i resolve these issues ?
Thanks
well, the solution was to properly upgrade the makefile to work for post 2.6 kernel. If you happen to read this page: write your makefile like this:http://lwn.net/Articles/21823/
I thought I would try out SDP on our infiniband hardware.
However, when I try to add AF_INET_SDP as the first argument to socket() I get the following error:
"Address family not supported by protocol".
Originally I had:
#define AF_INET_SDP 26
But after doing some reading, noticed a patch applied some time back to change this value to 27.
When set to 26 I get the error:
"Error binding socket: No such device"
Has anyone managed to get SDP working on Ubuntu 12.04? what did you do to get it up and running?
I have installed libsdp1 and libsdpa-dev
Using the LD_PRELOAD method on iperf I also get the first error:
LD_PRELOAD=libsdp.so iperf -s
dir: /tmp/libsdp.log.1000 file: /tmp/libsdp.log.1000/log
socket failed: Address family not supported by protocol
bind failed: Bad file descriptor
Therefore I assume 27 is the correct domain number.
SDP hasn't been accepted on the mainline linux kernel. On recent fedora, they don't ship it, neither the user space libsdp.
If you still want to experiment, Matt is right, the module in question is 'ib_sdp'.
try modprobe ib_sdp and run your example again.