SlideShare a Scribd company logo
How to make a pseudo-file
As a follow-up to our first lab, we
examine the steps needed to
create our own ‘/proc’ file
The ‘extensibility’ imperative
• A modern OS needs the ability to evolve
• It will need to support new devices
• It will need to allow ‘bugs’ to be repaired
• It will need to permit performance gains
• Otherwise: suffer early obsolescence!
Two Extensibility Mechanisms
• ‘Open Source’ programming
• ‘Installable’ kernel modules
‘Open Source’
• Source text for the Linux kernel is on disk
(usual directory-location is ‘/usr/src/linux’)
• Majority of the sources are written in ‘C’
(so ‘portable’ across CPU architectures)
• Some are written in assembly languages
(for nearly twenty different architectures)
• Files are grouped into major categories
(kernel, mm, fs, net, ipc, lib, drivers, init)
• You could edit and recompile your kernel
Installable kernel modules
• Great mechanism for kernel ‘extensibility’
• Neat tool for studying how kernel works
• Kernel can be modified while it’s running
• Unnecessary to recompile and then reboot
• But inherently unsafe: programming bugs
in the kernel can cause system crashes!
Some ‘superuser’ privileges
• Since modifying a running kernel is ‘risky’,
only authorized ‘system administrators’ are
normally allowed to install kernel modules
• But our systems are specially configured
to permit you to install or remove modules
• This is purely for ‘educational’ purposes
(so you should use this privilege wisely)
Linux module structure
• Two ‘module administration’ functions are
mandatory components in every module
plus
• Appropriate ‘module service’ functions and
their supporting kernel data-structures are
optional components in particular modules
also
• Recent kernels require a Module License!
A minimal module-template
#include <linux/module.h>
int init_module( void )
{
// code here gets called during module installation
}
void cleanup_module( void )
{
// code here gets called during module removal
}
MODULE_LICENSE(“GPL”);
How to compile a module
• You could directly invoke the C-compiler:
$ gcc –c –O –D__KERNEL__ -DMODULE
–I/lib/modules/2.4.26/build/include mod.c
• OR: you can use the ‘make’ utility:
$ make mod.o
The ‘printk()’ function
• Kernel modules cannot call any functions
in the C runtime library (e.g., ‘printf()’)
• But similar kernel versions of important
functions are provided (e.g., ‘printk()’)
• Syntax and semantics are slightly different
(e.g., priority and message-destination)
• Capabilities may be somewhat restricted
(e.g., printk() can’t show floating-point)
Simple module example
#include <linux/module.h>
int init_module( void )
{
printk( “<1>Hello, world!n” );
return 0; // SUCCESS
}
void cleanup_module( void )
{
printk( “<1>Goodbye everyonen” );
}
MODULE_LICENSE(“GPL”);
How to install and remove
root# /sbin/insmod hello.o
root# /sbin/rmmod hello
A non-trivial module-example
• Let’s see how we can use kernel functions
to create our own ‘/proc’ file
• Easy if we use ‘create_proc_read_entry()’
during module-initialization (and then use
‘remove_proc_entry()’ during cleanup)
• Prototypes in the <linux/proc_fs.h> header
• We’ll show the current value of a volatile
kernel variable, named ‘jiffies’
jiffies
• unsigned long volatile jiffies;
• global kernel variable (used by scheduler)
• Initialized to zero when system reboots
• Gets incremented when timer interrupts
• So it counts ‘clock-ticks’ since cpu restart
• ‘tick-frequency’ is architecture dependent
Writing the ‘jiffies.c’ module
• We need to declare a name for pseudo-file
static char modname[ ] = “jiffies”;
• We need to define a ‘proc_read’ function
(see code on the following slide)
• We need to ‘register’ our filename, along
with its ‘read’ method, in ‘init_module()’
• We need to ‘unregister’ our pseudo-file in
‘cleanup_module()’
Our module’s ‘read’ method
static int
my_proc_read( char *buf, char **start,
off_t off, int count, int *eof, void *data )
{
return sprintf( buf, “jiffies=%lun”, jiffies );
}
Our ‘initialization’ function
int init_module( void )
{
create_proc_read_entry( modname,
0, NULL, my_proc_read, NULL );
return 0;
}
Our ‘cleanup’ function
void cleanup_module( void )
{
remove_proc_entry( modname, NULL );
}
In-class exercise #1
• Use an editor (e.g., ‘vi’) to create a source
file in C (named ‘jiffies.c’) for your module
• Use the ‘make’ command to compile your
module’s source-file into an object-file
• Use the ‘insmod’ command to install your
module object-file into the running kernel
• Use the ‘cat’ command to display ‘jiffies’
• Then use ‘rmmod’ to remove your module
Makefile
• We need it to automate module compiles
• Otherwise we type a VERY long command
• To compile ‘jiffies.c’ type: $ make jiffies.o
• Our Makefile defines some ‘implicit rules’
• ‘make’ works by doing pattern-matching
Rule syntax
targets … : dependencies …
commands
...
Pattern Rule example
CC = gcc
INCLUDE = /lib/modules/2.4.26/build/include
CFLAGS = -c –O
CFLAGS += -D__KERNEL__ -DMODULE
# primary pattern rule
%.o : %.c %.h
$(CC) -I$(INCLUDE) $CFLAGS $<
# fallback pattern rule
%.o : %.c
$(CC) -I$(INCLUDE) $CFLAGS $<
‘Makefile’ is on class website
• You can download the ‘Makefile’ from our
website: http://cs.usfca.edu/~cruse/cs326/
• Put the ‘Makefile’ in your current working
directory (along with your module source)
• Then you can compile by typing this short
command: $ make jiffies.o
In-class exercise #2
• Use your knowledge of standard C library
functions (e.g., open(), read(), close() ) to
write an application-program (name it
‘showjifs.cpp’) which reads the information
from your ‘proc/jiffies’ pseudo-file and then
prints it on the screen
• Use a program-loop to re-read the pseudo
file multiple times
• How rapidly does ‘jiffies’ value increase?

