Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: I/O Watchdog Daemon

by GlitchMr (Sexton)
on Aug 22, 2012 at 09:25 UTC ( #988986=note: print w/replies, xml ) Need Help??

in reply to I/O Watchdog Daemon

I would assume that "system is out to get you", so you should just assume that trying to read files will fail, but what exactly will happen doesn't matter.

For example, when you open file or next FILE, I would instead rerun glob and try again (to protect from files removed in /usr/bin - you wouldn't want this to reboot system, right?). As for basic idea, I've something similar to your script except more... paranoic (when something will go wrong, it will reboot). You don't need to know what will happen when EIO will appear - some condition in the code (or autodie pragma) will catch something is wrong (what exactly is wrong doesn't matter).

#!/usr/bin/perl # License: Do what you want with this code snippet. You could even # claim it as yours and remove this license. I don't care. # Automatically die when something goes wrong use autodie; use constant DIRECTORY => '/usr/bin'; use strictures 1; use Try::Tiny; my @files; sub glob_files { my ($directory) = @_; opendir( my $dh, $directory ); # On my system, /usr/bin has directories such as /usr/bin/X11 return grep { -f "$directory/$_" } readdir $dh or die "Empty glob" +; } sub try_random_file { my ($file) = @_; open my $fh, '<', $file; # Four bytes should be enough to determine if stuff works read $fh, my $buffer, 4; # Shouldn't happen if ( length $buffer != 4 ) { die 'File is too small.'; } } sub reboot { my ($error) = @_; exec '/var/tmpfs/reboot', '-nf'; } @files = glob_files DIRECTORY; while (1) { try { my $file = $files[ int rand @files ]; try { try_random_file DIRECTORY . "/$file"; } catch { # If file doesn't exist then it was possibly deleted. # Reglob everything and try again with other file. If # it won't work then something is simply broken. die $_ if -f $file; @files = glob_files DIRECTORY; try_random_file DIRECTORY . "/$files[ int rand @files ]"; } } catch { reboot $_; }; # Usually, you should sleep just so script won't take entire CPU sleep 15; }

Replies are listed 'Best First'.
Re^2: I/O Watchdog Daemon
by IdleResonance (Acolyte) on Aug 22, 2012 at 15:34 UTC

    Interesting script. I'll dig through it a bit later on this evening. As per your concerns, if a file were removed it would not cause a reboot. Instead, the open would fail and it would try the next file. I will be adding a test to ensure that it is a file, but even in the case that it is a directory, the sysread will return EISDIR instead of EIO and would just sleep for 15 seconds and try the next file.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://988986]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2018-03-23 01:53 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (287 votes). Check out past polls.