Skip to content

Commit

Permalink
Cherry pick PR #2971: Enabling errno across platform (#3191)
Browse files Browse the repository at this point in the history
Refer to the original PR: #2971

b/321999529

Test-On-Device: true

---------

Co-authored-by: Hao <[email protected]>
Co-authored-by: Yijia Zhang <[email protected]>
  • Loading branch information
3 people authored Jun 13, 2024
1 parent b53244b commit 7d99be1
Show file tree
Hide file tree
Showing 27 changed files with 356 additions and 51 deletions.
1 change: 1 addition & 0 deletions starboard/android/shared/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ static_library("starboard_platform") {
"player_set_max_video_input_size.cc",
"player_set_max_video_input_size.h",
"player_set_playback_rate.cc",
"posix_emu/errno.cc",
"posix_emu/file.cc",
"posix_emu/pthread.cc",
"posix_emu/stat.cc",
Expand Down
2 changes: 1 addition & 1 deletion starboard/android/shared/platform_configuration/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ config("platform_configuration") {
defines = []

include_dirs = [
# POSIX emulation headers
# POSIX emulation headers - these need to come *before* system include_dirs.
"//starboard/android/shared/posix_emu/include",
]

Expand Down
19 changes: 19 additions & 0 deletions starboard/android/shared/posix_emu/errno.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2024 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <errno.h>

int* __errno_location(void) {
return __errno();
}
31 changes: 31 additions & 0 deletions starboard/android/shared/posix_emu/include/errno.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2024 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef STARBOARD_ANDROID_SHARED_POSIX_EMU_INCLUDE_ERRNO_H_
#define STARBOARD_ANDROID_SHARED_POSIX_EMU_INCLUDE_ERRNO_H_

#include_next <errno.h> // The AndroidSdk version of the same file.

#ifdef __cplusplus
extern "C" {
#endif

// __errno_location not available in Android NDK.
int* __errno_location(void);

#ifdef __cplusplus
}
#endif

#endif // STARBOARD_ANDROID_SHARED_POSIX_EMU_INCLUDE_ERRNO_H_
2 changes: 2 additions & 0 deletions starboard/elf_loader/exported_symbols.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "starboard/elf_loader/exported_symbols.h"

#include <errno.h>
#include <fcntl.h>
#include <ifaddrs.h>
#include <netdb.h>
Expand Down Expand Up @@ -456,6 +457,7 @@ ExportedSymbols::ExportedSymbols() {

#if SB_API_VERSION >= 16
// POSIX APIs
REGISTER_SYMBOL(__errno_location);
REGISTER_SYMBOL(accept);
REGISTER_SYMBOL(bind);
REGISTER_SYMBOL(calloc);
Expand Down
1 change: 1 addition & 0 deletions starboard/nplb/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ target(gtest_target_type, "nplb") {
"posix_compliance/posix_socket_bind_test.cc",
"posix_compliance/posix_socket_connect_test.cc",
"posix_compliance/posix_socket_create_test.cc",
"posix_compliance/posix_socket_errno_test.cc",
"posix_compliance/posix_socket_helpers.cc",
"posix_compliance/posix_socket_listen_test.cc",
"posix_compliance/posix_socket_receive_test.cc",
Expand Down
69 changes: 69 additions & 0 deletions starboard/nplb/posix_compliance/posix_socket_errno_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2024 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <errno.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>

#include "starboard/common/log.h"
#include "starboard/nplb/posix_compliance/posix_socket_helpers.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace starboard {
namespace nplb {
namespace {

TEST(PosixErrnoTest, CreateInvalidSocket) {
EXPECT_FALSE(socket(-1, SOCK_STREAM, IPPROTO_TCP) == 0);
EXPECT_TRUE(errno == EAFNOSUPPORT);
SB_DLOG(INFO) << "Failed to create invalid socket, errno = "
<< strerror(errno);
}

TEST(PosixErrnoTest, AcceptInvalidSocket) {
int invalid_socket_fd = -1;
EXPECT_FALSE(accept(invalid_socket_fd, NULL, NULL) == 0);
EXPECT_TRUE(errno == EBADF);
SB_DLOG(INFO) << "Failed to accept invalid socket, errno = "
<< strerror(errno);
}

TEST(PosixErrnoTest, ConnectUnavailableAddress) {
int socket_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
ASSERT_TRUE(socket_fd > 0);

sockaddr_in6 address = {};
#if SB_HAS(IPV6)
EXPECT_TRUE(
PosixGetLocalAddressIPv4(reinterpret_cast<sockaddr*>(&address)) == 0 ||
PosixGetLocalAddressIPv6(reinterpret_cast<sockaddr*>(&address)) == 0);
#else
EXPECT_TRUE(PosixGetLocalAddressIPv4(reinterpret_cast<sockaddr*>(&address)) ==
0);
#endif

// Attempt to connect to an address where we expect connection to be refused
connect(socket_fd, (struct sockaddr*)&address, sizeof(address));

EXPECT_TRUE(errno == ECONNREFUSED || errno == EADDRNOTAVAIL ||
errno == EINPROGRESS || errno == EINVAL);
SB_DLOG(INFO) << "Failed to connect to unavailable address, errno = "
<< strerror(errno);

close(socket_fd);
}
} // namespace
} // namespace nplb
} // namespace starboard
3 changes: 1 addition & 2 deletions starboard/shared/posix/socket_create.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "starboard/common/socket.h"

#include <errno.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>

#include "starboard/common/log.h"
#include "starboard/common/socket.h"
#include "starboard/shared/posix/handle_eintr.h"
#include "starboard/shared/posix/set_non_blocking_internal.h"
#include "starboard/shared/posix/socket_internal.h"
Expand Down
2 changes: 1 addition & 1 deletion starboard/shared/posix/socket_listen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ SbSocketError SbSocketListen(SbSocket socket) {
#endif
int result = listen(socket->socket_fd, kMaxConn);
if (result != 0) {
return (socket->error = sbposix::TranslateSocketErrno(result));
return (socket->error = sbposix::TranslateSocketErrno(errno));
}

return (socket->error = kSbSocketOk);
Expand Down
Loading

0 comments on commit 7d99be1

Please sign in to comment.