forked from google/boringssl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Abstract fd operations better in tool.
Windows and POSIX implement very similar fd operations, but differ slightly: - ssize_t in POSIX is usually int on Windows. - POSIX needs EINTR retry loops. - Windows wants _open rather than open, etc. - POSIX fds and sockets are the same thing, while Windows sockets are HANDLEs and leaves fd as a C runtime construct. Rather than ad-hoc macros and redefinitions of ssize_t (which reportedly upset MINGW), add some actual abstractions. While I'm here, add a scoped file descriptor type. That still leaves recv/send which are only used in one file, so defined a socket_result_t for them. Change-Id: I17fca2a50c77191f573852bfd27553996e3e9c3f Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/41725 Commit-Queue: David Benjamin <davidben@google.com> Reviewed-by: Adam Langley <agl@google.com>
- Loading branch information
Showing
6 changed files
with
232 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ add_executable( | |
client.cc | ||
const.cc | ||
digest.cc | ||
fd.cc | ||
file.cc | ||
generate_ed25519.cc | ||
genrsa.cc | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* Copyright (c) 2020, Google Inc. | ||
* | ||
* Permission to use, copy, modify, and/or distribute this software for any | ||
* purpose with or without fee is hereby granted, provided that the above | ||
* copyright notice and this permission notice appear in all copies. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY | ||
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION | ||
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN | ||
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ | ||
|
||
#include <openssl/base.h> | ||
|
||
#include <errno.h> | ||
#include <limits.h> | ||
#include <stdio.h> | ||
|
||
#include <algorithm> | ||
|
||
#include "internal.h" | ||
|
||
#if defined(OPENSSL_WINDOWS) | ||
#include <io.h> | ||
#else | ||
#include <fcntl.h> | ||
#include <unistd.h> | ||
#endif | ||
|
||
|
||
ScopedFD OpenFD(const char *path, int flags) { | ||
#if defined(OPENSSL_WINDOWS) | ||
return ScopedFD(_open(path, flags)); | ||
#else | ||
int fd; | ||
do { | ||
fd = open(path, flags); | ||
} while (fd == -1 && errno == EINTR); | ||
return ScopedFD(fd); | ||
#endif | ||
} | ||
|
||
void CloseFD(int fd) { | ||
#if defined(OPENSSL_WINDOWS) | ||
_close(fd); | ||
#else | ||
close(fd); | ||
#endif | ||
} | ||
|
||
bool ReadFromFD(int fd, size_t *out_bytes_read, void *out, size_t num) { | ||
#if defined(OPENSSL_WINDOWS) | ||
// On Windows, the buffer must be at most |INT_MAX|. See | ||
// https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/read?view=vs-2019 | ||
int ret = _read(fd, out, std::min(size_t{INT_MAX}, num)); | ||
#else | ||
ssize_t ret; | ||
do { | ||
ret = read(fd, out, num); | ||
} while (ret == -1 && errno == EINVAL); | ||
#endif | ||
|
||
if (ret < 0) { | ||
*out_bytes_read = 0; | ||
return false; | ||
} | ||
*out_bytes_read = ret; | ||
return true; | ||
} | ||
|
||
bool WriteToFD(int fd, size_t *out_bytes_written, const void *in, size_t num) { | ||
#if defined(OPENSSL_WINDOWS) | ||
// The documentation for |_write| does not say the buffer must be at most | ||
// |INT_MAX|, but clamp it to |INT_MAX| instead of |UINT_MAX| in case. | ||
int ret = _write(fd, in, std::min(size_t{INT_MAX}, num)); | ||
#else | ||
ssize_t ret; | ||
do { | ||
ret = write(fd, in, num); | ||
} while (ret == -1 && errno == EINVAL); | ||
#endif | ||
|
||
if (ret < 0) { | ||
*out_bytes_written = 0; | ||
return false; | ||
} | ||
*out_bytes_written = ret; | ||
return true; | ||
} | ||
|
||
ScopedFILE FDToFILE(ScopedFD fd, const char *mode) { | ||
ScopedFILE ret; | ||
#if defined(OPENSSL_WINDOWS) | ||
ret.reset(_fdopen(fd.get(), mode)); | ||
#else | ||
ret.reset(fdopen(fd.get(), mode)); | ||
#endif | ||
// |fdopen| takes ownership of |fd| on success. | ||
if (ret) { | ||
fd.release(); | ||
} | ||
return ret; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.