-
Notifications
You must be signed in to change notification settings - Fork 7.9k
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
base: PHP-8.3
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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"); | ||
|
@@ -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); | ||
#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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Windows use c client so it doesn't spawn external process. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Windows doesn't inlude, There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah it's actually just check for -1 as document in https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/pclose?view=msvc-170 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From my read of that documentation, the return value is -1, if |
||
if (ret != 0) | ||
#endif | ||
#endif | ||
{ | ||
MAIL_RET(0); | ||
|
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) | ||
TimWolla marked this conversation as resolved.
Show resolved
Hide resolved
|
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) |
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) |
There was a problem hiding this comment.
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:
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:
So that would match with your description.
Are you experience this issue only on BSD platform or is Linux working differently than documented?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
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:
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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.