Sie sind auf Seite 1von 4

8/14/13

Write For Us Submit Tips

Device Drivers, Part 16: Kernel Window -- Peeping through /proc - LINUX For You
Subscribe to Print Edition Search

HOME

REVIEWS

HOW-TOS

CODING

INTERVIEWS

FEATURES

OVERVIEW

BLOGS

SERIES

IT ADMIN

Device Drivers, Part 16: Kernel Window Peeping through /proc


By Anil Kumar Pugalia on March 26, 2012 in Coding, Developers 1 Comment

Search for:

Search

This article, which is part of the series on Linux device drivers, demonstrates the creation and usage of files under the /proc virtual filesystem.
After many months, Shweta and Pugs got together for some peaceful technical romancing. All through, they had been using all kinds of kernel windows, especially through the / p r o cvirtual filesystem (using c a t ), to help them decode various details of Linux device drivers. Heres a non-exhaustive summary listing:
/ p r o c / m o d u l e s dynamically loaded modules / p r o c / d e v i c e s registered character and block major numbers / p r o c / i o m e m on-system physical RAM and bus device addresses / p r o c / i o p o r t s on-system I/O port addresses (especially for x86 systems) / p r o c / i n t e r r u p t s registered interrupt request numbers / p r o c / s o f t i r q s registered soft IRQs / p r o c / k a l l s y m s running kernel symbols, including from loaded modules / p r o c / p a r t i t i o n s currently connected block devices and their partitions / p r o c / f i l e s y s t e m s currently active filesystem drivers / p r o c / s w a p s currently active swaps / p r o c / c p u i n f o information about the CPU(s) on the system / p r o c / m e m i n f o information about the memory on the system, viz., RAM, swap,

Get Connected RSS Feed Twitter

Custom kernel windows


Yes, these have been really helpful in understanding and debugging Linux device drivers. But is it possible for us to also provide some help? Yes, I mean can we create one such kernel window through / p r o c ? asked Shweta. Why just one? You can have as many as you want. And its simple just use the right set of APIs, and there you go. For you, everything is simple, Shweta grumbled.

LINUX For You on


No yaar, this is seriously simple, smiled Pugs. Just watch me creating one for you, he added. And in a jiffy, Pugs created the p r o c _ w i n d o w . cfile below:
1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 # i n c l u d e< l i n u x / m o d u l e . h > # i n c l u d e< l i n u x / k e r n e l . h > # i n c l u d e< l i n u x / p r o c _ f s . h > # i n c l u d e< l i n u x / j i f f i e s . h > s t a t i cs t r u c tp r o c _ d i r _ e n t r y* p a r e n t ,* f i l e ,* l i n k ; s t a t i ci n ts t a t e=0 ; i n tt i m e _ r e a d ( c h a r* p a g e ,c h a r* * s t a r t ,o f f _ to f f ,i n tc o u n t ,i n t* e o f ,v o i d* d a t a ){ i n tl e n ,v a l ; u n s i g n e dl o n ga c t _ j i f f i e s ; l e n=s p r i n t f ( p a g e ," s t a t e=% d \ n " ,s t a t e ) ; a c t _ j i f f i e s=j i f f i e s-I N I T I A L _ J I F F I E S ; v a l=j i f f i e s _ t o _ m s e c s ( a c t _ j i f f i e s ) ; s w i t c h( s t a t e ){ c a s e0 :

Follow

+2,494

www.linuxforu.com/2012/03/device-drivers-kernel-window-peeping-through-proc/

1/4

8/14/13
1 8 1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 3 0 3 1 3 2 3 3 3 4 3 5 3 6 3 7 3 8 3 9 4 0 4 1 4 2 4 3 4 4 4 5 4 6 4 7 4 8 4 9 5 0 5 1 5 2 5 3 5 4 5 5 5 6 5 7 5 8 5 9 6 0 6 1 6 2 6 3 6 4 6 5 6 6 6 7 6 8 6 9 7 0 7 1 7 2 7 3 7 4 7 5 7 6 7 7 7 8 7 9 8 0 8 1 8 2

Device Drivers, Part 16: Kernel Window -- Peeping through /proc - LINUX For You
l e n+ =s p r i n t f ( p a g e+l e n ," t i m e=% l dj i f f i e s \ n " ,a c t _ j i f f i e s ) ; b r e a k ; c a s e1 : l e n+ =s p r i n t f ( p a g e+l e n ," t i m e=% dm s e c s \ n " ,v a l ) ; b r e a k ; c a s e2 : l e n+ =s p r i n t f ( p a g e+l e n ," t i m e=% d s% d m s \ n " , v a l/1 0 0 0 ,v a l%1 0 0 0 ) ; b r e a k ; c a s e3 : v a l/ =1 0 0 0 ; l e n+ =s p r i n t f ( p a g e+l e n ," t i m e=% 0 2 d : % 0 2 d : % 0 2 d \ n " , v a l/3 6 0 0 ,( v a l/6 0 )%6 0 ,v a l%6 0 ) ; b r e a k ; d e f a u l t : l e n+ =s p r i n t f ( p a g e+l e n ," < n o ti m p l e m e n t e d > \ n " ) ; b r e a k ;
Find us on Facebook

