Linux: Getting Mount Point Utilization from /proc? - c

I know I can get mount point utilization using command line tools like df and doing something like the following:
popen("df -h /var/log | awk '{if($1==\"tmpfs\") print $5}'","r")
where the fifth column is the usage percentage.
What I want to know is if there is a way to access mount point utilization from /proc/, such as /proc/mounts. I feel that getting this information from /proc/ would be a quicker and more efficient solution than using df and awk to parse out the usage. I've searched mntent.h but couldn't find anything promising there either. Any ideas?

You probably want the statfs(2) syscall (it does not use /proc/). You probably would use it thru statvfs(3) Posix function.
Notice that the /proc/ filesystem (and also /sys/ ...) contains pseudo-files which are generated by the kernel on demand without any IO. See proc(5). You could read sequentially /proc/self/mounts and/or /proc/self/mountstats and/or appropriate files under /proc/fs/ and/or /sys/ (such as some files in /proc/fs/ext4/sda1/ or even in /sys/block/sda/sda1/ for my desktop computer ; it would be different on yours ....)
Perhaps systemd is also able to give such information, but I don't know it enough.

Related

Why is Jena tdb2.tdbquery optimization stuck on "Reorder/generic"

I am using apache-jena-4.5.0 and fuseki pretty much out-of-the-box. I had created a TDB2 dataset using fuseki, but now shut it off and using command-line utilities of jena on a Windows box inside a bash shell.
My basic command is:
java -cp "*" tdb2.tdbquery --loc ~/path/to/databases/DEMO--explain --set arq:logExec=FINE --time --query ~/path/to/demoquery.txt
And my question is why does the output always contain only Reorder/generic like this:
15:56:00 INFO exec :: Reorder/generic
Even after I have tried all these:
successfully run tdb2.tdbstats and gotten a reasonable-looking temp.opt file as output
moved that temp.opt to each of /path/to/DEMO/stats.opt and /path/to/DEMO/Data-001/stats.opt
tried uppercase STATS.OPT for each since I'm on windows, just to be sure
Still I don't seem to be able to produce any output with Reorder/stats
This question did not contain enough detail to answer. The intended question was why won't TDB2 optimize my query and the answer was in the SPARQL, not in the invocation of tdb2.tdbquery or the location of the stats.opt file.
My SPARQL contained multiple FROM clauses, which forced TDB into BGP mode (instead of quads) and thwarted any optimization. As best we can tell at the moment, one wishing to use the TDB2 optimizer should use either the default graph, or a combination of FROM NAMED and GRAPH which causes the evaluation of graphs one at a time.

How do I get the disk addresses of files in C/C++?

When a file is saved into a drive, its contents are written & then indexed. I want to get the indexes and to access the raw contents of the files.
Any idea on the method how to do it, especially for ex4 & btrfs?
UPDATE: I want to get the addresses of the extents of a file. The information about the addresses must be stored somewhere onto the disk. I want to retrieve this info, in order to map the physical location of the file contents. Any methods in order to achieve that?
UPDATE: Hello, all! Thanks for your replies. What I want is a function/command which returns me a list of extent addresses. debugfs seems the function/command with the most-relevant functionality.
It depends of the filesystem you are using. If you are running Linux you can use debufs to seek the file in the filesystem.
I have to say that all FSs are mounted through a VFS, a virtual filesystem that is like a simplified interface with the standard operations (open, close, read...). What is the meaning of that? No filesystem nor its contents(files, dirs) are opened directly from disk, when you open something, you move it to the main memory(your RAM) you do your operations and when you close something it returns to the disk drive.
Now, the question is: Can I get the absolute address in a FS? Yes, if you open your whole filesystem like open ("/dev/sdaX", 0_RDONLY); so you get the address relative to your filesystem using lseek in C for example.
And then... Can I get the same in the whole drive? No, that is because you cannot open the whole drive as a file descriptor. Remember /dev/sdaXin UNIX? Partitions and its can be opened like files because they have a virtual interface running on them.
Your last answer: Can I read really raw contents? All files are read as they appear on disk, the only thing that changes is the descriptor used by the OS and some data about how is indexed, all this as a "file header".
I hope all your questions are answered.
The current solution/workaround is to call these functions with popen:
filefrag -e /path/to/file
hdparm --fibmap /path/to/filename
Then one should simply parse the stringoutputs of these programs. It is not a real solution (i.e.: outputs at C/C++ level), but I'll accept it for now.
Sources:
https://unix.stackexchange.com/questions/106802/what-command-do-i-use-to-see-the-start-and-end-block-of-a-file-in-the-file-syste
https://serverfault.com/questions/29886/how-do-i-list-a-files-data-blocks-on-linux

Get full path of executable of running process on AIX

