# Note that I've hardcoded things like EPERM,EFAULT,EINVAL from sys/errno.h, # SYS_setrlimit from sys/syscall.h, and RLIMIT_CPU from sys/resource.h # it might be preferable to h2ph the respective .h files... sub set_max_cpu_time { my $n_seconds = shift; my $s = pack( 'LL', $n_seconds, $n_seconds+1 ); $! = 0; if ( syscall( 128, 0, $s ) == -1 ) # SYS_setrlimit, RLIMIT_CPU { if ( $! == 1 ) # EPERM { die "$!; Sorry, you need to be superuser to do that.\n"; } elsif ( $! == 14 ) # EFAULT { die "$!; Error: argument to setrlimit pointed to an illegal address.\n"; } elsif ( $! == 22 ) # EINVAL { die "$!; Error: the new rlim_cur exceeds the new rlim_max, or the limit specified cannot be lowered because current usage is already higher than the limit.\n"; } } } # test it: set_max_cpu_time( 8 ); my $n = 0; while ( 1 ) { $n++ } # Hmm, mysteriously dumps core after about 8 seconds! :-)