Cambiar el PID en el Process Descriptor

Language / Lenguaje:

(Para entender lo que voy a explicar a continuación recomiendo leer Understanding The Linux Kernel (3º ED)):

Ahora que tengo algo de tiempo para dedicar al EnyeLKM, voy a explicar como cambiar el PID a un proceso de manera sencilla.

El método que se usa es recorrer el descriptor de procesos con la macro for_each_process para encontrar el correspondiente con un PID X que será cambiado por otro.

(Recomiendo documentarse sobre: task struct).

LKM para cambiar un PID de un proceso:

/*
Proyecto : EnyeLKM priv8 version
Modulo : Swapper DEMO (Fistconference Version)
Autor : David Reguera Garcia
-
Update : 28 Noviembre 2006
-
The spinlocks has been removed in this POC :P
*/

#include “Swapper.h”

static int pid_to_find = 0;
static int pid_to_change = 0;

#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,47)
MODULE_PARM( pid_to_find , “i” );
MODULE_PARM( pid_to_change , “i” );
#else
module_param( pid_to_find , int, S_IRUGO );
module_param( pid_to_change , int, S_IRUGO );
#endif

int init_module( void )
{
struct task_struct * actual_task;

if ( ( pid_to_find == 0 ) || ( pid_to_change == 0 ) )
{
printk( KERN_INFO “Swapper Syntax: pid_to_find=pid pid_to_change=pid.\n” );
return -1;
}

for_each_process( actual_task )
{
if ( actual_task->pid == pid_to_find )
{
printk( KERN_INFO “Found process: %d.\n”, actual_task->pid );
actual_task->pid = pid_to_change;
printk( KERN_INFO “Change to: %d.\n”, actual_task->pid );

return 0;
}
}

printk( KERN_INFO “Not found process: %d.\n”, pid_to_find );

return -1;
}

void cleanup_module( void )
{
printk( KERN_INFO “Swapper descargado\n” );
}

/* EOF */
Código (LKMs) con scripts para facilitar el trabajo:

swapping_and_list_pids_from_pd.tar.gz


Posted by David Reguera Garcia

Leave a Reply

You must be logged in to post a comment.