plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) International Business Machines Corp., 2001 |
| 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; if not, write to the Free Software |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * Test Name: listen01 |
| 22 | * |
| 23 | * Test Description: |
| 24 | * Verify that listen() returns the proper errno for various failure cases |
| 25 | * |
| 26 | * Usage: <for command-line> |
| 27 | * listen01 [-c n] [-e] [-i n] [-I x] [-P x] [-t] |
| 28 | * where, -c n : Run n copies concurrently. |
| 29 | * -e : Turn on errno logging. |
| 30 | * -i n : Execute test n times. |
| 31 | * -I x : Execute test for x seconds. |
| 32 | * -P x : Pause for x seconds between iterations. |
| 33 | * -t : Turn on syscall timing. |
| 34 | * |
| 35 | * HISTORY |
| 36 | * 07/2001 Ported by Wayne Boyer |
| 37 | * |
| 38 | * RESTRICTIONS: |
| 39 | * None. |
| 40 | * |
| 41 | */ |
| 42 | |
| 43 | #include <stdio.h> |
| 44 | #include <unistd.h> |
| 45 | #include <errno.h> |
plars | 0c49ebf | 2002-03-26 20:36:34 +0000 | [diff] [blame] | 46 | #include <fcntl.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 47 | |
| 48 | #include <sys/types.h> |
| 49 | #include <sys/socket.h> |
| 50 | #include <sys/signal.h> |
| 51 | #include <sys/un.h> |
| 52 | |
| 53 | #include <netinet/in.h> |
| 54 | |
| 55 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 56 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 57 | char *TCID = "listen01"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 58 | int testno; |
| 59 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 60 | int s; /* socket descriptor */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 61 | |
| 62 | void setup(void), setup0(void), setup1(void), |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 63 | cleanup(void), cleanup0(void), cleanup1(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 64 | |
| 65 | struct test_case_t { /* test case structure */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 66 | int domain; /* PF_INET, PF_UNIX, ... */ |
| 67 | int type; /* SOCK_STREAM, SOCK_DGRAM ... */ |
| 68 | int proto; /* protocol number (usually 0 = default) */ |
| 69 | int backlog; /* connect's 3rd argument */ |
| 70 | int retval; /* syscall return value */ |
| 71 | int experrno; /* expected errno */ |
| 72 | void (*setup) (void); |
| 73 | void (*cleanup) (void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 74 | char *desc; |
| 75 | } tdat[] = { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 76 | { |
| 77 | 0, 0, 0, 0, -1, EBADF, setup0, cleanup0, "bad file descriptor"}, { |
| 78 | 0, 0, 0, 0, -1, ENOTSOCK, setup0, cleanup0, "not a socket"}, { |
| 79 | PF_INET, SOCK_DGRAM, 0, 0, -1, EOPNOTSUPP, setup1, cleanup1, |
| 80 | "UDP listen"},}; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 82 | int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 84 | int main(int argc, char *argv[]) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 85 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 86 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 87 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 88 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 89 | msg = parse_opts(argc, argv, NULL, NULL); |
| 90 | if (msg != NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 91 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | setup(); |
| 95 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 96 | for (lc = 0; TEST_LOOPING(lc); ++lc) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 97 | tst_count = 0; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 98 | for (testno = 0; testno < TST_TOTAL; ++testno) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | tdat[testno].setup(); |
| 100 | |
| 101 | TEST(listen(s, tdat[testno].backlog)); |
| 102 | if (TEST_RETURN != tdat[testno].retval || |
| 103 | (TEST_RETURN < 0 && |
| 104 | TEST_ERRNO != tdat[testno].experrno)) { |
| 105 | tst_resm(TFAIL, "%s ; returned" |
subrata_modak | 358c3ee | 2009-10-26 14:55:46 +0000 | [diff] [blame] | 106 | " %ld (expected %d), errno %d (expected" |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 107 | " %d)", tdat[testno].desc, |
| 108 | TEST_RETURN, tdat[testno].retval, |
| 109 | TEST_ERRNO, tdat[testno].experrno); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 110 | } else { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | tst_resm(TPASS, "%s successful", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 112 | tdat[testno].desc); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | } |
| 114 | tdat[testno].cleanup(); |
| 115 | } |
| 116 | } |
| 117 | cleanup(); |
| 118 | |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 119 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 120 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 122 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | { |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 124 | TEST_PAUSE; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 125 | } |
| 126 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 127 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 128 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 129 | } |
| 130 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 131 | void setup0(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 132 | { |
| 133 | if (tdat[testno].experrno == EBADF) |
| 134 | s = 400; /* anything not an open file */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 135 | else if ((s = open("/dev/null", O_WRONLY)) == -1) |
plars | 0c49ebf | 2002-03-26 20:36:34 +0000 | [diff] [blame] | 136 | tst_brkm(TBROK, cleanup, "error opening /dev/null - " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 137 | "errno: %s", strerror(errno)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 138 | } |
| 139 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 140 | void cleanup0(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 141 | { |
| 142 | s = -1; |
| 143 | } |
| 144 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 145 | void setup1(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 146 | { |
| 147 | s = socket(tdat[testno].domain, tdat[testno].type, tdat[testno].proto); |
| 148 | if (s < 0) { |
| 149 | tst_brkm(TBROK, cleanup, "socket setup failed for listen: " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 150 | "%s", strerror(errno)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 154 | void cleanup1(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 155 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 156 | (void)close(s); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 157 | s = -1; |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 158 | } |