Project: current System Variable


This chapter explored the task_struct and current, which is the system variable that points to the currently executing task's task_struct. This project's goal is to reinforce the idea that the kernel is an orderly yet ever-changing series of linked structures that are created and destroyed as programs run. As we have seen, the task_struct structure is one of the most important structures the kernel owns, in that it has all the information needed for any given task. This project module accesses that structure just as the kernel does and serves as a base for future explorations for the reader.

In this project, we access the current task_struct and print various elements from that structure. Starting with the file include/linux/sched.h, we find the task_struct. Using sched.h, we reference current->pid and current->comm., the current process ID and name, and follow these structures to the parent's pid and comm. Next, we borrow a routine from printk() and send a message back to the current tty terminal we are using.

See the following code:

NOTE

From running the first program (hellomod), what do you think the name of the current process will be when the initialization routine prints out current->comm? What will be the name of the parent process? See the following code discussion.


Project Source Code[8]

[8] You can use the project source as a starting point to explore the running kernel. Although the kernel has many useful routines to view, such as its internals (for example, strace()), building your own tools, such as this project, sheds light on the real-time aspects of the Linux kernel.

[View full width]

----------------------------------------------------------------------- currentptr.c 001 #include <linux/module.h> 002 #include <linux//kernel.h> 003 #include <linux/init.h> 004 #include <linux/sched.h> 005 #include <linux/tty.h> 006 007 void tty_write_message1(struct tty_struct *, char *); 008 009 static int my_init( void ) 010 { 011 012 char *msg="Hello tty!"; 013 014 printk("Hello, from the kernel...\n"); 015 printk("parent pid =%d(%s)\n",current->parent->pid ,current->parent->comm); 016 printk("current pid =%d(%s)\n",current->pid,current->comm); 017 018 tty_write_message1(current->signal->tty,msg); 019 return 0; 020 } 022 static void my_cleanup( void ) { printk("Goodbye, from the kernel...\n"); } 027 module_init(my_init); 028 module_exit(my_cleanup); // This routine was borrowed from <printk.c> 032 void tty_write_message1(struct tty_struct *tty, char *msg) { if (tty && tty->driver->write) tty->driver->write(tty, 0, msg, strlen(msg)); return; 037 } -----------------------------------------------------------------------

Line 4

sched.h contains struct task_struct {}, which is where we reference the process ID (->pid), and the name of the current task (->comm.), as well as the pointer to the parent PID (->parent), which references the parent task structure. We also find a pointer to the signal structure, which contains a reference to the tty structure (see lines 1822).

Line 5

tty.h contains struct tty_struct {}, which is used by the routine we borrowed from printk.c (see lines 3237).

Line 12

This is the simple message string we want to send back to our terminal.

Line 15

Here, we reference the parent PID and its name from our current task structure. The answer to the previous question is that the parent of our task is the current shell program; in this case, it was Bash.

Line 16

Here, we reference the current PID and its name from our current task structure. To answer the other half of the previous question, we entered insmod on the Bash command line and that is printed out as the current process.

Line 18

This is a function borrowed from kernel/printk.c. It is used to redirect messages to a specific tty. To illustrate our current point, we pass this routine the tty_struct of the tty (window or command line) from where we invoke our program. This is referenced by way of current->signal->tty. The msg parm is the string we declared on line 12.

Lines 3238

The tty write function checks that the tty exists and then calls the appropriate device driver with the message.

Running the Code

Compile and insmod() the code as in the first project.




The Linux Kernel Primer. A Top-Down Approach for x86 and PowerPC Architectures
The Linux Kernel Primer. A Top-Down Approach for x86 and PowerPC Architectures
ISBN: 131181637
EAN: N/A
Year: 2005
Pages: 134

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net