Linux gets process name from pid inside kernel

hi i used sys_getpid () from the kernel to get the process id how to find the process name from the kernel structure? Does it exist in the core?

Thank you very much

+4
source share
4 answers

struct task_struct contains the comm member, it contains the executable name excluding path .

Get the current macro from this file , you will get the name of the program that started the current process (as in insmod / modprobe).

Using the above information, you can use the title information.

+13
source

My kernel module loads "modprobe -v my_module --allow-unsupported -o some-data" and I retrieve the "some data" parameter. The following code gave me the entire command line, and here is how I analyzed the parameter of interest:

 struct mm_struct *mm; unsigned char x, cmdlen; mm = get_task_mm(current); down_read(&mm->mmap_sem); cmdlen = mm->arg_end - mm->arg_start; for(x=0; x<cmdlen; x++) { if(*(unsigned char *)(mm->arg_start + x) == '-' && *(unsigned char *)(mm->arg_start + (x+1)) == 'o') { break; } } up_read(&mm->mmap_sem); if(x == cmdlen) { printk(KERN_ERR "inject: ERROR - no target specified\n"); return -EINVAL; } strcpy(target,(unsigned char *)(mm->arg_start + (x+3))); 

"target" contains the line after the -o option. You can compress this somewhat - the caller (in this case modprobe) will be the first line in mm-> arg_start - according to your needs.

+1
source

Not sure, but find_task_by_pid_ns might be useful.

0
source

you can see special files in /proc/<pid>/

For example, /proc/<pid>/exe is a symbolic link pointing to the actual binary.

/proc/<pid>/cmdline is a null command line list, so the first word is the name of the process.

0
source

All Articles