linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: Tim Tianyang Chen <tianyang.chen@oracle.com>
Cc: linux-kernel@vger.kernel.org, dhaval.giani@oracle.com
Subject: Re: [PATCH v3 1/4] Ktest: add email support
Date: Sat, 7 Apr 2018 18:51:50 -0400	[thread overview]
Message-ID: <20180407185150.461d9a52@gandalf.local.home> (raw)
In-Reply-To: <d9720e54-4207-abf6-2ac2-b94ac847674e@oracle.com>

On Fri, 6 Apr 2018 16:09:28 -0700
Tim Tianyang Chen <tianyang.chen@oracle.com> wrote:


> >   sub _sendmail_send {
> >       my ($subject, $message) = @_;
> > -    system("echo -e \"Subject: $subject\n\n$message\" | sendmail -t $mailto");
> > +
> > +    if (!defined($mail_exec)) {
> > +	$mail_exec = "/usr/sbin/sendmail";
> > +    }
> > +    run_command "echo \'Subject: $subject\n\n$message\' | $mail_exec -t $mailto";
> >   }
> >   
> Not sure if I understand why $mail_exec is necessary. Doesn't $mailer 
> already have a default? Wouldn't people just use $mailer to define the 
> executable they want to use? What if the $mailx_exec specified doesn't 
> use '-t' option?

sendmail isn't in my path, I had to specify the path name to find it.
Hmm, we could have "MAILER_PATH" to find it. I can change that.

Actually, what we should also add is the format to print it out.

MAIL_COMMAND = echo \'Subject: $SUBJECT\n\n$MESSAGE\' | sendmail -t ${MAILTO}

Note the difference between $SUBJECT, $MESSAGE and ${MAILTO}. When we
have ${FOO} that is a variable the user controls. But $FOO is a
variable that is hard coded for certain commands.

That may be better to have.

Here's my latest total diff:

