root/opal/util/fd.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. opal_fd_read
  2. opal_fd_write
  3. opal_fd_set_cloexec
  4. opal_fd_is_regular
  5. opal_fd_is_chardev
  6. opal_fd_is_blkdev
  7. opal_fd_get_peer_name

   1 /*
   2  * Copyright (c) 2008-2018 Cisco Systems, Inc.  All rights reserved
   3  * Copyright (c) 2009 Sandia National Laboratories. All rights reserved.
   4  * Copyright (c) 2017      Mellanox Technologies. All rights reserved.
   5  *
   6  * $COPYRIGHT$
   7  *
   8  * Additional copyrights may follow
   9  *
  10  * $HEADER$
  11  */
  12 
  13 #include "opal_config.h"
  14 
  15 #ifdef HAVE_SYS_TYPES_H
  16 #include <sys/types.h>
  17 #endif
  18 #ifdef HAVE_SYS_STAT_H
  19 #include <sys/stat.h>
  20 #endif
  21 #ifdef HAVE_SYS_SOCKET_H
  22 #include <sys/socket.h>
  23 #endif
  24 #ifdef HAVE_ARPA_INET_H
  25 #include <arpa/inet.h>
  26 #endif
  27 #ifdef HAVE_NETINET_IN_H
  28 #include <netinet/in.h>
  29 #endif
  30 #ifdef HAVE_UNISTD_H
  31 #include <unistd.h>
  32 #endif
  33 #include <errno.h>
  34 #include <fcntl.h>
  35 #include <stdlib.h>
  36 #include <string.h>
  37 
  38 #include "opal/util/fd.h"
  39 #include "opal/util/string_copy.h"
  40 #include "opal/constants.h"
  41 
  42 
  43 /*
  44  * Simple loop over reading from a fd
  45  */
  46 int opal_fd_read(int fd, int len, void *buffer)
  47 {
  48     int rc;
  49     char *b = buffer;
  50 
  51     while (len > 0) {
  52         rc = read(fd, b, len);
  53         if (rc < 0 && (EAGAIN == errno || EINTR == errno)) {
  54             continue;
  55         } else if (rc > 0) {
  56             len -= rc;
  57             b += rc;
  58         } else if (0 == rc) {
  59             return OPAL_ERR_TIMEOUT;
  60         } else {
  61             return OPAL_ERR_IN_ERRNO;
  62         }
  63     }
  64     return OPAL_SUCCESS;
  65 }
  66 
  67 
  68 /*
  69  * Simple loop over writing to an fd
  70  */
  71 int opal_fd_write(int fd, int len, const void *buffer)
  72 {
  73     int rc;
  74     const char *b = buffer;
  75 
  76     while (len > 0) {
  77         rc = write(fd, b, len);
  78         if (rc < 0 && (EAGAIN == errno || EINTR == errno)) {
  79             continue;
  80         } else if (rc > 0) {
  81             len -= rc;
  82             b += rc;
  83         } else {
  84             return OPAL_ERR_IN_ERRNO;
  85         }
  86     }
  87 
  88     return OPAL_SUCCESS;
  89 }
  90 
  91 
  92 int opal_fd_set_cloexec(int fd)
  93 {
  94 #ifdef FD_CLOEXEC
  95     int flags;
  96 
  97     /* Stevens says that we should get the fd's flags before we set
  98        them.  So say we all. */
  99     flags = fcntl(fd, F_GETFD, 0);
 100     if (-1 == flags) {
 101         return OPAL_ERR_IN_ERRNO;
 102     }
 103 
 104     if (fcntl(fd, F_SETFD, FD_CLOEXEC | flags) == -1) {
 105         return OPAL_ERR_IN_ERRNO;
 106     }
 107 #endif
 108 
 109     return OPAL_SUCCESS;
 110 }
 111 
 112 bool opal_fd_is_regular(int fd)
 113 {
 114     struct stat buf;
 115     if (fstat(fd, &buf)) {
 116         return false;
 117     }
 118     return S_ISREG(buf.st_mode);
 119 }
 120 
 121 bool opal_fd_is_chardev(int fd)
 122 {
 123     struct stat buf;
 124     if (fstat(fd, &buf)) {
 125         return false;
 126     }
 127     return S_ISCHR(buf.st_mode);
 128 }
 129 
 130 bool opal_fd_is_blkdev(int fd)
 131 {
 132     struct stat buf;
 133     if (fstat(fd, &buf)) {
 134         return false;
 135     }
 136     return S_ISBLK(buf.st_mode);
 137 }
 138 
 139 const char *opal_fd_get_peer_name(int fd)
 140 {
 141     char *str;
 142     const char *ret = NULL;
 143     struct sockaddr sa;
 144     socklen_t slt = (socklen_t) sizeof(sa);
 145 
 146     int rc = getpeername(fd, &sa, &slt);
 147     if (0 != rc) {
 148         ret = strdup("Unknown");
 149         return ret;
 150     }
 151 
 152     size_t len = INET_ADDRSTRLEN;
 153 #if OPAL_ENABLE_IPV6
 154     len = INET6_ADDRSTRLEN;
 155 #endif
 156     str = calloc(1, len);
 157     if (NULL == str) {
 158         return NULL;
 159     }
 160 
 161     if (sa.sa_family == AF_INET) {
 162         struct sockaddr_in *si;
 163         si = (struct sockaddr_in*) &sa;
 164         ret = inet_ntop(AF_INET, &(si->sin_addr), str, INET_ADDRSTRLEN);
 165         if (NULL == ret) {
 166             free(str);
 167         }
 168     }
 169 #if OPAL_ENABLE_IPV6
 170     else if (sa.sa_family == AF_INET6) {
 171         struct sockaddr_in6 *si6;
 172         si6 = (struct sockaddr_in6*) &sa;
 173         ret = inet_ntop(AF_INET6, &(si6->sin6_addr), str, INET6_ADDRSTRLEN);
 174         if (NULL == ret) {
 175             free(str);
 176         }
 177     }
 178 #endif
 179     else {
 180         // This string is guaranteed to be <= INET_ADDRSTRLEN
 181         opal_string_copy(str, "Unknown", len);
 182         ret = str;
 183     }
 184 
 185     return ret;
 186 }

/* [<][>][^][v][top][bottom][index][help] */