From 95246ee9d7b8bf7f5f173cae211523dd9241ed1f Mon Sep 17 00:00:00 2001 From: Bart Van Assche Date: Mon, 9 Jul 2012 06:12:47 +0000 Subject: [PATCH] scstadmin: Make _syswrite() translate EBUSY into "writing failed" _syswrite() callers check whether writing into an SCST sysfs attribute succeeded by checking whether the value returned by this function is defined(). Return undef (failed) instead of -1 (success) if syswrite() returned error code EBUSY (found this through source reading). Return $length instead of length($cmd) if the initial write triggered EAGAIN. Remove a superfluous $bytes = undef statement. Eliminate the variable $wait. git-svn-id: http://svn.code.sf.net/p/scst/svn/trunk@4400 d57e44dd-8a1f-0410-8b47-8ef2f437770f --- .../scst-0.9.10/lib/SCST/SCST.pm | 43 +++++++++---------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/scstadmin/scstadmin.sysfs/scst-0.9.10/lib/SCST/SCST.pm b/scstadmin/scstadmin.sysfs/scst-0.9.10/lib/SCST/SCST.pm index 5f0ff2e16..8c170b677 100644 --- a/scstadmin/scstadmin.sysfs/scst-0.9.10/lib/SCST/SCST.pm +++ b/scstadmin/scstadmin.sysfs/scst-0.9.10/lib/SCST/SCST.pm @@ -4855,6 +4855,8 @@ sub errorString { return $string; } +# Write the first $3 bytes of $2 into the SCST sysfs file $1. Return either +# the number of bytes written or undef if writing failed. sub _syswrite { my $io = shift; my $cmd = shift; @@ -4865,35 +4867,32 @@ sub _syswrite { my $bytes = syswrite($io, $cmd, $length); - if (!defined($bytes) && defined($res_file)) { - if ($! == EAGAIN) { - my $res = new IO::File $res_file, O_RDONLY; + if (!defined($bytes) && defined($res_file) && $! == EAGAIN) { + my $res_io = new IO::File $res_file, O_RDONLY; - if (!$res) { - cluck("FATAL: Failed opening $res_file: $!"); - return undef; - } + if (!$res_io) { + cluck("FATAL: Failed opening $res_file: $!"); + return undef; + } - my $wait = TRUE; - my $result; + my $res_val; - while ($wait && (($now + $TIMEOUT) > time())) { - sysread($res, $result, 8); - $wait = FALSE if ($! != EAGAIN); + while (($now + $TIMEOUT) > time()) { + if (!defined(sysread($res_io, $res_val, 8)) && + $! == EAGAIN) { sleep 1; - } - - if ($wait) { - my $_cmd = $cmd; chomp $_cmd; - cluck("Timeout while waiting for command '$_cmd' to complete"); - $bytes = undef; } else { - $bytes = length($cmd) if ($result == 0); + last; } + } - close $res; - } elsif ($! == EBUSY) { - return -1; + close $res_io; + + if (!defined($res_val)) { + my $_cmd = $cmd; chomp $_cmd; + cluck("Timeout while waiting for command '$_cmd' to complete"); + } elsif ($res_val == 0) { + $bytes = $length; } }