Hi all,
I am a Fedora user and I have a module (just an example) that does not work for the latest kernels (FC6).
The kernel simbol tasklist_lock (sched.h) is not found by the compiler (I searched and found a kernel patch, but I do not want to recompile the kernel) .
What is the adviced way to do this working?
Thanks for your help,
V.D.
The module is:
/* ==================================
pidstate.c - How to show the task state for a given PID.
Compile with this:
1) Write a file named Makefile containing only this line:
obj-m += pidstate.o
(no spaces allowed before obj-m!!!)
2) Issue the following command:
make -C /lib/modules/`uname -r`/build SUBDIRS=$PWD modules
Then, load the module with (example):
insmod ./pidstate.ko my_target_pid=2718
Finaly, remove the module:
rmmod pidstate
Warning: you need the kernel source installed.
Tested using kernel 2.6.16 and mot working with 2.6.18 - 2.6.20
*/
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/stat.h>
#include <linux/sched.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Cristian Sava");
MODULE_DESCRIPTION("Show the task state for a given PID");
static int my_target_pid = 0;
static struct task_struct *my_target_task;
/*
* module_param(foo, int, 0000)
* The first param is the parameters name
* The second param is it's data type
* The final argument is the permissions bits,
* for exposing parameters in sysfs (if non-zero) at a later stage.
*/
module_param(my_target_pid, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
MODULE_PARM_DESC(my_target_pid, "The PID must be an integer");
/*
* The task state array is a strange "bitmap" of reasons to sleep.
* Thus "running" is zero, and you can test for combinations of
* others with simple bit tests.
* (this is from fs/proc/array.c - because we do not have access there)
*/
static const char *task_state_array[] = {
"R (running)", /* 0 */
"S (sleeping)", /* 1 */
"D (disk sleep)", /* 2 */
"T (stopped)", /* 4 */
"T (tracing stop)", /* 8 */
"Z (zombie)", /* 16 */
"X (dead)" /* 32 */
};
/* This is from fs/proc/array.c */
static inline const char * get_task_state(struct task_struct *tsk)
{
unsigned int state = (tsk->state & (TASK_RUNNING |
TASK_INTERRUPTIBLE |
TASK_UNINTERRUPTIBLE |
TASK_STOPPED |
TASK_TRACED)) |
(tsk->exit_state & (EXIT_ZOMBIE |
EXIT_DEAD));
const char **p = &task_state_array[0];
while (state) {
p++;
state >>= 1;
}
return *p;
}
static int __init pidstate_init(void)
{
printk(KERN_ALERT "Hello, pidstate!\n================\n");
printk(KERN_ALERT "my_target_pid is an integer: %d\n", my_target_pid);
/*
* First we need to lock the task list for reading
*/
read_lock(&tasklist_lock);
/*
* Each process has a task_struct (defined in linux/sched.h)
* Using the PID we get the task_struct for it
*/
my_target_task = find_task_by_pid(my_target_pid);
/*
* Only if the PID exists (we test if we really have my_target_task)
* we will use pid_alive to find out if the process is alive
* and we show the task state using get_task_state
*/
if (my_target_task)
{
if (pid_alive(my_target_task))
printk(KERN_ALERT "my_target_pid: %d is alive \n", my_target_pid);
printk(KERN_ALERT "State: %s \n", get_task_state(my_target_task));
}
/*
* Now we unlock the task list
*/
read_unlock(&tasklist_lock);
return 0;
}
static void __exit pidstate_exit(void)
{
printk(KERN_ALERT "Goodbye, pidstate\n");
}
module_init(pidstate_init);
module_exit(pidstate_exit);
I am a Fedora user and I have a module (just an example) that does not work for the latest kernels (FC6).
The kernel simbol tasklist_lock (sched.h) is not found by the compiler (I searched and found a kernel patch, but I do not want to recompile the kernel) .
What is the adviced way to do this working?
Thanks for your help,
V.D.
The module is:
/* ==================================
pidstate.c - How to show the task state for a given PID.
Compile with this:
1) Write a file named Makefile containing only this line:
obj-m += pidstate.o
(no spaces allowed before obj-m!!!)
2) Issue the following command:
make -C /lib/modules/`uname -r`/build SUBDIRS=$PWD modules
Then, load the module with (example):
insmod ./pidstate.ko my_target_pid=2718
Finaly, remove the module:
rmmod pidstate
Warning: you need the kernel source installed.
Tested using kernel 2.6.16 and mot working with 2.6.18 - 2.6.20
*/
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/stat.h>
#include <linux/sched.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Cristian Sava");
MODULE_DESCRIPTION("Show the task state for a given PID");
static int my_target_pid = 0;
static struct task_struct *my_target_task;
/*
* module_param(foo, int, 0000)
* The first param is the parameters name
* The second param is it's data type
* The final argument is the permissions bits,
* for exposing parameters in sysfs (if non-zero) at a later stage.
*/
module_param(my_target_pid, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
MODULE_PARM_DESC(my_target_pid, "The PID must be an integer");
/*
* The task state array is a strange "bitmap" of reasons to sleep.
* Thus "running" is zero, and you can test for combinations of
* others with simple bit tests.
* (this is from fs/proc/array.c - because we do not have access there)
*/
static const char *task_state_array[] = {
"R (running)", /* 0 */
"S (sleeping)", /* 1 */
"D (disk sleep)", /* 2 */
"T (stopped)", /* 4 */
"T (tracing stop)", /* 8 */
"Z (zombie)", /* 16 */
"X (dead)" /* 32 */
};
/* This is from fs/proc/array.c */
static inline const char * get_task_state(struct task_struct *tsk)
{
unsigned int state = (tsk->state & (TASK_RUNNING |
TASK_INTERRUPTIBLE |
TASK_UNINTERRUPTIBLE |
TASK_STOPPED |
TASK_TRACED)) |
(tsk->exit_state & (EXIT_ZOMBIE |
EXIT_DEAD));
const char **p = &task_state_array[0];
while (state) {
p++;
state >>= 1;
}
return *p;
}
static int __init pidstate_init(void)
{
printk(KERN_ALERT "Hello, pidstate!\n================\n");
printk(KERN_ALERT "my_target_pid is an integer: %d\n", my_target_pid);
/*
* First we need to lock the task list for reading
*/
read_lock(&tasklist_lock);
/*
* Each process has a task_struct (defined in linux/sched.h)
* Using the PID we get the task_struct for it
*/
my_target_task = find_task_by_pid(my_target_pid);
/*
* Only if the PID exists (we test if we really have my_target_task)
* we will use pid_alive to find out if the process is alive
* and we show the task state using get_task_state
*/
if (my_target_task)
{
if (pid_alive(my_target_task))
printk(KERN_ALERT "my_target_pid: %d is alive \n", my_target_pid);
printk(KERN_ALERT "State: %s \n", get_task_state(my_target_task));
}
/*
* Now we unlock the task list
*/
read_unlock(&tasklist_lock);
return 0;
}
static void __exit pidstate_exit(void)
{
printk(KERN_ALERT "Goodbye, pidstate\n");
}
module_init(pidstate_init);
module_exit(pidstate_exit);
Ahhh...imagining that irresistible "new car" smell?
Check out new cars at Yahoo! Autos.