|
From: Jeremy F. <je...@go...> - 2003-12-18 02:39:53
|
CVS commit by fitzhardinge:
Patch from Tom Hughes: set VG_(max_fd) based on the current file
descriptor limit rather than assuming 1024.
M +6 -8 coregrind/vg_include.h 1.161
M +24 -1 coregrind/vg_main.c 1.131
M +13 -2 coregrind/vg_mylibc.c 1.61
M +4 -4 coregrind/vg_syscalls.c 1.70
M +3 -0 include/vg_skin.h.base 1.3
--- valgrind/coregrind/vg_include.h #1.160:1.161
@@ -109,12 +109,7 @@
#define VG_SCHEDULING_QUANTUM 50000
-/* Maximum FD Valgrind can use for its internal file descriptors. */
-#define VG_MAX_SAFE_FD 1024 /* usual ulimit */
-
-/* Maximum allowed application-visible file descriptor. Valgrind's
- internal fds hide above this (starting at VG_MAX_FD+1). This is
- derived from the default fd limit (1024) minus the 2 fds per thread
- and a small number of extra fds. */
-#define VG_MAX_FD (VG_MAX_SAFE_FD - (VG_N_THREADS*2 + 4))
+/* Number of file descriptors that Valgrind tries to reserve for
+ it's own use - two per thread plues a small number of extras. */
+#define VG_N_RESERVED_FDS (VG_N_THREADS*2 + 4)
/* Stack size for a thread. We try and check that they do not go
@@ -183,4 +178,7 @@ extern Int VG_(main_pid);
extern Int VG_(main_pgrp);
+/* Maximum allowed application-visible file descriptor */
+extern Int VG_(max_fd);
+
/* Should we stop collecting errors if too many appear? default: YES */
extern Bool VG_(clo_error_limit);
--- valgrind/coregrind/vg_main.c #1.130:1.131
@@ -154,4 +154,7 @@ Int VG_(main_pid);
Int VG_(main_pgrp);
+/* Maximum allowed application-visible file descriptor */
+Int VG_(max_fd);
+
/* Words. */
static Int baB_off = 0;
@@ -1110,5 +1113,5 @@ static void process_cmd_line_options ( c
/* Move logfile_fd into the safe range, so it doesn't conflict with any app fds */
- eventually_logfile_fd = VG_(fcntl)(VG_(clo_logfile_fd), VKI_F_DUPFD, VG_MAX_FD+1);
+ eventually_logfile_fd = VG_(fcntl)(VG_(clo_logfile_fd), VKI_F_DUPFD, VG_(max_fd)+1);
if (eventually_logfile_fd < 0)
VG_(message)(Vg_UserMsg, "valgrind: failed to move logfile fd into safe range");
@@ -1365,4 +1368,5 @@ void VG_(main) ( const KickstartParams *
{
VgSchedReturnCode src;
+ struct vki_rlimit rl;
/* initial state */
@@ -1415,4 +1419,23 @@ void VG_(main) ( const KickstartParams *
newpid(VG_INVALID_THREADID);
+ /* Get the current file descriptor limits. */
+ if (VG_(getrlimit)(VKI_RLIMIT_NOFILE, &rl) < 0) {
+ rl.rlim_cur = 1024;
+ rl.rlim_max = 1024;
+ }
+
+ /* Work out where to move the soft limit to. */
+ if (rl.rlim_cur + VG_N_RESERVED_FDS <= rl.rlim_max) {
+ rl.rlim_cur = rl.rlim_cur + VG_N_RESERVED_FDS;
+ } else {
+ rl.rlim_cur = rl.rlim_max;
+ }
+
+ /* Reserve some file descriptors for our use. */
+ VG_(max_fd) = rl.rlim_cur - VG_N_RESERVED_FDS;
+
+ /* Update the soft limit. */
+ VG_(setrlimit)(VKI_RLIMIT_NOFILE, &rl);
+
/* Read /proc/self/maps into a buffer. Must be before:
- SK_(pre_clo_init)(): so that if it calls VG_(malloc)(), any mmap'd
--- valgrind/coregrind/vg_mylibc.c #1.60:1.61
@@ -1204,5 +1204,5 @@ Int VG_(safe_fd)(Int oldfd)
Int newfd;
- newfd = VG_(fcntl)(oldfd, VKI_F_DUPFD, VG_MAX_FD+1);
+ newfd = VG_(fcntl)(oldfd, VKI_F_DUPFD, VG_(max_fd)+1);
if (newfd != -1)
VG_(close)(oldfd);
@@ -1210,5 +1210,5 @@ Int VG_(safe_fd)(Int oldfd)
VG_(fcntl)(newfd, VKI_F_SETFD, VKI_FD_CLOEXEC);
- vg_assert(newfd > VG_MAX_FD);
+ vg_assert(newfd > VG_(max_fd));
return newfd;
}
@@ -1430,4 +1430,15 @@ Int VG_(getrlimit) (Int resource, struct
+/* Support for setrlimit. */
+Int VG_(setrlimit) (Int resource, struct vki_rlimit *rlim)
+{
+ Int res;
+ /* res = setrlimit( resource, rlim ); */
+ res = VG_(do_syscall)(__NR_setrlimit, (UInt)resource, (UInt)rlim);
+ if(VG_(is_kerror)(res)) res = -1;
+ return res;
+}
+
+
/* Support for getdents. */
Int VG_(getdents) (UInt fd, struct vki_dirent *dirp, UInt count)
--- valgrind/coregrind/vg_syscalls.c #1.69:1.70
@@ -312,5 +312,5 @@ void record_fd_close(Int tid, Int fd)
OpenFd *i = allocated_fds;
- if (fd > VG_MAX_FD)
+ if (fd > VG_(max_fd))
return; /* Valgrind internal */
@@ -345,5 +345,5 @@ void record_fd_open(Int tid, Int fd, cha
OpenFd *i;
- if (fd > VG_MAX_FD)
+ if (fd > VG_(max_fd))
return; /* Valgrind internal */
@@ -890,5 +890,5 @@ static Bool valid_client_addr(Addr start
static Bool fd_allowed(Int fd, const Char *syscall, ThreadId tid)
{
- if (fd < 0 || fd > VG_MAX_FD || fd == VG_(clo_logfile_fd)) {
+ if (fd < 0 || fd > VG_(max_fd) || fd == VG_(clo_logfile_fd)) {
VG_(message)(Vg_UserMsg,
"Warning: invalid file descriptor %d in syscall %s()",
@@ -4156,5 +4156,5 @@ POST(socketcall)
case SYS_SOCKETPAIR:
- /* XXX TODO: check return fd against VG_MAX_FD */
+ /* XXX TODO: check return fd against VG_(max_fd) */
VG_TRACK( post_mem_write, ((UInt*)arg2)[3], 2*sizeof(int) );
if(VG_(clo_track_fds)) {
--- valgrind/include/vg_skin.h.base #1.2:1.3
@@ -397,4 +397,7 @@
extern Int VG_(getrlimit) ( Int resource, struct vki_rlimit *rlim );
+/* Set client resource limit*/
+extern Int VG_(setrlimit) ( Int resource, struct vki_rlimit *rlim );
+
/* Crude stand-in for the glibc system() call. */
extern Int VG_(system) ( Char* cmd );
|