hello all,
Can anybody tell me how to obtain process name if we have pid , in a kernel module??
I have tried the way of reading /proc/pid/status, But is there any other way to do so,, is it possible to read proces name from kernel data structures( like task_struct) ??
And i also want to know that ,
is it possible to open a file in kernel mode (kernel module) if so then which functions should be used to open file and read its contents??
Please reply..
Thanks in advance,,
It is not clear to me what yo
It is not clear to me what you want.
A process ID is unique. A process "name" not (in fact there is
no such thing as the "process name"). You can
have 10 people issuing a "ls" command at the same time. All
these will have different PIDs but the same process "name".
What you want is probably what the name of the command was
(as entered by the user on the shell command line).
But that one is easy:
int main( int argc, char *argv[]) { printf( "My name is: %s\n", argv[0]) return 0; }Greetings
AC
Sorry, "but that one is easy"
Sorry, "but that one is easy" in user space.
I do not know if it is possible inside a kernel module.
AC
task_struct defined in includ
task_struct defined in include/linux/sched.h have char comm[TASK_COMM_LEN] as in 2.6.18 kernel, hope this helps.
ps: but what for do you need process name anyway in your driver ?
ps2: /proc/$pid/ files can be somesort usefull
THANKS
Thanks mator ,,
it worked with comm[TASK_COMM_LEN] this field. thanks a lot for your help..
how u use comm[TASK_COMM_LEN]?
i'm using linux 2.6.8
also hav to print out the process name, and i oredi get the pid,
how u use "comm[TASK_COMM_LEN]" to get process name?
printk("%c",comm[TASK_COMM_LEN]);
can't work...in sched.c
You need to print the first c
You need to print the first character of the task name,errr?
Why don't you use %s instead? :)
Because he's getting into ker
Because he's getting into kernel programming before learning C?
On systems with procfs
On systems with procfs you can always check content of /proc/$PID/cmdline
I know no other way. Linux keeps only that info and procfs is only way I know to access that info.
File IO from kernel space
> is it possible to open a file in kernel mode (kernel module)
> if so then which functions should be used to open file and
> read its contents??
No, it is generally impossible. Under Linux you do not really read file, you map it into cache and cause page fault to load its content into memory/cache. Kernel doesn't allow kernel itself to produce page faults.
One of the possible ways is thru helper application. (Do not have copy of kernel at hand - can't recall function name.) Module can ask kernel to spawn a process. Then the application would read file you need and with e.g. ioctls() would transfer its content into kernel space.
> Kernel doesn't allow kernel
> Kernel doesn't allow kernel itself to produce page faults.
Maybe digressing from the topic but why is that so???
time...
cause OS must work far quicker. It doesn't have that much time to spare.
FILE I/O From User Space
It is _not_ impossible, just stupid. You can
use filp_open(), filp_close, and kernel_read().
Writing is more difficult but still possible.
But the original questioner obviously is
pretty clueless and is just asking for trouble
doing this. There are a large variety of better ways to do whatever he is trying to do.
I hardly ever venture into
I hardly ever venture into the kernel discussion boards because of posts like this one:
But the original questioner obviously is
pretty clueless and is just asking for trouble
doing this. There are a large variety of better ways to do whatever he is trying to do.
How is this even remotely helpful to the community? I understand it must get irritating hearing newbie questions all day long, but there is no need for taking cheap shots at interested, motivated individuals. I agree that the enthusiast who started this thread is doing something sub-optimal, but, hey, we all have to make mistakes to learn.
So for heavens sake, once you've pointed out that the guy is making a bad choice in his approach, try pointing him in the right direction.
Get process name (cmdline) from pid?
Is there anyway to do this in user space in GNU/Linux apart from reading /proc/pid/cmdline? HP-UX has pstat, but I can't find anything comparable.
find_task_by_pid(pid)
ok
you have the pid
pid_t var;
what you can do is
Use find_task_by_pid(pid) which returns a pointer to the task_struct. The comm field of the task_struct gives the name of the process.
struct task_struct *my;
my = find_task_by_id(pid)
printf("%s",my->comm); //print f or whater you want to do with it
getting process name from process id
in userspace iam not able to get process name using find_task_by_id(pid). can any one point me how to get the process name from process id in user space.
man ps
After you have read "man ps", you might also read "man tr" "man proc" and "man bash". You can also avoid waiting for replies to queries on this site by reading "man man", and trying "man -k xrated".
observe how i get the name
observe how i get the name of a program given its PID
[bash]$ ps -aux
...
user 23453 0.0 0.0 2356 780 tty2 S 12:18 0:00 gnome-pty-helper
...
[bash]$ cat /proc/23453/cmdline
gnome-pty-helper