2017-02-27 01:42:03 +03:00
|
|
|
/*
|
|
|
|
* 9p utilities
|
|
|
|
*
|
|
|
|
* Copyright IBM, Corp. 2017
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Greg Kurz <groug@kaod.org>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
|
|
* See the COPYING file in the top-level directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef QEMU_9P_UTIL_H
|
|
|
|
#define QEMU_9P_UTIL_H
|
|
|
|
|
2017-08-10 15:21:04 +03:00
|
|
|
#ifdef O_PATH
|
|
|
|
#define O_PATH_9P_UTIL O_PATH
|
|
|
|
#else
|
|
|
|
#define O_PATH_9P_UTIL 0
|
|
|
|
#endif
|
|
|
|
|
2022-02-28 01:35:19 +03:00
|
|
|
#ifdef CONFIG_DARWIN
|
|
|
|
#define qemu_fgetxattr(...) fgetxattr(__VA_ARGS__, 0, 0)
|
|
|
|
#define qemu_lgetxattr(...) getxattr(__VA_ARGS__, 0, XATTR_NOFOLLOW)
|
|
|
|
#define qemu_llistxattr(...) listxattr(__VA_ARGS__, XATTR_NOFOLLOW)
|
|
|
|
#define qemu_lremovexattr(...) removexattr(__VA_ARGS__, XATTR_NOFOLLOW)
|
|
|
|
static inline int qemu_lsetxattr(const char *path, const char *name,
|
|
|
|
const void *value, size_t size, int flags) {
|
|
|
|
return setxattr(path, name, value, size, 0, flags | XATTR_NOFOLLOW);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define qemu_fgetxattr fgetxattr
|
|
|
|
#define qemu_lgetxattr lgetxattr
|
|
|
|
#define qemu_llistxattr llistxattr
|
|
|
|
#define qemu_lremovexattr lremovexattr
|
|
|
|
#define qemu_lsetxattr lsetxattr
|
|
|
|
#endif
|
|
|
|
|
2017-02-27 01:42:03 +03:00
|
|
|
static inline void close_preserve_errno(int fd)
|
|
|
|
{
|
|
|
|
int serrno = errno;
|
|
|
|
close(fd);
|
|
|
|
errno = serrno;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int openat_dir(int dirfd, const char *name)
|
|
|
|
{
|
2017-03-06 19:34:01 +03:00
|
|
|
return openat(dirfd, name,
|
2017-08-10 15:21:04 +03:00
|
|
|
O_DIRECTORY | O_RDONLY | O_NOFOLLOW | O_PATH_9P_UTIL);
|
2017-02-27 01:42:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int openat_file(int dirfd, const char *name, int flags,
|
|
|
|
mode_t mode)
|
|
|
|
{
|
|
|
|
int fd, serrno, ret;
|
|
|
|
|
2022-02-28 01:35:16 +03:00
|
|
|
#ifndef CONFIG_DARWIN
|
9pfs: local: ignore O_NOATIME if we don't have permissions
QEMU's local 9pfs server passes through O_NOATIME from the client. If
the QEMU process doesn't have permissions to use O_NOATIME (namely, it
does not own the file nor have the CAP_FOWNER capability), the open will
fail. This causes issues when from the client's point of view, it
believes it has permissions to use O_NOATIME (e.g., a process running as
root in the virtual machine). Additionally, overlayfs on Linux opens
files on the lower layer using O_NOATIME, so in this case a 9pfs mount
can't be used as a lower layer for overlayfs (cf.
https://github.com/osandov/drgn/blob/dabfe1971951701da13863dbe6d8a1d172ad9650/vmtest/onoatimehack.c
and https://github.com/NixOS/nixpkgs/issues/54509).
Luckily, O_NOATIME is effectively a hint, and is often ignored by, e.g.,
network filesystems. open(2) notes that O_NOATIME "may not be effective
on all filesystems. One example is NFS, where the server maintains the
access time." This means that we can honor it when possible but fall
back to ignoring it.
Acked-by: Christian Schoenebeck <qemu_oss@crudebyte.com>
Signed-off-by: Omar Sandoval <osandov@fb.com>
Message-Id: <e9bee604e8df528584693a4ec474ded6295ce8ad.1587149256.git.osandov@fb.com>
Signed-off-by: Greg Kurz <groug@kaod.org>
2020-05-14 09:06:43 +03:00
|
|
|
again:
|
2022-02-28 01:35:16 +03:00
|
|
|
#endif
|
2017-02-27 01:42:03 +03:00
|
|
|
fd = openat(dirfd, name, flags | O_NOFOLLOW | O_NOCTTY | O_NONBLOCK,
|
|
|
|
mode);
|
|
|
|
if (fd == -1) {
|
2022-02-28 01:35:16 +03:00
|
|
|
#ifndef CONFIG_DARWIN
|
9pfs: local: ignore O_NOATIME if we don't have permissions
QEMU's local 9pfs server passes through O_NOATIME from the client. If
the QEMU process doesn't have permissions to use O_NOATIME (namely, it
does not own the file nor have the CAP_FOWNER capability), the open will
fail. This causes issues when from the client's point of view, it
believes it has permissions to use O_NOATIME (e.g., a process running as
root in the virtual machine). Additionally, overlayfs on Linux opens
files on the lower layer using O_NOATIME, so in this case a 9pfs mount
can't be used as a lower layer for overlayfs (cf.
https://github.com/osandov/drgn/blob/dabfe1971951701da13863dbe6d8a1d172ad9650/vmtest/onoatimehack.c
and https://github.com/NixOS/nixpkgs/issues/54509).
Luckily, O_NOATIME is effectively a hint, and is often ignored by, e.g.,
network filesystems. open(2) notes that O_NOATIME "may not be effective
on all filesystems. One example is NFS, where the server maintains the
access time." This means that we can honor it when possible but fall
back to ignoring it.
Acked-by: Christian Schoenebeck <qemu_oss@crudebyte.com>
Signed-off-by: Omar Sandoval <osandov@fb.com>
Message-Id: <e9bee604e8df528584693a4ec474ded6295ce8ad.1587149256.git.osandov@fb.com>
Signed-off-by: Greg Kurz <groug@kaod.org>
2020-05-14 09:06:43 +03:00
|
|
|
if (errno == EPERM && (flags & O_NOATIME)) {
|
|
|
|
/*
|
|
|
|
* The client passed O_NOATIME but we lack permissions to honor it.
|
|
|
|
* Rather than failing the open, fall back without O_NOATIME. This
|
|
|
|
* doesn't break the semantics on the client side, as the Linux
|
|
|
|
* open(2) man page notes that O_NOATIME "may not be effective on
|
|
|
|
* all filesystems". In particular, NFS and other network
|
|
|
|
* filesystems ignore it entirely.
|
|
|
|
*/
|
|
|
|
flags &= ~O_NOATIME;
|
|
|
|
goto again;
|
|
|
|
}
|
2022-02-28 01:35:16 +03:00
|
|
|
#endif
|
2017-02-27 01:42:03 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
serrno = errno;
|
2017-08-10 15:21:04 +03:00
|
|
|
/* O_NONBLOCK was only needed to open the file. Let's drop it. We don't
|
|
|
|
* do that with O_PATH since fcntl(F_SETFL) isn't supported, and openat()
|
|
|
|
* ignored it anyway.
|
|
|
|
*/
|
|
|
|
if (!(flags & O_PATH_9P_UTIL)) {
|
|
|
|
ret = fcntl(fd, F_SETFL, flags);
|
|
|
|
assert(!ret);
|
|
|
|
}
|
2017-02-27 01:42:03 +03:00
|
|
|
errno = serrno;
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2017-02-27 01:42:26 +03:00
|
|
|
ssize_t fgetxattrat_nofollow(int dirfd, const char *path, const char *name,
|
|
|
|
void *value, size_t size);
|
2017-02-27 01:42:43 +03:00
|
|
|
int fsetxattrat_nofollow(int dirfd, const char *path, const char *name,
|
|
|
|
void *value, size_t size, int flags);
|
2018-06-07 13:17:22 +03:00
|
|
|
ssize_t flistxattrat_nofollow(int dirfd, const char *filename,
|
|
|
|
char *list, size_t size);
|
|
|
|
ssize_t fremovexattrat_nofollow(int dirfd, const char *filename,
|
|
|
|
const char *name);
|
2017-02-27 01:42:03 +03:00
|
|
|
|
2022-03-03 16:20:29 +03:00
|
|
|
/*
|
2022-02-28 01:35:15 +03:00
|
|
|
* Darwin has d_seekoff, which appears to function similarly to d_off.
|
|
|
|
* However, it does not appear to be supported on all file systems,
|
|
|
|
* so ensure it is manually injected earlier and call here when
|
|
|
|
* needed.
|
|
|
|
*/
|
|
|
|
static inline off_t qemu_dirent_off(struct dirent *dent)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_DARWIN
|
|
|
|
return dent->d_seekoff;
|
|
|
|
#else
|
|
|
|
return dent->d_off;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-03-01 23:33:49 +03:00
|
|
|
/**
|
2022-03-03 16:20:29 +03:00
|
|
|
* qemu_dirent_dup() - Duplicate directory entry @dent.
|
|
|
|
*
|
|
|
|
* @dent: original directory entry to be duplicated
|
|
|
|
* Return: duplicated directory entry which should be freed with g_free()
|
2022-03-01 23:33:49 +03:00
|
|
|
*
|
|
|
|
* It is highly recommended to use this function instead of open coding
|
2022-03-02 21:30:39 +03:00
|
|
|
* duplication of dirent objects, because the actual struct dirent
|
|
|
|
* size may be bigger or shorter than sizeof(struct dirent) and correct
|
2022-03-01 23:33:49 +03:00
|
|
|
* handling is platform specific (see gitlab issue #841).
|
|
|
|
*/
|
|
|
|
static inline struct dirent *qemu_dirent_dup(struct dirent *dent)
|
|
|
|
{
|
|
|
|
size_t sz = 0;
|
|
|
|
#if defined _DIRENT_HAVE_D_RECLEN
|
|
|
|
/* Avoid use of strlen() if platform supports d_reclen. */
|
|
|
|
sz = dent->d_reclen;
|
|
|
|
#endif
|
|
|
|
/*
|
|
|
|
* Test sz for zero even if d_reclen is available
|
|
|
|
* because some drivers may set d_reclen to zero.
|
|
|
|
*/
|
|
|
|
if (sz == 0) {
|
|
|
|
/* Fallback to the most portable way. */
|
|
|
|
sz = offsetof(struct dirent, d_name) +
|
|
|
|
strlen(dent->d_name) + 1;
|
|
|
|
}
|
|
|
|
return g_memdup(dent, sz);
|
|
|
|
}
|
|
|
|
|
2022-02-28 01:35:20 +03:00
|
|
|
/*
|
|
|
|
* As long as mknodat is not available on macOS, this workaround
|
|
|
|
* using pthread_fchdir_np is needed. qemu_mknodat is defined in
|
|
|
|
* os-posix.c. pthread_fchdir_np is weakly linked here as a guard
|
|
|
|
* in case it disappears in future macOS versions, because it is
|
|
|
|
* is a private API.
|
|
|
|
*/
|
|
|
|
#if defined CONFIG_DARWIN && defined CONFIG_PTHREAD_FCHDIR_NP
|
|
|
|
int pthread_fchdir_np(int fd) __attribute__((weak_import));
|
|
|
|
#endif
|
|
|
|
int qemu_mknodat(int dirfd, const char *filename, mode_t mode, dev_t dev);
|
2022-02-28 01:35:15 +03:00
|
|
|
|
2017-02-27 01:42:03 +03:00
|
|
|
#endif
|