Reading system files using perl without issuing additional search engines in the open

I am trying to use perl to parse some pseudo files from /procand /syspseudo linux file systems ( procfs and sysfs ). Such files are different from ordinary files - they are implemented by handler operations with user files. Most of them are zero in size stat, some cannot be opened for reading, others cannot be written. Sometimes they are executed incorrectly (this is an error, but it is already in the kernel), and I still want to read them directly from perl without starting some auxiliary tools.

There is a quick example of reading /proc/loadavgwith perl, this file runs correctly:

perl -e 'open F,"</proc/loadavg"; $_=<F>; print '

Using the command, straceI can test how Perl implements the function open:

$ 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 a system call lseekused by the openperl function .

cat /proc/loadavgThere were no additional system calls with strace seek:

$ 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

A special file that I want to read (or write) misimplement seekfile operations, and will not give any useful data for read(or write) syscall after seek.

Is there a way to open files for reading in perl5 (without external modules) without calling additional ones lseek? (and without use system("cat < /proc/loadavg"))

Is there a way to open files for writing in perl5 without invoking additional ones lseek?

There is sysopen, but it also does an additional lseek: perl -e 'use Fcntl;sysopen(F,"/proc/loadavg",O_RDONLY);sysread(F,$_,2048); print '

+4
1

, Perl builtin open . , sysopen POSIX::open(), . POSIX::open() , , Perl, Perl POSIX::read() . , .

POSIX perl Perl 5, , , Perl .

+1

Source: https://habr.com/ru/post/1659576/


All Articles