diff mbox series

[v2,9/9] block: check availablity for preadv/pwritev on mac

Message ID 20201019013928.72770-10-j@getutm.app (mailing list archive)
State New, archived
Headers show
Series iOS and Apple Silicon host support | expand

Commit Message

Joelle van Dyne Oct. 19, 2020, 1:39 a.m. UTC
From: osy <osy86@users.noreply.github.com>

macOS 11/iOS 14 added preadv/pwritev APIs. Due to weak linking, configure
will succeed with CONFIG_PREADV even when targeting a lower OS version. We
therefore need to check at run time if we can actually use these APIs.

Signed-off-by: Joelle van Dyne <j@getutm.app>
---
 block/file-posix.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

Comments

Thomas Huth Oct. 19, 2020, 8:27 a.m. UTC | #1
On 19/10/2020 03.39, Joelle van Dyne wrote:
> From: osy <osy86@users.noreply.github.com>

That "From:" line looks wrong ... could you please fix the "Author" of your
patches / your git config?

> macOS 11/iOS 14 added preadv/pwritev APIs. Due to weak linking, configure
> will succeed with CONFIG_PREADV even when targeting a lower OS version. We
> therefore need to check at run time if we can actually use these APIs.

That sounds like the wrong approach to me ... could you please try to fix
the check in "configure" instead? E.g. by running compile_prog with
"-Werror", so that the test fails if there is no valid prototype available?

 Thomas
Joelle van Dyne Oct. 19, 2020, 10:20 p.m. UTC | #2
On Mon, Oct 19, 2020 at 1:27 AM Thomas Huth <thuth@redhat.com> wrote:
>
> On 19/10/2020 03.39, Joelle van Dyne wrote:
> > From: osy <osy86@users.noreply.github.com>
>
> That "From:" line looks wrong ... could you please fix the "Author" of your
> patches / your git config?
osy wrote the original changes. I joined the UTM project to help bring
the changes upstream with permission. However, they have agreed that
if required that we can use my name as the author.

>
> > macOS 11/iOS 14 added preadv/pwritev APIs. Due to weak linking, configure
> > will succeed with CONFIG_PREADV even when targeting a lower OS version. We
> > therefore need to check at run time if we can actually use these APIs.
>
> That sounds like the wrong approach to me ... could you please try to fix
> the check in "configure" instead? E.g. by running compile_prog with
> "-Werror", so that the test fails if there is no valid prototype available?
It's not that simple. Xcode 11 and below (supporting macOS 10.15 and
below, iOS 13 and below, etc) does not have preadv/pwritev symbols
defined and would fail to compile. Xcode 12 (supporting macOS 11 and
below, iOS 14 and below, etc) have preadv/pwritev weakly defined so if
it runs on, for example, 10.15, it would abort. There is no way to
determine at compile time if you can use preadv/pwritev or not when
building with Xcode 12. The availability checks are Apple's preferred
way to handle this kind of situation (they discourage directly
checking if an API exists on a system).

-j

>
>  Thomas
>
Thomas Huth Oct. 20, 2020, 5:19 a.m. UTC | #3
On 20/10/2020 00.20, Joelle van Dyne wrote:
> On Mon, Oct 19, 2020 at 1:27 AM Thomas Huth <thuth@redhat.com> wrote:
>>
>> On 19/10/2020 03.39, Joelle van Dyne wrote:
>>> From: osy <osy86@users.noreply.github.com>
>>
>> That "From:" line looks wrong ... could you please fix the "Author" of your
>> patches / your git config?
> osy wrote the original changes. I joined the UTM project to help bring
> the changes upstream with permission. However, they have agreed that
> if required that we can use my name as the author.

In any way, that "users.noreply.github.com" does not look like a valid
e-mail address and should be replaced.

>>
>>> macOS 11/iOS 14 added preadv/pwritev APIs. Due to weak linking, configure
>>> will succeed with CONFIG_PREADV even when targeting a lower OS version. We
>>> therefore need to check at run time if we can actually use these APIs.
>>
>> That sounds like the wrong approach to me ... could you please try to fix
>> the check in "configure" instead? E.g. by running compile_prog with
>> "-Werror", so that the test fails if there is no valid prototype available?
> It's not that simple. Xcode 11 and below (supporting macOS 10.15 and
> below, iOS 13 and below, etc) does not have preadv/pwritev symbols
> defined and would fail to compile. Xcode 12 (supporting macOS 11 and
> below, iOS 14 and below, etc) have preadv/pwritev weakly defined so if
> it runs on, for example, 10.15, it would abort. There is no way to
> determine at compile time if you can use preadv/pwritev or not when
> building with Xcode 12. The availability checks are Apple's preferred
> way to handle this kind of situation (they discourage directly
> checking if an API exists on a system).

Ok, got it now, thanks for the detailed explanation!

 Thomas
diff mbox series

Patch

diff --git a/block/file-posix.c b/block/file-posix.c
index cdc73b5f1d..d7482036a3 100644
--- a/block/file-posix.c
+++ b/block/file-posix.c
@@ -1393,12 +1393,24 @@  static bool preadv_present = true;
 static ssize_t
 qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset)
 {
+#ifdef CONFIG_DARWIN /* preadv introduced in macOS 11 */
+    if (!__builtin_available(macOS 11, iOS 14, watchOS 7, tvOS 14, *)) {
+        preadv_present = false;
+        return -ENOSYS;
+    } else
+#endif
     return preadv(fd, iov, nr_iov, offset);
 }
 
 static ssize_t
 qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset)
 {
+#ifdef CONFIG_DARWIN /* pwritev introduced in macOS 11 */
+    if (!__builtin_available(macOS 11, iOS 14, watchOS 7, tvOS 14, *)) {
+        preadv_present = false;
+        return -ENOSYS;
+    } else
+#endif
     return pwritev(fd, iov, nr_iov, offset);
 }