MPSAFE TTY: Linux PTY's
Chagin Dmitry
dchagin at freebsd.org
Sun Aug 31 11:06:30 UTC 2008
On Fri, Aug 22, 2008 at 01:29:46PM +0200, Roman Divacky wrote:
> On Fri, Aug 22, 2008 at 01:29:27PM +0200, Ed Schouten wrote:
> > Hello Emulation folks,
> >
> > I just wanted to send you all a message to say one of the things I tried
> > to improve in the MPSAFE TTY branch was support for PTY's for Linux
> > binaries.
> >
> > At home I've got a FreeBSD Jail running Debian Etch. Unfortunately,
> > Linux sendmsg() is a little broken on FreeBSD/amd64, but so far I've
> > been able to at least get OpenSSH (as root) and GNU Screen working.
>
> I believe dmitry has a patch for this..
the patch is bellow, I tested a patch only on LTP tests (with little changes),
it's necessary to test on real apps, it will be good if Ed will test..
diff --git a/src/sys/compat/linux/linux_socket.c b/src/sys/compat/linux/linux_socket.c
index 7202944..41556bb 100644
--- a/src/sys/compat/linux/linux_socket.c
+++ b/src/sys/compat/linux/linux_socket.c
@@ -421,6 +421,65 @@ linux_sa_put(struct osockaddr *osa)
}
static int
+linux_to_bsd_cmsg_type(int cmsg_type)
+{
+
+ switch (cmsg_type) {
+ case LINUX_SCM_RIGHTS:
+ return (SCM_RIGHTS);
+ case LINUX_SCM_CREDENTIALS:
+ return (SCM_CREDS);
+ }
+ return (cmsg_type);
+}
+
+static int
+bsd_to_linux_cmsg_type(int cmsg_type)
+{
+
+ switch (cmsg_type) {
+ case SCM_RIGHTS:
+ return (LINUX_SCM_RIGHTS);
+ case SCM_CREDS:
+ return (LINUX_SCM_CREDENTIALS);
+ }
+ return (cmsg_type);
+}
+
+
+
+static int
+linux_to_bsd_msghdr(struct msghdr *bhdr, const struct l_msghdr *lhdr)
+{
+ if (lhdr->msg_controllen > INT_MAX)
+ return (ENOBUFS);
+
+ bhdr->msg_name = PTRIN(lhdr->msg_name);
+ bhdr->msg_namelen = lhdr->msg_namelen;
+ bhdr->msg_iov = PTRIN(lhdr->msg_iov);
+ bhdr->msg_iovlen = lhdr->msg_iovlen;
+ bhdr->msg_control = PTRIN(lhdr->msg_control);
+ bhdr->msg_controllen = lhdr->msg_controllen;
+ bhdr->msg_flags = lhdr->msg_flags;
+ return (0);
+}
+
+static int
+bsd_to_linux_msghdr(const struct msghdr *bhdr, struct l_msghdr *lhdr)
+{
+ lhdr->msg_name = bhdr->msg_name;
+ lhdr->msg_namelen = bhdr->msg_namelen;
+ lhdr->msg_iov = bhdr->msg_iov;
+ lhdr->msg_iovlen = bhdr->msg_iovlen;
+ lhdr->msg_control = bhdr->msg_control;
+ lhdr->msg_controllen = bhdr->msg_controllen;
+ return (0);
+}
+
+#define CMSGHDR_SIZE CMSG_LEN(0)
+#define L_CMSGHDR_SIZE LINUX_CMSG_LEN(0)
+
+static int
linux_sendit(struct thread *td, int s, struct msghdr *mp, int flags,
enum uio_seg segflg)
{
@@ -437,25 +496,57 @@ linux_sendit(struct thread *td, int s, struct msghdr *mp, int flags,
to = NULL;
if (mp->msg_control != NULL) {
+ struct l_cmsghdr *ptr_cmsg;
+ struct l_cmsghdr linux_cmsg;
struct cmsghdr *cmsg;
-
- if (mp->msg_controllen < sizeof(struct cmsghdr)) {
- error = EINVAL;
- goto bad;
- }
- error = sockargs(&control, mp->msg_control,
- mp->msg_controllen, MT_CONTROL);
- if (error)
- goto bad;
-
- cmsg = mtod(control, struct cmsghdr *);
- cmsg->cmsg_level = linux_to_bsd_sockopt_level(cmsg->cmsg_level);
+ void *data;
+ socklen_t datalen;
+
+ cmsg = malloc(CMSGHDR_SIZE, M_TEMP, M_WAITOK | M_ZERO);
+ control = m_get(M_WAIT, MT_CONTROL);
+ ptr_cmsg = LINUX_CMSG_FIRSTHDR(mp);
+
+ do {
+ error = copyin(ptr_cmsg, &linux_cmsg,
+ sizeof(struct l_cmsghdr));
+ if (error)
+ goto bad;
+ if (linux_cmsg.cmsg_len < sizeof(struct l_cmsghdr) ||
+ linux_cmsg.cmsg_len > INT_MAX) {
+ error = EINVAL;
+ goto bad;
+ }
+
+ switch (linux_cmsg.cmsg_type) {
+ case LINUX_SCM_RIGHTS:
+ cmsg->cmsg_type =
+ linux_to_bsd_cmsg_type(linux_cmsg.cmsg_type);
+ break;
+ default:
+ error = EINVAL;
+ goto bad;
+ }
+ cmsg->cmsg_level =
+ linux_to_bsd_sockopt_level(linux_cmsg.cmsg_level);
+
+ datalen = linux_cmsg.cmsg_len - L_CMSGHDR_SIZE;
+ cmsg->cmsg_len = CMSG_LEN(datalen);
+ data = LINUX_CMSG_DATA(ptr_cmsg);
+
+ error = ENOBUFS;
+ if (!m_append(control, CMSGHDR_SIZE, (c_caddr_t) cmsg))
+ goto bad;
+ if (!m_append(control, datalen, (c_caddr_t) data))
+ goto bad;
+
+ } while ((ptr_cmsg = LINUX_CMSG_NXTHDR(mp, ptr_cmsg)));
+
+ free(cmsg, M_TEMP);
} else
control = NULL;
error = kern_sendit(td, s, mp, linux_to_bsd_msg_flags(flags), control,
segflg);
-
bad:
if (to)
FREE(to, M_SONAME);
@@ -1014,17 +1105,15 @@ struct linux_sendmsg_args {
static int
linux_sendmsg(struct thread *td, struct linux_sendmsg_args *args)
{
- struct linux_sendmsg_args linux_args;
struct msghdr msg;
+ struct l_msghdr linux_msg;
struct iovec *iov;
int error;
- /* XXXTJR sendmsg is broken on amd64 */
-
- error = copyin(args, &linux_args, sizeof(linux_args));
+ error = copyin(PTRIN(args->msg), &linux_msg, sizeof(linux_msg));
if (error)
return (error);
- error = copyin(PTRIN(linux_args.msg), &msg, sizeof(msg));
+ error = linux_to_bsd_msghdr(&msg, &linux_msg);
if (error)
return (error);
@@ -1042,8 +1131,7 @@ linux_sendmsg(struct thread *td, struct linux_sendmsg_args *args)
return (error);
msg.msg_iov = iov;
msg.msg_flags = 0;
- error = linux_sendit(td, linux_args.s, &msg, linux_args.flags,
- UIO_USERSPACE);
+ error = linux_sendit(td, args->s, &msg, args->flags, UIO_USERSPACE);
free(iov, M_IOV);
return (error);
}
@@ -1057,48 +1145,116 @@ struct linux_recvmsg_args {
static int
linux_recvmsg(struct thread *td, struct linux_recvmsg_args *args)
{
- struct linux_recvmsg_args linux_args;
- struct recvmsg_args /* {
- int s;
- struct msghdr *msg;
- int flags;
- } */ bsd_args;
struct msghdr msg;
- struct cmsghdr *cmsg;
+ struct l_msghdr linux_msg;
+ struct iovec *uiov, *iov;
+ struct mbuf *control = NULL;
+ struct mbuf **controlp;
+ struct l_cmsghdr *cmsg = NULL;
int error;
- /* XXXTJR recvmsg is broken on amd64 */
-
- if ((error = copyin(args, &linux_args, sizeof(linux_args))))
+ error = copyin(PTRIN(args->msg), &linux_msg, sizeof (linux_msg));
+ if (error)
return (error);
-
- if ((error = copyin(PTRIN(args->msg), &msg, sizeof (msg))))
+ error = linux_to_bsd_msghdr(&msg, &linux_msg);
+ if (error)
return (error);
-
- bsd_args.s = linux_args.s;
- bsd_args.msg = PTRIN(linux_args.msg);
- bsd_args.flags = linux_to_bsd_msg_flags(linux_args.flags);
- if (msg.msg_name) {
- linux_to_bsd_sockaddr((struct sockaddr *)msg.msg_name,
- msg.msg_namelen);
- error = recvmsg(td, &bsd_args);
- bsd_to_linux_sockaddr((struct sockaddr *)msg.msg_name);
- } else
- error = recvmsg(td, &bsd_args);
+ error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
if (error)
return (error);
- if (bsd_args.msg->msg_control != NULL &&
- bsd_args.msg->msg_controllen > 0) {
- cmsg = (struct cmsghdr*)bsd_args.msg->msg_control;
- cmsg->cmsg_level = bsd_to_linux_sockopt_level(cmsg->cmsg_level);
+ uiov = msg.msg_iov;
+ msg.msg_iov = iov;
+ msg.msg_flags = linux_to_bsd_msg_flags(linux_msg.msg_flags);
+
+ if (msg.msg_name) {
+ error = linux_to_bsd_sockaddr((struct sockaddr *)msg.msg_name,
+ msg.msg_namelen);
+ if (error)
+ goto bad;
}
- error = copyin(PTRIN(linux_args.msg), &msg, sizeof(msg));
+ controlp = (msg.msg_control != NULL) ? &control : NULL;
+ error = kern_recvit(td, args->s, &msg, UIO_USERSPACE, controlp);
if (error)
- return (error);
- if (msg.msg_name && msg.msg_namelen > 2)
- error = linux_sa_put(msg.msg_name);
+ goto bad;
+
+ msg.msg_iov = uiov;
+
+ error = bsd_to_linux_msghdr(&msg, &linux_msg);
+ if (error)
+ goto bad;
+
+ if (linux_msg.msg_name)
+ bsd_to_linux_sockaddr((struct sockaddr *)linux_msg.msg_name);
+ if (linux_msg.msg_name && linux_msg.msg_namelen > 2) {
+ error = linux_sa_put(linux_msg.msg_name);
+ if (error)
+ goto bad;
+ }
+
+ if (control) {
+ caddr_t outbuf;
+ struct cmsghdr *cm;
+
+ socklen_t datalen, outlen;
+ socklen_t clen;
+ void *data;
+
+ cmsg = malloc(L_CMSGHDR_SIZE, M_TEMP, M_WAITOK | M_ZERO);
+ outbuf = linux_msg.msg_control;
+ cm = mtod(control, struct cmsghdr *);
+ outlen = 0;
+ clen = control->m_len;
+
+ while (cm != NULL) {
+ data = CMSG_DATA(cm);
+ datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
+
+ outlen += LINUX_CMSG_LEN(datalen);
+ if (outlen > linux_msg.msg_controllen) {
+ linux_msg.msg_flags |= LINUX_MSG_CTRUNC;
+ break;
+ }
+
+ /*
+ * XXX here special handler of SCM_CREDS
+ */
+
+ cmsg->cmsg_len = LINUX_CMSG_LEN(datalen);
+ cmsg->cmsg_type = bsd_to_linux_cmsg_type(cm->cmsg_type);
+ cmsg->cmsg_level =
+ bsd_to_linux_sockopt_level(cm->cmsg_level);
+
+ error = copyout(cmsg, outbuf, L_CMSGHDR_SIZE);
+ if (error)
+ goto bad;
+ outbuf += L_CMSGHDR_SIZE;
+
+ error = copyout(data, outbuf, datalen);
+ if (error)
+ goto bad;
+ outbuf += LINUX_CMSG_ALIGN(datalen);
+ linux_msg.msg_controllen = outlen;
+
+ if (CMSG_SPACE(datalen) < clen) {
+ clen -= CMSG_SPACE(datalen);
+ cm = (struct cmsghdr *)
+ ((caddr_t)cm + CMSG_SPACE(datalen));
+ } else
+ cm = NULL;
+ }
+ }
+
+ error = copyout(&linux_msg, PTRIN(args->msg), sizeof(linux_msg));
+
+bad:
+ free(iov, M_IOV);
+
+ if (control != NULL)
+ m_freem(control);
+ if (cmsg != NULL)
+ free(cmsg, M_TEMP);
return (error);
}
diff --git a/src/sys/compat/linux/linux_socket.h b/src/sys/compat/linux/linux_socket.h
index 074e8e0..668ec7f 100644
--- a/src/sys/compat/linux/linux_socket.h
+++ b/src/sys/compat/linux/linux_socket.h
@@ -49,4 +49,49 @@
#define LINUX_MSG_ERRQUEUE 0x2000
#define LINUX_MSG_NOSIGNAL 0x4000
+/* Socket-level control message types */
+
+#define LINUX_SCM_RIGHTS 0x01
+#define LINUX_SCM_CREDENTIALS 0x02
+
+struct l_msghdr {
+ void *msg_name;
+ l_int msg_namelen;
+ struct iovec *msg_iov;
+ l_size_t msg_iovlen;
+ void *msg_control;
+ l_size_t msg_controllen;
+ unsigned msg_flags;
+};
+
+struct l_cmsghdr {
+ l_size_t cmsg_len;
+ l_int cmsg_level;
+ l_int cmsg_type;
+};
+
+/* Ancilliary data object information macros */
+
+#define LINUX_CMSG_ALIGN(len) (((len) + sizeof(long)-1) & ~(sizeof(long)-1))
+#define LINUX_CMSG_DATA(cmsg) ((void *)((char *)(cmsg) + \
+ LINUX_CMSG_ALIGN(sizeof(struct l_cmsghdr))))
+#define LINUX_CMSG_SPACE(len) (LINUX_CMSG_ALIGN(sizeof(struct l_cmsghdr)) + \
+ LINUX_CMSG_ALIGN(len))
+#define LINUX_CMSG_LEN(len) (LINUX_CMSG_ALIGN(sizeof(struct l_cmsghdr)) + \
+ (len))
+#define LINUX_CMSG_FIRSTHDR(msg) \
+ ((msg)->msg_controllen >= \
+ sizeof(struct l_cmsghdr) ? \
+ (struct l_cmsghdr *)((msg)->msg_control) : \
+ (struct l_cmsghdr *)(NULL))
+#define LINUX_CMSG_NXTHDR(msg, cmsg) \
+ ((((char *)(cmsg) + \
+ LINUX_CMSG_ALIGN((cmsg)->cmsg_len) + \
+ sizeof(*(cmsg))) > \
+ (((char *)(msg)->msg_control) + \
+ (msg)->msg_controllen)) ? \
+ (struct l_cmsghdr *) NULL : \
+ (struct l_cmsghdr *)((char *)(cmsg) + \
+ LINUX_CMSG_ALIGN((cmsg)->cmsg_len)))
+
#endif /* _LINUX_SOCKET_H_ */
--
Have fun!
chd
More information about the freebsd-emulation
mailing list