scstadmin: Support passing an empty string to set*Attribute()

Validate the $value argument of set functions with defined() instead of
length(). Append a newline to the end of $value before writing it into
a sysfs attribute to make sure something gets written if $value eq "".


git-svn-id: http://svn.code.sf.net/p/scst/svn/trunk@9158 d57e44dd-8a1f-0410-8b47-8ef2f437770f
This commit is contained in:
Bart Van Assche
2020-09-14 01:56:26 +00:00
parent 7e3fe7a80e
commit 6d60231631
2 changed files with 42 additions and 12 deletions
@@ -492,7 +492,7 @@ sub setScstAttribute {
my $attribute = shift;
my $value = shift;
return TRUE if (!length($attribute) || !length($value));
return TRUE if (!length($attribute) || !defined($value));
my $bytes = -ENOENT;
my $path = make_path(SCST_ROOT_DIR(), $attribute);
@@ -501,6 +501,7 @@ sub setScstAttribute {
if ($self->{'debug'}) {
print "DBG($$): $path -> $attribute = $value\n";
} else {
$value .= "\n";
$bytes = _syswrite($io, $value, length($value));
}
close $io;
@@ -2587,7 +2588,7 @@ sub setDriverAttribute {
my $attribute = shift;
my $value = shift;
return TRUE if (!length($attribute) || !length($value));
return TRUE if (!length($attribute) || !defined($value));
my $path = make_path(SCST_TARGETS_DIR(), $driver, $attribute);
@@ -2597,6 +2598,7 @@ sub setDriverAttribute {
if ($self->{'debug'}) {
print "DBG($$): $path -> $attribute = $value\n";
} else {
$value .= "\n";
$bytes = _syswrite($io, $value, length($value));
}
close $io;
@@ -2724,7 +2726,7 @@ sub setTargetAttribute {
my $value = shift;
return TRUE if (!length($driver) || !length($target) ||
!length($attribute) || !length($value));
!length($attribute) || !defined($value));
my ($path, $cmd);
$path = make_path(SCST_TARGETS_DIR(), $driver, $target, $attribute);
@@ -2850,7 +2852,7 @@ sub setGroupAttribute {
my $attribute = shift;
my $value = shift;
return TRUE if (!length($attribute) || !length($value));
return TRUE if (!length($attribute) || !defined($value));
my ($path, $cmd);
$path = make_path(SCST_TARGETS_DIR(), $driver, $target, SCST_GROUPS,
@@ -2863,6 +2865,7 @@ sub setGroupAttribute {
if ($self->{'debug'}) {
print "DBG($$): $cmd > $path\n";
} else {
$value .= "\n";
$bytes = _syswrite($io, $value, length($value));
}
close $io;
@@ -3002,7 +3005,7 @@ sub setLunAttribute {
return SCST_C_LUN_SETATTR_FAIL
if (!length($driver) || !length($target) || !length($lun) ||
!length($attribute) || !length($value));
!length($attribute) || !defined($value));
my $path;
@@ -3021,6 +3024,7 @@ sub setLunAttribute {
if ($self->{'debug'}) {
print "DBG($$): $path -> $attribute = $value\n";
} else {
$value .= "\n";
$bytes = _syswrite($io, $value, length($value));
}
close $io;
@@ -3152,7 +3156,7 @@ sub setInitiatorAttribute {
my $attribute = shift;
my $value = shift;
return TRUE if (!length($attribute) || !length($value));
return TRUE if (!length($attribute) || !defined($value));
my $path = make_path(SCST_TARGETS_DIR(), $driver, $target, SCST_GROUPS,
$group, SCST_LUNS, $initiator, $attribute);
@@ -3163,6 +3167,7 @@ sub setInitiatorAttribute {
if ($self->{'debug'}) {
print "DBG($$): $path -> $attribute = $value\n";
} else {
$value .= "\n";
$bytes = _syswrite($io, $value, length($value));
}
close $io;
@@ -3439,7 +3444,7 @@ sub setAluaAttribute {
my $attribute = shift;
my $value = shift;
return TRUE if (!length($attribute) || !length($value));
return TRUE if (!length($attribute) || !defined($value));
my $bytes = - ENOENT;
my $path = make_path(SCST_DEV_GROUP_DIR(), $attribute);
@@ -3448,6 +3453,7 @@ sub setAluaAttribute {
if ($self->{'debug'}) {
print "DBG($$): $value > $path\n";
} else {
$value .= "\n";
$bytes = _syswrite($io, $value, length($value));
}
close $io;
@@ -3465,7 +3471,7 @@ sub setDeviceGroupAttribute {
my $attribute = shift;
my $value = shift;
return TRUE if (!length($attribute) || !length($value));
return TRUE if (!length($attribute) || !defined($value));
my $bytes = - ENOENT;
my $path = make_path(SCST_DEV_GROUP_DIR(), $group, $attribute);
@@ -3474,6 +3480,7 @@ sub setDeviceGroupAttribute {
if ($self->{'debug'}) {
print "DBG($$): $path -> $attribute = $value\n";
} else {
$value .= "\n";
$bytes = _syswrite($io, $value, length($value));
}
close $io;
@@ -3497,7 +3504,7 @@ sub setTargetGroupAttribute {
my $value = shift;
return TRUE if (!length($group) || !length($tgroup) ||
!length($attribute) || !length($value));
!length($attribute) || !defined($value));
my $bytes = - ENOENT;
my $path = make_path(SCST_DEV_GROUP_DIR(), $group, SCST_DG_TGROUPS, $tgroup, $attribute);
@@ -3506,6 +3513,7 @@ sub setTargetGroupAttribute {
if ($self->{'debug'}) {
print "DBG($$): $path -> $attribute = $value\n";
} else {
$value .= "\n";
$bytes = _syswrite($io, $value, length($value));
}
close $io;
@@ -3535,7 +3543,7 @@ sub setTargetGroupTargetAttribute {
return TRUE if (!length($group) || !length($tgroup) ||
!length($tgt) || !length($attribute) ||
!length($value));
!defined($value));
my $bytes = - ENOENT;
my $path = make_path(SCST_DEV_GROUP_DIR(), $group, SCST_DG_TGROUPS,
@@ -3545,6 +3553,7 @@ sub setTargetGroupTargetAttribute {
if ($self->{'debug'}) {
print "DBG($$): $path -> $attribute = $value\n";
} else {
$value .= "\n";
$bytes = _syswrite($io, $value, length($value));
}
close $io;
@@ -3616,7 +3625,7 @@ sub setHandlerAttribute {
my $attribute = shift;
my $value = shift;
return TRUE if (!length($attribute) || !length($value));
return TRUE if (!length($attribute) || !defined($value));
my $bytes = - ENOENT;
my $path = make_path(SCST_HANDLERS_DIR(), $handler, $attribute);
@@ -3625,6 +3634,7 @@ sub setHandlerAttribute {
if ($self->{'debug'}) {
print "DBG($$): $path -> $attribute = $value\n";
} else {
$value .= "\n";
$bytes = _syswrite($io, $value, length($value));
}
close $io;
@@ -4622,6 +4632,7 @@ sub make_path {
if ($path && rindex($path, '/') != length($path) - 1) {
$path .= '/';
}
cluck("make_path: invalid argument") if !length($element);
$path .= $element;
}
@@ -5,7 +5,7 @@ use warnings;
use Test;
BEGIN {
plan tests => 6 + ($> == 0 ? 4 : 0);
plan tests => 7 + ($> == 0 ? 8 : 0);
}
use SCST::SCST;
@@ -22,13 +22,21 @@ my $_DEBUG_ = 0;
my $SCST = eval { new SCST::SCST($_DEBUG_) };
die("Creation of SCST object failed") if (!defined($SCST));
# Missing `attribute` and `value` arguments.
ok($SCST->setScstAttribute(), 1);
# `attribute` argument is an empty string.
ok($SCST->setScstAttribute(''), 1);
# `attribute` argument is not an existing attribute and the `value` argument is
# missing.
ok($SCST->setScstAttribute('no-such-attribute'), 1);
# `attribute` argument is not an existing attribute.
ok($SCST->setScstAttribute('no-such-attribute', '1'),
$SCST->SCST_C_BAD_ATTRIBUTES);
# Attempt to modify a read-only attribute.
ok($SCST->setScstAttribute('last_sysfs_mgmt_res', '1'),
$SCST->SCST_C_ATTRIBUTE_STATIC);
@@ -36,6 +44,7 @@ my $threads = getScstThreadCount($SCST);
ok(ref(\$threads), "SCALAR");
ok(defined($threads));
# $> represents the effective user ID of this process.
if ($> == 0) {
ok($SCST->setScstAttribute('threads', $threads + 1), 0);
@@ -44,4 +53,14 @@ if ($> == 0) {
ok($SCST->setScstAttribute('threads', $threads), 0);
ok(getScstThreadCount($SCST), $threads);
ok($SCST->setScstAttribute('measure_latency', undef), 1);
ok($SCST->setScstAttribute('measure_latency', ""),
$SCST->SCST_C_SETATTR_FAIL);
ok($SCST->setScstAttribute('measure_latency', "."),
$SCST->SCST_C_SETATTR_FAIL);
ok($SCST->setScstAttribute('measure_latency', "0"), 0);
}