| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
#include "asterisk.h" |
| 35 |
#include <signal.h> |
| 36 |
#include <sys/types.h> |
| 37 |
#include <sys/stat.h> |
| 38 |
#include <fcntl.h> |
| 39 |
#include <errno.h> |
| 40 |
#include <unistd.h> |
| 41 |
|
| 42 |
#include "asterisk/utils.h" |
| 43 |
#include "asterisk/module.h" |
| 44 |
#include "asterisk/test.h" |
| 45 |
#include "asterisk/poll-compat.h" |
| 46 |
|
| 47 |
static void *failsafe_cancel(void *vparent) |
| 48 |
{ |
| 49 |
pthread_t parent = (pthread_t) (long) vparent; |
| 50 |
|
| 51 |
sleep(1); |
| 52 |
pthread_testcancel(); |
| 53 |
pthread_kill(parent, SIGURG); |
| 54 |
sleep(1); |
| 55 |
pthread_testcancel(); |
| 56 |
pthread_kill(parent, SIGURG); |
| 57 |
sleep(1); |
| 58 |
pthread_testcancel(); |
| 59 |
pthread_kill(parent, SIGURG); |
| 60 |
pthread_exit(NULL); |
| 61 |
} |
| 62 |
|
| 63 |
#define RESET for (i = 0; i < 4; i++) { pfd[i].revents = 0; } |
| 64 |
AST_TEST_DEFINE(poll_test) |
| 65 |
{ |
| 66 |
#define FDNO 3 |
| 67 |
int fd[2], res = AST_TEST_PASS, i, res2; |
| 68 |
int rdblocker[2]; |
| 69 |
#if FDNO > 3 |
| 70 |
int wrblocker[2], consec_interrupt = 0; |
| 71 |
#endif |
| 72 |
struct pollfd pfd[4] = { { .events = POLLOUT, }, { .events = POLLIN, }, { .events = POLLIN }, { .events = POLLOUT } }; |
| 73 |
pthread_t failsafe_tid; |
| 74 |
struct timeval tv = { 0, 0 }; |
| 75 |
#if FDNO > 3 |
| 76 |
char garbage[256] = |
| 77 |
"abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ@/" |
| 78 |
"abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ@/" |
| 79 |
"abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ@/" |
| 80 |
"abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ@/"; |
| 81 |
#endif |
| 82 |
|
| 83 |
switch (cmd) { |
| 84 |
case TEST_INIT: |
| 85 |
info->name = "poll_test"; |
| 86 |
info->category = "/main/poll/"; |
| 87 |
info->summary = "unit test for the ast_poll() API"; |
| 88 |
info->description = |
| 89 |
"Verifies behavior for the ast_poll() API call"; |
| 90 |
return AST_TEST_NOT_RUN; |
| 91 |
case TEST_EXECUTE: |
| 92 |
break; |
| 93 |
} |
| 94 |
|
| 95 |
ast_test_status_update(test, "Creating handle that should NEVER block on write\n"); |
| 96 |
if ((fd[0] = open("/dev/null", O_WRONLY)) < 0) { |
| 97 |
ast_test_status_update(test, "Unable to open a writable handle to /dev/null: %s\n", strerror(errno)); |
| 98 |
return AST_TEST_FAIL; |
| 99 |
} |
| 100 |
|
| 101 |
ast_test_status_update(test, "Creating handle that should NEVER block on read\n"); |
| 102 |
if ((fd[1] = open("/dev/zero", O_RDONLY)) < 0) { |
| 103 |
ast_test_status_update(test, "Unable to open a readable handle to /dev/zero: %s\n", strerror(errno)); |
| 104 |
close(fd[0]); |
| 105 |
return AST_TEST_FAIL; |
| 106 |
} |
| 107 |
|
| 108 |
ast_test_status_update(test, "Creating handle that should block on read\n"); |
| 109 |
if (pipe(rdblocker) < 0) { |
| 110 |
ast_test_status_update(test, "Unable to open a pipe: %s\n", strerror(errno)); |
| 111 |
close(fd[0]); |
| 112 |
close(fd[1]); |
| 113 |
return AST_TEST_FAIL; |
| 114 |
} |
| 115 |
|
| 116 |
#if FDNO > 3 |
| 117 |
ast_test_status_update(test, "Creating handle that should block on write\n"); |
| 118 |
if (pipe(wrblocker) < 0) { |
| 119 |
ast_test_status_update(test, "Unable to open a pipe: %s\n", strerror(errno)); |
| 120 |
close(fd[0]); |
| 121 |
close(fd[1]); |
| 122 |
close(rdblocker[0]); |
| 123 |
close(rdblocker[1]); |
| 124 |
return AST_TEST_FAIL; |
| 125 |
} |
| 126 |
|
| 127 |
ast_test_status_update(test, "Starting thread to ensure we don't block forever\n"); |
| 128 |
if (ast_pthread_create_background(&failsafe_tid, NULL, failsafe_cancel, (void *) (long) pthread_self())) { |
| 129 |
ast_test_status_update(test, "Unable to start failsafe thread\n"); |
| 130 |
close(fd[0]); |
| 131 |
close(fd[1]); |
| 132 |
close(fd[2]); |
| 133 |
close(rdblocker[0]); |
| 134 |
close(rdblocker[1]); |
| 135 |
close(wrblocker[0]); |
| 136 |
close(wrblocker[1]); |
| 137 |
return AST_TEST_FAIL; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
ast_test_status_update(test, "Making pipe block on write\n"); |
| 142 |
for (i = 0; i < 4096; i++) { |
| 143 |
errno = 0; |
| 144 |
if (write(wrblocker[1], garbage, sizeof(garbage)) < sizeof(garbage)) { |
| 145 |
ast_test_status_update(test, "Got %d\n", errno); |
| 146 |
if (errno == EINTR && ++consec_interrupt > 1) { |
| 147 |
break; |
| 148 |
} |
| 149 |
} else { |
| 150 |
consec_interrupt = 0; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
ast_test_status_update(test, "Cancelling failsafe thread.\n"); |
| 155 |
pthread_cancel(failsafe_tid); |
| 156 |
pthread_kill(failsafe_tid, SIGURG); |
| 157 |
pthread_join(failsafe_tid, NULL); |
| 158 |
#endif |
| 159 |
|
| 160 |
pfd[0].fd = fd[0]; |
| 161 |
pfd[1].fd = fd[1]; |
| 162 |
pfd[2].fd = rdblocker[0]; |
| 163 |
#if FDNO > 3 |
| 164 |
pfd[3].fd = wrblocker[1]; |
| 165 |
#endif |
| 166 |
|
| 167 |
|
| 168 |
ast_test_status_update(test, "Starting thread to ensure we don't block forever\n"); |
| 169 |
if (ast_pthread_create_background(&failsafe_tid, NULL, failsafe_cancel, (void *) (long) pthread_self())) { |
| 170 |
ast_test_status_update(test, "Unable to start failsafe thread\n"); |
| 171 |
close(fd[0]); |
| 172 |
close(fd[1]); |
| 173 |
close(rdblocker[0]); |
| 174 |
close(rdblocker[1]); |
| 175 |
#if FDNO > 3 |
| 176 |
close(wrblocker[0]); |
| 177 |
close(wrblocker[1]); |
| 178 |
#endif |
| 179 |
return AST_TEST_FAIL; |
| 180 |
} |
| 181 |
|
| 182 |
RESET; |
| 183 |
if ((res2 = ast_poll(pfd, FDNO, -1)) != 2) { |
| 184 |
ast_test_status_update(test, "ast_poll does not return that only two handles are available (inf timeout): %d, %s\n", res2, res2 == -1 ? strerror(errno) : ""); |
| 185 |
res = AST_TEST_FAIL; |
| 186 |
} |
| 187 |
|
| 188 |
RESET; |
| 189 |
if ((res2 = ast_poll2(pfd, FDNO, NULL)) != 2) { |
| 190 |
ast_test_status_update(test, "ast_poll2 does not return that only two handles are available (inf timeout): %d %s\n", res2, res2 == -1 ? strerror(errno) : ""); |
| 191 |
res = AST_TEST_FAIL; |
| 192 |
} |
| 193 |
|
| 194 |
ast_test_status_update(test, "Cancelling failsafe thread.\n"); |
| 195 |
pthread_cancel(failsafe_tid); |
| 196 |
pthread_kill(failsafe_tid, SIGURG); |
| 197 |
pthread_join(failsafe_tid, NULL); |
| 198 |
|
| 199 |
RESET; |
| 200 |
if (ast_poll(pfd, FDNO, 0) != 2) { |
| 201 |
ast_test_status_update(test, "ast_poll does not return that only two handles are available (0 timeout): %d, %s\n", res2, res2 == -1 ? strerror(errno) : ""); |
| 202 |
res = AST_TEST_FAIL; |
| 203 |
} |
| 204 |
|
| 205 |
RESET; |
| 206 |
if (ast_poll2(pfd, FDNO, &tv) != 2) { |
| 207 |
ast_test_status_update(test, "ast_poll2 does not return that only two handles are available (0 timeout): %d, %s\n", res2, res2 == -1 ? strerror(errno) : ""); |
| 208 |
res = AST_TEST_FAIL; |
| 209 |
} |
| 210 |
|
| 211 |
RESET; |
| 212 |
if (ast_poll(pfd, FDNO, 1) != 2) { |
| 213 |
ast_test_status_update(test, "ast_poll does not return that only two handles are available (1ms timeout): %d, %s\n", res2, res2 == -1 ? strerror(errno) : ""); |
| 214 |
res = AST_TEST_FAIL; |
| 215 |
} |
| 216 |
|
| 217 |
tv.tv_usec = 1000; |
| 218 |
if (ast_poll2(pfd, FDNO, &tv) != 2) { |
| 219 |
ast_test_status_update(test, "ast_poll2 does not return that only two handles are available (1ms timeout): %d, %s\n", res2, res2 == -1 ? strerror(errno) : ""); |
| 220 |
res = AST_TEST_FAIL; |
| 221 |
} |
| 222 |
|
| 223 |
close(fd[0]); |
| 224 |
close(fd[1]); |
| 225 |
close(rdblocker[0]); |
| 226 |
close(rdblocker[1]); |
| 227 |
#if FDNO > 3 |
| 228 |
close(wrblocker[0]); |
| 229 |
close(wrblocker[1]); |
| 230 |
#endif |
| 231 |
return res; |
| 232 |
} |
| 233 |
|
| 234 |
static int unload_module(void) |
| 235 |
{ |
| 236 |
AST_TEST_UNREGISTER(poll_test); |
| 237 |
return 0; |
| 238 |
} |
| 239 |
|
| 240 |
static int load_module(void) |
| 241 |
{ |
| 242 |
AST_TEST_REGISTER(poll_test); |
| 243 |
return AST_MODULE_LOAD_SUCCESS; |
| 244 |
} |
| 245 |
|
| 246 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Poll test"); |