diff --git a/tools/testing/ktest/ktest.pl b/tools/testing/ktest/ktest.pl
index 30a4c053f98b..a14fc309d140 100755
--- a/tools/testing/ktest/ktest.pl
+++ b/tools/testing/ktest/ktest.pl
@@ -23,7 +23,7 @@ my %evals;
 
 #default opts
 my %default = (
-    "MAILER"				=> "sendmail",  # default mailer
+    "MAILER"			=> "sendmail",  # default mailer
     "EMAIL_ON_ERROR"		=> 1,
     "EMAIL_WHEN_FINISHED"	=> 1,
     "EMAIL_WHEN_CANCELED"	=> 0,
@@ -218,6 +218,8 @@ my $dirname = $FindBin::Bin;
 
 my $mailto;
 my $mailer;
+my $mail_path;
+my $mail_command;
 my $email_on_error;
 my $email_when_finished;
 my $email_when_started;
@@ -250,8 +252,10 @@ my $no_reboot = 1;
 my $reboot_success = 0;
 
 my %option_map = (
-    "MAILTO"				=> \$mailto,
-    "MAILER"				=> \$mailer,
+    "MAILTO"			=> \$mailto,
+    "MAILER"			=> \$mailer,
+    "MAIL_PATH"			=> \$mail_path,
+    "MAIL_COMMAND"		=> \$mail_command,
     "EMAIL_ON_ERROR"		=> \$email_on_error,
     "EMAIL_WHEN_FINISHED"	=> \$email_when_finished,
     "EMAIL_WHEN_STARTED"	=> \$email_when_started,
@@ -1431,7 +1435,14 @@ sub do_not_reboot {
 	($test_type eq "config_bisect" && $opt{"CONFIG_BISECT_TYPE[$i]"} eq "build");
 }
 
+my $in_die = 0;
+
 sub dodie {
+
+    # avoid recusion
+    return if ($in_die);
+    $in_die = 1;
+
     doprint "CRITICAL FAILURE... ", @_, "\n";
 
     my $i = $iteration;
@@ -4124,23 +4135,61 @@ sub set_test_option {
     return eval_option($name, $option, $i);
 }
 
-sub _mailx_send {
-    my ($subject, $message) = @_;
-    system("$mailer -s \'$subject\' $mailto <<< \'$message\'");
+sub find_mailer {
+    my ($mailer) = @_;
+
+    my @paths = split /:/, $ENV{PATH};
+
+    # sendmail is usually in /usr/sbin
+    $paths[$#paths + 1] = "/usr/sbin";
+
+    foreach my $path (@paths) {
+	if (-x "$path/$mailer") {
+	    return $path;
+	}
+    }
+
+    return undef;
 }
 
-sub _sendmail_send {
+sub do_send_mail {
     my ($subject, $message) = @_;
-    system("echo -e \"Subject: $subject\n\n$message\" | sendmail -t $mailto");
+
+    if (!defined($mail_path)) {
+	# find the mailer
+	$mail_path = find_mailer $mailer;
+	if (!defined($mail_path)) {
+	    die "\nCan not find $mailer in PATH\n";
+	}
+    }
+
+    if (!defined($mail_command)) {
+	if ($mailer eq "mail" || $mailer eq "mailx") {
+	    $mail_command = "\$MAIL_PATH/\$MAILER -s \'\$SUBJECT\' \$MAILTO <<< \'\$MESSAGE\'";
+	} elsif ($mailer eq "sendmail" ) {
+	    $mail_command =  "echo \'Subject: \$SUBJECT\n\n\$MESSAGE\' | \$MAIL_PATH/\$MAILER -t \$MAILTO";
+	} else {
+	    die "\nYour mailer: $mailer is not supported.\n";
+	}
+    }
+
+    $mail_command =~ s/\$MAILER/$mailer/g;
+    $mail_command =~ s/\$MAIL_PATH/$mail_path/g;
+    $mail_command =~ s/\$MAILTO/$mailto/g;
+    $mail_command =~ s/\$SUBJECT/$subject/g;
+    $mail_command =~ s/\$MESSAGE/$message/g;
+
+    run_command $mail_command;
 }
 
 sub send_email {
-    if (defined($mailto) && defined($mailer)) {
-        if ($mailer eq "mail" || $mailer eq "mailx"){ _mailx_send(@_);}
-        elsif ($mailer eq "sendmail" ) { _sendmail_send(@_);}
-        else { doprint "\nYour mailer: $mailer is not supported.\n" }
-    } else {
-        print "No email sent: email or mailer not specified in config.\n"
+
+    if (defined($mailto)) {
+	if (!defined($mailer)) {
+	    doprint "No email sent: email or mailer not specified in config.\n";
+	    return;
+	}
+	do_send_mail @_;
     }
 }
 
diff --git a/tools/testing/ktest/sample.conf b/tools/testing/ktest/sample.conf
index d1a2626aaa0a..86e7cffc45c0 100644
--- a/tools/testing/ktest/sample.conf
+++ b/tools/testing/ktest/sample.conf
@@ -411,6 +411,10 @@
 # (default sendmail)
 #MAILER = sendmail
 #
+# The executable to run
+# (default: for sendmail "/usr/sbin/sendmail", otherwise equals ${MAILER})
+#MAIL_EXEC = /usr/sbin/sendmail
+#
 # Errors are defined as those would terminate the script
 # (default 1)
 #EMAIL_ON_ERROR = 1

-- Steve

  parent reply	other threads:[~2018-04-07 22:51 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-26 20:08 [PATCH v3 0/4] Ktest: add email support Tim Tianyang Chen
2018-03-26 20:08 ` [PATCH v3 1/4] " Tim Tianyang Chen
2018-04-06 18:24   ` Steven Rostedt
2018-04-06 22:19     ` Tim Tianyang Chen
2018-04-06 22:41       ` Steven Rostedt
2018-04-06 23:12         ` Tim Tianyang Chen
     [not found]         ` <d9720e54-4207-abf6-2ac2-b94ac847674e@oracle.com>
2018-04-07 22:51           ` Steven Rostedt [this message]
2018-03-26 20:08 ` [PATCH v3 2/4] Ktest: add SigInt handling Tim Tianyang Chen
2018-03-26 20:08 ` [PATCH v3 3/4] Ktest: use dodie for critical falures Tim Tianyang Chen
2018-03-26 20:08 ` [PATCH v3 4/4] Ktest: add email options to sample.config Tim Tianyang Chen
2018-04-03 19:06 ` [PATCH v3 0/4] Ktest: add email support Dhaval Giani
2018-04-03 19:52   ` Tim Tianyang Chen
2018-04-03 22:38     ` Steven Rostedt
2018-04-04 19:50       ` Steven Rostedt
2018-04-06 22:17 [PATCH v3 0/4] ktest: " Tim Tianyang Chen
2018-04-06 22:17 ` [PATCH v3 1/4] " Tim Tianyang Chen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180407185150.461d9a52@gandalf.local.home \
    --to=rostedt@goodmis.org \
    --cc=dhaval.giani@oracle.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=tianyang.chen@oracle.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).