PATCHING & HACKING

My first Hack

 Since you have been working on your schemata, you have surely had a chance to use of “printk()”  printk() is the kernel equivalent of () and much like printf(), it’s an invaluable tool for interacting with your kernel  If you have worked with printf you have probably worked with a similar syntax. Printk()’s syntax is as follows: printk( “format string %d”, someint);  You shall see some about printk() in the debugging class Hacking for posterity

 So if you can program in C, you can presumably hack the Linux kernel.  How do you go about sharing all those wonderful hacks with all the rest of us poor saps?  Clue: Patches

When is patching useful? Well…

 Patching is a good way to pass your code on to others  Especially if your code is an addendum to someone else’s code  Particularly if your code is spread across several files or even directories  In other words: If you are writing code in the Linux kernel patching is for you How to stuff

 Take a or files that you wish to modify  Copy them into a separate directory  Modify them  Take both the original file and the modified file and apply the patch command More specifically

 Say, for argument’s sake, that the original file is called /rootdir/somedir/orig.c and the modified file is /rootdir/someotherdir/new.c  the following in the command line from the rootdir directory

$ –urN –X somedir/orig.c someotherdir/new.c > changes.patch

 This shall create a diff file you may use as a patch A few more words on diff

 What did we just do in the command line?  -u Shall cause our diff to output a file in the ‘unified’ format  -r prompts the diff command to be issued recursively on any subfiles and directories found  -N Treat’s absent files as empty  -X ignore files and subdirectories whose basenames contain any pattern contained in file Diff is more than a just tool for making sure two homeworks are not too similar

 I heartily suggest checking out the sights the diff  And now back to patching Anatomy of a diff patch

Original file range (starts at line New file range given new 287 and is 4 lines long) lines

Path to original plus timestamp Path to new file plus timestamp Original lines

Added lines Now you have your patch file, how to you patch an original?

 Call the following from the command line within the directory you wish to patch (or the directory where the file you wish to patch resides): $patch –p1 < changes.patch A few words on the patch command

 The –pN flag is used to manage the number of slashes in the patch file’s path.  If the file inside the patch were /root/next/last then  -p0 would leave it as /root/next/last  -p1 would take the first slash leaving it root/next/last  -p3 would leave the path as “last”  As always, you are encouraged to read up on the man page for more information So what now?

We have gone over the basic tools you require to complete your schemata (with the notable exception of a proper class on debugging – however this shall be rectified next week)  You should know your way about the kernel source as well as how to stuff regardless  You can configure, compile and run  You can patch And here it is: Your moment of zen