2010-10-19 14:23:51 +00:00
|
|
|
/*
|
2011-07-19 18:32:58 +00:00
|
|
|
* virfile.c: safer file handling
|
2010-10-19 14:23:51 +00:00
|
|
|
*
|
2014-02-20 03:23:44 +00:00
|
|
|
* Copyright (C) 2010-2014 Red Hat, Inc.
|
2010-10-19 14:23:51 +00:00
|
|
|
* Copyright (C) 2010 IBM Corporation
|
|
|
|
* Copyright (C) 2010 Stefan Berger
|
|
|
|
* Copyright (C) 2010 Eric Blake
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-09-20 22:30:55 +00:00
|
|
|
* License along with this library. If not, see
|
2012-07-21 10:06:23 +00:00
|
|
|
* <http://www.gnu.org/licenses/>.
|
2010-10-19 14:23:51 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
2011-07-11 21:26:33 +00:00
|
|
|
#include "internal.h"
|
2010-10-19 14:23:51 +00:00
|
|
|
|
2011-07-11 21:26:33 +00:00
|
|
|
#include <fcntl.h>
|
2020-01-09 13:18:50 +00:00
|
|
|
#ifndef WIN32
|
|
|
|
# include <termios.h>
|
2020-09-01 12:23:32 +00:00
|
|
|
#endif
|
2020-09-01 11:27:44 +00:00
|
|
|
#ifdef WITH_PTY_H
|
2020-01-07 16:19:52 +00:00
|
|
|
/* Linux openpty */
|
|
|
|
# include <pty.h>
|
2020-09-01 12:23:32 +00:00
|
|
|
#endif
|
2020-09-01 11:27:44 +00:00
|
|
|
#ifdef WITH_UTIL_H
|
2020-01-07 16:19:52 +00:00
|
|
|
/* macOS openpty */
|
|
|
|
# include <util.h>
|
2020-09-01 12:23:32 +00:00
|
|
|
#endif
|
2020-09-01 11:27:44 +00:00
|
|
|
#ifdef WITH_LIBUTIL_H
|
2020-01-07 16:19:52 +00:00
|
|
|
/* FreeBSD openpty */
|
|
|
|
# include <libutil.h>
|
2020-09-01 12:23:32 +00:00
|
|
|
#endif
|
2011-07-11 21:26:33 +00:00
|
|
|
#include <sys/stat.h>
|
2020-09-01 11:27:44 +00:00
|
|
|
#if defined(WITH_SYS_MOUNT_H)
|
2016-11-10 15:17:48 +00:00
|
|
|
# include <sys/mount.h>
|
|
|
|
#endif
|
2010-10-19 14:23:51 +00:00
|
|
|
#include <unistd.h>
|
2012-07-03 14:06:27 +00:00
|
|
|
#include <dirent.h>
|
2020-09-01 11:27:44 +00:00
|
|
|
#if defined WITH_MNTENT_H && defined WITH_GETMNTENT_R
|
2013-05-09 18:59:04 +00:00
|
|
|
# include <mntent.h>
|
|
|
|
#endif
|
2020-09-01 11:27:44 +00:00
|
|
|
#if WITH_MMAP
|
2013-05-11 02:46:36 +00:00
|
|
|
# include <sys/mman.h>
|
|
|
|
#endif
|
2020-09-01 11:27:44 +00:00
|
|
|
#if WITH_SYS_SYSCALL_H
|
2014-12-08 13:06:57 +00:00
|
|
|
# include <sys/syscall.h>
|
|
|
|
#endif
|
2020-09-01 11:27:44 +00:00
|
|
|
#if WITH_SYS_ACL_H
|
2016-11-22 10:14:08 +00:00
|
|
|
# include <sys/acl.h>
|
|
|
|
#endif
|
2018-02-08 13:53:20 +00:00
|
|
|
#include <sys/file.h>
|
2012-07-03 14:06:27 +00:00
|
|
|
|
2014-03-29 20:15:33 +00:00
|
|
|
#ifdef __linux__
|
2020-09-01 11:27:44 +00:00
|
|
|
# if WITH_LINUX_MAGIC_H
|
2014-03-29 20:15:33 +00:00
|
|
|
# include <linux/magic.h>
|
|
|
|
# endif
|
|
|
|
# include <sys/statfs.h>
|
2020-09-01 11:27:44 +00:00
|
|
|
# if WITH_DECL_LO_FLAGS_AUTOCLEAR
|
2018-04-23 15:10:07 +00:00
|
|
|
# include <linux/loop.h>
|
|
|
|
# endif
|
2018-04-23 15:41:14 +00:00
|
|
|
# include <sys/ioctl.h>
|
|
|
|
# include <linux/cdrom.h>
|
2020-07-17 18:16:27 +00:00
|
|
|
# include <linux/fs.h>
|
2012-07-03 14:06:27 +00:00
|
|
|
#endif
|
2010-10-19 14:23:51 +00:00
|
|
|
|
2020-09-01 11:27:44 +00:00
|
|
|
#if WITH_LIBATTR
|
2018-08-06 08:50:03 +00:00
|
|
|
# include <sys/xattr.h>
|
|
|
|
#endif
|
|
|
|
|
2011-07-11 21:26:33 +00:00
|
|
|
#include "configmake.h"
|
2019-04-01 14:28:05 +00:00
|
|
|
#include "viralloc.h"
|
2013-05-09 18:59:04 +00:00
|
|
|
#include "vircommand.h"
|
2012-12-13 18:21:53 +00:00
|
|
|
#include "virerror.h"
|
2013-05-09 18:59:04 +00:00
|
|
|
#include "virfile.h"
|
2015-07-20 12:37:48 +00:00
|
|
|
#include "virkmod.h"
|
2012-12-12 17:59:27 +00:00
|
|
|
#include "virlog.h"
|
2013-05-09 18:59:04 +00:00
|
|
|
#include "virprocess.h"
|
2013-04-03 10:36:23 +00:00
|
|
|
#include "virstring.h"
|
2013-05-09 18:59:04 +00:00
|
|
|
#include "virutil.h"
|
2020-01-14 17:38:59 +00:00
|
|
|
#include "virsocket.h"
|
2011-07-11 21:26:33 +00:00
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_NONE
|
2010-10-19 14:23:51 +00:00
|
|
|
|
2014-02-28 12:16:17 +00:00
|
|
|
VIR_LOG_INIT("util.file");
|
|
|
|
|
2020-01-17 11:24:19 +00:00
|
|
|
#ifndef S_ISUID
|
|
|
|
# define S_ISUID 04000
|
|
|
|
#endif
|
|
|
|
#ifndef S_ISGID
|
|
|
|
# define S_ISGID 02000
|
|
|
|
#endif
|
|
|
|
#ifndef S_ISVTX
|
|
|
|
# define S_ISVTX 01000
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2020-01-22 11:46:59 +00:00
|
|
|
#ifndef O_DIRECT
|
|
|
|
# define O_DIRECT 0
|
|
|
|
#endif
|
|
|
|
|
2012-06-07 13:16:50 +00:00
|
|
|
int virFileClose(int *fdptr, virFileCloseFlags flags)
|
2010-10-19 14:23:51 +00:00
|
|
|
{
|
2011-08-16 17:54:15 +00:00
|
|
|
int saved_errno = 0;
|
2010-10-19 14:23:51 +00:00
|
|
|
int rc = 0;
|
|
|
|
|
2012-05-30 13:54:18 +00:00
|
|
|
if (*fdptr < 0)
|
|
|
|
return 0;
|
|
|
|
|
2012-06-07 13:16:50 +00:00
|
|
|
if (flags & VIR_FILE_CLOSE_PRESERVE_ERRNO)
|
2012-05-30 13:54:18 +00:00
|
|
|
saved_errno = errno;
|
|
|
|
|
|
|
|
rc = close(*fdptr);
|
2012-06-07 13:16:50 +00:00
|
|
|
|
|
|
|
if (!(flags & VIR_FILE_CLOSE_DONT_LOG)) {
|
|
|
|
if (rc < 0) {
|
|
|
|
if (errno == EBADF) {
|
|
|
|
if (!(flags & VIR_FILE_CLOSE_IGNORE_EBADF))
|
|
|
|
VIR_WARN("Tried to close invalid fd %d", *fdptr);
|
|
|
|
} else {
|
|
|
|
VIR_DEBUG("Failed to close fd %d: %s",
|
2020-02-26 17:57:34 +00:00
|
|
|
*fdptr, g_strerror(errno));
|
2012-06-07 13:16:50 +00:00
|
|
|
}
|
2012-05-30 13:54:18 +00:00
|
|
|
} else {
|
2012-06-07 13:16:50 +00:00
|
|
|
VIR_DEBUG("Closed fd %d", *fdptr);
|
2012-05-30 13:54:18 +00:00
|
|
|
}
|
2010-10-19 14:23:51 +00:00
|
|
|
}
|
2012-05-30 13:54:18 +00:00
|
|
|
*fdptr = -1;
|
2012-06-07 13:16:50 +00:00
|
|
|
|
|
|
|
if (flags & VIR_FILE_CLOSE_PRESERVE_ERRNO)
|
2012-05-30 13:54:18 +00:00
|
|
|
errno = saved_errno;
|
2010-10-19 14:23:51 +00:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
2010-11-17 02:13:29 +00:00
|
|
|
|
|
|
|
|
2011-07-19 18:32:58 +00:00
|
|
|
int virFileFclose(FILE **file, bool preserve_errno)
|
2010-11-17 02:13:29 +00:00
|
|
|
{
|
2011-08-16 17:54:15 +00:00
|
|
|
int saved_errno = 0;
|
2010-11-17 02:13:29 +00:00
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
if (*file) {
|
|
|
|
if (preserve_errno)
|
|
|
|
saved_errno = errno;
|
|
|
|
rc = fclose(*file);
|
|
|
|
*file = NULL;
|
|
|
|
if (preserve_errno)
|
|
|
|
errno = saved_errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-19 18:32:58 +00:00
|
|
|
FILE *virFileFdopen(int *fdptr, const char *mode)
|
2010-11-17 02:13:29 +00:00
|
|
|
{
|
|
|
|
FILE *file = NULL;
|
|
|
|
|
|
|
|
if (*fdptr >= 0) {
|
|
|
|
file = fdopen(*fdptr, mode);
|
|
|
|
if (file)
|
|
|
|
*fdptr = -1;
|
|
|
|
} else {
|
|
|
|
errno = EBADF;
|
|
|
|
}
|
|
|
|
|
|
|
|
return file;
|
|
|
|
}
|
2011-07-11 21:26:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virFileDirectFdFlag:
|
|
|
|
*
|
|
|
|
* Returns 0 if the kernel can avoid file system cache pollution
|
|
|
|
* without any additional flags, O_DIRECT if the original fd must be
|
|
|
|
* opened in direct mode, or -1 if there is no support for bypassing
|
|
|
|
* the file system cache.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virFileDirectFdFlag(void)
|
|
|
|
{
|
|
|
|
/* XXX For now, Linux posix_fadvise is not powerful enough to
|
|
|
|
* avoid O_DIRECT. */
|
|
|
|
return O_DIRECT ? O_DIRECT : -1;
|
|
|
|
}
|
|
|
|
|
2012-02-06 13:40:48 +00:00
|
|
|
/* Opaque type for managing a wrapper around a fd. For now,
|
|
|
|
* read-write is not supported, just a single direction. */
|
|
|
|
struct _virFileWrapperFd {
|
2019-02-19 15:58:27 +00:00
|
|
|
bool closed; /* Whether virFileWrapperFdClose() has been already called */
|
2012-02-06 13:40:48 +00:00
|
|
|
virCommandPtr cmd; /* Child iohelper process to do the I/O. */
|
2012-10-22 15:07:49 +00:00
|
|
|
char *err_msg; /* stderr of @cmd */
|
2012-02-06 13:40:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#ifndef WIN32
|
2011-07-11 21:26:33 +00:00
|
|
|
/**
|
2012-02-06 13:40:48 +00:00
|
|
|
* virFileWrapperFdNew:
|
2011-07-11 21:26:33 +00:00
|
|
|
* @fd: pointer to fd to wrap
|
|
|
|
* @name: name of fd, for diagnostics
|
2012-02-06 13:40:48 +00:00
|
|
|
* @flags: bitwise-OR of virFileWrapperFdFlags
|
|
|
|
*
|
|
|
|
* Update @fd so that it meets parameters requested by @flags.
|
|
|
|
*
|
|
|
|
* If VIR_FILE_WRAPPER_BYPASS_CACHE bit is set in @flags, @fd will be updated
|
|
|
|
* in a way that all I/O to that file will bypass the system cache. The
|
|
|
|
* original fd must have been created with virFileDirectFdFlag() among the
|
|
|
|
* flags to open().
|
2011-07-11 21:26:33 +00:00
|
|
|
*
|
2012-02-06 13:40:48 +00:00
|
|
|
* If VIR_FILE_WRAPPER_NON_BLOCKING bit is set in @flags, @fd will be updated
|
|
|
|
* to ensure it properly supports non-blocking I/O, i.e., it will report
|
|
|
|
* EAGAIN.
|
|
|
|
*
|
|
|
|
* This must be called after open() and optional fchown() or fchmod(), but
|
|
|
|
* before any seek or I/O, and only on seekable fd. The file must be O_RDONLY
|
|
|
|
* (to read the entire existing file) or O_WRONLY (to write to an empty file).
|
|
|
|
* In some cases, @fd is changed to a non-seekable pipe; in this case, the
|
|
|
|
* caller must not do anything further with the original fd.
|
2011-07-11 21:26:33 +00:00
|
|
|
*
|
|
|
|
* On success, the new wrapper object is returned, which must be later
|
2012-02-06 13:40:48 +00:00
|
|
|
* freed with virFileWrapperFdFree(). On failure, @fd is unchanged, an
|
2011-07-11 21:26:33 +00:00
|
|
|
* error message is output, and NULL is returned.
|
|
|
|
*/
|
2012-02-06 13:40:48 +00:00
|
|
|
virFileWrapperFdPtr
|
|
|
|
virFileWrapperFdNew(int *fd, const char *name, unsigned int flags)
|
2011-07-11 21:26:33 +00:00
|
|
|
{
|
2012-02-06 13:40:48 +00:00
|
|
|
virFileWrapperFdPtr ret = NULL;
|
2011-07-11 21:26:33 +00:00
|
|
|
bool output = false;
|
|
|
|
int pipefd[2] = { -1, -1 };
|
|
|
|
int mode = -1;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *iohelper_path = NULL;
|
2011-07-11 21:26:33 +00:00
|
|
|
|
2012-02-06 13:40:48 +00:00
|
|
|
if (!flags) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("invalid use with no flags"));
|
2012-02-06 13:40:48 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX support posix_fadvise rather than O_DIRECT, if the kernel support
|
|
|
|
* for that is decent enough. In that case, we will also need to
|
|
|
|
* explicitly support VIR_FILE_WRAPPER_NON_BLOCKING since
|
|
|
|
* VIR_FILE_WRAPPER_BYPASS_CACHE alone will no longer require spawning
|
|
|
|
* iohelper.
|
|
|
|
*/
|
2011-07-11 21:26:33 +00:00
|
|
|
|
2012-02-06 13:40:48 +00:00
|
|
|
if ((flags & VIR_FILE_WRAPPER_BYPASS_CACHE) && !O_DIRECT) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("O_DIRECT unsupported on this platform"));
|
2011-07-11 21:26:33 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-07-04 10:17:18 +00:00
|
|
|
if (VIR_ALLOC(ret) < 0)
|
2011-07-11 21:26:33 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
mode = fcntl(*fd, F_GETFL);
|
|
|
|
|
|
|
|
if (mode < 0) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, _("invalid fd %d for %s"),
|
|
|
|
*fd, name);
|
2011-07-11 21:26:33 +00:00
|
|
|
goto error;
|
|
|
|
} else if ((mode & O_ACCMODE) == O_WRONLY) {
|
|
|
|
output = true;
|
|
|
|
} else if ((mode & O_ACCMODE) != O_RDONLY) {
|
2017-09-25 10:43:33 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, _("unexpected mode 0x%x for %s"),
|
2012-07-18 10:26:24 +00:00
|
|
|
mode & O_ACCMODE, name);
|
2011-07-11 21:26:33 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2020-01-24 15:22:12 +00:00
|
|
|
if (virPipe(pipefd) < 0)
|
2011-07-11 21:26:33 +00:00
|
|
|
goto error;
|
|
|
|
|
2014-03-25 08:23:12 +00:00
|
|
|
if (!(iohelper_path = virFileFindResource("libvirt_iohelper",
|
2019-03-12 14:11:47 +00:00
|
|
|
abs_top_builddir "/src",
|
2014-03-25 08:23:12 +00:00
|
|
|
LIBEXECDIR)))
|
|
|
|
goto error;
|
|
|
|
|
2017-09-07 07:44:13 +00:00
|
|
|
ret->cmd = virCommandNewArgList(iohelper_path, name, NULL);
|
2014-03-25 08:23:12 +00:00
|
|
|
|
2011-07-11 21:26:33 +00:00
|
|
|
if (output) {
|
|
|
|
virCommandSetInputFD(ret->cmd, pipefd[0]);
|
|
|
|
virCommandSetOutputFD(ret->cmd, fd);
|
|
|
|
virCommandAddArg(ret->cmd, "1");
|
|
|
|
} else {
|
|
|
|
virCommandSetInputFD(ret->cmd, *fd);
|
|
|
|
virCommandSetOutputFD(ret->cmd, &pipefd[1]);
|
|
|
|
virCommandAddArg(ret->cmd, "0");
|
|
|
|
}
|
|
|
|
|
2013-01-17 10:09:39 +00:00
|
|
|
/* In order to catch iohelper stderr, we must change
|
|
|
|
* iohelper's env so virLog functions print to stderr
|
2012-10-22 15:07:49 +00:00
|
|
|
*/
|
|
|
|
virCommandAddEnvPair(ret->cmd, "LIBVIRT_LOG_OUTPUTS", "1:stderr");
|
2013-01-17 10:09:39 +00:00
|
|
|
virCommandSetErrorBuffer(ret->cmd, &ret->err_msg);
|
|
|
|
virCommandDoAsyncIO(ret->cmd);
|
2012-10-22 15:07:49 +00:00
|
|
|
|
2011-07-11 21:26:33 +00:00
|
|
|
if (virCommandRunAsync(ret->cmd, NULL) < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (VIR_CLOSE(pipefd[!output]) < 0) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("unable to close pipe"));
|
2011-07-11 21:26:33 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_FORCE_CLOSE(*fd);
|
|
|
|
*fd = pipefd[output];
|
|
|
|
return ret;
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
error:
|
2011-07-11 21:26:33 +00:00
|
|
|
VIR_FORCE_CLOSE(pipefd[0]);
|
|
|
|
VIR_FORCE_CLOSE(pipefd[1]);
|
2012-02-06 13:40:48 +00:00
|
|
|
virFileWrapperFdFree(ret);
|
2011-07-11 21:26:33 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2020-01-17 11:24:19 +00:00
|
|
|
#else /* WIN32 */
|
2012-02-06 13:40:48 +00:00
|
|
|
virFileWrapperFdPtr
|
2019-10-14 12:45:33 +00:00
|
|
|
virFileWrapperFdNew(int *fd G_GNUC_UNUSED,
|
|
|
|
const char *name G_GNUC_UNUSED,
|
|
|
|
unsigned int fdflags G_GNUC_UNUSED)
|
2012-02-06 13:40:48 +00:00
|
|
|
{
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
2018-09-19 08:38:14 +00:00
|
|
|
_("virFileWrapperFd unsupported on this platform"));
|
2012-02-06 13:40:48 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2020-01-17 11:24:19 +00:00
|
|
|
#endif /* WIN32 */
|
2011-07-11 21:26:33 +00:00
|
|
|
|
|
|
|
/**
|
2012-02-06 13:40:48 +00:00
|
|
|
* virFileWrapperFdClose:
|
|
|
|
* @wfd: fd wrapper, or NULL
|
2011-07-11 21:26:33 +00:00
|
|
|
*
|
2012-02-06 13:40:48 +00:00
|
|
|
* If @wfd is valid, then ensure that I/O has completed, which may
|
2011-07-11 21:26:33 +00:00
|
|
|
* include reaping a child process. Return 0 if all data for the
|
|
|
|
* wrapped fd is complete, or -1 on failure with an error emitted.
|
2012-02-06 13:40:48 +00:00
|
|
|
* This function intentionally returns 0 when @wfd is NULL, so that
|
|
|
|
* callers can conditionally create a virFileWrapperFd wrapper but
|
2011-07-11 21:26:33 +00:00
|
|
|
* unconditionally call the cleanup code. To avoid deadlock, only
|
2012-02-06 13:40:48 +00:00
|
|
|
* call this after closing the fd resulting from virFileWrapperFdNew().
|
2019-02-19 15:58:27 +00:00
|
|
|
*
|
|
|
|
* This function can be safely called multiple times on the same @wfd.
|
2011-07-11 21:26:33 +00:00
|
|
|
*/
|
|
|
|
int
|
2012-02-06 13:40:48 +00:00
|
|
|
virFileWrapperFdClose(virFileWrapperFdPtr wfd)
|
2011-07-11 21:26:33 +00:00
|
|
|
{
|
2013-02-07 11:23:34 +00:00
|
|
|
int ret;
|
|
|
|
|
2019-02-19 15:58:27 +00:00
|
|
|
if (!wfd || wfd->closed)
|
2011-07-11 21:26:33 +00:00
|
|
|
return 0;
|
|
|
|
|
2013-02-07 11:23:34 +00:00
|
|
|
ret = virCommandWait(wfd->cmd, NULL);
|
2019-02-19 15:58:27 +00:00
|
|
|
|
2019-02-19 16:20:41 +00:00
|
|
|
/* If the command used to process I/O has failed and produced some
|
|
|
|
* messages on stderr, it's fair to assume those will be more
|
|
|
|
* relevant to the user than whatever eg. QEMU can figure out on its
|
|
|
|
* own having no knowledge of the fact a command is handling its I/O
|
|
|
|
* in the first place, so it's okay if we end up discarding an
|
|
|
|
* existing error here */
|
|
|
|
if (ret < 0 && wfd->err_msg && *wfd->err_msg)
|
|
|
|
virReportError(VIR_ERR_OPERATION_FAILED, "%s", wfd->err_msg);
|
2019-02-20 13:16:46 +00:00
|
|
|
|
2019-02-19 15:58:27 +00:00
|
|
|
wfd->closed = true;
|
|
|
|
|
2013-02-07 11:23:34 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2012-10-22 15:07:49 +00:00
|
|
|
|
2011-07-11 21:26:33 +00:00
|
|
|
/**
|
2012-02-06 13:40:48 +00:00
|
|
|
* virFileWrapperFdFree:
|
|
|
|
* @wfd: fd wrapper, or NULL
|
2011-07-11 21:26:33 +00:00
|
|
|
*
|
2012-02-06 13:40:48 +00:00
|
|
|
* Free all remaining resources associated with @wfd. If
|
|
|
|
* virFileWrapperFdClose() was not previously called, then this may
|
2011-07-11 21:26:33 +00:00
|
|
|
* discard some previous I/O. To avoid deadlock, only call this after
|
2012-02-06 13:40:48 +00:00
|
|
|
* closing the fd resulting from virFileWrapperFdNew().
|
2011-07-11 21:26:33 +00:00
|
|
|
*/
|
|
|
|
void
|
2012-02-06 13:40:48 +00:00
|
|
|
virFileWrapperFdFree(virFileWrapperFdPtr wfd)
|
2011-07-11 21:26:33 +00:00
|
|
|
{
|
2012-02-06 13:40:48 +00:00
|
|
|
if (!wfd)
|
2011-07-11 21:26:33 +00:00
|
|
|
return;
|
|
|
|
|
2018-07-23 12:22:41 +00:00
|
|
|
VIR_FREE(wfd->err_msg);
|
2012-02-06 13:40:48 +00:00
|
|
|
virCommandFree(wfd->cmd);
|
|
|
|
VIR_FREE(wfd);
|
2011-07-11 21:26:33 +00:00
|
|
|
}
|
2011-07-06 16:06:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
#ifndef WIN32
|
2018-02-08 13:53:20 +00:00
|
|
|
|
2011-07-06 16:06:58 +00:00
|
|
|
/**
|
|
|
|
* virFileLock:
|
|
|
|
* @fd: file descriptor to acquire the lock on
|
|
|
|
* @shared: type of lock to acquire
|
|
|
|
* @start: byte offset to start lock
|
|
|
|
* @len: length of lock (0 to acquire entire remaining file from @start)
|
2014-03-17 14:17:36 +00:00
|
|
|
* @waitForLock: wait for previously held lock or not
|
2011-07-06 16:06:58 +00:00
|
|
|
*
|
|
|
|
* Attempt to acquire a lock on the file @fd. If @shared
|
|
|
|
* is true, then a shared lock will be acquired,
|
|
|
|
* otherwise an exclusive lock will be acquired. If
|
|
|
|
* the lock cannot be acquired, an error will be
|
2014-03-17 14:17:36 +00:00
|
|
|
* returned. If @waitForLock is true, this will wait
|
|
|
|
* for the lock if another process has already acquired it.
|
2011-07-06 16:06:58 +00:00
|
|
|
*
|
|
|
|
* The lock will be released when @fd is closed. The lock
|
|
|
|
* will also be released if *any* other open file descriptor
|
|
|
|
* pointing to the same underlying file is closed. As such
|
|
|
|
* this function should not be relied on in multi-threaded
|
|
|
|
* apps where other threads can be opening/closing arbitrary
|
|
|
|
* files.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, or -errno otherwise
|
|
|
|
*/
|
2014-03-17 14:17:36 +00:00
|
|
|
int virFileLock(int fd, bool shared, off_t start, off_t len, bool waitForLock)
|
2011-07-06 16:06:58 +00:00
|
|
|
{
|
|
|
|
struct flock fl = {
|
|
|
|
.l_type = shared ? F_RDLCK : F_WRLCK,
|
|
|
|
.l_whence = SEEK_SET,
|
|
|
|
.l_start = start,
|
|
|
|
.l_len = len,
|
|
|
|
};
|
|
|
|
|
2014-03-17 14:17:36 +00:00
|
|
|
int cmd = waitForLock ? F_SETLKW : F_SETLK;
|
|
|
|
|
|
|
|
if (fcntl(fd, cmd, &fl) < 0)
|
2011-07-06 16:06:58 +00:00
|
|
|
return -errno;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virFileUnlock:
|
|
|
|
* @fd: file descriptor to release the lock on
|
|
|
|
* @start: byte offset to start unlock
|
|
|
|
* @len: length of lock (0 to release entire remaining file from @start)
|
|
|
|
*
|
|
|
|
* Release a lock previously acquired with virFileUnlock().
|
|
|
|
* NB the lock will also be released if any open file descriptor
|
|
|
|
* pointing to the same file as @fd is closed
|
|
|
|
*
|
2016-06-20 11:52:20 +00:00
|
|
|
* Returns 0 on success, or -errno on error
|
2011-07-06 16:06:58 +00:00
|
|
|
*/
|
|
|
|
int virFileUnlock(int fd, off_t start, off_t len)
|
|
|
|
{
|
|
|
|
struct flock fl = {
|
|
|
|
.l_type = F_UNLCK,
|
|
|
|
.l_whence = SEEK_SET,
|
|
|
|
.l_start = start,
|
|
|
|
.l_len = len,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (fcntl(fd, F_SETLK, &fl) < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2018-02-08 13:53:20 +00:00
|
|
|
|
|
|
|
|
2020-01-17 11:24:19 +00:00
|
|
|
#else /* WIN32 */
|
2018-02-08 13:53:20 +00:00
|
|
|
|
2020-07-29 10:46:12 +00:00
|
|
|
|
2019-10-14 12:45:33 +00:00
|
|
|
int virFileLock(int fd G_GNUC_UNUSED,
|
|
|
|
bool shared G_GNUC_UNUSED,
|
|
|
|
off_t start G_GNUC_UNUSED,
|
|
|
|
off_t len G_GNUC_UNUSED,
|
|
|
|
bool waitForLock G_GNUC_UNUSED)
|
2011-07-06 16:06:58 +00:00
|
|
|
{
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
2018-02-08 13:53:20 +00:00
|
|
|
|
|
|
|
|
2019-10-14 12:45:33 +00:00
|
|
|
int virFileUnlock(int fd G_GNUC_UNUSED,
|
|
|
|
off_t start G_GNUC_UNUSED,
|
|
|
|
off_t len G_GNUC_UNUSED)
|
2011-07-06 16:06:58 +00:00
|
|
|
{
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
2018-02-08 13:53:20 +00:00
|
|
|
|
|
|
|
|
2020-01-17 11:24:19 +00:00
|
|
|
#endif /* WIN32 */
|
2011-10-13 10:17:12 +00:00
|
|
|
|
2018-02-08 13:53:20 +00:00
|
|
|
|
2011-10-13 10:17:12 +00:00
|
|
|
int
|
|
|
|
virFileRewrite(const char *path,
|
|
|
|
mode_t mode,
|
|
|
|
virFileRewriteFunc rewrite,
|
2016-11-28 08:00:55 +00:00
|
|
|
const void *opaque)
|
2011-10-13 10:17:12 +00:00
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *newfile = NULL;
|
2011-10-13 10:17:12 +00:00
|
|
|
int fd = -1;
|
|
|
|
int ret = -1;
|
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
newfile = g_strdup_printf("%s.new", path);
|
2011-10-13 10:17:12 +00:00
|
|
|
|
|
|
|
if ((fd = open(newfile, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) {
|
|
|
|
virReportSystemError(errno, _("cannot create file '%s'"),
|
|
|
|
newfile);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rewrite(fd, opaque) < 0) {
|
|
|
|
virReportSystemError(errno, _("cannot write data to file '%s'"),
|
|
|
|
newfile);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2019-12-20 15:57:24 +00:00
|
|
|
if (g_fsync(fd) < 0) {
|
2011-10-13 10:17:12 +00:00
|
|
|
virReportSystemError(errno, _("cannot sync file '%s'"),
|
|
|
|
newfile);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (VIR_CLOSE(fd) < 0) {
|
|
|
|
virReportSystemError(errno, _("cannot save file '%s'"),
|
|
|
|
newfile);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rename(newfile, path) < 0) {
|
|
|
|
virReportSystemError(errno, _("cannot rename file '%s' as '%s'"),
|
|
|
|
newfile, path);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
cleanup:
|
2011-10-13 10:17:12 +00:00
|
|
|
VIR_FORCE_CLOSE(fd);
|
2019-11-12 21:10:12 +00:00
|
|
|
unlink(newfile);
|
2011-10-13 10:17:12 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2012-01-11 09:58:59 +00:00
|
|
|
|
|
|
|
|
2016-11-28 08:00:55 +00:00
|
|
|
static int
|
|
|
|
virFileRewriteStrHelper(int fd, const void *opaque)
|
|
|
|
{
|
|
|
|
const char *data = opaque;
|
|
|
|
|
|
|
|
if (safewrite(fd, data, strlen(data)) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
virFileRewriteStr(const char *path,
|
|
|
|
mode_t mode,
|
|
|
|
const char *str)
|
|
|
|
{
|
|
|
|
return virFileRewrite(path, mode,
|
|
|
|
virFileRewriteStrHelper, str);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-11 09:58:59 +00:00
|
|
|
int virFileTouch(const char *path, mode_t mode)
|
|
|
|
{
|
|
|
|
int fd = -1;
|
|
|
|
|
|
|
|
if ((fd = open(path, O_WRONLY | O_CREAT, mode)) < 0) {
|
|
|
|
virReportSystemError(errno, _("cannot create file '%s'"),
|
|
|
|
path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (VIR_CLOSE(fd) < 0) {
|
|
|
|
virReportSystemError(errno, _("cannot save file '%s'"),
|
|
|
|
path);
|
|
|
|
VIR_FORCE_CLOSE(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2012-04-27 13:50:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
#define MODE_BITS (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
|
|
|
|
|
|
|
|
int virFileUpdatePerm(const char *path,
|
|
|
|
mode_t mode_remove,
|
|
|
|
mode_t mode_add)
|
|
|
|
{
|
|
|
|
struct stat sb;
|
|
|
|
mode_t mode;
|
|
|
|
|
|
|
|
if (mode_remove & ~MODE_BITS || mode_add & ~MODE_BITS) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INVALID_ARG, "%s", _("invalid mode"));
|
2012-04-27 13:50:22 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stat(path, &sb) < 0) {
|
|
|
|
virReportSystemError(errno, _("cannot stat '%s'"), path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
mode = sb.st_mode & MODE_BITS;
|
|
|
|
|
|
|
|
if ((mode & mode_remove) == 0 && (mode & mode_add) == mode_add)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
mode &= MODE_BITS ^ mode_remove;
|
|
|
|
mode |= mode_add;
|
|
|
|
|
|
|
|
if (chmod(path, mode) < 0) {
|
|
|
|
virReportSystemError(errno, _("cannot change permission of '%s'"),
|
|
|
|
path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2012-07-03 14:06:27 +00:00
|
|
|
|
|
|
|
|
2020-09-01 11:27:44 +00:00
|
|
|
#if defined(__linux__) && WITH_DECL_LO_FLAGS_AUTOCLEAR
|
2013-09-05 11:04:33 +00:00
|
|
|
|
2020-09-01 11:27:44 +00:00
|
|
|
# if WITH_DECL_LOOP_CTL_GET_FREE
|
2013-09-05 11:04:33 +00:00
|
|
|
|
2014-04-20 20:07:46 +00:00
|
|
|
/* virFileLoopDeviceOpenLoopCtl() returns -1 when a real failure has occurred
|
2013-09-05 11:04:33 +00:00
|
|
|
* while in the process of allocating or opening the loop device. On success
|
|
|
|
* we return 0 and modify the fd to the appropriate file descriptor.
|
|
|
|
* If /dev/loop-control does not exist, we return 0 and do not set fd. */
|
|
|
|
|
|
|
|
static int virFileLoopDeviceOpenLoopCtl(char **dev_name, int *fd)
|
|
|
|
{
|
|
|
|
int devnr;
|
|
|
|
int ctl_fd;
|
|
|
|
char *looppath = NULL;
|
|
|
|
|
|
|
|
VIR_DEBUG("Opening loop-control device");
|
|
|
|
if ((ctl_fd = open("/dev/loop-control", O_RDWR)) < 0) {
|
|
|
|
if (errno == ENOENT)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
virReportSystemError(errno, "%s",
|
|
|
|
_("Unable to open /dev/loop-control"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((devnr = ioctl(ctl_fd, LOOP_CTL_GET_FREE)) < 0) {
|
|
|
|
virReportSystemError(errno, "%s",
|
|
|
|
_("Unable to get free loop device via ioctl"));
|
|
|
|
close(ctl_fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
close(ctl_fd);
|
|
|
|
|
|
|
|
VIR_DEBUG("Found free loop device number %i", devnr);
|
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
looppath = g_strdup_printf("/dev/loop%i", devnr);
|
2013-09-05 11:04:33 +00:00
|
|
|
|
|
|
|
if ((*fd = open(looppath, O_RDWR)) < 0) {
|
|
|
|
virReportSystemError(errno,
|
2018-09-19 08:38:14 +00:00
|
|
|
_("Unable to open %s"), looppath);
|
2013-09-05 11:04:33 +00:00
|
|
|
VIR_FREE(looppath);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*dev_name = looppath;
|
|
|
|
return 0;
|
|
|
|
}
|
2020-09-01 11:27:44 +00:00
|
|
|
# endif /* WITH_DECL_LOOP_CTL_GET_FREE */
|
2013-09-05 11:04:33 +00:00
|
|
|
|
|
|
|
static int virFileLoopDeviceOpenSearch(char **dev_name)
|
2012-07-03 14:06:27 +00:00
|
|
|
{
|
|
|
|
int fd = -1;
|
|
|
|
DIR *dh = NULL;
|
|
|
|
struct dirent *de;
|
2013-01-22 14:15:43 +00:00
|
|
|
char *looppath = NULL;
|
2012-07-03 14:06:27 +00:00
|
|
|
struct loop_info64 lo;
|
2014-04-25 20:45:49 +00:00
|
|
|
int direrr;
|
2012-07-03 14:06:27 +00:00
|
|
|
|
|
|
|
VIR_DEBUG("Looking for loop devices in /dev");
|
|
|
|
|
2016-06-21 14:34:08 +00:00
|
|
|
if (virDirOpen(&dh, "/dev") < 0)
|
2012-07-03 14:06:27 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2014-04-25 20:45:49 +00:00
|
|
|
while ((direrr = virDirRead(dh, &de, "/dev")) > 0) {
|
2013-08-09 16:25:47 +00:00
|
|
|
/* Checking 'loop' prefix is insufficient, since
|
|
|
|
* new kernels have a dev named 'loop-control'
|
|
|
|
*/
|
|
|
|
if (!STRPREFIX(de->d_name, "loop") ||
|
2019-11-18 14:13:11 +00:00
|
|
|
!g_ascii_isdigit(de->d_name[4]))
|
2012-07-03 14:06:27 +00:00
|
|
|
continue;
|
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
looppath = g_strdup_printf("/dev/%s", de->d_name);
|
2012-07-03 14:06:27 +00:00
|
|
|
|
|
|
|
VIR_DEBUG("Checking up on device %s", looppath);
|
|
|
|
if ((fd = open(looppath, O_RDWR)) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Unable to open %s"), looppath);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ioctl(fd, LOOP_GET_STATUS64, &lo) < 0) {
|
|
|
|
/* Got a free device, return the fd */
|
|
|
|
if (errno == ENXIO)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
VIR_FORCE_CLOSE(fd);
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Unable to get loop status on %s"),
|
|
|
|
looppath);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Oh well, try the next device */
|
|
|
|
VIR_FORCE_CLOSE(fd);
|
|
|
|
VIR_FREE(looppath);
|
|
|
|
}
|
2014-04-25 20:45:49 +00:00
|
|
|
if (direrr < 0)
|
|
|
|
goto cleanup;
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Unable to find a free loop device in /dev"));
|
2012-07-03 14:06:27 +00:00
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
cleanup:
|
2012-07-03 14:06:27 +00:00
|
|
|
if (fd != -1) {
|
|
|
|
VIR_DEBUG("Got free loop device %s %d", looppath, fd);
|
|
|
|
*dev_name = looppath;
|
|
|
|
} else {
|
|
|
|
VIR_DEBUG("No free loop devices available");
|
|
|
|
VIR_FREE(looppath);
|
|
|
|
}
|
2016-06-21 10:40:29 +00:00
|
|
|
VIR_DIR_CLOSE(dh);
|
2012-07-03 14:06:27 +00:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2013-09-05 11:04:33 +00:00
|
|
|
static int virFileLoopDeviceOpen(char **dev_name)
|
|
|
|
{
|
|
|
|
int loop_fd = -1;
|
|
|
|
|
2020-09-01 11:27:44 +00:00
|
|
|
# if WITH_DECL_LOOP_CTL_GET_FREE
|
2013-09-05 11:04:33 +00:00
|
|
|
if (virFileLoopDeviceOpenLoopCtl(dev_name, &loop_fd) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2015-10-27 18:14:01 +00:00
|
|
|
VIR_DEBUG("Return from loop-control got fd %d", loop_fd);
|
2013-09-05 11:04:33 +00:00
|
|
|
|
|
|
|
if (loop_fd >= 0)
|
|
|
|
return loop_fd;
|
2020-09-01 11:27:44 +00:00
|
|
|
# endif /* WITH_DECL_LOOP_CTL_GET_FREE */
|
2013-09-05 11:04:33 +00:00
|
|
|
|
|
|
|
/* Without the loop control device we just use the old technique. */
|
|
|
|
loop_fd = virFileLoopDeviceOpenSearch(dev_name);
|
|
|
|
|
|
|
|
return loop_fd;
|
|
|
|
}
|
2012-07-03 14:06:27 +00:00
|
|
|
|
|
|
|
int virFileLoopDeviceAssociate(const char *file,
|
|
|
|
char **dev)
|
|
|
|
{
|
|
|
|
int lofd = -1;
|
|
|
|
int fsfd = -1;
|
|
|
|
struct loop_info64 lo;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *loname = NULL;
|
2012-07-03 14:06:27 +00:00
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
if ((lofd = virFileLoopDeviceOpen(&loname)) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
memset(&lo, 0, sizeof(lo));
|
|
|
|
lo.lo_flags = LO_FLAGS_AUTOCLEAR;
|
|
|
|
|
2019-09-02 17:00:27 +00:00
|
|
|
/* Set backing file name for LOOP_GET_STATUS64 queries */
|
|
|
|
if (virStrncpy((char *) lo.lo_file_name, file,
|
|
|
|
strlen(file), LO_NAME_SIZE) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Unable to set backing file %s"), file);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2012-07-03 14:06:27 +00:00
|
|
|
if ((fsfd = open(file, O_RDWR)) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Unable to open %s"), file);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ioctl(lofd, LOOP_SET_FD, fsfd) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Unable to attach %s to loop device"),
|
|
|
|
file);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ioctl(lofd, LOOP_SET_STATUS64, &lo) < 0) {
|
|
|
|
virReportSystemError(errno, "%s",
|
|
|
|
_("Unable to mark loop device as autoclear"));
|
|
|
|
|
|
|
|
if (ioctl(lofd, LOOP_CLR_FD, 0) < 0)
|
|
|
|
VIR_WARN("Unable to detach %s from loop device", file);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_DEBUG("Attached loop device %s %d to %s", file, lofd, loname);
|
|
|
|
*dev = loname;
|
|
|
|
loname = NULL;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
cleanup:
|
2012-07-03 14:06:27 +00:00
|
|
|
VIR_FORCE_CLOSE(fsfd);
|
|
|
|
if (ret == -1)
|
|
|
|
VIR_FORCE_CLOSE(lofd);
|
|
|
|
return lofd;
|
|
|
|
}
|
|
|
|
|
2013-04-22 14:06:16 +00:00
|
|
|
|
|
|
|
# define SYSFS_BLOCK_DIR "/sys/block"
|
2015-07-20 12:37:48 +00:00
|
|
|
# define NBD_DRIVER "nbd"
|
2013-04-22 14:06:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
static int
|
2014-10-01 14:07:46 +00:00
|
|
|
virFileNBDDeviceIsBusy(const char *dev_name)
|
2013-04-22 14:06:16 +00:00
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *path = NULL;
|
2013-04-22 14:06:16 +00:00
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
path = g_strdup_printf(SYSFS_BLOCK_DIR "/%s/pid", dev_name);
|
2013-04-22 14:06:16 +00:00
|
|
|
|
2013-09-13 13:32:43 +00:00
|
|
|
if (!virFileExists(path)) {
|
2013-04-22 14:06:16 +00:00
|
|
|
if (errno == ENOENT)
|
2018-07-13 17:54:48 +00:00
|
|
|
return 0;
|
2013-04-22 14:06:16 +00:00
|
|
|
else
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Cannot check NBD device %s pid"),
|
2014-10-01 14:07:46 +00:00
|
|
|
dev_name);
|
2018-07-13 17:54:48 +00:00
|
|
|
return -1;
|
2013-04-22 14:06:16 +00:00
|
|
|
}
|
2018-07-13 17:54:48 +00:00
|
|
|
return 1;
|
2013-04-22 14:06:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
virFileNBDDeviceFindUnused(void)
|
|
|
|
{
|
|
|
|
DIR *dh;
|
|
|
|
char *ret = NULL;
|
|
|
|
struct dirent *de;
|
2014-04-25 20:45:49 +00:00
|
|
|
int direrr;
|
2013-04-22 14:06:16 +00:00
|
|
|
|
2016-06-21 14:34:08 +00:00
|
|
|
if (virDirOpen(&dh, SYSFS_BLOCK_DIR) < 0)
|
2013-04-22 14:06:16 +00:00
|
|
|
return NULL;
|
|
|
|
|
2014-04-25 20:45:49 +00:00
|
|
|
while ((direrr = virDirRead(dh, &de, SYSFS_BLOCK_DIR)) > 0) {
|
2013-04-22 14:06:16 +00:00
|
|
|
if (STRPREFIX(de->d_name, "nbd")) {
|
|
|
|
int rv = virFileNBDDeviceIsBusy(de->d_name);
|
|
|
|
if (rv < 0)
|
|
|
|
goto cleanup;
|
|
|
|
if (rv == 0) {
|
2019-10-22 13:26:14 +00:00
|
|
|
ret = g_strdup_printf("/dev/%s", de->d_name);
|
2013-04-22 14:06:16 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-25 20:45:49 +00:00
|
|
|
if (direrr < 0)
|
|
|
|
goto cleanup;
|
|
|
|
virReportSystemError(EBUSY, "%s",
|
|
|
|
_("No free NBD devices"));
|
2013-04-22 14:06:16 +00:00
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
cleanup:
|
2016-06-21 10:40:29 +00:00
|
|
|
VIR_DIR_CLOSE(dh);
|
2013-04-22 14:06:16 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-07-20 12:37:48 +00:00
|
|
|
static bool
|
|
|
|
virFileNBDLoadDriver(void)
|
|
|
|
{
|
2020-06-16 11:12:09 +00:00
|
|
|
if (virKModIsProhibited(NBD_DRIVER)) {
|
2015-07-30 09:10:48 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
2015-07-20 12:37:48 +00:00
|
|
|
_("Failed to load nbd module: "
|
|
|
|
"administratively prohibited"));
|
|
|
|
return false;
|
|
|
|
} else {
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *errbuf = NULL;
|
2015-07-20 12:37:48 +00:00
|
|
|
|
2020-06-16 11:07:30 +00:00
|
|
|
if ((errbuf = virKModLoad(NBD_DRIVER))) {
|
2015-07-30 09:10:48 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
2015-07-20 12:37:48 +00:00
|
|
|
_("Failed to load nbd module"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2013-04-22 14:06:16 +00:00
|
|
|
|
|
|
|
int virFileNBDDeviceAssociate(const char *file,
|
2014-04-27 00:15:22 +00:00
|
|
|
virStorageFileFormat fmt,
|
2013-04-22 14:06:16 +00:00
|
|
|
bool readonly,
|
|
|
|
char **dev)
|
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *nbddev = NULL;
|
|
|
|
g_autofree char *qemunbd = NULL;
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virCommand) cmd = NULL;
|
2013-04-22 14:06:16 +00:00
|
|
|
const char *fmtstr = NULL;
|
|
|
|
|
2015-07-20 12:37:48 +00:00
|
|
|
if (!virFileNBDLoadDriver())
|
2018-07-13 17:54:49 +00:00
|
|
|
return -1;
|
2015-07-20 12:37:48 +00:00
|
|
|
|
2013-04-22 14:06:16 +00:00
|
|
|
if (!(nbddev = virFileNBDDeviceFindUnused()))
|
2018-07-13 17:54:49 +00:00
|
|
|
return -1;
|
2013-04-22 14:06:16 +00:00
|
|
|
|
|
|
|
if (!(qemunbd = virFindFileInPath("qemu-nbd"))) {
|
|
|
|
virReportSystemError(ENOENT, "%s",
|
|
|
|
_("Unable to find 'qemu-nbd' binary in $PATH"));
|
2018-07-13 17:54:49 +00:00
|
|
|
return -1;
|
2013-04-22 14:06:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fmt > 0)
|
|
|
|
fmtstr = virStorageFileFormatTypeToString(fmt);
|
|
|
|
|
|
|
|
cmd = virCommandNew(qemunbd);
|
|
|
|
|
|
|
|
/* Explicitly not trying to cope with old qemu-nbd which
|
|
|
|
* lacked --format. We want to see a fatal error in that
|
|
|
|
* case since it would be security flaw to continue */
|
|
|
|
if (fmtstr)
|
|
|
|
virCommandAddArgList(cmd, "--format", fmtstr, NULL);
|
|
|
|
|
|
|
|
if (readonly)
|
|
|
|
virCommandAddArg(cmd, "-r");
|
|
|
|
|
|
|
|
virCommandAddArgList(cmd,
|
|
|
|
"-n", /* Don't cache in qemu-nbd layer */
|
|
|
|
"-c", nbddev,
|
|
|
|
file, NULL);
|
|
|
|
|
|
|
|
/* qemu-nbd will daemonize itself */
|
|
|
|
|
|
|
|
if (virCommandRun(cmd, NULL) < 0)
|
2018-07-13 17:54:49 +00:00
|
|
|
return -1;
|
2013-04-22 14:06:16 +00:00
|
|
|
|
2013-10-08 15:35:15 +00:00
|
|
|
VIR_DEBUG("Associated NBD device %s with file %s and format %s",
|
|
|
|
nbddev, file, fmtstr);
|
2013-04-22 14:06:16 +00:00
|
|
|
*dev = nbddev;
|
|
|
|
nbddev = NULL;
|
|
|
|
|
2018-07-13 17:54:49 +00:00
|
|
|
return 0;
|
2013-04-22 14:06:16 +00:00
|
|
|
}
|
|
|
|
|
2012-07-03 14:06:27 +00:00
|
|
|
#else /* __linux__ */
|
|
|
|
|
|
|
|
int virFileLoopDeviceAssociate(const char *file,
|
2019-10-14 12:45:33 +00:00
|
|
|
char **dev G_GNUC_UNUSED)
|
2012-07-03 14:06:27 +00:00
|
|
|
{
|
|
|
|
virReportSystemError(ENOSYS,
|
|
|
|
_("Unable to associate file %s with loop device"),
|
|
|
|
file);
|
2012-07-09 21:21:10 +00:00
|
|
|
*dev = NULL;
|
|
|
|
return -1;
|
2012-07-03 14:06:27 +00:00
|
|
|
}
|
|
|
|
|
2013-04-22 14:06:16 +00:00
|
|
|
int virFileNBDDeviceAssociate(const char *file,
|
2019-10-14 12:45:33 +00:00
|
|
|
virStorageFileFormat fmt G_GNUC_UNUSED,
|
|
|
|
bool readonly G_GNUC_UNUSED,
|
|
|
|
char **dev G_GNUC_UNUSED)
|
2013-04-22 14:06:16 +00:00
|
|
|
{
|
|
|
|
virReportSystemError(ENOSYS,
|
|
|
|
_("Unable to associate file %s with NBD device"),
|
|
|
|
file);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-07-03 14:06:27 +00:00
|
|
|
#endif /* __linux__ */
|
2013-03-28 14:46:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virFileDeleteTree:
|
|
|
|
*
|
|
|
|
* Recursively deletes all files / directories
|
|
|
|
* starting from the directory @dir. Does not
|
|
|
|
* follow symlinks
|
|
|
|
*
|
|
|
|
* NB the algorithm is not efficient, and is subject to
|
|
|
|
* race conditions which can be exploited by malicious
|
|
|
|
* code. It should not be used in any scenarios where
|
|
|
|
* performance is important, or security is critical.
|
|
|
|
*/
|
|
|
|
int virFileDeleteTree(const char *dir)
|
|
|
|
{
|
2015-09-15 20:33:36 +00:00
|
|
|
DIR *dh;
|
2013-03-28 14:46:45 +00:00
|
|
|
struct dirent *de;
|
|
|
|
int ret = -1;
|
2014-04-25 20:45:49 +00:00
|
|
|
int direrr;
|
2013-03-28 14:46:45 +00:00
|
|
|
|
2015-09-15 20:33:36 +00:00
|
|
|
/* Silently return 0 if passed NULL or directory doesn't exist */
|
|
|
|
if (!dir || !virFileExists(dir))
|
|
|
|
return 0;
|
|
|
|
|
2016-06-21 14:34:08 +00:00
|
|
|
if (virDirOpen(&dh, dir) < 0)
|
2013-03-28 14:46:45 +00:00
|
|
|
return -1;
|
|
|
|
|
2014-04-25 20:45:49 +00:00
|
|
|
while ((direrr = virDirRead(dh, &de, dir)) > 0) {
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *filepath = NULL;
|
2019-12-24 15:19:17 +00:00
|
|
|
GStatBuf sb;
|
2013-03-28 14:46:45 +00:00
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
filepath = g_strdup_printf("%s/%s", dir, de->d_name);
|
2013-03-28 14:46:45 +00:00
|
|
|
|
2019-12-24 15:19:17 +00:00
|
|
|
if (g_lstat(filepath, &sb) < 0) {
|
2013-03-28 14:46:45 +00:00
|
|
|
virReportSystemError(errno, _("Cannot access '%s'"),
|
|
|
|
filepath);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (S_ISDIR(sb.st_mode)) {
|
|
|
|
if (virFileDeleteTree(filepath) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
} else {
|
|
|
|
if (unlink(filepath) < 0 && errno != ENOENT) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Cannot delete file '%s'"),
|
|
|
|
filepath);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-25 20:45:49 +00:00
|
|
|
if (direrr < 0)
|
2013-03-28 14:46:45 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (rmdir(dir) < 0 && errno != ENOENT) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Cannot delete directory '%s'"),
|
|
|
|
dir);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
cleanup:
|
2016-06-21 10:40:29 +00:00
|
|
|
VIR_DIR_CLOSE(dh);
|
2013-03-28 14:46:45 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2013-05-09 18:59:04 +00:00
|
|
|
|
|
|
|
/* Like read(), but restarts after EINTR. Doesn't play
|
|
|
|
* nicely with nonblocking FD and EAGAIN, in which case
|
|
|
|
* you want to use bare read(). Or even use virSocket()
|
|
|
|
* if the FD is related to a socket rather than a plain
|
|
|
|
* file or pipe. */
|
|
|
|
ssize_t
|
|
|
|
saferead(int fd, void *buf, size_t count)
|
|
|
|
{
|
|
|
|
size_t nread = 0;
|
|
|
|
while (count > 0) {
|
|
|
|
ssize_t r = read(fd, buf, count);
|
|
|
|
if (r < 0 && errno == EINTR)
|
|
|
|
continue;
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
if (r == 0)
|
|
|
|
return nread;
|
|
|
|
buf = (char *)buf + r;
|
|
|
|
count -= r;
|
|
|
|
nread += r;
|
|
|
|
}
|
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Like write(), but restarts after EINTR. Doesn't play
|
|
|
|
* nicely with nonblocking FD and EAGAIN, in which case
|
|
|
|
* you want to use bare write(). Or even use virSocket()
|
|
|
|
* if the FD is related to a socket rather than a plain
|
|
|
|
* file or pipe. */
|
|
|
|
ssize_t
|
|
|
|
safewrite(int fd, const void *buf, size_t count)
|
|
|
|
{
|
|
|
|
size_t nwritten = 0;
|
|
|
|
while (count > 0) {
|
|
|
|
ssize_t r = write(fd, buf, count);
|
|
|
|
|
|
|
|
if (r < 0 && errno == EINTR)
|
|
|
|
continue;
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
if (r == 0)
|
|
|
|
return nwritten;
|
|
|
|
buf = (const char *)buf + r;
|
|
|
|
count -= r;
|
|
|
|
nwritten += r;
|
|
|
|
}
|
|
|
|
return nwritten;
|
|
|
|
}
|
|
|
|
|
2020-09-01 11:27:44 +00:00
|
|
|
#ifdef WITH_POSIX_FALLOCATE
|
2014-12-05 21:18:29 +00:00
|
|
|
static int
|
|
|
|
safezero_posix_fallocate(int fd, off_t offset, off_t len)
|
2013-05-09 18:59:04 +00:00
|
|
|
{
|
|
|
|
int ret = posix_fallocate(fd, offset, len);
|
|
|
|
if (ret == 0)
|
|
|
|
return 0;
|
|
|
|
errno = ret;
|
|
|
|
return -1;
|
|
|
|
}
|
2020-09-01 11:27:44 +00:00
|
|
|
#else /* !WITH_POSIX_FALLOCATE */
|
2014-12-16 19:10:20 +00:00
|
|
|
static int
|
2019-10-14 12:45:33 +00:00
|
|
|
safezero_posix_fallocate(int fd G_GNUC_UNUSED,
|
|
|
|
off_t offset G_GNUC_UNUSED,
|
|
|
|
off_t len G_GNUC_UNUSED)
|
2014-12-16 19:10:20 +00:00
|
|
|
{
|
2015-01-07 15:49:00 +00:00
|
|
|
return -2;
|
2014-12-16 19:10:20 +00:00
|
|
|
}
|
2020-09-01 11:27:44 +00:00
|
|
|
#endif /* !WITH_POSIX_FALLOCATE */
|
2013-10-02 07:31:09 +00:00
|
|
|
|
2020-09-01 11:27:44 +00:00
|
|
|
#if WITH_SYS_SYSCALL_H && defined(SYS_fallocate)
|
2014-12-08 13:06:57 +00:00
|
|
|
static int
|
|
|
|
safezero_sys_fallocate(int fd,
|
|
|
|
off_t offset,
|
|
|
|
off_t len)
|
|
|
|
{
|
2015-01-07 15:49:00 +00:00
|
|
|
return syscall(SYS_fallocate, fd, 0, offset, len);
|
2014-12-16 19:10:20 +00:00
|
|
|
}
|
2020-09-01 11:27:44 +00:00
|
|
|
#else /* !WITH_SYS_SYSCALL_H || !defined(SYS_fallocate) */
|
2014-12-16 19:10:20 +00:00
|
|
|
static int
|
2019-10-14 12:45:33 +00:00
|
|
|
safezero_sys_fallocate(int fd G_GNUC_UNUSED,
|
|
|
|
off_t offset G_GNUC_UNUSED,
|
|
|
|
off_t len G_GNUC_UNUSED)
|
2014-12-16 19:10:20 +00:00
|
|
|
{
|
2015-01-07 15:49:00 +00:00
|
|
|
return -2;
|
2014-12-08 13:06:57 +00:00
|
|
|
}
|
2020-09-01 11:27:44 +00:00
|
|
|
#endif /* !WITH_SYS_SYSCALL_H || !defined(SYS_fallocate) */
|
2014-12-08 13:06:57 +00:00
|
|
|
|
2020-09-01 11:27:44 +00:00
|
|
|
#ifdef WITH_MMAP
|
2014-12-05 21:18:29 +00:00
|
|
|
static int
|
|
|
|
safezero_mmap(int fd, off_t offset, off_t len)
|
2013-05-09 18:59:04 +00:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
char *buf;
|
2014-10-28 18:38:04 +00:00
|
|
|
static long pagemask;
|
2013-10-02 07:31:09 +00:00
|
|
|
off_t map_skip;
|
2013-05-09 18:59:04 +00:00
|
|
|
|
2013-09-30 11:01:45 +00:00
|
|
|
/* align offset and length, rounding offset down and length up */
|
|
|
|
if (pagemask == 0)
|
2015-02-02 10:26:49 +00:00
|
|
|
pagemask = ~(virGetSystemPageSize() - 1);
|
2013-09-30 11:01:45 +00:00
|
|
|
map_skip = offset - (offset & pagemask);
|
|
|
|
|
2013-05-09 18:59:04 +00:00
|
|
|
/* memset wants the mmap'ed file to be present on disk so create a
|
|
|
|
* sparse file
|
|
|
|
*/
|
|
|
|
r = ftruncate(fd, offset + len);
|
|
|
|
if (r < 0)
|
|
|
|
return -1;
|
|
|
|
|
2013-09-30 11:01:45 +00:00
|
|
|
buf = mmap(NULL, len + map_skip, PROT_READ | PROT_WRITE, MAP_SHARED,
|
|
|
|
fd, offset - map_skip);
|
2013-10-02 07:31:09 +00:00
|
|
|
if (buf != MAP_FAILED) {
|
|
|
|
memset(buf + map_skip, 0, len);
|
|
|
|
munmap(buf, len + map_skip);
|
2013-05-09 18:59:04 +00:00
|
|
|
|
2013-10-02 07:31:09 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2013-05-09 18:59:04 +00:00
|
|
|
|
2013-10-02 07:31:09 +00:00
|
|
|
/* fall back to writing zeroes using safewrite if mmap fails (for
|
|
|
|
* example because of virtual memory limits) */
|
2015-01-07 15:49:00 +00:00
|
|
|
return -2;
|
2014-12-05 21:18:29 +00:00
|
|
|
}
|
2020-09-01 11:27:44 +00:00
|
|
|
#else /* !WITH_MMAP */
|
2014-12-16 19:10:20 +00:00
|
|
|
static int
|
2019-10-14 12:45:33 +00:00
|
|
|
safezero_mmap(int fd G_GNUC_UNUSED,
|
|
|
|
off_t offset G_GNUC_UNUSED,
|
|
|
|
off_t len G_GNUC_UNUSED)
|
2014-12-16 19:10:20 +00:00
|
|
|
{
|
2015-01-07 15:49:00 +00:00
|
|
|
return -2;
|
2014-12-16 19:10:20 +00:00
|
|
|
}
|
2020-09-01 11:27:44 +00:00
|
|
|
#endif /* !WITH_MMAP */
|
2014-12-05 21:18:29 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
safezero_slow(int fd, off_t offset, off_t len)
|
|
|
|
{
|
|
|
|
int r;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *buf = NULL;
|
2014-12-05 21:18:29 +00:00
|
|
|
unsigned long long remain, bytes;
|
2013-05-09 18:59:04 +00:00
|
|
|
|
|
|
|
if (lseek(fd, offset, SEEK_SET) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Split up the write in small chunks so as not to allocate lots of RAM */
|
|
|
|
remain = len;
|
2013-10-04 13:49:05 +00:00
|
|
|
bytes = MIN(1024 * 1024, len);
|
2013-05-09 18:59:04 +00:00
|
|
|
|
|
|
|
r = VIR_ALLOC_N(buf, bytes);
|
|
|
|
if (r < 0) {
|
|
|
|
errno = ENOMEM;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (remain) {
|
|
|
|
if (bytes > remain)
|
|
|
|
bytes = remain;
|
|
|
|
|
|
|
|
r = safewrite(fd, buf, bytes);
|
2018-07-13 17:54:48 +00:00
|
|
|
if (r < 0)
|
2013-05-09 18:59:04 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* safewrite() guarantees all data will be written */
|
|
|
|
remain -= bytes;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-01-07 15:49:00 +00:00
|
|
|
int safezero(int fd, off_t offset, off_t len)
|
2014-12-05 21:18:29 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = safezero_posix_fallocate(fd, offset, len);
|
2015-01-07 15:49:00 +00:00
|
|
|
if (ret != -2)
|
2014-12-05 21:18:29 +00:00
|
|
|
return ret;
|
|
|
|
|
2015-01-07 15:49:00 +00:00
|
|
|
if (safezero_sys_fallocate(fd, offset, len) == 0)
|
2014-12-05 21:18:29 +00:00
|
|
|
return 0;
|
2015-01-07 15:49:00 +00:00
|
|
|
|
|
|
|
ret = safezero_mmap(fd, offset, len);
|
|
|
|
if (ret != -2)
|
|
|
|
return ret;
|
2014-12-05 21:18:29 +00:00
|
|
|
return safezero_slow(fd, offset, len);
|
|
|
|
}
|
2013-05-09 18:59:04 +00:00
|
|
|
|
2017-09-25 14:29:34 +00:00
|
|
|
int virFileAllocate(int fd, off_t offset, off_t len)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = safezero_posix_fallocate(fd, offset, len);
|
|
|
|
if (ret != -2)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return safezero_sys_fallocate(fd, offset, len);
|
|
|
|
}
|
|
|
|
|
2020-09-01 11:27:44 +00:00
|
|
|
#if defined WITH_MNTENT_H && defined WITH_GETMNTENT_R
|
2013-05-09 18:59:04 +00:00
|
|
|
/* search /proc/mounts for mount point of *type; return pointer to
|
|
|
|
* malloc'ed string of the path if found, otherwise return NULL
|
|
|
|
* with errno set to an appropriate value.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
virFileFindMountPoint(const char *type)
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
struct mntent mb;
|
|
|
|
char mntbuf[1024];
|
|
|
|
char *ret = NULL;
|
|
|
|
|
|
|
|
f = setmntent("/proc/mounts", "r");
|
|
|
|
if (!f)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
while (getmntent_r(f, &mb, mntbuf, sizeof(mntbuf))) {
|
|
|
|
if (STREQ(mb.mnt_type, type)) {
|
2019-10-18 11:27:03 +00:00
|
|
|
ret = g_strdup(mb.mnt_dir);
|
2013-05-09 18:59:04 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
errno = ENOENT;
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
cleanup:
|
2013-05-09 18:59:04 +00:00
|
|
|
endmntent(f);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-09-01 11:27:44 +00:00
|
|
|
#else /* defined WITH_MNTENT_H && defined WITH_GETMNTENT_R */
|
2013-05-09 18:59:04 +00:00
|
|
|
|
|
|
|
char *
|
2019-10-14 12:45:33 +00:00
|
|
|
virFileFindMountPoint(const char *type G_GNUC_UNUSED)
|
2013-05-09 18:59:04 +00:00
|
|
|
{
|
|
|
|
errno = ENOSYS;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-09-01 11:27:44 +00:00
|
|
|
#endif /* defined WITH_MNTENT_H && defined WITH_GETMNTENT_R */
|
2013-05-09 18:59:04 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
virBuildPathInternal(char **path, ...)
|
|
|
|
{
|
|
|
|
char *path_component = NULL;
|
2020-07-03 02:30:20 +00:00
|
|
|
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
|
2013-05-09 18:59:04 +00:00
|
|
|
va_list ap;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
va_start(ap, path);
|
|
|
|
|
|
|
|
path_component = va_arg(ap, char *);
|
|
|
|
virBufferAdd(&buf, path_component, -1);
|
|
|
|
|
2014-09-03 22:24:43 +00:00
|
|
|
while ((path_component = va_arg(ap, char *)) != NULL) {
|
2013-05-09 18:59:04 +00:00
|
|
|
virBufferAddChar(&buf, '/');
|
|
|
|
virBufferAdd(&buf, path_component, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
*path = virBufferContentAndReset(&buf);
|
2014-11-13 14:28:18 +00:00
|
|
|
if (*path == NULL)
|
2013-05-09 18:59:04 +00:00
|
|
|
ret = -1;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-01-17 16:00:27 +00:00
|
|
|
/* Read no more than the specified maximum number of bytes. */
|
2013-05-09 18:59:04 +00:00
|
|
|
static char *
|
|
|
|
saferead_lim(int fd, size_t max_len, size_t *length)
|
|
|
|
{
|
|
|
|
char *buf = NULL;
|
|
|
|
size_t alloc = 0;
|
|
|
|
size_t size = 0;
|
|
|
|
int save_errno;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
int count;
|
|
|
|
int requested;
|
|
|
|
|
|
|
|
if (size + BUFSIZ + 1 > alloc) {
|
|
|
|
alloc += alloc / 2;
|
|
|
|
if (alloc < size + BUFSIZ + 1)
|
|
|
|
alloc = size + BUFSIZ + 1;
|
|
|
|
|
|
|
|
if (VIR_REALLOC_N(buf, alloc) < 0) {
|
|
|
|
save_errno = errno;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ensure that (size + requested <= max_len); */
|
|
|
|
requested = MIN(size < max_len ? max_len - size : 0,
|
|
|
|
alloc - size - 1);
|
|
|
|
count = saferead(fd, buf + size, requested);
|
|
|
|
size += count;
|
|
|
|
|
|
|
|
if (count != requested || requested == 0) {
|
|
|
|
save_errno = errno;
|
|
|
|
if (count < 0)
|
|
|
|
break;
|
|
|
|
buf[size] = '\0';
|
|
|
|
*length = size;
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_FREE(buf);
|
|
|
|
errno = save_errno;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-11-05 17:30:56 +00:00
|
|
|
|
|
|
|
/* A wrapper around saferead_lim that merely stops reading at the
|
|
|
|
* specified maximum size. */
|
|
|
|
int
|
|
|
|
virFileReadHeaderFD(int fd, int maxlen, char **buf)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
if (maxlen <= 0) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
s = saferead_lim(fd, maxlen, &len);
|
|
|
|
if (s == NULL)
|
|
|
|
return -1;
|
|
|
|
*buf = s;
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-12 15:23:40 +00:00
|
|
|
int
|
|
|
|
virFileReadHeaderQuiet(const char *path,
|
|
|
|
int maxlen,
|
|
|
|
char **buf)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
fd = open(path, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
len = virFileReadHeaderFD(fd, maxlen, buf);
|
|
|
|
VIR_FORCE_CLOSE(fd);
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-09 18:59:04 +00:00
|
|
|
/* A wrapper around saferead_lim that maps a failure due to
|
|
|
|
exceeding the maximum size limitation to EOVERFLOW. */
|
|
|
|
int
|
|
|
|
virFileReadLimFD(int fd, int maxlen, char **buf)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
if (maxlen <= 0) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
s = saferead_lim(fd, maxlen+1, &len);
|
|
|
|
if (s == NULL)
|
|
|
|
return -1;
|
|
|
|
if (len > maxlen || (int)len != len) {
|
|
|
|
VIR_FREE(s);
|
|
|
|
/* There was at least one byte more than MAXLEN.
|
|
|
|
Set errno accordingly. */
|
|
|
|
errno = EOVERFLOW;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*buf = s;
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
virFileReadAll(const char *path, int maxlen, char **buf)
|
|
|
|
{
|
2020-07-28 17:50:28 +00:00
|
|
|
int fd;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
fd = open(path, O_RDONLY);
|
2013-05-09 18:59:04 +00:00
|
|
|
if (fd < 0) {
|
|
|
|
virReportSystemError(errno, _("Failed to open file '%s'"), path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-07-28 17:50:28 +00:00
|
|
|
len = virFileReadLimFD(fd, maxlen, buf);
|
2013-05-09 18:59:04 +00:00
|
|
|
VIR_FORCE_CLOSE(fd);
|
|
|
|
if (len < 0) {
|
|
|
|
virReportSystemError(errno, _("Failed to read file '%s'"), path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2014-06-30 12:59:42 +00:00
|
|
|
int
|
|
|
|
virFileReadAllQuiet(const char *path, int maxlen, char **buf)
|
|
|
|
{
|
2020-07-28 17:50:28 +00:00
|
|
|
int fd;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
fd = open(path, O_RDONLY);
|
2014-06-30 12:59:42 +00:00
|
|
|
if (fd < 0)
|
|
|
|
return -errno;
|
|
|
|
|
2020-07-28 17:50:28 +00:00
|
|
|
len = virFileReadLimFD(fd, maxlen, buf);
|
2014-06-30 12:59:42 +00:00
|
|
|
VIR_FORCE_CLOSE(fd);
|
|
|
|
if (len < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2016-05-03 09:12:40 +00:00
|
|
|
/* Read @file into preallocated buffer @buf of size @len.
|
|
|
|
* Return value is -errno in case of errors and size
|
|
|
|
* of data read (no trailing zero) in case of success.
|
|
|
|
* If there is more data then @len - 1 then data will be
|
|
|
|
* truncated. */
|
|
|
|
int
|
|
|
|
virFileReadBufQuiet(const char *file, char *buf, int len)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
ssize_t sz;
|
|
|
|
|
|
|
|
fd = open(file, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
sz = saferead(fd, buf, len - 1);
|
|
|
|
VIR_FORCE_CLOSE(fd);
|
|
|
|
if (sz < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
buf[sz] = '\0';
|
|
|
|
return sz;
|
|
|
|
}
|
|
|
|
|
2013-05-09 18:59:04 +00:00
|
|
|
/* Truncate @path and write @str to it. If @mode is 0, ensure that
|
|
|
|
@path exists; otherwise, use @mode if @path must be created.
|
|
|
|
Return 0 for success, nonzero for failure.
|
|
|
|
Be careful to preserve any errno value upon failure. */
|
|
|
|
int
|
|
|
|
virFileWriteStr(const char *path, const char *str, mode_t mode)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
if (mode)
|
|
|
|
fd = open(path, O_WRONLY|O_TRUNC|O_CREAT, mode);
|
|
|
|
else
|
|
|
|
fd = open(path, O_WRONLY|O_TRUNC);
|
|
|
|
if (fd == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (safewrite(fd, str, strlen(str)) < 0) {
|
|
|
|
VIR_FORCE_CLOSE(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Use errno from failed close only if there was no write error. */
|
|
|
|
if (VIR_CLOSE(fd) != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define SAME_INODE(Stat_buf_1, Stat_buf_2) \
|
|
|
|
((Stat_buf_1).st_ino == (Stat_buf_2).st_ino \
|
|
|
|
&& (Stat_buf_1).st_dev == (Stat_buf_2).st_dev)
|
|
|
|
|
|
|
|
/* Return nonzero if checkLink and checkDest
|
2014-04-10 23:36:06 +00:00
|
|
|
* refer to the same file. Otherwise, return 0.
|
|
|
|
*/
|
2013-05-09 18:59:04 +00:00
|
|
|
int
|
|
|
|
virFileLinkPointsTo(const char *checkLink,
|
|
|
|
const char *checkDest)
|
|
|
|
{
|
|
|
|
struct stat src_sb;
|
|
|
|
struct stat dest_sb;
|
|
|
|
|
|
|
|
return (stat(checkLink, &src_sb) == 0
|
|
|
|
&& stat(checkDest, &dest_sb) == 0
|
|
|
|
&& SAME_INODE(src_sb, dest_sb));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-10 23:36:06 +00:00
|
|
|
/* Return positive if checkLink (residing within directory if not
|
|
|
|
* absolute) and checkDest refer to the same file. Otherwise, return
|
|
|
|
* -1 on allocation failure (error reported), or 0 if not the same
|
|
|
|
* (silent).
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virFileRelLinkPointsTo(const char *directory,
|
|
|
|
const char *checkLink,
|
|
|
|
const char *checkDest)
|
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *candidate = NULL;
|
2014-04-10 23:36:06 +00:00
|
|
|
|
|
|
|
if (*checkLink == '/')
|
|
|
|
return virFileLinkPointsTo(checkLink, checkDest);
|
|
|
|
if (!directory) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("cannot resolve '%s' without starting directory"),
|
|
|
|
checkLink);
|
|
|
|
return -1;
|
|
|
|
}
|
2019-10-22 13:26:14 +00:00
|
|
|
candidate = g_strdup_printf("%s/%s", directory, checkLink);
|
2018-07-13 17:54:48 +00:00
|
|
|
return virFileLinkPointsTo(candidate, checkDest);
|
2014-04-10 23:36:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-09 18:59:04 +00:00
|
|
|
static int
|
|
|
|
virFileResolveLinkHelper(const char *linkpath,
|
|
|
|
bool intermediatePaths,
|
|
|
|
char **resultpath)
|
|
|
|
{
|
2019-12-24 15:19:17 +00:00
|
|
|
GStatBuf st;
|
2013-05-09 18:59:04 +00:00
|
|
|
|
|
|
|
*resultpath = NULL;
|
|
|
|
|
|
|
|
/* We don't need the full canonicalization of intermediate
|
|
|
|
* directories, if linkpath is absolute and the basename is
|
|
|
|
* already a non-symlink. */
|
2019-12-23 15:54:41 +00:00
|
|
|
if (g_path_is_absolute(linkpath) && !intermediatePaths) {
|
2019-12-24 15:19:17 +00:00
|
|
|
if (g_lstat(linkpath, &st) < 0)
|
2013-05-09 18:59:04 +00:00
|
|
|
return -1;
|
|
|
|
|
2020-01-17 11:24:19 +00:00
|
|
|
#ifndef WIN32
|
2019-10-20 11:49:46 +00:00
|
|
|
if (!S_ISLNK(st.st_mode)) {
|
|
|
|
*resultpath = g_strdup(linkpath);
|
|
|
|
return 0;
|
|
|
|
}
|
2020-01-17 11:24:19 +00:00
|
|
|
#endif /* WIN32 */
|
2013-05-09 18:59:04 +00:00
|
|
|
}
|
|
|
|
|
2018-05-03 08:04:38 +00:00
|
|
|
*resultpath = virFileCanonicalizePath(linkpath);
|
2013-05-09 18:59:04 +00:00
|
|
|
|
|
|
|
return *resultpath == NULL ? -1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Attempt to resolve a symbolic link, returning an
|
|
|
|
* absolute path where only the last component is guaranteed
|
|
|
|
* not to be a symlink.
|
|
|
|
*
|
|
|
|
* Return 0 if path was not a symbolic, or the link was
|
|
|
|
* resolved. Return -1 with errno set upon error
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virFileResolveLink(const char *linkpath, char **resultpath)
|
|
|
|
{
|
|
|
|
return virFileResolveLinkHelper(linkpath, false, resultpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Attempt to resolve a symbolic link, returning an
|
|
|
|
* absolute path where every component is guaranteed
|
|
|
|
* not to be a symlink.
|
|
|
|
*
|
|
|
|
* Return 0 if path was not a symbolic, or the link was
|
|
|
|
* resolved. Return -1 with errno set upon error
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virFileResolveAllLinks(const char *linkpath, char **resultpath)
|
|
|
|
{
|
|
|
|
return virFileResolveLinkHelper(linkpath, true, resultpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check whether the given file is a link.
|
|
|
|
* Returns 1 in case of the file being a link, 0 in case it is not
|
|
|
|
* a link and the negative errno in all other cases.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virFileIsLink(const char *linkpath)
|
|
|
|
{
|
2019-12-24 15:19:17 +00:00
|
|
|
GStatBuf st;
|
2013-05-09 18:59:04 +00:00
|
|
|
|
2020-01-17 11:24:19 +00:00
|
|
|
/* Still do this on Windows so we report
|
|
|
|
* errors like ENOENT, etc
|
|
|
|
*/
|
2019-12-24 15:19:17 +00:00
|
|
|
if (g_lstat(linkpath, &st) < 0)
|
2013-05-09 18:59:04 +00:00
|
|
|
return -errno;
|
|
|
|
|
2020-01-17 11:24:19 +00:00
|
|
|
#ifndef WIN32
|
2013-05-09 18:59:04 +00:00
|
|
|
return S_ISLNK(st.st_mode) != 0;
|
2020-01-17 11:24:19 +00:00
|
|
|
#else /* WIN32 */
|
|
|
|
return 0;
|
|
|
|
#endif /* WIN32 */
|
2013-05-09 18:59:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Finds a requested executable file in the PATH env. e.g.:
|
2016-04-15 21:00:40 +00:00
|
|
|
* "qemu-img" will return "/usr/bin/qemu-img"
|
2013-05-09 18:59:04 +00:00
|
|
|
*
|
|
|
|
* You must free the result
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
virFindFileInPath(const char *file)
|
|
|
|
{
|
2013-10-09 10:18:15 +00:00
|
|
|
const char *origpath = NULL;
|
2019-11-14 09:25:31 +00:00
|
|
|
VIR_AUTOSTRINGLIST paths = NULL;
|
|
|
|
char **pathiter;
|
2013-05-09 18:59:04 +00:00
|
|
|
|
|
|
|
if (file == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* if we are passed an absolute path (starting with /), return a
|
|
|
|
* copy of that path, after validating that it is executable
|
|
|
|
*/
|
2019-12-23 15:54:41 +00:00
|
|
|
if (g_path_is_absolute(file)) {
|
2019-11-14 09:16:54 +00:00
|
|
|
if (!virFileIsExecutable(file))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return g_strdup(file);
|
2013-05-09 18:59:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If we are passed an anchored path (containing a /), then there
|
|
|
|
* is no path search - it must exist in the current directory
|
|
|
|
*/
|
|
|
|
if (strchr(file, '/')) {
|
2019-11-14 09:16:54 +00:00
|
|
|
char *abspath = NULL;
|
|
|
|
|
|
|
|
if (!virFileIsExecutable(file))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ignore_value(virFileAbsPath(file, &abspath));
|
|
|
|
return abspath;
|
2013-05-09 18:59:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* copy PATH env so we can tweak it */
|
2019-08-01 12:35:56 +00:00
|
|
|
origpath = getenv("PATH");
|
2013-10-09 10:18:15 +00:00
|
|
|
if (!origpath)
|
|
|
|
origpath = "/bin:/usr/bin";
|
2013-05-09 18:59:04 +00:00
|
|
|
|
|
|
|
/* for each path segment, append the file to search for and test for
|
|
|
|
* it. return it if found.
|
|
|
|
*/
|
2019-11-14 09:25:31 +00:00
|
|
|
|
|
|
|
if (!(paths = virStringSplit(origpath, ":", 0)))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (pathiter = paths; *pathiter; pathiter++) {
|
|
|
|
g_autofree char *fullpath = g_strdup_printf("%s/%s", *pathiter, file);
|
2019-10-22 13:26:14 +00:00
|
|
|
if (virFileIsExecutable(fullpath))
|
2019-11-14 09:05:06 +00:00
|
|
|
return g_steal_pointer(&fullpath);
|
2013-05-09 18:59:04 +00:00
|
|
|
}
|
|
|
|
|
2019-11-14 09:05:06 +00:00
|
|
|
return NULL;
|
2013-05-09 18:59:04 +00:00
|
|
|
}
|
|
|
|
|
2014-04-24 14:05:42 +00:00
|
|
|
|
|
|
|
static bool useDirOverride;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virFileFindResourceFull:
|
|
|
|
* @filename: libvirt distributed filename without any path
|
|
|
|
* @prefix: optional string to prepend to filename
|
|
|
|
* @suffix: optional string to append to filename
|
2015-02-13 13:25:27 +00:00
|
|
|
* @builddir: location of the filename in the build tree including
|
2019-03-12 14:11:47 +00:00
|
|
|
* abs_top_srcdir or abs_top_builddir prefix
|
2014-04-24 14:05:42 +00:00
|
|
|
* @installdir: location of the installed binary
|
|
|
|
* @envname: environment variable used to override all dirs
|
|
|
|
*
|
|
|
|
* A helper which will return a path to @filename within
|
|
|
|
* the current build tree, if the calling binary is being
|
|
|
|
* run from the source tree. Otherwise it will return the
|
|
|
|
* path in the installed location.
|
|
|
|
*
|
2018-04-19 14:25:01 +00:00
|
|
|
* Note that this function does not actually check whether
|
|
|
|
* the file exists on disk, it merely builds the fully
|
|
|
|
* qualified path where it is supposed to exist.
|
|
|
|
*
|
2014-04-24 14:05:42 +00:00
|
|
|
* If @envname is non-NULL it will override all other
|
2015-02-13 13:25:27 +00:00
|
|
|
* directory lookup.
|
2014-04-24 14:05:42 +00:00
|
|
|
*
|
|
|
|
* Only use this with @filename files that are part of
|
|
|
|
* the libvirt tree, not 3rd party binaries/files.
|
|
|
|
*
|
|
|
|
* Returns the resolved path (caller frees) or NULL on error
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
virFileFindResourceFull(const char *filename,
|
|
|
|
const char *prefix,
|
|
|
|
const char *suffix,
|
|
|
|
const char *builddir,
|
|
|
|
const char *installdir,
|
|
|
|
const char *envname)
|
|
|
|
{
|
|
|
|
char *ret = NULL;
|
2019-08-01 12:35:56 +00:00
|
|
|
const char *envval = envname ? getenv(envname) : NULL;
|
2015-02-13 13:25:27 +00:00
|
|
|
const char *path;
|
2014-04-24 14:05:42 +00:00
|
|
|
|
|
|
|
if (!prefix)
|
|
|
|
prefix = "";
|
|
|
|
if (!suffix)
|
|
|
|
suffix = "";
|
|
|
|
|
2015-02-13 13:25:27 +00:00
|
|
|
if (envval)
|
|
|
|
path = envval;
|
|
|
|
else if (useDirOverride)
|
|
|
|
path = builddir;
|
|
|
|
else
|
|
|
|
path = installdir;
|
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
ret = g_strdup_printf("%s/%s%s%s", path, prefix, filename, suffix);
|
2014-04-24 14:05:42 +00:00
|
|
|
|
|
|
|
VIR_DEBUG("Resolved '%s' to '%s'", filename, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
virFileFindResource(const char *filename,
|
|
|
|
const char *builddir,
|
|
|
|
const char *installdir)
|
|
|
|
{
|
|
|
|
return virFileFindResourceFull(filename, NULL, NULL, builddir, installdir, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2019-08-29 10:52:08 +00:00
|
|
|
* virFileActivateDirOverrideForProg:
|
2014-04-24 14:05:42 +00:00
|
|
|
* @argv0: argv[0] of the calling program
|
|
|
|
*
|
2020-07-10 10:07:16 +00:00
|
|
|
* Canonicalize current process path from argv0 and check if abs_top_builddir
|
|
|
|
* matches as prefix in the path.
|
2014-04-24 14:05:42 +00:00
|
|
|
*/
|
|
|
|
void
|
2019-08-29 10:52:08 +00:00
|
|
|
virFileActivateDirOverrideForProg(const char *argv0)
|
2014-04-24 14:05:42 +00:00
|
|
|
{
|
2020-07-10 10:07:16 +00:00
|
|
|
g_autofree char *path = virFileCanonicalizePath(argv0);
|
|
|
|
|
|
|
|
if (!path) {
|
|
|
|
VIR_DEBUG("Failed to get canonicalized path errno=%d", errno);
|
2014-04-24 14:05:42 +00:00
|
|
|
return;
|
2020-07-10 10:07:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (STRPREFIX(path, abs_top_builddir)) {
|
2014-04-24 14:05:42 +00:00
|
|
|
useDirOverride = true;
|
2020-07-10 10:07:16 +00:00
|
|
|
VIR_DEBUG("Activating build dir override for %s", path);
|
2014-04-24 14:05:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-17 17:18:27 +00:00
|
|
|
|
2019-08-29 10:52:08 +00:00
|
|
|
/**
|
|
|
|
* virFileActivateDirOverrideForLib:
|
|
|
|
*
|
|
|
|
* Look for LIBVIRT_DIR_OVERRIDE env var to see if we should find files from
|
|
|
|
* the build/src tree instead of install tree.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
virFileActivateDirOverrideForLib(void)
|
|
|
|
{
|
|
|
|
if (getenv("LIBVIRT_DIR_OVERRIDE") != NULL)
|
|
|
|
useDirOverride = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-17 17:18:27 +00:00
|
|
|
/**
|
|
|
|
* virFileLength:
|
|
|
|
* @path: full path of the file
|
|
|
|
* @fd: open file descriptor for file (or -1 to use @path)
|
|
|
|
*
|
|
|
|
* If fd >= 0, return the length of the open file indicated by @fd.
|
|
|
|
* If fd < 0 (i.e. -1) return the length of the file indicated by
|
|
|
|
* @path.
|
|
|
|
*
|
|
|
|
* Returns the length, or -1 if the file doesn't
|
|
|
|
* exist or its info was inaccessible. No error is logged.
|
|
|
|
*/
|
|
|
|
off_t
|
|
|
|
virFileLength(const char *path, int fd)
|
|
|
|
{
|
|
|
|
struct stat s;
|
|
|
|
|
|
|
|
if (fd >= 0) {
|
|
|
|
if (fstat(fd, &s) < 0)
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
if (stat(path, &s) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!S_ISREG(s.st_mode))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return s.st_size;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-09 18:59:04 +00:00
|
|
|
bool
|
|
|
|
virFileIsDir(const char *path)
|
|
|
|
{
|
|
|
|
struct stat s;
|
|
|
|
return (stat(path, &s) == 0) && S_ISDIR(s.st_mode);
|
|
|
|
}
|
|
|
|
|
2018-06-06 16:24:12 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
virFileIsRegular(const char *path)
|
|
|
|
{
|
|
|
|
struct stat s;
|
|
|
|
return (stat(path, &s) == 0) && S_ISREG(s.st_mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-13 12:35:34 +00:00
|
|
|
/**
|
|
|
|
* virFileExists: Check for presence of file
|
|
|
|
* @path: Path of file to check
|
|
|
|
*
|
2017-02-24 13:33:08 +00:00
|
|
|
* Returns true if the file exists, false if it doesn't, setting errno
|
|
|
|
* appropriately.
|
2013-09-13 12:35:34 +00:00
|
|
|
*/
|
2013-05-09 18:59:04 +00:00
|
|
|
bool
|
|
|
|
virFileExists(const char *path)
|
|
|
|
{
|
|
|
|
return access(path, F_OK) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check that a file is regular and has executable bits. If false is
|
|
|
|
* returned, errno is valid.
|
|
|
|
*
|
|
|
|
* Note: In the presence of ACLs, this may return true for a file that
|
|
|
|
* would actually fail with EACCES for a given user, or false for a
|
|
|
|
* file that the user could actually execute, but setups with ACLs
|
|
|
|
* that weird are unusual. */
|
|
|
|
bool
|
|
|
|
virFileIsExecutable(const char *file)
|
|
|
|
{
|
|
|
|
struct stat sb;
|
|
|
|
|
|
|
|
/* We would also want to check faccessat if we cared about ACLs,
|
|
|
|
* but we don't. */
|
|
|
|
if (stat(file, &sb) < 0)
|
|
|
|
return false;
|
|
|
|
if (S_ISREG(sb.st_mode) && (sb.st_mode & 0111) != 0)
|
|
|
|
return true;
|
|
|
|
errno = S_ISDIR(sb.st_mode) ? EISDIR : EACCES;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-07 11:51:58 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check that a file refers to a mount point. Trick is that for
|
|
|
|
* a mount point, the st_dev field will differ from the parent
|
|
|
|
* directory.
|
|
|
|
*
|
|
|
|
* Note that this will not detect bind mounts of dirs/files,
|
|
|
|
* only true filesystem mounts.
|
|
|
|
*/
|
|
|
|
int virFileIsMountPoint(const char *file)
|
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *parent = NULL;
|
2018-07-13 17:54:48 +00:00
|
|
|
int ret;
|
2013-10-07 11:51:58 +00:00
|
|
|
struct stat sb1, sb2;
|
|
|
|
|
2019-12-20 16:48:02 +00:00
|
|
|
parent = g_path_get_dirname(file);
|
2013-10-07 11:51:58 +00:00
|
|
|
|
|
|
|
VIR_DEBUG("Comparing '%s' to '%s'", file, parent);
|
|
|
|
|
|
|
|
if (stat(file, &sb1) < 0) {
|
|
|
|
if (errno == ENOENT)
|
2018-07-13 17:54:48 +00:00
|
|
|
return 0;
|
2013-10-07 11:51:58 +00:00
|
|
|
else
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Cannot stat '%s'"),
|
|
|
|
file);
|
2018-07-13 17:54:48 +00:00
|
|
|
return -1;
|
2013-10-07 11:51:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (stat(parent, &sb2) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Cannot stat '%s'"),
|
|
|
|
parent);
|
2018-07-13 17:54:48 +00:00
|
|
|
return -1;
|
2013-10-07 11:51:58 +00:00
|
|
|
}
|
|
|
|
|
2018-07-13 17:54:48 +00:00
|
|
|
if (!S_ISDIR(sb1.st_mode))
|
|
|
|
return 0;
|
2013-10-07 11:51:58 +00:00
|
|
|
|
|
|
|
ret = sb1.st_dev != sb2.st_dev;
|
|
|
|
VIR_DEBUG("Is mount %d", ret);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-11-27 15:19:49 +00:00
|
|
|
|
2018-04-23 15:41:14 +00:00
|
|
|
#if defined(__linux__)
|
|
|
|
/**
|
|
|
|
* virFileIsCDROM:
|
|
|
|
* @path: File to check
|
|
|
|
*
|
|
|
|
* Returns 1 if @path is a cdrom device 0 if it is not a cdrom and -1 on
|
|
|
|
* error. 'errno' of the failure is preserved and no libvirt errors are
|
|
|
|
* reported.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virFileIsCDROM(const char *path)
|
|
|
|
{
|
|
|
|
struct stat st;
|
2018-09-12 09:46:36 +00:00
|
|
|
VIR_AUTOCLOSE fd = -1;
|
2018-04-23 15:41:14 +00:00
|
|
|
|
|
|
|
if ((fd = open(path, O_RDONLY | O_NONBLOCK)) < 0)
|
2018-09-12 09:46:36 +00:00
|
|
|
return -1;
|
2018-04-23 15:41:14 +00:00
|
|
|
|
|
|
|
if (fstat(fd, &st) < 0)
|
2018-09-12 09:46:36 +00:00
|
|
|
return -1;
|
2018-04-23 15:41:14 +00:00
|
|
|
|
2018-09-12 09:46:36 +00:00
|
|
|
if (!S_ISBLK(st.st_mode))
|
|
|
|
return 0;
|
2018-04-23 15:41:14 +00:00
|
|
|
|
|
|
|
/* Attempt to detect via a CDROM specific ioctl */
|
|
|
|
if (ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT) >= 0)
|
2018-09-12 09:46:36 +00:00
|
|
|
return 1;
|
2018-04-23 15:41:14 +00:00
|
|
|
|
2018-09-12 09:46:36 +00:00
|
|
|
return 0;
|
2018-04-23 15:41:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
int
|
|
|
|
virFileIsCDROM(const char *path)
|
|
|
|
{
|
2018-05-07 15:12:26 +00:00
|
|
|
if (STRPREFIX(path, "/dev/cd") ||
|
|
|
|
STRPREFIX(path, "/dev/acd"))
|
2018-04-23 15:41:14 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* defined(__linux__) */
|
|
|
|
|
|
|
|
|
2020-09-01 11:27:44 +00:00
|
|
|
#if defined WITH_MNTENT_H && defined WITH_GETMNTENT_R
|
2013-11-27 15:19:49 +00:00
|
|
|
static int
|
|
|
|
virFileGetMountSubtreeImpl(const char *mtabpath,
|
|
|
|
const char *prefix,
|
|
|
|
char ***mountsret,
|
|
|
|
size_t *nmountsret,
|
|
|
|
bool reverse)
|
|
|
|
{
|
|
|
|
FILE *procmnt;
|
|
|
|
struct mntent mntent;
|
|
|
|
char mntbuf[1024];
|
|
|
|
int ret = -1;
|
|
|
|
char **mounts = NULL;
|
|
|
|
size_t nmounts = 0;
|
|
|
|
|
|
|
|
VIR_DEBUG("prefix=%s", prefix);
|
|
|
|
|
|
|
|
*mountsret = NULL;
|
|
|
|
*nmountsret = 0;
|
|
|
|
|
|
|
|
if (!(procmnt = setmntent(mtabpath, "r"))) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Failed to read %s"), mtabpath);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (getmntent_r(procmnt, &mntent, mntbuf, sizeof(mntbuf)) != NULL) {
|
2013-11-27 15:21:41 +00:00
|
|
|
if (!(STREQ(mntent.mnt_dir, prefix) ||
|
|
|
|
(STRPREFIX(mntent.mnt_dir, prefix) &&
|
|
|
|
mntent.mnt_dir[strlen(prefix)] == '/')))
|
2013-11-27 15:19:49 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (VIR_EXPAND_N(mounts, nmounts, nmounts ? 1 : 2) < 0)
|
|
|
|
goto cleanup;
|
2019-10-20 11:49:46 +00:00
|
|
|
mounts[nmounts - 2] = g_strdup(mntent.mnt_dir);
|
2013-11-27 15:19:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mounts)
|
|
|
|
qsort(mounts, nmounts - 1, sizeof(mounts[0]),
|
|
|
|
reverse ? virStringSortRevCompare : virStringSortCompare);
|
|
|
|
|
|
|
|
*mountsret = mounts;
|
|
|
|
*nmountsret = nmounts ? nmounts - 1 : 0;
|
|
|
|
ret = 0;
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
cleanup:
|
2013-11-27 15:19:49 +00:00
|
|
|
if (ret < 0)
|
2020-08-02 17:36:03 +00:00
|
|
|
g_strfreev(mounts);
|
2013-11-27 15:19:49 +00:00
|
|
|
endmntent(procmnt);
|
|
|
|
return ret;
|
|
|
|
}
|
2020-09-01 11:27:44 +00:00
|
|
|
#else /* ! defined WITH_MNTENT_H && defined WITH_GETMNTENT_R */
|
2013-11-27 15:19:49 +00:00
|
|
|
static int
|
2019-10-14 12:45:33 +00:00
|
|
|
virFileGetMountSubtreeImpl(const char *mtabpath G_GNUC_UNUSED,
|
|
|
|
const char *prefix G_GNUC_UNUSED,
|
|
|
|
char ***mountsret G_GNUC_UNUSED,
|
|
|
|
size_t *nmountsret G_GNUC_UNUSED,
|
|
|
|
bool reverse G_GNUC_UNUSED)
|
2013-11-27 15:19:49 +00:00
|
|
|
{
|
|
|
|
virReportSystemError(ENOSYS, "%s",
|
|
|
|
_("Unable to determine mount table on this platform"));
|
|
|
|
return -1;
|
|
|
|
}
|
2020-09-01 11:27:44 +00:00
|
|
|
#endif /* ! defined WITH_MNTENT_H && defined WITH_GETMNTENT_R */
|
2013-11-27 15:19:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* virFileGetMountSubtree:
|
|
|
|
* @mtabpath: mount file to parser (eg /proc/mounts)
|
|
|
|
* @prefix: mount path prefix to match
|
|
|
|
* @mountsret: allocated and filled with matching mounts
|
|
|
|
* @nmountsret: filled with number of matching mounts, not counting NULL terminator
|
|
|
|
*
|
|
|
|
* Return the list of mounts from @mtabpath which contain
|
2017-01-05 15:19:08 +00:00
|
|
|
* the path @prefix, sorted alphabetically.
|
2013-11-27 15:19:49 +00:00
|
|
|
*
|
|
|
|
* The @mountsret array will be NULL terminated and should
|
2020-08-02 22:01:11 +00:00
|
|
|
* be freed with g_strfreev
|
2013-11-27 15:19:49 +00:00
|
|
|
*
|
|
|
|
* Returns 0 on success, -1 on error
|
|
|
|
*/
|
|
|
|
int virFileGetMountSubtree(const char *mtabpath,
|
|
|
|
const char *prefix,
|
|
|
|
char ***mountsret,
|
|
|
|
size_t *nmountsret)
|
|
|
|
{
|
|
|
|
return virFileGetMountSubtreeImpl(mtabpath, prefix, mountsret, nmountsret, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virFileGetMountReverseSubtree:
|
|
|
|
* @mtabpath: mount file to parser (eg /proc/mounts)
|
|
|
|
* @prefix: mount path prefix to match
|
|
|
|
* @mountsret: allocated and filled with matching mounts
|
|
|
|
* @nmountsret: filled with number of matching mounts, not counting NULL terminator
|
|
|
|
*
|
|
|
|
* Return the list of mounts from @mtabpath which contain
|
2017-01-05 15:19:08 +00:00
|
|
|
* the path @prefix, reverse-sorted alphabetically.
|
2013-11-27 15:19:49 +00:00
|
|
|
*
|
|
|
|
* The @mountsret array will be NULL terminated and should
|
2020-08-02 22:01:11 +00:00
|
|
|
* be freed with g_strfreev
|
2013-11-27 15:19:49 +00:00
|
|
|
*
|
|
|
|
* Returns 0 on success, -1 on error
|
|
|
|
*/
|
|
|
|
int virFileGetMountReverseSubtree(const char *mtabpath,
|
|
|
|
const char *prefix,
|
|
|
|
char ***mountsret,
|
|
|
|
size_t *nmountsret)
|
|
|
|
{
|
|
|
|
return virFileGetMountSubtreeImpl(mtabpath, prefix, mountsret, nmountsret, true);
|
|
|
|
}
|
|
|
|
|
2013-05-09 18:59:04 +00:00
|
|
|
#ifndef WIN32
|
|
|
|
/* Check that a file is accessible under certain
|
|
|
|
* user & gid.
|
|
|
|
* @mode can be F_OK, or a bitwise combination of R_OK, W_OK, and X_OK.
|
|
|
|
* see 'man access' for more details.
|
|
|
|
* Returns 0 on success, -1 on fail with errno set.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virFileAccessibleAs(const char *path, int mode,
|
|
|
|
uid_t uid, gid_t gid)
|
|
|
|
{
|
|
|
|
pid_t pid = 0;
|
|
|
|
int status, ret = 0;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree gid_t *groups = NULL;
|
util: make virSetUIDGID async-signal-safe
https://bugzilla.redhat.com/show_bug.cgi?id=964358
POSIX states that multi-threaded apps should not use functions
that are not async-signal-safe between fork and exec, yet we
were using getpwuid_r and initgroups. Although rare, it is
possible to hit deadlock in the child, when it tries to grab
a mutex that was already held by another thread in the parent.
I actually hit this deadlock when testing multiple domains
being started in parallel with a command hook, with the following
backtrace in the child:
Thread 1 (Thread 0x7fd56bbf2700 (LWP 3212)):
#0 __lll_lock_wait ()
at ../nptl/sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:136
#1 0x00007fd5761e7388 in _L_lock_854 () from /lib64/libpthread.so.0
#2 0x00007fd5761e7257 in __pthread_mutex_lock (mutex=0x7fd56be00360)
at pthread_mutex_lock.c:61
#3 0x00007fd56bbf9fc5 in _nss_files_getpwuid_r (uid=0, result=0x7fd56bbf0c70,
buffer=0x7fd55c2a65f0 "", buflen=1024, errnop=0x7fd56bbf25b8)
at nss_files/files-pwd.c:40
#4 0x00007fd575aeff1d in __getpwuid_r (uid=0, resbuf=0x7fd56bbf0c70,
buffer=0x7fd55c2a65f0 "", buflen=1024, result=0x7fd56bbf0cb0)
at ../nss/getXXbyYY_r.c:253
#5 0x00007fd578aebafc in virSetUIDGID (uid=0, gid=0) at util/virutil.c:1031
#6 0x00007fd578aebf43 in virSetUIDGIDWithCaps (uid=0, gid=0, capBits=0,
clearExistingCaps=true) at util/virutil.c:1388
#7 0x00007fd578a9a20b in virExec (cmd=0x7fd55c231f10) at util/vircommand.c:654
#8 0x00007fd578a9dfa2 in virCommandRunAsync (cmd=0x7fd55c231f10, pid=0x0)
at util/vircommand.c:2247
#9 0x00007fd578a9d74e in virCommandRun (cmd=0x7fd55c231f10, exitstatus=0x0)
at util/vircommand.c:2100
#10 0x00007fd56326fde5 in qemuProcessStart (conn=0x7fd53c000df0,
driver=0x7fd55c0dc4f0, vm=0x7fd54800b100, migrateFrom=0x0, stdin_fd=-1,
stdin_path=0x0, snapshot=0x0, vmop=VIR_NETDEV_VPORT_PROFILE_OP_CREATE,
flags=1) at qemu/qemu_process.c:3694
...
The solution is to split the work of getpwuid_r/initgroups into the
unsafe portions (getgrouplist, called pre-fork) and safe portions
(setgroups, called post-fork).
* src/util/virutil.h (virSetUIDGID, virSetUIDGIDWithCaps): Adjust
signature.
* src/util/virutil.c (virSetUIDGID): Add parameters.
(virSetUIDGIDWithCaps): Adjust clients.
* src/util/vircommand.c (virExec): Likewise.
* src/util/virfile.c (virFileAccessibleAs, virFileOpenForked)
(virDirCreate): Likewise.
* src/security/security_dac.c (virSecurityDACSetProcessLabel):
Likewise.
* src/lxc/lxc_container.c (lxcContainerSetID): Likewise.
* configure.ac (AC_CHECK_FUNCS_ONCE): Check for setgroups, not
initgroups.
Signed-off-by: Eric Blake <eblake@redhat.com>
2013-05-22 02:59:10 +00:00
|
|
|
int ngroups;
|
2013-05-09 18:59:04 +00:00
|
|
|
|
2013-10-09 11:13:45 +00:00
|
|
|
if (uid == geteuid() &&
|
|
|
|
gid == getegid())
|
2013-05-09 18:59:04 +00:00
|
|
|
return access(path, mode);
|
|
|
|
|
util: make virSetUIDGID async-signal-safe
https://bugzilla.redhat.com/show_bug.cgi?id=964358
POSIX states that multi-threaded apps should not use functions
that are not async-signal-safe between fork and exec, yet we
were using getpwuid_r and initgroups. Although rare, it is
possible to hit deadlock in the child, when it tries to grab
a mutex that was already held by another thread in the parent.
I actually hit this deadlock when testing multiple domains
being started in parallel with a command hook, with the following
backtrace in the child:
Thread 1 (Thread 0x7fd56bbf2700 (LWP 3212)):
#0 __lll_lock_wait ()
at ../nptl/sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:136
#1 0x00007fd5761e7388 in _L_lock_854 () from /lib64/libpthread.so.0
#2 0x00007fd5761e7257 in __pthread_mutex_lock (mutex=0x7fd56be00360)
at pthread_mutex_lock.c:61
#3 0x00007fd56bbf9fc5 in _nss_files_getpwuid_r (uid=0, result=0x7fd56bbf0c70,
buffer=0x7fd55c2a65f0 "", buflen=1024, errnop=0x7fd56bbf25b8)
at nss_files/files-pwd.c:40
#4 0x00007fd575aeff1d in __getpwuid_r (uid=0, resbuf=0x7fd56bbf0c70,
buffer=0x7fd55c2a65f0 "", buflen=1024, result=0x7fd56bbf0cb0)
at ../nss/getXXbyYY_r.c:253
#5 0x00007fd578aebafc in virSetUIDGID (uid=0, gid=0) at util/virutil.c:1031
#6 0x00007fd578aebf43 in virSetUIDGIDWithCaps (uid=0, gid=0, capBits=0,
clearExistingCaps=true) at util/virutil.c:1388
#7 0x00007fd578a9a20b in virExec (cmd=0x7fd55c231f10) at util/vircommand.c:654
#8 0x00007fd578a9dfa2 in virCommandRunAsync (cmd=0x7fd55c231f10, pid=0x0)
at util/vircommand.c:2247
#9 0x00007fd578a9d74e in virCommandRun (cmd=0x7fd55c231f10, exitstatus=0x0)
at util/vircommand.c:2100
#10 0x00007fd56326fde5 in qemuProcessStart (conn=0x7fd53c000df0,
driver=0x7fd55c0dc4f0, vm=0x7fd54800b100, migrateFrom=0x0, stdin_fd=-1,
stdin_path=0x0, snapshot=0x0, vmop=VIR_NETDEV_VPORT_PROFILE_OP_CREATE,
flags=1) at qemu/qemu_process.c:3694
...
The solution is to split the work of getpwuid_r/initgroups into the
unsafe portions (getgrouplist, called pre-fork) and safe portions
(setgroups, called post-fork).
* src/util/virutil.h (virSetUIDGID, virSetUIDGIDWithCaps): Adjust
signature.
* src/util/virutil.c (virSetUIDGID): Add parameters.
(virSetUIDGIDWithCaps): Adjust clients.
* src/util/vircommand.c (virExec): Likewise.
* src/util/virfile.c (virFileAccessibleAs, virFileOpenForked)
(virDirCreate): Likewise.
* src/security/security_dac.c (virSecurityDACSetProcessLabel):
Likewise.
* src/lxc/lxc_container.c (lxcContainerSetID): Likewise.
* configure.ac (AC_CHECK_FUNCS_ONCE): Check for setgroups, not
initgroups.
Signed-off-by: Eric Blake <eblake@redhat.com>
2013-05-22 02:59:10 +00:00
|
|
|
ngroups = virGetGroupList(uid, gid, &groups);
|
|
|
|
if (ngroups < 0)
|
|
|
|
return -1;
|
|
|
|
|
virFork: simplify semantics
The old semantics of virFork() violates the priciple of good
usability: it requires the caller to check the pid argument
after use, *even when virFork returned -1*, in order to properly
abort a child process that failed setup done immediately after
fork() - that is, the caller must call _exit() in the child.
While uses in virfile.c did this correctly, uses in 'virsh
lxc-enter-namespace' and 'virt-login-shell' would happily return
from the calling function in both the child and the parent,
leading to very confusing results. [Thankfully, I found the
problem by inspection, and can't actually trigger the double
return on error without an LD_PRELOAD library.]
It is much better if the semantics of virFork are impossible
to abuse. Looking at virFork(), the parent could only ever
return -1 with a non-negative pid if it misused pthread_sigmask,
but this never happens. Up until this patch series, the child
could return -1 with non-negative pid if it fails to set up
signals correctly, but we recently fixed that to make the child
call _exit() at that point instead of forcing the caller to do
it. Thus, the return value and contents of the pid argument are
now redundant (a -1 return now happens only for failure to fork,
a child 0 return only happens for a successful 0 pid, and a
parent 0 return only happens for a successful non-zero pid),
so we might as well return the pid directly rather than an
integer of whether it succeeded or failed; this is also good
from the interface design perspective as users are already
familiar with fork() semantics.
One last change in this patch: before returning the pid directly,
I found cases where using virProcessWait unconditionally on a
cleanup path of a virFork's -1 pid return would be nicer if there
were a way to avoid it overwriting an earlier message. While
such paths are a bit harder to come by with my change to a direct
pid return, I decided to keep the virProcessWait change in this
patch.
* src/util/vircommand.h (virFork): Change signature.
* src/util/vircommand.c (virFork): Guarantee that child will only
return on success, to simplify callers. Return pid rather than
status, now that the situations are always the same.
(virExec): Adjust caller, also avoid open-coding process death.
* src/util/virprocess.c (virProcessWait): Tweak semantics when pid
is -1.
(virProcessRunInMountNamespace): Adjust caller.
* src/util/virfile.c (virFileAccessibleAs, virFileOpenForked)
(virDirCreate): Likewise.
* tools/virt-login-shell.c (main): Likewise.
* tools/virsh-domain.c (cmdLxcEnterNamespace): Likewise.
* tests/commandtest.c (test23): Likewise.
Signed-off-by: Eric Blake <eblake@redhat.com>
2013-12-22 00:54:33 +00:00
|
|
|
pid = virFork();
|
2013-05-09 18:59:04 +00:00
|
|
|
|
2018-07-13 17:54:48 +00:00
|
|
|
if (pid < 0)
|
2013-05-09 18:59:04 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (pid) { /* parent */
|
2014-02-20 03:23:44 +00:00
|
|
|
if (virProcessWait(pid, &status, false) < 0) {
|
|
|
|
/* virProcessWait() already reported error */
|
virFork: simplify semantics
The old semantics of virFork() violates the priciple of good
usability: it requires the caller to check the pid argument
after use, *even when virFork returned -1*, in order to properly
abort a child process that failed setup done immediately after
fork() - that is, the caller must call _exit() in the child.
While uses in virfile.c did this correctly, uses in 'virsh
lxc-enter-namespace' and 'virt-login-shell' would happily return
from the calling function in both the child and the parent,
leading to very confusing results. [Thankfully, I found the
problem by inspection, and can't actually trigger the double
return on error without an LD_PRELOAD library.]
It is much better if the semantics of virFork are impossible
to abuse. Looking at virFork(), the parent could only ever
return -1 with a non-negative pid if it misused pthread_sigmask,
but this never happens. Up until this patch series, the child
could return -1 with non-negative pid if it fails to set up
signals correctly, but we recently fixed that to make the child
call _exit() at that point instead of forcing the caller to do
it. Thus, the return value and contents of the pid argument are
now redundant (a -1 return now happens only for failure to fork,
a child 0 return only happens for a successful 0 pid, and a
parent 0 return only happens for a successful non-zero pid),
so we might as well return the pid directly rather than an
integer of whether it succeeded or failed; this is also good
from the interface design perspective as users are already
familiar with fork() semantics.
One last change in this patch: before returning the pid directly,
I found cases where using virProcessWait unconditionally on a
cleanup path of a virFork's -1 pid return would be nicer if there
were a way to avoid it overwriting an earlier message. While
such paths are a bit harder to come by with my change to a direct
pid return, I decided to keep the virProcessWait change in this
patch.
* src/util/vircommand.h (virFork): Change signature.
* src/util/vircommand.c (virFork): Guarantee that child will only
return on success, to simplify callers. Return pid rather than
status, now that the situations are always the same.
(virExec): Adjust caller, also avoid open-coding process death.
* src/util/virprocess.c (virProcessWait): Tweak semantics when pid
is -1.
(virProcessRunInMountNamespace): Adjust caller.
* src/util/virfile.c (virFileAccessibleAs, virFileOpenForked)
(virDirCreate): Likewise.
* tools/virt-login-shell.c (main): Likewise.
* tools/virsh-domain.c (cmdLxcEnterNamespace): Likewise.
* tests/commandtest.c (test23): Likewise.
Signed-off-by: Eric Blake <eblake@redhat.com>
2013-12-22 00:54:33 +00:00
|
|
|
errno = EINTR;
|
2013-05-09 18:59:04 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status) {
|
2014-02-20 03:23:44 +00:00
|
|
|
errno = status;
|
2013-05-09 18:59:04 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
util: make virSetUIDGID async-signal-safe
https://bugzilla.redhat.com/show_bug.cgi?id=964358
POSIX states that multi-threaded apps should not use functions
that are not async-signal-safe between fork and exec, yet we
were using getpwuid_r and initgroups. Although rare, it is
possible to hit deadlock in the child, when it tries to grab
a mutex that was already held by another thread in the parent.
I actually hit this deadlock when testing multiple domains
being started in parallel with a command hook, with the following
backtrace in the child:
Thread 1 (Thread 0x7fd56bbf2700 (LWP 3212)):
#0 __lll_lock_wait ()
at ../nptl/sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:136
#1 0x00007fd5761e7388 in _L_lock_854 () from /lib64/libpthread.so.0
#2 0x00007fd5761e7257 in __pthread_mutex_lock (mutex=0x7fd56be00360)
at pthread_mutex_lock.c:61
#3 0x00007fd56bbf9fc5 in _nss_files_getpwuid_r (uid=0, result=0x7fd56bbf0c70,
buffer=0x7fd55c2a65f0 "", buflen=1024, errnop=0x7fd56bbf25b8)
at nss_files/files-pwd.c:40
#4 0x00007fd575aeff1d in __getpwuid_r (uid=0, resbuf=0x7fd56bbf0c70,
buffer=0x7fd55c2a65f0 "", buflen=1024, result=0x7fd56bbf0cb0)
at ../nss/getXXbyYY_r.c:253
#5 0x00007fd578aebafc in virSetUIDGID (uid=0, gid=0) at util/virutil.c:1031
#6 0x00007fd578aebf43 in virSetUIDGIDWithCaps (uid=0, gid=0, capBits=0,
clearExistingCaps=true) at util/virutil.c:1388
#7 0x00007fd578a9a20b in virExec (cmd=0x7fd55c231f10) at util/vircommand.c:654
#8 0x00007fd578a9dfa2 in virCommandRunAsync (cmd=0x7fd55c231f10, pid=0x0)
at util/vircommand.c:2247
#9 0x00007fd578a9d74e in virCommandRun (cmd=0x7fd55c231f10, exitstatus=0x0)
at util/vircommand.c:2100
#10 0x00007fd56326fde5 in qemuProcessStart (conn=0x7fd53c000df0,
driver=0x7fd55c0dc4f0, vm=0x7fd54800b100, migrateFrom=0x0, stdin_fd=-1,
stdin_path=0x0, snapshot=0x0, vmop=VIR_NETDEV_VPORT_PROFILE_OP_CREATE,
flags=1) at qemu/qemu_process.c:3694
...
The solution is to split the work of getpwuid_r/initgroups into the
unsafe portions (getgrouplist, called pre-fork) and safe portions
(setgroups, called post-fork).
* src/util/virutil.h (virSetUIDGID, virSetUIDGIDWithCaps): Adjust
signature.
* src/util/virutil.c (virSetUIDGID): Add parameters.
(virSetUIDGIDWithCaps): Adjust clients.
* src/util/vircommand.c (virExec): Likewise.
* src/util/virfile.c (virFileAccessibleAs, virFileOpenForked)
(virDirCreate): Likewise.
* src/security/security_dac.c (virSecurityDACSetProcessLabel):
Likewise.
* src/lxc/lxc_container.c (lxcContainerSetID): Likewise.
* configure.ac (AC_CHECK_FUNCS_ONCE): Check for setgroups, not
initgroups.
Signed-off-by: Eric Blake <eblake@redhat.com>
2013-05-22 02:59:10 +00:00
|
|
|
if (virSetUIDGID(uid, gid, groups, ngroups) < 0) {
|
2013-05-09 18:59:04 +00:00
|
|
|
ret = errno;
|
|
|
|
goto childerror;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (access(path, mode) < 0)
|
|
|
|
ret = errno;
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
childerror:
|
2013-05-09 18:59:04 +00:00
|
|
|
if ((ret & 0xFF) != ret) {
|
|
|
|
VIR_WARN("unable to pass desired return value %d", ret);
|
|
|
|
ret = 0xFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
_exit(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* virFileOpenForceOwnerMode() - an internal utility function called
|
|
|
|
* only by virFileOpenAs(). Sets the owner and mode of the file
|
|
|
|
* opened as "fd" if it's not correct AND the flags say it should be
|
|
|
|
* forced. */
|
|
|
|
static int
|
|
|
|
virFileOpenForceOwnerMode(const char *path, int fd, mode_t mode,
|
|
|
|
uid_t uid, gid_t gid, unsigned int flags)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (!(flags & (VIR_FILE_OPEN_FORCE_OWNER | VIR_FILE_OPEN_FORCE_MODE)))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (fstat(fd, &st) == -1) {
|
|
|
|
ret = -errno;
|
|
|
|
virReportSystemError(errno, _("stat of '%s' failed"), path);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
/* NB: uid:gid are never "-1" (default) at this point - the caller
|
|
|
|
* has always changed -1 to the value of get[gu]id().
|
|
|
|
*/
|
|
|
|
if ((flags & VIR_FILE_OPEN_FORCE_OWNER) &&
|
|
|
|
((st.st_uid != uid) || (st.st_gid != gid)) &&
|
|
|
|
(fchown(fd, uid, gid) < 0)) {
|
|
|
|
ret = -errno;
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("cannot chown '%s' to (%u, %u)"),
|
|
|
|
path, (unsigned int) uid,
|
|
|
|
(unsigned int) gid);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
if ((flags & VIR_FILE_OPEN_FORCE_MODE) &&
|
|
|
|
((mode & (S_IRWXU|S_IRWXG|S_IRWXO)) !=
|
|
|
|
(st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO))) &&
|
|
|
|
(fchmod(fd, mode) < 0)) {
|
|
|
|
ret = -errno;
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("cannot set mode of '%s' to %04o"),
|
|
|
|
path, mode);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* virFileOpenForked() - an internal utility function called only by
|
|
|
|
* virFileOpenAs(). It forks, then the child does setuid+setgid to
|
|
|
|
* given uid:gid and attempts to open the file, while the parent just
|
|
|
|
* calls recvfd to get the open fd back from the child. returns the
|
2015-10-08 12:39:51 +00:00
|
|
|
* fd, or -errno if there is an error. Additionally, to avoid another
|
|
|
|
* round-trip to unlink the file in a forked process; on error if this
|
|
|
|
* function created the file, but failed to perform some action after
|
|
|
|
* creation, then perform the unlink of the file. The storage driver
|
|
|
|
* buildVol backend function expects the file to be deleted on error.
|
|
|
|
*/
|
2013-05-09 18:59:04 +00:00
|
|
|
static int
|
|
|
|
virFileOpenForked(const char *path, int openflags, mode_t mode,
|
|
|
|
uid_t uid, gid_t gid, unsigned int flags)
|
|
|
|
{
|
|
|
|
pid_t pid;
|
2015-09-08 22:09:53 +00:00
|
|
|
int status = 0, ret = 0;
|
virfile: Adjust error path for virFileOpenForked
Rather than have a dummy waitpid loop and return of the failure status
from recvfd, adjust the logic to save the recvfd error & fd and then
in priority order:
- if waitpid failed, use that errno value
- waitpid succeeded, but if the child exited abnormally, report failure
(use EACCES to report as return failure, since either EACCES or EPERM is
what caused us to fall into the fork+setuid path)
- waitpid succeeded, but if the child reported non-zero status, report
failure (use the errno value that the child encoded into exit status)
- waitpid succeeded, but if recvfd failed, report recvfd_errno
- waitpid and recvfd succeeded, use the fd
NOTE: Original logic to retry the open and force owner mode was
"documented" as only being attempted if we had already tried opening
with the fork+setuid, but checked flags vs. VIR_FILE_OPEN_NOFORK which
is counter to how we would get to that point. So that code was removed.
2015-01-28 17:34:54 +00:00
|
|
|
int recvfd_errno = 0;
|
2013-05-09 18:59:04 +00:00
|
|
|
int fd = -1;
|
|
|
|
int pair[2] = { -1, -1 };
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree gid_t *groups = NULL;
|
util: make virSetUIDGID async-signal-safe
https://bugzilla.redhat.com/show_bug.cgi?id=964358
POSIX states that multi-threaded apps should not use functions
that are not async-signal-safe between fork and exec, yet we
were using getpwuid_r and initgroups. Although rare, it is
possible to hit deadlock in the child, when it tries to grab
a mutex that was already held by another thread in the parent.
I actually hit this deadlock when testing multiple domains
being started in parallel with a command hook, with the following
backtrace in the child:
Thread 1 (Thread 0x7fd56bbf2700 (LWP 3212)):
#0 __lll_lock_wait ()
at ../nptl/sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:136
#1 0x00007fd5761e7388 in _L_lock_854 () from /lib64/libpthread.so.0
#2 0x00007fd5761e7257 in __pthread_mutex_lock (mutex=0x7fd56be00360)
at pthread_mutex_lock.c:61
#3 0x00007fd56bbf9fc5 in _nss_files_getpwuid_r (uid=0, result=0x7fd56bbf0c70,
buffer=0x7fd55c2a65f0 "", buflen=1024, errnop=0x7fd56bbf25b8)
at nss_files/files-pwd.c:40
#4 0x00007fd575aeff1d in __getpwuid_r (uid=0, resbuf=0x7fd56bbf0c70,
buffer=0x7fd55c2a65f0 "", buflen=1024, result=0x7fd56bbf0cb0)
at ../nss/getXXbyYY_r.c:253
#5 0x00007fd578aebafc in virSetUIDGID (uid=0, gid=0) at util/virutil.c:1031
#6 0x00007fd578aebf43 in virSetUIDGIDWithCaps (uid=0, gid=0, capBits=0,
clearExistingCaps=true) at util/virutil.c:1388
#7 0x00007fd578a9a20b in virExec (cmd=0x7fd55c231f10) at util/vircommand.c:654
#8 0x00007fd578a9dfa2 in virCommandRunAsync (cmd=0x7fd55c231f10, pid=0x0)
at util/vircommand.c:2247
#9 0x00007fd578a9d74e in virCommandRun (cmd=0x7fd55c231f10, exitstatus=0x0)
at util/vircommand.c:2100
#10 0x00007fd56326fde5 in qemuProcessStart (conn=0x7fd53c000df0,
driver=0x7fd55c0dc4f0, vm=0x7fd54800b100, migrateFrom=0x0, stdin_fd=-1,
stdin_path=0x0, snapshot=0x0, vmop=VIR_NETDEV_VPORT_PROFILE_OP_CREATE,
flags=1) at qemu/qemu_process.c:3694
...
The solution is to split the work of getpwuid_r/initgroups into the
unsafe portions (getgrouplist, called pre-fork) and safe portions
(setgroups, called post-fork).
* src/util/virutil.h (virSetUIDGID, virSetUIDGIDWithCaps): Adjust
signature.
* src/util/virutil.c (virSetUIDGID): Add parameters.
(virSetUIDGIDWithCaps): Adjust clients.
* src/util/vircommand.c (virExec): Likewise.
* src/util/virfile.c (virFileAccessibleAs, virFileOpenForked)
(virDirCreate): Likewise.
* src/security/security_dac.c (virSecurityDACSetProcessLabel):
Likewise.
* src/lxc/lxc_container.c (lxcContainerSetID): Likewise.
* configure.ac (AC_CHECK_FUNCS_ONCE): Check for setgroups, not
initgroups.
Signed-off-by: Eric Blake <eblake@redhat.com>
2013-05-22 02:59:10 +00:00
|
|
|
int ngroups;
|
2015-10-08 12:39:51 +00:00
|
|
|
bool created = false;
|
2013-05-09 18:59:04 +00:00
|
|
|
|
|
|
|
/* parent is running as root, but caller requested that the
|
|
|
|
* file be opened as some other user and/or group). The
|
|
|
|
* following dance avoids problems caused by root-squashing
|
|
|
|
* NFS servers. */
|
|
|
|
|
util: make virSetUIDGID async-signal-safe
https://bugzilla.redhat.com/show_bug.cgi?id=964358
POSIX states that multi-threaded apps should not use functions
that are not async-signal-safe between fork and exec, yet we
were using getpwuid_r and initgroups. Although rare, it is
possible to hit deadlock in the child, when it tries to grab
a mutex that was already held by another thread in the parent.
I actually hit this deadlock when testing multiple domains
being started in parallel with a command hook, with the following
backtrace in the child:
Thread 1 (Thread 0x7fd56bbf2700 (LWP 3212)):
#0 __lll_lock_wait ()
at ../nptl/sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:136
#1 0x00007fd5761e7388 in _L_lock_854 () from /lib64/libpthread.so.0
#2 0x00007fd5761e7257 in __pthread_mutex_lock (mutex=0x7fd56be00360)
at pthread_mutex_lock.c:61
#3 0x00007fd56bbf9fc5 in _nss_files_getpwuid_r (uid=0, result=0x7fd56bbf0c70,
buffer=0x7fd55c2a65f0 "", buflen=1024, errnop=0x7fd56bbf25b8)
at nss_files/files-pwd.c:40
#4 0x00007fd575aeff1d in __getpwuid_r (uid=0, resbuf=0x7fd56bbf0c70,
buffer=0x7fd55c2a65f0 "", buflen=1024, result=0x7fd56bbf0cb0)
at ../nss/getXXbyYY_r.c:253
#5 0x00007fd578aebafc in virSetUIDGID (uid=0, gid=0) at util/virutil.c:1031
#6 0x00007fd578aebf43 in virSetUIDGIDWithCaps (uid=0, gid=0, capBits=0,
clearExistingCaps=true) at util/virutil.c:1388
#7 0x00007fd578a9a20b in virExec (cmd=0x7fd55c231f10) at util/vircommand.c:654
#8 0x00007fd578a9dfa2 in virCommandRunAsync (cmd=0x7fd55c231f10, pid=0x0)
at util/vircommand.c:2247
#9 0x00007fd578a9d74e in virCommandRun (cmd=0x7fd55c231f10, exitstatus=0x0)
at util/vircommand.c:2100
#10 0x00007fd56326fde5 in qemuProcessStart (conn=0x7fd53c000df0,
driver=0x7fd55c0dc4f0, vm=0x7fd54800b100, migrateFrom=0x0, stdin_fd=-1,
stdin_path=0x0, snapshot=0x0, vmop=VIR_NETDEV_VPORT_PROFILE_OP_CREATE,
flags=1) at qemu/qemu_process.c:3694
...
The solution is to split the work of getpwuid_r/initgroups into the
unsafe portions (getgrouplist, called pre-fork) and safe portions
(setgroups, called post-fork).
* src/util/virutil.h (virSetUIDGID, virSetUIDGIDWithCaps): Adjust
signature.
* src/util/virutil.c (virSetUIDGID): Add parameters.
(virSetUIDGIDWithCaps): Adjust clients.
* src/util/vircommand.c (virExec): Likewise.
* src/util/virfile.c (virFileAccessibleAs, virFileOpenForked)
(virDirCreate): Likewise.
* src/security/security_dac.c (virSecurityDACSetProcessLabel):
Likewise.
* src/lxc/lxc_container.c (lxcContainerSetID): Likewise.
* configure.ac (AC_CHECK_FUNCS_ONCE): Check for setgroups, not
initgroups.
Signed-off-by: Eric Blake <eblake@redhat.com>
2013-05-22 02:59:10 +00:00
|
|
|
ngroups = virGetGroupList(uid, gid, &groups);
|
|
|
|
if (ngroups < 0)
|
|
|
|
return -errno;
|
|
|
|
|
2013-05-09 18:59:04 +00:00
|
|
|
if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) < 0) {
|
|
|
|
ret = -errno;
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("failed to create socket needed for '%s'"),
|
|
|
|
path);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
virFork: simplify semantics
The old semantics of virFork() violates the priciple of good
usability: it requires the caller to check the pid argument
after use, *even when virFork returned -1*, in order to properly
abort a child process that failed setup done immediately after
fork() - that is, the caller must call _exit() in the child.
While uses in virfile.c did this correctly, uses in 'virsh
lxc-enter-namespace' and 'virt-login-shell' would happily return
from the calling function in both the child and the parent,
leading to very confusing results. [Thankfully, I found the
problem by inspection, and can't actually trigger the double
return on error without an LD_PRELOAD library.]
It is much better if the semantics of virFork are impossible
to abuse. Looking at virFork(), the parent could only ever
return -1 with a non-negative pid if it misused pthread_sigmask,
but this never happens. Up until this patch series, the child
could return -1 with non-negative pid if it fails to set up
signals correctly, but we recently fixed that to make the child
call _exit() at that point instead of forcing the caller to do
it. Thus, the return value and contents of the pid argument are
now redundant (a -1 return now happens only for failure to fork,
a child 0 return only happens for a successful 0 pid, and a
parent 0 return only happens for a successful non-zero pid),
so we might as well return the pid directly rather than an
integer of whether it succeeded or failed; this is also good
from the interface design perspective as users are already
familiar with fork() semantics.
One last change in this patch: before returning the pid directly,
I found cases where using virProcessWait unconditionally on a
cleanup path of a virFork's -1 pid return would be nicer if there
were a way to avoid it overwriting an earlier message. While
such paths are a bit harder to come by with my change to a direct
pid return, I decided to keep the virProcessWait change in this
patch.
* src/util/vircommand.h (virFork): Change signature.
* src/util/vircommand.c (virFork): Guarantee that child will only
return on success, to simplify callers. Return pid rather than
status, now that the situations are always the same.
(virExec): Adjust caller, also avoid open-coding process death.
* src/util/virprocess.c (virProcessWait): Tweak semantics when pid
is -1.
(virProcessRunInMountNamespace): Adjust caller.
* src/util/virfile.c (virFileAccessibleAs, virFileOpenForked)
(virDirCreate): Likewise.
* tools/virt-login-shell.c (main): Likewise.
* tools/virsh-domain.c (cmdLxcEnterNamespace): Likewise.
* tests/commandtest.c (test23): Likewise.
Signed-off-by: Eric Blake <eblake@redhat.com>
2013-12-22 00:54:33 +00:00
|
|
|
pid = virFork();
|
2018-07-13 17:54:48 +00:00
|
|
|
if (pid < 0)
|
|
|
|
return -errno;
|
2013-05-09 18:59:04 +00:00
|
|
|
|
|
|
|
if (pid == 0) {
|
|
|
|
|
|
|
|
/* child */
|
|
|
|
|
|
|
|
/* set desired uid/gid, then attempt to create the file */
|
virFork: simplify semantics
The old semantics of virFork() violates the priciple of good
usability: it requires the caller to check the pid argument
after use, *even when virFork returned -1*, in order to properly
abort a child process that failed setup done immediately after
fork() - that is, the caller must call _exit() in the child.
While uses in virfile.c did this correctly, uses in 'virsh
lxc-enter-namespace' and 'virt-login-shell' would happily return
from the calling function in both the child and the parent,
leading to very confusing results. [Thankfully, I found the
problem by inspection, and can't actually trigger the double
return on error without an LD_PRELOAD library.]
It is much better if the semantics of virFork are impossible
to abuse. Looking at virFork(), the parent could only ever
return -1 with a non-negative pid if it misused pthread_sigmask,
but this never happens. Up until this patch series, the child
could return -1 with non-negative pid if it fails to set up
signals correctly, but we recently fixed that to make the child
call _exit() at that point instead of forcing the caller to do
it. Thus, the return value and contents of the pid argument are
now redundant (a -1 return now happens only for failure to fork,
a child 0 return only happens for a successful 0 pid, and a
parent 0 return only happens for a successful non-zero pid),
so we might as well return the pid directly rather than an
integer of whether it succeeded or failed; this is also good
from the interface design perspective as users are already
familiar with fork() semantics.
One last change in this patch: before returning the pid directly,
I found cases where using virProcessWait unconditionally on a
cleanup path of a virFork's -1 pid return would be nicer if there
were a way to avoid it overwriting an earlier message. While
such paths are a bit harder to come by with my change to a direct
pid return, I decided to keep the virProcessWait change in this
patch.
* src/util/vircommand.h (virFork): Change signature.
* src/util/vircommand.c (virFork): Guarantee that child will only
return on success, to simplify callers. Return pid rather than
status, now that the situations are always the same.
(virExec): Adjust caller, also avoid open-coding process death.
* src/util/virprocess.c (virProcessWait): Tweak semantics when pid
is -1.
(virProcessRunInMountNamespace): Adjust caller.
* src/util/virfile.c (virFileAccessibleAs, virFileOpenForked)
(virDirCreate): Likewise.
* tools/virt-login-shell.c (main): Likewise.
* tools/virsh-domain.c (cmdLxcEnterNamespace): Likewise.
* tests/commandtest.c (test23): Likewise.
Signed-off-by: Eric Blake <eblake@redhat.com>
2013-12-22 00:54:33 +00:00
|
|
|
VIR_FORCE_CLOSE(pair[0]);
|
util: make virSetUIDGID async-signal-safe
https://bugzilla.redhat.com/show_bug.cgi?id=964358
POSIX states that multi-threaded apps should not use functions
that are not async-signal-safe between fork and exec, yet we
were using getpwuid_r and initgroups. Although rare, it is
possible to hit deadlock in the child, when it tries to grab
a mutex that was already held by another thread in the parent.
I actually hit this deadlock when testing multiple domains
being started in parallel with a command hook, with the following
backtrace in the child:
Thread 1 (Thread 0x7fd56bbf2700 (LWP 3212)):
#0 __lll_lock_wait ()
at ../nptl/sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:136
#1 0x00007fd5761e7388 in _L_lock_854 () from /lib64/libpthread.so.0
#2 0x00007fd5761e7257 in __pthread_mutex_lock (mutex=0x7fd56be00360)
at pthread_mutex_lock.c:61
#3 0x00007fd56bbf9fc5 in _nss_files_getpwuid_r (uid=0, result=0x7fd56bbf0c70,
buffer=0x7fd55c2a65f0 "", buflen=1024, errnop=0x7fd56bbf25b8)
at nss_files/files-pwd.c:40
#4 0x00007fd575aeff1d in __getpwuid_r (uid=0, resbuf=0x7fd56bbf0c70,
buffer=0x7fd55c2a65f0 "", buflen=1024, result=0x7fd56bbf0cb0)
at ../nss/getXXbyYY_r.c:253
#5 0x00007fd578aebafc in virSetUIDGID (uid=0, gid=0) at util/virutil.c:1031
#6 0x00007fd578aebf43 in virSetUIDGIDWithCaps (uid=0, gid=0, capBits=0,
clearExistingCaps=true) at util/virutil.c:1388
#7 0x00007fd578a9a20b in virExec (cmd=0x7fd55c231f10) at util/vircommand.c:654
#8 0x00007fd578a9dfa2 in virCommandRunAsync (cmd=0x7fd55c231f10, pid=0x0)
at util/vircommand.c:2247
#9 0x00007fd578a9d74e in virCommandRun (cmd=0x7fd55c231f10, exitstatus=0x0)
at util/vircommand.c:2100
#10 0x00007fd56326fde5 in qemuProcessStart (conn=0x7fd53c000df0,
driver=0x7fd55c0dc4f0, vm=0x7fd54800b100, migrateFrom=0x0, stdin_fd=-1,
stdin_path=0x0, snapshot=0x0, vmop=VIR_NETDEV_VPORT_PROFILE_OP_CREATE,
flags=1) at qemu/qemu_process.c:3694
...
The solution is to split the work of getpwuid_r/initgroups into the
unsafe portions (getgrouplist, called pre-fork) and safe portions
(setgroups, called post-fork).
* src/util/virutil.h (virSetUIDGID, virSetUIDGIDWithCaps): Adjust
signature.
* src/util/virutil.c (virSetUIDGID): Add parameters.
(virSetUIDGIDWithCaps): Adjust clients.
* src/util/vircommand.c (virExec): Likewise.
* src/util/virfile.c (virFileAccessibleAs, virFileOpenForked)
(virDirCreate): Likewise.
* src/security/security_dac.c (virSecurityDACSetProcessLabel):
Likewise.
* src/lxc/lxc_container.c (lxcContainerSetID): Likewise.
* configure.ac (AC_CHECK_FUNCS_ONCE): Check for setgroups, not
initgroups.
Signed-off-by: Eric Blake <eblake@redhat.com>
2013-05-22 02:59:10 +00:00
|
|
|
if (virSetUIDGID(uid, gid, groups, ngroups) < 0) {
|
2013-05-09 18:59:04 +00:00
|
|
|
ret = -errno;
|
|
|
|
goto childerror;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((fd = open(path, openflags, mode)) < 0) {
|
|
|
|
ret = -errno;
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("child process failed to create file '%s'"),
|
|
|
|
path);
|
|
|
|
goto childerror;
|
|
|
|
}
|
2015-10-08 12:39:51 +00:00
|
|
|
if (openflags & O_CREAT)
|
|
|
|
created = true;
|
2013-05-09 18:59:04 +00:00
|
|
|
|
|
|
|
/* File is successfully open. Set permissions if requested. */
|
|
|
|
ret = virFileOpenForceOwnerMode(path, fd, mode, uid, gid, flags);
|
2015-08-24 16:37:41 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
ret = -errno;
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("child process failed to force owner mode file '%s'"),
|
|
|
|
path);
|
2013-05-09 18:59:04 +00:00
|
|
|
goto childerror;
|
2015-08-24 16:37:41 +00:00
|
|
|
}
|
2013-05-09 18:59:04 +00:00
|
|
|
|
|
|
|
do {
|
2020-01-17 11:57:17 +00:00
|
|
|
ret = virSocketSendFD(pair[1], fd);
|
2013-05-09 18:59:04 +00:00
|
|
|
} while (ret < 0 && errno == EINTR);
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
ret = -errno;
|
|
|
|
virReportSystemError(errno, "%s",
|
|
|
|
_("child process failed to send fd to parent"));
|
|
|
|
goto childerror;
|
|
|
|
}
|
|
|
|
|
|
|
|
childerror:
|
|
|
|
/* ret tracks -errno on failure, but exit value must be positive.
|
|
|
|
* If the child exits with EACCES, then the parent tries again. */
|
|
|
|
/* XXX This makes assumptions about errno being < 255, which is
|
|
|
|
* not true on Hurd. */
|
|
|
|
VIR_FORCE_CLOSE(pair[1]);
|
2015-10-08 12:39:51 +00:00
|
|
|
if (ret < 0) {
|
2013-05-09 18:59:04 +00:00
|
|
|
VIR_FORCE_CLOSE(fd);
|
2015-10-08 12:39:51 +00:00
|
|
|
if (created)
|
|
|
|
unlink(path);
|
|
|
|
}
|
2013-05-09 18:59:04 +00:00
|
|
|
ret = -ret;
|
|
|
|
if ((ret & 0xff) != ret) {
|
|
|
|
VIR_WARN("unable to pass desired return value %d", ret);
|
|
|
|
ret = 0xff;
|
|
|
|
}
|
|
|
|
_exit(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* parent */
|
|
|
|
|
|
|
|
VIR_FORCE_CLOSE(pair[1]);
|
|
|
|
|
|
|
|
do {
|
2020-01-17 11:57:17 +00:00
|
|
|
fd = virSocketRecvFD(pair[0], 0);
|
2013-05-09 18:59:04 +00:00
|
|
|
} while (fd < 0 && errno == EINTR);
|
|
|
|
VIR_FORCE_CLOSE(pair[0]); /* NB: this preserves errno */
|
virfile: Adjust error path for virFileOpenForked
Rather than have a dummy waitpid loop and return of the failure status
from recvfd, adjust the logic to save the recvfd error & fd and then
in priority order:
- if waitpid failed, use that errno value
- waitpid succeeded, but if the child exited abnormally, report failure
(use EACCES to report as return failure, since either EACCES or EPERM is
what caused us to fall into the fork+setuid path)
- waitpid succeeded, but if the child reported non-zero status, report
failure (use the errno value that the child encoded into exit status)
- waitpid succeeded, but if recvfd failed, report recvfd_errno
- waitpid and recvfd succeeded, use the fd
NOTE: Original logic to retry the open and force owner mode was
"documented" as only being attempted if we had already tried opening
with the fork+setuid, but checked flags vs. VIR_FILE_OPEN_NOFORK which
is counter to how we would get to that point. So that code was removed.
2015-01-28 17:34:54 +00:00
|
|
|
if (fd < 0)
|
|
|
|
recvfd_errno = errno;
|
2013-05-09 18:59:04 +00:00
|
|
|
|
2015-09-08 22:09:53 +00:00
|
|
|
if (virProcessWait(pid, &status, 0) < 0) {
|
|
|
|
/* virProcessWait() reports errno on waitpid failure, so we'll just
|
|
|
|
* set our return status to EINTR; otherwise, set status to EACCES
|
|
|
|
* since the original failure for the fork+setuid path would have
|
|
|
|
* been EACCES or EPERM by definition.
|
|
|
|
*/
|
|
|
|
if (virLastErrorIsSystemErrno(0))
|
|
|
|
status = EINTR;
|
|
|
|
else if (!status)
|
|
|
|
status = EACCES;
|
2013-05-09 18:59:04 +00:00
|
|
|
}
|
virfile: Adjust error path for virFileOpenForked
Rather than have a dummy waitpid loop and return of the failure status
from recvfd, adjust the logic to save the recvfd error & fd and then
in priority order:
- if waitpid failed, use that errno value
- waitpid succeeded, but if the child exited abnormally, report failure
(use EACCES to report as return failure, since either EACCES or EPERM is
what caused us to fall into the fork+setuid path)
- waitpid succeeded, but if the child reported non-zero status, report
failure (use the errno value that the child encoded into exit status)
- waitpid succeeded, but if recvfd failed, report recvfd_errno
- waitpid and recvfd succeeded, use the fd
NOTE: Original logic to retry the open and force owner mode was
"documented" as only being attempted if we had already tried opening
with the fork+setuid, but checked flags vs. VIR_FILE_OPEN_NOFORK which
is counter to how we would get to that point. So that code was removed.
2015-01-28 17:34:54 +00:00
|
|
|
|
2015-09-08 22:09:53 +00:00
|
|
|
if (status) {
|
2013-05-09 18:59:04 +00:00
|
|
|
VIR_FORCE_CLOSE(fd);
|
2015-09-08 22:09:53 +00:00
|
|
|
return -status;
|
2013-05-09 18:59:04 +00:00
|
|
|
}
|
virfile: Adjust error path for virFileOpenForked
Rather than have a dummy waitpid loop and return of the failure status
from recvfd, adjust the logic to save the recvfd error & fd and then
in priority order:
- if waitpid failed, use that errno value
- waitpid succeeded, but if the child exited abnormally, report failure
(use EACCES to report as return failure, since either EACCES or EPERM is
what caused us to fall into the fork+setuid path)
- waitpid succeeded, but if the child reported non-zero status, report
failure (use the errno value that the child encoded into exit status)
- waitpid succeeded, but if recvfd failed, report recvfd_errno
- waitpid and recvfd succeeded, use the fd
NOTE: Original logic to retry the open and force owner mode was
"documented" as only being attempted if we had already tried opening
with the fork+setuid, but checked flags vs. VIR_FILE_OPEN_NOFORK which
is counter to how we would get to that point. So that code was removed.
2015-01-28 17:34:54 +00:00
|
|
|
|
|
|
|
/* if waitpid succeeded, but recvfd failed, report recvfd_errno */
|
|
|
|
if (recvfd_errno != 0) {
|
|
|
|
virReportSystemError(recvfd_errno,
|
|
|
|
_("failed recvfd for child creating '%s'"),
|
|
|
|
path);
|
|
|
|
return -recvfd_errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* otherwise, waitpid and recvfd succeeded, return the fd */
|
2013-05-09 18:59:04 +00:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virFileOpenAs:
|
|
|
|
* @path: file to open or create
|
|
|
|
* @openflags: flags to pass to open
|
|
|
|
* @mode: mode to use on creation or when forcing permissions
|
|
|
|
* @uid: uid that should own file on creation
|
|
|
|
* @gid: gid that should own file
|
|
|
|
* @flags: bit-wise or of VIR_FILE_OPEN_* flags
|
|
|
|
*
|
|
|
|
* Open @path, and return an fd to the open file. @openflags contains
|
|
|
|
* the flags normally passed to open(2), while those in @flags are
|
|
|
|
* used internally. If @flags includes VIR_FILE_OPEN_NOFORK, then try
|
|
|
|
* opening the file while executing with the current uid:gid
|
|
|
|
* (i.e. don't fork+setuid+setgid before the call to open()). If
|
|
|
|
* @flags includes VIR_FILE_OPEN_FORK, then try opening the file while
|
|
|
|
* the effective user id is @uid (by forking a child process); this
|
|
|
|
* allows one to bypass root-squashing NFS issues; NOFORK is always
|
|
|
|
* tried before FORK (the absence of both flags is treated identically
|
|
|
|
* to (VIR_FILE_OPEN_NOFORK | VIR_FILE_OPEN_FORK)). If @flags includes
|
|
|
|
* VIR_FILE_OPEN_FORCE_OWNER, then ensure that @path is owned by
|
|
|
|
* uid:gid before returning (even if it already existed with a
|
|
|
|
* different owner). If @flags includes VIR_FILE_OPEN_FORCE_MODE,
|
|
|
|
* ensure it has those permissions before returning (again, even if
|
|
|
|
* the file already existed with different permissions).
|
|
|
|
*
|
|
|
|
* The return value (if non-negative) is the file descriptor, left
|
2015-10-08 12:39:51 +00:00
|
|
|
* open. Returns -errno on failure. Additionally, to avoid another
|
|
|
|
* round-trip to unlink the file; on error if this function created the
|
|
|
|
* file, but failed to perform some action after creation, then perform
|
|
|
|
* the unlink of the file. The storage driver buildVol backend function
|
|
|
|
* expects the file to be deleted on error.
|
2013-05-09 18:59:04 +00:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
virFileOpenAs(const char *path, int openflags, mode_t mode,
|
|
|
|
uid_t uid, gid_t gid, unsigned int flags)
|
|
|
|
{
|
|
|
|
int ret = 0, fd = -1;
|
2015-10-08 12:39:51 +00:00
|
|
|
bool created = false;
|
2013-05-09 18:59:04 +00:00
|
|
|
|
|
|
|
/* allow using -1 to mean "current value" */
|
|
|
|
if (uid == (uid_t) -1)
|
2013-10-09 11:13:45 +00:00
|
|
|
uid = geteuid();
|
2013-05-09 18:59:04 +00:00
|
|
|
if (gid == (gid_t) -1)
|
2013-10-09 11:13:45 +00:00
|
|
|
gid = getegid();
|
2013-05-09 18:59:04 +00:00
|
|
|
|
|
|
|
/* treat absence of both flags as presence of both for simpler
|
|
|
|
* calling. */
|
|
|
|
if (!(flags & (VIR_FILE_OPEN_NOFORK|VIR_FILE_OPEN_FORK)))
|
|
|
|
flags |= VIR_FILE_OPEN_NOFORK|VIR_FILE_OPEN_FORK;
|
|
|
|
|
|
|
|
if ((flags & VIR_FILE_OPEN_NOFORK)
|
2013-10-09 11:13:45 +00:00
|
|
|
|| (geteuid() != 0)
|
2013-05-09 18:59:04 +00:00
|
|
|
|| ((uid == 0) && (gid == 0))) {
|
|
|
|
|
|
|
|
if ((fd = open(path, openflags, mode)) < 0) {
|
|
|
|
ret = -errno;
|
|
|
|
if (!(flags & VIR_FILE_OPEN_FORK))
|
|
|
|
goto error;
|
|
|
|
} else {
|
2015-10-08 12:39:51 +00:00
|
|
|
if (openflags & O_CREAT)
|
|
|
|
created = true;
|
2013-05-09 18:59:04 +00:00
|
|
|
ret = virFileOpenForceOwnerMode(path, fd, mode, uid, gid, flags);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we either 1) didn't try opening as current user at all, or
|
|
|
|
* 2) failed, and errno/virStorageFileIsSharedFS indicate we might
|
|
|
|
* be successful if we try as a different uid, then try doing
|
|
|
|
* fork+setuid+setgid before opening.
|
|
|
|
*/
|
|
|
|
if ((fd < 0) && (flags & VIR_FILE_OPEN_FORK)) {
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
/* An open(2) that failed due to insufficient permissions
|
|
|
|
* could return one or the other of these depending on OS
|
|
|
|
* version and circumstances. Any other errno indicates a
|
|
|
|
* problem that couldn't be remedied by fork+setuid
|
|
|
|
* anyway. */
|
|
|
|
if (ret != -EACCES && ret != -EPERM)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
/* On Linux we can also verify the FS-type of the
|
|
|
|
* directory. (this is a NOP on other platforms). */
|
2014-03-29 20:15:33 +00:00
|
|
|
if (virFileIsSharedFS(path) <= 0)
|
2013-05-09 18:59:04 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* passed all prerequisites - retry the open w/fork+setuid */
|
|
|
|
if ((fd = virFileOpenForked(path, openflags, mode, uid, gid, flags)) < 0) {
|
|
|
|
ret = fd;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* File is successfully opened */
|
|
|
|
return fd;
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
error:
|
2013-05-09 18:59:04 +00:00
|
|
|
if (fd >= 0) {
|
|
|
|
/* some other failure after the open succeeded */
|
|
|
|
VIR_FORCE_CLOSE(fd);
|
2015-10-08 12:39:51 +00:00
|
|
|
if (created)
|
|
|
|
unlink(path);
|
2013-05-09 18:59:04 +00:00
|
|
|
}
|
|
|
|
/* whoever failed the open last has already set ret = -errno */
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-08-24 21:00:02 +00:00
|
|
|
|
2016-03-09 15:53:54 +00:00
|
|
|
/* virFileRemoveNeedsSetuid:
|
2016-03-09 17:20:37 +00:00
|
|
|
* @path: file we plan to remove
|
2016-03-09 15:53:54 +00:00
|
|
|
* @uid: file uid to check
|
|
|
|
* @gid: file gid to check
|
|
|
|
*
|
|
|
|
* Return true if we should use setuid/setgid before deleting a file
|
|
|
|
* owned by the passed uid/gid pair. Needed for NFS with root-squash
|
|
|
|
*/
|
|
|
|
static bool
|
2016-03-09 17:20:37 +00:00
|
|
|
virFileRemoveNeedsSetuid(const char *path, uid_t uid, gid_t gid)
|
2016-03-09 15:53:54 +00:00
|
|
|
{
|
|
|
|
/* If running unprivileged, setuid isn't going to work */
|
|
|
|
if (geteuid() != 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* uid/gid weren't specified */
|
|
|
|
if ((uid == (uid_t) -1) && (gid == (gid_t) -1))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* already running as proper uid/gid */
|
|
|
|
if (uid == geteuid() && gid == getegid())
|
|
|
|
return false;
|
|
|
|
|
2016-03-09 17:20:37 +00:00
|
|
|
/* Only perform the setuid stuff for NFS, which is the only case
|
|
|
|
that may actually need it. This can error, but just be safe and
|
|
|
|
only check for a clear negative result. */
|
|
|
|
if (virFileIsSharedFSType(path, VIR_FILE_SHFS_NFS) == 0)
|
|
|
|
return false;
|
|
|
|
|
2016-03-09 15:53:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-21 11:36:55 +00:00
|
|
|
/* virFileRemove:
|
|
|
|
* @path: file to unlink or directory to remove
|
2015-08-24 21:00:02 +00:00
|
|
|
* @uid: uid that was used to create the file (not required)
|
|
|
|
* @gid: gid that was used to create the file (not required)
|
|
|
|
*
|
|
|
|
* If a file/volume was created in an NFS root-squash environment,
|
|
|
|
* then we must 'unlink' the file in the same environment. Unlike
|
|
|
|
* the virFileOpenAs[Forked] and virDirCreate[NoFork], this code
|
|
|
|
* takes no extra flags and does not bother with EACCES failures
|
|
|
|
* from the child.
|
|
|
|
*/
|
|
|
|
int
|
2015-09-21 11:36:55 +00:00
|
|
|
virFileRemove(const char *path,
|
2015-08-24 21:00:02 +00:00
|
|
|
uid_t uid,
|
|
|
|
gid_t gid)
|
|
|
|
{
|
|
|
|
pid_t pid;
|
2015-09-14 20:51:14 +00:00
|
|
|
int status = 0, ret = 0;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree gid_t *groups = NULL;
|
2015-08-24 21:00:02 +00:00
|
|
|
int ngroups;
|
|
|
|
|
2016-03-09 17:20:37 +00:00
|
|
|
if (!virFileRemoveNeedsSetuid(path, uid, gid)) {
|
2015-09-21 11:36:55 +00:00
|
|
|
if (virFileIsDir(path))
|
|
|
|
return rmdir(path);
|
|
|
|
else
|
|
|
|
return unlink(path);
|
|
|
|
}
|
2015-08-24 21:00:02 +00:00
|
|
|
|
|
|
|
/* Otherwise, we have to deal with the NFS root-squash craziness
|
|
|
|
* to run under the uid/gid that created the volume in order to
|
|
|
|
* perform the unlink of the volume.
|
|
|
|
*/
|
|
|
|
if (uid == (uid_t) -1)
|
|
|
|
uid = geteuid();
|
|
|
|
if (gid == (gid_t) -1)
|
|
|
|
gid = getegid();
|
|
|
|
|
|
|
|
ngroups = virGetGroupList(uid, gid, &groups);
|
|
|
|
if (ngroups < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
pid = virFork();
|
|
|
|
|
2018-07-13 17:54:48 +00:00
|
|
|
if (pid < 0)
|
|
|
|
return -errno;
|
2015-08-24 21:00:02 +00:00
|
|
|
|
|
|
|
if (pid) { /* parent */
|
|
|
|
/* wait for child to complete, and retrieve its exit code */
|
|
|
|
|
2015-09-14 20:51:14 +00:00
|
|
|
if (virProcessWait(pid, &status, 0) < 0) {
|
|
|
|
/* virProcessWait() reports errno on waitpid failure, so we'll just
|
|
|
|
* set our return status to EINTR; otherwise, set status to EACCES
|
|
|
|
* since the original failure for the fork+setuid path would have
|
|
|
|
* been EACCES or EPERM by definition.
|
|
|
|
*/
|
|
|
|
if (virLastErrorIsSystemErrno(0))
|
|
|
|
status = EINTR;
|
|
|
|
else if (!status)
|
|
|
|
status = EACCES;
|
2015-08-24 21:00:02 +00:00
|
|
|
}
|
|
|
|
|
2015-09-30 21:37:27 +00:00
|
|
|
if (status) {
|
|
|
|
errno = status;
|
|
|
|
ret = -1;
|
|
|
|
}
|
2015-08-24 21:00:02 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* child */
|
|
|
|
|
|
|
|
/* set desired uid/gid, then attempt to unlink the file */
|
|
|
|
if (virSetUIDGID(uid, gid, groups, ngroups) < 0) {
|
|
|
|
ret = errno;
|
|
|
|
goto childerror;
|
|
|
|
}
|
|
|
|
|
2015-09-21 11:36:55 +00:00
|
|
|
if (virFileIsDir(path)) {
|
|
|
|
if (rmdir(path) < 0) {
|
|
|
|
ret = errno;
|
|
|
|
goto childerror;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (unlink(path) < 0) {
|
|
|
|
ret = errno;
|
|
|
|
goto childerror;
|
|
|
|
}
|
2015-08-24 21:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
childerror:
|
|
|
|
if ((ret & 0xff) != ret) {
|
|
|
|
VIR_WARN("unable to pass desired return value %d", ret);
|
|
|
|
ret = 0xff;
|
|
|
|
}
|
|
|
|
_exit(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-08 12:50:34 +00:00
|
|
|
/* Attempt to create a directory and possibly adjust its owner/group and
|
|
|
|
* permissions.
|
|
|
|
*
|
|
|
|
* return 0 on success or -errno on failure. Additionally to avoid another
|
|
|
|
* round-trip to remove the directory on failure, perform the rmdir when
|
|
|
|
* a mkdir was successful, but some other failure would cause a -1 return.
|
|
|
|
* The storage driver buildVol backend function expects the directory to
|
|
|
|
* be deleted on error.
|
|
|
|
*/
|
2013-05-09 18:59:04 +00:00
|
|
|
static int
|
|
|
|
virDirCreateNoFork(const char *path,
|
|
|
|
mode_t mode, uid_t uid, gid_t gid,
|
|
|
|
unsigned int flags)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
struct stat st;
|
2015-10-08 12:50:34 +00:00
|
|
|
bool created = false;
|
2013-05-09 18:59:04 +00:00
|
|
|
|
2015-05-05 16:34:41 +00:00
|
|
|
if (!((flags & VIR_DIR_CREATE_ALLOW_EXIST) && virFileExists(path))) {
|
2015-04-27 20:48:05 +00:00
|
|
|
if (mkdir(path, mode) < 0) {
|
|
|
|
ret = -errno;
|
|
|
|
virReportSystemError(errno, _("failed to create directory '%s'"),
|
|
|
|
path);
|
|
|
|
goto error;
|
|
|
|
}
|
2015-10-08 12:50:34 +00:00
|
|
|
created = true;
|
2013-05-09 18:59:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (stat(path, &st) == -1) {
|
|
|
|
ret = -errno;
|
|
|
|
virReportSystemError(errno, _("stat of '%s' failed"), path);
|
|
|
|
goto error;
|
|
|
|
}
|
2020-01-17 11:24:19 +00:00
|
|
|
# ifndef WIN32
|
2015-04-27 20:48:05 +00:00
|
|
|
if (((uid != (uid_t) -1 && st.st_uid != uid) ||
|
|
|
|
(gid != (gid_t) -1 && st.st_gid != gid))
|
2013-05-09 18:59:04 +00:00
|
|
|
&& (chown(path, uid, gid) < 0)) {
|
|
|
|
ret = -errno;
|
|
|
|
virReportSystemError(errno, _("cannot chown '%s' to (%u, %u)"),
|
|
|
|
path, (unsigned int) uid, (unsigned int) gid);
|
|
|
|
goto error;
|
|
|
|
}
|
2020-01-17 11:24:19 +00:00
|
|
|
# endif /* !WIN32 */
|
2015-04-27 20:48:05 +00:00
|
|
|
if (mode != (mode_t) -1 && chmod(path, mode) < 0) {
|
2013-05-09 18:59:04 +00:00
|
|
|
ret = -errno;
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("cannot set mode of '%s' to %04o"),
|
|
|
|
path, mode);
|
|
|
|
goto error;
|
|
|
|
}
|
2014-03-25 06:53:22 +00:00
|
|
|
error:
|
2015-10-08 12:50:34 +00:00
|
|
|
if (ret < 0 && created)
|
|
|
|
rmdir(path);
|
2013-05-09 18:59:04 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-10-08 12:50:34 +00:00
|
|
|
/*
|
|
|
|
* virDirCreate:
|
|
|
|
* @path: directory to create
|
|
|
|
* @mode: mode to use on creation or when forcing permissions
|
|
|
|
* @uid: uid that should own directory
|
|
|
|
* @gid: gid that should own directory
|
|
|
|
* @flags: bit-wise or of VIR_DIR_CREATE_* flags
|
|
|
|
*
|
|
|
|
* Attempt to create a directory and possibly adjust its owner/group and
|
|
|
|
* permissions. If conditions allow, use the *NoFork code in order to create
|
|
|
|
* the directory under current owner/group rather than via a forked process.
|
|
|
|
*
|
|
|
|
* return 0 on success or -errno on failure. Additionally to avoid another
|
|
|
|
* round-trip to remove the directory on failure, perform the rmdir if a
|
|
|
|
* mkdir was successful, but some other failure would cause a -1 return.
|
|
|
|
* The storage driver buildVol backend function expects the directory to
|
|
|
|
* be deleted on error.
|
|
|
|
*
|
|
|
|
*/
|
2013-05-09 18:59:04 +00:00
|
|
|
int
|
|
|
|
virDirCreate(const char *path,
|
|
|
|
mode_t mode, uid_t uid, gid_t gid,
|
|
|
|
unsigned int flags)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
pid_t pid;
|
2015-09-15 12:27:38 +00:00
|
|
|
int status = 0, ret = 0;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree gid_t *groups = NULL;
|
util: make virSetUIDGID async-signal-safe
https://bugzilla.redhat.com/show_bug.cgi?id=964358
POSIX states that multi-threaded apps should not use functions
that are not async-signal-safe between fork and exec, yet we
were using getpwuid_r and initgroups. Although rare, it is
possible to hit deadlock in the child, when it tries to grab
a mutex that was already held by another thread in the parent.
I actually hit this deadlock when testing multiple domains
being started in parallel with a command hook, with the following
backtrace in the child:
Thread 1 (Thread 0x7fd56bbf2700 (LWP 3212)):
#0 __lll_lock_wait ()
at ../nptl/sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:136
#1 0x00007fd5761e7388 in _L_lock_854 () from /lib64/libpthread.so.0
#2 0x00007fd5761e7257 in __pthread_mutex_lock (mutex=0x7fd56be00360)
at pthread_mutex_lock.c:61
#3 0x00007fd56bbf9fc5 in _nss_files_getpwuid_r (uid=0, result=0x7fd56bbf0c70,
buffer=0x7fd55c2a65f0 "", buflen=1024, errnop=0x7fd56bbf25b8)
at nss_files/files-pwd.c:40
#4 0x00007fd575aeff1d in __getpwuid_r (uid=0, resbuf=0x7fd56bbf0c70,
buffer=0x7fd55c2a65f0 "", buflen=1024, result=0x7fd56bbf0cb0)
at ../nss/getXXbyYY_r.c:253
#5 0x00007fd578aebafc in virSetUIDGID (uid=0, gid=0) at util/virutil.c:1031
#6 0x00007fd578aebf43 in virSetUIDGIDWithCaps (uid=0, gid=0, capBits=0,
clearExistingCaps=true) at util/virutil.c:1388
#7 0x00007fd578a9a20b in virExec (cmd=0x7fd55c231f10) at util/vircommand.c:654
#8 0x00007fd578a9dfa2 in virCommandRunAsync (cmd=0x7fd55c231f10, pid=0x0)
at util/vircommand.c:2247
#9 0x00007fd578a9d74e in virCommandRun (cmd=0x7fd55c231f10, exitstatus=0x0)
at util/vircommand.c:2100
#10 0x00007fd56326fde5 in qemuProcessStart (conn=0x7fd53c000df0,
driver=0x7fd55c0dc4f0, vm=0x7fd54800b100, migrateFrom=0x0, stdin_fd=-1,
stdin_path=0x0, snapshot=0x0, vmop=VIR_NETDEV_VPORT_PROFILE_OP_CREATE,
flags=1) at qemu/qemu_process.c:3694
...
The solution is to split the work of getpwuid_r/initgroups into the
unsafe portions (getgrouplist, called pre-fork) and safe portions
(setgroups, called post-fork).
* src/util/virutil.h (virSetUIDGID, virSetUIDGIDWithCaps): Adjust
signature.
* src/util/virutil.c (virSetUIDGID): Add parameters.
(virSetUIDGIDWithCaps): Adjust clients.
* src/util/vircommand.c (virExec): Likewise.
* src/util/virfile.c (virFileAccessibleAs, virFileOpenForked)
(virDirCreate): Likewise.
* src/security/security_dac.c (virSecurityDACSetProcessLabel):
Likewise.
* src/lxc/lxc_container.c (lxcContainerSetID): Likewise.
* configure.ac (AC_CHECK_FUNCS_ONCE): Check for setgroups, not
initgroups.
Signed-off-by: Eric Blake <eblake@redhat.com>
2013-05-22 02:59:10 +00:00
|
|
|
int ngroups;
|
2015-10-08 12:50:34 +00:00
|
|
|
bool created = false;
|
2013-05-09 18:59:04 +00:00
|
|
|
|
2015-04-27 20:48:05 +00:00
|
|
|
/* Everything after this check is crazyness to allow setting uid/gid
|
|
|
|
* on directories that are on root-squash NFS shares. We only want
|
|
|
|
* to go that route if the follow conditions are true:
|
|
|
|
*
|
|
|
|
* 1) VIR_DIR_CREATE_AS_UID was passed, currently only used when
|
|
|
|
* directory is being created for a NETFS pool
|
|
|
|
* 2) We are running as root, since that's when the root-squash
|
|
|
|
* workaround is required.
|
|
|
|
* 3) An explicit uid/gid was requested
|
|
|
|
* 4) The directory doesn't already exist and the ALLOW_EXIST flag
|
|
|
|
* wasn't passed.
|
|
|
|
*
|
|
|
|
* If any of those conditions are _not_ met, ignore the fork crazyness
|
|
|
|
*/
|
2013-05-09 18:59:04 +00:00
|
|
|
if ((!(flags & VIR_DIR_CREATE_AS_UID))
|
2013-10-09 11:13:45 +00:00
|
|
|
|| (geteuid() != 0)
|
2015-04-27 20:48:05 +00:00
|
|
|
|| ((uid == (uid_t) -1) && (gid == (gid_t) -1))
|
|
|
|
|| ((flags & VIR_DIR_CREATE_ALLOW_EXIST) && virFileExists(path))) {
|
2013-05-09 18:59:04 +00:00
|
|
|
return virDirCreateNoFork(path, mode, uid, gid, flags);
|
|
|
|
}
|
|
|
|
|
2015-04-27 20:48:05 +00:00
|
|
|
if (uid == (uid_t) -1)
|
|
|
|
uid = geteuid();
|
|
|
|
if (gid == (gid_t) -1)
|
|
|
|
gid = getegid();
|
|
|
|
|
util: make virSetUIDGID async-signal-safe
https://bugzilla.redhat.com/show_bug.cgi?id=964358
POSIX states that multi-threaded apps should not use functions
that are not async-signal-safe between fork and exec, yet we
were using getpwuid_r and initgroups. Although rare, it is
possible to hit deadlock in the child, when it tries to grab
a mutex that was already held by another thread in the parent.
I actually hit this deadlock when testing multiple domains
being started in parallel with a command hook, with the following
backtrace in the child:
Thread 1 (Thread 0x7fd56bbf2700 (LWP 3212)):
#0 __lll_lock_wait ()
at ../nptl/sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:136
#1 0x00007fd5761e7388 in _L_lock_854 () from /lib64/libpthread.so.0
#2 0x00007fd5761e7257 in __pthread_mutex_lock (mutex=0x7fd56be00360)
at pthread_mutex_lock.c:61
#3 0x00007fd56bbf9fc5 in _nss_files_getpwuid_r (uid=0, result=0x7fd56bbf0c70,
buffer=0x7fd55c2a65f0 "", buflen=1024, errnop=0x7fd56bbf25b8)
at nss_files/files-pwd.c:40
#4 0x00007fd575aeff1d in __getpwuid_r (uid=0, resbuf=0x7fd56bbf0c70,
buffer=0x7fd55c2a65f0 "", buflen=1024, result=0x7fd56bbf0cb0)
at ../nss/getXXbyYY_r.c:253
#5 0x00007fd578aebafc in virSetUIDGID (uid=0, gid=0) at util/virutil.c:1031
#6 0x00007fd578aebf43 in virSetUIDGIDWithCaps (uid=0, gid=0, capBits=0,
clearExistingCaps=true) at util/virutil.c:1388
#7 0x00007fd578a9a20b in virExec (cmd=0x7fd55c231f10) at util/vircommand.c:654
#8 0x00007fd578a9dfa2 in virCommandRunAsync (cmd=0x7fd55c231f10, pid=0x0)
at util/vircommand.c:2247
#9 0x00007fd578a9d74e in virCommandRun (cmd=0x7fd55c231f10, exitstatus=0x0)
at util/vircommand.c:2100
#10 0x00007fd56326fde5 in qemuProcessStart (conn=0x7fd53c000df0,
driver=0x7fd55c0dc4f0, vm=0x7fd54800b100, migrateFrom=0x0, stdin_fd=-1,
stdin_path=0x0, snapshot=0x0, vmop=VIR_NETDEV_VPORT_PROFILE_OP_CREATE,
flags=1) at qemu/qemu_process.c:3694
...
The solution is to split the work of getpwuid_r/initgroups into the
unsafe portions (getgrouplist, called pre-fork) and safe portions
(setgroups, called post-fork).
* src/util/virutil.h (virSetUIDGID, virSetUIDGIDWithCaps): Adjust
signature.
* src/util/virutil.c (virSetUIDGID): Add parameters.
(virSetUIDGIDWithCaps): Adjust clients.
* src/util/vircommand.c (virExec): Likewise.
* src/util/virfile.c (virFileAccessibleAs, virFileOpenForked)
(virDirCreate): Likewise.
* src/security/security_dac.c (virSecurityDACSetProcessLabel):
Likewise.
* src/lxc/lxc_container.c (lxcContainerSetID): Likewise.
* configure.ac (AC_CHECK_FUNCS_ONCE): Check for setgroups, not
initgroups.
Signed-off-by: Eric Blake <eblake@redhat.com>
2013-05-22 02:59:10 +00:00
|
|
|
ngroups = virGetGroupList(uid, gid, &groups);
|
|
|
|
if (ngroups < 0)
|
|
|
|
return -errno;
|
|
|
|
|
virFork: simplify semantics
The old semantics of virFork() violates the priciple of good
usability: it requires the caller to check the pid argument
after use, *even when virFork returned -1*, in order to properly
abort a child process that failed setup done immediately after
fork() - that is, the caller must call _exit() in the child.
While uses in virfile.c did this correctly, uses in 'virsh
lxc-enter-namespace' and 'virt-login-shell' would happily return
from the calling function in both the child and the parent,
leading to very confusing results. [Thankfully, I found the
problem by inspection, and can't actually trigger the double
return on error without an LD_PRELOAD library.]
It is much better if the semantics of virFork are impossible
to abuse. Looking at virFork(), the parent could only ever
return -1 with a non-negative pid if it misused pthread_sigmask,
but this never happens. Up until this patch series, the child
could return -1 with non-negative pid if it fails to set up
signals correctly, but we recently fixed that to make the child
call _exit() at that point instead of forcing the caller to do
it. Thus, the return value and contents of the pid argument are
now redundant (a -1 return now happens only for failure to fork,
a child 0 return only happens for a successful 0 pid, and a
parent 0 return only happens for a successful non-zero pid),
so we might as well return the pid directly rather than an
integer of whether it succeeded or failed; this is also good
from the interface design perspective as users are already
familiar with fork() semantics.
One last change in this patch: before returning the pid directly,
I found cases where using virProcessWait unconditionally on a
cleanup path of a virFork's -1 pid return would be nicer if there
were a way to avoid it overwriting an earlier message. While
such paths are a bit harder to come by with my change to a direct
pid return, I decided to keep the virProcessWait change in this
patch.
* src/util/vircommand.h (virFork): Change signature.
* src/util/vircommand.c (virFork): Guarantee that child will only
return on success, to simplify callers. Return pid rather than
status, now that the situations are always the same.
(virExec): Adjust caller, also avoid open-coding process death.
* src/util/virprocess.c (virProcessWait): Tweak semantics when pid
is -1.
(virProcessRunInMountNamespace): Adjust caller.
* src/util/virfile.c (virFileAccessibleAs, virFileOpenForked)
(virDirCreate): Likewise.
* tools/virt-login-shell.c (main): Likewise.
* tools/virsh-domain.c (cmdLxcEnterNamespace): Likewise.
* tests/commandtest.c (test23): Likewise.
Signed-off-by: Eric Blake <eblake@redhat.com>
2013-12-22 00:54:33 +00:00
|
|
|
pid = virFork();
|
2013-05-09 18:59:04 +00:00
|
|
|
|
2018-07-13 17:54:48 +00:00
|
|
|
if (pid < 0)
|
|
|
|
return -errno;
|
2013-05-09 18:59:04 +00:00
|
|
|
|
|
|
|
if (pid) { /* parent */
|
|
|
|
/* wait for child to complete, and retrieve its exit code */
|
2015-06-16 06:50:03 +00:00
|
|
|
|
2015-09-15 12:27:38 +00:00
|
|
|
if (virProcessWait(pid, &status, 0) < 0) {
|
|
|
|
/* virProcessWait() reports errno on waitpid failure, so we'll just
|
|
|
|
* set our return status to EINTR; otherwise, set status to EACCES
|
|
|
|
* since the original failure for the fork+setuid path would have
|
|
|
|
* been EACCES or EPERM by definition.
|
|
|
|
*/
|
|
|
|
if (virLastErrorIsSystemErrno(0))
|
|
|
|
status = EINTR;
|
|
|
|
else if (!status)
|
|
|
|
status = EACCES;
|
2013-05-09 18:59:04 +00:00
|
|
|
}
|
2015-06-11 08:51:23 +00:00
|
|
|
|
|
|
|
/*
|
2015-09-15 12:27:38 +00:00
|
|
|
* If the child exited with EACCES, then fall back to non-fork method
|
|
|
|
* as in the original logic introduced and explained by commit 98f6f381.
|
2015-06-11 08:51:23 +00:00
|
|
|
*/
|
2015-09-15 12:27:38 +00:00
|
|
|
if (status == EACCES) {
|
|
|
|
virResetLastError();
|
|
|
|
return virDirCreateNoFork(path, mode, uid, gid, flags);
|
2013-05-09 18:59:04 +00:00
|
|
|
}
|
2015-06-11 08:51:23 +00:00
|
|
|
|
2015-09-15 12:27:38 +00:00
|
|
|
if (status)
|
|
|
|
ret = -status;
|
|
|
|
|
2013-05-09 18:59:04 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* child */
|
|
|
|
|
|
|
|
/* set desired uid/gid, then attempt to create the directory */
|
util: make virSetUIDGID async-signal-safe
https://bugzilla.redhat.com/show_bug.cgi?id=964358
POSIX states that multi-threaded apps should not use functions
that are not async-signal-safe between fork and exec, yet we
were using getpwuid_r and initgroups. Although rare, it is
possible to hit deadlock in the child, when it tries to grab
a mutex that was already held by another thread in the parent.
I actually hit this deadlock when testing multiple domains
being started in parallel with a command hook, with the following
backtrace in the child:
Thread 1 (Thread 0x7fd56bbf2700 (LWP 3212)):
#0 __lll_lock_wait ()
at ../nptl/sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:136
#1 0x00007fd5761e7388 in _L_lock_854 () from /lib64/libpthread.so.0
#2 0x00007fd5761e7257 in __pthread_mutex_lock (mutex=0x7fd56be00360)
at pthread_mutex_lock.c:61
#3 0x00007fd56bbf9fc5 in _nss_files_getpwuid_r (uid=0, result=0x7fd56bbf0c70,
buffer=0x7fd55c2a65f0 "", buflen=1024, errnop=0x7fd56bbf25b8)
at nss_files/files-pwd.c:40
#4 0x00007fd575aeff1d in __getpwuid_r (uid=0, resbuf=0x7fd56bbf0c70,
buffer=0x7fd55c2a65f0 "", buflen=1024, result=0x7fd56bbf0cb0)
at ../nss/getXXbyYY_r.c:253
#5 0x00007fd578aebafc in virSetUIDGID (uid=0, gid=0) at util/virutil.c:1031
#6 0x00007fd578aebf43 in virSetUIDGIDWithCaps (uid=0, gid=0, capBits=0,
clearExistingCaps=true) at util/virutil.c:1388
#7 0x00007fd578a9a20b in virExec (cmd=0x7fd55c231f10) at util/vircommand.c:654
#8 0x00007fd578a9dfa2 in virCommandRunAsync (cmd=0x7fd55c231f10, pid=0x0)
at util/vircommand.c:2247
#9 0x00007fd578a9d74e in virCommandRun (cmd=0x7fd55c231f10, exitstatus=0x0)
at util/vircommand.c:2100
#10 0x00007fd56326fde5 in qemuProcessStart (conn=0x7fd53c000df0,
driver=0x7fd55c0dc4f0, vm=0x7fd54800b100, migrateFrom=0x0, stdin_fd=-1,
stdin_path=0x0, snapshot=0x0, vmop=VIR_NETDEV_VPORT_PROFILE_OP_CREATE,
flags=1) at qemu/qemu_process.c:3694
...
The solution is to split the work of getpwuid_r/initgroups into the
unsafe portions (getgrouplist, called pre-fork) and safe portions
(setgroups, called post-fork).
* src/util/virutil.h (virSetUIDGID, virSetUIDGIDWithCaps): Adjust
signature.
* src/util/virutil.c (virSetUIDGID): Add parameters.
(virSetUIDGIDWithCaps): Adjust clients.
* src/util/vircommand.c (virExec): Likewise.
* src/util/virfile.c (virFileAccessibleAs, virFileOpenForked)
(virDirCreate): Likewise.
* src/security/security_dac.c (virSecurityDACSetProcessLabel):
Likewise.
* src/lxc/lxc_container.c (lxcContainerSetID): Likewise.
* configure.ac (AC_CHECK_FUNCS_ONCE): Check for setgroups, not
initgroups.
Signed-off-by: Eric Blake <eblake@redhat.com>
2013-05-22 02:59:10 +00:00
|
|
|
if (virSetUIDGID(uid, gid, groups, ngroups) < 0) {
|
2015-06-16 11:43:38 +00:00
|
|
|
ret = errno;
|
2013-05-09 18:59:04 +00:00
|
|
|
goto childerror;
|
|
|
|
}
|
2015-06-16 06:50:03 +00:00
|
|
|
|
2013-05-09 18:59:04 +00:00
|
|
|
if (mkdir(path, mode) < 0) {
|
2015-06-16 11:43:38 +00:00
|
|
|
ret = errno;
|
|
|
|
if (ret != EACCES) {
|
2013-05-09 18:59:04 +00:00
|
|
|
/* in case of EACCES, the parent will retry */
|
|
|
|
virReportSystemError(errno, _("child failed to create directory '%s'"),
|
|
|
|
path);
|
|
|
|
}
|
|
|
|
goto childerror;
|
|
|
|
}
|
2015-10-08 12:50:34 +00:00
|
|
|
created = true;
|
2015-06-16 06:50:03 +00:00
|
|
|
|
2013-05-09 18:59:04 +00:00
|
|
|
/* check if group was set properly by creating after
|
|
|
|
* setgid. If not, try doing it with chown */
|
|
|
|
if (stat(path, &st) == -1) {
|
2015-06-16 11:43:38 +00:00
|
|
|
ret = errno;
|
2013-05-09 18:59:04 +00:00
|
|
|
virReportSystemError(errno,
|
|
|
|
_("stat of '%s' failed"), path);
|
|
|
|
goto childerror;
|
|
|
|
}
|
2015-06-16 06:50:03 +00:00
|
|
|
|
2013-05-09 18:59:04 +00:00
|
|
|
if ((st.st_gid != gid) && (chown(path, (uid_t) -1, gid) < 0)) {
|
2015-06-16 11:43:38 +00:00
|
|
|
ret = errno;
|
2013-05-09 18:59:04 +00:00
|
|
|
virReportSystemError(errno,
|
|
|
|
_("cannot chown '%s' to group %u"),
|
|
|
|
path, (unsigned int) gid);
|
|
|
|
goto childerror;
|
|
|
|
}
|
2015-06-16 06:50:03 +00:00
|
|
|
|
2015-04-27 20:48:05 +00:00
|
|
|
if (mode != (mode_t) -1 && chmod(path, mode) < 0) {
|
2013-05-09 18:59:04 +00:00
|
|
|
virReportSystemError(errno,
|
|
|
|
_("cannot set mode of '%s' to %04o"),
|
|
|
|
path, mode);
|
|
|
|
goto childerror;
|
|
|
|
}
|
2015-06-16 06:50:03 +00:00
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
childerror:
|
2015-10-08 12:50:34 +00:00
|
|
|
if (ret != 0 && created)
|
|
|
|
rmdir(path);
|
|
|
|
|
2015-06-16 11:43:38 +00:00
|
|
|
if ((ret & 0xff) != ret) {
|
|
|
|
VIR_WARN("unable to pass desired return value %d", ret);
|
|
|
|
ret = 0xff;
|
|
|
|
}
|
2013-05-09 18:59:04 +00:00
|
|
|
_exit(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* WIN32 */
|
|
|
|
|
|
|
|
int
|
|
|
|
virFileAccessibleAs(const char *path,
|
|
|
|
int mode,
|
2019-10-14 12:45:33 +00:00
|
|
|
uid_t uid G_GNUC_UNUSED,
|
|
|
|
gid_t gid G_GNUC_UNUSED)
|
2013-05-09 18:59:04 +00:00
|
|
|
{
|
|
|
|
VIR_WARN("Ignoring uid/gid due to WIN32");
|
|
|
|
|
|
|
|
return access(path, mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return -errno on failure, or 0 on success */
|
|
|
|
int
|
2019-10-14 12:45:33 +00:00
|
|
|
virFileOpenAs(const char *path G_GNUC_UNUSED,
|
|
|
|
int openflags G_GNUC_UNUSED,
|
|
|
|
mode_t mode G_GNUC_UNUSED,
|
|
|
|
uid_t uid G_GNUC_UNUSED,
|
|
|
|
gid_t gid G_GNUC_UNUSED,
|
|
|
|
unsigned int flags_unused G_GNUC_UNUSED)
|
2013-05-09 18:59:04 +00:00
|
|
|
{
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"%s", _("virFileOpenAs is not implemented for WIN32"));
|
|
|
|
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2019-10-14 12:45:33 +00:00
|
|
|
virDirCreate(const char *path G_GNUC_UNUSED,
|
|
|
|
mode_t mode G_GNUC_UNUSED,
|
|
|
|
uid_t uid G_GNUC_UNUSED,
|
|
|
|
gid_t gid G_GNUC_UNUSED,
|
|
|
|
unsigned int flags_unused G_GNUC_UNUSED)
|
2013-05-09 18:59:04 +00:00
|
|
|
{
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"%s", _("virDirCreate is not implemented for WIN32"));
|
|
|
|
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
2015-09-07 08:24:47 +00:00
|
|
|
|
|
|
|
int
|
2015-09-21 11:36:55 +00:00
|
|
|
virFileRemove(const char *path,
|
2019-10-14 12:45:33 +00:00
|
|
|
uid_t uid G_GNUC_UNUSED,
|
|
|
|
gid_t gid G_GNUC_UNUSED)
|
2015-09-07 08:24:47 +00:00
|
|
|
{
|
|
|
|
if (unlink(path) < 0) {
|
|
|
|
virReportSystemError(errno, _("Unable to unlink path '%s'"),
|
|
|
|
path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2013-05-09 18:59:04 +00:00
|
|
|
#endif /* WIN32 */
|
|
|
|
|
2016-06-21 14:33:36 +00:00
|
|
|
static int
|
2016-06-21 14:52:11 +00:00
|
|
|
virDirOpenInternal(DIR **dirp, const char *name, bool ignoreENOENT, bool quiet)
|
2016-06-21 14:33:36 +00:00
|
|
|
{
|
2016-06-21 14:53:42 +00:00
|
|
|
*dirp = opendir(name); /* exempt from syntax-check */
|
2016-06-21 14:33:36 +00:00
|
|
|
if (!*dirp) {
|
2016-06-21 14:52:11 +00:00
|
|
|
if (quiet)
|
|
|
|
return -1;
|
|
|
|
|
2016-06-21 14:43:16 +00:00
|
|
|
if (ignoreENOENT && errno == ENOENT)
|
|
|
|
return 0;
|
2016-06-21 14:33:36 +00:00
|
|
|
virReportSystemError(errno, _("cannot open directory '%s'"), name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virDirOpen
|
|
|
|
* @dirp: directory stream
|
|
|
|
* @name: path of the directory
|
|
|
|
*
|
|
|
|
* Returns 1 on success.
|
|
|
|
* On failure, -1 is returned and an error is reported.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virDirOpen(DIR **dirp, const char *name)
|
|
|
|
{
|
2016-06-21 14:52:11 +00:00
|
|
|
return virDirOpenInternal(dirp, name, false, false);
|
2016-06-21 14:43:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virDirOpenIfExists
|
|
|
|
* @dirp: directory stream
|
|
|
|
* @name: path of the directory
|
|
|
|
*
|
|
|
|
* Returns 1 on success.
|
|
|
|
* If opendir returns ENOENT, 0 is returned without reporting an error.
|
|
|
|
* On other errors, -1 is returned and an error is reported.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virDirOpenIfExists(DIR **dirp, const char *name)
|
|
|
|
{
|
2016-06-21 14:52:11 +00:00
|
|
|
return virDirOpenInternal(dirp, name, true, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virDirOpenQuiet
|
|
|
|
* @dirp: directory stream
|
|
|
|
* @name: path of the directory
|
|
|
|
*
|
|
|
|
* Returns 1 on success.
|
|
|
|
* -1 on failure.
|
|
|
|
*
|
|
|
|
* Does not report any errors and errno is preserved.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virDirOpenQuiet(DIR **dirp, const char *name)
|
|
|
|
{
|
|
|
|
return virDirOpenInternal(dirp, name, false, true);
|
2016-06-21 14:33:36 +00:00
|
|
|
}
|
|
|
|
|
2014-04-20 11:53:45 +00:00
|
|
|
/**
|
|
|
|
* virDirRead:
|
|
|
|
* @dirp: directory to read
|
2017-03-29 11:41:20 +00:00
|
|
|
* @ent: output one entry
|
2014-04-20 11:53:45 +00:00
|
|
|
* @name: if non-NULL, the name related to @dirp for use in error reporting
|
|
|
|
*
|
|
|
|
* Wrapper around readdir. Typical usage:
|
2017-03-29 11:41:20 +00:00
|
|
|
* struct dirent *ent;
|
2016-06-21 14:33:36 +00:00
|
|
|
* int rc;
|
2014-04-20 11:53:45 +00:00
|
|
|
* DIR *dir;
|
2016-06-21 14:33:36 +00:00
|
|
|
* if (virDirOpen(&dir, name) < 0)
|
2014-04-20 11:53:45 +00:00
|
|
|
* goto error;
|
2016-06-21 14:33:36 +00:00
|
|
|
* while ((rc = virDirRead(dir, &ent, name)) > 0)
|
2014-04-20 11:53:45 +00:00
|
|
|
* process ent;
|
2016-06-21 14:33:36 +00:00
|
|
|
* if (rc < 0)
|
2014-04-20 11:53:45 +00:00
|
|
|
* goto error;
|
|
|
|
*
|
|
|
|
* Returns -1 on error, with error already reported if @name was
|
|
|
|
* supplied. On success, returns 1 for entry read, 0 for end-of-dir.
|
|
|
|
*/
|
|
|
|
int virDirRead(DIR *dirp, struct dirent **ent, const char *name)
|
|
|
|
{
|
2016-06-21 15:23:41 +00:00
|
|
|
do {
|
|
|
|
errno = 0;
|
|
|
|
*ent = readdir(dirp); /* exempt from syntax-check */
|
|
|
|
if (!*ent && errno) {
|
|
|
|
if (name)
|
|
|
|
virReportSystemError(errno, _("Unable to read directory '%s'"),
|
|
|
|
name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} while (*ent && (STREQ((*ent)->d_name, ".") ||
|
|
|
|
STREQ((*ent)->d_name, "..")));
|
2014-04-20 11:53:45 +00:00
|
|
|
return !!*ent;
|
|
|
|
}
|
|
|
|
|
2016-06-21 10:40:29 +00:00
|
|
|
void virDirClose(DIR **dirp)
|
|
|
|
{
|
|
|
|
if (!*dirp)
|
|
|
|
return;
|
|
|
|
|
|
|
|
closedir(*dirp); /* exempt from syntax-check */
|
|
|
|
*dirp = NULL;
|
|
|
|
}
|
|
|
|
|
2017-04-04 16:22:31 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* virFileChownFiles:
|
|
|
|
* @name: name of the directory
|
|
|
|
* @uid: uid
|
|
|
|
* @gid: gid
|
|
|
|
*
|
|
|
|
* Change ownership of all regular files in a directory.
|
|
|
|
*
|
|
|
|
* Returns -1 on error, with error already reported, 0 on success.
|
|
|
|
*/
|
2020-01-17 11:24:19 +00:00
|
|
|
#ifndef WIN32
|
2017-04-04 16:22:31 +00:00
|
|
|
int virFileChownFiles(const char *name,
|
|
|
|
uid_t uid,
|
|
|
|
gid_t gid)
|
|
|
|
{
|
|
|
|
struct dirent *ent;
|
|
|
|
int ret = -1;
|
|
|
|
int direrr;
|
|
|
|
DIR *dir;
|
|
|
|
|
|
|
|
if (virDirOpen(&dir, name) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
while ((direrr = virDirRead(dir, &ent, name)) > 0) {
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *path = NULL;
|
2018-07-13 17:54:48 +00:00
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
path = g_strdup_printf("%s/%s", name, ent->d_name);
|
2017-04-04 16:22:31 +00:00
|
|
|
|
2018-06-06 16:24:12 +00:00
|
|
|
if (!virFileIsRegular(path))
|
|
|
|
continue;
|
|
|
|
|
2017-04-04 16:22:31 +00:00
|
|
|
if (chown(path, uid, gid) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("cannot chown '%s' to (%u, %u)"),
|
|
|
|
ent->d_name, (unsigned int) uid,
|
|
|
|
(unsigned int) gid);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (direrr < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
virDirClose(&dir);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-01-17 11:24:19 +00:00
|
|
|
#else /* WIN32 */
|
|
|
|
|
|
|
|
int virFileChownFiles(const char *name,
|
|
|
|
uid_t uid,
|
|
|
|
gid_t gid)
|
|
|
|
{
|
|
|
|
virReportSystemError(ENOSYS,
|
|
|
|
_("cannot chown '%s' to (%u, %u)"),
|
|
|
|
name, (unsigned int) uid,
|
|
|
|
(unsigned int) gid);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif /* WIN32 */
|
2017-04-04 16:22:31 +00:00
|
|
|
|
2013-05-09 18:59:04 +00:00
|
|
|
static int
|
|
|
|
virFileMakePathHelper(char *path, mode_t mode)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
VIR_DEBUG("path=%s mode=0%o", path, mode);
|
|
|
|
|
|
|
|
if (stat(path, &st) >= 0) {
|
|
|
|
if (S_ISDIR(st.st_mode))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
errno = ENOTDIR;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errno != ENOENT)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if ((p = strrchr(path, '/')) == NULL) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p != path) {
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
if (virFileMakePathHelper(path, mode) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
*p = '/';
|
|
|
|
}
|
|
|
|
|
2020-01-17 11:21:29 +00:00
|
|
|
if (g_mkdir(path, mode) < 0 && errno != EEXIST)
|
2013-05-09 18:59:04 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the given directory with mode 0777 if it's not already existing.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, or -1 if an error occurred (in which case, errno
|
|
|
|
* is set appropriately).
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virFileMakePath(const char *path)
|
|
|
|
{
|
|
|
|
return virFileMakePathWithMode(path, 0777);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
virFileMakePathWithMode(const char *path,
|
|
|
|
mode_t mode)
|
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *tmp = NULL;
|
2013-05-09 18:59:04 +00:00
|
|
|
|
2019-10-20 11:49:46 +00:00
|
|
|
tmp = g_strdup(path);
|
2013-05-09 18:59:04 +00:00
|
|
|
|
2018-07-13 17:54:48 +00:00
|
|
|
return virFileMakePathHelper(tmp, mode);
|
2013-05-09 18:59:04 +00:00
|
|
|
}
|
|
|
|
|
2014-01-30 17:06:39 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
virFileMakeParentPath(const char *path)
|
|
|
|
{
|
|
|
|
char *p;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *tmp = NULL;
|
2014-01-30 17:06:39 +00:00
|
|
|
|
|
|
|
VIR_DEBUG("path=%s", path);
|
|
|
|
|
2019-10-20 11:49:46 +00:00
|
|
|
tmp = g_strdup(path);
|
2014-01-30 17:06:39 +00:00
|
|
|
|
|
|
|
if ((p = strrchr(tmp, '/')) == NULL) {
|
|
|
|
errno = EINVAL;
|
2018-07-13 17:54:48 +00:00
|
|
|
return -1;
|
2014-01-30 17:06:39 +00:00
|
|
|
}
|
|
|
|
*p = '\0';
|
|
|
|
|
2018-07-13 17:54:48 +00:00
|
|
|
return virFileMakePathHelper(tmp, 0777);
|
2014-01-30 17:06:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-09 18:59:04 +00:00
|
|
|
/* Build up a fully qualified path for a config file to be
|
|
|
|
* associated with a persistent guest or network */
|
|
|
|
char *
|
|
|
|
virFileBuildPath(const char *dir, const char *name, const char *ext)
|
|
|
|
{
|
|
|
|
char *path;
|
|
|
|
|
|
|
|
if (ext == NULL) {
|
2019-10-22 13:26:14 +00:00
|
|
|
path = g_strdup_printf("%s/%s", dir, name);
|
2013-05-09 18:59:04 +00:00
|
|
|
} else {
|
2019-10-22 13:26:14 +00:00
|
|
|
path = g_strdup_printf("%s/%s%s", dir, name, ext);
|
2013-05-09 18:59:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2020-06-17 09:48:34 +00:00
|
|
|
/* Open a non-blocking primary side of a pty. If ttyName is not NULL,
|
|
|
|
* then populate it with the name of the secondary peer. If rawmode is
|
|
|
|
* set, also put the primary side into raw mode before returning. */
|
2013-05-09 18:59:04 +00:00
|
|
|
#ifndef WIN32
|
|
|
|
int
|
2020-06-17 09:48:34 +00:00
|
|
|
virFileOpenTty(int *ttyprimary, char **ttyName, int rawmode)
|
2013-05-09 18:59:04 +00:00
|
|
|
{
|
|
|
|
/* XXX A word of caution - on some platforms (Solaris and HP-UX),
|
2020-06-17 09:48:34 +00:00
|
|
|
* additional ioctl() calls are needs after opening the secondary
|
2013-05-09 18:59:04 +00:00
|
|
|
* before it will cause isatty() to return true. Should we make
|
2020-06-17 09:48:34 +00:00
|
|
|
* virFileOpenTty also return the opened secondary fd, so the caller
|
2013-05-09 18:59:04 +00:00
|
|
|
* doesn't have to worry about that mess? */
|
|
|
|
int ret = -1;
|
2020-06-17 09:48:34 +00:00
|
|
|
int secondary = -1;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *name = NULL;
|
2013-05-09 18:59:04 +00:00
|
|
|
|
|
|
|
/* Unfortunately, we can't use the name argument of openpty, since
|
|
|
|
* there is no guarantee on how large the buffer has to be.
|
|
|
|
* Likewise, we can't use the termios argument: we have to use
|
|
|
|
* read-modify-write since there is no portable way to initialize
|
|
|
|
* a struct termios without use of tcgetattr. */
|
2020-06-17 09:48:34 +00:00
|
|
|
if (openpty(ttyprimary, &secondary, NULL, NULL, NULL) < 0)
|
2013-05-09 18:59:04 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* What a shame that openpty cannot atomically set FD_CLOEXEC, but
|
|
|
|
* that using posix_openpt/grantpt/unlockpt/ptsname is not
|
|
|
|
* thread-safe, and that ptsname_r is not portable. */
|
2020-06-17 09:48:34 +00:00
|
|
|
if (virSetNonBlock(*ttyprimary) < 0 ||
|
|
|
|
virSetCloseExec(*ttyprimary) < 0)
|
2013-05-09 18:59:04 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2020-06-17 09:48:34 +00:00
|
|
|
/* While Linux supports tcgetattr on either the primary or the
|
|
|
|
* secondary, Solaris requires it to be on the secondary. */
|
2013-05-09 18:59:04 +00:00
|
|
|
if (rawmode) {
|
|
|
|
struct termios ttyAttr;
|
2020-06-17 09:48:34 +00:00
|
|
|
if (tcgetattr(secondary, &ttyAttr) < 0)
|
2013-05-09 18:59:04 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
cfmakeraw(&ttyAttr);
|
|
|
|
|
2020-06-17 09:48:34 +00:00
|
|
|
if (tcsetattr(secondary, TCSADRAIN, &ttyAttr) < 0)
|
2013-05-09 18:59:04 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2020-06-17 09:48:34 +00:00
|
|
|
/* ttyname_r on the secondary is required by POSIX, while ptsname_r on
|
|
|
|
* the primary is a glibc extension, and the POSIX ptsname is not
|
2013-05-09 18:59:04 +00:00
|
|
|
* thread-safe. Since openpty gave us both descriptors, guess
|
|
|
|
* which way we will determine the name? :) */
|
|
|
|
if (ttyName) {
|
|
|
|
/* Initial guess of 64 is generally sufficient; rely on ERANGE
|
|
|
|
* to tell us if we need to grow. */
|
|
|
|
size_t len = 64;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (VIR_ALLOC_N(name, len) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2020-06-17 09:48:34 +00:00
|
|
|
while ((rc = ttyname_r(secondary, name, len)) == ERANGE) {
|
2013-05-09 18:59:04 +00:00
|
|
|
if (VIR_RESIZE_N(name, len, len, len) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (rc != 0) {
|
|
|
|
errno = rc;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
*ttyName = name;
|
|
|
|
name = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
cleanup:
|
2013-05-09 18:59:04 +00:00
|
|
|
if (ret != 0)
|
2020-06-17 09:48:34 +00:00
|
|
|
VIR_FORCE_CLOSE(*ttyprimary);
|
|
|
|
VIR_FORCE_CLOSE(secondary);
|
2013-05-09 18:59:04 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#else /* WIN32 */
|
|
|
|
int
|
2020-06-17 09:48:34 +00:00
|
|
|
virFileOpenTty(int *ttyprimary G_GNUC_UNUSED,
|
2019-10-14 12:45:33 +00:00
|
|
|
char **ttyName G_GNUC_UNUSED,
|
|
|
|
int rawmode G_GNUC_UNUSED)
|
2013-05-09 18:59:04 +00:00
|
|
|
{
|
2020-01-17 16:00:27 +00:00
|
|
|
/* mingw completely lacks pseudo-terminals */
|
2013-05-09 18:59:04 +00:00
|
|
|
errno = ENOSYS;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif /* WIN32 */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Creates an absolute path for a potentially relative path.
|
|
|
|
* Return 0 if the path was not relative, or on success.
|
|
|
|
* Return -1 on error.
|
|
|
|
*
|
|
|
|
* You must free the result.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virFileAbsPath(const char *path, char **abspath)
|
|
|
|
{
|
|
|
|
if (path[0] == '/') {
|
2019-10-20 11:49:46 +00:00
|
|
|
*abspath = g_strdup(path);
|
2013-05-09 18:59:04 +00:00
|
|
|
} else {
|
2019-12-20 16:51:16 +00:00
|
|
|
g_autofree char *buf = g_get_current_dir();
|
2013-05-09 18:59:04 +00:00
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
*abspath = g_strdup_printf("%s/%s", buf, path);
|
2013-05-09 18:59:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove spurious / characters from a path. The result must be freed */
|
|
|
|
char *
|
|
|
|
virFileSanitizePath(const char *path)
|
|
|
|
{
|
|
|
|
const char *cur = path;
|
2015-04-08 08:57:07 +00:00
|
|
|
char *uri;
|
2013-05-09 18:59:04 +00:00
|
|
|
char *cleanpath;
|
|
|
|
int idx = 0;
|
|
|
|
|
2019-10-20 11:49:46 +00:00
|
|
|
cleanpath = g_strdup(path);
|
2013-05-09 18:59:04 +00:00
|
|
|
|
2015-04-08 08:57:07 +00:00
|
|
|
/* don't sanitize URIs - rfc3986 states that two slashes may lead to a
|
|
|
|
* different resource, thus removing them would possibly change the path */
|
|
|
|
if ((uri = strstr(path, "://")) && strchr(path, '/') > uri)
|
|
|
|
return cleanpath;
|
|
|
|
|
2013-05-09 18:59:04 +00:00
|
|
|
/* Need to sanitize:
|
|
|
|
* // -> //
|
|
|
|
* /// -> /
|
|
|
|
* /../foo -> /../foo
|
|
|
|
* /foo///bar/ -> /foo/bar
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Starting with // is valid posix, but ///foo == /foo */
|
|
|
|
if (cur[0] == '/' && cur[1] == '/' && cur[2] != '/') {
|
|
|
|
idx = 2;
|
|
|
|
cur += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sanitize path in place */
|
|
|
|
while (*cur != '\0') {
|
|
|
|
if (*cur != '/') {
|
|
|
|
cleanpath[idx++] = *cur++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Skip all extra / */
|
|
|
|
while (*++cur == '/')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Don't add a trailing / */
|
|
|
|
if (idx != 0 && *cur == '\0')
|
|
|
|
break;
|
|
|
|
|
|
|
|
cleanpath[idx++] = '/';
|
|
|
|
}
|
|
|
|
cleanpath[idx] = '\0';
|
|
|
|
|
|
|
|
return cleanpath;
|
|
|
|
}
|
2013-05-17 14:11:24 +00:00
|
|
|
|
2018-05-03 07:55:19 +00:00
|
|
|
/**
|
|
|
|
* virFileCanonicalizePath:
|
|
|
|
*
|
|
|
|
* Returns the canonical representation of @path.
|
|
|
|
*
|
|
|
|
* The returned string must be freed after use.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
virFileCanonicalizePath(const char *path)
|
|
|
|
{
|
2019-12-23 14:21:54 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
/* Does not resolve symlinks, only expands . & .. & repeated /.
|
|
|
|
* It will never fail, so sanitize errno to indicate success */
|
|
|
|
errno = 0;
|
|
|
|
return g_canonicalize_filename(path, NULL);
|
|
|
|
#else
|
|
|
|
return realpath(path, NULL); /* exempt from syntax-check */
|
|
|
|
#endif
|
2018-05-03 07:55:19 +00:00
|
|
|
}
|
|
|
|
|
2016-05-12 11:05:37 +00:00
|
|
|
/**
|
|
|
|
* virFileRemoveLastComponent:
|
|
|
|
*
|
|
|
|
* For given path cut off the last component. If there's no dir
|
|
|
|
* separator (whole path is one file name), @path is turned into
|
|
|
|
* an empty string.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
virFileRemoveLastComponent(char *path)
|
|
|
|
{
|
|
|
|
char *tmp;
|
|
|
|
|
2019-12-18 20:11:43 +00:00
|
|
|
if ((tmp = strrchr(path, G_DIR_SEPARATOR)))
|
2016-05-12 11:05:37 +00:00
|
|
|
tmp[1] = '\0';
|
|
|
|
else
|
|
|
|
path[0] = '\0';
|
|
|
|
}
|
2013-05-17 14:11:24 +00:00
|
|
|
|
2014-03-29 20:15:33 +00:00
|
|
|
#ifdef __linux__
|
|
|
|
|
|
|
|
# ifndef NFS_SUPER_MAGIC
|
|
|
|
# define NFS_SUPER_MAGIC 0x6969
|
|
|
|
# endif
|
|
|
|
# ifndef OCFS2_SUPER_MAGIC
|
|
|
|
# define OCFS2_SUPER_MAGIC 0x7461636f
|
|
|
|
# endif
|
|
|
|
# ifndef GFS2_MAGIC
|
|
|
|
# define GFS2_MAGIC 0x01161970
|
|
|
|
# endif
|
|
|
|
# ifndef AFS_FS_MAGIC
|
|
|
|
# define AFS_FS_MAGIC 0x6B414653
|
|
|
|
# endif
|
|
|
|
# ifndef SMB_SUPER_MAGIC
|
|
|
|
# define SMB_SUPER_MAGIC 0x517B
|
|
|
|
# endif
|
|
|
|
# ifndef CIFS_SUPER_MAGIC
|
|
|
|
# define CIFS_SUPER_MAGIC 0xFF534D42
|
|
|
|
# endif
|
2014-07-23 15:37:17 +00:00
|
|
|
# ifndef HUGETLBFS_MAGIC
|
|
|
|
# define HUGETLBFS_MAGIC 0x958458f6
|
|
|
|
# endif
|
2018-09-27 14:19:31 +00:00
|
|
|
# ifndef FUSE_SUPER_MAGIC
|
|
|
|
# define FUSE_SUPER_MAGIC 0x65735546
|
|
|
|
# endif
|
2019-01-24 08:52:42 +00:00
|
|
|
# ifndef CEPH_SUPER_MAGIC
|
|
|
|
# define CEPH_SUPER_MAGIC 0x00C36400
|
|
|
|
# endif
|
2019-02-25 18:19:03 +00:00
|
|
|
# ifndef GPFS_SUPER_MAGIC
|
|
|
|
# define GPFS_SUPER_MAGIC 0x47504653
|
|
|
|
# endif
|
2019-05-28 12:55:29 +00:00
|
|
|
# ifndef QB_MAGIC
|
|
|
|
# define QB_MAGIC 0x51626d6e
|
|
|
|
# endif
|
2018-09-27 14:19:31 +00:00
|
|
|
|
2019-11-26 16:29:47 +00:00
|
|
|
# define VIR_ACFS_MAGIC 0x61636673
|
|
|
|
|
2018-09-27 14:19:31 +00:00
|
|
|
# define PROC_MOUNTS "/proc/mounts"
|
|
|
|
|
|
|
|
static int
|
|
|
|
virFileIsSharedFixFUSE(const char *path,
|
2018-10-10 08:04:04 +00:00
|
|
|
long long *f_type)
|
2018-09-27 14:19:31 +00:00
|
|
|
{
|
|
|
|
FILE *f = NULL;
|
|
|
|
struct mntent mb;
|
|
|
|
char mntbuf[1024];
|
2018-10-09 11:08:07 +00:00
|
|
|
char *mntDir = NULL;
|
|
|
|
char *mntType = NULL;
|
2018-10-18 12:57:19 +00:00
|
|
|
char *canonPath = NULL;
|
2018-10-09 11:08:07 +00:00
|
|
|
size_t maxMatching = 0;
|
2018-09-27 14:19:31 +00:00
|
|
|
int ret = -1;
|
|
|
|
|
2018-10-18 12:57:19 +00:00
|
|
|
if (!(canonPath = virFileCanonicalizePath(path))) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("unable to canonicalize %s"),
|
|
|
|
path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_DEBUG("Path canonicalization: %s->%s", path, canonPath);
|
|
|
|
|
2018-09-27 14:19:31 +00:00
|
|
|
if (!(f = setmntent(PROC_MOUNTS, "r"))) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Unable to open %s"),
|
|
|
|
PROC_MOUNTS);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (getmntent_r(f, &mb, mntbuf, sizeof(mntbuf))) {
|
2018-10-09 11:08:07 +00:00
|
|
|
const char *p;
|
|
|
|
size_t len = strlen(mb.mnt_dir);
|
2018-09-27 14:19:31 +00:00
|
|
|
|
2018-10-18 12:57:19 +00:00
|
|
|
if (!(p = STRSKIP(canonPath, mb.mnt_dir)))
|
2018-10-09 11:08:07 +00:00
|
|
|
continue;
|
2018-09-27 14:19:31 +00:00
|
|
|
|
2018-10-09 11:08:07 +00:00
|
|
|
if (*(p - 1) != '/' && *p != '/' && *p != '\0')
|
|
|
|
continue;
|
2018-09-27 14:19:31 +00:00
|
|
|
|
2018-10-09 11:08:07 +00:00
|
|
|
if (len > maxMatching) {
|
|
|
|
maxMatching = len;
|
|
|
|
VIR_FREE(mntType);
|
|
|
|
VIR_FREE(mntDir);
|
2019-10-20 11:49:46 +00:00
|
|
|
mntDir = g_strdup(mb.mnt_dir);
|
|
|
|
mntType = g_strdup(mb.mnt_type);
|
2018-09-27 14:19:31 +00:00
|
|
|
}
|
2018-10-09 11:08:07 +00:00
|
|
|
}
|
2018-09-27 14:19:31 +00:00
|
|
|
|
2018-10-09 11:08:07 +00:00
|
|
|
if (STREQ_NULLABLE(mntType, "fuse.glusterfs")) {
|
|
|
|
VIR_DEBUG("Found gluster FUSE mountpoint=%s for path=%s. "
|
2018-10-18 12:57:19 +00:00
|
|
|
"Fixing shared FS type", mntDir, canonPath);
|
2018-10-09 11:08:07 +00:00
|
|
|
*f_type = GFS2_MAGIC;
|
2019-05-28 12:55:29 +00:00
|
|
|
} else if (STREQ_NULLABLE(mntType, "fuse.quobyte")) {
|
|
|
|
VIR_DEBUG("Found Quobyte FUSE mountpoint=%s for path=%s. "
|
|
|
|
"Fixing shared FS type", mntDir, canonPath);
|
|
|
|
*f_type = QB_MAGIC;
|
2018-10-09 11:08:07 +00:00
|
|
|
}
|
2018-09-27 14:19:31 +00:00
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
2018-10-18 12:57:19 +00:00
|
|
|
VIR_FREE(canonPath);
|
2018-10-09 11:08:07 +00:00
|
|
|
VIR_FREE(mntType);
|
|
|
|
VIR_FREE(mntDir);
|
2018-09-27 14:19:31 +00:00
|
|
|
endmntent(f);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-03-29 20:15:33 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
virFileIsSharedFSType(const char *path,
|
|
|
|
int fstypes)
|
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *dirpath = NULL;
|
2018-10-09 13:23:10 +00:00
|
|
|
char *p = NULL;
|
2014-03-29 20:15:33 +00:00
|
|
|
struct statfs sb;
|
|
|
|
int statfs_ret;
|
2018-10-10 08:04:04 +00:00
|
|
|
long long f_type = 0;
|
2014-03-29 20:15:33 +00:00
|
|
|
|
2019-10-20 11:49:46 +00:00
|
|
|
dirpath = g_strdup(path);
|
2014-03-29 20:15:33 +00:00
|
|
|
|
2018-10-09 13:23:10 +00:00
|
|
|
statfs_ret = statfs(dirpath, &sb);
|
2014-03-29 20:15:33 +00:00
|
|
|
|
2018-10-09 13:23:10 +00:00
|
|
|
while ((statfs_ret < 0) && (p != dirpath)) {
|
2014-03-29 20:15:33 +00:00
|
|
|
/* Try less and less of the path until we get to a
|
|
|
|
* directory we can stat. Even if we don't have 'x'
|
|
|
|
* permission on any directory in the path on the NFS
|
|
|
|
* server (assuming it's NFS), we will be able to stat the
|
|
|
|
* mount point, and that will properly tell us if the
|
|
|
|
* fstype is NFS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((p = strrchr(dirpath, '/')) == NULL) {
|
|
|
|
virReportSystemError(EINVAL,
|
2018-09-19 08:38:14 +00:00
|
|
|
_("Invalid relative path '%s'"), path);
|
2014-03-29 20:15:33 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p == dirpath)
|
|
|
|
*(p+1) = '\0';
|
|
|
|
else
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
statfs_ret = statfs(dirpath, &sb);
|
2018-10-09 13:23:10 +00:00
|
|
|
}
|
2014-03-29 20:15:33 +00:00
|
|
|
|
|
|
|
if (statfs_ret < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("cannot determine filesystem for '%s'"),
|
|
|
|
path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-10-10 08:04:04 +00:00
|
|
|
f_type = sb.f_type;
|
|
|
|
|
|
|
|
if (f_type == FUSE_SUPER_MAGIC) {
|
2018-09-27 14:19:31 +00:00
|
|
|
VIR_DEBUG("Found FUSE mount for path=%s. Trying to fix it", path);
|
2018-10-10 08:04:04 +00:00
|
|
|
virFileIsSharedFixFUSE(path, &f_type);
|
2018-09-27 14:19:31 +00:00
|
|
|
}
|
|
|
|
|
2014-03-29 20:15:33 +00:00
|
|
|
VIR_DEBUG("Check if path %s with FS magic %lld is shared",
|
2018-10-10 08:04:04 +00:00
|
|
|
path, f_type);
|
2014-03-29 20:15:33 +00:00
|
|
|
|
|
|
|
if ((fstypes & VIR_FILE_SHFS_NFS) &&
|
2018-10-10 08:04:04 +00:00
|
|
|
(f_type == NFS_SUPER_MAGIC))
|
2014-03-29 20:15:33 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
if ((fstypes & VIR_FILE_SHFS_GFS2) &&
|
2018-10-10 08:04:04 +00:00
|
|
|
(f_type == GFS2_MAGIC))
|
2014-03-29 20:15:33 +00:00
|
|
|
return 1;
|
|
|
|
if ((fstypes & VIR_FILE_SHFS_OCFS) &&
|
2018-10-10 08:04:04 +00:00
|
|
|
(f_type == OCFS2_SUPER_MAGIC))
|
2014-03-29 20:15:33 +00:00
|
|
|
return 1;
|
|
|
|
if ((fstypes & VIR_FILE_SHFS_AFS) &&
|
2018-10-10 08:04:04 +00:00
|
|
|
(f_type == AFS_FS_MAGIC))
|
2014-03-29 20:15:33 +00:00
|
|
|
return 1;
|
|
|
|
if ((fstypes & VIR_FILE_SHFS_SMB) &&
|
2018-10-10 08:04:04 +00:00
|
|
|
(f_type == SMB_SUPER_MAGIC))
|
2014-03-29 20:15:33 +00:00
|
|
|
return 1;
|
|
|
|
if ((fstypes & VIR_FILE_SHFS_CIFS) &&
|
2018-10-10 08:04:04 +00:00
|
|
|
(f_type == CIFS_SUPER_MAGIC))
|
2014-03-29 20:15:33 +00:00
|
|
|
return 1;
|
2019-01-24 08:52:42 +00:00
|
|
|
if ((fstypes & VIR_FILE_SHFS_CEPH) &&
|
|
|
|
(f_type == CEPH_SUPER_MAGIC))
|
|
|
|
return 1;
|
2019-02-25 18:19:03 +00:00
|
|
|
if ((fstypes & VIR_FILE_SHFS_GPFS) &&
|
|
|
|
(f_type == GPFS_SUPER_MAGIC))
|
|
|
|
return 1;
|
2019-05-28 12:55:29 +00:00
|
|
|
if ((fstypes & VIR_FILE_SHFS_QB) &&
|
|
|
|
(f_type == QB_MAGIC))
|
|
|
|
return 1;
|
2019-11-26 16:29:47 +00:00
|
|
|
if ((fstypes & VIR_FILE_SHFS_ACFS) &&
|
|
|
|
(f_type == VIR_ACFS_MAGIC))
|
|
|
|
return 1;
|
2014-03-29 20:15:33 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2014-07-23 15:37:17 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
virFileGetHugepageSize(const char *path,
|
|
|
|
unsigned long long *size)
|
|
|
|
{
|
|
|
|
struct statfs fs;
|
|
|
|
|
|
|
|
if (statfs(path, &fs) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("cannot determine filesystem for '%s'"),
|
|
|
|
path);
|
2019-10-21 18:19:04 +00:00
|
|
|
return -1;
|
2014-07-23 15:37:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fs.f_type != HUGETLBFS_MAGIC) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("not a hugetlbfs mount: '%s'"),
|
|
|
|
path);
|
2019-10-21 18:19:04 +00:00
|
|
|
return -1;
|
2014-07-23 15:37:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*size = fs.f_bsize / 1024; /* we are storing size in KiB */
|
2019-10-21 18:19:04 +00:00
|
|
|
|
|
|
|
return 0;
|
2014-07-23 15:37:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# define PROC_MEMINFO "/proc/meminfo"
|
|
|
|
# define HUGEPAGESIZE_STR "Hugepagesize:"
|
|
|
|
|
|
|
|
static int
|
|
|
|
virFileGetDefaultHugepageSize(unsigned long long *size)
|
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *meminfo = NULL;
|
2018-07-13 17:54:48 +00:00
|
|
|
char *c;
|
|
|
|
char *n;
|
|
|
|
char *unit;
|
2014-07-23 15:37:17 +00:00
|
|
|
|
|
|
|
if (virFileReadAll(PROC_MEMINFO, 4096, &meminfo) < 0)
|
2018-07-13 17:54:48 +00:00
|
|
|
return -1;
|
2014-07-23 15:37:17 +00:00
|
|
|
|
|
|
|
if (!(c = strstr(meminfo, HUGEPAGESIZE_STR))) {
|
2014-08-10 10:42:37 +00:00
|
|
|
virReportError(VIR_ERR_NO_SUPPORT,
|
|
|
|
_("%s not found in %s"),
|
|
|
|
HUGEPAGESIZE_STR,
|
2014-07-23 15:37:17 +00:00
|
|
|
PROC_MEMINFO);
|
2018-07-13 17:54:48 +00:00
|
|
|
return -1;
|
2014-07-23 15:37:17 +00:00
|
|
|
}
|
|
|
|
c += strlen(HUGEPAGESIZE_STR);
|
|
|
|
|
|
|
|
if ((n = strchr(c, '\n'))) {
|
|
|
|
/* Cut off the rest of the meminfo file */
|
|
|
|
*n = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virStrToLong_ull(c, &unit, 10, size) < 0 || STRNEQ(unit, " kB")) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unable to parse %s %s"),
|
|
|
|
HUGEPAGESIZE_STR, c);
|
2018-07-13 17:54:48 +00:00
|
|
|
return -1;
|
2014-07-23 15:37:17 +00:00
|
|
|
}
|
|
|
|
|
2018-07-13 17:54:48 +00:00
|
|
|
return 0;
|
2014-07-23 15:37:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
virFileFindHugeTLBFS(virHugeTLBFSPtr *ret_fs,
|
|
|
|
size_t *ret_nfs)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
FILE *f = NULL;
|
|
|
|
struct mntent mb;
|
|
|
|
char mntbuf[1024];
|
|
|
|
virHugeTLBFSPtr fs = NULL;
|
|
|
|
size_t nfs = 0;
|
2014-08-06 08:37:48 +00:00
|
|
|
unsigned long long default_hugepagesz = 0;
|
2014-07-23 15:37:17 +00:00
|
|
|
|
|
|
|
if (!(f = setmntent(PROC_MOUNTS, "r"))) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Unable to open %s"),
|
|
|
|
PROC_MOUNTS);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (getmntent_r(f, &mb, mntbuf, sizeof(mntbuf))) {
|
|
|
|
virHugeTLBFSPtr tmp;
|
|
|
|
|
|
|
|
if (STRNEQ(mb.mnt_type, "hugetlbfs"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (VIR_EXPAND_N(fs, nfs, 1) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
tmp = &fs[nfs - 1];
|
|
|
|
|
2019-10-20 11:49:46 +00:00
|
|
|
tmp->mnt_dir = g_strdup(mb.mnt_dir);
|
2014-07-23 15:37:17 +00:00
|
|
|
|
|
|
|
if (virFileGetHugepageSize(tmp->mnt_dir, &tmp->size) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2014-08-06 08:37:48 +00:00
|
|
|
if (!default_hugepagesz &&
|
|
|
|
virFileGetDefaultHugepageSize(&default_hugepagesz) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2014-07-23 15:37:17 +00:00
|
|
|
tmp->deflt = tmp->size == default_hugepagesz;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ret_fs = fs;
|
|
|
|
*ret_nfs = nfs;
|
|
|
|
fs = NULL;
|
|
|
|
nfs = 0;
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
endmntent(f);
|
|
|
|
while (nfs)
|
|
|
|
VIR_FREE(fs[--nfs].mnt_dir);
|
|
|
|
VIR_FREE(fs);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* defined __linux__ */
|
|
|
|
|
2019-10-14 12:45:33 +00:00
|
|
|
int virFileIsSharedFSType(const char *path G_GNUC_UNUSED,
|
|
|
|
int fstypes G_GNUC_UNUSED)
|
2014-03-29 20:15:33 +00:00
|
|
|
{
|
|
|
|
/* XXX implement me :-) */
|
|
|
|
return 0;
|
|
|
|
}
|
2014-07-23 15:37:17 +00:00
|
|
|
|
|
|
|
int
|
2019-10-14 12:45:33 +00:00
|
|
|
virFileGetHugepageSize(const char *path G_GNUC_UNUSED,
|
|
|
|
unsigned long long *size G_GNUC_UNUSED)
|
2014-07-23 15:37:17 +00:00
|
|
|
{
|
|
|
|
/* XXX implement me :-) */
|
|
|
|
virReportUnsupportedError();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2019-10-14 12:45:33 +00:00
|
|
|
virFileFindHugeTLBFS(virHugeTLBFSPtr *ret_fs G_GNUC_UNUSED,
|
|
|
|
size_t *ret_nfs G_GNUC_UNUSED)
|
2014-07-23 15:37:17 +00:00
|
|
|
{
|
|
|
|
/* XXX implement me :-) */
|
|
|
|
virReportUnsupportedError();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif /* defined __linux__ */
|
2014-03-29 20:15:33 +00:00
|
|
|
|
2019-03-29 15:10:09 +00:00
|
|
|
/**
|
|
|
|
* virFileGetDefaultHugepage:
|
|
|
|
* @fs: array of hugetlbfs mount points
|
|
|
|
* @nfs: number of items in @fs
|
|
|
|
*
|
|
|
|
* In the passed array of hugetlbfs mount points @fs find the
|
|
|
|
* default one. It's the one which has no '-o pagesize'.
|
|
|
|
*
|
|
|
|
* Returns: default hugepage, or
|
|
|
|
* NULL if none found
|
|
|
|
*/
|
|
|
|
virHugeTLBFSPtr
|
|
|
|
virFileGetDefaultHugepage(virHugeTLBFSPtr fs,
|
|
|
|
size_t nfs)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < nfs; i++) {
|
|
|
|
if (fs[i].deflt)
|
|
|
|
return &fs[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-03-29 20:15:33 +00:00
|
|
|
int virFileIsSharedFS(const char *path)
|
|
|
|
{
|
|
|
|
return virFileIsSharedFSType(path,
|
|
|
|
VIR_FILE_SHFS_NFS |
|
|
|
|
VIR_FILE_SHFS_GFS2 |
|
|
|
|
VIR_FILE_SHFS_OCFS |
|
|
|
|
VIR_FILE_SHFS_AFS |
|
|
|
|
VIR_FILE_SHFS_SMB |
|
2019-01-24 08:52:42 +00:00
|
|
|
VIR_FILE_SHFS_CIFS |
|
2019-02-25 18:19:03 +00:00
|
|
|
VIR_FILE_SHFS_CEPH |
|
2019-05-28 12:55:29 +00:00
|
|
|
VIR_FILE_SHFS_GPFS|
|
2019-11-26 16:29:47 +00:00
|
|
|
VIR_FILE_SHFS_QB |
|
|
|
|
VIR_FILE_SHFS_ACFS);
|
2014-03-29 20:15:33 +00:00
|
|
|
}
|
2016-11-10 15:17:48 +00:00
|
|
|
|
|
|
|
|
2020-09-01 11:27:44 +00:00
|
|
|
#if defined(__linux__) && defined(WITH_SYS_MOUNT_H)
|
2016-11-10 15:17:48 +00:00
|
|
|
int
|
|
|
|
virFileSetupDev(const char *path,
|
|
|
|
const char *mount_options)
|
|
|
|
{
|
|
|
|
const unsigned long mount_flags = MS_NOSUID;
|
|
|
|
const char *mount_fs = "tmpfs";
|
|
|
|
|
|
|
|
if (virFileMakePath(path) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Failed to make path %s"), path);
|
2019-10-21 18:19:04 +00:00
|
|
|
return -1;
|
2016-11-10 15:17:48 +00:00
|
|
|
}
|
|
|
|
|
2017-09-25 10:43:33 +00:00
|
|
|
VIR_DEBUG("Mount devfs on %s type=tmpfs flags=0x%lx, opts=%s",
|
2016-11-10 15:17:48 +00:00
|
|
|
path, mount_flags, mount_options);
|
|
|
|
if (mount("devfs", path, mount_fs, mount_flags, mount_options) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Failed to mount devfs on %s type %s (%s)"),
|
|
|
|
path, mount_fs, mount_options);
|
2019-10-21 18:19:04 +00:00
|
|
|
return -1;
|
2016-11-10 15:17:48 +00:00
|
|
|
}
|
|
|
|
|
2019-10-21 18:19:04 +00:00
|
|
|
return 0;
|
2016-11-10 15:17:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
virFileBindMountDevice(const char *src,
|
|
|
|
const char *dst)
|
|
|
|
{
|
2019-10-04 19:01:49 +00:00
|
|
|
if (!virFileExists(dst)) {
|
|
|
|
if (virFileIsDir(src)) {
|
|
|
|
if (virFileMakePath(dst) < 0) {
|
|
|
|
virReportSystemError(errno, _("Unable to make dir %s"), dst);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (virFileTouch(dst, 0666) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2016-11-10 15:17:48 +00:00
|
|
|
|
|
|
|
if (mount(src, dst, "none", MS_BIND, NULL) < 0) {
|
|
|
|
virReportSystemError(errno, _("Failed to bind %s on to %s"), src,
|
|
|
|
dst);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-01-11 10:29:00 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
virFileMoveMount(const char *src,
|
|
|
|
const char *dst)
|
|
|
|
{
|
|
|
|
const unsigned long mount_flags = MS_MOVE;
|
|
|
|
|
2018-06-26 03:18:21 +00:00
|
|
|
if (mount(src, dst, "none", mount_flags, NULL) < 0) {
|
2017-01-11 10:29:00 +00:00
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Unable to move %s mount to %s"),
|
|
|
|
src, dst);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-01 11:27:44 +00:00
|
|
|
#else /* !defined(__linux__) || !defined(WITH_SYS_MOUNT_H) */
|
2016-11-10 15:17:48 +00:00
|
|
|
|
|
|
|
int
|
2019-10-14 12:45:33 +00:00
|
|
|
virFileSetupDev(const char *path G_GNUC_UNUSED,
|
|
|
|
const char *mount_options G_GNUC_UNUSED)
|
2016-11-10 15:17:48 +00:00
|
|
|
{
|
|
|
|
virReportSystemError(ENOSYS, "%s",
|
|
|
|
_("mount is not supported on this platform."));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2019-10-14 12:45:33 +00:00
|
|
|
virFileBindMountDevice(const char *src G_GNUC_UNUSED,
|
|
|
|
const char *dst G_GNUC_UNUSED)
|
2016-11-10 15:17:48 +00:00
|
|
|
{
|
|
|
|
virReportSystemError(ENOSYS, "%s",
|
|
|
|
_("mount is not supported on this platform."));
|
|
|
|
return -1;
|
|
|
|
}
|
2017-01-11 10:29:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
int
|
2019-10-14 12:45:33 +00:00
|
|
|
virFileMoveMount(const char *src G_GNUC_UNUSED,
|
|
|
|
const char *dst G_GNUC_UNUSED)
|
2017-01-11 10:29:00 +00:00
|
|
|
{
|
|
|
|
virReportSystemError(ENOSYS, "%s",
|
|
|
|
_("mount move is not supported on this platform."));
|
|
|
|
return -1;
|
|
|
|
}
|
2020-09-01 11:27:44 +00:00
|
|
|
#endif /* !defined(__linux__) || !defined(WITH_SYS_MOUNT_H) */
|
2016-11-22 10:14:08 +00:00
|
|
|
|
|
|
|
|
2020-09-01 11:27:44 +00:00
|
|
|
#if defined(WITH_SYS_ACL_H)
|
2016-11-22 10:14:08 +00:00
|
|
|
int
|
|
|
|
virFileGetACLs(const char *file,
|
|
|
|
void **acl)
|
|
|
|
{
|
|
|
|
if (!(*acl = acl_get_file(file, ACL_TYPE_ACCESS)))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
virFileSetACLs(const char *file,
|
|
|
|
void *acl)
|
|
|
|
{
|
|
|
|
if (acl_set_file(file, ACL_TYPE_ACCESS, acl) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
virFileFreeACLs(void **acl)
|
|
|
|
{
|
|
|
|
acl_free(*acl);
|
|
|
|
*acl = NULL;
|
|
|
|
}
|
|
|
|
|
2020-09-01 11:27:44 +00:00
|
|
|
#else /* !defined(WITH_SYS_ACL_H) */
|
2016-11-22 10:14:08 +00:00
|
|
|
|
|
|
|
int
|
2019-10-14 12:45:33 +00:00
|
|
|
virFileGetACLs(const char *file G_GNUC_UNUSED,
|
|
|
|
void **acl G_GNUC_UNUSED)
|
2016-11-22 10:14:08 +00:00
|
|
|
{
|
|
|
|
errno = ENOTSUP;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2019-10-14 12:45:33 +00:00
|
|
|
virFileSetACLs(const char *file G_GNUC_UNUSED,
|
|
|
|
void *acl G_GNUC_UNUSED)
|
2016-11-22 10:14:08 +00:00
|
|
|
{
|
|
|
|
errno = ENOTSUP;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
virFileFreeACLs(void **acl)
|
|
|
|
{
|
|
|
|
*acl = NULL;
|
|
|
|
}
|
|
|
|
|
2020-09-01 11:27:44 +00:00
|
|
|
#endif /* !defined(WITH_SYS_ACL_H) */
|
2016-11-22 10:14:08 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
virFileCopyACLs(const char *src,
|
|
|
|
const char *dst)
|
|
|
|
{
|
|
|
|
void *acl = NULL;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
if (virFileGetACLs(src, &acl) < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (virFileSetACLs(dst, acl) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
virFileFreeACLs(&acl);
|
|
|
|
return ret;
|
|
|
|
}
|
2017-02-07 09:19:06 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* virFileComparePaths:
|
|
|
|
* @p1: source path 1
|
|
|
|
* @p2: source path 2
|
|
|
|
*
|
|
|
|
* Compares two paths for equality. To do so, it first canonicalizes both paths
|
|
|
|
* to resolve all symlinks and discard relative path components. If symlinks
|
|
|
|
* resolution or path canonicalization fails, plain string equality of @p1
|
|
|
|
* and @p2 is performed.
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* 1 : Equal
|
|
|
|
* 0 : Non-Equal
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virFileComparePaths(const char *p1, const char *p2)
|
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *res1 = NULL;
|
|
|
|
g_autofree char *res2 = NULL;
|
2017-02-07 09:19:06 +00:00
|
|
|
|
|
|
|
/* Assume p1 and p2 are symlinks, so try to resolve and canonicalize them.
|
|
|
|
* Canonicalization fails for example on file systems names like 'proc' or
|
|
|
|
* 'sysfs', since they're no real paths so fallback to plain string
|
|
|
|
* comparison.
|
|
|
|
*/
|
|
|
|
ignore_value(virFileResolveLink(p1, &res1));
|
2019-10-18 13:08:21 +00:00
|
|
|
if (!res1)
|
|
|
|
res1 = g_strdup(p1);
|
2017-02-07 09:19:06 +00:00
|
|
|
|
|
|
|
ignore_value(virFileResolveLink(p2, &res2));
|
2019-10-18 13:08:21 +00:00
|
|
|
if (!res2)
|
|
|
|
res2 = g_strdup(p2);
|
2016-06-16 08:42:47 +00:00
|
|
|
|
2018-07-13 17:54:48 +00:00
|
|
|
return STREQ_NULLABLE(res1, res2);
|
2016-06-16 08:42:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-01 11:27:44 +00:00
|
|
|
#if WITH_DECL_SEEK_HOLE
|
2016-06-16 08:42:47 +00:00
|
|
|
/**
|
|
|
|
* virFileInData:
|
|
|
|
* @fd: file to check
|
|
|
|
* @inData: true if current position in the @fd is in data section
|
|
|
|
* @length: amount of bytes until the end of the current section
|
|
|
|
*
|
|
|
|
* With sparse files not every extent has to be physically stored on
|
|
|
|
* the disk. This results in so called data or hole sections. This
|
|
|
|
* function checks whether the current position in the file @fd is
|
|
|
|
* in a data section (@inData = 1) or in a hole (@inData = 0). Also,
|
|
|
|
* it sets @length to match the number of bytes remaining until the
|
|
|
|
* end of the current section.
|
|
|
|
*
|
|
|
|
* As a special case, there is an implicit hole at the end of any
|
|
|
|
* file. In this case, the function sets @inData = 0, @length = 0.
|
|
|
|
*
|
|
|
|
* Upon its return, the position in the @fd is left unchanged, i.e.
|
|
|
|
* despite this function lseek()-ing back and forth it always
|
|
|
|
* restores the original position in the file.
|
|
|
|
*
|
|
|
|
* NB, @length is type of long long because it corresponds to off_t
|
|
|
|
* the best.
|
|
|
|
*
|
|
|
|
* Returns 0 on success,
|
|
|
|
* -1 otherwise.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virFileInData(int fd,
|
|
|
|
int *inData,
|
|
|
|
long long *length)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
off_t cur, data, hole, end;
|
|
|
|
|
|
|
|
/* Get current position */
|
|
|
|
cur = lseek(fd, 0, SEEK_CUR);
|
|
|
|
if (cur == (off_t) -1) {
|
|
|
|
virReportSystemError(errno, "%s",
|
|
|
|
_("Unable to get current position in file"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now try to get data and hole offsets */
|
|
|
|
data = lseek(fd, cur, SEEK_DATA);
|
|
|
|
|
|
|
|
/* There are four options:
|
|
|
|
* 1) data == cur; @cur is in data
|
|
|
|
* 2) data > cur; @cur is in a hole, next data at @data
|
|
|
|
* 3) data < 0, errno = ENXIO; either @cur is in trailing hole, or @cur is beyond EOF.
|
|
|
|
* 4) data < 0, errno != ENXIO; we learned nothing
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (data == (off_t) -1) {
|
|
|
|
/* cases 3 and 4 */
|
|
|
|
if (errno != ENXIO) {
|
|
|
|
virReportSystemError(errno, "%s",
|
|
|
|
_("Unable to seek to data"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
*inData = 0;
|
|
|
|
/* There are two situations now. There is always an
|
|
|
|
* implicit hole at EOF. However, there might be a
|
|
|
|
* trailing hole just before EOF too. If that's the case
|
|
|
|
* report it. */
|
|
|
|
if ((end = lseek(fd, 0, SEEK_END)) == (off_t) -1) {
|
|
|
|
virReportSystemError(errno, "%s",
|
|
|
|
_("Unable to seek to EOF"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
*length = end - cur;
|
|
|
|
} else if (data > cur) {
|
|
|
|
/* case 2 */
|
|
|
|
*inData = 0;
|
|
|
|
*length = data - cur;
|
|
|
|
} else {
|
|
|
|
/* case 1 */
|
|
|
|
*inData = 1;
|
|
|
|
|
|
|
|
/* We don't know where does the next hole start. Let's
|
|
|
|
* find out. Here we get the same 4 possibilities as
|
|
|
|
* described above.*/
|
|
|
|
hole = lseek(fd, data, SEEK_HOLE);
|
|
|
|
if (hole == (off_t) -1 || hole == data) {
|
|
|
|
/* cases 1, 3 and 4 */
|
|
|
|
/* Wait a second. The reason why we are here is
|
|
|
|
* because we are in data. But at the same time we
|
|
|
|
* are in a trailing hole? Wut!? Do the best what we
|
|
|
|
* can do here. */
|
|
|
|
virReportSystemError(errno, "%s",
|
|
|
|
_("unable to seek to hole"));
|
|
|
|
goto cleanup;
|
|
|
|
} else {
|
|
|
|
/* case 2 */
|
|
|
|
*length = (hole - data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
/* At any rate, reposition back to where we started. */
|
2018-10-08 15:36:10 +00:00
|
|
|
if (cur != (off_t) -1) {
|
|
|
|
int theerrno = errno;
|
|
|
|
|
|
|
|
if (lseek(fd, cur, SEEK_SET) == (off_t) -1) {
|
|
|
|
virReportSystemError(errno, "%s",
|
|
|
|
_("unable to restore position in file"));
|
|
|
|
ret = -1;
|
|
|
|
if (theerrno == 0)
|
|
|
|
theerrno = errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
errno = theerrno;
|
2017-07-11 08:56:19 +00:00
|
|
|
}
|
2017-02-07 09:19:06 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2017-03-07 09:36:54 +00:00
|
|
|
|
2020-09-01 11:27:44 +00:00
|
|
|
#else /* !WITH_DECL_SEEK_HOLE */
|
2017-05-18 09:55:12 +00:00
|
|
|
|
|
|
|
int
|
2019-10-14 12:45:33 +00:00
|
|
|
virFileInData(int fd G_GNUC_UNUSED,
|
|
|
|
int *inData G_GNUC_UNUSED,
|
|
|
|
long long *length G_GNUC_UNUSED)
|
2017-05-18 09:55:12 +00:00
|
|
|
{
|
|
|
|
errno = ENOSYS;
|
|
|
|
virReportSystemError(errno, "%s",
|
|
|
|
_("sparse files not supported"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-09-01 11:27:44 +00:00
|
|
|
#endif /* !WITH_DECL_SEEK_HOLE */
|
2017-05-18 09:55:12 +00:00
|
|
|
|
2017-03-07 09:36:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* virFileReadValueInt:
|
|
|
|
* @value: pointer to int to be filled in with the value
|
2017-04-05 14:13:52 +00:00
|
|
|
* @format, ...: file to read from
|
2017-03-07 09:36:54 +00:00
|
|
|
*
|
2017-04-05 14:13:52 +00:00
|
|
|
* Read int from @format and put it into @value.
|
2017-03-07 09:36:54 +00:00
|
|
|
*
|
|
|
|
* Return -2 for non-existing file, -1 on other errors and 0 if everything went
|
|
|
|
* fine.
|
|
|
|
*/
|
|
|
|
int
|
2017-04-05 14:13:52 +00:00
|
|
|
virFileReadValueInt(int *value, const char *format, ...)
|
2017-03-07 09:36:54 +00:00
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *str = NULL;
|
|
|
|
g_autofree char *path = NULL;
|
2017-04-05 14:13:52 +00:00
|
|
|
va_list ap;
|
2017-03-07 09:36:54 +00:00
|
|
|
|
2017-04-05 14:13:52 +00:00
|
|
|
va_start(ap, format);
|
2019-10-22 12:11:15 +00:00
|
|
|
path = g_strdup_vprintf(format, ap);
|
2017-04-05 14:13:52 +00:00
|
|
|
va_end(ap);
|
2017-03-07 09:36:54 +00:00
|
|
|
|
2018-07-13 17:54:48 +00:00
|
|
|
if (!virFileExists(path))
|
|
|
|
return -2;
|
2017-04-05 14:13:52 +00:00
|
|
|
|
2020-01-14 13:30:07 +00:00
|
|
|
if (virFileReadAll(path, VIR_INT64_STR_BUFLEN, &str) < 0)
|
2018-07-13 17:54:48 +00:00
|
|
|
return -1;
|
2017-03-07 09:36:54 +00:00
|
|
|
|
2017-03-29 11:48:22 +00:00
|
|
|
virStringTrimOptionalNewline(str);
|
|
|
|
|
|
|
|
if (virStrToLong_i(str, NULL, 10, value) < 0) {
|
2017-03-07 09:36:54 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Invalid integer value '%s' in file '%s'"),
|
|
|
|
str, path);
|
2018-07-13 17:54:48 +00:00
|
|
|
return -1;
|
2017-03-07 09:36:54 +00:00
|
|
|
}
|
|
|
|
|
2018-07-13 17:54:48 +00:00
|
|
|
return 0;
|
2017-03-07 09:36:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virFileReadValueUint:
|
2017-04-05 14:13:52 +00:00
|
|
|
* @value: pointer to int to be filled in with the value
|
|
|
|
* @format, ...: file to read from
|
2017-03-07 09:36:54 +00:00
|
|
|
*
|
2017-04-05 14:13:52 +00:00
|
|
|
* Read unsigned int from @format and put it into @value.
|
2017-03-07 09:36:54 +00:00
|
|
|
*
|
|
|
|
* Return -2 for non-existing file, -1 on other errors and 0 if everything went
|
|
|
|
* fine.
|
|
|
|
*/
|
|
|
|
int
|
2017-04-05 14:13:52 +00:00
|
|
|
virFileReadValueUint(unsigned int *value, const char *format, ...)
|
2017-03-07 09:36:54 +00:00
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *str = NULL;
|
|
|
|
g_autofree char *path = NULL;
|
2017-04-05 14:13:52 +00:00
|
|
|
va_list ap;
|
2017-03-07 09:36:54 +00:00
|
|
|
|
2017-04-05 14:13:52 +00:00
|
|
|
va_start(ap, format);
|
2019-10-22 12:11:15 +00:00
|
|
|
path = g_strdup_vprintf(format, ap);
|
2017-04-05 14:13:52 +00:00
|
|
|
va_end(ap);
|
2017-03-07 09:36:54 +00:00
|
|
|
|
2018-07-13 17:54:48 +00:00
|
|
|
if (!virFileExists(path))
|
|
|
|
return -2;
|
2017-04-05 14:13:52 +00:00
|
|
|
|
2020-01-14 13:30:07 +00:00
|
|
|
if (virFileReadAll(path, VIR_INT64_STR_BUFLEN, &str) < 0)
|
2018-07-13 17:54:48 +00:00
|
|
|
return -1;
|
2017-03-07 09:36:54 +00:00
|
|
|
|
2017-03-29 11:48:22 +00:00
|
|
|
virStringTrimOptionalNewline(str);
|
|
|
|
|
2017-04-05 14:13:52 +00:00
|
|
|
if (virStrToLong_uip(str, NULL, 10, value) < 0) {
|
2017-03-07 09:36:54 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Invalid unsigned integer value '%s' in file '%s'"),
|
|
|
|
str, path);
|
2018-07-13 17:54:48 +00:00
|
|
|
return -1;
|
2017-03-07 09:36:54 +00:00
|
|
|
}
|
|
|
|
|
2018-07-13 17:54:48 +00:00
|
|
|
return 0;
|
2017-04-05 14:13:52 +00:00
|
|
|
}
|
2017-03-07 09:36:54 +00:00
|
|
|
|
2017-04-05 14:13:52 +00:00
|
|
|
|
2020-01-02 10:45:04 +00:00
|
|
|
/**
|
|
|
|
* virFileReadValueUllong:
|
|
|
|
* @value: pointer to unsigned long long to be filled in with the value
|
|
|
|
* @format, ...: file to read from
|
|
|
|
*
|
|
|
|
* Read unsigned int from @format and put it into @value.
|
|
|
|
*
|
|
|
|
* Return -2 for non-existing file, -1 on other errors and 0 if everything went
|
|
|
|
* fine.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virFileReadValueUllong(unsigned long long *value, const char *format, ...)
|
|
|
|
{
|
|
|
|
g_autofree char *str = NULL;
|
|
|
|
g_autofree char *path = NULL;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
path = g_strdup_vprintf(format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
if (!virFileExists(path))
|
|
|
|
return -2;
|
|
|
|
|
2020-01-14 13:30:07 +00:00
|
|
|
if (virFileReadAll(path, VIR_INT64_STR_BUFLEN, &str) < 0)
|
2020-01-02 10:45:04 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
virStringTrimOptionalNewline(str);
|
|
|
|
|
|
|
|
if (virStrToLong_ullp(str, NULL, 10, value) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Invalid unsigned long long value '%s' in file '%s'"),
|
|
|
|
str, path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-05 14:13:52 +00:00
|
|
|
/**
|
|
|
|
* virFileReadValueScaledInt:
|
|
|
|
* @value: pointer to unsigned long long int to be filled in with the value
|
|
|
|
* @format, ...: file to read from
|
|
|
|
*
|
|
|
|
* Read unsigned scaled int from @format and put it into @value.
|
|
|
|
*
|
|
|
|
* Return -2 for non-existing file, -1 on other errors and 0 if everything went
|
|
|
|
* fine.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virFileReadValueScaledInt(unsigned long long *value, const char *format, ...)
|
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *str = NULL;
|
|
|
|
g_autofree char *path = NULL;
|
2017-04-05 14:13:52 +00:00
|
|
|
char *endp = NULL;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
2019-10-22 12:11:15 +00:00
|
|
|
path = g_strdup_vprintf(format, ap);
|
2017-04-05 14:13:52 +00:00
|
|
|
va_end(ap);
|
|
|
|
|
2018-07-13 17:54:48 +00:00
|
|
|
if (!virFileExists(path))
|
|
|
|
return -2;
|
2017-04-05 14:13:52 +00:00
|
|
|
|
2020-01-14 13:30:07 +00:00
|
|
|
if (virFileReadAll(path, VIR_INT64_STR_BUFLEN, &str) < 0)
|
2018-07-13 17:54:48 +00:00
|
|
|
return -1;
|
2017-04-05 14:13:52 +00:00
|
|
|
|
|
|
|
virStringTrimOptionalNewline(str);
|
|
|
|
|
|
|
|
if (virStrToLong_ullp(str, &endp, 10, value) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Invalid unsigned scaled integer value '%s' in file '%s'"),
|
|
|
|
str, path);
|
2018-07-13 17:54:48 +00:00
|
|
|
return -1;
|
2017-04-05 14:13:52 +00:00
|
|
|
}
|
|
|
|
|
2018-07-13 17:54:48 +00:00
|
|
|
return virScaleInteger(value, endp, 1024, ULLONG_MAX);
|
2017-03-07 09:36:54 +00:00
|
|
|
}
|
|
|
|
|
2017-04-05 14:13:52 +00:00
|
|
|
/* Arbitrarily sized number, feel free to change, but the function should be
|
|
|
|
* used for small, interface-like files, so it should not be huge (subjective) */
|
|
|
|
#define VIR_FILE_READ_VALUE_STRING_MAX 4096
|
|
|
|
|
2017-03-07 09:36:54 +00:00
|
|
|
/**
|
|
|
|
* virFileReadValueBitmap:
|
2017-04-05 14:13:52 +00:00
|
|
|
* @value: pointer to virBitmapPtr to be allocated and filled in with the value
|
|
|
|
* @format, ...: file to read from
|
2017-03-07 09:36:54 +00:00
|
|
|
*
|
2017-04-05 14:13:52 +00:00
|
|
|
* Read int from @format and put it into @value.
|
2017-03-07 09:36:54 +00:00
|
|
|
*
|
|
|
|
* Return -2 for non-existing file, -1 on other errors and 0 if everything went
|
|
|
|
* fine.
|
|
|
|
*/
|
|
|
|
int
|
2017-04-05 14:13:52 +00:00
|
|
|
virFileReadValueBitmap(virBitmapPtr *value, const char *format, ...)
|
2017-03-07 09:36:54 +00:00
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *str = NULL;
|
|
|
|
g_autofree char *path = NULL;
|
2017-04-05 14:13:52 +00:00
|
|
|
va_list ap;
|
2017-03-07 09:36:54 +00:00
|
|
|
|
2017-04-05 14:13:52 +00:00
|
|
|
va_start(ap, format);
|
2019-10-22 12:11:15 +00:00
|
|
|
path = g_strdup_vprintf(format, ap);
|
2017-04-05 14:13:52 +00:00
|
|
|
va_end(ap);
|
2017-03-07 09:36:54 +00:00
|
|
|
|
2018-07-13 17:54:48 +00:00
|
|
|
if (!virFileExists(path))
|
|
|
|
return -2;
|
2017-03-07 09:36:54 +00:00
|
|
|
|
2017-04-05 14:13:52 +00:00
|
|
|
if (virFileReadAll(path, VIR_FILE_READ_VALUE_STRING_MAX, &str) < 0)
|
2018-07-13 17:54:48 +00:00
|
|
|
return -1;
|
2017-03-07 09:36:54 +00:00
|
|
|
|
2017-04-05 14:13:52 +00:00
|
|
|
virStringTrimOptionalNewline(str);
|
|
|
|
|
|
|
|
*value = virBitmapParseUnlimited(str);
|
2017-03-07 09:36:54 +00:00
|
|
|
if (!*value)
|
2018-07-13 17:54:48 +00:00
|
|
|
return -1;
|
2017-03-07 09:36:54 +00:00
|
|
|
|
2018-07-13 17:54:48 +00:00
|
|
|
return 0;
|
2017-04-05 14:13:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virFileReadValueString:
|
|
|
|
* @value: pointer to char * to be allocated and filled in with the value
|
|
|
|
* @format, ...: file to read from
|
|
|
|
*
|
|
|
|
* Read string from @format and put it into @value. Don't get this mixed with
|
|
|
|
* virFileReadAll(). This function is a wrapper over it with the behaviour
|
|
|
|
* aligned to other virFileReadValue* functions
|
|
|
|
*
|
|
|
|
* Return -2 for non-existing file, -1 on other errors and 0 if everything went
|
|
|
|
* fine.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virFileReadValueString(char **value, const char *format, ...)
|
|
|
|
{
|
2018-07-13 17:54:48 +00:00
|
|
|
int ret;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *path = NULL;
|
2017-04-05 14:13:52 +00:00
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
2019-10-22 12:11:15 +00:00
|
|
|
path = g_strdup_vprintf(format, ap);
|
2017-04-05 14:13:52 +00:00
|
|
|
va_end(ap);
|
|
|
|
|
2018-07-13 17:54:48 +00:00
|
|
|
if (!virFileExists(path))
|
|
|
|
return -2;
|
2017-04-05 14:13:52 +00:00
|
|
|
|
|
|
|
ret = virFileReadAll(path, VIR_FILE_READ_VALUE_STRING_MAX, value);
|
|
|
|
|
|
|
|
if (*value)
|
|
|
|
virStringTrimOptionalNewline(*value);
|
2018-07-13 17:54:48 +00:00
|
|
|
|
2017-03-07 09:36:54 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2017-06-20 14:09:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virFileWaitForExists:
|
|
|
|
* @path: absolute path to a sysfs attribute (can be a symlink)
|
|
|
|
* @ms: how long to wait (in milliseconds)
|
|
|
|
* @tries: how many times should we try to wait for @path to become accessible
|
|
|
|
*
|
|
|
|
* Checks the existence of @path. In case the file defined by @path
|
|
|
|
* doesn't exist, we wait for it to appear in @ms milliseconds (for up to
|
|
|
|
* @tries attempts).
|
|
|
|
*
|
|
|
|
* Returns 0 on success, -1 on error, setting errno appropriately.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virFileWaitForExists(const char *path,
|
|
|
|
size_t ms,
|
|
|
|
size_t tries)
|
|
|
|
{
|
|
|
|
errno = 0;
|
|
|
|
|
|
|
|
/* wait for @path to be accessible in @ms milliseconds, up to @tries */
|
|
|
|
while (tries-- > 0 && !virFileExists(path)) {
|
|
|
|
if (tries == 0 || errno != ENOENT)
|
|
|
|
return -1;
|
|
|
|
|
2019-10-02 17:01:11 +00:00
|
|
|
g_usleep(ms * 1000);
|
2017-06-20 14:09:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2018-08-06 08:50:03 +00:00
|
|
|
|
|
|
|
|
2020-09-01 11:27:44 +00:00
|
|
|
#if WITH_LIBATTR
|
2018-08-06 08:50:03 +00:00
|
|
|
/**
|
2019-03-22 14:08:36 +00:00
|
|
|
* virFileGetXAttrQuiet;
|
2018-08-06 08:50:03 +00:00
|
|
|
* @path: a filename
|
|
|
|
* @name: name of xattr
|
|
|
|
* @value: read value
|
|
|
|
*
|
|
|
|
* Reads xattr with @name for given @path and stores it into
|
|
|
|
* @value. Caller is responsible for freeing @value.
|
|
|
|
*
|
|
|
|
* Returns: 0 on success,
|
|
|
|
* -1 otherwise (with errno set).
|
|
|
|
*/
|
|
|
|
int
|
2019-03-22 14:08:36 +00:00
|
|
|
virFileGetXAttrQuiet(const char *path,
|
|
|
|
const char *name,
|
|
|
|
char **value)
|
2018-08-06 08:50:03 +00:00
|
|
|
{
|
2020-09-11 11:42:17 +00:00
|
|
|
g_autofree char *buf = NULL;
|
2018-08-06 08:50:03 +00:00
|
|
|
|
|
|
|
/* We might be racing with somebody who sets the same attribute. */
|
|
|
|
while (1) {
|
|
|
|
ssize_t need;
|
|
|
|
ssize_t got;
|
|
|
|
|
|
|
|
/* The first call determines how many bytes we need to allocate. */
|
|
|
|
if ((need = getxattr(path, name, NULL, 0)) < 0)
|
2020-09-11 11:42:17 +00:00
|
|
|
return -1;
|
2018-08-06 08:50:03 +00:00
|
|
|
|
2020-09-11 11:42:17 +00:00
|
|
|
buf = g_renew(char, buf, need + 1);
|
2018-08-06 08:50:03 +00:00
|
|
|
|
|
|
|
if ((got = getxattr(path, name, buf, need)) < 0) {
|
|
|
|
if (errno == ERANGE)
|
|
|
|
continue;
|
2020-09-11 11:42:17 +00:00
|
|
|
return -1;
|
2018-08-06 08:50:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
buf[got] = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-10-16 11:43:52 +00:00
|
|
|
*value = g_steal_pointer(&buf);
|
2020-09-11 11:42:17 +00:00
|
|
|
return 0;
|
2018-08-06 08:50:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virFileSetXAttr:
|
|
|
|
* @path: a filename
|
|
|
|
* @name: name of xattr
|
|
|
|
* @value: value to set
|
|
|
|
*
|
|
|
|
* Sets xattr of @name and @value on @path.
|
|
|
|
*
|
|
|
|
* Returns: 0 on success,
|
2019-03-22 14:34:59 +00:00
|
|
|
* -1 otherwise (with errno set AND error reported).
|
2018-08-06 08:50:03 +00:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
virFileSetXAttr(const char *path,
|
|
|
|
const char *name,
|
|
|
|
const char *value)
|
|
|
|
{
|
2019-03-22 14:34:59 +00:00
|
|
|
if (setxattr(path, name, value, strlen(value), 0) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Unable to set XATTR %s on %s"),
|
|
|
|
name, path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2018-08-06 08:50:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virFileRemoveXAttr:
|
|
|
|
* @path: a filename
|
|
|
|
* @name: name of xattr
|
|
|
|
*
|
|
|
|
* Remove xattr of @name on @path.
|
|
|
|
*
|
|
|
|
* Returns: 0 on success,
|
2019-03-22 14:41:06 +00:00
|
|
|
* -1 otherwise (with errno set AND error reported).
|
2018-08-06 08:50:03 +00:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
virFileRemoveXAttr(const char *path,
|
|
|
|
const char *name)
|
|
|
|
{
|
2019-03-22 14:41:06 +00:00
|
|
|
if (removexattr(path, name) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Unable to remove XATTR %s on %s"),
|
|
|
|
name, path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2018-08-06 08:50:03 +00:00
|
|
|
}
|
|
|
|
|
2020-09-01 11:27:44 +00:00
|
|
|
#else /* !WITH_LIBATTR */
|
2018-08-06 08:50:03 +00:00
|
|
|
|
|
|
|
int
|
2019-10-14 12:45:33 +00:00
|
|
|
virFileGetXAttrQuiet(const char *path G_GNUC_UNUSED,
|
|
|
|
const char *name G_GNUC_UNUSED,
|
|
|
|
char **value G_GNUC_UNUSED)
|
2018-08-06 08:50:03 +00:00
|
|
|
{
|
|
|
|
errno = ENOSYS;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2019-03-22 14:34:59 +00:00
|
|
|
virFileSetXAttr(const char *path,
|
|
|
|
const char *name,
|
2019-10-14 12:45:33 +00:00
|
|
|
const char *value G_GNUC_UNUSED)
|
2018-08-06 08:50:03 +00:00
|
|
|
{
|
|
|
|
errno = ENOSYS;
|
2019-03-22 14:34:59 +00:00
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Unable to set XATTR %s on %s"),
|
|
|
|
name, path);
|
2018-08-06 08:50:03 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2019-03-22 14:41:06 +00:00
|
|
|
virFileRemoveXAttr(const char *path,
|
|
|
|
const char *name)
|
2018-08-06 08:50:03 +00:00
|
|
|
{
|
|
|
|
errno = ENOSYS;
|
2019-03-22 14:41:06 +00:00
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Unable to remove XATTR %s on %s"),
|
|
|
|
name, path);
|
2018-08-06 08:50:03 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-09-01 11:27:44 +00:00
|
|
|
#endif /* WITH_LIBATTR */
|
2019-03-22 14:08:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* virFileGetXAttr;
|
|
|
|
* @path: a filename
|
|
|
|
* @name: name of xattr
|
|
|
|
* @value: read value
|
|
|
|
*
|
|
|
|
* Reads xattr with @name for given @path and stores it into
|
|
|
|
* @value. Caller is responsible for freeing @value.
|
|
|
|
*
|
|
|
|
* Returns: 0 on success,
|
|
|
|
* -1 otherwise (with errno set AND error reported).
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virFileGetXAttr(const char *path,
|
|
|
|
const char *name,
|
|
|
|
char **value)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if ((ret = virFileGetXAttrQuiet(path, name, value)) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Unable to get XATTR %s on %s"),
|
|
|
|
name, path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2019-12-24 15:14:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
virFileDataSync(int fd)
|
|
|
|
{
|
|
|
|
#if defined(__APPLE__) || defined(WIN32)
|
|
|
|
return g_fsync(fd);
|
|
|
|
#else
|
|
|
|
return fdatasync(fd);
|
|
|
|
#endif
|
|
|
|
}
|
2020-07-17 18:16:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virFileSetCow:
|
|
|
|
* @path: file or directory to control the COW flag on
|
|
|
|
* @state: the desired state of the COW flag
|
|
|
|
*
|
|
|
|
* When @state is VIR_TRISTATE_BOOL_ABSENT, some helpful
|
|
|
|
* default logic will be used. Specifically if the filesystem
|
|
|
|
* containing @path is 'btrfs', then it will attempt to
|
|
|
|
* disable the COW flag, but errors will be ignored. For
|
|
|
|
* any other filesystem no change will be made.
|
|
|
|
*
|
|
|
|
* When @state is VIR_TRISTATE_BOOL_YES or VIR_TRISTATE_BOOL_NO,
|
|
|
|
* it will attempt to set the COW flag state to that explicit
|
|
|
|
* value, and always return an error if it fails. Note this
|
|
|
|
* means it will always return error if the filesystem is not
|
|
|
|
* 'btrfs'.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virFileSetCOW(const char *path,
|
|
|
|
virTristateBool state)
|
|
|
|
{
|
|
|
|
#if __linux__
|
|
|
|
int val = 0;
|
|
|
|
struct statfs buf;
|
|
|
|
VIR_AUTOCLOSE fd = -1;
|
|
|
|
|
|
|
|
VIR_DEBUG("Setting COW flag on '%s' to '%s'",
|
|
|
|
path, virTristateBoolTypeToString(state));
|
|
|
|
|
|
|
|
fd = open(path, O_RDONLY|O_NONBLOCK|O_LARGEFILE);
|
|
|
|
if (fd < 0) {
|
|
|
|
virReportSystemError(errno, _("unable to open '%s'"),
|
|
|
|
path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fstatfs(fd, &buf) < 0) {
|
|
|
|
virReportSystemError(errno, _("unable query filesystem type on '%s'"),
|
|
|
|
path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buf.f_type != BTRFS_SUPER_MAGIC) {
|
2020-08-05 08:01:45 +00:00
|
|
|
if (state != VIR_TRISTATE_BOOL_ABSENT) {
|
2020-07-17 18:16:27 +00:00
|
|
|
virReportSystemError(ENOSYS,
|
|
|
|
_("unable to control COW flag on '%s', not btrfs"),
|
|
|
|
path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ioctl(fd, FS_IOC_GETFLAGS, &val) < 0) {
|
|
|
|
virReportSystemError(errno, _("unable get directory flags on '%s'"),
|
|
|
|
path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_DEBUG("Current flags on '%s' are 0x%x", path, val);
|
|
|
|
if (state == VIR_TRISTATE_BOOL_YES) {
|
|
|
|
val &= ~FS_NOCOW_FL;
|
|
|
|
} else {
|
|
|
|
val |= FS_NOCOW_FL;
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_DEBUG("New flags on '%s' will be 0x%x", path, val);
|
|
|
|
if (ioctl(fd, FS_IOC_SETFLAGS, &val) < 0) {
|
|
|
|
int saved_err = errno;
|
|
|
|
VIR_DEBUG("Failed to set flags on '%s': %s", path, g_strerror(saved_err));
|
|
|
|
if (state != VIR_TRISTATE_BOOL_ABSENT) {
|
|
|
|
virReportSystemError(saved_err,
|
|
|
|
_("unable control COW flag on '%s'"),
|
|
|
|
path);
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
VIR_DEBUG("Ignoring failure to set COW");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
#else /* ! __linux__ */
|
|
|
|
if (state != VIR_TRISTATE_BOOL_ABSENT) {
|
|
|
|
virReportSystemError(ENOSYS,
|
|
|
|
_("Unable to set copy-on-write state on '%s' to '%s'"),
|
|
|
|
path, virTristateBoolTypeToString(state));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
#endif /* ! __linux__ */
|
|
|
|
}
|