Skip to content

mail: fix exit code handling of sendmail cmd #18765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: PHP-8.3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions ext/standard/mail.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
#include "ext/date/php_date.h"
#include "zend_smart_str.h"

#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif

#ifdef HAVE_SYSEXITS_H
# include <sysexits.h>
#endif
Expand Down Expand Up @@ -417,6 +421,7 @@ PHPAPI int php_mail(const char *to, const char *subject, const char *message, co
#endif
FILE *sendmail;
int ret;
int wstatus;
char *sendmail_path = INI_STR("sendmail_path");
char *sendmail_cmd = NULL;
char *mail_log = INI_STR("mail.log");
Expand Down Expand Up @@ -557,24 +562,41 @@ PHPAPI int php_mail(const char *to, const char *subject, const char *message, co
fprintf(sendmail, "%s%s", hdr, line_sep);
}
fprintf(sendmail, "%s%s%s", line_sep, message, line_sep);
#ifdef PHP_WIN32
ret = pclose(sendmail);

#if PHP_SIGCHILD
if (sig_handler) {
signal(SIGCHLD, sig_handler);
}
#endif

#ifdef PHP_WIN32
if (ret == -1)
#else
wstatus = pclose(sendmail);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I'm just reading the manual for pclose ( https://man7.org/linux/man-pages/man3/popen.3.html ) and it says:

pclose(): on success, returns the exit status of the command; if
wait4(2) returns an error, or some other error is detected, -1 is returned.

That's for Linux. However FreeBSD docs ( https://man.freebsd.org/cgi/man.cgi?query=pclose&sektion=3&manpath=FreeBSD+14.2-RELEASE+and+Ports ) say this:

The pclose() function returns -1 if stream is not associated with a "popened" command, if stream already "pclosed", or if wait4(2) returns an error.

So that would match with your description.

Are you experience this issue only on BSD platform or is Linux working differently than documented?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried to read glibc source and it's using posix_spawn for popen but doen't see exactly the logic for return code in pclose which just uses normal IO way of closing the FILE. Would need to spend more time on it to see.

In any case not sure if we should merge anything not matching the docs as there might be other variant. Maybe if it's FreeBSD only, it should be fine.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm testing on a Debian Linux box, the documentation is a bit hard to parse, but on the Linux man page you referenced the last paragraph of the description says:

The pclose() function waits for the associated process to
terminate and returns the exit status of the command as returned
by wait4(2).

Which ultimately leads to man 2 wait as being the return value, the same as the BSDs.

Here is small program to test the behavior:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
  FILE *pipe;
  int wstatus;

  pipe = popen("exit 75", "r");

  if (pipe == NULL) {
    perror("popen failed");
    exit(EXIT_FAILURE);
  }

  wstatus = pclose(pipe);

  if (wstatus == -1) {
    perror("pclose failed");
    exit(EXIT_FAILURE);
  } else {
    printf("pclose status %d\n", wstatus);
    if (WIFEXITED(wstatus)) {
      printf("WEXITSTATUS status: %d\n", WEXITSTATUS(wstatus));
    } else if (WIFSIGNALED(wstatus)) {
      printf("child killed by signal %d (%s)\n", WTERMSIG(wstatus),
             strsignal(WTERMSIG(wstatus)));
    } else {
      printf("unknown status %x\n", wstatus);
    }
  }
  return EXIT_SUCCESS;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was also confused by the terminology in the man-page on Ubuntu, but at least the Musl implementation also just passes along the value retrieved from wait4:

https://github.com/esmil/musl/blob/194f9cf93da8ae62491b7386edf481ea8565ae4e/src/stdio/pclose.c

So the implementation seems correct to me.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @TimWolla the musl source code is unbelievably readable!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah looks like you are right. The docs are quite confusing though (although the description paragraph that I didn't initially read makes it more clear) so not surprised that this was implemented in this way.

#if PHP_SIGCHILD
if (sig_handler) {
signal(SIGCHLD, sig_handler);
}
#endif
/* Determine the wait(2) exit status */
if (wstatus == -1) {
MAIL_RET(0);
} else if (WIFSIGNALED(wstatus)) {
MAIL_RET(0);
} else {
if (WIFEXITED(wstatus)) {
ret = WEXITSTATUS(wstatus);
} else {
MAIL_RET(0);
}
}
#endif

#if defined(EX_TEMPFAIL)
if ((ret != EX_OK)&&(ret != EX_TEMPFAIL))
#elif defined(EX_OK)
if (ret != EX_OK)
#else
Comment on lines 594 to 598
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it intentional that this is not also running for Windows? The commit message is not entirely clear on that and I lack the Windows expertise.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Windows use c client so it doesn't spawn external process.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to actually re-read it how it works - we have got a smtp implementation but seems like it's using popen_ex in some cases.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Windows doesn't inlude, sysexits.h where these are defined, so that is intentional.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, it would need sendmail path to be specified but the default is NULL is it will usually use the c client but there is a possibility it might use it. In terms of this check I suppose that it won't probably report error in pclose so it's just ignored.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my read of that documentation, the return value is -1, if _pclose fails, otherwise it is the return value of the process. So if _pclose returns something other than 0, we should also fail.

if (ret != 0)
#endif
#endif
{
MAIL_RET(0);
Expand Down
22 changes: 22 additions & 0 deletions ext/standard/tests/mail/mail_variation3.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
Test mail() function : variation sendmail temp fail
--INI--
sendmail_path=exit 75
--SKIPIF--
<?php
if (substr(PHP_OS, 0, 3) == "WIN")
die("skip Won't run on Windows");
?>
--FILE--
<?php
echo "*** Testing mail() : variation ***\n";

// Initialise all required variables
$to = '[email protected]';
$subject = 'Test Subject';
$message = 'A Message';
var_dump(mail($to, $subject, $message));
?>
--EXPECT--
*** Testing mail() : variation ***
bool(true)
22 changes: 22 additions & 0 deletions ext/standard/tests/mail/mail_variation4.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
Test mail() function : variation sigterm
--INI--
sendmail_path="kill \$\$"
--SKIPIF--
<?php
if (substr(PHP_OS, 0, 3) == "WIN")
die("skip Won't run on Windows");
?>
--FILE--
<?php
echo "*** Testing mail() : variation ***\n";

// Initialise all required variables
$to = '[email protected]';
$subject = 'Test Subject';
$message = 'A Message';
var_dump(mail($to, $subject, $message));
?>
--EXPECT--
*** Testing mail() : variation ***
bool(false)
22 changes: 22 additions & 0 deletions ext/standard/tests/mail/mail_variation5.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
Test mail() function : variation non-zero exit
--INI--
sendmail_path="exit 123"
--SKIPIF--
<?php
if (substr(PHP_OS, 0, 3) == "WIN")
die("skip Won't run on Windows");
?>
--FILE--
<?php
echo "*** Testing mail() : variation ***\n";

// Initialise all required variables
$to = '[email protected]';
$subject = 'Test Subject';
$message = 'A Message';
var_dump(mail($to, $subject, $message));
?>
--EXPECT--
*** Testing mail() : variation ***
bool(false)
Loading