qemu with hax to log dma reads & writes jcs.org/2018/11/12/vfio

io: Fix QIOChannelFile when creating and opening read-write

The code wrongly passes the mode to open() only if O_WRONLY is set.
Instead, the mode should be passed when O_CREAT is set (or O_TMPFILE on
Linux). Fix this by always passing the mode since open() will correctly
ignore the mode if it is not needed. Add a testcase which exercises this
bug and also change the existing testcase to check that the mode of the
created file is correct.

Signed-off-by: Ross Lagerwall <ross.lagerwall@citrix.com>
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>

authored by

Ross Lagerwall and committed by
Daniel P. Berrangé
902f6e14 a46ded1d

+27 -10
+1 -1
include/io/channel-file.h
··· 73 73 * qio_channel_file_new_path: 74 74 * @path: the file path 75 75 * @flags: the open flags (O_RDONLY|O_WRONLY|O_RDWR, etc) 76 - * @mode: the file creation mode if O_WRONLY is set in @flags 76 + * @mode: the file creation mode if O_CREAT is set in @flags 77 77 * @errp: pointer to initialized error object 78 78 * 79 79 * Create a new IO channel object for a file represented
+1 -5
io/channel-file.c
··· 50 50 51 51 ioc = QIO_CHANNEL_FILE(object_new(TYPE_QIO_CHANNEL_FILE)); 52 52 53 - if (flags & O_WRONLY) { 54 - ioc->fd = open(path, flags, mode); 55 - } else { 56 - ioc->fd = open(path, flags); 57 - } 53 + ioc->fd = open(path, flags, mode); 58 54 if (ioc->fd < 0) { 59 55 object_unref(OBJECT(ioc)); 60 56 error_setg_errno(errp, errno,
+25 -4
tests/test-io-channel-file.c
··· 24 24 #include "io-channel-helpers.h" 25 25 #include "qapi/error.h" 26 26 27 - static void test_io_channel_file(void) 27 + #define TEST_FILE "tests/test-io-channel-file.txt" 28 + #define TEST_MASK 0600 29 + 30 + static void test_io_channel_file_helper(int flags) 28 31 { 29 32 QIOChannel *src, *dst; 30 33 QIOChannelTest *test; 34 + struct stat st; 35 + mode_t mask; 36 + int ret; 31 37 32 - #define TEST_FILE "tests/test-io-channel-file.txt" 33 38 unlink(TEST_FILE); 34 39 src = QIO_CHANNEL(qio_channel_file_new_path( 35 40 TEST_FILE, 36 - O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0600, 41 + flags, TEST_MASK, 37 42 &error_abort)); 38 43 dst = QIO_CHANNEL(qio_channel_file_new_path( 39 44 TEST_FILE, ··· 45 50 qio_channel_test_run_reader(test, dst); 46 51 qio_channel_test_validate(test); 47 52 53 + /* Check that the requested mode took effect. */ 54 + mask = umask(0); 55 + umask(mask); 56 + ret = stat(TEST_FILE, &st); 57 + g_assert_cmpint(ret, >, -1); 58 + g_assert_cmpuint(TEST_MASK & ~mask, ==, st.st_mode & 0777); 59 + 48 60 unlink(TEST_FILE); 49 61 object_unref(OBJECT(src)); 50 62 object_unref(OBJECT(dst)); 51 63 } 52 64 65 + static void test_io_channel_file(void) 66 + { 67 + test_io_channel_file_helper(O_WRONLY | O_CREAT | O_TRUNC | O_BINARY); 68 + } 69 + 70 + static void test_io_channel_file_rdwr(void) 71 + { 72 + test_io_channel_file_helper(O_RDWR | O_CREAT | O_TRUNC | O_BINARY); 73 + } 53 74 54 75 static void test_io_channel_fd(void) 55 76 { 56 77 QIOChannel *ioc; 57 78 int fd = -1; 58 79 59 - #define TEST_FILE "tests/test-io-channel-file.txt" 60 80 fd = open(TEST_FILE, O_CREAT | O_TRUNC | O_WRONLY, 0600); 61 81 g_assert_cmpint(fd, >, -1); 62 82 ··· 114 134 g_test_init(&argc, &argv, NULL); 115 135 116 136 g_test_add_func("/io/channel/file", test_io_channel_file); 137 + g_test_add_func("/io/channel/file/rdwr", test_io_channel_file_rdwr); 117 138 g_test_add_func("/io/channel/file/fd", test_io_channel_fd); 118 139 #ifndef _WIN32 119 140 g_test_add_func("/io/channel/pipe/sync", test_io_channel_pipe_sync);