Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions graphics/grMain.c
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,15 @@ grFgets(str, n, stream, name)
twentySecs.tv_sec = 20;
twentySecs.tv_usec = 0;

const int fd = fileno(stream);
ASSERT(fd >= 0 && fd < FD_SETSIZE, "fd>=0&&fd<FD_SETSIZE");
if (fd < 0 || fd >= FD_SETSIZE)
{
TxError("WARNING: grFgets(fd=%d) called with fd out of range 0..%d\n", fd, FD_SETSIZE-1);
return NULL; /* allowing things to continue is UB */
}
FD_ZERO(&fn);
FD_SET(fileno(stream), &fn);
FD_SET(fd, &fn);
newstr = str;
n--;
if (n < 0) return (char *) NULL;
Expand All @@ -470,13 +477,13 @@ grFgets(str, n, stream, name)
int sel;

f = fn;
sel = select(TX_MAX_OPEN_FILES, &f, (fd_set *) NULL, (fd_set *) NULL, &threeSec);
sel = select(fd + 1, &f, (fd_set *) NULL, (fd_set *) NULL, &threeSec);
if (sel == 0)
{
TxError("The %s is responding slowly, or not at all.\n", name);
TxError("I'll wait for 20 seconds and then give up.\n");
f = fn;
sel = select(TX_MAX_OPEN_FILES, &f, (fd_set *) NULL,
sel = select(fd + 1, &f, (fd_set *) NULL,
(fd_set *) NULL, &twentySecs);
if (sel == 0)
{
Expand Down
37 changes: 37 additions & 0 deletions scripts/configure
Original file line number Diff line number Diff line change
Expand Up @@ -4893,6 +4893,30 @@ fi
done


for ac_func in getrlimit
do :
ac_fn_c_check_func "$LINENO" "getrlimit" "ac_cv_func_getrlimit"
if test "x$ac_cv_func_getrlimit" = xyes; then :
cat >>confdefs.h <<_ACEOF
#define HAVE_GETRLIMIT 1
_ACEOF

fi
done


for ac_func in setrlimit
do :
ac_fn_c_check_func "$LINENO" "setrlimit" "ac_cv_func_setrlimit"
if test "x$ac_cv_func_setrlimit" = xyes; then :
cat >>confdefs.h <<_ACEOF
#define HAVE_SETRLIMIT 1
_ACEOF

fi
done


ac_fn_c_check_func "$LINENO" "vfork" "ac_cv_func_vfork"
if test "x$ac_cv_func_vfork" = xyes; then :

Expand Down Expand Up @@ -4964,6 +4988,19 @@ fi
done


for ac_header in sys/resource.h
do :
ac_fn_c_check_header_mongrel "$LINENO" "sys/resource.h" "ac_cv_header_sys_resource_h" "$ac_includes_default"
if test "x$ac_cv_header_sys_resource_h" = xyes; then :
cat >>confdefs.h <<_ACEOF
#define HAVE_SYS_RESOURCE_H 1
_ACEOF

fi

done


for ac_header in sys/time.h
do :
ac_fn_c_check_header_mongrel "$LINENO" "sys/time.h" "ac_cv_header_sys_time_h" "$ac_includes_default"
Expand Down
9 changes: 9 additions & 0 deletions scripts/configure.in
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ AC_HEADER_STDC
dnl Need either setenv or putenv
AC_CHECK_FUNCS(setenv putenv)

dnl Check for getrlimit
AC_CHECK_FUNCS(getrlimit)

dnl Check for setrlimit
AC_CHECK_FUNCS(setrlimit)

dnl Check for vfork
AC_CHECK_FUNC(vfork)

Expand All @@ -150,6 +156,9 @@ AC_CHECK_HEADERS(param.h)
dnl Check for <paths.h>
AC_CHECK_HEADERS(paths.h)

dnl Check for <sys/resource.h>
AC_CHECK_HEADERS(sys/resource.h)

dnl Check for <sys/time.h>
AC_CHECK_HEADERS(sys/time.h)

Expand Down
7 changes: 7 additions & 0 deletions sim/SimRsim.c
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,13 @@ SimFillBuffer(buffHead, pLastChar, charCount)
FD_ZERO(&exceptfds);
#endif /* SYSV */

ASSERT(pipeIn >= 0 && pipeIn < FD_SETSIZE, "pipeIn>=0&&pipeIn<FD_SETSIZE");
if (pipeIn < 0 || pipeIn >= FD_SETSIZE)
{
TxError("WARNING: SimFillBuffer(fd=%d) called with fd out of range 0..%d\n", pipeIn, FD_SETSIZE-1);
return -1; /* allowing things to continue is UB */
}

nfd = pipeIn + 1;

try_again:
Expand Down
60 changes: 60 additions & 0 deletions tcltk/tclmagic.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
#include <signal.h>
#include <string.h>
#include <errno.h>
#ifdef HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif

#include "tcltk/tclmagic.h"
#include "utils/main.h"
Expand Down Expand Up @@ -530,6 +533,61 @@ MakeWindowCommand(char *wname, MagWindow *mw)
freeMagic(tclcmdstr);
}

#ifdef HAVE_SETRLIMIT
static int
process_rlimit_nofile_ensure(rlim_t nofile)
{
struct rlimit rlim;
int err = getrlimit(RLIMIT_NOFILE, &rlim);
if (err < 0)
return err;
rlim_t rlim_cur = rlim.rlim_cur;
/* nofile != RLIM_INFINITY && rlim.rlim_max != RLIM_INFINITY */
if (nofile > rlim.rlim_max && nofile != rlim.rlim_max)
return -1;
if (rlim.rlim_cur < nofile || nofile == RLIM_INFINITY)
{
rlim.rlim_cur = nofile;
err = setrlimit(RLIMIT_NOFILE, &rlim);
}
if (err != 0)
TxPrintf("WARNING: process_rlimit_nofile_ensure(%lu) = %d (%d) [rlim_cur=%lu rlim_max=%lu]\n", nofile, err, errno, rlim_cur, rlim.rlim_max);
return err;
}
#endif /* HAVE_SETRLIMIT */

/* this function encapsulates the default policy on startup */
static int
process_rlimit_startup_check(void)
{
#ifdef HAVE_GETRLIMIT
#if TCL_MAJOR_VERSION < 9
/* TCL8 has select() support and no support for poll/epoll for the main event loop */
struct rlimit rlim;
int err = getrlimit(RLIMIT_NOFILE, &rlim);
if (err < 0)
return err;
if (rlim.rlim_cur > FD_SETSIZE)
{
TxPrintf("WARNING: RLIMIT_NOFILE is above %d and Tcl_Version<9 this may cause runtime issues [rlim_cur=%lu]\n", FD_SETSIZE, rlim.rlim_cur);
return -1;
}
return 0;
#else
#ifdef HAVE_SETRLIMIT
/* TCL9 has poll/epoll support for the main event loop,
* ifdef due to rlim_t type availbility
*/
return process_rlimit_nofile_ensure(4096);
#else
return -1;
#endif /* HAVE_SETRLIMIT */
#endif /* TCL_MAJOR_VERSION < 9 */
#else
return -1;
#endif /* HAVE_GETRLIMIT */
}

/*------------------------------------------------------*/
/* Main startup procedure */
/*------------------------------------------------------*/
Expand Down Expand Up @@ -588,6 +646,8 @@ _magic_initialize(ClientData clientData,
TxPrintf("Using the terminal as the console.\n");
TxFlushOut();

process_rlimit_startup_check();

if (mainInitAfterArgs() != 0) goto magicfatal;

/* Registration of commands is performed after calling the */
Expand Down
2 changes: 0 additions & 2 deletions textio/textio.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,4 @@ extern void TxInit(void);
extern void TxInitReadline(void);
#endif

#define TX_MAX_OPEN_FILES 20

#endif /* _TEXTIO_H */
Loading
Loading