Blame sysdeps/unix/sysv/linux/netlink_assert_response.c

Packit 6c4009
/* Check recvmsg results for netlink sockets.
Packit 6c4009
   Copyright (C) 2015-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <sys/socket.h>
Packit 6c4009
Packit 6c4009
#include "netlinkaccess.h"
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
get_address_family (int fd)
Packit 6c4009
{
Packit 6c4009
  struct sockaddr_storage sa;
Packit 6c4009
  socklen_t sa_len = sizeof (sa);
Packit 6c4009
  if (__getsockname (fd, (struct sockaddr *) &sa, &sa_len) < 0)
Packit 6c4009
    return -1;
Packit 6c4009
  /* Check that the socket family number is preserved despite in-band
Packit 6c4009
     signaling.  */
Packit 6c4009
  _Static_assert (sizeof (sa.ss_family) < sizeof (int), "address family size");
Packit 6c4009
  _Static_assert (0 < (__typeof__ (sa.ss_family)) -1,
Packit 6c4009
                  "address family unsigned");
Packit 6c4009
  return sa.ss_family;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
__netlink_assert_response (int fd, ssize_t result)
Packit 6c4009
{
Packit 6c4009
  if (result < 0)
Packit 6c4009
    {
Packit 6c4009
      /* Check if the error is unexpected.  */
Packit 6c4009
      bool terminate = false;
Packit 6c4009
      int error_code = errno;
Packit 6c4009
      int family = get_address_family (fd);
Packit 6c4009
      if (family != AF_NETLINK)
Packit 6c4009
        /* If the address family does not match (or getsockname
Packit 6c4009
           failed), report the original error.  */
Packit 6c4009
        terminate = true;
Packit 6c4009
      else if (error_code == EBADF
Packit 6c4009
          || error_code == ENOTCONN
Packit 6c4009
          || error_code == ENOTSOCK
Packit 6c4009
          || error_code == ECONNREFUSED)
Packit 6c4009
        /* These errors indicate that the descriptor is not a
Packit 6c4009
           connected socket.  */
Packit 6c4009
        terminate = true;
Packit 6c4009
      else if (error_code == EAGAIN || error_code == EWOULDBLOCK)
Packit 6c4009
        {
Packit 6c4009
          /* The kernel might return EAGAIN for other reasons than a
Packit 6c4009
             non-blocking socket.  But if the socket is not blocking,
Packit 6c4009
             it is not ours, so report the error.  */
Packit 6c4009
          int mode = __fcntl (fd, F_GETFL, 0);
Packit 6c4009
          if (mode < 0 || (mode & O_NONBLOCK) != 0)
Packit 6c4009
            terminate = true;
Packit 6c4009
        }
Packit 6c4009
      if (terminate)
Packit 6c4009
        {
Packit 6c4009
          char message[200];
Packit 6c4009
          if (family < 0)
Packit 6c4009
            __snprintf (message, sizeof (message),
Packit Service f2997a
                        "Unexpected error %d on netlink descriptor %d.\n",
Packit 6c4009
                        error_code, fd);
Packit 6c4009
          else
Packit 6c4009
            __snprintf (message, sizeof (message),
Packit 6c4009
                        "Unexpected error %d on netlink descriptor %d"
Packit Service f2997a
                        " (address family %d).\n",
Packit 6c4009
                        error_code, fd, family);
Packit 6c4009
          __libc_fatal (message);
Packit 6c4009
        }
Packit 6c4009
      else
Packit 6c4009
        /* Restore orignal errno value.  */
Packit 6c4009
        __set_errno (error_code);
Packit 6c4009
    }
Packit 6c4009
  else if (result < sizeof (struct nlmsghdr))
Packit 6c4009
    {
Packit 6c4009
      char message[200];
Packit 6c4009
      int family = get_address_family (fd);
Packit 6c4009
      if (family < 0)
Packit 6c4009
          __snprintf (message, sizeof (message),
Packit 6c4009
                      "Unexpected netlink response of size %zd"
Packit 6c4009
                      " on descriptor %d",
Packit 6c4009
                      result, fd);
Packit 6c4009
      else
Packit 6c4009
          __snprintf (message, sizeof (message),
Packit 6c4009
                      "Unexpected netlink response of size %zd"
Packit 6c4009
                      " on descriptor %d (address family %d)",
Packit 6c4009
                      result, fd, family);
Packit 6c4009
      __libc_fatal (message);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
libc_hidden_def (__netlink_assert_response)