Add trivial functions to loop over read()'ing and write()'ing with a
file descriptor (i.e., read and write complete messages, transparently handling partial reads/writes, EAGAIN, and EINTR). This code effectively already exists in a few places in the code base; this is mainly a consolidation. This commit was SVN r23450.
Этот коммит содержится в:
родитель
35690ecad5
Коммит
ab5fc1b570
@ -9,7 +9,7 @@
|
|||||||
# University of Stuttgart. All rights reserved.
|
# University of Stuttgart. All rights reserved.
|
||||||
# Copyright (c) 2004-2005 The Regents of the University of California.
|
# Copyright (c) 2004-2005 The Regents of the University of California.
|
||||||
# All rights reserved.
|
# All rights reserved.
|
||||||
# Copyright (c) 2007-2009 Cisco Systems, Inc. All rights reserved.
|
# Copyright (c) 2007-2010 Cisco Systems, Inc. All rights reserved.
|
||||||
# $COPYRIGHT$
|
# $COPYRIGHT$
|
||||||
#
|
#
|
||||||
# Additional copyrights may follow
|
# Additional copyrights may follow
|
||||||
@ -39,6 +39,8 @@ headers = \
|
|||||||
daemon_init.h \
|
daemon_init.h \
|
||||||
error.h \
|
error.h \
|
||||||
few.h \
|
few.h \
|
||||||
|
fd.h \
|
||||||
|
fd.c \
|
||||||
if.h \
|
if.h \
|
||||||
keyval_parse.h \
|
keyval_parse.h \
|
||||||
malloc.h \
|
malloc.h \
|
||||||
|
67
opal/util/fd.c
Обычный файл
67
opal/util/fd.c
Обычный файл
@ -0,0 +1,67 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2008-2009 Cisco Systems, Inc. All rights reserved.
|
||||||
|
* Copyright (c) 2009 Sandia National Laboratories. All rights reserved.
|
||||||
|
*
|
||||||
|
* $COPYRIGHT$
|
||||||
|
*
|
||||||
|
* Additional copyrights may follow
|
||||||
|
*
|
||||||
|
* $HEADER$
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "opal_config.h"
|
||||||
|
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <errno.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 {
|
||||||
|
return OPAL_ERR_IN_ERRNO;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return OPAL_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Simple loop over writing to an fd
|
||||||
|
*/
|
||||||
|
int opal_fd_write(int fd, int len, void *buffer)
|
||||||
|
{
|
||||||
|
int rc;
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
49
opal/util/fd.h
Обычный файл
49
opal/util/fd.h
Обычный файл
@ -0,0 +1,49 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2008-2009 Cisco Systems, Inc. All rights reserved.
|
||||||
|
* Copyright (c) 2009 Sandia National Laboratories. All rights reserved.
|
||||||
|
*
|
||||||
|
* $COPYRIGHT$
|
||||||
|
*
|
||||||
|
* Additional copyrights may follow
|
||||||
|
*
|
||||||
|
* $HEADER$
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* @file */
|
||||||
|
|
||||||
|
#ifndef OPAL_UTIL_FD_H_
|
||||||
|
#define OPAL_UTIL_FD_H_
|
||||||
|
|
||||||
|
#include "opal_config.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read a complete buffer from a file descriptor.
|
||||||
|
*
|
||||||
|
* @param fd File descriptor
|
||||||
|
* @param len Number of bytes to read
|
||||||
|
* @param buffer Pre-allocated buffer (large enough to hold len bytes)
|
||||||
|
*
|
||||||
|
* @returns OPAL_SUCCESS upon success.
|
||||||
|
* @returns OPAL_ERR_IN_ERRNO otherwise.
|
||||||
|
*
|
||||||
|
* Loop over reading from the fd until len bytes are read or an error
|
||||||
|
* occurs. EAGAIN and EINTR are transparently handled.
|
||||||
|
*/
|
||||||
|
OPAL_DECLSPEC int opal_fd_read(int fd, int len, void *buffer);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write a complete buffer to a file descriptor.
|
||||||
|
*
|
||||||
|
* @param fd File descriptor
|
||||||
|
* @param len Number of bytes to write
|
||||||
|
* @param buffer Buffer to write from
|
||||||
|
*
|
||||||
|
* @returns OPAL_SUCCESS upon success.
|
||||||
|
* @returns OPAL_ERR_IN_ERRNO otherwise.
|
||||||
|
*
|
||||||
|
* Loop over writing to the fd until len bytes are written or an error
|
||||||
|
* occurs. EAGAIN and EINTR are transparently handled.
|
||||||
|
*/
|
||||||
|
OPAL_DECLSPEC int opal_fd_write(int fd, int len, void *buffer);
|
||||||
|
|
||||||
|
#endif
|
Загрузка…
x
Ссылка в новой задаче
Block a user