From 675702d121650be7ff428922b0d42b3c70ee6308 Mon Sep 17 00:00:00 2001 From: akr Date: Wed, 14 Jun 2006 14:10:39 +0000 Subject: * configure.in: check sizeof(rlim_t). check setrlimit. * process.c (proc_getrlimit): new method Process.getrlimit. (proc_setrlimit): new method Process.setrlimit. * ruby.h (NUM2ULL): new macro. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@10262 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 10 ++++ configure.in | 23 ++++++++- process.c | 160 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ruby.h | 1 + 4 files changed, 193 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index b26b0f0524..6cf270df86 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +Wed Jun 14 23:03:53 2006 Tanaka Akira + + * configure.in: check sizeof(rlim_t). + check setrlimit. + + * process.c (proc_getrlimit): new method Process.getrlimit. + (proc_setrlimit): new method Process.setrlimit. + + * ruby.h (NUM2ULL): new macro. + Mon Jun 12 22:25:09 2006 Yukihiro Matsumoto * sprintf.c (rb_f_sprintf): adjust precision length to prevent diff --git a/configure.in b/configure.in index 1ceacbb5c5..1c7e7bcace 100644 --- a/configure.in +++ b/configure.in @@ -373,7 +373,10 @@ os2-emx*) LIBS="-lm $LIBS" msdosdjgpp*) LIBS="-lm $LIBS" ac_cv_func_getpgrp_void=yes ac_cv_func_setitimer=no + ac_cv_sizeof_rlim_t=4 ;; +bsdi*) LIBS="-lm $LIBS" + ac_cv_sizeof_rlim_t=8;; freebsd*) LIBS="-lm $LIBS" AC_CACHE_CHECK([whether -lxpg4 has to be linked], rb_cv_lib_xpg4_needed, @@ -439,6 +442,23 @@ AC_CHECK_HEADERS(stdlib.h string.h unistd.h limits.h sys/file.h sys/ioctl.h sys/ sys/mkdev.h sys/utime.h netinet/in_systm.h float.h ieeefp.h pthread.h \ ucontext.h intrinsics.h) +dnl Check additional types. +AC_CHECK_SIZEOF(rlim_t, 0, [ + #ifdef HAVE_SYS_TYPES_H + # include + #endif + #ifdef HAVE_SYS_TIME_H + # include + #endif + #ifdef HAVE_SYS_RESOURCE_H + # include + #endif + #ifdef HAVE_UNISTD_H + # include + #endif + #include +]) + dnl Checks for typedefs, structures, and compiler characteristics. AC_TYPE_SIZE_T AC_STRUCT_ST_BLKSIZE @@ -460,7 +480,8 @@ AC_CHECK_FUNCS(fmod killpg wait4 waitpid syscall chroot fsync getcwd eaccess\ readlink setitimer setruid seteuid setreuid setresuid\ setproctitle setrgid setegid setregid setresgid issetugid pause\ lchown lchmod getpgrp setpgrp getpgid setpgid initgroups\ - getgroups setgroups getpriority getrlimit dlopen sigprocmask\ + getgroups setgroups getpriority getrlimit setrlimit\ + dlopen sigprocmask\ sigaction _setjmp setsid telldir seekdir fchmod mktime timegm\ cosh sinh tanh setuid setgid setenv unsetenv) AC_ARG_ENABLE(setreuid, diff --git a/process.c b/process.c index abdba730b1..ad51cee8a3 100644 --- a/process.c +++ b/process.c @@ -1818,6 +1818,115 @@ proc_setpriority(obj, which, who, prio) #endif } +#if SIZEOF_RLIM_T == SIZEOF_INT +# define RLIM2NUM(v) UINT2NUM(v) +# define NUM2RLIM(v) NUM2UINT(v) +#elif SIZEOF_RLIM_T == SIZEOF_LONG +# define RLIM2NUM(v) ULONG2NUM(v) +# define NUM2RLIM(v) NUM2ULONG(v) +#elif SIZEOF_RLIM_T == SIZEOF_LONG_LONG +# define RLIM2NUM(v) ULL2NUM(v) +# define NUM2RLIM(v) NUM2ULL(v) +#endif + +/* + * call-seq: + * Process.getrlimit(resource) => [cur_limit, max_limit] + * + * Gets the resource limit of the process. + * _cur_limit_ means current (soft) limit and + * _max_limit_ means maximum (hard) limit. + * + * _resource_ indicates the kind of resource to limit: + * such as Process::RLIMIT_CORE, + * Process::RLIMIT_CPU, etc. + * See Process.setrlimit for details. + * + * _cur_limit_ and _max_limit_ may be Process::RLIM_INFINITY, + * Process::RLIM_SAVED_MAX or + * Process::RLIM_SAVED_CUR. + * See Process.setrlimit and the system getrlimit(2) manual for details. + */ + +static VALUE +proc_getrlimit(VALUE obj, VALUE resource) +{ +#if defined(HAVE_GETRLIMIT) && defined(RLIM2NUM) + struct rlimit rlim; + + rb_secure(2); + + if (getrlimit(NUM2INT(resource), &rlim) < 0) { + rb_sys_fail("getrlimit"); + } + return rb_assoc_new(RLIM2NUM(rlim.rlim_cur), RLIM2NUM(rlim.rlim_max)); +#else + rb_notimplement(); +#endif +} + +/* + * call-seq: + * Process.setrlimit(resource, cur_limit, max_limit) => nil + * Process.setrlimit(resource, cur_limit) => nil + * + * Sets the resource limit of the process. + * _cur_limit_ means current (soft) limit and + * _max_limit_ means maximum (hard) limit. + * + * If _max_limit_ is not given, _cur_limit_ is used. + * + * _resource_ indicates the kind of resource to limit. + * The list of resources are OS dependent. + * Ruby may support following resources. + * + * [Process::RLIMIT_CORE] core size (bytes) (SUSv3) + * [Process::RLIMIT_CPU] CPU time (seconds) (SUSv3) + * [Process::RLIMIT_DATA] data segment (bytes) (SUSv3) + * [Process::RLIMIT_FSIZE] file size (bytes) (SUSv3) + * [Process::RLIMIT_NOFILE] file descriptors (number) (SUSv3) + * [Process::RLIMIT_STACK] stack size (bytes) (SUSv3) + * [Process::RLIMIT_AS] total available memory (bytes) (SUSv3, NetBSD, FreeBSD, OpenBSD but 4.4BSD-Lite) + * [Process::RLIMIT_MEMLOCK] total size for mlock(2) (bytes) (4.4BSD, GNU/Linux) + * [Process::RLIMIT_NPROC] number of processes for the user (number) (4.4BSD, GNU/Linux) + * [Process::RLIMIT_RSS] resident memory size (bytes) (4.2BSD, GNU/Linux) + * [Process::RLIMIT_SBSIZE] all socket buffers (bytes) (NetBSD, FreeBSD) + * + * Other Process::RLIMIT_??? constants may be defined. + * + * _cur_limit_ and _max_limit_ may be Process::RLIM_INFINITY, + * which means that the resource is not limited. + * They may be Process::RLIM_SAVED_MAX or + * Process::RLIM_SAVED_CUR too. + * See system setrlimit(2) manual for details. + * + */ + +static VALUE +proc_setrlimit(int argc, VALUE *argv, VALUE obj) +{ +#if defined(HAVE_SETRLIMIT) && defined(NUM2RLIM) + VALUE resource, rlim_cur, rlim_max; + struct rlimit rlim; + + rb_secure(2); + + rb_scan_args(argc, argv, "21", &resource, &rlim_cur, &rlim_max); + if (rlim_max == Qnil) + rlim_max = rlim_cur; + + rlim.rlim_cur = NUM2RLIM(rlim_cur); + rlim.rlim_max = NUM2RLIM(rlim_max); + + if (setrlimit(NUM2INT(resource), &rlim) < 0) { + rb_sys_fail("setrlimit"); + } + return Qnil; +#else + rb_notimplement(); +#endif +} + static int under_uid_switch = 0; static void check_uid_switch() @@ -3435,6 +3544,57 @@ Init_process() rb_define_const(rb_mProcess, "PRIO_USER", INT2FIX(PRIO_USER)); #endif +#ifdef HAVE_GETRLIMIT + rb_define_module_function(rb_mProcess, "getrlimit", proc_getrlimit, 1); +#endif +#ifdef HAVE_SETRLIMIT + rb_define_module_function(rb_mProcess, "setrlimit", proc_setrlimit, -1); +#endif +#ifdef RLIM2NUM +#ifdef RLIM_INFINITY + rb_define_const(rb_mProcess, "RLIM_INFINITY", RLIM2NUM(RLIM_INFINITY)); +#endif +#ifdef RLIM_SAVED_MAX + rb_define_const(rb_mProcess, "RLIM_SAVED_MAX", RLIM2NUM(RLIM_SAVED_MAX)); +#endif +#ifdef RLIM_SAVED_CUR + rb_define_const(rb_mProcess, "RLIM_SAVED_CUR", RLIM2NUM(RLIM_SAVED_CUR)); +#endif +#ifdef RLIMIT_CORE + rb_define_const(rb_mProcess, "RLIMIT_CORE", INT2FIX(RLIMIT_CORE)); +#endif +#ifdef RLIMIT_CPU + rb_define_const(rb_mProcess, "RLIMIT_CPU", INT2FIX(RLIMIT_CPU)); +#endif +#ifdef RLIMIT_DATA + rb_define_const(rb_mProcess, "RLIMIT_DATA", INT2FIX(RLIMIT_DATA)); +#endif +#ifdef RLIMIT_FSIZE + rb_define_const(rb_mProcess, "RLIMIT_FSIZE", INT2FIX(RLIMIT_FSIZE)); +#endif +#ifdef RLIMIT_NOFILE + rb_define_const(rb_mProcess, "RLIMIT_NOFILE", INT2FIX(RLIMIT_NOFILE)); +#endif +#ifdef RLIMIT_STACK + rb_define_const(rb_mProcess, "RLIMIT_STACK", INT2FIX(RLIMIT_STACK)); +#endif +#ifdef RLIMIT_AS + rb_define_const(rb_mProcess, "RLIMIT_AS", INT2FIX(RLIMIT_AS)); +#endif +#ifdef RLIMIT_MEMLOCK + rb_define_const(rb_mProcess, "RLIMIT_MEMLOCK", INT2FIX(RLIMIT_MEMLOCK)); +#endif +#ifdef RLIMIT_NPROC + rb_define_const(rb_mProcess, "RLIMIT_NPROC", INT2FIX(RLIMIT_NPROC)); +#endif +#ifdef RLIMIT_RSS + rb_define_const(rb_mProcess, "RLIMIT_RSS", INT2FIX(RLIMIT_RSS)); +#endif +#ifdef RLIMIT_SBSIZE + rb_define_const(rb_mProcess, "RLIMIT_SBSIZE", INT2FIX(RLIMIT_SBSIZE)); +#endif +#endif + rb_define_module_function(rb_mProcess, "uid", proc_getuid, 0); rb_define_module_function(rb_mProcess, "uid=", proc_setuid, 1); rb_define_module_function(rb_mProcess, "gid", proc_getgid, 0); diff --git a/ruby.h b/ruby.h index dcb38f4a71..c23a60daa8 100644 --- a/ruby.h +++ b/ruby.h @@ -269,6 +269,7 @@ unsigned long rb_fix2uint _((VALUE)); LONG_LONG rb_num2ll _((VALUE)); unsigned LONG_LONG rb_num2ull _((VALUE)); # define NUM2LL(x) (FIXNUM_P(x)?FIX2LONG(x):rb_num2ll((VALUE)x)) +# define NUM2ULL(x) rb_num2ull((VALUE)x) #endif #if HAVE_LONG_LONG && SIZEOF_OFF_T > SIZEOF_LONG -- cgit v1.2.3