some requests trapped and handled by valgrind.
Patch from Matthias Schwarzott via BZ#294047.
git-svn-id: svn://svn.valgrind.org/valgrind/trunk@12411
arg1 &= ~_RLIMIT_POSIX_FLAG;
#endif
- if (arg1 == VKI_RLIMIT_NOFILE) {
+ if (ARG2 &&
+ ((struct vki_rlimit *)ARG2)->rlim_cur > ((struct vki_rlimit *)ARG2)->rlim_max) {
+ SET_STATUS_Failure( VKI_EINVAL );
+ }
+ else if (arg1 == VKI_RLIMIT_NOFILE) {
if (((struct vki_rlimit *)ARG2)->rlim_cur > VG_(fd_hard_limit) ||
((struct vki_rlimit *)ARG2)->rlim_max != VG_(fd_hard_limit)) {
SET_STATUS_Failure( VKI_EPERM );
PRE_MEM_READ( "rlimit64(new_rlim)", ARG3, sizeof(struct vki_rlimit64) );
if (ARG4)
PRE_MEM_WRITE( "rlimit64(old_rlim)", ARG4, sizeof(struct vki_rlimit64) );
-}
-
-POST(sys_prlimit64)
-{
- if (ARG4) {
- POST_MEM_WRITE( ARG4, sizeof(struct vki_rlimit64) );
+ if (ARG3 &&
+ ((struct vki_rlimit64 *)ARG3)->rlim_cur > ((struct vki_rlimit64 *)ARG3)->rlim_max) {
+ SET_STATUS_Failure( VKI_EINVAL );
+ }
+ else if (ARG1 == 0 || ARG1 == VG_(getpid)()) {
switch (ARG2) {
case VKI_RLIMIT_NOFILE:
- ((struct vki_rlimit64 *)ARG4)->rlim_cur = VG_(fd_soft_limit);
- ((struct vki_rlimit64 *)ARG4)->rlim_max = VG_(fd_hard_limit);
+ SET_STATUS_Success( 0 );
+ if (ARG4) {
+ ((struct vki_rlimit64 *)ARG4)->rlim_cur = VG_(fd_soft_limit);
+ ((struct vki_rlimit64 *)ARG4)->rlim_max = VG_(fd_hard_limit);
+ }
+ if (ARG3) {
+ if (((struct vki_rlimit64 *)ARG3)->rlim_cur > VG_(fd_hard_limit) ||
+ ((struct vki_rlimit64 *)ARG3)->rlim_max != VG_(fd_hard_limit)) {
+ SET_STATUS_Failure( VKI_EPERM );
+ }
+ else {
+ VG_(fd_soft_limit) = ((struct vki_rlimit64 *)ARG3)->rlim_cur;
+ }
+ }
break;
case VKI_RLIMIT_DATA:
- ((struct vki_rlimit64 *)ARG4)->rlim_cur = VG_(client_rlimit_data).rlim_cur;
- ((struct vki_rlimit64 *)ARG4)->rlim_max = VG_(client_rlimit_data).rlim_max;
+ SET_STATUS_Success( 0 );
+ if (ARG4) {
+ ((struct vki_rlimit64 *)ARG4)->rlim_cur = VG_(client_rlimit_data).rlim_cur;
+ ((struct vki_rlimit64 *)ARG4)->rlim_max = VG_(client_rlimit_data).rlim_max;
+ }
+ if (ARG3) {
+ if (((struct vki_rlimit64 *)ARG3)->rlim_cur > VG_(client_rlimit_data).rlim_max ||
+ ((struct vki_rlimit64 *)ARG3)->rlim_max > VG_(client_rlimit_data).rlim_max) {
+ SET_STATUS_Failure( VKI_EPERM );
+ }
+ else {
+ VG_(client_rlimit_data).rlim_cur = ((struct vki_rlimit64 *)ARG3)->rlim_cur;
+ VG_(client_rlimit_data).rlim_max = ((struct vki_rlimit64 *)ARG3)->rlim_max;
+ }
+ }
break;
case VKI_RLIMIT_STACK:
- ((struct vki_rlimit64 *)ARG4)->rlim_cur = VG_(client_rlimit_stack).rlim_cur;
- ((struct vki_rlimit64 *)ARG4)->rlim_max = VG_(client_rlimit_stack).rlim_max;
+ SET_STATUS_Success( 0 );
+ if (ARG4) {
+ ((struct vki_rlimit64 *)ARG4)->rlim_cur = VG_(client_rlimit_stack).rlim_cur;
+ ((struct vki_rlimit64 *)ARG4)->rlim_max = VG_(client_rlimit_stack).rlim_max;
+ }
+ if (ARG3) {
+ if (((struct vki_rlimit64 *)ARG3)->rlim_cur > VG_(client_rlimit_stack).rlim_max ||
+ ((struct vki_rlimit64 *)ARG3)->rlim_max > VG_(client_rlimit_stack).rlim_max) {
+ SET_STATUS_Failure( VKI_EPERM );
+ }
+ else {
+ VG_(threads)[tid].client_stack_szB = ((struct vki_rlimit64 *)ARG3)->rlim_cur;
+ VG_(client_rlimit_stack).rlim_cur = ((struct vki_rlimit64 *)ARG3)->rlim_cur;
+ VG_(client_rlimit_stack).rlim_max = ((struct vki_rlimit64 *)ARG3)->rlim_max;
+ }
+ }
break;
}
}
}
+POST(sys_prlimit64)
+{
+ if (ARG4)
+ POST_MEM_WRITE( ARG4, sizeof(struct vki_rlimit64) );
+}
+
/* ---------------------------------------------------------------------
tid-related wrappers
------------------------------------------------------------------ */
res_search.stderr.exp res_search.stdout.exp res_search.vgtest \
resolv.stderr.exp resolv.stdout.exp resolv.vgtest \
rlimit_nofile.stderr.exp rlimit_nofile.stdout.exp rlimit_nofile.vgtest \
+ rlimit64_nofile.stderr.exp rlimit64_nofile.stdout.exp rlimit64_nofile.vgtest \
selfrun.stderr.exp selfrun.stdout.exp selfrun.vgtest \
sem.stderr.exp sem.stdout.exp sem.vgtest \
semlimit.stderr.exp semlimit.stdout.exp semlimit.vgtest \
rcrl readline1 \
require-text-symbol \
res_search resolv \
- rlimit_nofile selfrun sem semlimit sha1_test \
+ rlimit_nofile rlimit64_nofile selfrun sem semlimit sha1_test \
shortpush shorts stackgrowth sigstackgrowth \
syscall-restart1 syscall-restart2 \
syslog \
--- /dev/null
+#define _LARGEFILE_SOURCE
+#define _LARGEFILE64_SOURCE
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/resource.h>
+#include <unistd.h>
+#include "fdleak.h"
+
+int main(int argc, char **argv)
+{
+ struct rlimit64 oldrlim;
+ struct rlimit64 newrlim;
+ int fd;
+
+ CLOSE_INHERITED_FDS;
+
+ if (getrlimit64(RLIMIT_NOFILE, &oldrlim) < 0)
+ {
+ perror("getrlimit");
+ exit(1);
+ }
+
+ newrlim.rlim_cur = oldrlim.rlim_max+1;
+ newrlim.rlim_max = oldrlim.rlim_max;
+ if (setrlimit64(RLIMIT_NOFILE, &newrlim) == -1)
+ {
+ if (errno != EINVAL) {
+ fprintf(stderr, "setrlimit64 exceeding hardlimit must set errno=EINVAL\n");
+ exit(1);
+ }
+ }
+ else
+ {
+ fprintf(stderr, "setrlimit64 exceeding hardlimit must return -1\n");
+ exit(1);
+ }
+
+ newrlim.rlim_cur = oldrlim.rlim_max;
+ newrlim.rlim_max = oldrlim.rlim_max+1;
+ if (setrlimit64(RLIMIT_NOFILE, &newrlim) == -1)
+ {
+ if (errno != EPERM) {
+ fprintf(stderr, "setrlimit64 changing hardlimit must set errno=EPERM\n");
+ exit(1);
+ }
+ }
+ else
+ {
+ fprintf(stderr, "setrlimit64 changing hardlimit must return -1\n");
+ exit(1);
+ }
+
+ newrlim.rlim_cur = oldrlim.rlim_cur / 2;
+ newrlim.rlim_max = oldrlim.rlim_max;
+
+ if (setrlimit64(RLIMIT_NOFILE, &newrlim) < 0)
+ {
+ perror("setrlimit64");
+ exit(1);
+ }
+
+ if (getrlimit64(RLIMIT_NOFILE, &newrlim) < 0)
+ {
+ perror("getrlimit");
+ exit(1);
+ }
+
+ if (newrlim.rlim_cur != oldrlim.rlim_cur / 2)
+ {
+ fprintf(stderr, "rlim_cur is %llu (should be %llu)\n",
+ (unsigned long long)newrlim.rlim_cur,
+ (unsigned long long)oldrlim.rlim_cur / 2);
+ }
+
+ if (newrlim.rlim_max != oldrlim.rlim_max)
+ {
+ fprintf(stderr, "rlim_max is %llu (should be %llu)\n",
+ (unsigned long long)newrlim.rlim_max,
+ (unsigned long long)oldrlim.rlim_max);
+ }
+
+ newrlim.rlim_cur -= 3; /* allow for stdin, stdout and stderr */
+
+ while (newrlim.rlim_cur-- > 0)
+ {
+ if (open("/dev/null", O_RDONLY) < 0)
+ {
+ perror("open");
+ }
+ }
+
+ if ((fd = open("/dev/null", O_RDONLY)) >= 0)
+ {
+ fprintf(stderr, "open succeeded with fd %d - it should have failed!\n", fd);
+ }
+ else if (errno != EMFILE)
+ {
+ perror("open");
+ }
+
+ exit(0);
+}
--- /dev/null
+prog: rlimit64_nofile
exit(1);
}
+ newrlim.rlim_cur = oldrlim.rlim_max+1;
+ newrlim.rlim_max = oldrlim.rlim_max;
+ if (setrlimit(RLIMIT_NOFILE, &newrlim) == -1)
+ {
+ if (errno != EINVAL) {
+ fprintf(stderr, "setrlimit exceeding hardlimit must set errno=EINVAL\n");
+ exit(1);
+ }
+ }
+ else
+ {
+ fprintf(stderr, "setrlimit exceeding hardlimit must return -1\n");
+ exit(1);
+ }
+
+ newrlim.rlim_cur = oldrlim.rlim_max;
+ newrlim.rlim_max = oldrlim.rlim_max+1;
+ if (setrlimit(RLIMIT_NOFILE, &newrlim) == -1)
+ {
+ if (errno != EPERM) {
+ fprintf(stderr, "setrlimit changing hardlimit must set errno=EPERM\n");
+ exit(1);
+ }
+ }
+ else
+ {
+ fprintf(stderr, "setrlimit changing hardlimit must return -1\n");
+ exit(1);
+ }
+
newrlim.rlim_cur = oldrlim.rlim_cur / 2;
newrlim.rlim_max = oldrlim.rlim_max;