I have to test the given folder path is a mount point or it is in local to my system with c code.
I read on net that /etc/mtab file have entry for mounted point. I can check that file but is their any system call or any function to check mount point.
My kernel version is 2.6.28.10.
These functions can be used for data structure access the mtab file:
FILE * setmntent(const char *file, const char *mode)
int endmntent (FILE *stream)
struct mntent * getmntent (FILE *stream)
struct mntent * getmntent_r (FILE *stream, struct mentent *result, char *buffer, int bufsize)
int addmntent (FILE *stream, const struct mntent *mnt)
char * hasmntopt (const struct mntent *mnt, const char *opt)
For more details about these functions refer the man page.
On linux, getmntent() is suitable for parsing /etc/mtab.
Related
I need to implement the following function as part of a kernel module:
ssize_t write(struct file *filp, const char *buf, size_t count, loff_t *f_pos)
which writes buf into the file filp using f_pos, and then updates f_pos.
my question is how can I write to f_pos and then change it's value?
I understand that it points to the start of the free file space but how can I update it after i'm done writing?
Thank you
I need to remount / read-only with C running on Linux. So far, I've come up with the following code:
mount("/dev/sda1", "/", "ext4", MS_REMOUNT | MS_RDONLY, NULL);
But I was wondering if there was a way to do that without specifying the source device (/dev/sda1) or the file system type (ext4), like the command mount -o remount,ro /. Can those sections of the syscall simply be replaced with NULL?
Use getmntent() to iterate over all the mounted filesystems:
NAME
getmntent, setmntent, addmntent, endmntent, hasmntopt, getmntent_r -
get filesystem descriptor file entry
SYNOPSIS
#include <stdio.h>
#include <mntent.h>
FILE *setmntent(const char *filename, const char *type);
struct mntent *getmntent(FILE *stream);
int addmntent(FILE *stream, const struct mntent *mnt);
int endmntent(FILE *streamp);
char *hasmntopt(const struct mntent *mnt, const char *opt);
/* GNU extension */
#include <mntent.h>
struct mntent *getmntent_r(FILE *streamp, struct mntent *mntbuf,
char *buf, int buflen);
Find the filesystem mounted at /, and get it's device from the struct mntent returned.
According to the mount man page it seems you can just omit both file system type and source mount point:
...
Remounting an existing mount
An existing mount may be remounted by specifying MS_REMOUNT in
mountflags. This allows you to change the mountflags and data of
an existing mount without having to unmount and remount the
filesystem. target should be the same value specified in the
initial mount() call.
The source and filesystemtype arguments are ignored.
...
I'd like to share a variable between kernel and user space and I've found that it's possible with procfs.
The kernel module must act in certain way if given value is set. The user space program is responsible for changing this value, but the kernel module must read it when necessary.
I know that I must create the /proc file in the kernel module.
My question is, how to read the file from the kernel module?
Source : linux.die.net/lkmpg/x769.html
/**
* This function is called with the /proc file is written
*
*/
int procfile_write(struct file *file, const char *buffer, unsigned long count,
void *data)
{
/* get buffer size */
procfs_buffer_size = count;
if (procfs_buffer_size > PROCFS_MAX_SIZE ) {
procfs_buffer_size = PROCFS_MAX_SIZE;
}
/* write data to the buffer */
if ( copy_from_user(procfs_buffer, buffer, procfs_buffer_size) ) {
return -EFAULT;
}
return procfs_buffer_size;
}
To clarify, in Your module whenever user writes to Your file in procfs, this example shows how to handle such write.
In kernel >= 3.10 proc_write is moved to structure file_operations where declaration of write is different, so in newest your solution won't work.
You can implement typical file_operations.write(struct file *, const char __user *, size_t, loff_t *) and reference this to:
struct proc_dir_entry your_proc_dir_entry{
.proc_fops = &your_fops,
}
I want to know on what file in /sys/* the functionstatic ssize_t
sysfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos) were called. the question is how to get the file name/path by having struct file *file ?
struct file {
mode_t f_mode;
loff_t f_pos;
unsigned short f_flags;
unsigned short f_count;
unsigned long f_reada, f_ramax, f_raend, f_ralen, f_rawin;
struct file *f_next, *f_prev;
int f_owner; /* pid or -pgrp where SIGIO should be sent */
struct inode * f_inode;
struct file_operations * f_op;
unsigned long f_version;
void *private_data; /* needed for tty driver, and maybe others */
};
EDIT:
I understand there is no one-to-one mapping between inode to a name, but I must know the major/minor/file descriptor and can search for some of the inode's name in the file system.
Actuall you can retrieve the file path used to open the file. In linux struct file represents an opened file and has the path used to open that file associate with the structure. Before 2.6.20 it was a member called f_dentry and from 2.6.20 it is a new member called f_path. You can use function d_path() to retrieve the full path of your file directly.
Refer to the following Linux source code:
http://lxr.free-electrons.com/source/include/linux/fs.h?v=3.10#L763
http://lxr.free-electrons.com/source/fs/dcache.c?v=3.0#L2626
And also answer:
How can I get a filename from a file descriptor inside a kernel module?
In Linux and other Unix-like OSs, a file object can be pointed to by any number of names, and the file object itself has no pointers back to any of them. Names are not a feature of the file, they are just an external index.
I'm setting up the FUSE filesystem, and I need to get a file pointer to any open files so that I can encrypt them as they get written. The FUSE system uses a special struct. One of the components of the struct is called fh and is of type uint64_t. When a file is opened, it is captured in an int variable as you can see in the following code:
/* File open operation */
int bb_open(const char *path, struct fuse_file_info *fi)
{
int fd;
fd = open(path, fi->flags);
fi->fh = fd;
return fd;
}
This fi struct is available to me in the write function, and I'm hoping that I can use it to create a file pointer that I can pass to my encryption function. Here is the code for the write function as I have it set up at the moment:
/* Write data to an open file */
int bb_write(const char *path, const char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
char* password;
FILE* fp;
//malloc and instantiate password
fp = (FILE*) fi->fh; //my lame attempt to get a file pointer
return encrypt(<inpFilePtr>, <outFilePtr>, 1, password);
}
Finally, the signature of my encryption function looks like this:
extern int encrypt(FILE* in, FILE* out, int action, char* key_str);
I'd like to take that fi->fh variable and turn it into a file pointer so that I can use it as my input file pointer argument. Since it was originally created by an "open" operation, it seems like there should be a way to do this, but I can't make it work.
Can anyone help? Thanks!
File descriptors and FILE * pointers aren't the same thing. A FILE * pointer is a pointer to an opaque type provided by your C implementation - the APIs that deal with FILE * (fopen, fread, fwrite, fclose, etc.) are part of standard C. File descriptors, on the other hand, are operated on using the POSIX system calls (open, read, write, close, etc.) that are often used to implement the C-level abstractions.
Luckily there are functions that let you get one from the other. In your case, you'll want to use fdopen(3) to get a FILE * from the file descriptor. If you wanted to go the other way, you'd want to look into fileno(3).