Reputation: 4452
I would like to interface with linux directly from perl, to execute some calls such as statvfs. I am curious if there is a way of doing this with just the core perl package (no additional modules).
In particular, I'd like to get disk usage information for servers that don't have df installed/enabled.
Upvotes: 2
Views: 596
Reputation: 80433
I would like to interface with linux directly from perl, to execute some calls such as statvfs. I am curious if there is a way of doing this with just the core perl package (no additional modules).
The technical, but probably not very useful, answer to your question is that yes, there does indeed exist a way of doing what statvfs does with just core perl and no modules. It works like this:
my $statfs_buffer = "\0" x length(pack($STATFS_TEMPLATE, ()));
my $retval = syscall($SYS_statfs, $some_filename, $statfs_buffer);
if ($retval == -1) {
die "statfs failed: $!";
}
my($f_type, $f_bsize, $f_blocks, $f_bfree,
$f_bavail, $f_files, $f_ffree,
$f_fsid, $f_namelen) = unpack($STATFS_TEMPLATE, $statfs_buffer);
The problem is you’ll have to figure out the values for $SYS_statfs
and $STATFS_TEMPLATE
yourself; the former is easy, and the latter is not hard.
However, on some systems you’ll have to use the statfs64
variant of those.
So yes, it is possible to do what you want, provided that you’re determined enough. There are better ways to do it, though.
Upvotes: 2
Reputation: 10569
use POSIX;
comes to mind. statvfs is however not among the functions offered by it. Perhaps you are looking for use Filesys::Df;
here.
I have yet to see a system that has perl, but not coreutils(df) installed...
Upvotes: 3