【发布时间】:2017-11-15 20:41:02
【问题描述】:
我正在尝试在单独的pthread 中发送一封带有sendmail 的电子邮件。这段代码在 99.9% 的情况下都有效。
void* emailClientThreadFct(void* emailClientPtr)
{
EmailClient* emailClient = static_cast<EmailClient*>(emailClientPtr);
try
{
emailClient->Send();
}
catch (const exception& excep)
{
SYSLOG_ERROR("E-mail client exception: %s", excep.what());
}
delete emailClient;
return NULL;
}
// Send email for current output in a separate thread
pthread_t emailThread;
pthread_attr_t attr;
/* Initialize and set thread detached attribute */
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pthread_create(&emailThread, &attr, emailClientThreadFct, emailClientObj);
0.1% 的时间,我在执行以下调用时收到错误 fwrite error Broken Pipe。根据我的阅读,Broken Pipe (EPIPE 32) 通常是一个接收器问题,但 sendmail 是一个本地进程......可能是我发送了太多数据来 fwrite 吗?或者我在我的 pthread 实例化中做错了什么?还是 sendmail 崩溃了?
void EmailClient::Send() const
{
// Flush all open output streams, as recommended by popen man page
fflush(NULL);
string popen_command = "sendmail -t -oi >/dev/null 2>&1");
// Open pipe to Mail Transport Agent (MTA)
errno = 0;
FILE* stream = popen(popen_command.c_str(), "w");
if (stream == NULL)
{
throw exception("Cannot send email popen");
}
errno = 0;
if (fwrite(message.data(), message.size(), 1, stream) < 1)
{
pclose(stream);
throw exception("fwrite error ", strerror(errno));
}
// Close MTA
errno = 0;
if (pclose(stream) == -1)
printf("\"Error closing the MTA pipe (%s)\"", strerror(errno))
}
【问题讨论】:
标签: c++ pthreads sendmail popen broken-pipe