about summary refs log tree commit diff
path: root/src/libwpactrl/wpactrl_fd_recv.c
blob: 3e2a006c739ea2a2539fb204c0495fdf1cc0dd94 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/* ISC license. */

#include <skalibs/sysdeps.h>
#include <skalibs/nonposix.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <errno.h>
#include <bcnm/wpactrl.h>
#include "wpactrl-internal.h"

ssize_t wpactrl_fd_recv (int fd, char *s, size_t len)
{
  static int const bsd_braindeadness_workaround_flags =
#ifdef SKALIBS_HASMSGDONTWAIT
    MSG_DONTWAIT
#else
    0
#endif
    |
#ifdef SKALIBS_HASNBWAITALL
    MSG_WAITALL
#else
    0
#endif
    |
#ifdef SKALIBS_HASCMSGCLOEXEC
    MSG_CMSG_CLOEXEC
#else
    0
#endif
    ;
  struct iovec iov = { .iov_base = s, .iov_len = len } ;
  struct msghdr msghdr =
  {
    .msg_name = 0,
    .msg_namelen = 0,
    .msg_iov = &iov,
    .msg_iovlen = 1,
    .msg_flags = 0,
    .msg_control = 0,
    .msg_controllen = 0
  } ;
  ssize_t r ;
  do r = recvmsg(fd, &msghdr, bsd_braindeadness_workaround_flags) ;
  while (r == -1 && errno == EINTR) ;
  return r > 0 && msghdr.msg_flags | MSG_TRUNC ? (errno = EMSGSIZE, -1) : r ;
}