Una función para conseguir el pid de un proceso según su nombre(bastante optimizada). Con la opción de crear una zona de exclusión mutua primitiva con lo son los spinlocks.
/*
* ************************************************
* Autor: NvK
* Descripción: Obtener el pid de un proceso en el kernel según su nombre.
* Fecha: 18-1-2014
* ************************************************
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/sched.h>
static uint32_t __attribute__((__section__(".text.init"))) get_kern_pid(const char *proc_name, bool *_lock)
{
struct task_struct *kern_ts;
unsigned int pid;
kern_ts= &init_task;
if(likely(_lock))spin_lock(&kern_ts->alloc_lock);
&((*(struct task_struct *)0)).comm;
asm __volatile__( "":"=r"(pid):"r"(0x000));
for(;(kern_ts=next_task(kern_ts))!=&init_task;){
if(strstr(kern_ts->comm, proc_name))
pid= (*kern_ts).pid;
}
if(likely(_lock))spin_unlock(&kern_ts->alloc_lock);
return(pid);
}
ejemplo de uso:
static int __section(.init.text) __cold notrace INIT_KERNEL(void)
{
printk("process pid: %d \n", get_kern_pid("bash", true)); /* crear la zona de exclusión */
return 0;
}