xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame^] | 1 | /*************************************************************************** |
| 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
| 8 | * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al. |
| 9 | * |
| 10 | * This software is licensed as described in the file COPYING, which |
| 11 | * you should have received as part of this distribution. The terms |
| 12 | * are also available at https://curl.se/docs/copyright.html. |
| 13 | * |
| 14 | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
| 15 | * copies of the Software, and permit persons to whom the Software is |
| 16 | * furnished to do so, under the terms of the COPYING file. |
| 17 | * |
| 18 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 19 | * KIND, either express or implied. |
| 20 | * |
| 21 | * SPDX-License-Identifier: curl |
| 22 | * |
| 23 | ***************************************************************************/ |
| 24 | #include "test.h" |
| 25 | |
| 26 | #ifdef HAVE_SYS_RESOURCE_H |
| 27 | #include <sys/resource.h> |
| 28 | #endif |
| 29 | #ifdef HAVE_FCNTL_H |
| 30 | #include <fcntl.h> |
| 31 | #endif |
| 32 | #include <limits.h> |
| 33 | |
| 34 | #include "warnless.h" |
| 35 | #include "memdebug.h" |
| 36 | |
| 37 | #if !defined(HAVE_POLL_FINE) && \ |
| 38 | !defined(USE_WINSOCK) && \ |
| 39 | !defined(FD_SETSIZE) |
| 40 | #error "this test requires FD_SETSIZE" |
| 41 | #endif |
| 42 | |
| 43 | #define SAFETY_MARGIN (11) |
| 44 | |
| 45 | #if defined(WIN32) || defined(_WIN32) || defined(MSDOS) |
| 46 | #define DEV_NULL "NUL" |
| 47 | #else |
| 48 | #define DEV_NULL "/dev/null" |
| 49 | #endif |
| 50 | |
| 51 | #if defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT) |
| 52 | |
| 53 | static int *fd = NULL; |
| 54 | static struct rlimit num_open; |
| 55 | static char msgbuff[256]; |
| 56 | |
| 57 | static void store_errmsg(const char *msg, int err) |
| 58 | { |
| 59 | if(!err) |
| 60 | msnprintf(msgbuff, sizeof(msgbuff), "%s", msg); |
| 61 | else |
| 62 | msnprintf(msgbuff, sizeof(msgbuff), "%s, errno %d, %s", msg, err, |
| 63 | strerror(err)); |
| 64 | } |
| 65 | |
| 66 | static void close_file_descriptors(void) |
| 67 | { |
| 68 | for(num_open.rlim_cur = 0; |
| 69 | num_open.rlim_cur < num_open.rlim_max; |
| 70 | num_open.rlim_cur++) |
| 71 | if(fd[num_open.rlim_cur] > 0) |
| 72 | close(fd[num_open.rlim_cur]); |
| 73 | free(fd); |
| 74 | fd = NULL; |
| 75 | } |
| 76 | |
| 77 | static int fopen_works(void) |
| 78 | { |
| 79 | FILE *fpa[3]; |
| 80 | int i; |
| 81 | int ret = 1; |
| 82 | |
| 83 | for(i = 0; i < 3; i++) { |
| 84 | fpa[i] = NULL; |
| 85 | } |
| 86 | for(i = 0; i < 3; i++) { |
| 87 | fpa[i] = fopen(DEV_NULL, FOPEN_READTEXT); |
| 88 | if(!fpa[i]) { |
| 89 | store_errmsg("fopen failed", errno); |
| 90 | fprintf(stderr, "%s\n", msgbuff); |
| 91 | ret = 0; |
| 92 | break; |
| 93 | } |
| 94 | } |
| 95 | for(i = 0; i < 3; i++) { |
| 96 | if(fpa[i]) |
| 97 | fclose(fpa[i]); |
| 98 | } |
| 99 | return ret; |
| 100 | } |
| 101 | |
| 102 | static int rlimit(int keep_open) |
| 103 | { |
| 104 | int *tmpfd; |
| 105 | rlim_t nitems, i; |
| 106 | int *memchunk = NULL; |
| 107 | char *fmt; |
| 108 | struct rlimit rl; |
| 109 | char strbuff[256]; |
| 110 | char strbuff1[81]; |
| 111 | char fmt_u[] = "%u"; |
| 112 | char fmt_lu[] = "%lu"; |
| 113 | #ifdef HAVE_LONGLONG |
| 114 | char fmt_llu[] = "%llu"; |
| 115 | |
| 116 | if(sizeof(rl.rlim_max) > sizeof(long)) |
| 117 | fmt = fmt_llu; |
| 118 | else |
| 119 | #endif |
| 120 | fmt = (sizeof(rl.rlim_max) < sizeof(long))?fmt_u:fmt_lu; |
| 121 | |
| 122 | /* get initial open file limits */ |
| 123 | |
| 124 | if(getrlimit(RLIMIT_NOFILE, &rl) != 0) { |
| 125 | store_errmsg("getrlimit() failed", errno); |
| 126 | fprintf(stderr, "%s\n", msgbuff); |
| 127 | return -1; |
| 128 | } |
| 129 | |
| 130 | /* show initial open file limits */ |
| 131 | |
| 132 | #ifdef RLIM_INFINITY |
| 133 | if(rl.rlim_cur == RLIM_INFINITY) |
| 134 | strcpy(strbuff, "INFINITY"); |
| 135 | else |
| 136 | #endif |
| 137 | msnprintf(strbuff, sizeof(strbuff), fmt, rl.rlim_cur); |
| 138 | fprintf(stderr, "initial soft limit: %s\n", strbuff); |
| 139 | |
| 140 | #ifdef RLIM_INFINITY |
| 141 | if(rl.rlim_max == RLIM_INFINITY) |
| 142 | strcpy(strbuff, "INFINITY"); |
| 143 | else |
| 144 | #endif |
| 145 | msnprintf(strbuff, sizeof(strbuff), fmt, rl.rlim_max); |
| 146 | fprintf(stderr, "initial hard limit: %s\n", strbuff); |
| 147 | |
| 148 | /* |
| 149 | * if soft limit and hard limit are different we ask the |
| 150 | * system to raise soft limit all the way up to the hard |
| 151 | * limit. Due to some other system limit the soft limit |
| 152 | * might not be raised up to the hard limit. So from this |
| 153 | * point the resulting soft limit is our limit. Trying to |
| 154 | * open more than soft limit file descriptors will fail. |
| 155 | */ |
| 156 | |
| 157 | if(rl.rlim_cur != rl.rlim_max) { |
| 158 | |
| 159 | #ifdef OPEN_MAX |
| 160 | if((rl.rlim_cur > 0) && |
| 161 | (rl.rlim_cur < OPEN_MAX)) { |
| 162 | fprintf(stderr, "raising soft limit up to OPEN_MAX\n"); |
| 163 | rl.rlim_cur = OPEN_MAX; |
| 164 | if(setrlimit(RLIMIT_NOFILE, &rl) != 0) { |
| 165 | /* on failure don't abort just issue a warning */ |
| 166 | store_errmsg("setrlimit() failed", errno); |
| 167 | fprintf(stderr, "%s\n", msgbuff); |
| 168 | msgbuff[0] = '\0'; |
| 169 | } |
| 170 | } |
| 171 | #endif |
| 172 | |
| 173 | fprintf(stderr, "raising soft limit up to hard limit\n"); |
| 174 | rl.rlim_cur = rl.rlim_max; |
| 175 | if(setrlimit(RLIMIT_NOFILE, &rl) != 0) { |
| 176 | /* on failure don't abort just issue a warning */ |
| 177 | store_errmsg("setrlimit() failed", errno); |
| 178 | fprintf(stderr, "%s\n", msgbuff); |
| 179 | msgbuff[0] = '\0'; |
| 180 | } |
| 181 | |
| 182 | /* get current open file limits */ |
| 183 | |
| 184 | if(getrlimit(RLIMIT_NOFILE, &rl) != 0) { |
| 185 | store_errmsg("getrlimit() failed", errno); |
| 186 | fprintf(stderr, "%s\n", msgbuff); |
| 187 | return -3; |
| 188 | } |
| 189 | |
| 190 | /* show current open file limits */ |
| 191 | |
| 192 | #ifdef RLIM_INFINITY |
| 193 | if(rl.rlim_cur == RLIM_INFINITY) |
| 194 | strcpy(strbuff, "INFINITY"); |
| 195 | else |
| 196 | #endif |
| 197 | msnprintf(strbuff, sizeof(strbuff), fmt, rl.rlim_cur); |
| 198 | fprintf(stderr, "current soft limit: %s\n", strbuff); |
| 199 | |
| 200 | #ifdef RLIM_INFINITY |
| 201 | if(rl.rlim_max == RLIM_INFINITY) |
| 202 | strcpy(strbuff, "INFINITY"); |
| 203 | else |
| 204 | #endif |
| 205 | msnprintf(strbuff, sizeof(strbuff), fmt, rl.rlim_max); |
| 206 | fprintf(stderr, "current hard limit: %s\n", strbuff); |
| 207 | |
| 208 | } /* (rl.rlim_cur != rl.rlim_max) */ |
| 209 | |
| 210 | /* |
| 211 | * test 537 is all about testing libcurl functionality |
| 212 | * when the system has nearly exhausted the number of |
| 213 | * available file descriptors. Test 537 will try to run |
| 214 | * with a very small number of file descriptors available. |
| 215 | * This implies that any file descriptor which is open |
| 216 | * when the test runs will have a number in the high range |
| 217 | * of whatever the system supports. |
| 218 | */ |
| 219 | |
| 220 | /* |
| 221 | * reserve a chunk of memory before opening file descriptors to |
| 222 | * avoid a low memory condition once the file descriptors are |
| 223 | * open. System conditions that could make the test fail should |
| 224 | * be addressed in the precheck phase. This chunk of memory shall |
| 225 | * be always free()ed before exiting the rlimit() function so |
| 226 | * that it becomes available to the test. |
| 227 | */ |
| 228 | |
| 229 | for(nitems = i = 1; nitems <= i; i *= 2) |
| 230 | nitems = i; |
| 231 | if(nitems > 0x7fff) |
| 232 | nitems = 0x40000; |
| 233 | do { |
| 234 | num_open.rlim_max = sizeof(*memchunk) * nitems; |
| 235 | msnprintf(strbuff, sizeof(strbuff), fmt, num_open.rlim_max); |
| 236 | fprintf(stderr, "allocating memchunk %s byte array\n", strbuff); |
| 237 | memchunk = malloc(sizeof(*memchunk) * (size_t)nitems); |
| 238 | if(!memchunk) { |
| 239 | fprintf(stderr, "memchunk, malloc() failed\n"); |
| 240 | nitems /= 2; |
| 241 | } |
| 242 | } while(nitems && !memchunk); |
| 243 | if(!memchunk) { |
| 244 | store_errmsg("memchunk, malloc() failed", errno); |
| 245 | fprintf(stderr, "%s\n", msgbuff); |
| 246 | return -4; |
| 247 | } |
| 248 | |
| 249 | /* initialize it to fight lazy allocation */ |
| 250 | |
| 251 | fprintf(stderr, "initializing memchunk array\n"); |
| 252 | |
| 253 | for(i = 0; i < nitems; i++) |
| 254 | memchunk[i] = -1; |
| 255 | |
| 256 | /* set the number of file descriptors we will try to open */ |
| 257 | |
| 258 | #ifdef RLIM_INFINITY |
| 259 | if((rl.rlim_cur > 0) && (rl.rlim_cur != RLIM_INFINITY)) { |
| 260 | #else |
| 261 | if(rl.rlim_cur > 0) { |
| 262 | #endif |
| 263 | /* soft limit minus SAFETY_MARGIN */ |
| 264 | num_open.rlim_max = rl.rlim_cur - SAFETY_MARGIN; |
| 265 | } |
| 266 | else { |
| 267 | /* a huge number of file descriptors */ |
| 268 | for(nitems = i = 1; nitems <= i; i *= 2) |
| 269 | nitems = i; |
| 270 | if(nitems > 0x7fff) |
| 271 | nitems = 0x40000; |
| 272 | num_open.rlim_max = nitems; |
| 273 | } |
| 274 | |
| 275 | /* verify that we won't overflow size_t in malloc() */ |
| 276 | |
| 277 | if((size_t)(num_open.rlim_max) > ((size_t)-1) / sizeof(*fd)) { |
| 278 | msnprintf(strbuff1, sizeof(strbuff1), fmt, num_open.rlim_max); |
| 279 | msnprintf(strbuff, sizeof(strbuff), "unable to allocate an array for %s " |
| 280 | "file descriptors, would overflow size_t", strbuff1); |
| 281 | store_errmsg(strbuff, 0); |
| 282 | fprintf(stderr, "%s\n", msgbuff); |
| 283 | free(memchunk); |
| 284 | return -5; |
| 285 | } |
| 286 | |
| 287 | /* allocate array for file descriptors */ |
| 288 | |
| 289 | do { |
| 290 | msnprintf(strbuff, sizeof(strbuff), fmt, num_open.rlim_max); |
| 291 | fprintf(stderr, "allocating array for %s file descriptors\n", strbuff); |
| 292 | fd = malloc(sizeof(*fd) * (size_t)(num_open.rlim_max)); |
| 293 | if(!fd) { |
| 294 | fprintf(stderr, "fd, malloc() failed\n"); |
| 295 | num_open.rlim_max /= 2; |
| 296 | } |
| 297 | } while(num_open.rlim_max && !fd); |
| 298 | if(!fd) { |
| 299 | store_errmsg("fd, malloc() failed", errno); |
| 300 | fprintf(stderr, "%s\n", msgbuff); |
| 301 | free(memchunk); |
| 302 | return -6; |
| 303 | } |
| 304 | |
| 305 | /* initialize it to fight lazy allocation */ |
| 306 | |
| 307 | fprintf(stderr, "initializing fd array\n"); |
| 308 | |
| 309 | for(num_open.rlim_cur = 0; |
| 310 | num_open.rlim_cur < num_open.rlim_max; |
| 311 | num_open.rlim_cur++) |
| 312 | fd[num_open.rlim_cur] = -1; |
| 313 | |
| 314 | msnprintf(strbuff, sizeof(strbuff), fmt, num_open.rlim_max); |
| 315 | fprintf(stderr, "trying to open %s file descriptors\n", strbuff); |
| 316 | |
| 317 | /* open a dummy descriptor */ |
| 318 | |
| 319 | fd[0] = open(DEV_NULL, O_RDONLY); |
| 320 | if(fd[0] < 0) { |
| 321 | msnprintf(strbuff, sizeof(strbuff), "opening of %s failed", DEV_NULL); |
| 322 | store_errmsg(strbuff, errno); |
| 323 | fprintf(stderr, "%s\n", msgbuff); |
| 324 | free(fd); |
| 325 | fd = NULL; |
| 326 | free(memchunk); |
| 327 | return -7; |
| 328 | } |
| 329 | |
| 330 | /* create a bunch of file descriptors */ |
| 331 | |
| 332 | for(num_open.rlim_cur = 1; |
| 333 | num_open.rlim_cur < num_open.rlim_max; |
| 334 | num_open.rlim_cur++) { |
| 335 | |
| 336 | fd[num_open.rlim_cur] = dup(fd[0]); |
| 337 | |
| 338 | if(fd[num_open.rlim_cur] < 0) { |
| 339 | |
| 340 | fd[num_open.rlim_cur] = -1; |
| 341 | |
| 342 | msnprintf(strbuff1, sizeof(strbuff1), fmt, num_open.rlim_cur); |
| 343 | msnprintf(strbuff, sizeof(strbuff), "dup() attempt %s failed", strbuff1); |
| 344 | fprintf(stderr, "%s\n", strbuff); |
| 345 | |
| 346 | msnprintf(strbuff1, sizeof(strbuff1), fmt, num_open.rlim_cur); |
| 347 | msnprintf(strbuff, sizeof(strbuff), "fds system limit seems close to %s", |
| 348 | strbuff1); |
| 349 | fprintf(stderr, "%s\n", strbuff); |
| 350 | |
| 351 | num_open.rlim_max = num_open.rlim_cur - SAFETY_MARGIN; |
| 352 | |
| 353 | num_open.rlim_cur -= num_open.rlim_max; |
| 354 | msnprintf(strbuff1, sizeof(strbuff1), fmt, num_open.rlim_cur); |
| 355 | msnprintf(strbuff, sizeof(strbuff), "closing %s file descriptors", |
| 356 | strbuff1); |
| 357 | fprintf(stderr, "%s\n", strbuff); |
| 358 | |
| 359 | for(num_open.rlim_cur = num_open.rlim_max; |
| 360 | fd[num_open.rlim_cur] >= 0; |
| 361 | num_open.rlim_cur++) { |
| 362 | close(fd[num_open.rlim_cur]); |
| 363 | fd[num_open.rlim_cur] = -1; |
| 364 | } |
| 365 | |
| 366 | msnprintf(strbuff, sizeof(strbuff), fmt, num_open.rlim_max); |
| 367 | fprintf(stderr, "shrinking array for %s file descriptors\n", strbuff); |
| 368 | |
| 369 | /* we don't care if we can't shrink it */ |
| 370 | |
| 371 | tmpfd = realloc(fd, sizeof(*fd) * (size_t)(num_open.rlim_max)); |
| 372 | if(tmpfd) { |
| 373 | fd = tmpfd; |
| 374 | tmpfd = NULL; |
| 375 | } |
| 376 | |
| 377 | break; |
| 378 | |
| 379 | } |
| 380 | |
| 381 | } |
| 382 | |
| 383 | msnprintf(strbuff, sizeof(strbuff), fmt, num_open.rlim_max); |
| 384 | fprintf(stderr, "%s file descriptors open\n", strbuff); |
| 385 | |
| 386 | #if !defined(HAVE_POLL_FINE) && !defined(USE_WINSOCK) |
| 387 | |
| 388 | /* |
| 389 | * when using select() instead of poll() we cannot test |
| 390 | * libcurl functionality with a socket number equal or |
| 391 | * greater than FD_SETSIZE. In any case, macro VERIFY_SOCK |
| 392 | * in lib/select.c enforces this check and protects libcurl |
| 393 | * from a possible crash. The effect of this protection |
| 394 | * is that test 537 will always fail, since the actual |
| 395 | * call to select() never takes place. We skip test 537 |
| 396 | * with an indication that select limit would be exceeded. |
| 397 | */ |
| 398 | |
| 399 | num_open.rlim_cur = FD_SETSIZE - SAFETY_MARGIN; |
| 400 | if(num_open.rlim_max > num_open.rlim_cur) { |
| 401 | msnprintf(strbuff, sizeof(strbuff), "select limit is FD_SETSIZE %d", |
| 402 | FD_SETSIZE); |
| 403 | store_errmsg(strbuff, 0); |
| 404 | fprintf(stderr, "%s\n", msgbuff); |
| 405 | close_file_descriptors(); |
| 406 | free(memchunk); |
| 407 | return -8; |
| 408 | } |
| 409 | |
| 410 | num_open.rlim_cur = FD_SETSIZE - SAFETY_MARGIN; |
| 411 | for(rl.rlim_cur = 0; |
| 412 | rl.rlim_cur < num_open.rlim_max; |
| 413 | rl.rlim_cur++) { |
| 414 | if((fd[rl.rlim_cur] > 0) && |
| 415 | ((unsigned int)fd[rl.rlim_cur] > num_open.rlim_cur)) { |
| 416 | msnprintf(strbuff, sizeof(strbuff), "select limit is FD_SETSIZE %d", |
| 417 | FD_SETSIZE); |
| 418 | store_errmsg(strbuff, 0); |
| 419 | fprintf(stderr, "%s\n", msgbuff); |
| 420 | close_file_descriptors(); |
| 421 | free(memchunk); |
| 422 | return -9; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | #endif /* using a FD_SETSIZE bound select() */ |
| 427 | |
| 428 | /* |
| 429 | * Old or 'backwards compatible' implementations of stdio do not allow |
| 430 | * handling of streams with an underlying file descriptor number greater |
| 431 | * than 255, even when allowing high numbered file descriptors for sockets. |
| 432 | * At this point we have a big number of file descriptors which have been |
| 433 | * opened using dup(), so lets test the stdio implementation and discover |
| 434 | * if it is capable of fopen()ing some additional files. |
| 435 | */ |
| 436 | |
| 437 | if(!fopen_works()) { |
| 438 | msnprintf(strbuff1, sizeof(strbuff1), fmt, num_open.rlim_max); |
| 439 | msnprintf(strbuff, sizeof(strbuff), "fopen fails with %s fds open", |
| 440 | strbuff1); |
| 441 | fprintf(stderr, "%s\n", msgbuff); |
| 442 | msnprintf(strbuff, sizeof(strbuff), "fopen fails with lots of fds open"); |
| 443 | store_errmsg(strbuff, 0); |
| 444 | close_file_descriptors(); |
| 445 | free(memchunk); |
| 446 | return -10; |
| 447 | } |
| 448 | |
| 449 | /* free the chunk of memory we were reserving so that it |
| 450 | becomes becomes available to the test */ |
| 451 | |
| 452 | free(memchunk); |
| 453 | |
| 454 | /* close file descriptors unless instructed to keep them */ |
| 455 | |
| 456 | if(!keep_open) { |
| 457 | close_file_descriptors(); |
| 458 | } |
| 459 | |
| 460 | return 0; |
| 461 | } |
| 462 | |
| 463 | int test(char *URL) |
| 464 | { |
| 465 | CURLcode res; |
| 466 | CURL *curl; |
| 467 | |
| 468 | if(!strcmp(URL, "check")) { |
| 469 | /* used by the test script to ask if we can run this test or not */ |
| 470 | if(rlimit(FALSE)) { |
| 471 | fprintf(stdout, "rlimit problem: %s\n", msgbuff); |
| 472 | return 1; |
| 473 | } |
| 474 | return 0; /* sure, run this! */ |
| 475 | } |
| 476 | |
| 477 | if(rlimit(TRUE)) { |
| 478 | /* failure */ |
| 479 | return TEST_ERR_MAJOR_BAD; |
| 480 | } |
| 481 | |
| 482 | /* run the test with the bunch of open file descriptors |
| 483 | and close them all once the test is over */ |
| 484 | |
| 485 | if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) { |
| 486 | fprintf(stderr, "curl_global_init() failed\n"); |
| 487 | close_file_descriptors(); |
| 488 | return TEST_ERR_MAJOR_BAD; |
| 489 | } |
| 490 | |
| 491 | curl = curl_easy_init(); |
| 492 | if(!curl) { |
| 493 | fprintf(stderr, "curl_easy_init() failed\n"); |
| 494 | close_file_descriptors(); |
| 495 | curl_global_cleanup(); |
| 496 | return TEST_ERR_MAJOR_BAD; |
| 497 | } |
| 498 | |
| 499 | test_setopt(curl, CURLOPT_URL, URL); |
| 500 | test_setopt(curl, CURLOPT_HEADER, 1L); |
| 501 | |
| 502 | res = curl_easy_perform(curl); |
| 503 | |
| 504 | test_cleanup: |
| 505 | |
| 506 | close_file_descriptors(); |
| 507 | curl_easy_cleanup(curl); |
| 508 | curl_global_cleanup(); |
| 509 | |
| 510 | return (int)res; |
| 511 | } |
| 512 | |
| 513 | #else /* defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT) */ |
| 514 | |
| 515 | int test(char *URL) |
| 516 | { |
| 517 | (void)URL; |
| 518 | printf("system lacks necessary system function(s)"); |
| 519 | return 1; /* skip test */ |
| 520 | } |
| 521 | |
| 522 | #endif /* defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT) */ |