Skip to content

Commit be1546b

Browse files
committed
ktest.pl: Add MAIL_PATH option to define where to find the mailer
The option MAIL_PATH lets the user decide how to find the mailer they are using. For example, sendmail is usually located in /usr/sbin but is not always in the path of non admin users. Have ktest look through the user's PATH environment variable (adding /usr/sbin) as well, but if that's not good enough, allow the user to define where to find the mailer. Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org> squash to mail exec Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
1 parent f5ef488 commit be1546b

2 files changed

Lines changed: 35 additions & 5 deletions

File tree

‎tools/testing/ktest/ktest.pl‎

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
#default opts
2525
my %default = (
26-
"MAILER" => "sendmail", # default mailer
26+
"MAILER" => "sendmail", # default mailer
2727
"EMAIL_ON_ERROR" => 1,
2828
"EMAIL_WHEN_FINISHED" => 1,
2929
"EMAIL_WHEN_CANCELED" => 0,
@@ -218,6 +218,7 @@
218218

219219
my $mailto;
220220
my $mailer;
221+
my $mail_path;
221222
my $email_on_error;
222223
my $email_when_finished;
223224
my $email_when_started;
@@ -250,8 +251,9 @@
250251
my $reboot_success = 0;
251252

252253
my %option_map = (
253-
"MAILTO" => \$mailto,
254-
"MAILER" => \$mailer,
254+
"MAILTO" => \$mailto,
255+
"MAILER" => \$mailer,
256+
"MAIL_PATH" => \$mail_path,
255257
"EMAIL_ON_ERROR" => \$email_on_error,
256258
"EMAIL_WHEN_FINISHED" => \$email_when_finished,
257259
"EMAIL_WHEN_STARTED" => \$email_when_started,
@@ -4126,12 +4128,29 @@ sub set_test_option {
41264128

41274129
sub _mailx_send {
41284130
my ($subject, $message) = @_;
4129-
system("$mailer -s \'$subject\' $mailto <<< \'$message\'");
4131+
system("$mail_path/$mailer -s \'$subject\' $mailto <<< \'$message\'");
41304132
}
41314133

41324134
sub _sendmail_send {
41334135
my ($subject, $message) = @_;
4134-
system("echo -e \"Subject: $subject\n\n$message\" | sendmail -t $mailto");
4136+
system("echo -e \"Subject: $subject\n\n$message\" | $mail_path/sendmail -t $mailto");
4137+
}
4138+
4139+
sub find_mailer {
4140+
my ($mailer) = @_;
4141+
4142+
my @paths = split /:/, $ENV{PATH};
4143+
4144+
# sendmail is usually in /usr/sbin
4145+
$paths[$#paths + 1] = "/usr/sbin";
4146+
4147+
foreach my $path (@paths) {
4148+
if (-x "$path/$mailer") {
4149+
return $path;
4150+
}
4151+
}
4152+
4153+
return undef;
41354154
}
41364155

41374156
sub send_email {
@@ -4140,6 +4159,13 @@ sub send_email {
41404159
doprint "No email sent: email or mailer not specified in config.\n";
41414160
return;
41424161
}
4162+
if (!defined($mail_path)) {
4163+
# find the mailer
4164+
$mail_path = find_mailer $mailer;
4165+
if (!defined($mail_path)) {
4166+
die "\nCan not find $mailer in PATH\n";
4167+
}
4168+
}
41434169
if ($mailer eq "mail" || $mailer eq "mailx"){ _mailx_send(@_);}
41444170
elsif ($mailer eq "sendmail" ) { _sendmail_send(@_);}
41454171
else { doprint "\nYour mailer: $mailer is not supported.\n" }

‎tools/testing/ktest/sample.conf‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,10 @@
411411
# (default sendmail)
412412
#MAILER = sendmail
413413
#
414+
# The executable to run
415+
# (default: for sendmail "/usr/sbin/sendmail", otherwise equals ${MAILER})
416+
#MAIL_EXEC = /usr/sbin/sendmail
417+
#
414418
# Errors are defined as those would terminate the script
415419
# (default 1)
416420
#EMAIL_ON_ERROR = 1

0 commit comments

Comments
 (0)