More Related Content

Similar to lesson03.ppt

MattsonTutorialSC14.pptx
MattsonTutorialSC14.pptxMattsonTutorialSC14.pptx
MattsonTutorialSC14.pptx
gopikahari7
 
Kernel module programming
Kernel module programmingKernel module programming
Kernel module programming
Vandana Salve
 
Linux kernel modules
Linux kernel modulesLinux kernel modules
Linux kernel modules
Eddy Reyes
 
NSC #2 - D3 02 - Peter Hlavaty - Attack on the Core
NSC #2 - D3 02 - Peter Hlavaty - Attack on the CoreNSC #2 - D3 02 - Peter Hlavaty - Attack on the Core
NSC #2 - D3 02 - Peter Hlavaty - Attack on the Core
NoSuchCon
 
brief intro to Linux device drivers
brief intro to Linux device driversbrief intro to Linux device drivers
brief intro to Linux device drivers
Alexandre Moreno
 
Introduction to Linux Kernel Development
Introduction to Linux Kernel DevelopmentIntroduction to Linux Kernel Development
Introduction to Linux Kernel Development
Levente Kurusa
 
Introduction Linux Device Drivers
Introduction Linux Device DriversIntroduction Linux Device Drivers
Introduction Linux Device Drivers
NEEVEE Technologies
 
Linux Kernel Programming
Linux Kernel ProgrammingLinux Kernel Programming
Linux Kernel ProgrammingNalin Sharma
 
Packaging perl (LPW2010)
Packaging perl (LPW2010)Packaging perl (LPW2010)
Packaging perl (LPW2010)
p3castro
 
Course 102: Lecture 25: Devices and Device Drivers
Course 102: Lecture 25: Devices and Device Drivers Course 102: Lecture 25: Devices and Device Drivers
Course 102: Lecture 25: Devices and Device Drivers
Ahmed El-Arabawy
 
Linux Kernel Module - For NLKB
Linux Kernel Module - For NLKBLinux Kernel Module - For NLKB
Linux Kernel Module - For NLKB
shimosawa
 
Linux kernel code
Linux kernel codeLinux kernel code
Linux kernel code
Ganesh Naik
 
Intro To Node.js
Intro To Node.jsIntro To Node.js
Intro To Node.js
Chris Cowan
 
Linux Kernel Development
Linux Kernel DevelopmentLinux Kernel Development
Linux Kernel Development
Priyank Kapadia
 
Linux Device Driver’s
Linux Device Driver’sLinux Device Driver’s
Linux Device Driver’s
Rashmi Warghade
 
Linux Char Device Driver
Linux Char Device DriverLinux Char Device Driver
Linux Char Device Driver
Gary Yeh
 
Build your own embedded linux distributions by yocto project
Build your own embedded linux distributions by yocto projectBuild your own embedded linux distributions by yocto project
Build your own embedded linux distributions by yocto project
Yen-Chin Lee
 
Lecture 5 Kernel Development
Lecture 5 Kernel DevelopmentLecture 5 Kernel Development
Lecture 5 Kernel Development
Mohammed Farrag
 
Attack on the Core
Attack on the CoreAttack on the Core
Attack on the Core
Peter Hlavaty
 
