* [PATCH] cmsg.3: ensure buf is suitably aligned in sending example
@ 2014-10-13 8:14 David Wragg
0 siblings, 0 replies; 3+ messages in thread
From: David Wragg @ 2014-10-13 8:14 UTC (permalink / raw)
To: mtk.manpages-Re5JQEeQqe8AvxtiuMwx3w; +Cc: linux-man-u79uwXL29TY76Z2rM5mHXA
Inspection of the definition of CMSG_FIRSTHDR (both in glibc and the
suggested definition in RFC3542) shows that it yields the msg_control
field. So when sending, the pointer placed in msg_control should be
suitably aligned as a struct cmsghdr. In the sending example, buf was
declared as a bare char array, and so is not necessarily suitably aligned.
The solution here involves placing buf inside a union, and is based on
the sockets/scm_rights_send.c sample from The Linux Programming
Interface "dist" source code collection.
Signed-off-by: David Wragg <david-vIJKVkNGfPnYtjvyW6yDsg@public.gmane.org>
---
man3/cmsg.3 | 11 ++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)
diff --git a/man3/cmsg.3 b/man3/cmsg.3
index f355e6b..03f542e 100644
--- a/man3/cmsg.3
+++ b/man3/cmsg.3
@@ -198,11 +198,16 @@ UNIX domain socket using
struct msghdr msg = {0};
struct cmsghdr *cmsg;
int myfds[NUM_FD]; /* Contains the file descriptors to pass. */
-char buf[CMSG_SPACE(sizeof myfds)]; /* ancillary data buffer */
+union {
+ /* ancillary data buffer, wrapped in a union in order to ensure it is
+ suitably aligned */
+ char buf[CMSG_SPACE(sizeof myfds)];
+ struct cmsghdr align;
+} u;
int *fdptr;
-msg.msg_control = buf;
-msg.msg_controllen = sizeof buf;
+msg.msg_control = u.buf;
+msg.msg_controllen = sizeof u.buf;
cmsg = CMSG_FIRSTHDR(&msg);
cmsg\->cmsg_level = SOL_SOCKET;
cmsg\->cmsg_type = SCM_RIGHTS;
--
1.9.3
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH] cmsg.3: ensure buf is suitably aligned in sending example
@ 2014-11-02 23:08 David Wragg
[not found] ` <sa7fve1i4jw.fsf-vIJKVkNGfPnYtjvyW6yDsg@public.gmane.org>
0 siblings, 1 reply; 3+ messages in thread
From: David Wragg @ 2014-11-02 23:08 UTC (permalink / raw)
To: mtk.manpages-Re5JQEeQqe8AvxtiuMwx3w; +Cc: linux-man-u79uwXL29TY76Z2rM5mHXA
Inspection of the definition of CMSG_FIRSTHDR (both in glibc and the
suggested definition in RFC3542) shows that it yields the msg_control
field. So when sending, the pointer placed in msg_control should be
suitably aligned as a struct cmsghdr. In the sending example, buf was
declared as a bare char array, and so is not necessarily suitably
aligned.
The solution here involves placing buf inside a union, and is based on
the sockets/scm_rights_send.c sample from The Linux Programming
Interface "dist" source code collection.
Signed-off-by: David Wragg <david-vIJKVkNGfPnYtjvyW6yDsg@public.gmane.org>
---
man3/cmsg.3 | 11 ++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)
diff --git a/man3/cmsg.3 b/man3/cmsg.3
index f355e6b..03f542e 100644
--- a/man3/cmsg.3
+++ b/man3/cmsg.3
@@ -198,11 +198,16 @@ UNIX domain socket using
struct msghdr msg = {0};
struct cmsghdr *cmsg;
int myfds[NUM_FD]; /* Contains the file descriptors to pass. */
-char buf[CMSG_SPACE(sizeof myfds)]; /* ancillary data buffer */
+union {
+ /* ancillary data buffer, wrapped in a union in order to ensure it is
+ suitably aligned */
+ char buf[CMSG_SPACE(sizeof myfds)];
+ struct cmsghdr align;
+} u;
int *fdptr;
-msg.msg_control = buf;
-msg.msg_controllen = sizeof buf;
+msg.msg_control = u.buf;
+msg.msg_controllen = sizeof u.buf;
cmsg = CMSG_FIRSTHDR(&msg);
cmsg\->cmsg_level = SOL_SOCKET;
cmsg\->cmsg_type = SCM_RIGHTS;
--
1.9.3
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] cmsg.3: ensure buf is suitably aligned in sending example
[not found] ` <sa7fve1i4jw.fsf-vIJKVkNGfPnYtjvyW6yDsg@public.gmane.org>
@ 2014-11-11 9:04 ` Michael Kerrisk (man-pages)
0 siblings, 0 replies; 3+ messages in thread
From: Michael Kerrisk (man-pages) @ 2014-11-11 9:04 UTC (permalink / raw)
To: David Wragg
Cc: mtk.manpages-Re5JQEeQqe8AvxtiuMwx3w,
linux-man-u79uwXL29TY76Z2rM5mHXA
On 11/03/2014 12:08 AM, David Wragg wrote:
> Inspection of the definition of CMSG_FIRSTHDR (both in glibc and the
> suggested definition in RFC3542) shows that it yields the msg_control
> field. So when sending, the pointer placed in msg_control should be
> suitably aligned as a struct cmsghdr. In the sending example, buf was
> declared as a bare char array, and so is not necessarily suitably
> aligned.
>
> The solution here involves placing buf inside a union, and is based on
> the sockets/scm_rights_send.c sample from The Linux Programming
> Interface "dist" source code collection.
Thanks, David. Applied.
Cheers,
Michael
> Signed-off-by: David Wragg <david-vIJKVkNGfPnYtjvyW6yDsg@public.gmane.org>
> ---
> man3/cmsg.3 | 11 ++++++++---
> 1 file changed, 8 insertions(+), 3 deletions(-)
>
> diff --git a/man3/cmsg.3 b/man3/cmsg.3
> index f355e6b..03f542e 100644
> --- a/man3/cmsg.3
> +++ b/man3/cmsg.3
> @@ -198,11 +198,16 @@ UNIX domain socket using
> struct msghdr msg = {0};
> struct cmsghdr *cmsg;
> int myfds[NUM_FD]; /* Contains the file descriptors to pass. */
> -char buf[CMSG_SPACE(sizeof myfds)]; /* ancillary data buffer */
> +union {
> + /* ancillary data buffer, wrapped in a union in order to ensure it is
> + suitably aligned */
> + char buf[CMSG_SPACE(sizeof myfds)];
> + struct cmsghdr align;
> +} u;
> int *fdptr;
>
> -msg.msg_control = buf;
> -msg.msg_controllen = sizeof buf;
> +msg.msg_control = u.buf;
> +msg.msg_controllen = sizeof u.buf;
> cmsg = CMSG_FIRSTHDR(&msg);
> cmsg\->cmsg_level = SOL_SOCKET;
> cmsg\->cmsg_type = SCM_RIGHTS;
>
--
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2014-11-11 9:04 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-11-02 23:08 [PATCH] cmsg.3: ensure buf is suitably aligned in sending example David Wragg
[not found] ` <sa7fve1i4jw.fsf-vIJKVkNGfPnYtjvyW6yDsg@public.gmane.org>
2014-11-11 9:04 ` Michael Kerrisk (man-pages)
-- strict thread matches above, loose matches on Subject: below --
2014-10-13 8:14 David Wragg
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).