I writing a linux kernel module that does some work with /proc... I'm trying to use the new seq methods for returning the data for /proc... Anyhow, after I call proc_create_data() I have a proc_dir_entry (whose ->data member is pointing at my supplied context)... Anyhow, the file_operations structure is also passed and I really need to know how to access either the proc_dir_entry or the proc_dir_entry->data from the open() file operation...
The answer was to use the PDE macro to convert the inode* into a pointer to the proc_dir_entry, which of course had a "data" member pointing at what I needed.
struct proc_dir_entry* pde = PDE( inode );
Related
I am new to Linux and I have been assigned this
In Linux kernel sources, Find _do_fork(), the fundamental routine for creating a new process
What is the purpose (give a high-level description) of copy_process() ?
Within copy_process, what exact code guards against fork() bombs?
can somebody help me out?
Firstly, you need to know that fork() will use system call and find interrrupt function from Interrupt table which is named as 'sys_fork()`, which is like below
SYSCALL_DEFINE0(fork)
{
......
return _do_fork(SIGCHLD, 0, 0, NULL, NULL, 0);
}
So the core of fork is _do_fork()
This function will do two major work
call copy_process() to copy structure of process/thread in kernel, which named as task_struct
call wake_up_new() to wake up the task
Like I said in my comment, elixir.bootlin.com is a very good resource for looking at the source code of Linux. It has a very good search engine. I've been looking at the source code and I think I found the code which does what you are looking for (related to fork bombs prevention).
In kernel/fork.c in the copy_process() function you find the following lines:
if (atomic_read(&p->real_cred->user->processes) >=
task_rlimit(p, RLIMIT_NPROC)) {
if (p->real_cred->user != INIT_USER &&
!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN))
goto bad_fork_free;
}
This code does an atomic_read of the current task_struct structure (p) being copied. It reads the member real_cred which is a struct of type cred defined in include/linux/cred.h. This struct contains a member named user which is a struct of type user_struct defined in include/linux/sched.h. This user_struct contains a member named processes which is an atomic_t which is itself a struct containing one member (an int). So processes is basically an int which tells the kernel how much processes a user have. The code above checks this member against the value returned by task_rlimit() function. If the limit is exceeded, it cancels the whole thing. Hosnestly, I don't completely understand the if in the middle. But you can always look for details in the source code.
I'm attempting to learn how to write a sysfs module, and am confused at the basic initialization. In this document looking at the kobject.h file, there are several different functions related to creating a sysfs entry.
From the looks of it, the function "kobject_init_and_add" seems like the right thing to use, which takes the following:
90 int kobject_init_and_add(struct kobject *kobj,
91 struct kobj_type *ktype, struct kobject *parent,
92 const char *fmt, ...);
struct kobject and struct kobj_type are straightforward enough, but I don't understand what the *parent kobject and *fmt cstring are supposed to be.
Further, after initializing these objects, I would need to remove them at the exit_module function, but there are two options that seem possible: kobject_del and kobject_puts. What are the differences between these?
Part of my confusion comes from the fact that while googling for the answer, I see tutorials which says to use functions like kobject_register instead, but that function doesn't actually exist.
Yes there are lot of example on mainline kernel which you can refers for your implementatin. For Your doubts I am adding the some example code"
Module Probe/init function
static struct kobject *module_kobject;
module_kobject=kobject_create_and_add("module_status",NULL);
sysfs_create_group(module_kobject,&module_attr);
Module Remove/exit function
sysfs_remove_group(module_kobject,&module_attr);
kobject_put(module_kobject);
If you want to expose more than one attribute on the user space; than you need to define the group as well
static struct attribute_group module_attr={
.attrs = module_attribute,
};
There is some more implementation and function you may need like:
static ssize_t module_show_status(struct kobject *kobj,struct kobj_attribute *attr,char *buf);
static ssize_t module_store__status(struct kobject *kobj,struct kobj_attribute *attr,const char *buf,size_t len);
I think you can start your sysfs module implementation based on the above code and Feel free for any help.
There are many kernel modules that create sysfs entries. For example,
http://lxr.free-electrons.com/source/net/bridge/br_sysfs_br.c
This module uses kobject_create_and_add(), which gets as a parameter a kobject instance, created by sysfs_create_group(). I believe that looking into such module, and trying to code step by step, following the patterns in that module, can help. Also look in
http://lxr.free-electrons.com/source/Documentation/kobject.txt
Rami Rosen
I work on Ubuntu kernel-mode netfilter module and need information about all network interfaces and their properties in module code.
Inside of init_module() I use register_netdevice_notifier() for that purpose. When callback function is called I can see correct event codes like up/down and other, but it seems that third parameter void* casted to net_device* provides object with invalid properties. ->name is empty string, ->if index is some nonsense number etc.
I tried debug version of module on kernel 3.19 and rebuild also on 4.2. Result is the same, I cannot read properties of net_device relating to event.
What can be problem ?
From what I can see from LXR, you need to call netdev_notifier_info_to_dev on the last parameter to get your net_device * (see here)
get pointer for existing device class (struct class) in Linux kernel module
Hi all!
I am trying to register a device in an existing device class, but I am having trouble getting the pointer to an existing class. Kernel version is Linux 3.3.6.
I can create a class in a module, get the pointer to it and then use it to register the device with:
*cl = class_create(THIS_MODULE, className);
dev_ret = device_create(*cl, NULL, *dev, NULL, driverName);
However I need to register another device in the same class with another module, but I couldn't find a way to get the pointer to an existing class. And I can not create the class again in the other module, because since class already exists class_create returns NULL and not the pointer to the class required by device_create.
I found in:
http://lwn.net/Articles/102500/
A function that returns a pointer to a class by its name:
struct class * class_find(char * name)
However when I try to compile the function compiler says it does not exist.
I thought this function was exported by the kernel (my module have license GPL) but it appears it is not.
Maybe I need to include some header?
I tried to rewrite this function since, its code is list in the above link. But when I try to iterate over class_subsys with:
list_for_each_entry(this_class, &class_subsys.kset.list, subsys.kset.kobj.entry)
now symbol class_subsys is not found. Again I thought it is exported to the kernel.
I am not sure what is missing. Some header?
Am I doing it the wrong way?
There is another function to do it?
I suppose if I could traverse sysfs from start I could get a pointer to an existing class.
But I also did not find how to start traversing sysfs.
All functions I have seen requires a pointer to kobject or kset to start traversing. But I have no pointer even to the root of sysfs or kernel objects, so I can not start traversing the tree to get a class pointer.
Can anyone point me in the right direction please?
I think input core is done this way, here is the snippet
Take a look at: https://github.com/torvalds/linux/blob/master/drivers/input/input.c#L1720
Best regards!
this is how the function looks like:
struct net_device_stats* (*get_stats)(struct net_device *dev);
I simply need to call this function in my code and get it's results in a net_device_stats struct I declared earlier. Can anyone give a simple implementation code for this?
This is not a function. Its the declaration of a function pointer get_stats which points to a function which receives a pointer to the structure net_device and returns a pointer to a structure of type net_device_stats
Here is one use case
struct net_device *dev;
struct net_device_stats *device;
device = get_stats(dev);
EDIT From your comments, I see you are using an older version of the kernel. IN later kernels , structure net_device still resides in linux/netdevice.h but there's no get_stats function pointer. Its changed into ndo_get_stats and it is now under another structure net_device_ops
So start using these new function pointers.