src: convert code to use virPipe APIs

This addreses portability to Windows and standardizes
error reporting. This fixes a number of places which
failed to set O_CLOEXEC or failed to report errors.

Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrangé 2020-01-24 15:22:12 +00:00
parent ab36f72947
commit cc46e137eb
20 changed files with 40 additions and 82 deletions

View File

@ -584,7 +584,7 @@ libxlDomainMigrationDstPrepareTunnel3(virConnectPtr dconn,
* The data flow of tunnel3 migration in the dest side:
* stream -> pipe -> recvfd of libxlDomainStartRestore
*/
if (pipe(dataFD) < 0)
if (virPipe(dataFD) < 0)
goto endjob;
/* Stream data will be written to pipeIn */
@ -916,10 +916,8 @@ libxlMigrationSrcStartTunnel(libxlDriverPrivatePtr driver,
tc->dataFD[0] = -1;
tc->dataFD[1] = -1;
if (pipe(tc->dataFD) < 0) {
virReportError(errno, "%s", _("Unable to make pipes"));
if (virPipe(tc->dataFD) < 0)
goto out;
}
arg = &tc->tmThread;
/* Read from pipe */

View File

@ -356,7 +356,7 @@ static int
virLockDaemonForkIntoBackground(const char *argv0)
{
int statuspipe[2];
if (pipe(statuspipe) < 0)
if (virPipeQuiet(statuspipe) < 0)
return -1;
pid_t pid = fork();

View File

@ -298,7 +298,7 @@ static int
virLogDaemonForkIntoBackground(const char *argv0)
{
int statuspipe[2];
if (pipe(statuspipe) < 0)
if (virPipeQuiet(statuspipe) < 0)
return -1;
pid_t pid = fork();

View File

@ -385,11 +385,9 @@ virLogHandlerDomainOpenLogFile(virLogHandlerPtr handler,
}
}
if (pipe(pipefd) < 0) {
virReportSystemError(errno, "%s",
_("Cannot open fifo pipe"));
if (virPipe(pipefd) < 0)
goto error;
}
if (VIR_ALLOC(file) < 0)
goto error;

View File

@ -1363,11 +1363,8 @@ int virLXCProcessStart(virConnectPtr conn,
goto cleanup;
}
if (pipe(handshakefds) < 0) {
virReportSystemError(errno, "%s",
_("Unable to create pipe"));
if (virPipe(handshakefds) < 0)
goto cleanup;
}
if (!(cmd = virLXCProcessBuildControllerCmd(driver,
vm,

View File

@ -2533,11 +2533,8 @@ qemuMigrationDstPrepareAny(virQEMUDriverPtr driver,
goto done;
if (tunnel &&
(pipe(dataFD) < 0 || virSetCloseExec(dataFD[1]) < 0)) {
virReportSystemError(errno, "%s",
_("cannot create pipe for tunnelled migration"));
virPipe(dataFD) < 0)
goto stopjob;
}
startFlags = VIR_QEMU_PROCESS_START_AUTODESTROY;
@ -3290,11 +3287,8 @@ qemuMigrationSrcStartTunnel(virStreamPtr st,
qemuMigrationIOThreadPtr io = NULL;
int wakeupFD[2] = { -1, -1 };
if (pipe2(wakeupFD, O_CLOEXEC) < 0) {
virReportSystemError(errno, "%s",
_("Unable to make pipe"));
if (virPipe(wakeupFD) < 0)
goto error;
}
if (VIR_ALLOC(io) < 0)
goto error;
@ -3908,10 +3902,12 @@ qemuMigrationSrcPerformTunnel(virQEMUDriverPtr driver,
spec.dest.fd.qemu = -1;
spec.dest.fd.local = -1;
if (pipe2(fds, O_CLOEXEC) == 0) {
spec.dest.fd.qemu = fds[1];
spec.dest.fd.local = fds[0];
}
if (virPipe(fds) < 0)
goto cleanup;
spec.dest.fd.qemu = fds[1];
spec.dest.fd.local = fds[0];
if (spec.dest.fd.qemu == -1 ||
qemuSecuritySetImageFDLabel(driver->securityManager, vm->def,
spec.dest.fd.qemu) < 0) {
@ -5273,11 +5269,8 @@ qemuMigrationSrcToFile(virQEMUDriverPtr driver, virDomainObjPtr vm,
return -1;
}
if (compressor && pipe(pipeFD) < 0) {
virReportSystemError(errno, "%s",
_("Failed to create pipe for migration"));
if (compressor && virPipe(pipeFD) < 0)
return -1;
}
/* All right! We can use fd migration, which means that qemu
* doesn't have to open() the file, so while we still have to

View File

@ -366,11 +366,8 @@ qemuTPMSetupEncryption(const unsigned char *secretuuid,
&secret, &secret_len) < 0)
goto error;
if (pipe(pipefd) == -1) {
virReportSystemError(errno, "%s",
_("Unable to create pipe"));
if (virPipe(pipefd) < 0)
goto error;
}
if (virCommandSetSendBuffer(cmd, pipefd[1], secret, secret_len) < 0)
goto error;

View File

@ -113,7 +113,7 @@ VIR_ENUM_IMPL(virDaemonErr,
static int daemonForkIntoBackground(const char *argv0)
{
int statuspipe[2];
if (pipe(statuspipe) < 0)
if (virPipeQuiet(statuspipe) < 0)
return -1;
pid_t pid = fork();

View File

@ -671,11 +671,8 @@ virNetDaemonSignalSetup(virNetDaemonPtr dmn)
if (dmn->sigwrite != -1)
return 0;
if (pipe2(fds, O_CLOEXEC|O_NONBLOCK) < 0) {
virReportSystemError(errno, "%s",
_("Unable to create signal pipe"));
if (virPipeNonBlock(fds) < 0)
return -1;
}
if ((dmn->sigwatch = virEventAddHandle(fds[0],
VIR_EVENT_HANDLE_READABLE,

View File

@ -794,11 +794,8 @@ int virNetSocketNewConnectCommand(virCommandPtr cmd,
goto error;
}
if (pipe(errfd) < 0) {
virReportSystemError(errno, "%s",
_("unable to create socket pair"));
if (virPipe(errfd) < 0)
goto error;
}
virCommandSetInputFD(cmd, sv[1]);
virCommandSetOutputFD(cmd, &sv[1]);

View File

@ -585,11 +585,8 @@ virExec(virCommandPtr cmd)
if (cmd->outfdptr != NULL) {
if (*cmd->outfdptr == -1) {
if (pipe2(pipeout, O_CLOEXEC) < 0) {
virReportSystemError(errno,
"%s", _("cannot create pipe"));
if (virPipe(pipeout) < 0)
goto cleanup;
}
if ((cmd->flags & VIR_EXEC_NONBLOCK) &&
virSetNonBlock(pipeout[0]) == -1) {
@ -612,11 +609,8 @@ virExec(virCommandPtr cmd)
if (cmd->errfdptr == cmd->outfdptr) {
childerr = childout;
} else if (*cmd->errfdptr == -1) {
if (pipe2(pipeerr, O_CLOEXEC) < 0) {
virReportSystemError(errno,
"%s", _("Failed to create pipe"));
if (virPipe(pipeerr) < 0)
goto cleanup;
}
if ((cmd->flags & VIR_EXEC_NONBLOCK) &&
virSetNonBlock(pipeerr[0]) == -1) {
@ -2478,9 +2472,7 @@ virCommandRunAsync(virCommandPtr cmd, pid_t *pid)
* virCommandDoAsyncIO. */
if (cmd->inbuf && cmd->infd == -1 &&
(synchronous || cmd->flags & VIR_EXEC_ASYNC_IO)) {
if (pipe2(infd, O_CLOEXEC) < 0) {
virReportSystemError(errno, "%s",
_("unable to open pipe"));
if (virPipe(infd) < 0) {
cmd->has_error = -1;
return -1;
}
@ -2724,11 +2716,11 @@ void virCommandRequireHandshake(virCommandPtr cmd)
return;
}
if (pipe2(cmd->handshakeWait, O_CLOEXEC) < 0) {
if (virPipeQuiet(cmd->handshakeWait) < 0) {
cmd->has_error = errno;
return;
}
if (pipe2(cmd->handshakeNotify, O_CLOEXEC) < 0) {
if (virPipeQuiet(cmd->handshakeNotify) < 0) {
VIR_FORCE_CLOSE(cmd->handshakeWait[0]);
VIR_FORCE_CLOSE(cmd->handshakeWait[1]);
cmd->has_error = errno;

View File

@ -1274,11 +1274,8 @@ virFDStreamOpenFileInternal(virStreamPtr st,
goto error;
}
if (pipe(pipefds) < 0) {
virReportSystemError(errno, "%s",
_("Unable to create pipe"));
if (virPipe(pipefds) < 0)
goto error;
}
if (VIR_ALLOC(threadData) < 0)
goto error;

View File

@ -275,11 +275,8 @@ virFileWrapperFdNew(int *fd, const char *name, unsigned int flags)
goto error;
}
if (pipe2(pipefd, O_CLOEXEC) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("unable to create pipe for %s"), name);
if (virPipe(pipefd) < 0)
goto error;
}
if (!(iohelper_path = virFileFindResource("libvirt_iohelper",
abs_top_builddir "/src",

View File

@ -171,7 +171,7 @@ virPolkitAgentCreate(void)
if (!isatty(STDIN_FILENO))
goto error;
if (pipe2(pipe_fd, 0) < 0)
if (virPipe(pipe_fd) < 0)
goto error;
if (VIR_ALLOC(agent) < 0)

View File

@ -1174,11 +1174,8 @@ virProcessRunInFork(virProcessForkCallback cb,
pid_t parent = getpid();
int errfd[2] = { -1, -1 };
if (pipe2(errfd, O_CLOEXEC) < 0) {
virReportSystemError(errno, "%s",
_("Cannot create pipe for child"));
if (virPipe(errfd) < 0)
return -1;
}
if ((child = virFork()) < 0)
goto cleanup;

View File

@ -1019,7 +1019,7 @@ static int test25(const void *unused G_GNUC_UNUSED)
int ngroups;
virCommandPtr cmd = virCommandNew("some/nonexistent/binary");
if (pipe(pipeFD) < 0) {
if (virPipeQuiet(pipeFD) < 0) {
fprintf(stderr, "Unable to create pipe\n");
goto cleanup;
}
@ -1187,7 +1187,7 @@ static int test27(const void *unused G_GNUC_UNUSED)
errexpect = g_strdup_printf(TEST27_ERREXPECT_TEMP,
buffer0, buffer1, buffer2);
if (pipe(pipe1) < 0 || pipe(pipe2) < 0) {
if (virPipeQuiet(pipe1) < 0 || virPipeQuiet(pipe2) < 0) {
printf("Could not create pipe: %s\n", g_strerror(errno));
goto cleanup;
}

View File

@ -321,7 +321,7 @@ mymain(void)
char one = '1';
for (i = 0; i < NUM_FDS; i++) {
if (pipe(handles[i].pipeFD) < 0) {
if (virPipeQuiet(handles[i].pipeFD) < 0) {
fprintf(stderr, "Cannot create pipe: %d", errno);
return EXIT_FAILURE;
}

View File

@ -377,7 +377,7 @@ virTestCaptureProgramOutput(const char *const argv[], char **buf, int maxlen)
int pipefd[2];
int len;
if (pipe(pipefd) < 0)
if (virPipeQuiet(pipefd) < 0)
return -1;
pid_t pid = fork();

View File

@ -4423,7 +4423,7 @@ cmdSave(vshControl *ctl, const vshCmd *cmd)
if (vshCommandOptBool(cmd, "verbose"))
verbose = true;
if (pipe(p) < 0)
if (virPipeQuiet(p) < 0)
goto cleanup;
data.ctl = ctl;
@ -4732,7 +4732,7 @@ cmdManagedSave(vshControl *ctl, const vshCmd *cmd)
if (vshCommandOptBool(cmd, "verbose"))
verbose = true;
if (pipe(p) < 0)
if (virPipeQuiet(p) < 0)
goto cleanup;
data.ctl = ctl;
@ -5462,7 +5462,7 @@ cmdDump(vshControl *ctl, const vshCmd *cmd)
if (vshCommandOptBool(cmd, "verbose"))
verbose = true;
if (pipe(p) < 0)
if (virPipeQuiet(p) < 0)
goto cleanup;
data.ctl = ctl;
@ -11033,7 +11033,7 @@ cmdMigrate(vshControl *ctl, const vshCmd *cmd)
goto cleanup;
}
if (pipe(p) < 0)
if (virPipeQuiet(p) < 0)
goto cleanup;
data.ctl = ctl;

View File

@ -2061,11 +2061,9 @@ vshEventStart(vshControl *ctl, int timeout_ms)
assert(ctl->eventPipe[0] == -1 && ctl->eventPipe[1] == -1 &&
ctl->eventTimerId >= 0);
if (pipe2(ctl->eventPipe, O_CLOEXEC) < 0) {
char ebuf[1024];
vshError(ctl, _("failed to create pipe: %s"),
virStrerror(errno, ebuf, sizeof(ebuf)));
if (virPipe(ctl->eventPipe) < 0) {
vshSaveLibvirtError();
vshReportError(ctl);
return -1;
}