Using ioctl communication between Kernel mode and user mode - ubuntu-10.04

I want to communicate with my kernel module using ioctl. I have written two c program one for kernel module and other for user mode. I am getting this error while compiling kernel module:
error: unknown field ‘ioctl’ specified in initializer
at this line :
struct file_operations Fops = {
.read = device_read,
.write = device_write,
.ioctl = device_ioctl, ------> at this point error is occuring.
.open = device_open,
.release = device_release,
};
any idea why this is happening.
thanks

In newer kernels, the preferred way is to use .unlocked_ioctl or .compat_ioctl fields. The plain .ioctl was removed from struct file_operations. This discussion may clarify what happened and how to deal with that.

In newer kernels, use .unlocked_ioctl in the place of .ioctl. It works fine.

Related

How does a ioctl() call the driver code

I am working on a testing tool for nvme-cli(written in c and can run on linux).
For SSD validation purpose, i was actually looking for a custom command(For e.g. I/O command, write and then read the same and finally compare if both the data are same)
For read the ioctl() function is used as shown in the below code.
struct nvme_user_io io = {
.opcode = opcode,
.flags = 0,
.control = control,
.nblocks = nblocks,
.rsvd = 0,
.metadata = (__u64)(uintptr_t) metadata,
.addr = (__u64)(uintptr_t) data,
.slba = slba,
.dsmgmt = dsmgmt,
.reftag = reftag,
.appmask = appmask,
.apptag = apptag,
};
err = ioctl(fd, NVME_IOCTL_SUBMIT_IO, &io);
Can I to where exactly the control of execution goes in order to understand the read.
Also I want to have another command that looks like
err = ioctl(fd,NVME_IOCTL_WRITE_AND_COMPARE_IO, &io);
so that I can internally do a write, then read the same location and finally compare the both data to ensure that the disk contains only the data that I wanted to write.
Since I am new to this nvme/ioctl(), if there is any mistakes please correct me.
nvme_io() is a main command handler that accepts as a parameter the NVMe opcode that you want to send to your device. According to the standard, you have separate commands (opcodes) for read, write and compare. You could either send those commands separately, or add a vendor specific command to calculate what you need.

What linux kernel C function schedule () appears "need_resched:" mean

need_resched:
preempt_disable();
cpu = smp_processor_id();
rq = cpu_rq(cpu);
rcu_note_context_switch(cpu);
prev = rq->curr;
switch_count = &prev->nivcsw;
release_kernel_lock(prev);
I would like to ask is: "need_resched:" What is the role.
In detail,The linux kernel version is 2.6.35.3.
need_schedule: is simply a label. Later in the code you will find:
if (need_resched())
goto need_resched;
I.e., if the rescheduling flag ist set (what is tested by need_reschedule()), this point in code is executed (again).

Node.js function not working for isr galileo board

I want to use node.js mraa library for Galileo.
I need to set up an interrupt.
I achieve this by:
var param=1;
var myLed = new mraa.Gpio(2);
myLed.dir(mraa.DIR_IN); //set the gpio direction to input
myLed.isr(mraa.EDGE_BOTH,function f(x){},param );
i get this errors
in method 'Gpio_isr', argument 3 of type 'void (*)(void *)'
The documentation for this function states
mraa_result_t isr ( Edge mode,
void(*)(void *) fptr,
void * args
)
Sets a callback to be called when pin value changes
Parameters
mode The edge mode to set
fptr Function pointer to function to be called when interupt is triggered
args Arguments passed to the interrupt handler (fptr)
Returns
Result of operation
I don't know how to set up the params of function...
There is an open issue about this. The current response is that the isr method is not currently working.
Link:
https://github.com/intel-iot-devkit/mraa/issues/110
As pointed out in the issue, you can now do:
var m = require('mraa')
function h() {
console.log("HELLO!!!!")
}
x = new m.Gpio(14)
x.isr(m.EDGE_BOTH, h)
You'll need to be on v0.5.4-134-gd6891e8 or later from the master branch. You can use npm to get the correct version installed on your board or just compile form sources (you'll need SWIG 3.x)
npm install mraa

Why is the kernel using the default block driver instead of my driver's code?

