This source file includes following definitions.
- pmix_fd_read
- pmix_fd_write
- pmix_fd_set_cloexec
- pmix_fd_is_regular
- pmix_fd_is_chardev
- pmix_fd_is_blkdev
   1 
   2 
   3 
   4 
   5 
   6 
   7 
   8 
   9 
  10 
  11 
  12 
  13 
  14 #include <src/include/pmix_config.h>
  15 
  16 #include <pmix_common.h>
  17 
  18 #ifdef HAVE_UNISTD_H
  19 #include <unistd.h>
  20 #endif
  21 #include <errno.h>
  22 #include <fcntl.h>
  23 #ifdef HAVE_SYS_TYPES_H
  24 #include <sys/types.h>
  25 #endif
  26 #ifdef HAVE_SYS_STAT_H
  27 #include <sys/stat.h>
  28 #endif
  29 
  30 #include "src/util/error.h"
  31 #include "src/util/fd.h"
  32 
  33 
  34 
  35 
  36 
  37 pmix_status_t pmix_fd_read(int fd, int len, void *buffer)
  38 {
  39     int rc;
  40     char *b = buffer;
  41 
  42     while (len > 0) {
  43         rc = read(fd, b, len);
  44         if (rc < 0 && (EAGAIN == errno || EINTR == errno)) {
  45             continue;
  46         } else if (rc > 0) {
  47             len -= rc;
  48             b += rc;
  49         } else if (0 == rc) {
  50             return PMIX_ERR_TIMEOUT;
  51         } else {
  52             return PMIX_ERR_IN_ERRNO;
  53         }
  54     }
  55     return PMIX_SUCCESS;
  56 }
  57 
  58 
  59 
  60 
  61 
  62 pmix_status_t pmix_fd_write(int fd, int len, const void *buffer)
  63 {
  64     int rc;
  65     const char *b = buffer;
  66 
  67     while (len > 0) {
  68         rc = write(fd, b, len);
  69         if (rc < 0 && (EAGAIN == errno || EINTR == errno)) {
  70             continue;
  71         } else if (rc > 0) {
  72             len -= rc;
  73             b += rc;
  74         } else {
  75             return PMIX_ERR_IN_ERRNO;
  76         }
  77     }
  78 
  79     return PMIX_SUCCESS;
  80 }
  81 
  82 
  83 pmix_status_t pmix_fd_set_cloexec(int fd)
  84 {
  85 #ifdef FD_CLOEXEC
  86     int flags;
  87 
  88     
  89 
  90     flags = fcntl(fd, F_GETFD, 0);
  91     if (-1 == flags) {
  92         return PMIX_ERR_IN_ERRNO;
  93     }
  94 
  95     if (fcntl(fd, F_SETFD, FD_CLOEXEC | flags) == -1) {
  96         return PMIX_ERR_IN_ERRNO;
  97     }
  98 #endif
  99 
 100     return PMIX_SUCCESS;
 101 }
 102 
 103 
 104 bool pmix_fd_is_regular(int fd)
 105 {
 106     struct stat buf;
 107     if (fstat(fd, &buf)) {
 108         return false;
 109     }
 110     return S_ISREG(buf.st_mode);
 111 }
 112 
 113 bool pmix_fd_is_chardev(int fd)
 114 {
 115     struct stat buf;
 116     if (fstat(fd, &buf)) {
 117         return false;
 118     }
 119     return S_ISCHR(buf.st_mode);
 120 }
 121 
 122 bool pmix_fd_is_blkdev(int fd)
 123 {
 124     struct stat buf;
 125     if (fstat(fd, &buf)) {
 126         return false;
 127     }
 128     return S_ISBLK(buf.st_mode);
 129 }