How to give path in create_proc_entry function? [duplicate] - linux-kernel

This question already has answers here:
Create ProcFS entry in /proc/net
(2 answers)
Closed 9 years ago.
how to mention the path in third argument of create_proc_entry() function. Till now i keep NULL over there, it is working fine but i want to keep under /proc/net/ directory for that i need to mention something over third argument.Let me give the instructions to do so

struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode,
struct proc_dir_entry *parent);
proc_entry = create_proc_entry("megharaj_proc", 0666, path/NULL);

The third parameter is a pointer to the "parent", not a path.
You can simply create a directory under /proc by calling:
proc_dir_entry *parent = NULL;
parent = proc_mkdir("your_parent_name",NULL);
Then use that pointer as your parent when creating your desired proc entry that
will appear under /proc/your_parent_name/my_proc as follows:
proc_dir_entry *my_proc = NULL
my_proc = create_proc_entry("my_proc", 0666, parent);

Related

Using auto to instance private classes [duplicate]

This question already has answers here:
Why can I use auto on a private type?
(5 answers)
What's the difference between type and name in C++?
(3 answers)
Closed 4 years ago.
Consider the following code:
struct A{
private:
struct B{};
public:
B make() const{return B{};}
};
int main(){
A a;
auto b1 = a.make(); (void)b1;
// A::B b2 = a.make(); (void)b2; // compile error: B is a private class
}
What is the logic behind being able to compile b1 line while the second b2 line? After all auto is supposed equivalent to replace the name of the class.
If it is what I think, this shows that auto is not simply syntax sugar.
auto on private classes can be used force the user not know about certain types but still be able to use instance of them! (and this was not possible in C++98?)
Am I mistaken in this interpretation?

In Linux, how can I get the filename from the "struct file" structure, while stepping thru the kernel with kgdb?

I'm trying to view the filename via kgdb, so I cannot call functions and macros to get it programatically. I need to find it by manually inspecting data structures.
Like if I had a breakpoint here in gdb, how could I look around with gdb and find the filename?
I've tried looking around in filp.f_path, filp.f_inode, etc. I cannot see the filename anywhere.
ssize_t do_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
{
struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
struct kiocb kiocb;
ssize_t ret;
init_sync_kiocb(&kiocb, filp);
kiocb.ki_pos = *ppos;
kiocb.ki_left = len;
kiocb.ki_nbytes = len;
ret = filp->f_op->aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
if (-EIOCBQUEUED == ret)
ret = wait_on_sync_kiocb(&kiocb);
*ppos = kiocb.ki_pos;
return ret;
}
You can get the filename from struct file *filp with filp->f_path.dentry->d_iname.
To get the full path call dentry_path_raw(filp->f_path.dentry,buf,buflen).
In the Linux kernel, the file structure is essentially how the kernel "sees" the file. The kernel is not interested in the file name, just the inode of the open file. This means that all of the other information which is important to the user is lost.
EDIT: This answer is wrong. You can get the dentry using filp->f_path.dentry. From there you can get the name of the dentry or the full path using the relevant FS flags.
The path is stored in the file->f_path structure as it's name implies. Just not in a plain-text form, but parsed into objects that are more useful for kernel operation, namely a chain of dentry structures, and the vfsmount structure pointing to the root of the current subtree.
You can use the d_path function to regenerate a human-readable path name for a struct path like file->f_path. Note that however this is not a cheap operation and it may slow down your workload significantly.
The above mentioned issues about open but unlinked files, multiple hardlinks and similar are valid for mapping from and inode to a pathname, and open file always has a path associated with it. If the file has been unlinked d_path will prepend a " (deleted)" to the name, and if the filename it has been opened with has been changed to something else using rename since it was opened d_path will not print the original name, but the current name of the entry that was used for opening it.
filp->f_path.dentry->d_name.name
This worked for me

Listing all files in a folder, only first character of file name gets printed [duplicate]

This question already has answers here:
Filenames truncate to only show first character
(3 answers)
Closed 9 years ago.
I'm trying to access all images in a designated folder, get their names, and then pass them for further processing (getting their pixel values, to be precise, but this isn't relevant now). The following test code should list the name of every image found, however, for some reason it only lists the first letter for each image.
#include <windows.h>
int main(int argc, char* argv[])
{
WIN32_FIND_DATA search_data;
memset(&search_data, 0, sizeof(WIN32_FIND_DATA));
HANDLE handle = FindFirstFile(L"images\\*.jpg", &search_data);
while(handle != INVALID_HANDLE_VALUE)
{
printf("Found file: %s\r\n", search_data.cFileName);
if(FindNextFile(handle, &search_data) == FALSE)
break;
}
return 0;
}
Your program is compiled for Unicode, but your printf format string is expecting an ASCII string. Change the %s to %S.

Use same file_operation for different files in VFS

I am creating several dentry objects in the securityfs in a custom kernel module. Here's how I'm doing it:
inst->output_file = securityfs_create_file("1",
S_IRUSR | S_IRGRP, uprp_dir, NULL,
&my_file_ops);
inst->output_file = securityfs_create_file("2",
S_IRUSR | S_IRGRP, uprp_dir, NULL,
&my_file_ops);
// and so on
I have the usual sequence operations implemented for my_file_ops. The problem however is that the following function gets called for all dentry objects:
static int ct_open(struct inode *inode, struct file *file)
which then goes on to use:
static void *my_seq_ops_start (struct seq_file *m, loff_t *pos)
The question is, how do I figure out which dentry object the user wants to read (in any of these functions)? -- meaning I want to output different things for the files 1 and 2.
When the VFS calls your ct_open() function through the my_file_ops structure, it passes back the file that's open, both as a struct inode *, and as a struct file *. The struct file contains a member f_dentry, which is a pointer back to the dentry that was returned by securityfs_create_file(). The struct dentry contains the filename.
However, even cleaner is the fact that the 4th argument of securityfs_create_file() is for your use. You can pass in any pointer to whatever internal structure you want, and retrieve it during the open operations from the inode.i_private pointer. This is normally the "right" level of abstraction, so your file operations don't need to know anything about the filename.

linux kernel fullpath from file struct

Good day. Im trying to get fullpath from struct file
char *buf = (char*)__get_free_page(GFP_USER);
char *buf2 = (char*)__get_free_page(GFP_USER);
char *name = dentry_path(file->f_dentry, buf, PAGE_SIZE);
char *root = dentry_path(file->f_vfsmnt->mnt_mountpoint, buf2, PAGE_SIZE);
***some operations***
free_page((unsigned long)buf);
free_page((unsigned long)buf2);
kfree(*root);
kfree(*name);
But i get an error
fs/read_write.c:707: error: request for member 'mnt_mountpoint' in something not a structure or union
How can i get vsfmount struct from file?
Kernel version linux-2.6.37.6
In kernel 2.6, your code should work.
In kernel 3.0, struct vfsmount contains member mnt_root, of type struct dentry.
I think this would give you the mount point path.
Which are you using?
Judging by the error, is it possible that f_vfsmnt is allocated in the file-struct?
I that case the mnt_mountpoint field should be accessible via file->f_vfsmnt.mnt_mountpoint.

Resources