OpenCL Programming 101
OpenCL Programming 101OpenCL Programming 101
OpenCL Programming 101
Yoss Cohen
 

Similar to lesson03.ppt (20)

MattsonTutorialSC14.pptx
MattsonTutorialSC14.pptxMattsonTutorialSC14.pptx
MattsonTutorialSC14.pptx
 
Kernel module programming
Kernel module programmingKernel module programming
Kernel module programming
 
Linux kernel modules
Linux kernel modulesLinux kernel modules
Linux kernel modules
 
NSC #2 - D3 02 - Peter Hlavaty - Attack on the Core
NSC #2 - D3 02 - Peter Hlavaty - Attack on the CoreNSC #2 - D3 02 - Peter Hlavaty - Attack on the Core
NSC #2 - D3 02 - Peter Hlavaty - Attack on the Core
 
brief intro to Linux device drivers
brief intro to Linux device driversbrief intro to Linux device drivers
brief intro to Linux device drivers
 
Introduction to Linux Kernel Development
Introduction to Linux Kernel DevelopmentIntroduction to Linux Kernel Development
Introduction to Linux Kernel Development
 
Introduction Linux Device Drivers
Introduction Linux Device DriversIntroduction Linux Device Drivers
Introduction Linux Device Drivers
 
Linux Kernel Programming
Linux Kernel ProgrammingLinux Kernel Programming
Linux Kernel Programming
 
Packaging perl (LPW2010)
Packaging perl (LPW2010)Packaging perl (LPW2010)
Packaging perl (LPW2010)
 
Course 102: Lecture 25: Devices and Device Drivers
Course 102: Lecture 25: Devices and Device Drivers Course 102: Lecture 25: Devices and Device Drivers
Course 102: Lecture 25: Devices and Device Drivers
 
Linux Kernel Module - For NLKB
Linux Kernel Module - For NLKBLinux Kernel Module - For NLKB
Linux Kernel Module - For NLKB
 
Linux kernel code
Linux kernel codeLinux kernel code
Linux kernel code
 
Intro To Node.js
Intro To Node.jsIntro To Node.js
Intro To Node.js
 
Linux Kernel Development
Linux Kernel DevelopmentLinux Kernel Development
Linux Kernel Development
 
Linux Device Driver’s
Linux Device Driver’sLinux Device Driver’s
Linux Device Driver’s
 
Linux Char Device Driver
Linux Char Device DriverLinux Char Device Driver
Linux Char Device Driver
 
Build your own embedded linux distributions by yocto project
Build your own embedded linux distributions by yocto projectBuild your own embedded linux distributions by yocto project
Build your own embedded linux distributions by yocto project
 
Lecture 5 Kernel Development
Lecture 5 Kernel DevelopmentLecture 5 Kernel Development
Lecture 5 Kernel Development
 
Attack on the Core
Attack on the CoreAttack on the Core
Attack on the Core
 
OpenCL Programming 101
OpenCL Programming 101OpenCL Programming 101
OpenCL Programming 101
 

Recently uploaded

Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
The Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptxThe Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptx
DhatriParmar
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
gb193092
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
Wasim Ak
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
Krisztián Száraz
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
kimdan468
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 

Recently uploaded (20)

Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
The Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptxThe Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptx
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 

