How do i use vfs_readdir in linux kernel? - c

I'm trying to use a system call to display the contents of a directory. I've been pointed in the direction of vfs_readdir, but I have no clue of how to use it or what to pass to it to get the contents of a directory. All I want to do is be able to list files in a directory similar to how ls works. (I eventually intend to store this in some sort of buffer, but for now just being able to print the contents of a dir would be enough).

I think you probably have to open the directory using filp_open.
For the "flags" argument, you proably need to put some combination of the LOOKUP_ flags listed in include/linux/namei.h
You can see what build_open_flags does here: http://lxr.linux.no/#linux+v3.1.5/fs/open.c#L876 to provide flags to filp_open.
As far as I know, filp_open IS the correct way of opening a file in kernel-space. However, doing so is discouraged.
Provided you do so from the context of a "normal" thread belonging to a user process, I think you will be ok.

Related

Copying a directory using sockets

I'm writing a program in C that sends files across the network using sockets. This works fine for files - they are read into a buffer and then written onto the socket. They are picked up at the other end by reversing this process.
However, how can this apply to directories? I also want to copy directories, keeping the permissions the same (so I don't think mkdir will work). At the moment when I try to run this on a directory, it says the size is -1. How is a directory represented?
To be clear, for example, if I want my program to copy /tmp across the network, it will do this:
/tmp/1.txt - OK
/tmp/2.txt - OK
/tmp/dir/ - Skip
/tmp/dir/3.txt - Can't write to path
There are several possibilities. It would fit fairly will with what you have already to tar the directory to transfer, send the resulting archive across the network, and untar on the other side.
Alternatively, you can walk the directory tree recursively. For each directory you need transfer only the name and whichever attributes you want to preserve, but then you must list the directory contents (probably via readdir()) and transfer each member.
By the way, don't neglect to think about how you're going to handle links, both symbolic ones and hard ones. And if you want your program to be really robust then consider also what to do with special files such as device files and FIFOs.
I guess it is homework, otherwise why not use FTP, scp, rsync, unison etc.
To test if a file path is a plain file, a device, a directory, etc etc... use
stat(2)
To read a directory, use opendir(3) then loop on readdir(3) (then of course closedir). You don't need to know how a directory is represented.
You probably should be interested in nftw(3) to recursively traverse a file tree.
To make one directory, use mkdir(2)
You should read Advanced Linux Programming
BTW, this answer contains useful information too...

Copying files from one folder to another in C Program

I want to copy some files from one folder to other folder and before copying or replacing the files I want to take backup from destination folder only those file which are I am trying to copy in to source file to destination file.
This is what I want to do but is this possible in pure C-language?.
First of all you should say on which platform you want to do that.
I'm not that familiar with Windows so what i'll be saying only applies to Linux (maybe it works on windows as well but I doubt it).
Have a look on man opendir and man readdir and those are the only things you need to do what you want (you obviously will also need to open/read/write and close but I imagine that you know that).
There's no native way in C to do that. You need platform-specific code. The simplest way to achieve that is to use the system() function
system("copy C:\\Windows\\notepad.exe D:\\"); // Windows
or
system("cp ~/myfile.txt ~/mycopiedfile.txt"); // *nix
But for better performance and/or more control over the result you need to call the corresponding APIs if available (like CopyFile() on Windows), or copy the file on your own by reading from source to a buffer with fread() then write to the destination with fwrite()
See Is there a POSIX function to copy a file?

How can I programmatically get the list of open file descriptors for a given PID on OS X?

Everything I've seen says to use lsof -p, but I'm looking for something that doesn't require a fork/exec.
For example on Linux one can simply walk /proc/{pid}/fd.
You can use proc_pidinfo with the PROC_PIDLISTFDS option to enumerate the files used by a given process. You can then use proc_pidfdinfo on each file in turn with the PROC_PIDFDVNODEPATHINFO option to get its path.

Prevent accessing files outside of given working directory

I am trying to prevent the access on files outside of a given working directory.
My first attempt was to use chdir and chroot, but chroot can only be used by root users.
Is there any other possibility? I have heard something about another one, but I can't remember.
Perhaps a simple function to check if the path is outside of the working directory or second argument.
Some details about the program:
shall be run on Linux
simple shell programm without any interactive elements
takes a directory argument, which is the working directory
Thanks for any advices.
EDIT:
After some research I found different aproachments, but I can't use any of them.
pivot_root
set_fs_root (linux kernel)
Is there any possibility to use that?
Perhaps there is a possibility to open a file which is contained by a given directory. So I call the function with the argument file path and the "root" path where to look.
I'm assuming that you're on a Linux/MacOSX platform. There are a couple of ways. One is to create a special user for your program who owns that directory, but doesn't have write permissions to anything else in the system*. The other option is to use a program like SELinux to only allow certain operations to the program, but that seems like overkill.
*: You must always give the user read permissions. How will your program run without read access to glibc?
You might want to look into a restricted shell; I think most of the common shells have options for a restricted mode that disables cd, prevents changes to certain environment variables, and some other things. For pdksh, it would be /bin/ksh -r. The option differ for other shells, though, so read the appropriate manual page.

How to know the file is modifed in linux

I want to know what system call is used in linux C programming is used to know whether a file is modified.
I know that make utility compiles the file using the modification dates only.
I want know how to find whether the file is modified or not.
Thanks in advance
Using md5sum or sha1sum will hash the contents of the file, which should give you a better indication of actual changes than modification dates.
stat(2) gives you file times and more.
Edit 0:
You can look into fcntl(2) and F_NOTIFY flag - you'd have to open the directory, not the file itself though. Or the newer Linux inotify(7) facility.
You can use ls and various flags on it, like -l or -t and pipe to grep or something. That will tell you when the last file was modified. But it doesn't really tell you if the file was modified. I think the only real way you can know that is if you are keeping track of when the last time it was modified in general (like checking from backups or something).

Resources