I'm trying to use perl to parse some pseudo files from /proc
and /sys
linux pseudo filesystems (procfs and sysfs). Such files are unlike regular files - they are implemented by custom file operation handlers. Most of them have zero size for stat
, some can't be open for read, other can't be written. Sometimes they are implemented incorrectly (which is error, but it is already in the kernel) and I still want to read them directly from perl without starting some helper tools.
There is quick example of reading /proc/loadavg
with perl, this file is implemented correctly:
perl -e 'open F,"</proc/loadavg"; $_=<F>; print '
With strace
of the command I can check how perl implements open
function:
$ strace perl -e 'open F,"</proc/loadavg"; $_=<F>; print ' 2>&1 | egrep -A5 ^open.*loadavg
open("/proc/loadavg", O_RDONLY) = 3
ioctl(...something strange...) = -1 ENOTTY
lseek(3, 0, SEEK_CUR) = 0
fstat(3, {st_mode=S_IFREG|0444, st_size=0, ...}) = 0
fcntl(3, F_SETFD, FD_CLOEXEC) = 0
There is lseek
system call used by open
perl function.
With strace of cat /proc/loadavg
there was no extra seek
-typed system calls:
$ strace cat /proc/loadavg 2>&1 | egrep -A2 ^open.*loadavg
open("/proc/loadavg", O_RDONLY) = 3
fstat(3, {st_mode=S_IFREG|0444, st_size=0, ...}) = 0
fadvise64(3, 0, 0, POSIX_FADV_SEQUENTIAL) = 0
The special file I want to read (or write) misimplement seek
file operations and will not give any useful data to read
(or to write
) syscall after seek
.
Is there way of opening files for reading in perl5 (no external modules) without calling extra lseek
? (and without using system("cat < /proc/loadavg")
)
Is there way of opening files for writing in perl5 without calling extra lseek
?
There is sysopen, but it does extra lseek too: perl -e 'use Fcntl;sysopen(F,"/proc/loadavg",O_RDONLY);sysread(F,$_,2048); print '