Xiao Yang | d2a810a | 2016-06-24 17:04:51 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) Ulrich Drepper <drepper@redhat.com> |
| 3 | * Copyright (c) International Business Machines Corp., 2009 |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 13 | * the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. |
| 17 | */ |
| 18 | |
| 19 | /* |
| 20 | * Test Name: socket02 |
| 21 | * |
| 22 | * Description: |
| 23 | * This program tests the new flag SOCK_CLOEXEC and SOCK_NONBLOCK introduced |
| 24 | * in socket() in kernel 2.6.27. |
| 25 | */ |
subrata_modak | 462f71a | 2009-01-16 10:01:50 +0000 | [diff] [blame] | 26 | |
subrata_modak | 462f71a | 2009-01-16 10:01:50 +0000 | [diff] [blame] | 27 | #include <fcntl.h> |
| 28 | #include <stdio.h> |
| 29 | #include <unistd.h> |
| 30 | #include <netinet/in.h> |
| 31 | #include <sys/socket.h> |
Cyril Hrubis | 98f8747 | 2014-01-08 13:32:41 +0100 | [diff] [blame] | 32 | #include "lapi/fcntl.h" |
Xiao Yang | d2a810a | 2016-06-24 17:04:51 +0800 | [diff] [blame] | 33 | #include "tst_test.h" |
subrata_modak | 462f71a | 2009-01-16 10:01:50 +0000 | [diff] [blame] | 34 | |
Xiao Yang | d2a810a | 2016-06-24 17:04:51 +0800 | [diff] [blame] | 35 | static int fd; |
subrata_modak | 462f71a | 2009-01-16 10:01:50 +0000 | [diff] [blame] | 36 | |
Xiao Yang | d2a810a | 2016-06-24 17:04:51 +0800 | [diff] [blame] | 37 | static struct tcase { |
| 38 | int type; |
| 39 | int flag; |
| 40 | int fl_flag; |
| 41 | char *des; |
| 42 | } tcases[] = { |
| 43 | {SOCK_STREAM, 0, F_GETFD, "no close-on-exec"}, |
| 44 | {SOCK_STREAM | SOCK_CLOEXEC, FD_CLOEXEC, F_GETFD, "close-on-exec"}, |
| 45 | {SOCK_STREAM, 0, F_GETFL, "no non-blocking"}, |
| 46 | {SOCK_STREAM | SOCK_NONBLOCK, O_NONBLOCK, F_GETFL, "non-blocking"} |
| 47 | }; |
subrata_modak | 462f71a | 2009-01-16 10:01:50 +0000 | [diff] [blame] | 48 | |
Xiao Yang | d2a810a | 2016-06-24 17:04:51 +0800 | [diff] [blame] | 49 | static void verify_socket(unsigned int n) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 50 | { |
Xiao Yang | d2a810a | 2016-06-24 17:04:51 +0800 | [diff] [blame] | 51 | int res; |
| 52 | struct tcase *tc = &tcases[n]; |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 53 | |
Xiao Yang | d2a810a | 2016-06-24 17:04:51 +0800 | [diff] [blame] | 54 | fd = socket(PF_INET, tc->type, 0); |
| 55 | if (fd == -1) |
| 56 | tst_brk(TFAIL | TERRNO, "socket() failed"); |
subrata_modak | 462f71a | 2009-01-16 10:01:50 +0000 | [diff] [blame] | 57 | |
Xiao Yang | d2a810a | 2016-06-24 17:04:51 +0800 | [diff] [blame] | 58 | res = SAFE_FCNTL(fd, tc->fl_flag); |
subrata_modak | 462f71a | 2009-01-16 10:01:50 +0000 | [diff] [blame] | 59 | |
Xiao Yang | d2a810a | 2016-06-24 17:04:51 +0800 | [diff] [blame] | 60 | if (tc->flag != 0 && (res & tc->flag) == 0) { |
| 61 | tst_res(TFAIL, "socket() failed to set %s flag", tc->des); |
| 62 | return; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 63 | } |
subrata_modak | 462f71a | 2009-01-16 10:01:50 +0000 | [diff] [blame] | 64 | |
Xiao Yang | d2a810a | 2016-06-24 17:04:51 +0800 | [diff] [blame] | 65 | if (tc->flag == 0 && (res & tc->flag) != 0) { |
| 66 | tst_res(TFAIL, "socket() failed to set %s flag", tc->des); |
| 67 | return; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 68 | } |
Xiao Yang | d2a810a | 2016-06-24 17:04:51 +0800 | [diff] [blame] | 69 | |
| 70 | tst_res(TPASS, "socket() passed to set %s flag", tc->des); |
| 71 | |
| 72 | SAFE_CLOSE(fd); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 73 | } |
Xiao Yang | d2a810a | 2016-06-24 17:04:51 +0800 | [diff] [blame] | 74 | |
| 75 | static void cleanup(void) |
| 76 | { |
Cyril Hrubis | e1c49ed | 2017-02-14 13:23:35 +0100 | [diff] [blame] | 77 | if (fd > 0) |
| 78 | SAFE_CLOSE(fd); |
Xiao Yang | d2a810a | 2016-06-24 17:04:51 +0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | static struct tst_test test = { |
Xiao Yang | d2a810a | 2016-06-24 17:04:51 +0800 | [diff] [blame] | 82 | .tcnt = ARRAY_SIZE(tcases), |
| 83 | .test = verify_socket, |
| 84 | .min_kver = "2.6.27", |
| 85 | .cleanup = cleanup |
| 86 | }; |