mirror of https://github.com/mkerrisk/man-pages
cmsg.3: ensure buf is suitably aligned in sending example
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@wragg.org> Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
This commit is contained in:
parent
e0f1f1765b
commit
a7bd3dab97
11
man3/cmsg.3
11
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;
|
||||
|
|
Loading…
Reference in New Issue