bpo-41687: Fix sendfile implementation to work with Solaris (#22040)

diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index a6a4b9f..00ba758 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -9518,6 +9518,25 @@
     if (!Py_off_t_converter(offobj, &offset))
         return NULL;
 
+#if defined(__sun) && defined(__SVR4)
+    // On Solaris, sendfile raises EINVAL rather than returning 0
+    // when the offset is equal or bigger than the in_fd size.
+    int res;
+    struct stat st;
+
+    do {
+        Py_BEGIN_ALLOW_THREADS
+        res = fstat(in_fd, &st);
+        Py_END_ALLOW_THREADS
+    } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
+    if (ret < 0)
+        return (!async_err) ? posix_error() : NULL;
+
+    if (offset >= st.st_size) {
+        return Py_BuildValue("i", 0);
+    }
+#endif
+
     do {
         Py_BEGIN_ALLOW_THREADS
         ret = sendfile(out_fd, in_fd, &offset, count);