Open Source For You


Like 252,617 people like Open Source For You.

} l e n+ =s p r i n t f ( p a g e+l e n ," { o f f s e t=% l d ;c o u n t=% d ; } \ n " ,o f f ,c o u n t ) ; } i n tt i m e _ w r i t e ( s t r u c tf i l e* f i l e ,c o n s tc h a r_ _ u s e r* b u f f e r ,u n s i g n e dl o n gc o u n t , i f( c o u n t>2 ) r e t u r nc o u n t ; i f( ( c o u n t= =2 )& &( b u f f e r [ 1 ]! =' \ n ' ) ) r e t u r nc o u n t ; i f( ( b u f f e r [ 0 ]<' 0 ' )| |( ' 9 '<b u f f e r [ 0 ] ) ) r e t u r nc o u n t ; s t a t e=b u f f e r [ 0 ]-' 0 ' ; r e t u r nc o u n t ; } s t a t i ci n t_ _ i n i tp r o c _ w i n _ i n i t ( v o i d ){ i f( ( p a r e n t=p r o c _ m k d i r ( " a n i l " ,N U L L ) )= =N U L L ){ r e t u r n1 ; } i f( ( f i l e=c r e a t e _ p r o c _ e n t r y ( " r e l _ t i m e " ,0 6 6 6 ,p a r e n t ) )= =N U L L ){ r e m o v e _ p r o c _ e n t r y ( " a n i l " ,N U L L ) ; r e t u r n1 ; } f i l e > r e a d _ p r o c=t i m e _ r e a d ; f i l e > w r i t e _ p r o c=t i m e _ w r i t e ; i f( ( l i n k=p r o c _ s y m l i n k ( " r e l _ t i m e _ l " ,p a r e n t ," r e l _ t i m e " ) )= =N U L L ){ r e m o v e _ p r o c _ e n t r y ( " r e l _ t i m e " ,p a r e n t ) ; r e m o v e _ p r o c _ e n t r y ( " a n i l " ,N U L L ) ; r e t u r n1 ; } l i n k > u i d=0 ; l i n k > g i d=1 0 0 ; r e t u r n0 ; } s t a t i cv o i d_ _ e x i tp r o c _ w i n _ e x i t ( v o i d ){ r e m o v e _ p r o c _ e n t r y ( " r e l _ t i m e _ l " ,p a r e n t ) ; r e m o v e _ p r o c _ e n t r y ( " r e l _ t i m e " ,p a r e n t ) ; r e m o v e _ p r o c _ e n t r y ( " a n i l " ,N U L L ) ; } m o d u l e _ i n i t ( p r o c _ w i n _ i n i t ) ; m o d u l e _ e x i t ( p r o c _ w i n _ e x i t ) ; M O D U L E _ L I C E N S E ( " G P L " ) ; M O D U L E _ A U T H O R ( " A n i lK u m a rP u g a l i a< e m a i l _ a t _ s a r i k a p u g s _ d o t _ c o m > " ) ; M O D U L E _ D E S C R I P T I O N ( " K e r n e lw i n d o w/ p r o cD e m o n s t r a t i o nD r i v e r " ) ; r e t u r nl e n ;

F acebook social plugin

Popular

Comments

Tag cloud

May 6, 2013 6 Comments Priyanka Sarkar

PHP Development: A Smart Career Move


August 13, 2013 5 Comments Diksha P Gupta

India has immense under-utilised talent in the cloud security space


June 20, 2013 3 Comments Priyanka Sarkar

What it Takes to be an Open Source Expert


June 20, 2013 2 Comments sophie-samuel

New and amazing features of Linux


May 6, 2013 1 Comments Deepti Sharma

A Simple guide to building your own Linux Kernel

And then Pugs did the following: Built the driver file (p r o c _ w i n d o w . k o ) using the usual drivers M a k e f i l e . Loaded the driver using i n s m o d . Showed various experiments using the newly created proc windows. (Refer to Figure 1.) And finally, unloaded the driver using r m m o d .

Figure 1: Peeping through /proc

Demystifying the details


www.linuxforu.com/2012/03/device-drivers-kernel-window-peeping-through-proc/ 2/4

8/14/13

Device Drivers, Part 16: Kernel Window -- Peeping through /proc - LINUX For You

Starting from the constructor p r o c _ w i n _ i n i t ( ) , three proc entries have been created: Directory a n i lunder / p r o c(i.e., NULL parent) with default permissions 0755, using
p r o c _ m k d i r ( )

