提交 c10f0978 编写于 作者: M Michal Privoznik

virsh: Allow graceful console shutdown

Currently, whenever there's a regular EOF on the console stream
or an error the virStreamAbort() is called regardless. While this
may not actually break anything, we should call virStreamFinish()
to let the daemon know we've successfully received all the data
and are shutting down the stream gracefully.
Signed-off-by: NMichal Privoznik <mprivozn@redhat.com>
Reviewed-by: NRoman Bolshakov <r.bolshakov@yadro.com>
上级 9935b435
...@@ -97,7 +97,8 @@ virConsoleHandleSignal(int sig ATTRIBUTE_UNUSED) ...@@ -97,7 +97,8 @@ virConsoleHandleSignal(int sig ATTRIBUTE_UNUSED)
static void static void
virConsoleShutdown(virConsolePtr con) virConsoleShutdown(virConsolePtr con,
bool graceful)
{ {
virErrorPtr err = virGetLastError(); virErrorPtr err = virGetLastError();
...@@ -105,10 +106,18 @@ virConsoleShutdown(virConsolePtr con) ...@@ -105,10 +106,18 @@ virConsoleShutdown(virConsolePtr con)
virCopyLastError(&con->error); virCopyLastError(&con->error);
if (con->st) { if (con->st) {
int rc;
virStreamEventRemoveCallback(con->st); virStreamEventRemoveCallback(con->st);
if (virStreamAbort(con->st) < 0) if (graceful)
rc = virStreamFinish(con->st);
else
rc = virStreamAbort(con->st);
if (rc < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("cannot terminate console stream")); _("cannot terminate console stream"));
}
virStreamFree(con->st); virStreamFree(con->st);
con->st = NULL; con->st = NULL;
} }
...@@ -160,7 +169,7 @@ virConsoleEventOnStream(virStreamPtr st, ...@@ -160,7 +169,7 @@ virConsoleEventOnStream(virStreamPtr st,
if (avail < 1024) { if (avail < 1024) {
if (VIR_REALLOC_N(con->streamToTerminal.data, if (VIR_REALLOC_N(con->streamToTerminal.data,
con->streamToTerminal.length + 1024) < 0) { con->streamToTerminal.length + 1024) < 0) {
virConsoleShutdown(con); virConsoleShutdown(con, false);
goto cleanup; goto cleanup;
} }
con->streamToTerminal.length += 1024; con->streamToTerminal.length += 1024;
...@@ -174,7 +183,7 @@ virConsoleEventOnStream(virStreamPtr st, ...@@ -174,7 +183,7 @@ virConsoleEventOnStream(virStreamPtr st,
if (got == -2) if (got == -2)
goto cleanup; /* blocking */ goto cleanup; /* blocking */
if (got <= 0) { if (got <= 0) {
virConsoleShutdown(con); virConsoleShutdown(con, got == 0);
goto cleanup; goto cleanup;
} }
con->streamToTerminal.offset += got; con->streamToTerminal.offset += got;
...@@ -193,7 +202,7 @@ virConsoleEventOnStream(virStreamPtr st, ...@@ -193,7 +202,7 @@ virConsoleEventOnStream(virStreamPtr st,
if (done == -2) if (done == -2)
goto cleanup; /* blocking */ goto cleanup; /* blocking */
if (done < 0) { if (done < 0) {
virConsoleShutdown(con); virConsoleShutdown(con, false);
goto cleanup; goto cleanup;
} }
memmove(con->terminalToStream.data, memmove(con->terminalToStream.data,
...@@ -214,7 +223,7 @@ virConsoleEventOnStream(virStreamPtr st, ...@@ -214,7 +223,7 @@ virConsoleEventOnStream(virStreamPtr st,
if (events & VIR_STREAM_EVENT_ERROR || if (events & VIR_STREAM_EVENT_ERROR ||
events & VIR_STREAM_EVENT_HANGUP) { events & VIR_STREAM_EVENT_HANGUP) {
virConsoleShutdown(con); virConsoleShutdown(con, false);
} }
cleanup: cleanup:
...@@ -244,7 +253,7 @@ virConsoleEventOnStdin(int watch ATTRIBUTE_UNUSED, ...@@ -244,7 +253,7 @@ virConsoleEventOnStdin(int watch ATTRIBUTE_UNUSED,
if (avail < 1024) { if (avail < 1024) {
if (VIR_REALLOC_N(con->terminalToStream.data, if (VIR_REALLOC_N(con->terminalToStream.data,
con->terminalToStream.length + 1024) < 0) { con->terminalToStream.length + 1024) < 0) {
virConsoleShutdown(con); virConsoleShutdown(con, false);
goto cleanup; goto cleanup;
} }
con->terminalToStream.length += 1024; con->terminalToStream.length += 1024;
...@@ -258,17 +267,17 @@ virConsoleEventOnStdin(int watch ATTRIBUTE_UNUSED, ...@@ -258,17 +267,17 @@ virConsoleEventOnStdin(int watch ATTRIBUTE_UNUSED,
if (got < 0) { if (got < 0) {
if (errno != EAGAIN) { if (errno != EAGAIN) {
virReportSystemError(errno, "%s", _("cannot read from stdin")); virReportSystemError(errno, "%s", _("cannot read from stdin"));
virConsoleShutdown(con); virConsoleShutdown(con, false);
} }
goto cleanup; goto cleanup;
} }
if (got == 0) { if (got == 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("EOF on stdin")); virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("EOF on stdin"));
virConsoleShutdown(con); virConsoleShutdown(con, false);
goto cleanup; goto cleanup;
} }
if (con->terminalToStream.data[con->terminalToStream.offset] == con->escapeChar) { if (con->terminalToStream.data[con->terminalToStream.offset] == con->escapeChar) {
virConsoleShutdown(con); virConsoleShutdown(con, true);
goto cleanup; goto cleanup;
} }
...@@ -281,13 +290,13 @@ virConsoleEventOnStdin(int watch ATTRIBUTE_UNUSED, ...@@ -281,13 +290,13 @@ virConsoleEventOnStdin(int watch ATTRIBUTE_UNUSED,
if (events & VIR_EVENT_HANDLE_ERROR) { if (events & VIR_EVENT_HANDLE_ERROR) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("IO error on stdin")); virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("IO error on stdin"));
virConsoleShutdown(con); virConsoleShutdown(con, false);
goto cleanup; goto cleanup;
} }
if (events & VIR_EVENT_HANDLE_HANGUP) { if (events & VIR_EVENT_HANDLE_HANGUP) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("EOF on stdin")); virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("EOF on stdin"));
virConsoleShutdown(con); virConsoleShutdown(con, false);
goto cleanup; goto cleanup;
} }
...@@ -320,7 +329,7 @@ virConsoleEventOnStdout(int watch ATTRIBUTE_UNUSED, ...@@ -320,7 +329,7 @@ virConsoleEventOnStdout(int watch ATTRIBUTE_UNUSED,
if (done < 0) { if (done < 0) {
if (errno != EAGAIN) { if (errno != EAGAIN) {
virReportSystemError(errno, "%s", _("cannot write to stdout")); virReportSystemError(errno, "%s", _("cannot write to stdout"));
virConsoleShutdown(con); virConsoleShutdown(con, false);
} }
goto cleanup; goto cleanup;
} }
...@@ -342,13 +351,13 @@ virConsoleEventOnStdout(int watch ATTRIBUTE_UNUSED, ...@@ -342,13 +351,13 @@ virConsoleEventOnStdout(int watch ATTRIBUTE_UNUSED,
if (events & VIR_EVENT_HANDLE_ERROR) { if (events & VIR_EVENT_HANDLE_ERROR) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("IO error stdout")); virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("IO error stdout"));
virConsoleShutdown(con); virConsoleShutdown(con, false);
goto cleanup; goto cleanup;
} }
if (events & VIR_EVENT_HANDLE_HANGUP) { if (events & VIR_EVENT_HANDLE_HANGUP) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("EOF on stdout")); virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("EOF on stdout"));
virConsoleShutdown(con); virConsoleShutdown(con, false);
goto cleanup; goto cleanup;
} }
...@@ -487,7 +496,7 @@ virshRunConsole(vshControl *ctl, ...@@ -487,7 +496,7 @@ virshRunConsole(vshControl *ctl,
ret = 0; ret = 0;
cleanup: cleanup:
virConsoleShutdown(con); virConsoleShutdown(con, ret == 0);
if (ret < 0) { if (ret < 0) {
vshResetLibvirtError(); vshResetLibvirtError();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册