Skip to contentSkip to conversation

linux-user: Implement sendmmsg syscall

PATCH In review Alexander Graf 3 participants 3 messages Sep 5, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux-user: Implement sendmmsg syscall

Glibc when built for newer kernels assumes that the sendmmsg syscall is available. Without it, dns resolution simply fails to work.

Wrap the syscall with existing infrastructure so that we don't have a host dependency on sendmmsg.

---
 linux-user/syscall.c      | 29 +++++++++++++++++++++++++++++
 linux-user/syscall_defs.h |  4 ++++
 2 files changed, 33 insertions(+)
linux-user/syscall.c +29 -0
index 0272990..59cfdf4 100644
@@ -1863,6 +1863,30 @@ out2:
     return ret;
 }
 
+#ifdef TARGET_NR_sendmmsg
+static abi_long do_sendmmsg(int fd, abi_ulong target_msgvec,
+                            unsigned int vlen, unsigned int flags)
+{
+    struct target_mmsghdr *mmsgp;
+    abi_ulong arg2 = target_msgvec;
+    int i;
+
+    if (!(mmsgp = lock_user(VERIFY_WRITE, target_msgvec,
+                            sizeof(*mmsgp) * vlen, 1))) {
+        return -TARGET_EFAULT;
+    }
+
+    for (i = 0; i < vlen; i++) {
+        mmsgp[i].msg_len = tswap32(do_sendrecvmsg(fd, arg2, flags, 1));
+        arg2 += sizeof(struct target_mmsghdr);
+    }
+
+    unlock_user(mmsgp, target_msgvec, 0);
+    /* XXX need to handle nonblocking case too */
+    return vlen;
+}
+#endif
+
 /* If we don't have a system accept4() then just call accept.
  * The callsites to do_accept4() will ensure that they don't
  * pass a non-zero flags argument in this config.
@@ -6973,6 +6997,11 @@ abi_long do_syscall(void *cpu_env, int num, abi_ulong arg1,
         ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
         break;
 #endif
+#ifdef TARGET_NR_sendmmsg
+    case TARGET_NR_sendmmsg:
+        ret = do_sendmmsg(arg1, arg2, arg3, arg4);
+        break;
+#endif
 #ifdef TARGET_NR_sendto
     case TARGET_NR_sendto:
         ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
linux-user/syscall_defs.h +4 -0
index e51ef31..37645de 100644
@@ -222,6 +222,10 @@ __target_cmsg_nxthdr (struct target_msghdr *__mhdr, struct target_cmsghdr *__cms
   return __cmsg;
 }
 
+struct target_mmsghdr {
+    struct target_msghdr msg_hdr;              /* Message header */
+    unsigned int         msg_len;              /* Number of bytes transmitted */
+};
 
 struct  target_rusage {
         struct target_timeval ru_utime;        /* user time used */
Signature · 1 line
1.8.1.4
#1 lore raw
Quoted 11 lines · Alexander Graf

Glibc when built for newer kernels assumes that the sendmmsg syscall is available.
Without it, dns resolution simply fails to work.

Wrap the syscall with existing infrastructure so that we don't have a host dependency
on sendmmsg.

Why don't you write a "do_sendrecvmmsg()" in the same way it is done for "do_sendrecvmsg()" to be able to manage also the "recvmmsg()" syscall ?

Regards,
Laurent

Quoted 74 lines

---
linux-user/syscall.c | 29 +++++++++++++++++++++++++++++ linux-user/syscall_defs.h | 4 ++++

2 files changed, 33 insertions(+)
linux-user/syscall.c +29 -0
index 0272990..59cfdf4 100644
@@ -1863,6 +1863,30 @@ out2:
 return ret;
 }
 
