Andrei Emeltchenko | 2f3ba88 | 2014-02-18 10:22:58 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * Contributed by: Intel Corporation, 2014 |
| 19 | */ |
| 20 | |
| 21 | #include <gtest/gtest.h> |
| 22 | |
| 23 | #if defined(__BIONIC__) |
| 24 | #define SOCK_CLOEXEC_SUPPORTED 1 |
| 25 | #elif defined(__GLIBC_PREREQ) |
| 26 | #if __GLIBC_PREREQ(2, 9) |
| 27 | #define SOCK_CLOEXEC_SUPPORTED 1 |
| 28 | #endif |
| 29 | #endif |
| 30 | |
| 31 | #if defined(SOCK_CLOEXEC_SUPPORTED) |
| 32 | |
| 33 | #include <errno.h> |
| 34 | #include <sys/types.h> |
| 35 | #include <sys/socket.h> |
| 36 | #include <sys/un.h> |
| 37 | #include <fcntl.h> |
| 38 | |
| 39 | #define SOCK_PATH "test" |
| 40 | |
| 41 | static void* ConnectFn(void*) { |
| 42 | int fd = socket(PF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC | SOCK_NONBLOCK, 0); |
| 43 | if (fd < 0) { |
| 44 | GTEST_LOG_(ERROR) << "socket call failed: " << strerror(errno); |
| 45 | return reinterpret_cast<void*>(-1); |
| 46 | } |
| 47 | |
| 48 | struct sockaddr_un addr; |
| 49 | memset(&addr, 0, sizeof(addr)); |
| 50 | addr.sun_family = AF_UNIX; |
| 51 | addr.sun_path[0] = '\0'; |
| 52 | strcpy(addr.sun_path + 1, SOCK_PATH); |
| 53 | |
| 54 | if (connect(fd, reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr)) < 0) { |
| 55 | GTEST_LOG_(ERROR) << "connect call failed: " << strerror(errno); |
| 56 | close(fd); |
| 57 | return reinterpret_cast<void*>(-1); |
| 58 | } |
| 59 | |
| 60 | close(fd); |
| 61 | |
| 62 | return NULL; |
| 63 | } |
| 64 | #endif |
| 65 | |
| 66 | TEST(accept4, smoke) { |
| 67 | #if defined(SOCK_CLOEXEC_SUPPORTED) |
| 68 | int fd = socket(PF_UNIX, SOCK_SEQPACKET, 0); |
| 69 | ASSERT_NE(fd, -1) << strerror(errno); |
| 70 | |
| 71 | struct sockaddr_un addr; |
| 72 | memset(&addr, 0, sizeof(addr)); |
| 73 | addr.sun_family = AF_UNIX; |
| 74 | addr.sun_path[0] = '\0'; |
| 75 | strcpy(addr.sun_path + 1, SOCK_PATH); |
| 76 | |
| 77 | ASSERT_NE(-1, bind(fd, reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr))) << strerror(errno); |
| 78 | |
| 79 | ASSERT_NE(-1, listen(fd, 1)) << strerror(errno); |
| 80 | |
| 81 | pthread_t thread; |
| 82 | ASSERT_EQ(0, pthread_create(&thread, NULL, ConnectFn, NULL)); |
| 83 | |
| 84 | fd_set read_set; |
| 85 | FD_ZERO(&read_set); |
| 86 | FD_SET(fd, &read_set); |
| 87 | timeval tv; |
| 88 | tv.tv_sec = 5; |
| 89 | tv.tv_usec = 0; |
| 90 | ASSERT_LT(0, select(fd+1, &read_set, NULL, NULL, &tv)); |
| 91 | |
| 92 | socklen_t len = sizeof(addr); |
| 93 | int fd_acc = accept4(fd, reinterpret_cast<struct sockaddr*>(&addr), &len, SOCK_CLOEXEC); |
| 94 | ASSERT_NE(fd_acc, -1) << strerror(errno); |
| 95 | |
| 96 | // Check that the flag was set properly. |
| 97 | ASSERT_EQ(FD_CLOEXEC, fcntl(fd_acc, F_GETFD) & FD_CLOEXEC); |
| 98 | |
| 99 | void* ret_val; |
| 100 | pthread_join(thread, &ret_val); |
| 101 | ASSERT_EQ(NULL, ret_val); |
| 102 | |
| 103 | close(fd_acc); |
| 104 | close(fd); |
| 105 | #else |
| 106 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 107 | #endif |
| 108 | } |