2

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.

GoldenNewby
  • 4,382
  • 8
  • 33
  • 44

2 Answers2

3

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

jørgensen
  • 10,149
  • 2
  • 20
  • 27
  • 2
    +1 for Filesys::Df. [Parsing `df` output](http://stackoverflow.com/a/6351257) is very error-prone. – daxim Mar 15 '12 at 09:08
  • Read the question: you aren’t allowed to use `Filesys::Df`, beacause it’s not part of core perl. – tchrist Mar 15 '12 at 15:54
2

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.

tchrist
  • 78,834
  • 30
  • 123
  • 180
  • require 'sys/syscall.ph' to get SYS_statfs – MkV Mar 15 '12 at 15:26
  • @tchrist How would I go about determining the values for either of those to variables? Would it change system to system? Or is it related to the operating system/architecture (or something like that)? – GoldenNewby Mar 15 '12 at 18:52
  • @GoldenNewby The easiest way is to write a little C program. Yes, it changes between systems. The BSD `statfs` structure looks different from the Linux one, and the syscall numbers are different, too. – tchrist Mar 15 '12 at 19:45