lesson03.ppt

  • 1. How to make a pseudo-file As a follow-up to our first lab, we examine the steps needed to create our own ‘/proc’ file
  • 2. The ‘extensibility’ imperative • A modern OS needs the ability to evolve • It will need to support new devices • It will need to allow ‘bugs’ to be repaired • It will need to permit performance gains • Otherwise: suffer early obsolescence!
  • 3. Two Extensibility Mechanisms • ‘Open Source’ programming • ‘Installable’ kernel modules
  • 4. ‘Open Source’ • Source text for the Linux kernel is on disk (usual directory-location is ‘/usr/src/linux’) • Majority of the sources are written in ‘C’ (so ‘portable’ across CPU architectures) • Some are written in assembly languages (for nearly twenty different architectures) • Files are grouped into major categories (kernel, mm, fs, net, ipc, lib, drivers, init) • You could edit and recompile your kernel
  • 5. Installable kernel modules • Great mechanism for kernel ‘extensibility’ • Neat tool for studying how kernel works • Kernel can be modified while it’s running • Unnecessary to recompile and then reboot • But inherently unsafe: programming bugs in the kernel can cause system crashes!
  • 6. Some ‘superuser’ privileges • Since modifying a running kernel is ‘risky’, only authorized ‘system administrators’ are normally allowed to install kernel modules • But our systems are specially configured to permit you to install or remove modules • This is purely for ‘educational’ purposes (so you should use this privilege wisely)
  • 7. Linux module structure • Two ‘module administration’ functions are mandatory components in every module plus • Appropriate ‘module service’ functions and their supporting kernel data-structures are optional components in particular modules also • Recent kernels require a Module License!
  • 8. A minimal module-template #include <linux/module.h> int init_module( void ) { // code here gets called during module installation } void cleanup_module( void ) { // code here gets called during module removal } MODULE_LICENSE(“GPL”);
  • 9. How to compile a module • You could directly invoke the C-compiler: $ gcc –c –O –D__KERNEL__ -DMODULE –I/lib/modules/2.4.26/build/include mod.c • OR: you can use the ‘make’ utility: $ make mod.o
  • 10. The ‘printk()’ function • Kernel modules cannot call any functions in the C runtime library (e.g., ‘printf()’) • But similar kernel versions of important functions are provided (e.g., ‘printk()’) • Syntax and semantics are slightly different (e.g., priority and message-destination) • Capabilities may be somewhat restricted (e.g., printk() can’t show floating-point)
  • 11. Simple module example #include <linux/module.h> int init_module( void ) { printk( “<1>Hello, world!n” ); return 0; // SUCCESS } void cleanup_module( void ) { printk( “<1>Goodbye everyonen” ); } MODULE_LICENSE(“GPL”);
  • 12. How to install and remove root# /sbin/insmod hello.o root# /sbin/rmmod hello
  • 13. A non-trivial module-example • Let’s see how we can use kernel functions to create our own ‘/proc’ file • Easy if we use ‘create_proc_read_entry()’ during module-initialization (and then use ‘remove_proc_entry()’ during cleanup) • Prototypes in the <linux/proc_fs.h> header • We’ll show the current value of a volatile kernel variable, named ‘jiffies’
  • 14. jiffies • unsigned long volatile jiffies; • global kernel variable (used by scheduler) • Initialized to zero when system reboots • Gets incremented when timer interrupts • So it counts ‘clock-ticks’ since cpu restart • ‘tick-frequency’ is architecture dependent
  • 15. Writing the ‘jiffies.c’ module • We need to declare a name for pseudo-file static char modname[ ] = “jiffies”; • We need to define a ‘proc_read’ function (see code on the following slide) • We need to ‘register’ our filename, along with its ‘read’ method, in ‘init_module()’ • We need to ‘unregister’ our pseudo-file in ‘cleanup_module()’
  • 16. Our module’s ‘read’ method static int my_proc_read( char *buf, char **start, off_t off, int count, int *eof, void *data ) { return sprintf( buf, “jiffies=%lun”, jiffies ); }
  • 17. Our ‘initialization’ function int init_module( void ) { create_proc_read_entry( modname, 0, NULL, my_proc_read, NULL ); return 0; }
  • 18. Our ‘cleanup’ function void cleanup_module( void ) { remove_proc_entry( modname, NULL ); }
  • 19. In-class exercise #1 • Use an editor (e.g., ‘vi’) to create a source file in C (named ‘jiffies.c’) for your module • Use the ‘make’ command to compile your module’s source-file into an object-file • Use the ‘insmod’ command to install your module object-file into the running kernel • Use the ‘cat’ command to display ‘jiffies’ • Then use ‘rmmod’ to remove your module
  • 20. Makefile • We need it to automate module compiles • Otherwise we type a VERY long command • To compile ‘jiffies.c’ type: $ make jiffies.o • Our Makefile defines some ‘implicit rules’ • ‘make’ works by doing pattern-matching
  • 21. Rule syntax targets … : dependencies … commands ...
  • 22. Pattern Rule example CC = gcc INCLUDE = /lib/modules/2.4.26/build/include CFLAGS = -c –O CFLAGS += -D__KERNEL__ -DMODULE # primary pattern rule %.o : %.c %.h $(CC) -I$(INCLUDE) $CFLAGS $< # fallback pattern rule %.o : %.c $(CC) -I$(INCLUDE) $CFLAGS $<
  • 23. ‘Makefile’ is on class website • You can download the ‘Makefile’ from our website: http://cs.usfca.edu/~cruse/cs326/ • Put the ‘Makefile’ in your current working directory (along with your module source) • Then you can compile by typing this short command: $ make jiffies.o
  • 24. In-class exercise #2 • Use your knowledge of standard C library functions (e.g., open(), read(), close() ) to write an application-program (name it ‘showjifs.cpp’) which reads the information from your ‘proc/jiffies’ pseudo-file and then prints it on the screen • Use a program-loop to re-read the pseudo file multiple times • How rapidly does ‘jiffies’ value increase?