2011-03-10 05:48:02 -05:00
|
|
|
/***************************************************************************
|
2006-10-25 05:20:44 -04:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2013-01-03 20:50:28 -05:00
|
|
|
* Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2011-03-10 05:48:02 -05:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2016-02-02 18:19:02 -05:00
|
|
|
* are also available at https://curl.haxx.se/docs/copyright.html.
|
2011-03-10 05:48:02 -05:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
2004-11-19 03:52:33 -05:00
|
|
|
#include "test.h"
|
|
|
|
|
2004-11-22 17:26:46 -05:00
|
|
|
#ifdef HAVE_SYS_RESOURCE_H
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_FCNTL_H
|
2004-11-19 03:52:33 -05:00
|
|
|
#include <fcntl.h>
|
2004-11-22 17:26:46 -05:00
|
|
|
#endif
|
2006-11-02 10:47:24 -05:00
|
|
|
#ifdef HAVE_LIMITS_H
|
|
|
|
#include <limits.h>
|
|
|
|
#endif
|
2004-11-19 03:52:33 -05:00
|
|
|
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "warnless.h"
|
|
|
|
#include "memdebug.h"
|
2008-09-20 00:26:55 -04:00
|
|
|
|
2006-11-02 15:50:18 -05:00
|
|
|
#ifndef FD_SETSIZE
|
2004-11-19 03:52:33 -05:00
|
|
|
#error "this test requires FD_SETSIZE"
|
|
|
|
#endif
|
|
|
|
|
2006-11-02 15:50:18 -05:00
|
|
|
#define SAFETY_MARGIN (16)
|
|
|
|
#define NUM_OPEN (FD_SETSIZE + 10)
|
|
|
|
#define NUM_NEEDED (NUM_OPEN + SAFETY_MARGIN)
|
2004-11-19 03:52:33 -05:00
|
|
|
|
2006-10-11 12:01:16 -04:00
|
|
|
#if defined(WIN32) || defined(_WIN32) || defined(MSDOS)
|
2004-11-19 08:50:41 -05:00
|
|
|
#define DEV_NULL "NUL"
|
|
|
|
#else
|
|
|
|
#define DEV_NULL "/dev/null"
|
|
|
|
#endif
|
|
|
|
|
2004-11-22 17:26:46 -05:00
|
|
|
#if defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT)
|
2004-11-23 04:50:16 -05:00
|
|
|
|
2006-10-30 12:24:31 -05:00
|
|
|
static int *fd = NULL;
|
|
|
|
static struct rlimit num_open;
|
2006-11-01 13:33:50 -05:00
|
|
|
static char msgbuff[256];
|
2004-12-01 05:34:46 -05:00
|
|
|
|
2006-11-01 13:33:50 -05:00
|
|
|
static void store_errmsg(const char *msg, int err)
|
|
|
|
{
|
|
|
|
if (!err)
|
2006-11-02 15:50:18 -05:00
|
|
|
sprintf(msgbuff, "%s", msg);
|
2006-11-01 13:33:50 -05:00
|
|
|
else
|
2006-11-02 15:50:18 -05:00
|
|
|
sprintf(msgbuff, "%s, errno %d, %s", msg, err, strerror(err));
|
2006-11-01 13:33:50 -05:00
|
|
|
}
|
|
|
|
|
2006-10-26 09:55:24 -04:00
|
|
|
static void close_file_descriptors(void)
|
|
|
|
{
|
2006-10-30 12:24:31 -05:00
|
|
|
for (num_open.rlim_cur = 0;
|
|
|
|
num_open.rlim_cur < num_open.rlim_max;
|
|
|
|
num_open.rlim_cur++)
|
2006-11-02 15:50:18 -05:00
|
|
|
if (fd[num_open.rlim_cur] > 0)
|
|
|
|
close(fd[num_open.rlim_cur]);
|
2006-10-30 12:24:31 -05:00
|
|
|
free(fd);
|
|
|
|
fd = NULL;
|
2006-10-26 09:55:24 -04:00
|
|
|
}
|
|
|
|
|
2007-04-03 23:19:59 -04:00
|
|
|
static int fopen_works(void)
|
2007-04-03 14:02:02 -04:00
|
|
|
{
|
2007-04-04 02:39:03 -04:00
|
|
|
FILE *fpa[3];
|
2007-04-03 14:02:02 -04:00
|
|
|
int i;
|
2007-04-03 23:19:59 -04:00
|
|
|
int ret = 1;
|
2007-04-03 14:02:02 -04:00
|
|
|
|
2007-04-04 02:39:03 -04:00
|
|
|
for (i = 0; i < 3; i++) {
|
2007-04-03 14:02:02 -04:00
|
|
|
fpa[i] = NULL;
|
|
|
|
}
|
2007-04-04 02:39:03 -04:00
|
|
|
for (i = 0; i < 3; i++) {
|
2007-04-03 14:02:02 -04:00
|
|
|
fpa[i] = fopen(DEV_NULL, "r");
|
|
|
|
if (fpa[i] == NULL) {
|
2007-04-04 04:58:36 -04:00
|
|
|
store_errmsg("fopen() failed", ERRNO);
|
|
|
|
fprintf(stderr, "%s\n", msgbuff);
|
2007-04-03 23:19:59 -04:00
|
|
|
ret = 0;
|
2007-04-03 14:02:02 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-04-04 02:39:03 -04:00
|
|
|
for (i = 0; i < 3; i++) {
|
2007-04-03 14:02:02 -04:00
|
|
|
if (fpa[i] != NULL)
|
|
|
|
fclose(fpa[i]);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-10-26 09:55:24 -04:00
|
|
|
static int rlimit(int keep_open)
|
2004-11-19 03:52:33 -05:00
|
|
|
{
|
2006-11-02 15:50:18 -05:00
|
|
|
int nitems, i;
|
2006-11-01 13:33:50 -05:00
|
|
|
int *memchunk = NULL;
|
2006-10-30 12:24:31 -05:00
|
|
|
char *fmt;
|
2004-11-22 17:26:46 -05:00
|
|
|
struct rlimit rl;
|
2006-11-01 13:33:50 -05:00
|
|
|
char strbuff[256];
|
|
|
|
char strbuff1[81];
|
|
|
|
char strbuff2[81];
|
2006-10-30 12:24:31 -05:00
|
|
|
char fmt_u[] = "%u";
|
|
|
|
char fmt_lu[] = "%lu";
|
|
|
|
#ifdef HAVE_LONGLONG
|
|
|
|
char fmt_llu[] = "%llu";
|
2006-10-26 09:55:24 -04:00
|
|
|
|
2006-10-30 12:24:31 -05:00
|
|
|
if (sizeof(rl.rlim_max) > sizeof(long))
|
|
|
|
fmt = fmt_llu;
|
2006-10-26 09:55:24 -04:00
|
|
|
else
|
2006-10-30 12:24:31 -05:00
|
|
|
#endif
|
|
|
|
fmt = (sizeof(rl.rlim_max) < sizeof(long))?fmt_u:fmt_lu;
|
2004-11-28 03:57:03 -05:00
|
|
|
|
2006-10-30 12:24:31 -05:00
|
|
|
/* get initial open file limits */
|
2004-11-28 03:57:03 -05:00
|
|
|
|
2006-10-26 09:55:24 -04:00
|
|
|
if (getrlimit(RLIMIT_NOFILE, &rl) != 0) {
|
2007-02-16 11:01:19 -05:00
|
|
|
store_errmsg("getrlimit() failed", ERRNO);
|
2006-11-01 13:33:50 -05:00
|
|
|
fprintf(stderr, "%s\n", msgbuff);
|
2004-11-23 04:50:16 -05:00
|
|
|
return -1;
|
2004-11-22 17:26:46 -05:00
|
|
|
}
|
|
|
|
|
2006-10-30 12:24:31 -05:00
|
|
|
/* show initial open file limits */
|
|
|
|
|
2006-10-26 09:55:24 -04:00
|
|
|
#ifdef RLIM_INFINITY
|
2006-10-30 12:24:31 -05:00
|
|
|
if (rl.rlim_cur == RLIM_INFINITY)
|
|
|
|
strcpy(strbuff, "INFINITY");
|
|
|
|
else
|
2006-10-26 09:55:24 -04:00
|
|
|
#endif
|
2006-11-02 15:50:18 -05:00
|
|
|
sprintf(strbuff, fmt, rl.rlim_cur);
|
2006-11-01 13:33:50 -05:00
|
|
|
fprintf(stderr, "initial soft limit: %s\n", strbuff);
|
2004-11-22 17:26:46 -05:00
|
|
|
|
2006-10-26 09:55:24 -04:00
|
|
|
#ifdef RLIM_INFINITY
|
2006-10-30 12:24:31 -05:00
|
|
|
if (rl.rlim_max == RLIM_INFINITY)
|
|
|
|
strcpy(strbuff, "INFINITY");
|
|
|
|
else
|
|
|
|
#endif
|
2006-11-02 15:50:18 -05:00
|
|
|
sprintf(strbuff, fmt, rl.rlim_max);
|
2006-11-01 13:33:50 -05:00
|
|
|
fprintf(stderr, "initial hard limit: %s\n", strbuff);
|
2006-10-30 12:24:31 -05:00
|
|
|
|
|
|
|
/* show our constants */
|
|
|
|
|
|
|
|
fprintf(stderr, "test518 FD_SETSIZE: %d\n", FD_SETSIZE);
|
|
|
|
fprintf(stderr, "test518 NUM_OPEN : %d\n", NUM_OPEN);
|
|
|
|
fprintf(stderr, "test518 NUM_NEEDED: %d\n", NUM_NEEDED);
|
|
|
|
|
2006-11-01 13:33:50 -05:00
|
|
|
/*
|
|
|
|
* if soft limit and hard limit are different we ask the
|
|
|
|
* system to raise soft limit all the way up to the hard
|
|
|
|
* limit. Due to some other system limit the soft limit
|
|
|
|
* might not be raised up to the hard limit. So from this
|
|
|
|
* point the resulting soft limit is our limit. Trying to
|
|
|
|
* open more than soft limit file descriptors will fail.
|
|
|
|
*/
|
2006-10-30 12:24:31 -05:00
|
|
|
|
|
|
|
if (rl.rlim_cur != rl.rlim_max) {
|
2006-11-02 10:47:24 -05:00
|
|
|
|
2008-01-17 13:57:50 -05:00
|
|
|
#ifdef OPEN_MAX
|
|
|
|
if ((rl.rlim_cur > 0) &&
|
|
|
|
(rl.rlim_cur < OPEN_MAX)) {
|
|
|
|
fprintf(stderr, "raising soft limit up to OPEN_MAX\n");
|
|
|
|
rl.rlim_cur = OPEN_MAX;
|
|
|
|
if (setrlimit(RLIMIT_NOFILE, &rl) != 0) {
|
|
|
|
/* on failure don't abort just issue a warning */
|
|
|
|
store_errmsg("setrlimit() failed", ERRNO);
|
|
|
|
fprintf(stderr, "%s\n", msgbuff);
|
|
|
|
msgbuff[0] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-11-01 13:33:50 -05:00
|
|
|
fprintf(stderr, "raising soft limit up to hard limit\n");
|
2006-10-30 12:24:31 -05:00
|
|
|
rl.rlim_cur = rl.rlim_max;
|
|
|
|
if (setrlimit(RLIMIT_NOFILE, &rl) != 0) {
|
2008-01-17 13:57:50 -05:00
|
|
|
/* on failure don't abort just issue a warning */
|
2007-02-16 11:01:19 -05:00
|
|
|
store_errmsg("setrlimit() failed", ERRNO);
|
2006-11-01 13:33:50 -05:00
|
|
|
fprintf(stderr, "%s\n", msgbuff);
|
2008-01-17 13:57:50 -05:00
|
|
|
msgbuff[0] = '\0';
|
2004-12-01 05:34:46 -05:00
|
|
|
}
|
2006-10-30 12:24:31 -05:00
|
|
|
|
2006-11-02 10:47:24 -05:00
|
|
|
/* get current open file limits */
|
2006-10-30 12:24:31 -05:00
|
|
|
|
2006-11-02 10:47:24 -05:00
|
|
|
if (getrlimit(RLIMIT_NOFILE, &rl) != 0) {
|
2007-02-16 11:01:19 -05:00
|
|
|
store_errmsg("getrlimit() failed", ERRNO);
|
2006-11-02 10:47:24 -05:00
|
|
|
fprintf(stderr, "%s\n", msgbuff);
|
|
|
|
return -3;
|
|
|
|
}
|
2006-10-30 12:24:31 -05:00
|
|
|
|
2006-11-02 10:47:24 -05:00
|
|
|
/* show current open file limits */
|
2006-10-26 09:55:24 -04:00
|
|
|
|
|
|
|
#ifdef RLIM_INFINITY
|
2006-11-02 10:47:24 -05:00
|
|
|
if (rl.rlim_cur == RLIM_INFINITY)
|
|
|
|
strcpy(strbuff, "INFINITY");
|
|
|
|
else
|
2006-10-26 09:55:24 -04:00
|
|
|
#endif
|
2006-11-02 15:50:18 -05:00
|
|
|
sprintf(strbuff, fmt, rl.rlim_cur);
|
2006-11-02 10:47:24 -05:00
|
|
|
fprintf(stderr, "current soft limit: %s\n", strbuff);
|
2006-10-26 09:55:24 -04:00
|
|
|
|
|
|
|
#ifdef RLIM_INFINITY
|
2006-11-02 10:47:24 -05:00
|
|
|
if (rl.rlim_max == RLIM_INFINITY)
|
|
|
|
strcpy(strbuff, "INFINITY");
|
|
|
|
else
|
2006-10-26 09:55:24 -04:00
|
|
|
#endif
|
2006-11-02 15:50:18 -05:00
|
|
|
sprintf(strbuff, fmt, rl.rlim_max);
|
2006-11-02 10:47:24 -05:00
|
|
|
fprintf(stderr, "current hard limit: %s\n", strbuff);
|
|
|
|
|
|
|
|
} /* (rl.rlim_cur != rl.rlim_max) */
|
2006-11-01 13:33:50 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* test 518 is all about testing libcurl functionality
|
|
|
|
* when more than FD_SETSIZE file descriptors are open.
|
|
|
|
* This means that if for any reason we are not able to
|
|
|
|
* open more than FD_SETSIZE file descriptors then test
|
|
|
|
* 518 should not be run.
|
|
|
|
*/
|
2006-10-30 12:24:31 -05:00
|
|
|
|
2006-11-01 13:33:50 -05:00
|
|
|
/*
|
|
|
|
* verify that soft limit is higher than NUM_NEEDED,
|
2010-02-14 14:40:18 -05:00
|
|
|
* which is the number of file descriptors we would
|
2006-11-02 20:56:55 -05:00
|
|
|
* try to open plus SAFETY_MARGIN to not exhaust the
|
|
|
|
* file descriptor pool
|
2006-11-01 13:33:50 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
num_open.rlim_cur = NUM_NEEDED;
|
|
|
|
|
|
|
|
if ((rl.rlim_cur > 0) &&
|
|
|
|
#ifdef RLIM_INFINITY
|
|
|
|
(rl.rlim_cur != RLIM_INFINITY) &&
|
|
|
|
#endif
|
|
|
|
(rl.rlim_cur <= num_open.rlim_cur)) {
|
2006-11-02 15:50:18 -05:00
|
|
|
sprintf(strbuff2, fmt, rl.rlim_cur);
|
|
|
|
sprintf(strbuff1, fmt, num_open.rlim_cur);
|
2006-11-03 05:05:21 -05:00
|
|
|
sprintf(strbuff, "fds needed %s > system limit %s",
|
2006-11-02 15:50:18 -05:00
|
|
|
strbuff1, strbuff2);
|
2006-11-01 13:33:50 -05:00
|
|
|
store_errmsg(strbuff, 0);
|
|
|
|
fprintf(stderr, "%s\n", msgbuff);
|
2006-11-02 15:50:18 -05:00
|
|
|
return -4;
|
2006-11-01 13:33:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* reserve a chunk of memory before opening file descriptors to
|
|
|
|
* avoid a low memory condition once the file descriptors are
|
|
|
|
* open. System conditions that could make the test fail should
|
|
|
|
* be addressed in the precheck phase. This chunk of memory shall
|
|
|
|
* be always free()ed before exiting the rlimit() function so
|
|
|
|
* that it becomes available to the test.
|
|
|
|
*/
|
|
|
|
|
2006-11-03 05:05:21 -05:00
|
|
|
for (nitems = i = 1; nitems <= i; i *= 2)
|
|
|
|
nitems = i;
|
|
|
|
if (nitems > 0x7fff)
|
|
|
|
nitems = 0x40000;
|
2006-11-02 15:50:18 -05:00
|
|
|
do {
|
2006-11-03 05:05:21 -05:00
|
|
|
num_open.rlim_max = sizeof(*memchunk) * (size_t)nitems;
|
|
|
|
sprintf(strbuff, fmt, num_open.rlim_max);
|
|
|
|
fprintf(stderr, "allocating memchunk %s byte array\n", strbuff);
|
2006-11-02 15:50:18 -05:00
|
|
|
memchunk = malloc(sizeof(*memchunk) * (size_t)nitems);
|
2006-11-03 05:05:21 -05:00
|
|
|
if (!memchunk) {
|
|
|
|
fprintf(stderr, "memchunk, malloc() failed\n");
|
2006-11-02 15:50:18 -05:00
|
|
|
nitems /= 2;
|
2006-11-03 05:05:21 -05:00
|
|
|
}
|
2006-11-02 15:50:18 -05:00
|
|
|
} while (nitems && !memchunk);
|
2006-11-01 13:33:50 -05:00
|
|
|
if (!memchunk) {
|
2007-02-16 11:01:19 -05:00
|
|
|
store_errmsg("memchunk, malloc() failed", ERRNO);
|
2006-11-01 13:33:50 -05:00
|
|
|
fprintf(stderr, "%s\n", msgbuff);
|
2006-11-02 20:56:55 -05:00
|
|
|
return -5;
|
2006-11-01 13:33:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* initialize it to fight lazy allocation */
|
|
|
|
|
2006-11-03 05:05:21 -05:00
|
|
|
fprintf(stderr, "initializing memchunk array\n");
|
|
|
|
|
2006-11-02 15:50:18 -05:00
|
|
|
for (i = 0; i < nitems; i++)
|
2006-11-01 13:33:50 -05:00
|
|
|
memchunk[i] = -1;
|
|
|
|
|
2006-11-02 15:50:18 -05:00
|
|
|
/* set the number of file descriptors we will try to open */
|
2006-11-01 13:33:50 -05:00
|
|
|
|
2006-10-30 12:24:31 -05:00
|
|
|
num_open.rlim_max = NUM_OPEN;
|
|
|
|
|
|
|
|
/* verify that we won't overflow size_t in malloc() */
|
|
|
|
|
2007-01-27 22:51:10 -05:00
|
|
|
if ((size_t)(num_open.rlim_max) > ((size_t)-1) / sizeof(*fd)) {
|
2006-11-02 15:50:18 -05:00
|
|
|
sprintf(strbuff1, fmt, num_open.rlim_max);
|
|
|
|
sprintf(strbuff, "unable to allocate an array for %s "
|
|
|
|
"file descriptors, would overflow size_t", strbuff1);
|
2006-11-01 13:33:50 -05:00
|
|
|
store_errmsg(strbuff, 0);
|
|
|
|
fprintf(stderr, "%s\n", msgbuff);
|
|
|
|
free(memchunk);
|
2006-11-02 20:56:55 -05:00
|
|
|
return -6;
|
2006-10-30 12:24:31 -05:00
|
|
|
}
|
|
|
|
|
2006-11-02 15:50:18 -05:00
|
|
|
/* allocate array for file descriptors */
|
|
|
|
|
|
|
|
sprintf(strbuff, fmt, num_open.rlim_max);
|
2006-11-01 13:33:50 -05:00
|
|
|
fprintf(stderr, "allocating array for %s file descriptors\n", strbuff);
|
|
|
|
|
2006-10-30 12:24:31 -05:00
|
|
|
fd = malloc(sizeof(*fd) * (size_t)(num_open.rlim_max));
|
|
|
|
if (!fd) {
|
2007-02-16 11:01:19 -05:00
|
|
|
store_errmsg("fd, malloc() failed", ERRNO);
|
2006-11-01 13:33:50 -05:00
|
|
|
fprintf(stderr, "%s\n", msgbuff);
|
|
|
|
free(memchunk);
|
2006-11-02 20:56:55 -05:00
|
|
|
return -7;
|
2006-10-30 12:24:31 -05:00
|
|
|
}
|
|
|
|
|
2006-11-01 13:33:50 -05:00
|
|
|
/* initialize it to fight lazy allocation */
|
2006-10-30 12:24:31 -05:00
|
|
|
|
2006-11-03 05:05:21 -05:00
|
|
|
fprintf(stderr, "initializing fd array\n");
|
|
|
|
|
2006-10-30 12:24:31 -05:00
|
|
|
for (num_open.rlim_cur = 0;
|
|
|
|
num_open.rlim_cur < num_open.rlim_max;
|
|
|
|
num_open.rlim_cur++)
|
|
|
|
fd[num_open.rlim_cur] = -1;
|
|
|
|
|
2006-11-02 15:50:18 -05:00
|
|
|
sprintf(strbuff, fmt, num_open.rlim_max);
|
2006-11-01 13:33:50 -05:00
|
|
|
fprintf(stderr, "trying to open %s file descriptors\n", strbuff);
|
2006-10-30 12:24:31 -05:00
|
|
|
|
2004-12-01 05:34:46 -05:00
|
|
|
/* open a dummy descriptor */
|
2006-11-01 13:33:50 -05:00
|
|
|
|
2004-12-01 05:34:46 -05:00
|
|
|
fd[0] = open(DEV_NULL, O_RDONLY);
|
2006-10-26 09:55:24 -04:00
|
|
|
if (fd[0] < 0) {
|
2006-11-02 15:50:18 -05:00
|
|
|
sprintf(strbuff, "opening of %s failed", DEV_NULL);
|
2007-02-16 11:01:19 -05:00
|
|
|
store_errmsg(strbuff, ERRNO);
|
2006-11-01 13:33:50 -05:00
|
|
|
fprintf(stderr, "%s\n", msgbuff);
|
2006-10-30 12:24:31 -05:00
|
|
|
free(fd);
|
|
|
|
fd = NULL;
|
2006-11-01 13:33:50 -05:00
|
|
|
free(memchunk);
|
2006-11-02 20:56:55 -05:00
|
|
|
return -8;
|
2004-12-01 05:34:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* create a bunch of file descriptors */
|
2006-11-01 13:33:50 -05:00
|
|
|
|
2010-02-14 14:40:18 -05:00
|
|
|
for (num_open.rlim_cur = 1;
|
|
|
|
num_open.rlim_cur < num_open.rlim_max;
|
2006-10-30 12:24:31 -05:00
|
|
|
num_open.rlim_cur++) {
|
2006-11-01 13:33:50 -05:00
|
|
|
|
2006-10-30 12:24:31 -05:00
|
|
|
fd[num_open.rlim_cur] = dup(fd[0]);
|
2006-11-01 13:33:50 -05:00
|
|
|
|
2006-10-30 12:24:31 -05:00
|
|
|
if (fd[num_open.rlim_cur] < 0) {
|
2006-11-01 13:33:50 -05:00
|
|
|
|
|
|
|
fd[num_open.rlim_cur] = -1;
|
|
|
|
|
2006-11-03 09:13:25 -05:00
|
|
|
sprintf(strbuff1, fmt, num_open.rlim_cur);
|
|
|
|
sprintf(strbuff, "dup() attempt %s failed", strbuff1);
|
|
|
|
fprintf(stderr, "%s\n", strbuff);
|
|
|
|
|
|
|
|
sprintf(strbuff1, fmt, num_open.rlim_cur);
|
|
|
|
sprintf(strbuff, "fds system limit seems close to %s", strbuff1);
|
|
|
|
fprintf(stderr, "%s\n", strbuff);
|
|
|
|
|
2006-11-02 10:47:24 -05:00
|
|
|
num_open.rlim_max = NUM_NEEDED;
|
2006-11-03 09:13:25 -05:00
|
|
|
|
2006-11-02 15:50:18 -05:00
|
|
|
sprintf(strbuff2, fmt, num_open.rlim_max);
|
|
|
|
sprintf(strbuff1, fmt, num_open.rlim_cur);
|
2006-11-03 05:05:21 -05:00
|
|
|
sprintf(strbuff, "fds needed %s > system limit %s",
|
2006-11-02 15:50:18 -05:00
|
|
|
strbuff2, strbuff1);
|
2006-11-02 10:47:24 -05:00
|
|
|
store_errmsg(strbuff, 0);
|
2006-11-01 13:33:50 -05:00
|
|
|
fprintf(stderr, "%s\n", msgbuff);
|
|
|
|
|
2006-10-30 12:24:31 -05:00
|
|
|
for (num_open.rlim_cur = 0;
|
|
|
|
fd[num_open.rlim_cur] >= 0;
|
|
|
|
num_open.rlim_cur++)
|
|
|
|
close(fd[num_open.rlim_cur]);
|
|
|
|
free(fd);
|
|
|
|
fd = NULL;
|
2006-11-01 13:33:50 -05:00
|
|
|
free(memchunk);
|
2006-11-02 20:56:55 -05:00
|
|
|
return -9;
|
2006-11-01 13:33:50 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-11-02 15:50:18 -05:00
|
|
|
sprintf(strbuff, fmt, num_open.rlim_max);
|
2006-10-30 20:24:03 -05:00
|
|
|
fprintf(stderr, "%s file descriptors open\n", strbuff);
|
|
|
|
|
2007-01-29 22:48:29 -05:00
|
|
|
#if !defined(HAVE_POLL_FINE) && \
|
|
|
|
!defined(USE_WINSOCK) && \
|
|
|
|
!defined(TPF)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* when using select() instead of poll() we cannot test
|
|
|
|
* libcurl functionality with a socket number equal or
|
|
|
|
* greater than FD_SETSIZE. In any case, macro VERIFY_SOCK
|
2013-01-03 20:50:28 -05:00
|
|
|
* in lib/select.c enforces this check and protects libcurl
|
2007-01-29 22:48:29 -05:00
|
|
|
* from a possible crash. The effect of this protection
|
|
|
|
* is that test 518 will always fail, since the actual
|
|
|
|
* call to select() never takes place. We skip test 518
|
|
|
|
* with an indication that select limit would be exceeded.
|
|
|
|
*/
|
|
|
|
|
2007-01-31 10:34:53 -05:00
|
|
|
num_open.rlim_cur = FD_SETSIZE - SAFETY_MARGIN;
|
|
|
|
if (num_open.rlim_max > num_open.rlim_cur) {
|
|
|
|
sprintf(strbuff, "select limit is FD_SETSIZE %d", FD_SETSIZE);
|
|
|
|
store_errmsg(strbuff, 0);
|
|
|
|
fprintf(stderr, "%s\n", msgbuff);
|
|
|
|
close_file_descriptors();
|
|
|
|
free(memchunk);
|
|
|
|
return -10;
|
|
|
|
}
|
2007-01-29 22:48:29 -05:00
|
|
|
|
2007-01-31 10:34:53 -05:00
|
|
|
num_open.rlim_cur = FD_SETSIZE - SAFETY_MARGIN;
|
|
|
|
for (rl.rlim_cur = 0;
|
|
|
|
rl.rlim_cur < num_open.rlim_max;
|
|
|
|
rl.rlim_cur++) {
|
2007-09-29 21:01:43 -04:00
|
|
|
if ((fd[rl.rlim_cur] > 0) &&
|
2007-09-29 21:27:39 -04:00
|
|
|
((unsigned int)fd[rl.rlim_cur] > num_open.rlim_cur)) {
|
2007-01-31 10:34:53 -05:00
|
|
|
sprintf(strbuff, "select limit is FD_SETSIZE %d", FD_SETSIZE);
|
|
|
|
store_errmsg(strbuff, 0);
|
|
|
|
fprintf(stderr, "%s\n", msgbuff);
|
|
|
|
close_file_descriptors();
|
|
|
|
free(memchunk);
|
|
|
|
return -11;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* using a FD_SETSIZE bound select() */
|
2007-01-29 22:48:29 -05:00
|
|
|
|
2007-04-04 04:58:36 -04:00
|
|
|
/*
|
|
|
|
* Old or 'backwards compatible' implementations of stdio do not allow
|
|
|
|
* handling of streams with an underlying file descriptor number greater
|
|
|
|
* than 255, even when allowing high numbered file descriptors for sockets.
|
|
|
|
* At this point we have a big number of file descriptors which have been
|
|
|
|
* opened using dup(), so lets test the stdio implementation and discover
|
|
|
|
* if it is capable of fopen()ing some additional files.
|
|
|
|
*/
|
2007-04-03 23:19:59 -04:00
|
|
|
|
|
|
|
if (!fopen_works()) {
|
|
|
|
sprintf(strbuff1, fmt, num_open.rlim_max);
|
2007-04-04 04:58:36 -04:00
|
|
|
sprintf(strbuff, "stdio fopen() fails with %s fds open()",
|
2007-04-03 23:19:59 -04:00
|
|
|
strbuff1);
|
|
|
|
fprintf(stderr, "%s\n", msgbuff);
|
2007-04-05 07:05:36 -04:00
|
|
|
sprintf(strbuff, "stdio fopen() fails with lots of fds open()");
|
|
|
|
store_errmsg(strbuff, 0);
|
2007-04-03 23:19:59 -04:00
|
|
|
close_file_descriptors();
|
|
|
|
free(memchunk);
|
|
|
|
return -12;
|
|
|
|
}
|
|
|
|
|
2006-11-01 13:33:50 -05:00
|
|
|
/* free the chunk of memory we were reserving so that it
|
|
|
|
becomes becomes available to the test */
|
|
|
|
|
|
|
|
free(memchunk);
|
|
|
|
|
2006-10-26 09:55:24 -04:00
|
|
|
/* close file descriptors unless instructed to keep them */
|
2006-11-02 20:56:55 -05:00
|
|
|
|
2006-10-26 09:55:24 -04:00
|
|
|
if (!keep_open) {
|
|
|
|
close_file_descriptors();
|
|
|
|
}
|
|
|
|
|
2004-11-23 04:50:16 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int test(char *URL)
|
|
|
|
{
|
|
|
|
CURLcode res;
|
|
|
|
CURL *curl;
|
|
|
|
|
|
|
|
if(!strcmp(URL, "check")) {
|
|
|
|
/* used by the test script to ask if we can run this test or not */
|
2006-10-26 09:55:24 -04:00
|
|
|
if(rlimit(FALSE)) {
|
2006-11-01 13:33:50 -05:00
|
|
|
fprintf(stdout, "rlimit problem: %s\n", msgbuff);
|
2004-11-23 04:50:16 -05:00
|
|
|
return 1;
|
2004-11-22 17:26:46 -05:00
|
|
|
}
|
2004-11-23 04:50:16 -05:00
|
|
|
return 0; /* sure, run this! */
|
2004-11-22 17:26:46 -05:00
|
|
|
}
|
|
|
|
|
2006-10-30 12:24:31 -05:00
|
|
|
if (rlimit(TRUE)) {
|
2004-11-23 04:50:16 -05:00
|
|
|
/* failure */
|
2006-10-30 12:24:31 -05:00
|
|
|
return TEST_ERR_MAJOR_BAD;
|
|
|
|
}
|
2004-11-23 04:50:16 -05:00
|
|
|
|
2010-02-14 14:40:18 -05:00
|
|
|
/* run the test with the bunch of open file descriptors
|
2006-11-01 13:33:50 -05:00
|
|
|
and close them all once the test is over */
|
2006-10-26 09:55:24 -04:00
|
|
|
|
2006-10-25 01:59:46 -04:00
|
|
|
if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
|
|
|
|
fprintf(stderr, "curl_global_init() failed\n");
|
2006-10-26 09:55:24 -04:00
|
|
|
close_file_descriptors();
|
2006-10-25 01:59:46 -04:00
|
|
|
return TEST_ERR_MAJOR_BAD;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((curl = curl_easy_init()) == NULL) {
|
|
|
|
fprintf(stderr, "curl_easy_init() failed\n");
|
2006-10-26 09:55:24 -04:00
|
|
|
close_file_descriptors();
|
2006-10-25 01:59:46 -04:00
|
|
|
curl_global_cleanup();
|
|
|
|
return TEST_ERR_MAJOR_BAD;
|
|
|
|
}
|
|
|
|
|
2010-02-05 13:07:19 -05:00
|
|
|
test_setopt(curl, CURLOPT_URL, URL);
|
|
|
|
test_setopt(curl, CURLOPT_HEADER, 1L);
|
2006-10-26 09:55:24 -04:00
|
|
|
|
2004-11-19 03:52:33 -05:00
|
|
|
res = curl_easy_perform(curl);
|
2006-10-26 09:55:24 -04:00
|
|
|
|
2010-02-05 13:07:19 -05:00
|
|
|
test_cleanup:
|
|
|
|
|
2006-10-26 09:55:24 -04:00
|
|
|
close_file_descriptors();
|
2004-11-19 03:52:33 -05:00
|
|
|
curl_easy_cleanup(curl);
|
2006-10-25 01:59:46 -04:00
|
|
|
curl_global_cleanup();
|
2004-11-19 03:52:33 -05:00
|
|
|
|
|
|
|
return (int)res;
|
|
|
|
}
|
2006-11-02 15:50:18 -05:00
|
|
|
|
|
|
|
#else /* defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT) */
|
|
|
|
|
2004-11-22 17:26:46 -05:00
|
|
|
int test(char *URL)
|
|
|
|
{
|
|
|
|
(void)URL;
|
2004-11-23 04:50:16 -05:00
|
|
|
printf("system lacks necessary system function(s)");
|
2006-11-02 15:50:18 -05:00
|
|
|
return 1; /* skip test */
|
2004-11-22 17:26:46 -05:00
|
|
|
}
|
2006-11-02 15:50:18 -05:00
|
|
|
|
|
|
|
#endif /* defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT) */
|