1
1
openmpi/opal/util/fd.c
Artem Polyakov d9ad918a14 orte/iof: Address the case when output is a regular file
Regular files are always write-ready, so non-blocking I/O does not
give any benefits for them.
More than that - if libevent is using "epoll" to track fd events,
epoll_ctl will refuse attempt to add an fd pointing to a regular
file descriptor with EPERM.
This fix checks the object referenced by fd and avoids event_add
using event_active instead.

In the original configuration that uncovered this issue "epoll"
was used in libevent, it was triggering the following warning
message:
"[warn] Epoll ADD(1) on fd 0 failed.  Old events were 0; read
change was 1 (add); write change was 0 (none): Operation not
permitted"
And the side effect was accumulation of all output in mpirun
memory and actually writing it only at mpirun exit.

Signed-off-by: Artem Polyakov <artpol84@gmail.com>
2017-07-01 02:24:14 +07:00

129 строки
2.3 KiB
C

/*
* Copyright (c) 2008-2014 Cisco Systems, Inc. All rights reserved.
* Copyright (c) 2009 Sandia National Laboratories. All rights reserved.
* Copyright (c) 2017 Mellanox Technologies. All rights reserved.
*
* $COPYRIGHT$
*
* Additional copyrights may follow
*
* $HEADER$
*/
#include "opal_config.h"
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <errno.h>
#include <fcntl.h>
#include "opal/util/fd.h"
#include "opal/constants.h"
/*
* Simple loop over reading from a fd
*/
int opal_fd_read(int fd, int len, void *buffer)
{
int rc;
char *b = buffer;
while (len > 0) {
rc = read(fd, b, len);
if (rc < 0 && (EAGAIN == errno || EINTR == errno)) {
continue;
} else if (rc > 0) {
len -= rc;
b += rc;
} else if (0 == rc) {
return OPAL_ERR_TIMEOUT;
} else {
return OPAL_ERR_IN_ERRNO;
}
}
return OPAL_SUCCESS;
}
/*
* Simple loop over writing to an fd
*/
int opal_fd_write(int fd, int len, const void *buffer)
{
int rc;
const char *b = buffer;
while (len > 0) {
rc = write(fd, b, len);
if (rc < 0 && (EAGAIN == errno || EINTR == errno)) {
continue;
} else if (rc > 0) {
len -= rc;
b += rc;
} else {
return OPAL_ERR_IN_ERRNO;
}
}
return OPAL_SUCCESS;
}
int opal_fd_set_cloexec(int fd)
{
#ifdef FD_CLOEXEC
int flags;
/* Stevens says that we should get the fd's flags before we set
them. So say we all. */
flags = fcntl(fd, F_GETFD, 0);
if (-1 == flags) {
return OPAL_ERR_IN_ERRNO;
}
if (fcntl(fd, F_SETFD, FD_CLOEXEC | flags) == -1) {
return OPAL_ERR_IN_ERRNO;
}
#endif
return OPAL_SUCCESS;
}
bool opal_fd_is_regular(int fd)
{
struct stat buf;
if (fstat(fd, &buf)) {
return false;
}
return S_ISREG(buf.st_mode);
}
bool opal_fd_is_chardev(int fd)
{
struct stat buf;
if (fstat(fd, &buf)) {
return false;
}
return S_ISCHR(buf.st_mode);
}
bool opal_fd_is_blkdev(int fd)
{
struct stat buf;
if (fstat(fd, &buf)) {
return false;
}
return S_ISBLK(buf.st_mode);
}