b123coder
asked on
read file from running kernel
can it be possible for a running kernel to read a file from a user space and
update its contents as it does for /proc file system?
i know it required to modify a kernel source? i dont know functions that can
be used to do this task? anybody knows it?
update its contents as it does for /proc file system?
i know it required to modify a kernel source? i dont know functions that can
be used to do this task? anybody knows it?
ASKER
hi stefan73,
where can i get those kernel sided files via VFS resources. i search on google but not got good web page.
where can i get those kernel sided files via VFS resources. i search on google but not got good web page.
I asked similar question and didn't receive very specific answer:
https://www.experts-exchange.com/questions/20878804/Open-File-and-mmap-from-driver-layer.html
However, from my personal experience I would suggest you to use:
kernel_read() call and to write yourself similar kernel_write() call.
It is not good from Linux architecture point of view but it works.
Source of kernel_read() call can be found in one of the kernel files: I think readwrite.c...
https://www.experts-exchange.com/questions/20878804/Open-File-and-mmap-from-driver-layer.html
However, from my personal experience I would suggest you to use:
kernel_read() call and to write yourself similar kernel_write() call.
It is not good from Linux architecture point of view but it works.
Source of kernel_read() call can be found in one of the kernel files: I think readwrite.c...
ASKER
hi dimitry
i want to know that can put_user() and get_user() functions also does same task? i found some articales in google but unable to get sample examples of how to implemwnt it?
i want to know that can put_user() and get_user() functions also does same task? i found some articales in google but unable to get sample examples of how to implemwnt it?
No, as far as I understand, put_user() and get_user() allows you to copy from driver's buffer to user's and back. It doesn't have any relation to files.
However maybe it is the way to solve your problem. But I'll suggest you to use copy_to_user() and copy_from_user().
http://www.gelato.unsw.edu.au/~dsw/public-files/kernel-docs/kernel-api/r4037.html
http://www.gelato.unsw.edu.au/~dsw/public-files/kernel-docs/kernel-api/r4081.html
My problem was that I needed to access files from the driver. If you need something else, please tell and maybe we can find different solution.
However maybe it is the way to solve your problem. But I'll suggest you to use copy_to_user() and copy_from_user().
http://www.gelato.unsw.edu.au/~dsw/public-files/kernel-docs/kernel-api/r4037.html
http://www.gelato.unsw.edu.au/~dsw/public-files/kernel-docs/kernel-api/r4081.html
My problem was that I needed to access files from the driver. If you need something else, please tell and maybe we can find different solution.
hi,
take a look on these threads,
http://www.uwsg.indiana.edu/hypermail/linux/kernel/0005.3/0061.html
http://www.uwsg.indiana.edu/hypermail/linux/kernel/0005.3/0438.html
I dont think it is possible.
take a look on these threads,
http://www.uwsg.indiana.edu/hypermail/linux/kernel/0005.3/0061.html
http://www.uwsg.indiana.edu/hypermail/linux/kernel/0005.3/0438.html
I dont think it is possible.
ASKER
hi dimitry & manish_regmi,
what i want to write a file in kernel in net directory and assign it task to read a file in /misc/my.txt and update it frquently. so which way i can do this?
what i want to write a file in kernel in net directory and assign it task to read a file in /misc/my.txt and update it frquently. so which way i can do this?
hi,
sorry,i didn't understand? can you please explain what u are trying to do.
sorry,i didn't understand? can you please explain what u are trying to do.
ASKER
hello manish,
what am i doing is that writing a program in linuxsrc/net/ipv4/my.c that will does some processinf and generate value string and he has to store it for few minutes. as therre is no persistent way to store it i have to use user file that does storage and updation work.
what am i doing is that writing a program in linuxsrc/net/ipv4/my.c that will does some processinf and generate value string and he has to store it for few minutes. as therre is no persistent way to store it i have to use user file that does storage and updation work.
hi,
I think your problem can be solved by creating a new entry in proc fs.
For eg,
you can crate a file /proc/b123coder and write to it. The user can read from there.
for this create a proc entry.
struct proc_dir_entry* create_proc_entry(const char* name, mode_t mode, struct proc_dir_entry* parent);
This technique takes kernel memory space instead or disk space.
See this for more info.
http://kernelnewbies.org/documents/kdoc/procfs-guide/lkprocfsguide.html
regards manish
I think your problem can be solved by creating a new entry in proc fs.
For eg,
you can crate a file /proc/b123coder and write to it. The user can read from there.
for this create a proc entry.
struct proc_dir_entry* create_proc_entry(const char* name, mode_t mode, struct proc_dir_entry* parent);
This technique takes kernel memory space instead or disk space.
See this for more info.
http://kernelnewbies.org/documents/kdoc/procfs-guide/lkprocfsguide.html
regards manish
ASKER
hello manish,
but what about kernel that has to read that file how cani read contents of /proc entry in kernel running ?
but what about kernel that has to read that file how cani read contents of /proc entry in kernel running ?
hi,
You create the proc entry using
struct proc_dir_entry* create_proc_entry(const char* name, mode_t mode, struct proc_dir_entry* parent);
It gives you pointer to proc_dir_entry structure which looks like,
struct proc_dir_entry {
unsigned short low_ino;
unsigned short namelen;
const char *name;
mode_t mode;
nlink_t nlink;
uid_t uid;
gid_t gid;
unsigned long size;
struct inode_operations * proc_iops;
struct file_operations * proc_fops;
get_info_t *get_info;
struct module *owner;
struct proc_dir_entry *next, *parent, *subdir;
void *data;
read_proc_t *read_proc;
write_proc_t *write_proc;
atomic_t count; /* use count */
int deleted; /* delete flag */
kdev_t rdev;
};
there is a void *data; which hold the data.
So, simply write to that address, any thing you write to this address will be seen on the /proc files.
remember to allocate enough space for this. use kmalloc();
regards manish
You create the proc entry using
struct proc_dir_entry* create_proc_entry(const char* name, mode_t mode, struct proc_dir_entry* parent);
It gives you pointer to proc_dir_entry structure which looks like,
struct proc_dir_entry {
unsigned short low_ino;
unsigned short namelen;
const char *name;
mode_t mode;
nlink_t nlink;
uid_t uid;
gid_t gid;
unsigned long size;
struct inode_operations * proc_iops;
struct file_operations * proc_fops;
get_info_t *get_info;
struct module *owner;
struct proc_dir_entry *next, *parent, *subdir;
void *data;
read_proc_t *read_proc;
write_proc_t *write_proc;
atomic_t count; /* use count */
int deleted; /* delete flag */
kdev_t rdev;
};
there is a void *data; which hold the data.
So, simply write to that address, any thing you write to this address will be seen on the /proc files.
remember to allocate enough space for this. use kmalloc();
regards manish
also remember,
parent can be any of
extern struct proc_dir_entry proc_root;
extern struct proc_dir_entry *proc_root_fs;
extern struct proc_dir_entry *proc_net;
extern struct proc_dir_entry *proc_bus;
extern struct proc_dir_entry *proc_root_driver;
extern struct proc_dir_entry *proc_root_kcore;
regards
parent can be any of
extern struct proc_dir_entry proc_root;
extern struct proc_dir_entry *proc_root_fs;
extern struct proc_dir_entry *proc_net;
extern struct proc_dir_entry *proc_bus;
extern struct proc_dir_entry *proc_root_driver;
extern struct proc_dir_entry *proc_root_kcore;
regards
ASKER
hello manish.
i dont understand your previous comment. also can i use read_proc_t *read_proc; this procedure to read from *data.
so i think that i can easily read and write to /proc entry using
read_proc_t *read_proc;
write_proc_t *write_proc; functions.
also what are thses functions and their uses
typedef int (read_proc_t)(char *page, char **start, off_t off,
int count, int *eof, void *data);
typedef int (get_info_t)(char *, char **, off_t, int);
i dont understand your previous comment. also can i use read_proc_t *read_proc; this procedure to read from *data.
so i think that i can easily read and write to /proc entry using
read_proc_t *read_proc;
write_proc_t *write_proc; functions.
also what are thses functions and their uses
typedef int (read_proc_t)(char *page, char **start, off_t off,
int count, int *eof, void *data);
typedef int (get_info_t)(char *, char **, off_t, int);
ASKER
hello manish,
one thing i forget to tell you that whatever i ask above i am asking those can be doen in running kernel or not?
i want to read and write from kernel to /proc file.
one thing i forget to tell you that whatever i ask above i am asking those can be doen in running kernel or not?
i want to read and write from kernel to /proc file.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
hi manish,
the program is not compiling completely. the errors are
[root@localhost my]# gcc -c manish.c -I /usr/src/linux-2.4.20-8/in clude/
manish.c: In function `init_ide':
manish.c:24: warning: passing arg 4 of `create_proc_read_entry' from incompatible pointer type
manish.c: In function `__init_module_inline':
manish.c:34: warning: return from incompatible pointer type
help
the program is not compiling completely. the errors are
[root@localhost my]# gcc -c manish.c -I /usr/src/linux-2.4.20-8/in
manish.c: In function `init_ide':
manish.c:24: warning: passing arg 4 of `create_proc_read_entry' from incompatible pointer type
manish.c: In function `__init_module_inline':
manish.c:34: warning: return from incompatible pointer type
help
hi,
it is just a warning of incompatible pointers. You can ignore it or typecast it.
the reason is that create_proc_read_entry expects the fourth arg as void * and you passed char *.
also make int init_ide and add return 0;
#include <linux/module.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/proc_fs.h>
struct proc_dir_entry *p;
char *mydata = "Hello";
void redd()
{
printk("%s",p->data);
}
int init_ide()
{
printk("<1>initiated \n");
p = (struct proc_dir_entry *)kmalloc(sizeof(struct proc_dir_entry), GFP_KERNEL);
p = create_proc_read_entry("ma nish", 0x777, &proc_root,redd, (void *)mydata);
return 0;
}
void close_ide()
{
printk("<1>closed \n");
remove_proc_entry("manish" , &proc_root);
kfree(p);
}
module_init(init_ide);
module_exit(close_ide);
MODULE_LICENSE("GPL");
regards manish
it is just a warning of incompatible pointers. You can ignore it or typecast it.
the reason is that create_proc_read_entry expects the fourth arg as void * and you passed char *.
also make int init_ide and add return 0;
#include <linux/module.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/proc_fs.h>
struct proc_dir_entry *p;
char *mydata = "Hello";
void redd()
{
printk("%s",p->data);
}
int init_ide()
{
printk("<1>initiated \n");
p = (struct proc_dir_entry *)kmalloc(sizeof(struct proc_dir_entry), GFP_KERNEL);
p = create_proc_read_entry("ma
return 0;
}
void close_ide()
{
printk("<1>closed \n");
remove_proc_entry("manish"
kfree(p);
}
module_init(init_ide);
module_exit(close_ide);
MODULE_LICENSE("GPL");
regards manish
/proc is not really a file system, but generates all data "on the fly".
Regarding reading user files, you should check how to access files kernel-sided via VFS.
Cheers,
Stefan