Merge pull request #25665 from poettering/dissect-memory
dissect: add a mode for operating on an in-memory copy of a DDI, inst…
This commit is contained in:
@@ -4,6 +4,9 @@
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#if HAVE_LINUX_MEMFD_H
|
||||
#include <linux/memfd.h>
|
||||
#endif
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "copy.h"
|
||||
@@ -12,6 +15,8 @@
|
||||
#include "fs-util.h"
|
||||
#include "io-util.h"
|
||||
#include "memfd-util.h"
|
||||
#include "missing_mman.h"
|
||||
#include "missing_syscall.h"
|
||||
#include "tmpfile-util.h"
|
||||
|
||||
/* When the data is smaller or equal to 64K, try to place the copy in a memfd/pipe */
|
||||
@@ -343,3 +348,50 @@ finish:
|
||||
|
||||
return fd_reopen(tmp_fd, O_RDONLY|O_CLOEXEC);
|
||||
}
|
||||
|
||||
int memfd_clone_fd(int fd, const char *name, int mode) {
|
||||
_cleanup_close_ int mfd = -EBADF;
|
||||
bool ro;
|
||||
int r;
|
||||
|
||||
/* Creates a clone of a regular file in a memfd. Unlike copy_data_fd() this returns strictly a memfd
|
||||
* (and if it can't it will fail). Thus the resulting fd is seekable, and definitely reports as
|
||||
* S_ISREG. */
|
||||
|
||||
assert(fd >= 0);
|
||||
assert(name);
|
||||
assert(IN_SET(mode & O_ACCMODE, O_RDONLY, O_RDWR));
|
||||
assert((mode & ~(O_RDONLY|O_RDWR|O_CLOEXEC)) == 0);
|
||||
|
||||
ro = (mode & O_ACCMODE) == O_RDONLY;
|
||||
|
||||
mfd = memfd_create(name,
|
||||
((FLAGS_SET(mode, O_CLOEXEC) || ro) ? MFD_CLOEXEC : 0) |
|
||||
(ro ? MFD_ALLOW_SEALING : 0));
|
||||
if (mfd < 0)
|
||||
return -errno;
|
||||
|
||||
r = copy_bytes(fd, mfd, UINT64_MAX, COPY_REFLINK);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
if (ro) {
|
||||
_cleanup_close_ int rfd = -1;
|
||||
|
||||
r = memfd_set_sealed(mfd);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
rfd = fd_reopen(mfd, mode);
|
||||
if (rfd < 0)
|
||||
return rfd;
|
||||
|
||||
return TAKE_FD(rfd);
|
||||
}
|
||||
|
||||
off_t f = lseek(mfd, 0, SEEK_SET);
|
||||
if (f < 0)
|
||||
return -errno;
|
||||
|
||||
return TAKE_FD(mfd);
|
||||
}
|
||||
|
||||
@@ -5,3 +5,4 @@
|
||||
|
||||
int acquire_data_fd(const void *data, size_t size, unsigned flags);
|
||||
int copy_data_fd(int fd);
|
||||
int memfd_clone_fd(int fd, const char *name, int mode);
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "blockdev-util.h"
|
||||
#include "data-fd-util.h"
|
||||
#include "device-util.h"
|
||||
#include "devnum-util.h"
|
||||
#include "env-util.h"
|
||||
@@ -640,6 +641,47 @@ int loop_device_make_by_path(
|
||||
return loop_device_make_internal(path, fd, open_flags, 0, 0, 0, loop_flags, lock_op, ret);
|
||||
}
|
||||
|
||||
int loop_device_make_by_path_memory(
|
||||
const char *path,
|
||||
int open_flags,
|
||||
uint32_t loop_flags,
|
||||
int lock_op,
|
||||
LoopDevice **ret) {
|
||||
|
||||
_cleanup_close_ int fd = -EBADF, mfd = -EBADF;
|
||||
_cleanup_free_ char *fn = NULL;
|
||||
struct stat st;
|
||||
int r;
|
||||
|
||||
assert(path);
|
||||
assert(IN_SET(open_flags, O_RDWR, O_RDONLY));
|
||||
assert(ret);
|
||||
|
||||
loop_flags &= ~LO_FLAGS_DIRECT_IO; /* memfds don't support O_DIRECT, hence LO_FLAGS_DIRECT_IO can't be used either */
|
||||
|
||||
fd = open(path, O_CLOEXEC|O_NONBLOCK|O_NOCTTY|O_RDONLY);
|
||||
if (fd < 0)
|
||||
return -errno;
|
||||
|
||||
if (fstat(fd, &st) < 0)
|
||||
return -errno;
|
||||
|
||||
if (!S_ISREG(st.st_mode) && !S_ISBLK(st.st_mode))
|
||||
return -EBADF;
|
||||
|
||||
r = path_extract_filename(path, &fn);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
mfd = memfd_clone_fd(fd, fn, open_flags|O_CLOEXEC);
|
||||
if (mfd < 0)
|
||||
return mfd;
|
||||
|
||||
fd = safe_close(fd); /* Let's close the original early */
|
||||
|
||||
return loop_device_make_internal(NULL, mfd, open_flags, 0, 0, 0, loop_flags, lock_op, ret);
|
||||
}
|
||||
|
||||
static LoopDevice* loop_device_free(LoopDevice *d) {
|
||||
_cleanup_close_ int control = -1;
|
||||
int r;
|
||||
|
||||
@@ -30,6 +30,7 @@ struct LoopDevice {
|
||||
|
||||
int loop_device_make(int fd, int open_flags, uint64_t offset, uint64_t size, uint32_t block_size, uint32_t loop_flags, int lock_op, LoopDevice **ret);
|
||||
int loop_device_make_by_path(const char *path, int open_flags, uint32_t loop_flags, int lock_op, LoopDevice **ret);
|
||||
int loop_device_make_by_path_memory(const char *path, int open_flags, uint32_t loop_flags, int lock_op, LoopDevice **ret);
|
||||
int loop_device_open(sd_device *dev, int open_flags, int lock_op, LoopDevice **ret);
|
||||
int loop_device_open_from_fd(int fd, int open_flags, int lock_op, LoopDevice **ret);
|
||||
int loop_device_open_from_path(const char *path, int open_flags, int lock_op, LoopDevice **ret);
|
||||
|
||||
Reference in New Issue
Block a user