IOCTL from kernel space

Roughly speaking, I am trying to issue an IOCTL call from kernel space without going to user space. (All the answers I found in SO propose going through user space).

Specifically, I try to fill the entropy pool (/dev/random) from kernel space (using a kernel module) [I know the dangers of doing this ;)]. Filling up the entropy pool from user space is done using IOCTL, e.g., rngaddentropy. Is there a way to do the same thing from kernel space?

Answers


You can use ioctl from the kernel space too.

Because ioctl command RNDADDENTROPY is file-specific, its processing should be implemented in the .unlocked_ioctl operation for /dev/random file (and it is actually implemented this way, see function random_ioctl).

For file-specific ioctl commands you may call .unlocked_ioctl file's operation directly:

// Open file
struct file* f = filp_open("/dev/random", O_WRONLY, 0);
// Replace user space with kernel space
mm_segment_t old_fs = get_fs();
set_fs(KERNEL_DS);

f->f_op->unlocked_ioctl(f, RNDADDENTROPY, entropy);

// Restore space
set_fs(old_fs);

// Close file
filp_close(f, 0);

Need Your Help

Two imageviews not showing at the same time

android

Can someone help me? The two imageviews are not showing at the same time. Here's the code:

How to select text in gedit

ubuntu copy mouse gedit

This only happens in gedit.