I wrote a block driver program which creates a dummy block device (sbd0). I registered all device operations for that block device: (Refer to include/linux/blkdev.h in 2.6.32 kernel source)
static struct block_device_operations sbd_ops = {
.owner = THIS_MODULE,
.open = sbd_open,
.release = sbd_close,
.ioctl = sbd_ioctl,
.getgeo = sbd_getgeo,
.locked_ioctl = sbd_locked_ioctl,
.compat_ioctl = sbd_compat_ioctl,
.direct_access = sbd_direct_access,
.media_changed = sbd_media_changed,
.revalidate_disk = sbd_revalidate_disk
};
I compiled the driver program. I inserted the module and /dev/sbd0 was created. Now I want to test my driver code. So I wrote an application as below.
fd = open("/dev/sbd0", O_RDONLY);
retval = ioctl(fd, BLKBSZGET, &blksz); //trying to get logical block size
Output is :4096
I wondered: I didn't implement ioctl for BLKBSZGET. It didn't invoke my sbd_ioctl, instead it used the default driver and gave me the result. For open, close calls it executed sbd_open and sbd_close (that I implemented). And then I tried:
retval = ioctl(fd, HDIO_GETGEO, &geoinfo);
It invoked sbd_getgeo but I thought it would invoke sbd_ioctl.
Here are my questions:
I implemented a driver and created a device. If I perform any operation on that device, it has to invoke my driver application. But how does it use a few of my driver functions and few default driver functions?
ioctl(fd, HDIO_GETGEO, ..) didn't invoke .ioctl call, but it invoked .getgeo. How is this possible?
The ioctl dispatching is handled by the blkdev_ioctl function, which will process some of the ioctls directly, without calling into your driver's specific routine.
For HDIO_GETGEO, it calls your driver's getgeo function directly (from kernel 3.13.6, doesn't appear to have changed much since 2.6.32):
[...]
/*
* We need to set the startsect first, the driver may
* want to override it.
*/
memset(&geo, 0, sizeof(geo));
geo.start = get_start_sect(bdev);
ret = disk->fops->getgeo(bdev, &geo); /* <- here */
[...]
For BLKBSZGET, it calls block_size(bdev)), which simply returns bdev->bd_block_size.
You'll find blkdev_ioctl in block/ioctl.c if you need to know what happens for other ioctls.

Python 3 C-API IO and File Execution

I am having some serious trouble getting a Python 2 based C++ engine to work in Python3. I know the whole IO stack has changed, but everything I seem to try just ends up in failure. Below is the pre-code (Python2) and post code (Python3). I am hoping someone can help me figure out what I'm doing wrong.I am also using boost::python to control the references.
The program is supposed to load a Python Object into memory via a map and then upon using the run function it then finds the file loaded in memory and runs it. I based my code off an example from the delta3d python manager, where they load in a file and run it immediately. I have not seen anything equivalent in Python3.
Python2 Code Begins here:
// what this does is first calls the Python C-API to load the file, then pass the returned
// PyObject* into handle, which takes reference and sets it as a boost::python::object.
// this takes care of all future referencing and dereferencing.
try{
bp::object file_object(bp::handle<>(PyFile_FromString(fullPath(filename), "r" )));
loaded_files_.insert(std::make_pair(std::string(fullPath(filename)), file_object));
}
catch(...)
{
getExceptionFromPy();
}
Next I load the file from the std::map and attempt to execute it:
bp::object loaded_file = getLoadedFile(filename);
try
{
PyRun_SimpleFile( PyFile_AsFile( loaded_file.ptr()), fullPath(filename) );
}
catch(...)
{
getExceptionFromPy();
}
Python3 Code Begins here: This is what I have so far based off some suggestions here... SO Question
Load:
PyObject *ioMod, *opened_file, *fd_obj;
ioMod = PyImport_ImportModule("io");
opened_file = PyObject_CallMethod(ioMod, "open", "ss", fullPath(filename), "r");
bp::handle<> h_open(opened_file);
bp::object file_obj(h_open);
loaded_files_.insert(std::make_pair(std::string(fullPath(filename)), file_obj));
Run:
bp::object loaded_file = getLoadedFile(filename);
int fd = PyObject_AsFileDescriptor(loaded_file.ptr());
PyObject* fileObj = PyFile_FromFd(fd,fullPath(filename),"r",-1,"", "\n","", 0);
FILE* f_open = _fdopen(fd,"r");
PyRun_SimpleFile( f_open, fullPath(filename) );
Lastly, the general state of the program at this point is the file gets loaded in as TextIOWrapper and in the Run: section the fd that is returned is always 3 and for some reason _fdopen can never open the FILE which means I can't do something like PyRun_SimpleFile. The error itself is a debug ASSERTION on _fdopen. Is there a better way to do all this I really appreciate any help.
If you want to see the full program of the Python2 version it's on Github
So this question was pretty hard to understand and I'm sorry, but I found out my old code wasn't quite working as I expected. Here's what I wanted the code to do. Load the python file into memory, store it into a map and then at a later date execute that code in memory. I accomplished this a bit differently than I expected, but it makes a lot of sense now.
Open the file using ifstream, see the code below
Convert the char into a boost::python::str
Execute the boost::python::str with boost::python::exec
Profit ???
Step 1)
vector<char> input;
ifstream file(fullPath(filename), ios::in);
if (!file.is_open())
{
// set our error message here
setCantFindFileError();
input.push_back('\0');
return input;
}
file >> std::noskipws;
copy(istream_iterator<char>(file), istream_iterator<char>(), back_inserter(input));
input.push_back('\n');
input.push_back('\0');
Step 2)
bp::str file_str(string(&input[0]));
loaded_files_.insert(std::make_pair(std::string(fullPath(filename)), file_str));
Step 3)
bp::str loaded_file = getLoadedFile(filename);
// Retrieve the main module
bp::object main = bp::import("__main__");
// Retrieve the main module's namespace
bp::object global(main.attr("__dict__"));
bp::exec(loaded_file, global, global);
Full Code is located on github:

Resources