Tuesday, June 25, 2019

Linux: How does the kernel invoke the correct driver for the corresponding /dev file ops?

To answer this question, I am picking snippets of texts from different blogs that I read online.
"When devfs is not being used, adding a new driver to the system means assigning a major number to it. The assignment should be made at driver (module) initialization by calling the following function, defined in <linux/fs.h>:
int register_chrdev(unsigned int major, const char *name,
struct file_operations *fops);
The return value indicates success or failure of the operation. A negative return code signals an error; a 0 or positive return code reports successful completion. The major argument is the major number being requested, name is the name of your device, which will appear in /proc/devices, and fops is the pointer to an array of function pointers" [1]
Thus, this will result in the registration of the device driver with a major number with the kernel. 
"Once the driver has been registered in the kernel table, its operations are associated with the given major number. Whenever an operation is performed on a character device file associated with that major number, the kernel finds and invokes the proper function  from the  file_operations structure. For this reason, the pointer passed to register_chrdev should point to a global structure within the driver, not to one local to the module’s initialization function."

Tuesday, June 18, 2019

Linux: Compare if two files are the same

Quick script I wrote in my .bashrc to check if two files are the same:

function md5comp()
    a=$(md5sum $1 | awk '{print $1}')
    b=$(md5sum $2 | awk '{print $1}')
    echo "Comparing $1 and $2"
    [ "$a" = "$b" ] && echo  "Equal" || echo "Not Equal"


Linux: Sleeping while atomic during kmalloc solved

So you ran into a crash where the kernel complains that you were sleeping while atomic.
You were either able to run objdump or gdb and track down that the crash is caused due
to a kmalloc? How is that possible?
Did you make recent changes which included putting in a kmalloc?

Monday, June 17, 2019

Linux: How to tar only selected folders and files

Step 1: Select the files and prepare a list.

I do this with a simple function in my .bashrc script:

$> cat >> ~/.bashrc
99 function cscopesetup()
100 {
101     rm cscope.files;

103     echo "Populating filenames...";
104     find $SRC/folder1/ -iname "*.c" -print >> cscope.files #folder1
105     find $SRC/folder9/ -iname "*.h" -print >> cscope.files #folder9
106     echo "Driver done"
107 }