unix-adapter: Accommodate partial write calls, if it happens somehow.

Maybe this can happen?  I don't know.
This commit is contained in:
Ryan Prichard 2015-12-14 23:28:13 -06:00
parent 80f0e87dc1
commit 230ae4fdf9
4 changed files with 91 additions and 12 deletions

View File

@ -30,6 +30,7 @@
#include "../shared/DebugClient.h"
#include "Event.h"
#include "Util.h"
#include "WakeupFd.h"
OutputHandler::OutputHandler(HANDLE winpty, WakeupFd &completionWakeup) :
@ -100,18 +101,7 @@ void OutputHandler::threadProc() {
}
break;
}
// I don't know if this write can be interrupted or not, but handle it
// just in case.
int written;
do {
written = write(STDOUT_FILENO, &buffer[0], numRead);
} while (written == -1 && errno == EINTR);
if (numRead != static_cast<DWORD>(written)) {
trace("OutputHandler: write failed: "
"errno=%d numRead=%u written=%d",
errno,
static_cast<unsigned int>(numRead),
written);
if (!writeAll(STDOUT_FILENO, &buffer[0], numRead)) {
break;
}
}

59
src/unix-adapter/Util.cc Executable file
View File

@ -0,0 +1,59 @@
// Copyright (c) 2015 Ryan Prichard
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
#include "Util.h"
#include <assert.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include "../shared/DebugClient.h"
// Write the entire buffer, restarting it as necessary.
bool writeAll(int fd, const void *buffer, size_t size) {
size_t written = 0;
while (written < size) {
int ret = write(fd,
reinterpret_cast<const char*>(buffer) + written,
size - written);
if (ret == -1 && errno == EINTR) {
continue;
}
if (ret <= 0) {
trace("write failed: "
"fd=%d errno=%d size=%u written=%d ret=%d",
fd,
errno,
static_cast<unsigned int>(size),
static_cast<unsigned int>(written),
ret);
return false;
}
assert(static_cast<size_t>(ret) <= size - written);
written += ret;
}
assert(written == size);
return true;
}
bool writeStr(int fd, const char *str) {
return writeAll(fd, str, strlen(str));
}

29
src/unix-adapter/Util.h Executable file
View File

@ -0,0 +1,29 @@
// Copyright (c) 2015 Ryan Prichard
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
#ifndef UNIX_ADAPTER_UTIL_H
#define UNIX_ADAPTER_UTIL_H
#include <stdlib.h>
bool writeAll(int fd, const void *buffer, size_t size);
bool writeStr(int fd, const char *str);
#endif // UNIX_ADAPTER_UTIL_H

View File

@ -23,6 +23,7 @@ ALL_TARGETS += build/$(UNIX_ADAPTER_EXE)
UNIX_ADAPTER_OBJECTS = \
build/unix/unix-adapter/InputHandler.o \
build/unix/unix-adapter/OutputHandler.o \
build/unix/unix-adapter/Util.o \
build/unix/unix-adapter/WakeupFd.o \
build/unix/unix-adapter/main.o \
build/unix/shared/DebugClient.o \