Unverified Commit 89894b67 authored by Pavel Labath's avatar Pavel Labath Committed by GitHub
Browse files

[libc] Add struct cmsghdr and associated macros (#193756)



The macros are the main source of subtlety. The interesting aspects are:
- some implementations CMSG_ALIGN the size of struct cmsghdr, but this
is a noop. Instead of doing that, I added an assertion in the test.
- POSIX permits CMSG_NXTHDR to return null if the buffer has no space
for the data array, and this behavior differs between implementations.
This implementation does not do that in order to match CMSG_FIRSTHDR,
which doesn't have such an option.
- some implementations redirect the CMSG_NXTHDR macro to an (extern or
static inline) function. I implemented this inside the macro to avoid
having to define a (private ?) entry point for that function.

---------

Co-authored-by: default avatarJeff Bailey <jbailey@raspberryginger.com>
parent 7189c4bb
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -394,6 +394,15 @@ add_proxy_header_library(
    libc.include.sys_socket
)

add_proxy_header_library(
  struct_cmsghdr
  HDRS
    struct_cmsghdr.h
  FULL_BUILD_DEPENDS
    libc.include.llvm-libc-types.struct_cmsghdr
    libc.include.sys_socket
)

add_proxy_header_library(
  struct_msghdr
  HDRS
+21 −0
Original line number Diff line number Diff line
//===-- Proxy for struct cmsghdr ------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_LIBC_HDR_TYPES_STRUCT_CMSGHDR_H
#define LLVM_LIBC_HDR_TYPES_STRUCT_CMSGHDR_H

#ifdef LIBC_FULL_BUILD

#include "include/llvm-libc-types/struct_cmsghdr.h"

#else

#include <sys/socket.h>

#endif // LIBC_FULL_BUILD

#endif // LLVM_LIBC_HDR_TYPES_STRUCT_CMSGHDR_H
+1 −0
Original line number Diff line number Diff line
@@ -740,6 +740,7 @@ add_header_macro(
    .llvm-libc-macros.sys_socket_macros
    .llvm-libc-types.sa_family_t
    .llvm-libc-types.socklen_t
    .llvm-libc-types.struct_cmsghdr
    .llvm-libc-types.struct_iovec
    .llvm-libc-types.struct_linger
    .llvm-libc-types.struct_msghdr
+19 −0
Original line number Diff line number Diff line
@@ -50,4 +50,23 @@
#define SHUT_WR 1
#define SHUT_RDWR 2

#define SCM_RIGHTS 1

#define CMSG_ALIGN(len) (((len) + sizeof(size_t) - 1) & ~(sizeof(size_t) - 1))
#define CMSG_LEN(len) (sizeof(struct cmsghdr) + (len))
#define CMSG_SPACE(len) (sizeof(struct cmsghdr) + CMSG_ALIGN(len))

#define CMSG_FIRSTHDR(msg)                                                     \
  ((msg)->msg_controllen >= sizeof(struct cmsghdr)                             \
       ? (struct cmsghdr *)(msg)->msg_control                                  \
       : 0)
#define __CMSG_NXTHDR_CANDIDATE(cmsg)                                          \
  ((struct cmsghdr *)((unsigned char *)(cmsg) + CMSG_ALIGN((cmsg)->cmsg_len)))
#define CMSG_NXTHDR(msg, cmsg)                                                 \
  ((char *)(__CMSG_NXTHDR_CANDIDATE(cmsg) + 1) <=                              \
           ((char *)((msg)->msg_control) + (msg)->msg_controllen)              \
       ? __CMSG_NXTHDR_CANDIDATE(cmsg)                                         \
       : 0)
#define CMSG_DATA(cmsg) ((unsigned char *)((struct cmsghdr *)(cmsg) + 1))

#endif // LLVM_LIBC_MACROS_LINUX_SYS_SOCKET_MACROS_H
+1 −0
Original line number Diff line number Diff line
@@ -200,6 +200,7 @@ add_header(struct_sockaddr HDR struct_sockaddr.h DEPENDS .sa_family_t)
add_header(struct_sockaddr_storage HDR struct_sockaddr_storage.h DEPENDS .sa_family_t)
add_header(struct_iovec HDR struct_iovec.h DEPENDS .size_t)
add_header(struct_linger HDR struct_linger.h)
add_header(struct_cmsghdr HDR struct_cmsghdr.h DEPENDS .size_t)
add_header(struct_msghdr HDR struct_msghdr.h DEPENDS .size_t .socklen_t .struct_iovec)
add_header(ACTION HDR ACTION.h)
add_header(ENTRY HDR ENTRY.h)
Loading