+#ifdef TARGET_NR_sendmmsg
+static abi_long do_sendmmsg(int fd, abi_ulong target_msgvec,
+ unsigned int vlen, unsigned int flags)
+{
+ struct target_mmsghdr *mmsgp;
+ abi_ulong arg2 = target_msgvec;
+ int i;
+
+ if (!(mmsgp = lock_user(VERIFY_WRITE, target_msgvec,
+ sizeof(*mmsgp) * vlen, 1))) {
+ return -TARGET_EFAULT;
+ }
+
+ for (i = 0; i < vlen; i++) {
+ mmsgp[i].msg_len = tswap32(do_sendrecvmsg(fd, arg2, flags, 1));
+ arg2 += sizeof(struct target_mmsghdr);
+ }
+
+ unlock_user(mmsgp, target_msgvec, 0);
+ /* XXX need to handle nonblocking case too */
+ return vlen;
+}
+#endif
+
 /* If we don't have a system accept4() then just call accept.
 * The callsites to do_accept4() will ensure that they don't
 * pass a non-zero flags argument in this config.
@@ -6973,6 +6997,11 @@ abi_long do_syscall(void *cpu_env, int num, abi_ulong
 arg1,
 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
 break;
 #endif
+#ifdef TARGET_NR_sendmmsg
+ case TARGET_NR_sendmmsg:
+ ret = do_sendmmsg(arg1, arg2, arg3, arg4);
+ break;
+#endif
 #ifdef TARGET_NR_sendto

case TARGET_NR_sendto:
ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);

linux-user/syscall_defs.h +0 -0
index e51ef31..37645de 100644
@@ -222,6 +222,10 @@ __target_cmsg_nxthdr (struct target_msghdr *__mhdr,

struct target_cmsghdr *__cms
return __cmsg;
}

linux-user/syscall_defs.h +4 -0
+struct target_mmsghdr {
+ struct target_msghdr msg_hdr; /* Message header */
+ unsigned int msg_len; /* Number of bytes transmitted */
+};

struct target_rusage {
struct target_timeval ru_utime; /* user time used */ --
1.8.1.4

#2 lore raw
In reply to Alexander Graf
Quoted 17 lines · Alexander Graf

Glibc when built for newer kernels assumes that the sendmmsg syscall is available. Without it, dns resolution simply fails to work.

Wrap the syscall with existing infrastructure so that we don't have a host dependency on sendmmsg.

---
 linux-user/syscall.c      | 29 +++++++++++++++++++++++++++++
 linux-user/syscall_defs.h |  4 ++++
 2 files changed, 33 insertions(+)
linux-user/syscall.c +0 -0
index 0272990..59cfdf4 100644

linux-user/syscall.c out2: · line 1872

Quoted 10 lines
linux-user/syscall.c +7 -0
@@ -1863,6 +1863,30 @@ out2:
     return ret;
 }
 
+#ifdef TARGET_NR_sendmmsg
+static abi_long do_sendmmsg(int fd, abi_ulong target_msgvec,
+                            unsigned int vlen, unsigned int flags)
+{
+    struct target_mmsghdr *mmsgp;
+    abi_ulong arg2 = target_msgvec;
+    int i;

If we're doing this in terms of sendmsg, we need to implement the kernel's "if CMSG_COMPAT fail EINVAL" logic ourselves. Compare
http://lxr.linux.no/#linux+v3.11/net/socket.c#L2175

linux-user/syscall.c out2: · line 1880

Quoted 8 lines
linux-user/syscall.c +8 -0
+
+    if (!(mmsgp = lock_user(VERIFY_WRITE, target_msgvec,
+                            sizeof(*mmsgp) * vlen, 1))) {
+        return -TARGET_EFAULT;
+    }
+
+    for (i = 0; i < vlen; i++) {
+        mmsgp[i].msg_len = tswap32(do_sendrecvmsg(fd, arg2, flags, 1));

If do_sendrecvmsg reports an errno we should stop
processing the array and return it, not just
stuff it into msg_len. Compare:
http://lxr.linux.no/#linux+v3.11/net/socket.c#L2155

linux-user/syscall.c out2: · line 1888

Quoted 8 lines
linux-user/syscall.c +8 -0
+        arg2 += sizeof(struct target_mmsghdr);
+    }
+
+    unlock_user(mmsgp, target_msgvec, 0);
+    /* XXX need to handle nonblocking case too */
+    return vlen;
+}
+#endif

thanks
-- PMM