Use poll() for the command pipe fd

refs #10410
This commit is contained in:
Gunnar Beutner 2015-11-10 11:41:21 +01:00
parent 9ef079fd92
commit 0a6505ce2d
2 changed files with 37 additions and 27 deletions

View File

@ -262,7 +262,13 @@ void Socket::Listen(void)
*/ */
size_t Socket::Write(const void *buffer, size_t count) size_t Socket::Write(const void *buffer, size_t count)
{ {
int rc = send(GetFD(), (const char *)buffer, count, 0); int rc;
#ifndef _WIN32
rc = write(GetFD(), (const char *)buffer, count);
#else /* _WIN32 */
rc = send(GetFD(), (const char *)buffer, count, 0);
#endif /* _WIN32 */
if (rc < 0) { if (rc < 0) {
#ifndef _WIN32 #ifndef _WIN32
@ -290,7 +296,13 @@ size_t Socket::Write(const void *buffer, size_t count)
*/ */
size_t Socket::Read(void *buffer, size_t count) size_t Socket::Read(void *buffer, size_t count)
{ {
int rc = recv(GetFD(), (char *)buffer, count, 0); int rc;
#ifndef _WIN32
rc = read(GetFD(), (char *)buffer, count);
#else /* _WIN32 */
rc = recv(GetFD(), (char *)buffer, count);
#endif /* _WIN32 */
if (rc < 0) { if (rc < 0) {
#ifndef _WIN32 #ifndef _WIN32

View File

@ -102,40 +102,38 @@ void ExternalCommandListener::CommandPipeThread(const String& commandPath)
return; return;
} }
Utility::SetNonBlocking(fd, false); FIFO::Ptr fifo = new FIFO();
Socket::Ptr sock = new Socket(fd);
StreamReadContext src;
FILE *fp = fdopen(fd, "r"); for (;;) {
sock->Poll(true, false);
if (fp == NULL) { char buffer[8192];
Log(LogCritical, "ExternalCommandListener") size_t rc = sock->Read(buffer, sizeof(buffer));
<< "fdopen() for fifo path '" << commandPath << "' failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\""; if (rc <= 0)
return; break;
}
const int linesize = 128 * 1024; fifo->Write(buffer, rc);
char *line = new char[linesize];
while (fgets(line, linesize, fp) != NULL) { for (;;) {
// remove trailing new-line String command;
while (strlen(line) > 0 && StreamReadStatus srs = fifo->ReadLine(&command, src);
(line[strlen(line) - 1] == '\r' || line[strlen(line) - 1] == '\n'))
line[strlen(line) - 1] = '\0';
String command = line; if (srs != StatusNewItem)
break;
try { try {
Log(LogInformation, "ExternalCommandListener") Log(LogInformation, "ExternalCommandListener")
<< "Executing external command: " << command; << "Executing external command: " << command;
ExternalCommandProcessor::Execute(command); ExternalCommandProcessor::Execute(command);
} catch (const std::exception& ex) { } catch (const std::exception& ex) {
Log(LogWarning, "ExternalCommandListener") Log(LogWarning, "ExternalCommandListener")
<< "External command failed." << DiagnosticInformation(ex); << "External command failed." << DiagnosticInformation(ex);
}
} }
} }
delete [] line;
fclose(fp);
} }
} }
#endif /* _WIN32 */ #endif /* _WIN32 */