mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-12-24 14:45:24 +00:00
util: rename virFileOperation to virFileOpenAs
This patch intentionally doesn't change indentation, in order to make it easier to review the real changes. * src/util/util.h (VIR_FILE_OP_RETURN_FD, virFileOperationHook): Delete. (virFileOperation): Rename... (virFileOpenAs): ...and reduce parameters. * src/util/util.c (virFileOperationNoFork, virFileOperation): Rename and simplify. * src/qemu/qemu_driver.c (qemudDomainSaveFlag): Adjust caller. * src/storage/storage_backend.c (virStorageBackendCreateRaw): Likewise. * src/libvirt_private.syms: Reflect rename.
This commit is contained in:
parent
fe303a4256
commit
1fdd50f999
@ -878,8 +878,8 @@ virFileIsExecutable;
|
|||||||
virFileLinkPointsTo;
|
virFileLinkPointsTo;
|
||||||
virFileMakePath;
|
virFileMakePath;
|
||||||
virFileMatchesNameSuffix;
|
virFileMatchesNameSuffix;
|
||||||
|
virFileOpenAs;
|
||||||
virFileOpenTty;
|
virFileOpenTty;
|
||||||
virFileOperation;
|
|
||||||
virFilePid;
|
virFilePid;
|
||||||
virFileReadAll;
|
virFileReadAll;
|
||||||
virFileReadLimFD;
|
virFileReadLimFD;
|
||||||
|
@ -1913,11 +1913,9 @@ static int qemudDomainSaveFlag(struct qemud_driver *driver, virDomainPtr dom,
|
|||||||
goto endjob;
|
goto endjob;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if ((fd = virFileOperation(path, O_CREAT|O_TRUNC|O_WRONLY,
|
if ((fd = virFileOpenAs(path, O_CREAT|O_TRUNC|O_WRONLY,
|
||||||
S_IRUSR|S_IWUSR,
|
S_IRUSR|S_IWUSR,
|
||||||
getuid(), getgid(),
|
getuid(), getgid(), 0)) < 0) {
|
||||||
NULL, NULL,
|
|
||||||
VIR_FILE_OP_RETURN_FD)) < 0) {
|
|
||||||
/* If we failed as root, and the error was permission-denied
|
/* If we failed as root, and the error was permission-denied
|
||||||
(EACCES or EPERM), assume it's on a network-connected share
|
(EACCES or EPERM), assume it's on a network-connected share
|
||||||
where root access is restricted (eg, root-squashed NFS). If the
|
where root access is restricted (eg, root-squashed NFS). If the
|
||||||
@ -1951,7 +1949,7 @@ static int qemudDomainSaveFlag(struct qemud_driver *driver, virDomainPtr dom,
|
|||||||
|
|
||||||
case 0:
|
case 0:
|
||||||
default:
|
default:
|
||||||
/* local file - log the error returned by virFileOperation */
|
/* local file - log the error returned by virFileOpenAs */
|
||||||
virReportSystemError(-rc,
|
virReportSystemError(-rc,
|
||||||
_("Failed to create domain save file '%s'"),
|
_("Failed to create domain save file '%s'"),
|
||||||
path);
|
path);
|
||||||
@ -1962,12 +1960,10 @@ static int qemudDomainSaveFlag(struct qemud_driver *driver, virDomainPtr dom,
|
|||||||
|
|
||||||
/* Retry creating the file as driver->user */
|
/* Retry creating the file as driver->user */
|
||||||
|
|
||||||
if ((fd = virFileOperation(path, O_CREAT|O_TRUNC|O_WRONLY,
|
if ((fd = virFileOpenAs(path, O_CREAT|O_TRUNC|O_WRONLY,
|
||||||
S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP,
|
S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP,
|
||||||
driver->user, driver->group,
|
driver->user, driver->group,
|
||||||
NULL, NULL,
|
VIR_FILE_OPEN_AS_UID)) < 0) {
|
||||||
(VIR_FILE_OP_AS_UID |
|
|
||||||
VIR_FILE_OP_RETURN_FD))) < 0) {
|
|
||||||
virReportSystemError(-fd,
|
virReportSystemError(-fd,
|
||||||
_("Error from child process creating '%s'"),
|
_("Error from child process creating '%s'"),
|
||||||
path);
|
path);
|
||||||
|
@ -390,14 +390,14 @@ virStorageBackendCreateRaw(virConnectPtr conn ATTRIBUTE_UNUSED,
|
|||||||
|
|
||||||
uid = (vol->target.perms.uid == -1) ? getuid() : vol->target.perms.uid;
|
uid = (vol->target.perms.uid == -1) ? getuid() : vol->target.perms.uid;
|
||||||
gid = (vol->target.perms.gid == -1) ? getgid() : vol->target.perms.gid;
|
gid = (vol->target.perms.gid == -1) ? getgid() : vol->target.perms.gid;
|
||||||
operation_flags = VIR_FILE_OP_FORCE_PERMS | VIR_FILE_OP_RETURN_FD;
|
operation_flags = VIR_FILE_OPEN_FORCE_PERMS;
|
||||||
if (pool->def->type == VIR_STORAGE_POOL_NETFS)
|
if (pool->def->type == VIR_STORAGE_POOL_NETFS)
|
||||||
operation_flags |= VIR_FILE_OP_AS_UID;
|
operation_flags |= VIR_FILE_OPEN_AS_UID;
|
||||||
|
|
||||||
if ((fd = virFileOperation(vol->target.path,
|
if ((fd = virFileOpenAs(vol->target.path,
|
||||||
O_RDWR | O_CREAT | O_EXCL,
|
O_RDWR | O_CREAT | O_EXCL,
|
||||||
vol->target.perms.mode, uid, gid,
|
vol->target.perms.mode, uid, gid,
|
||||||
NULL, NULL, operation_flags)) < 0) {
|
operation_flags)) < 0) {
|
||||||
virReportSystemError(-fd,
|
virReportSystemError(-fd,
|
||||||
_("cannot create path '%s'"),
|
_("cannot create path '%s'"),
|
||||||
vol->target.path);
|
vol->target.path);
|
||||||
|
105
src/util/util.c
105
src/util/util.c
@ -1391,10 +1391,10 @@ virFileIsExecutable(const char *file)
|
|||||||
|
|
||||||
#ifndef WIN32
|
#ifndef WIN32
|
||||||
/* return -errno on failure, or 0 on success */
|
/* return -errno on failure, or 0 on success */
|
||||||
static int virFileOperationNoFork(const char *path, int openflags, mode_t mode,
|
static int
|
||||||
uid_t uid, gid_t gid,
|
virFileOpenAsNoFork(const char *path, int openflags, mode_t mode,
|
||||||
virFileOperationHook hook, void *hookdata,
|
uid_t uid, gid_t gid, unsigned int flags)
|
||||||
unsigned int flags) {
|
{
|
||||||
int fd = -1;
|
int fd = -1;
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
struct stat st;
|
struct stat st;
|
||||||
@ -1417,7 +1417,7 @@ static int virFileOperationNoFork(const char *path, int openflags, mode_t mode,
|
|||||||
path, (unsigned int) uid, (unsigned int) gid);
|
path, (unsigned int) uid, (unsigned int) gid);
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
if ((flags & VIR_FILE_OP_FORCE_PERMS)
|
if ((flags & VIR_FILE_OPEN_FORCE_PERMS)
|
||||||
&& (fchmod(fd, mode) < 0)) {
|
&& (fchmod(fd, mode) < 0)) {
|
||||||
ret = -errno;
|
ret = -errno;
|
||||||
virReportSystemError(errno,
|
virReportSystemError(errno,
|
||||||
@ -1425,17 +1425,7 @@ static int virFileOperationNoFork(const char *path, int openflags, mode_t mode,
|
|||||||
path, mode);
|
path, mode);
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
if ((hook) && ((ret = hook(fd, hookdata)) != 0)) {
|
return fd;
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
if (flags & VIR_FILE_OP_RETURN_FD)
|
|
||||||
return fd;
|
|
||||||
if (VIR_CLOSE(fd) < 0) {
|
|
||||||
ret = -errno;
|
|
||||||
virReportSystemError(errno, _("failed to close new file '%s'"),
|
|
||||||
path);
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
error:
|
error:
|
||||||
VIR_FORCE_CLOSE(fd);
|
VIR_FORCE_CLOSE(fd);
|
||||||
@ -1482,35 +1472,27 @@ error:
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* virFileOperation:
|
* virFileOpenAs:
|
||||||
* @path: file to open or create
|
* @path: file to open or create
|
||||||
* @openflags: flags to pass to open
|
* @openflags: flags to pass to open
|
||||||
* @mode: mode to use on creation or when forcing permissions
|
* @mode: mode to use on creation or when forcing permissions
|
||||||
* @uid: uid that should own file on creation
|
* @uid: uid that should own file on creation
|
||||||
* @gid: gid that should own file
|
* @gid: gid that should own file
|
||||||
* @hook: callback to run once file is open; see below for restrictions
|
* @flags: bit-wise or of VIR_FILE_OPEN_* flags
|
||||||
* @hookdata: opaque data for @hook
|
|
||||||
* @flags: bit-wise or of VIR_FILE_OP_* flags
|
|
||||||
*
|
*
|
||||||
* Open @path, and execute an optional callback @hook on the open file
|
* Open @path, and execute an optional callback @hook on the open file
|
||||||
* description. @hook must return 0 on success, or -errno on failure.
|
* description. @hook must return 0 on success, or -errno on failure.
|
||||||
* If @flags includes VIR_FILE_OP_AS_UID, then open the file while the
|
* If @flags includes VIR_FILE_OPEN_AS_UID, then open the file while the
|
||||||
* effective user id is @uid (by using a child process); this allows
|
* effective user id is @uid (by using a child process); this allows
|
||||||
* one to bypass root-squashing NFS issues. If @flags includes
|
* one to bypass root-squashing NFS issues. If @flags includes
|
||||||
* VIR_FILE_OP_FORCE_PERMS, then ensure that @path has those
|
* VIR_FILE_OPEN_FORCE_PERMS, then ensure that @path has those
|
||||||
* permissions before the callback, even if the file already existed
|
* permissions before the callback, even if the file already existed
|
||||||
* with different permissions. If @flags includes
|
* with different permissions. The return value (if non-negative)
|
||||||
* VIR_FILE_OP_RETURN_FD, then use SCM_RIGHTS to guarantee that any
|
* is the file descriptor, left open. Return -errno on failure. */
|
||||||
* @hook is run in the parent, and the return value (if non-negative)
|
int
|
||||||
* is the file descriptor, left open. Otherwise, @hook might be run
|
virFileOpenAs(const char *path, int openflags, mode_t mode,
|
||||||
* in a child process, so it must be async-signal-safe (ie. no
|
uid_t uid, gid_t gid, unsigned int flags)
|
||||||
* malloc() or anything else that depends on modifying locks held in
|
{
|
||||||
* the parent), no file descriptor remains open, and 0 is returned on
|
|
||||||
* success. Return -errno on failure. */
|
|
||||||
int virFileOperation(const char *path, int openflags, mode_t mode,
|
|
||||||
uid_t uid, gid_t gid,
|
|
||||||
virFileOperationHook hook, void *hookdata,
|
|
||||||
unsigned int flags) {
|
|
||||||
struct stat st;
|
struct stat st;
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
int waitret, status, ret = 0;
|
int waitret, status, ret = 0;
|
||||||
@ -1523,11 +1505,10 @@ int virFileOperation(const char *path, int openflags, mode_t mode,
|
|||||||
struct iovec iov;
|
struct iovec iov;
|
||||||
int forkRet;
|
int forkRet;
|
||||||
|
|
||||||
if ((!(flags & VIR_FILE_OP_AS_UID))
|
if ((!(flags & VIR_FILE_OPEN_AS_UID))
|
||||||
|| (getuid() != 0)
|
|| (getuid() != 0)
|
||||||
|| ((uid == 0) && (gid == 0))) {
|
|| ((uid == 0) && (gid == 0))) {
|
||||||
return virFileOperationNoFork(path, openflags, mode, uid, gid,
|
return virFileOpenAsNoFork(path, openflags, mode, uid, gid, flags);
|
||||||
hook, hookdata, flags);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* parent is running as root, but caller requested that the
|
/* parent is running as root, but caller requested that the
|
||||||
@ -1535,7 +1516,7 @@ int virFileOperation(const char *path, int openflags, mode_t mode,
|
|||||||
* following dance avoids problems caused by root-squashing
|
* following dance avoids problems caused by root-squashing
|
||||||
* NFS servers. */
|
* NFS servers. */
|
||||||
|
|
||||||
if (flags & VIR_FILE_OP_RETURN_FD) {
|
{
|
||||||
if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) < 0) {
|
if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) < 0) {
|
||||||
ret = -errno;
|
ret = -errno;
|
||||||
virReportSystemError(errno,
|
virReportSystemError(errno,
|
||||||
@ -1565,7 +1546,7 @@ int virFileOperation(const char *path, int openflags, mode_t mode,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (pid) { /* parent */
|
if (pid) { /* parent */
|
||||||
if (flags & VIR_FILE_OP_RETURN_FD) {
|
{
|
||||||
VIR_FORCE_CLOSE(pair[1]);
|
VIR_FORCE_CLOSE(pair[1]);
|
||||||
|
|
||||||
do {
|
do {
|
||||||
@ -1601,19 +1582,13 @@ int virFileOperation(const char *path, int openflags, mode_t mode,
|
|||||||
goto parenterror;
|
goto parenterror;
|
||||||
}
|
}
|
||||||
if (!WIFEXITED(status) || (ret = -WEXITSTATUS(status)) == -EACCES ||
|
if (!WIFEXITED(status) || (ret = -WEXITSTATUS(status)) == -EACCES ||
|
||||||
((flags & VIR_FILE_OP_RETURN_FD) && fd == -1)) {
|
fd == -1) {
|
||||||
/* fall back to the simpler method, which works better in
|
/* fall back to the simpler method, which works better in
|
||||||
* some cases */
|
* some cases */
|
||||||
return virFileOperationNoFork(path, openflags, mode, uid, gid,
|
return virFileOpenAsNoFork(path, openflags, mode, uid, gid, flags);
|
||||||
hook, hookdata, flags);
|
|
||||||
}
|
}
|
||||||
if (!ret && (flags & VIR_FILE_OP_RETURN_FD)) {
|
if (!ret)
|
||||||
if ((hook) && ((ret = hook(fd, hookdata)) != 0)) {
|
|
||||||
VIR_FORCE_CLOSE(fd);
|
|
||||||
goto parenterror;
|
|
||||||
}
|
|
||||||
ret = fd;
|
ret = fd;
|
||||||
}
|
|
||||||
parenterror:
|
parenterror:
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@ -1626,6 +1601,7 @@ parenterror:
|
|||||||
ret = -errno;
|
ret = -errno;
|
||||||
goto childerror;
|
goto childerror;
|
||||||
}
|
}
|
||||||
|
VIR_FORCE_CLOSE(pair[0]);
|
||||||
|
|
||||||
/* set desired uid/gid, then attempt to create the file */
|
/* set desired uid/gid, then attempt to create the file */
|
||||||
|
|
||||||
@ -1665,7 +1641,7 @@ parenterror:
|
|||||||
path, (unsigned int) uid, (unsigned int) gid);
|
path, (unsigned int) uid, (unsigned int) gid);
|
||||||
goto childerror;
|
goto childerror;
|
||||||
}
|
}
|
||||||
if ((flags & VIR_FILE_OP_FORCE_PERMS)
|
if ((flags & VIR_FILE_OPEN_FORCE_PERMS)
|
||||||
&& (fchmod(fd, mode) < 0)) {
|
&& (fchmod(fd, mode) < 0)) {
|
||||||
ret = -errno;
|
ret = -errno;
|
||||||
virReportSystemError(errno,
|
virReportSystemError(errno,
|
||||||
@ -1673,8 +1649,7 @@ parenterror:
|
|||||||
path, mode);
|
path, mode);
|
||||||
goto childerror;
|
goto childerror;
|
||||||
}
|
}
|
||||||
if (flags & VIR_FILE_OP_RETURN_FD) {
|
{
|
||||||
VIR_FORCE_CLOSE(pair[0]);
|
|
||||||
memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd));
|
memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd));
|
||||||
|
|
||||||
do {
|
do {
|
||||||
@ -1685,17 +1660,6 @@ parenterror:
|
|||||||
ret = -errno;
|
ret = -errno;
|
||||||
goto childerror;
|
goto childerror;
|
||||||
}
|
}
|
||||||
ret = 0;
|
|
||||||
} else {
|
|
||||||
if ((hook) && ((ret = hook(fd, hookdata)) != 0))
|
|
||||||
goto childerror;
|
|
||||||
if (VIR_CLOSE(fd) < 0) {
|
|
||||||
ret = -errno;
|
|
||||||
virReportSystemError(errno,
|
|
||||||
_("child failed to close new file '%s'"),
|
|
||||||
path);
|
|
||||||
goto childerror;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = 0;
|
ret = 0;
|
||||||
@ -1704,6 +1668,7 @@ childerror:
|
|||||||
* If the child exits with EACCES, then the parent tries again. */
|
* If the child exits with EACCES, then the parent tries again. */
|
||||||
/* XXX This makes assumptions about errno being < 255, which is
|
/* XXX This makes assumptions about errno being < 255, which is
|
||||||
* not true on Hurd. */
|
* not true on Hurd. */
|
||||||
|
VIR_FORCE_CLOSE(pair[1]);
|
||||||
ret = -ret;
|
ret = -ret;
|
||||||
if ((ret & 0xff) != ret) {
|
if ((ret & 0xff) != ret) {
|
||||||
VIR_WARN("unable to pass desired return value %d", ret);
|
VIR_WARN("unable to pass desired return value %d", ret);
|
||||||
@ -1813,17 +1778,15 @@ childerror:
|
|||||||
#else /* WIN32 */
|
#else /* WIN32 */
|
||||||
|
|
||||||
/* return -errno on failure, or 0 on success */
|
/* return -errno on failure, or 0 on success */
|
||||||
int virFileOperation(const char *path ATTRIBUTE_UNUSED,
|
int virFileOpenAs(const char *path ATTRIBUTE_UNUSED,
|
||||||
int openflags ATTRIBUTE_UNUSED,
|
int openflags ATTRIBUTE_UNUSED,
|
||||||
mode_t mode ATTRIBUTE_UNUSED,
|
mode_t mode ATTRIBUTE_UNUSED,
|
||||||
uid_t uid ATTRIBUTE_UNUSED,
|
uid_t uid ATTRIBUTE_UNUSED,
|
||||||
gid_t gid ATTRIBUTE_UNUSED,
|
gid_t gid ATTRIBUTE_UNUSED,
|
||||||
virFileOperationHook hook ATTRIBUTE_UNUSED,
|
unsigned int flags ATTRIBUTE_UNUSED)
|
||||||
void *hookdata ATTRIBUTE_UNUSED,
|
|
||||||
unsigned int flags ATTRIBUTE_UNUSED)
|
|
||||||
{
|
{
|
||||||
virUtilError(VIR_ERR_INTERNAL_ERROR,
|
virUtilError(VIR_ERR_INTERNAL_ERROR,
|
||||||
"%s", _("virFileOperation is not implemented for WIN32"));
|
"%s", _("virFileOpenAs is not implemented for WIN32"));
|
||||||
|
|
||||||
return -ENOSYS;
|
return -ENOSYS;
|
||||||
}
|
}
|
||||||
|
@ -127,16 +127,13 @@ bool virFileIsExecutable(const char *file) ATTRIBUTE_NONNULL(1);
|
|||||||
char *virFileSanitizePath(const char *path);
|
char *virFileSanitizePath(const char *path);
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
VIR_FILE_OP_NONE = 0,
|
VIR_FILE_OPEN_NONE = 0,
|
||||||
VIR_FILE_OP_AS_UID = (1 << 0),
|
VIR_FILE_OPEN_AS_UID = (1 << 0),
|
||||||
VIR_FILE_OP_FORCE_PERMS = (1 << 1),
|
VIR_FILE_OPEN_FORCE_PERMS = (1 << 1),
|
||||||
VIR_FILE_OP_RETURN_FD = (1 << 2),
|
|
||||||
};
|
};
|
||||||
typedef int (*virFileOperationHook)(int fd, void *data);
|
int virFileOpenAs(const char *path, int openflags, mode_t mode,
|
||||||
int virFileOperation(const char *path, int openflags, mode_t mode,
|
uid_t uid, gid_t gid,
|
||||||
uid_t uid, gid_t gid,
|
unsigned int flags)
|
||||||
virFileOperationHook hook, void *hookdata,
|
|
||||||
unsigned int flags)
|
|
||||||
ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
|
ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
|
Loading…
Reference in New Issue
Block a user