This is most similar to Get full path of executable of running process on HPUX…, except for AIX.
The basic question is: how, on AIX, can I determine the full path to the current executable? Having to do this before doing anything else (e.g., chdir) is fine.
The most accurate answer I've found so far is to check the output from
svmon -P $$ -O format=nolimit,filename=on,filtertype=client
(where $$ has its shell meaning: current pid). That's not only heavy amounts of C, but svmon is also not very fast and can easily overwhelm the runtime of the rest of the application.
The next best answer seems to be to simply look at argv[0], and, if it has a slash in it, it's either a full path name (starts with a leading /) or a relative-to-current-dir name (does not start with a leading /). If it doesn't have a slash in it, it's relative to something in PATH.
And if, after this resolution, I end up with a symlink, then there's all the resolution of symlink(s) to deal with as well (hard links are probably beyond the scope of any solution). This solution looks like it's relatively cross-platform, but is also very heavy in the C code (should be faster than svmon). And I expect there are race-conditions and such to deal with.
Thanks,
Update: I'm looking for a solution to submit to the perl devs. And they're going to worry about an insecure PATH, especially in setuid/setgid scenarios. See perlsec. I think that we could be okay here, but if you combined setuid with faking argv[0], you could force perl to think it's somewhere else, and load the wrong modules. The "next best" answer above only really works when not in perl's taint-mode.
Why can't you use ps options as a base line? Granted, you'll still need to process the cmd value to see if has a leading '/' or not. Something like
ps -o pid,env,cwd,cmd | grep youAppName | awk -f programToRationalizePathName
I don't have access to AIX anymore, but I did work on it for 2 1/2 years and I know I've used this sort of feature. I didn't think it was slow, but I might have had different requirements than you do.
I get the impression you want a utility function, a 1-at-time call that returns the full path, but if you need an on-going process and are concerned about re-starting ps every 1 minute (for example), look at the AIX specific nmon utility. Not sure if it can generate output similar to the ps -o cmd but it can be set up to run as long as you want, as often as you want (down to 1 second intervals) and it is just one process, whose output can be redirected as needed. (Nmon is not part of the std install, but most organizations do install it, as it is IBM blessed (if not supported directly)).
Of course all of the 'not 100%' caveats apply from the similar questions mentioned by you and in the comments.
I hope this helps.
Use ps to get executable path address
ps -aef | grep app | awk '{print $8}'
above command gives your app executable path address

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).

Recording command line input and output on linux with C

Basically I want to do a program almost like a keylogger. The thing is that I as network admin sometimes I don't remember what I did to a machine on certain case, or same times I make howto's and tutorials for linux. I want to record what have i done.
So basically the idea of this program is:
you type the name of the program, (I call it rat for the moment)
$ rat
Welcome everything from now on will be recorded
recording $ ls
file1 file2 file3
recording $ quit
Bye bye
Everything you do will go out to an xml file. Something like this
<?xml version='1.0' encoding='UTF-8' ?>
<rat>
<command>
<input>ls</input>
<output>file1 file2 file3</output>
<err><err>
</command>
</rat>
i am doing some tests with fp_in = popen( input, "w");
and system, but first with popen i cant change directories and with "system i cant properly manage the input and output.
I was also checking if there is something I can do to bash like a plugin but haven't find any information.
At some points if feels like it I should create another shell (which is way beyond my current abilities) or fork bash sh. But it should been that complicated right.
I am open to suggestion where to start.
I am rusty with C, so I am reading again a lot of basic stuff.
With the xml file, later i was thinking on making a program to store this data and/or editing this data so i can create tutials and howto.
I can think of many ways of expanding this up to using printscreen so all the stored images go to a file you can upload to a server (for the moment i am glad to store the data). It could be a usefull tool.
ps. I do know this can be use for evil things too.
There already exists the script command, which will record all input and output into the terminal, writing it into a transcript. I would recommend just using that, unless you have particular needs that it doesn't meet. Actually, the nicest version of script that I've seen has been the NetBSD version, so you may want to look into that if the Linux version doesn't meet your needs.
If you would like to write it yourself, instead of using system, I would recommend that you use fork/exec to create a single shell process, which you copy all input and output into. To get an idea of how this works, I'd recommend looking at the source code for an existing version of script.
Most shells have a script built-in which will simply record the text in- and out- from the command line. Not quite what you're looking for... To my surprise script is not a built in, which means it is a model for building what you want.
The script command does almost what you want: it simply records the text in- and out- from the command line.
If you make your prompt distinctive (so that you can reliably tell the difference between shell commands and everything else) you can post-process the output of script to achieve your goals. Alternately you can hack script to get it to emit the XML you're looking for.
You can also try approaching this from a different angle. Instead of using a regular shell, connect to the machine using ssh or telnet and run your commands that way. Many ssh/telnet clients (PuTTY, for instance) have an option to log all console input and output during the session. You should be able to post-process this log to generate whatever type of logfile that you need.
Depending on your setup, you might not even have to use a second machine (you should be able to ssh into yourself).

Resources