Regular file r e l _ t i m ein the above directory, with permissions 0666, using
c r e a t e _ p r o c _ e n t r y ( )

Soft link r e l _ t i m e _ lto the file r e l _ t i m e , in the same directory, using p r o c _ s y m l i n k ( ) The corresponding removal of these is done with r e m o v e _ p r o c _ e n t r y ( )in the destructor, p r o c _ w i n _ e x i t ( ) , in chronological reverse order. For every entry created under / p r o c , a corresponding s t r u c tp r o c _ d i r _ e n t r yis created. For each, many of its fields could be further updated as needed: mode Permissions of the file uid User ID of the file gid Group ID of the file Additionally, for a regular file, the following two function pointers for reading and writing over the file could be provided, respectively:
i n t( * r e a d _ p r o c ) ( c h a r* p a g e ,c h a r* * s t a r t ,o f f _ to f f ,i n tc o u n t ,i n t* e o f , v o i d* d a t a ) i n t( * w r i t e _ p r o c ) ( s t r u c tf i l e* f i l e ,c o n s tc h a r_ _ u s e r* b u f f e r ,u n s i g n e d l o n gc o u n t ,v o i d* d a t a ) w r i t e _ p r o c ( )is very similar to the character drivers file operation w r i t e ( ) . The above

implementation lets the user write a digit from 0 to 9, and accordingly sets the internal state.
r e a d _ p r o c ( )in the above implementation provides the current state, and the time since the

system has been booted up in different units, based on the current state. These are jiffies in state 0; milliseconds in state 1; seconds and milliseconds in state 2; hours, minutes and seconds in state 3; and <not implemented> in other states. And to check the computation accuracy, Figure 2 highlights the system uptime in the output of top. r e a d _ p r o c s page parameter is a page-sized buffer, typically to be filled up with count bytes from offset off. But more often than not (because of less content), just the page is filled up, ignoring all other parameters.

Figure 2: Comparison w ith tops output

All the / p r o c -related structure definitions and function declarations are available through
< l i n u x / p r o c _ f s . h > . The jiffies-related function declarations and macro definitions are in < l i n u x / j i f f i e s . h > . As a special note, the actual jiffies are calculated by subtracting I N I T I A L _ J I F F I E S , since on boot-up, jiffies is initialised to I N I T I A L _ J I F F I E Sinstead of zero.

Summing up
Hey Pugs! Why did you set the folder name to a n i l ? Who is this Anil? You could have used my name, or maybe yours, suggested Shweta. Ha! Thats a surprise. My real name is Anil; its just that everyone in college knows me as Pugs, smiled Pugs. Watch out for further technical romancing from Pugs a.k.a Anil.

Related Posts:
www.linuxforu.com/2012/03/device-drivers-kernel-window-peeping-through-proc/ 3/4

8/14/13

Device Drivers, Part 16: Kernel Window -- Peeping through /proc - LINUX For You

Device Drivers, Part 17: Module Interactions Debugging the Linux Kernel with debugfs Device Drivers, Part 5: Character Device Files Creation & Operations Device Drivers, Part 6: Decoding Character Device File Operations Working with MTD Devices
Tags: device drivers, irqs, jiffy, kernel development, kernel modules, kernel window, LFY March 2012, Linux, linux device drivers, Linux Device Drivers Series, memory, proc, proc filesystem, Procfs, struct, virtual filesystem

Article written by:


Anil Kumar Pugalia
The author is a freelance trainer in Linux internals, Linux device drivers, embedded Linux and related topics. Prior to this, he had worked at Intel and Nvidia. He has been exploring Linux since 1994. A gold medallist from the Indian Institute of Science, Linux and knowledge-sharing are two of his many passions. Connect with him: Website - Twitter - Facebook - Google+

Previous Post

Next Post

Open Source Robotics Software Platforms

Exploring Software: A Software Engineer's Toolkit

Reviews

How-Tos

Coding

Interviews

Features

Overview

Blogs

Search
Popular tags
Linux , ubuntu, Java, MySQL, Google, python, Fedora, Android, PHP, C, html, w eb applications , India, Microsoft, unix , Window s , Red Hat, Oracle, Security , Apache, xml, LFY April 2012, FOSS, GNOME, http, JavaScript, LFY June 2011, open source, RAM, operating systems

For You & Me Developers Sysadmins Open Gurus CXOs Columns

All published articles are released under Creative Commons Attribution-NonCommercial 3.0 Unported License, unless otherw ise noted. LINUX For You is pow ered by WordPress, w hich gladly sits on top of a CentOS-based LEMP stack.

www.linuxforu.com/2012/03/device-drivers-kernel-window-peeping-through-proc/

4/4

Das könnte Ihnen auch gefallen